Quellcode durchsuchen

Implemented mastoapi notifications.

default vor 3 Jahren
Ursprung
Commit
51208b10c1
2 geänderte Dateien mit 55 neuen und 4 gelöschten Zeilen
  1. 3 1
      data.c
  2. 52 3
      mastoapi.c

+ 3 - 1
data.c

@@ -1525,9 +1525,11 @@ void notify_add(snac *snac, const char *type, const char *utype,
     noti = xs_dict_append(noti, "type",  type);
     noti = xs_dict_append(noti, "utype", utype);
     noti = xs_dict_append(noti, "actor", actor);
-    noti = xs_dict_append(noti, "objid", objid);
     noti = xs_dict_append(noti, "date",  date);
 
+    if (!xs_is_null(objid))
+        noti = xs_dict_append(noti, "objid", objid);
+
     if ((f = fopen(fn, "w")) != NULL) {
         xs *j = xs_json_dumps_pp(noti, 4);
 

+ 52 - 3
mastoapi.c

@@ -854,11 +854,60 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
     }
     else
     if (strcmp(cmd, "/notifications") == 0) {
-        /* TBD */
         if (logged_in) {
-            xs *l = notify_list(&snac1, 0);
+            xs *l      = notify_list(&snac1, 0);
+            xs *out    = xs_list_new();
+            xs_list *p = l;
+            xs_dict *v;
+
+            while (xs_list_iter(&p, &v)) {
+                const char *type = xs_dict_get(v, "type");
+                const char *objid = xs_dict_get(v, "objid");
+                xs *actor = NULL;
+                xs *entry = NULL;
+
+                if (!valid_status(object_get(xs_dict_get(v, "actor"), &actor)))
+                    continue;
 
-            *body  = xs_dup("[]");
+                if (objid != NULL && !valid_status(object_get(objid, &entry)))
+                    continue;
+
+                /* convert the type */
+                if (strcmp(type, "Like") == 0)
+                    type = "favourite";
+                else
+                if (strcmp(type, "Announce") == 0)
+                    type = "reblog";
+                else
+                if (strcmp(type, "Follow") == 0)
+                    type = "follow";
+                else
+                if (strcmp(type, "Create") == 0)
+                    type = "mention";
+                else
+                    continue;
+
+                xs *mn = xs_dict_new();
+
+                mn = xs_dict_append(mn, "type", type);
+
+                xs *id = xs_replace(xs_dict_get(v, "id"), ".", "");
+                mn = xs_dict_append(mn, "id", id);
+
+                mn = xs_dict_append(mn, "created_at", xs_dict_get(v, "date"));
+
+                xs *acct = mastoapi_account(actor);
+                mn = xs_dict_append(mn, "account", acct);
+
+                if (objid != NULL) {
+                    xs *st = mastoapi_status(&snac1, entry);
+                    mn = xs_dict_append(mn, "status", st);
+                }
+
+                out = xs_list_append(out, mn);
+            }
+
+            *body  = xs_json_dumps_pp(out, 4);
             *ctype = "application/json";
             status = 200;
         }