summaryrefslogtreecommitdiff
path: root/activitypub.c
diff options
context:
space:
mode:
authorGravatar grunfink2025-09-03 06:08:13 +0200
committerGravatar grunfink2025-09-03 06:08:13 +0200
commit59f3b5465ecca02cb90310fd1dbc46456d6a4908 (patch)
tree6f62214612ef15d9640e5094715b01739d4b0d89 /activitypub.c
parentVersion 2.82 RELEASED. (diff)
downloadsnac2-59f3b5465ecca02cb90310fd1dbc46456d6a4908.tar.gz
snac2-59f3b5465ecca02cb90310fd1dbc46456d6a4908.tar.xz
snac2-59f3b5465ecca02cb90310fd1dbc46456d6a4908.zip
New function collect_outbox().
Diffstat (limited to 'activitypub.c')
-rw-r--r--activitypub.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/activitypub.c b/activitypub.c
index 09da229..b9ef86b 100644
--- a/activitypub.c
+++ b/activitypub.c
@@ -1021,6 +1021,107 @@ void collect_replies(snac *user, const char *id)
1021} 1021}
1022 1022
1023 1023
1024void collect_outbox(snac *user, const char *actor_id)
1025/* gets an actor's outbox and inserts a bunch of posts in a user's timeline */
1026{
1027 int status;
1028 xs *actor = NULL;
1029
1030 if (!valid_status(status = actor_request(user, actor_id, &actor))) {
1031 snac_debug(user, 1, xs_fmt("collect_outbox: cannot get actor object '%s' %d", actor_id, status));
1032 return;
1033 }
1034
1035 xs *outbox = NULL;
1036 const char *outbox_url = xs_dict_get(actor, "outbox");
1037
1038 if (!xs_is_string(outbox_url))
1039 return;
1040
1041 if (!valid_status(status = activitypub_request(user, outbox_url, &outbox))) {
1042 snac_debug(user, 1, xs_fmt("collect_outbox: cannot get actor outbox '%s' %d", outbox_url, status));
1043 return;
1044 }
1045
1046 const xs_list *ordered_items = xs_dict_get(outbox, "orderedItems");
1047
1048 if (!xs_is_list(ordered_items)) {
1049 /* the list is not here; does it have a 'first'? */
1050 const char *first = xs_dict_get(outbox, "first");
1051
1052 if (xs_is_string(first)) {
1053 /* download this instead */
1054 xs *first2 = xs_dup(first);
1055 xs_free(outbox);
1056
1057 if (!valid_status(status = activitypub_request(user, first2, &outbox))) {
1058 snac_debug(user, 1, xs_fmt("collect_outbox: cannot get first page of outbox '%s' %d", first2, status));
1059 return;
1060 }
1061
1062 /* last chance */
1063 ordered_items = xs_dict_get(outbox, "orderedItems");
1064 }
1065 }
1066
1067 if (!xs_is_list(ordered_items)) {
1068 snac_debug(user, 1, xs_fmt("collect_outbox: cannot get list of posts for actor '%s' outbox", actor_id));
1069 return;
1070 }
1071
1072 /* well, ok, then */
1073 int max = 4;
1074 const xs_val *v;
1075
1076 xs_list_foreach(ordered_items, v) {
1077 if (max == 0)
1078 break;
1079
1080 xs *post = NULL;
1081
1082 if (xs_is_string(v)) {
1083 /* it's probably the post url */
1084 if (!valid_status(activitypub_request(user, v, &post)))
1085 continue;
1086 }
1087 else
1088 if (xs_is_dict(v))
1089 post = xs_dup(v);
1090
1091 if (post == NULL)
1092 continue;
1093
1094 const char *type = xs_dict_get(post, "type");
1095
1096 if (!xs_is_string(type) || strcmp(type, "Create")) {
1097 /* not a post */
1098 continue;
1099 }
1100
1101 const xs_dict *object = xs_dict_get(post, "object");
1102
1103 if (!xs_is_dict(object))
1104 continue;
1105
1106 type = xs_dict_get(object, "type");
1107 const char *id = xs_dict_get(object, "id");
1108 const char *attr_to = get_atto(object);
1109
1110 if (!xs_is_string(type) || !xs_is_string(id) || !xs_is_string(attr_to))
1111 continue;
1112
1113 if (!timeline_here(user, id)) {
1114 timeline_add(user, id, object);
1115 snac_log(user, xs_fmt("new '%s' (collect_outbox) %s %s", type, attr_to, id));
1116 }
1117 else
1118 snac_debug(user, 1, xs_fmt("collect_outbox: post '%s' already here", id));
1119
1120 max--;
1121 }
1122}
1123
1124
1024void notify(snac *snac, const char *type, const char *utype, const char *actor, const xs_dict *msg) 1125void notify(snac *snac, const char *type, const char *utype, const char *actor, const xs_dict *msg)
1025/* notifies the user of relevant events */ 1126/* notifies the user of relevant events */
1026{ 1127{