commit a07458f

grunfink  ·  2025-06-19 17:19:08 +0000 UTC
parent 8c9eb18
New command-line option export_posts.
3 files changed,  +60, -0
M main.c
M snac.h
M main.c
+6, -0
 1@@ -59,6 +59,7 @@ int usage(const char *cmd)
 2         "verify_links {basedir} {uid}         Verifies a user's links (in the metadata)\n"
 3         "search {basedir} {uid} {regex}       Searches posts by content\n"
 4         "export_csv {basedir} {uid}           Exports followers, lists, MUTEd and bookmarks to CSV\n"
 5+        "export_posts {basedir} {iod}         Exports all posts to outbox.json\n"
 6         "alias {basedir} {uid} {account}      Sets account (@user@host or actor url) as an alias\n"
 7         "migrate {basedir} {uid}              Migrates to the account defined as the alias\n"
 8         "import_csv {basedir} {uid}           Imports data from CSV files\n"
 9@@ -308,6 +309,11 @@ int main(int argc, char *argv[])
10         return 0;
11     }
12 
13+    if (strcmp(cmd, "export_posts") == 0) { /** **/
14+        export_posts(&snac);
15+        return 0;
16+    }
17+
18     if (strcmp(cmd, "import_csv") == 0) { /** **/
19         import_csv(&snac);
20         return 0;
M snac.h
+2, -0
1@@ -434,6 +434,8 @@ void mastoapi_purge(void);
2 void verify_links(snac *user);
3 
4 void export_csv(snac *user);
5+void export_posts(snac *user);
6+
7 int migrate_account(snac *user);
8 
9 void import_blocked_accounts_csv(snac *user, const char *fn);
+52, -0
 1@@ -725,6 +725,58 @@ void export_csv(snac *user)
 2 }
 3 
 4 
 5+void export_posts(snac *user)
 6+/* exports all posts to an OrderedCollection */
 7+{
 8+    xs *ifn = xs_fmt("%s/public.idx", user->basedir);
 9+    xs *index = index_list(ifn, XS_ALL);
10+    xs *ofn = xs_fmt("%s/export/outbox.json", user->basedir);
11+    FILE *f;
12+
13+    if ((f = fopen(ofn, "w")) == NULL) {
14+        snac_log(user, xs_fmt("Cannot create file %s", ofn));
15+        return;
16+    }
17+
18+    int cnt = 0;
19+
20+    /* raw output */
21+    fprintf(f, "{\"@context\": \"https:/" "/www.w3.org/ns/activitystreams\",");
22+    fprintf(f, "\"id\": \"outbox.json\",");
23+    fprintf(f, "\"type\": \"OrderedCollection\",");
24+    fprintf(f, "\"orderedItems\": [");
25+
26+    const char *md5;
27+
28+    snac_log(user, xs_fmt("Creating %s...", ofn));
29+
30+    xs_list_foreach(index, md5) {
31+        xs *obj = NULL;
32+
33+        if (!valid_status(object_get_by_md5(md5, &obj)))
34+            continue;
35+
36+        const char *type = xs_dict_get(obj, "type");
37+
38+        if (!xs_is_string(type) || strcmp(type, "Note"))
39+            continue;
40+
41+        const char *atto = get_atto(obj);
42+
43+        if (!xs_is_string(atto) || strcmp(atto, user->actor))
44+            continue;
45+
46+        xs *c_msg = msg_create(user, obj);
47+        xs_json_dump(c_msg, 0, f);
48+        cnt++;
49+    }
50+
51+    fprintf(f, "], \"totalItems\": %d}", cnt);
52+
53+    fclose(f);
54+}
55+
56+
57 void import_blocked_accounts_csv(snac *user, const char *ifn)
58 /* imports a Mastodon CSV file of blocked accounts */
59 {