Quellcode durchsuchen

Started work to support shared inboxes.

default vor 2 Jahren
Ursprung
Commit
bad729c631
3 geänderte Dateien mit 70 neuen und 1 gelöschten Zeilen
  1. 47 0
      activitypub.c
  2. 22 1
      data.c
  3. 1 0
      snac.h

+ 47 - 0
activitypub.c

@@ -1008,6 +1008,15 @@ xs_dict *msg_actor(snac *snac)
         msg = xs_dict_set(msg, "attachment", attach);
     }
 
+#ifdef SHARED_INBOX
+    {
+        xs *d = xs_dict_new();
+        xs *si = xs_fmt("%s/shared-inbox", srv_baseurl);
+        d = xs_dict_append(d, "sharedInbox", si);
+        msg = xs_dict_set(msg, "endpoints", d);
+    }
+#endif
+
     return msg;
 }
 
@@ -2024,6 +2033,39 @@ void process_queue_item(xs_dict *q_item)
 
         srv_log(xs_dup("purge end"));
     }
+    else
+    if (strcmp(type, "input") == 0) {
+        /* redistribute the input message to all users */
+        char *ntid = xs_dict_get(q_item, "ntid");
+        xs *tmpfn  = xs_fmt("%s/tmp/%s.json", srv_basedir, ntid);
+        FILE *f;
+
+        if ((f = fopen(tmpfn, "w")) != NULL) {
+            xs_json_dump(q_item, 4, f);
+            fclose(f);
+        }
+
+        xs *users = user_list();
+        xs_list *p = users;
+        char *v;
+
+        while (xs_list_iter(&p, &v)) {
+            snac user;
+
+            if (user_open(&user, v)) {
+                xs *fn = xs_fmt("%s/queue/%s.json", user.basedir, ntid);
+
+                srv_debug(1, xs_fmt("enqueue_input (from shared inbox) %s", fn));
+
+                if (link(tmpfn, fn) < 0)
+                    srv_log(xs_fmt("link(%s, %s) error", tmpfn, fn));
+
+                user_free(&user);
+            }
+        }
+
+        unlink(tmpfn);
+    }
     else
         srv_log(xs_fmt("unexpected q_item type '%s'", type));
 }
@@ -2197,6 +2239,11 @@ int activitypub_post_handler(const xs_dict *req, const char *q_path,
     /* get the user and path */
     xs *l = xs_split_n(q_path, "/", 2);
 
+    if (xs_list_len(l) == 2 && strcmp(xs_list_get(l, 1), "shared-inbox") == 0) {
+        enqueue_shared_input(msg, req, 0);
+        return 202;
+    }
+
     if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
         /* strange q_path */
         srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));

+ 22 - 1
data.c

@@ -99,6 +99,9 @@ int srv_open(char *basedir, int auto_upgrade)
     xs *ibdir = xs_fmt("%s/inbox", srv_basedir);
     mkdirx(ibdir);
 
+    xs *tmpdir = xs_fmt("%s/tmp", srv_basedir);
+    mkdirx(tmpdir);
+
 #ifdef __OpenBSD__
     char *v = xs_dict_get(srv_config, "disable_openbsd_security");
 
@@ -1586,9 +1589,12 @@ void tag_index(const char *id, const xs_dict *obj)
             char *name = xs_dict_get(v, "name");
 
             if (!xs_is_null(type) && !xs_is_null(name) && strcmp(type, "Hashtag") == 0) {
-                if (*name == '#')
+                while (*name == '#' || *name == '@')
                     name++;
 
+                if (*name == '\0')
+                    continue;
+
                 name = xs_tolower_i(name);
 
                 xs *md5_tag   = xs_md5_hex(name, strlen(name));
@@ -2123,6 +2129,21 @@ void enqueue_input(snac *snac, const xs_dict *msg, const xs_dict *req, int retri
 }
 
 
+void enqueue_shared_input(const xs_dict *msg, const xs_dict *req, int retries)
+/* enqueues an input message from the shared input */
+{
+    xs *qmsg   = _new_qmsg("input", msg, retries);
+    char *ntid = xs_dict_get(qmsg, "ntid");
+    xs *fn     = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
+
+    qmsg = xs_dict_append(qmsg, "req", req);
+
+    qmsg = _enqueue_put(fn, qmsg);
+
+    srv_debug(1, xs_fmt("enqueue_shared_input %s", fn));
+}
+
+
 void enqueue_output_raw(const char *keyid, const char *seckey,
                         xs_dict *msg, xs_str *inbox, int retries, int p_status)
 /* enqueues an output message to an inbox */

+ 1 - 0
snac.h

@@ -181,6 +181,7 @@ int instance_block(const char *instance);
 int instance_unblock(const char *instance);
 
 void enqueue_input(snac *snac, const xs_dict *msg, const xs_dict *req, int retries);
+void enqueue_shared_input(const xs_dict *msg, const xs_dict *req, int retries);
 void enqueue_output_raw(const char *keyid, const char *seckey,
                         xs_dict *msg, xs_str *inbox, int retries, int p_status);
 void enqueue_output(snac *snac, xs_dict *msg, xs_str *inbox, int retries, int p_status);