commit a1369b3

grunfink  ·  2025-05-28 07:07:19 +0000 UTC
parent e2bb807
Activated hashtag RSS polling.
5 files changed,  +43, -17
M html.c
M rss.c
M snac.h
+4, -0
 1@@ -3049,6 +3049,10 @@ void process_queue_item(xs_dict *q_item)
 2             }
 3         }
 4     }
 5+    else
 6+    if (strcmp(type, "rss_poll") == 0) {
 7+        rss_poll_hashtags();
 8+    }
 9     else
10         srv_log(xs_fmt("unexpected q_item type '%s'", type));
11 }
M html.c
+2, -1
 1@@ -1601,7 +1601,8 @@ xs_html *html_top_controls(snac *user)
 2                     xs_html_attr("name", "followed_hashtags"),
 3                     xs_html_attr("cols", "40"),
 4                     xs_html_attr("rows", "4"),
 5-                    xs_html_attr("placeholder", "#cats\n#windowfriday\n#classicalmusic"),
 6+                    xs_html_attr("placeholder", "#cats\n#windowfriday\n#classicalmusic\nhttps:/"
 7+                        "/mastodon.social/tags/dogs"),
 8                     xs_html_text(followed_hashtags)),
 9 
10                 xs_html_tag("br", NULL),
+31, -11
 1@@ -705,34 +705,36 @@ static pthread_cond_t  sleep_cond;
 2 static void *background_thread(void *arg)
 3 /* background thread (queue management and other things) */
 4 {
 5-    time_t purge_time;
 6+    time_t t, purge_time, rss_time;
 7 
 8     (void)arg;
 9 
10+    t = time(NULL);
11+
12     /* first purge time */
13-    purge_time = time(NULL) + 10 * 60;
14+    purge_time = t + 10 * 60;
15+
16+    /* first RSS polling time */
17+    rss_time = t + 15 * 60;
18 
19     srv_log(xs_fmt("background thread started"));
20 
21     while (p_state->srv_running) {
22-        time_t t;
23         int cnt = 0;
24 
25         p_state->th_state[0] = THST_QUEUE;
26 
27         {
28             xs *list = user_list();
29-            char *p;
30             const char *uid;
31 
32             /* process queues for all users */
33-            p = list;
34-            while (xs_list_iter(&p, &uid)) {
35-                snac snac;
36+            xs_list_foreach(list, uid) {
37+                snac user;
38 
39-                if (user_open(&snac, uid)) {
40-                    cnt += process_user_queue(&snac);
41-                    user_free(&snac);
42+                if (user_open(&user, uid)) {
43+                    cnt += process_user_queue(&user);
44+                    user_free(&user);
45                 }
46             }
47         }
48@@ -740,8 +742,10 @@ static void *background_thread(void *arg)
49         /* global queue */
50         cnt += process_queue();
51 
52+        t = time(NULL);
53+
54         /* time to purge? */
55-        if ((t = time(NULL)) > purge_time) {
56+        if (t > purge_time) {
57             /* next purge time is tomorrow */
58             purge_time = t + 24 * 60 * 60;
59 
60@@ -750,6 +754,22 @@ static void *background_thread(void *arg)
61             job_post(q_item, 0);
62         }
63 
64+        /* time to poll the RSS? */
65+        if (t > rss_time) {
66+            /* next RSS poll time */
67+            int hours = xs_number_get(xs_dict_get_def(srv_config, "rss_poll_hours", "4"));
68+
69+            /* don't hammer servers too much */
70+            if (hours < 1)
71+                hours = 1;
72+
73+            rss_time = t + 60 * 60 * hours;
74+
75+            xs *q_item = xs_dict_new();
76+            q_item = xs_dict_append(q_item, "type", "rss_poll");
77+            job_post(q_item, 0);
78+        }
79+
80         if (cnt == 0) {
81             /* sleep 3 seconds */
82 
M rss.c
+5, -4
 1@@ -110,6 +110,9 @@ xs_str *rss_from_timeline(snac *user, const xs_list *timeline,
 2 void rss_to_timeline(snac *user, const char *url)
 3 /* reads an RSS and inserts all ActivityPub posts into the user's timeline */
 4 {
 5+    if (!xs_startswith(url, "https:/") && !xs_startswith(url, "http:/"))
 6+        return;
 7+
 8     xs *hdrs = xs_dict_new();
 9     hdrs = xs_dict_set(hdrs, "accept",     "application/rss+xml");
10     hdrs = xs_dict_set(hdrs, "user-agent", USER_AGENT);
11@@ -204,10 +207,9 @@ void rss_to_timeline(snac *user, const char *url)
12 }
13 
14 
15-void rss_process(void)
16+void rss_poll_hashtags(void)
17 /* parses all RSS from all users */
18 {
19-#if 0
20     xs *list = user_list();
21     const char *uid;
22 
23@@ -215,7 +217,7 @@ void rss_process(void)
24         snac user;
25 
26         if (user_open(&user, uid)) {
27-            const xs_list *rss = xs_dict_get(user.config, "rss");
28+            const xs_list *rss = xs_dict_get(user.config, "followed_hashtags");
29 
30             if (xs_is_list(rss)) {
31                 const char *url;
32@@ -227,5 +229,4 @@ void rss_process(void)
33             user_free(&user);
34         }
35     }
36-#endif
37 }
M snac.h
+1, -1
1@@ -464,4 +464,4 @@ const char *lang_str(const char *str, const snac *user);
2 xs_str *rss_from_timeline(snac *user, const xs_list *timeline,
3                         const char *title, const char *link, const char *desc);
4 void rss_to_timeline(snac *user, const char *url);
5-void rss_process(void);
6+void rss_poll_hashtags(void);