diff --git a/include/mastodont_notification.h b/include/mastodont_notification.h index 57418c3..b55d513 100644 --- a/include/mastodont_notification.h +++ b/include/mastodont_notification.h @@ -49,6 +49,16 @@ struct mstdnt_get_notifications_args int limit; }; +struct _mstdnt_notifications_result_cb_args +{ + struct mstdnt_notification** notif; + size_t* size; +}; + +int _mstdnt_notifications_result_callback(struct mstdnt_fetch_results* results, + struct mstdnt_storage* storage, + void* _args); + int mastodont_get_notifications(mastodont_t* data, struct mstdnt_get_notifications_args* args, struct mstdnt_storage* storage, diff --git a/src/notification.c b/src/notification.c index 9d08855..b749100 100644 --- a/src/notification.c +++ b/src/notification.c @@ -14,16 +14,87 @@ */ #include +#include #include #include #include #include -int mstdnt_notification_from_json(struct mstdnt_notification* notif, cJSON* js) +static void _mstdnt_val_notif_type_call(cJSON* v, void* _type) { - + mstdnt_notification_t* type = _type; + + + if (strcmp(v->string, "type") != 0) + { + *type = 0; + return; + } + + if (strcmp(v->valuestring, "follow") == 0) *type = MSTDNT_NOTIFICATION_FOLLOW; + else if (strcmp(v->valuestring, "follow_request") == 0) *type = MSTDNT_NOTIFICATION_FOLLOW; + else if (strcmp(v->valuestring, "mention") == 0) *type = MSTDNT_NOTIFICATION_MENTION; + else if (strcmp(v->valuestring, "reblog") == 0) *type = MSTDNT_NOTIFICATION_REBLOG; + else if (strcmp(v->valuestring, "favourite") == 0) *type = MSTDNT_NOTIFICATION_FAVOURITE; + else if (strcmp(v->valuestring, "poll") == 0) *type = MSTDNT_NOTIFICATION_POLL; + else if (strcmp(v->valuestring, "status") == 0) *type = MSTDNT_NOTIFICATION_STATUS; + else if (strcmp(v->valuestring, "pleroma:emoji_reaction") == 0) *type = MSTDNT_NOTIFICATION_EMOJI_REACT; + else if (strcmp(v->valuestring, "pleroma:chat_mention") == 0) *type = MSTDNT_NOTIFICATION_CHAT_MENTION; + else if (strcmp(v->valuestring, "pleroma:report") == 0) *type = MSTDNT_NOTIFICATION_REPORT; } +int mstdnt_notification_from_json(struct mstdnt_notification* notif, cJSON* js) +{ + cJSON* v; + + struct _mstdnt_val_ref vals[] = { + /* { "account", &(notif->account), _mstdnt_val_account_call }, */ + { "created_at", &(notif->created_at), _mstdnt_val_string_call }, + { "id", &(notif->id), _mstdnt_val_string_call }, + /* { "status", &(notif->status), _mstdnt_val_status_call }, */ + /* { "pleroma", &(notif->pleroma), _mstdnt_val_notif_pleroma_call }, */ + { "type", &(notif->type), _mstdnt_val_notif_type_call }, + }; + + for (v = js; v; v = v->next) + _mstdnt_key_val_ref(v, vals, _mstdnt_arr_len(vals)); + + return 0; +} + +int mstdnt_notifications_from_result(struct mstdnt_fetch_results* results, + struct mstdnt_storage* storage, + struct mstdnt_notification* notif[], + size_t* size) +{ + size_t i = 0; + cJSON* root, *notif_j_list; + if (_mstdnt_json_init(&root, results, storage)) + return 1; + + if (size) *size = cJSON_GetArraySize(root); + + /* malloc array - cJSON does a loop to count, let's do it once preferably */ + *notif = malloc((size ? *size : cJSON_GetArraySize(root)) + * sizeof(struct mstdnt_notification)); + if (*notif == NULL) + return 1; + + cJSON_ArrayForEach(notif_j_list, root) + { + mstdnt_status_from_json((*notif) + i++, notif_j_list->child); + } + + return 0; +} + +int _mstdnt_notifications_result_callback(struct mstdnt_fetch_results* results, + struct mstdnt_storage* storage, + void* _args) +{ + struct _mstdnt_notifications_result_cb_args* args = _args; + return mstdnt_notifications_from_result(results, storage, args->notif, args->size); +} int mastodont_get_notifications(mastodont_t* data, diff --git a/src/status.c b/src/status.c index c14350f..2ace134 100644 --- a/src/status.c +++ b/src/status.c @@ -110,7 +110,6 @@ int mstdnt_statuses_from_result(struct mstdnt_storage* storage, mstdnt_status_from_json((*statuses) + i++, status_j_list->child); } - storage->needs_cleanup = 1; return 0; }