commit 51208b1
default
·
2023-04-13 15:56:00 +0000 UTC
parent 1a27e67
Implemented mastoapi notifications.
2 files changed,
+55,
-4
M
data.c
M
data.c
+3,
-1
1@@ -1525,9 +1525,11 @@ void notify_add(snac *snac, const char *type, const char *utype,
2 noti = xs_dict_append(noti, "type", type);
3 noti = xs_dict_append(noti, "utype", utype);
4 noti = xs_dict_append(noti, "actor", actor);
5- noti = xs_dict_append(noti, "objid", objid);
6 noti = xs_dict_append(noti, "date", date);
7
8+ if (!xs_is_null(objid))
9+ noti = xs_dict_append(noti, "objid", objid);
10+
11 if ((f = fopen(fn, "w")) != NULL) {
12 xs *j = xs_json_dumps_pp(noti, 4);
13
+52,
-3
1@@ -854,11 +854,60 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
2 }
3 else
4 if (strcmp(cmd, "/notifications") == 0) {
5- /* TBD */
6 if (logged_in) {
7- xs *l = notify_list(&snac1, 0);
8+ xs *l = notify_list(&snac1, 0);
9+ xs *out = xs_list_new();
10+ xs_list *p = l;
11+ xs_dict *v;
12+
13+ while (xs_list_iter(&p, &v)) {
14+ const char *type = xs_dict_get(v, "type");
15+ const char *objid = xs_dict_get(v, "objid");
16+ xs *actor = NULL;
17+ xs *entry = NULL;
18+
19+ if (!valid_status(object_get(xs_dict_get(v, "actor"), &actor)))
20+ continue;
21
22- *body = xs_dup("[]");
23+ if (objid != NULL && !valid_status(object_get(objid, &entry)))
24+ continue;
25+
26+ /* convert the type */
27+ if (strcmp(type, "Like") == 0)
28+ type = "favourite";
29+ else
30+ if (strcmp(type, "Announce") == 0)
31+ type = "reblog";
32+ else
33+ if (strcmp(type, "Follow") == 0)
34+ type = "follow";
35+ else
36+ if (strcmp(type, "Create") == 0)
37+ type = "mention";
38+ else
39+ continue;
40+
41+ xs *mn = xs_dict_new();
42+
43+ mn = xs_dict_append(mn, "type", type);
44+
45+ xs *id = xs_replace(xs_dict_get(v, "id"), ".", "");
46+ mn = xs_dict_append(mn, "id", id);
47+
48+ mn = xs_dict_append(mn, "created_at", xs_dict_get(v, "date"));
49+
50+ xs *acct = mastoapi_account(actor);
51+ mn = xs_dict_append(mn, "account", acct);
52+
53+ if (objid != NULL) {
54+ xs *st = mastoapi_status(&snac1, entry);
55+ mn = xs_dict_append(mn, "status", st);
56+ }
57+
58+ out = xs_list_append(out, mn);
59+ }
60+
61+ *body = xs_json_dumps_pp(out, 4);
62 *ctype = "application/json";
63 status = 200;
64 }