commit dc9d652

default  ·  2023-05-30 12:52:18 +0000 UTC
parent 0444b77
Add ended polls to the notification area.
1 files changed,  +124, -117
+124, -117
  1@@ -486,6 +486,109 @@ void process_tags(snac *snac, const char *content, xs_str **n_content, xs_list *
  2 }
  3 
  4 
  5+void notify(snac *snac, const char *type, const char *utype, const char *actor, const xs_dict *msg)
  6+/* notifies the user of relevant events */
  7+{
  8+    if (strcmp(type, "Create") == 0) {
  9+        /* only notify of notes specifically for us */
 10+        xs *rcpts = recipient_list(snac, msg, 0);
 11+
 12+        if (xs_list_in(rcpts, snac->actor) == -1)
 13+            return;
 14+    }
 15+
 16+    if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
 17+        return;
 18+
 19+    /* get the object id */
 20+    const char *objid = xs_dict_get(msg, "object");
 21+
 22+    if (xs_type(objid) == XSTYPE_DICT)
 23+        objid = xs_dict_get(objid, "id");
 24+    else
 25+        objid = xs_dict_get(msg, "id");
 26+
 27+    if (strcmp(type, "Like") == 0 || strcmp(type, "Announce") == 0) {
 28+        /* if it's not an admiration about something by us, done */
 29+        if (xs_is_null(objid) || !xs_startswith(objid, snac->actor))
 30+            return;
 31+    }
 32+
 33+    /* user will love to know about this! */
 34+
 35+    /* prepare message body */
 36+    xs *body = xs_fmt("User  : @%s@%s\n",
 37+        xs_dict_get(snac->config, "uid"),
 38+        xs_dict_get(srv_config,   "host")
 39+    );
 40+
 41+    if (strcmp(utype, "(null)") != 0) {
 42+        xs *s1 = xs_fmt("Type  : %s + %s\n", type, utype);
 43+        body = xs_str_cat(body, s1);
 44+    }
 45+    else {
 46+        xs *s1 = xs_fmt("Type  : %s\n", type);
 47+        body = xs_str_cat(body, s1);
 48+    }
 49+
 50+    {
 51+        xs *s1 = xs_fmt("Actor : %s\n", actor);
 52+        body = xs_str_cat(body, s1);
 53+    }
 54+
 55+    if (objid != NULL) {
 56+        xs *s1 = xs_fmt("Object: %s\n", objid);
 57+        body = xs_str_cat(body, s1);
 58+    }
 59+
 60+    /* email */
 61+
 62+    const char *email = "[disabled by admin]";
 63+
 64+    if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
 65+        email = xs_dict_get(snac->config_o, "email");
 66+        if (xs_is_null(email)) {
 67+            email = xs_dict_get(snac->config, "email");
 68+
 69+            if (xs_is_null(email))
 70+                email = "[empty]";
 71+        }
 72+    }
 73+
 74+    if (*email != '\0' && *email != '[') {
 75+        snac_debug(snac, 1, xs_fmt("email notify %s %s %s", type, utype, actor));
 76+
 77+        xs *subject = xs_fmt("snac notify for @%s@%s",
 78+                    xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
 79+        xs *from    = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
 80+        xs *header  = xs_fmt(
 81+                    "From: %s\n"
 82+                    "To: %s\n"
 83+                    "Subject: %s\n"
 84+                    "\n",
 85+                    from, email, subject);
 86+
 87+        xs *email_body = xs_fmt("%s%s", header, body);
 88+
 89+        enqueue_email(email_body, 0);
 90+    }
 91+
 92+    /* telegram */
 93+
 94+    char *bot     = xs_dict_get(snac->config, "telegram_bot");
 95+    char *chat_id = xs_dict_get(snac->config, "telegram_chat_id");
 96+
 97+    if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id)
 98+        enqueue_telegram(body, bot, chat_id);
 99+
100+    /* finally, store it in the notification folder */
101+    if (strcmp(type, "Follow") == 0)
102+        objid = xs_dict_get(msg, "id");
103+
104+    notify_add(snac, type, utype, actor, objid);
105+}
106+
107+
108 /** messages **/
109 
110 xs_dict *msg_base(snac *snac, const char *type, const char *id,
111@@ -1070,6 +1173,8 @@ int update_question(snac *user, const char *id)
112         if (strcmp(now, end_time) >= 0) {
113             xs *et = xs_dup(end_time);
114             msg    = xs_dict_set(msg, "closed", et);
115+
116+            notify(user, "Update", "Question", user->actor, msg);
117         }
118     }
119 
120@@ -1093,107 +1198,6 @@ int update_question(snac *user, const char *id)
121 }
122 
123 
124-void notify(snac *snac, xs_str *type, xs_str *utype, xs_str *actor, xs_dict *msg)
125-/* notifies the user of relevant events */
126-{
127-    if (strcmp(type, "Create") == 0) {
128-        /* only notify of notes specifically for us */
129-        xs *rcpts = recipient_list(snac, msg, 0);
130-
131-        if (xs_list_in(rcpts, snac->actor) == -1)
132-            return;
133-    }
134-
135-    if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
136-        return;
137-
138-    /* get the object id */
139-    const char *objid = xs_dict_get(msg, "object");
140-
141-    if (xs_type(objid) == XSTYPE_DICT)
142-        objid = xs_dict_get(objid, "id");
143-
144-    if (strcmp(type, "Like") == 0 || strcmp(type, "Announce") == 0) {
145-        /* if it's not an admiration about something by us, done */
146-        if (xs_is_null(objid) || !xs_startswith(objid, snac->actor))
147-            return;
148-    }
149-
150-    /* user will love to know about this! */
151-
152-    /* prepare message body */
153-    xs *body = xs_fmt("User  : @%s@%s\n",
154-        xs_dict_get(snac->config, "uid"),
155-        xs_dict_get(srv_config,   "host")
156-    );
157-
158-    if (strcmp(utype, "(null)") != 0) {
159-        xs *s1 = xs_fmt("Type  : %s + %s\n", type, utype);
160-        body = xs_str_cat(body, s1);
161-    }
162-    else {
163-        xs *s1 = xs_fmt("Type  : %s\n", type);
164-        body = xs_str_cat(body, s1);
165-    }
166-
167-    {
168-        xs *s1 = xs_fmt("Actor : %s\n", actor);
169-        body = xs_str_cat(body, s1);
170-    }
171-
172-    if (objid != NULL) {
173-        xs *s1 = xs_fmt("Object: %s\n", objid);
174-        body = xs_str_cat(body, s1);
175-    }
176-
177-    /* email */
178-
179-    const char *email = "[disabled by admin]";
180-
181-    if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
182-        email = xs_dict_get(snac->config_o, "email");
183-        if (xs_is_null(email)) {
184-            email = xs_dict_get(snac->config, "email");
185-
186-            if (xs_is_null(email))
187-                email = "[empty]";
188-        }
189-    }
190-
191-    if (*email != '\0' && *email != '[') {
192-        snac_debug(snac, 1, xs_fmt("email notify %s %s %s", type, utype, actor));
193-
194-        xs *subject = xs_fmt("snac notify for @%s@%s",
195-                    xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
196-        xs *from    = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
197-        xs *header  = xs_fmt(
198-                    "From: %s\n"
199-                    "To: %s\n"
200-                    "Subject: %s\n"
201-                    "\n",
202-                    from, email, subject);
203-
204-        xs *email_body = xs_fmt("%s%s", header, body);
205-
206-        enqueue_email(email_body, 0);
207-    }
208-
209-    /* telegram */
210-
211-    char *bot     = xs_dict_get(snac->config, "telegram_bot");
212-    char *chat_id = xs_dict_get(snac->config, "telegram_chat_id");
213-
214-    if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id)
215-        enqueue_telegram(body, bot, chat_id);
216-
217-    /* finally, store it in the notification folder */
218-    if (strcmp(type, "Follow") == 0)
219-        objid = xs_dict_get(msg, "id");
220-
221-    notify_add(snac, type, utype, actor, objid);
222-}
223-
224-
225 /** queues **/
226 
227 int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
228@@ -1270,7 +1274,7 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
229         return 1;
230     }
231 
232-    if (strcmp(type, "Follow") == 0) {
233+    if (strcmp(type, "Follow") == 0) { /** **/
234         if (!follower_check(snac, actor)) {
235             xs *f_msg = xs_dup(msg);
236             xs *reply = msg_accept(snac, f_msg, actor);
237@@ -1294,8 +1298,8 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
238             snac_log(snac, xs_fmt("repeated 'Follow' from %s", actor));
239     }
240     else
241-    if (strcmp(type, "Undo") == 0) {
242-        if (strcmp(utype, "Follow") == 0) {
243+    if (strcmp(type, "Undo") == 0) { /** **/
244+        if (strcmp(utype, "Follow") == 0) { /** **/
245             if (valid_status(follower_del(snac, actor))) {
246                 snac_log(snac, xs_fmt("no longer following us %s", actor));
247                 do_notify = 1;
248@@ -1307,11 +1311,11 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
249             snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
250     }
251     else
252-    if (strcmp(type, "Create") == 0) {
253+    if (strcmp(type, "Create") == 0) { /** **/
254         if (is_muted(snac, actor))
255             snac_log(snac, xs_fmt("ignored 'Create' + '%s' from muted actor %s", utype, actor));
256 
257-        if (strcmp(utype, "Note") == 0) {
258+        if (strcmp(utype, "Note") == 0) { /** **/
259             char *id          = xs_dict_get(object, "id");
260             char *in_reply_to = xs_dict_get(object, "inReplyTo");
261             xs *wrk           = NULL;
262@@ -1330,7 +1334,7 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
263                 update_question(snac, in_reply_to);
264         }
265         else
266-        if (strcmp(utype, "Question") == 0) {
267+        if (strcmp(utype, "Question") == 0) { /**  **/
268             char *id = xs_dict_get(object, "id");
269 
270             if (timeline_add(snac, id, object))
271@@ -1340,8 +1344,8 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
272             snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
273     }
274     else
275-    if (strcmp(type, "Accept") == 0) {
276-        if (strcmp(utype, "Follow") == 0) {
277+    if (strcmp(type, "Accept") == 0) { /** **/
278+        if (strcmp(utype, "Follow") == 0) { /** **/
279             if (following_check(snac, actor)) {
280                 following_add(snac, actor, msg);
281                 snac_log(snac, xs_fmt("confirmed follow from %s", actor));
282@@ -1353,7 +1357,7 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
283             snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
284     }
285     else
286-    if (strcmp(type, "Like") == 0) {
287+    if (strcmp(type, "Like") == 0) { /** **/
288         if (xs_type(object) == XSTYPE_DICT)
289             object = xs_dict_get(object, "id");
290 
291@@ -1362,7 +1366,7 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
292         do_notify = 1;
293     }
294     else
295-    if (strcmp(type, "Announce") == 0) {
296+    if (strcmp(type, "Announce") == 0) { /** **/
297         xs *a_msg = NULL;
298         xs *wrk   = NULL;
299 
300@@ -1393,7 +1397,7 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
301             snac_log(snac, xs_fmt("error requesting 'Announce' object %s", object));
302     }
303     else
304-    if (strcmp(type, "Update") == 0) {
305+    if (strcmp(type, "Update") == 0) { /** **/
306         if (strcmp(utype, "Person") == 0) {
307             actor_add(actor, xs_dict_get(msg, "object"));
308             timeline_touch(snac);
309@@ -1401,7 +1405,7 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
310             snac_log(snac, xs_fmt("updated actor %s", actor));
311         }
312         else
313-        if (strcmp(utype, "Note") == 0) {
314+        if (strcmp(utype, "Note") == 0) { /** **/
315             const char *id = xs_dict_get(object, "id");
316 
317             object_add_ow(id, object);
318@@ -1410,7 +1414,7 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
319             snac_log(snac, xs_fmt("updated post %s", id));
320         }
321         else
322-        if (strcmp(utype, "Question") == 0) {
323+        if (strcmp(utype, "Question") == 0) { /** **/
324             const char *id     = xs_dict_get(object, "id");
325             const char *closed = xs_dict_get(object, "closed");
326 
327@@ -1418,12 +1422,15 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
328             timeline_touch(snac);
329 
330             snac_log(snac, xs_fmt("%s poll %s", closed == NULL ? "updated" : "closed", id));
331+
332+            if (closed != NULL)
333+                do_notify = 1;
334         }
335         else
336             snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
337     }
338     else
339-    if (strcmp(type, "Delete") == 0) {
340+    if (strcmp(type, "Delete") == 0) { /** **/
341         if (xs_type(object) == XSTYPE_DICT)
342             object = xs_dict_get(object, "id");
343 
344@@ -1433,11 +1440,11 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
345             snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
346     }
347     else
348-    if (strcmp(type, "Pong") == 0) {
349+    if (strcmp(type, "Pong") == 0) { /** **/
350         snac_log(snac, xs_fmt("'Pong' received from %s", actor));
351     }
352     else
353-    if (strcmp(type, "Ping") == 0) {
354+    if (strcmp(type, "Ping") == 0) { /** **/
355         snac_log(snac, xs_fmt("'Ping' requested from %s", actor));
356 
357         xs *rsp = msg_pong(snac, actor, xs_dict_get(msg, "id"));