summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Paul Martin2024-12-19 19:55:56 +0000
committerGravatar Paul Martin2024-12-20 00:18:32 +0000
commitfa253f008a0f4d028dbb9ef14c83d6699a133614 (patch)
tree1a5f52d469327385bfc7890de5926768801ef6ed
parentUpdated documentation. (diff)
downloadpenes-snac2-fa253f008a0f4d028dbb9ef14c83d6699a133614.tar.gz
penes-snac2-fa253f008a0f4d028dbb9ef14c83d6699a133614.tar.xz
penes-snac2-fa253f008a0f4d028dbb9ef14c83d6699a133614.zip
Implement mastoapi markers for notifications and home.
Diffstat (limited to '')
-rw-r--r--data.c66
-rw-r--r--mastoapi.c58
-rw-r--r--snac.h3
3 files changed, 120 insertions, 7 deletions
diff --git a/data.c b/data.c
index 6394d1c..823975e 100644
--- a/data.c
+++ b/data.c
@@ -2840,6 +2840,72 @@ xs_str *notify_check_time(snac *snac, int reset)
2840 return t; 2840 return t;
2841} 2841}
2842 2842
2843xs_dict *markers_get(snac *snac, const xs_list *markers)
2844{
2845 xs_dict *data = NULL;
2846 xs_dict *returns = xs_dict_new();
2847 xs *fn = xs_fmt("%s/markers.json", snac->basedir);
2848 const xs_str *v = NULL;
2849 FILE *f;
2850
2851 if ((f = fopen(fn, "r")) != NULL) {
2852 data = xs_json_load(f);
2853 fclose(f);
2854 }
2855
2856 if (xs_is_null(data))
2857 data = xs_dict_new();
2858
2859 xs_list_foreach(markers, v) {
2860 const xs_dict *mark = xs_dict_get(data, v);
2861 if (!xs_is_null(mark)) {
2862 returns = xs_dict_append(returns, v, mark);
2863 }
2864 }
2865 return returns;
2866}
2867
2868xs_dict *markers_set(snac *snac, const char *home_marker, const char *notify_marker)
2869/* gets or sets notification marker */
2870{
2871 xs_dict *data = NULL;
2872 xs_dict *written = xs_dict_new();
2873 xs *fn = xs_fmt("%s/markers.json", snac->basedir);
2874 FILE *f;
2875
2876 if ((f = fopen(fn, "r")) != NULL) {
2877 data = xs_json_load(f);
2878 fclose(f);
2879 }
2880
2881 if (xs_is_null(data))
2882 data = xs_dict_new();
2883
2884 if (!xs_is_null(home_marker)) {
2885 xs_dict *home = xs_dict_new();
2886 home = xs_dict_append(home, "last_read_id", home_marker);
2887 home = xs_dict_append(home, "version", xs_stock(0));
2888 home = xs_dict_append(home, "updated_at", tid(0));
2889 data = xs_dict_set(data, "home", home);
2890 written = xs_dict_append(written, "home", home);
2891 }
2892
2893 if (!xs_is_null(notify_marker)) {
2894 xs_dict *notify = xs_dict_new();
2895 notify = xs_dict_append(notify, "last_read_id", notify_marker);
2896 notify = xs_dict_append(notify, "version", xs_stock(0));
2897 notify = xs_dict_append(notify, "updated_at", tid(0));
2898 data = xs_dict_set(data, "notifications", notify);
2899 written = xs_dict_append(written, "notifications", notify);
2900 }
2901
2902 if ((f = fopen(fn, "w")) != NULL) {
2903 xs_json_dump(data, 4, f);
2904 fclose(f);
2905 }
2906
2907 return written;
2908}
2843 2909
2844void notify_add(snac *snac, const char *type, const char *utype, 2910void notify_add(snac *snac, const char *type, const char *utype,
2845 const char *actor, const char *objid, const xs_dict *msg) 2911 const char *actor, const char *objid, const xs_dict *msg)
diff --git a/mastoapi.c b/mastoapi.c
index 2c8c04d..c78c380 100644
--- a/mastoapi.c
+++ b/mastoapi.c
@@ -1788,6 +1788,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
1788 xs *out = xs_list_new(); 1788 xs *out = xs_list_new();
1789 const xs_dict *v; 1789 const xs_dict *v;
1790 const xs_list *excl = xs_dict_get(args, "exclude_types[]"); 1790 const xs_list *excl = xs_dict_get(args, "exclude_types[]");
1791 const char *min_id = xs_dict_get(args, "min_id");
1791 const char *max_id = xs_dict_get(args, "max_id"); 1792 const char *max_id = xs_dict_get(args, "max_id");
1792 1793
1793 xs_list_foreach(l, v) { 1794 xs_list_foreach(l, v) {
@@ -1814,10 +1815,14 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
1814 continue; 1815 continue;
1815 1816
1816 if (max_id) { 1817 if (max_id) {
1817 if (strcmp(fid, max_id) == 0) 1818 if (strcmp(fid, max_id) > 0)
1818 max_id = NULL; 1819 continue;
1820 }
1819 1821
1820 continue; 1822 if (min_id) {
1823 if (strcmp(fid, min_id) <= 0) {
1824 continue;
1825 }
1821 } 1826 }
1822 1827
1823 /* convert the type */ 1828 /* convert the type */
@@ -2298,9 +2303,22 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
2298 } 2303 }
2299 else 2304 else
2300 if (strcmp(cmd, "/v1/markers") == 0) { /** **/ 2305 if (strcmp(cmd, "/v1/markers") == 0) { /** **/
2301 *body = xs_dup("{}"); 2306 if (logged_in) {
2302 *ctype = "application/json"; 2307 const xs_list *timeline = xs_dict_get(args, "timeline[]");
2303 status = HTTP_STATUS_OK; 2308 xs_str *json = NULL;
2309 if (!xs_is_null(timeline))
2310 json = xs_json_dumps(markers_get(&snac1, timeline), 4);
2311
2312 if (!xs_is_null(json))
2313 *body = json;
2314 else
2315 *body = xs_dup("{}");
2316
2317 *ctype = "application/json";
2318 status = HTTP_STATUS_OK;
2319 }
2320 else
2321 status = HTTP_STATUS_UNAUTHORIZED;
2304 } 2322 }
2305 else 2323 else
2306 if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/ 2324 if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
@@ -2997,9 +3015,35 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
2997 } 3015 }
2998 } 3016 }
2999 } 3017 }
3018 }
3019 else if (strcmp(cmd, "/v1/markers") == 0) { /** **/
3020 xs_str *json = NULL;
3021 if (logged_in) {
3022 const xs_str *home_marker = xs_dict_get(args, "home[last_read_id]");
3023 if (xs_is_null(home_marker)) {
3024 const xs_dict *home = xs_dict_get(args, "home");
3025 if (!xs_is_null(home))
3026 home_marker = xs_dict_get(home, "last_read_id");
3027 }
3028
3029 const xs_str *notify_marker = xs_dict_get(args, "notifications[last_read_id]");
3030 if (xs_is_null(notify_marker)) {
3031 const xs_dict *notify = xs_dict_get(args, "notifications");
3032 if (!xs_is_null(notify))
3033 notify_marker = xs_dict_get(notify, "last_read_id");
3034 }
3035 json = xs_json_dumps(markers_set(&snac, home_marker, notify_marker), 4);
3036 }
3037 if (!xs_is_null(json))
3038 *body = json;
3000 else 3039 else
3001 status = HTTP_STATUS_UNPROCESSABLE_CONTENT; 3040 *body = xs_dup("{}");
3041
3042 *ctype = "application/json";
3043 status = HTTP_STATUS_OK;
3002 } 3044 }
3045 else
3046 status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
3003 3047
3004 /* user cleanup */ 3048 /* user cleanup */
3005 if (logged_in) 3049 if (logged_in)
diff --git a/snac.h b/snac.h
index dc3fa0a..2b58669 100644
--- a/snac.h
+++ b/snac.h
@@ -236,6 +236,9 @@ int notify_new_num(snac *snac);
236xs_list *notify_list(snac *snac, int skip, int show); 236xs_list *notify_list(snac *snac, int skip, int show);
237void notify_clear(snac *snac); 237void notify_clear(snac *snac);
238 238
239xs_dict *markers_get(snac *snac, const xs_list *markers);
240xs_dict *markers_set(snac *snac, const char *home_marker, const char *notify_marker);
241
239void inbox_add(const char *inbox); 242void inbox_add(const char *inbox);
240void inbox_add_by_actor(const xs_dict *actor); 243void inbox_add_by_actor(const xs_dict *actor);
241xs_list *inbox_list(void); 244xs_list *inbox_list(void);