shithub: masto9

Download patch

ref: 7029129c4b7f1256bf5923b78c39537aa60267bf
parent: 1cab90a81a79d6a9891ea46395f666bfedb15ced
author: Julien Blanchard <julien@typed-hole.org>
date: Wed Nov 13 10:22:19 EST 2024

Respect toots and notifications limits, handle snac edge case with favourites

--- a/masto9.c
+++ b/masto9.c
@@ -12,8 +12,7 @@
 {
 	UserPasswd *p;
 
-	if((p = auth_getuserpasswd(
-			auth_getkey, "proto=pass service=mastodon server=%s", host)) == nil)
+	if((p = auth_getuserpasswd(auth_getkey, "proto=pass service=mastodon server=%s", host)) == nil)
 		sysfatal("getcredentials: failed to retrieve token: %r");
 
 	return p;
@@ -86,7 +85,7 @@
 	if(obj->t != JSONArray)
 		sysfatal("gethome: jsonparse: not an array");
 
-	for(JSONEl *p = obj->first; p != nil; p = p->next) {
+	for(JSONEl *p = obj->first; p != nil && i < TOOTSCOUNT; p = p->next) {
 		JSON *tootjson = p->val;
 
 		id = getjsonkey(tootjson, "id");
@@ -122,8 +121,7 @@
 
 		if(mediaattachments->s != nil) {
 			int j = 0;
-			for(JSONEl *at = mediaattachments->first; at != nil;
-				at = at->next) {
+			for(JSONEl *at = mediaattachments->first; at != nil; at = at->next) {
 				JSON *attachmentjson = at->val;
 				Attachment *attachment = emalloc(sizeof(Attachment));
 				type = getjsonkey(attachmentjson, "type");
@@ -151,7 +149,7 @@
 static void
 getnotifications(char *token, char *host, Notification *notifs, char *filter)
 {
-	JSON *obj, *id, *content, *displayname, *handle, *type, *account, *status, *statusid;
+	JSON *obj, *id, *content, *displayname, *handle, *type, *account, *status, *statusid, *reblog;
 	char *endpoint;
 	int i = 0;
 
@@ -165,9 +163,8 @@
 	if(obj->t != JSONArray)
 		sysfatal("getnotifications: jsonparse: not an array");
 
-	for(JSONEl *p = obj->first; p != nil; p = p->next) {
+	for(JSONEl *p = obj->first; p != nil && i < NOTIFSCOUNT; p = p->next) {
 		JSON *notifjson = p->val;
-
 		id = getjsonkey(notifjson, "id");
 		type = getjsonkey(notifjson, "type");
 
@@ -183,9 +180,19 @@
 
 		if(strcmp(type->s, "follow") != 0) {
 			status = getjsonkey(notifjson, "status");
-			content = getjsonkey(status, "content");
-			statusid = getjsonkey(status, "id");
+			reblog = getjsonkey(status, "reblog");
 
+			if(strcmp(type->s, "favourite") == 0 && reblog->s != nil) {
+				// Weird case with snac where a favourite is on a reblog
+				// so the "content" key is inside the reblog object as "text"
+				content = getjsonkey(reblog, "text");
+			} else if (strcmp(type->s, "reblog") == 0 && reblog->s != nil) {
+				content = getjsonkey(reblog, "content");
+			} else {
+				content = getjsonkey(status, "content");
+			}
+
+			statusid = getjsonkey(status, "id");
 			notif->content = estrdup((char *)content->s);
 			notif->statusid = estrdup((char *)statusid->s);
 		}