diff options
| author | 2024-04-29 07:43:01 +0200 | |
|---|---|---|
| committer | 2024-04-29 07:43:01 +0200 | |
| commit | 8275a5f4d8764ebc9f9f82a4db377bacfa9fbc75 (patch) | |
| tree | 7d896846e16d6324d16e061a2bc08cfc0183efb0 | |
| parent | Updated RELEASE_NOTES. (diff) | |
| download | snac2-8275a5f4d8764ebc9f9f82a4db377bacfa9fbc75.tar.gz snac2-8275a5f4d8764ebc9f9f82a4db377bacfa9fbc75.tar.xz snac2-8275a5f4d8764ebc9f9f82a4db377bacfa9fbc75.zip | |
Start of list support.
| -rw-r--r-- | data.c | 62 | ||||
| -rw-r--r-- | mastoapi.c | 47 | ||||
| -rw-r--r-- | snac.h | 2 |
3 files changed, 108 insertions, 3 deletions
| @@ -1729,6 +1729,68 @@ xs_list *tag_search(char *tag, int skip, int show) | |||
| 1729 | } | 1729 | } |
| 1730 | 1730 | ||
| 1731 | 1731 | ||
| 1732 | /** lists **/ | ||
| 1733 | |||
| 1734 | xs_list *list_maint(snac *user, const char *list, int op) | ||
| 1735 | { | ||
| 1736 | xs_list *l = NULL; | ||
| 1737 | |||
| 1738 | switch (op) { | ||
| 1739 | case 0: /** list of lists **/ | ||
| 1740 | { | ||
| 1741 | FILE *f; | ||
| 1742 | xs *spec = xs_fmt("%s/list/" "*.id", user->basedir); | ||
| 1743 | xs *ls = xs_glob(spec, 0, 0); | ||
| 1744 | int c = 0; | ||
| 1745 | char *v; | ||
| 1746 | |||
| 1747 | l = xs_list_new(); | ||
| 1748 | |||
| 1749 | while (xs_list_next(ls, &v, &c)) { | ||
| 1750 | if ((f = fopen(v, "r")) != NULL) { | ||
| 1751 | xs *title = xs_readline(f); | ||
| 1752 | fclose(f); | ||
| 1753 | |||
| 1754 | title = xs_strip_i(title); | ||
| 1755 | xs *md5 = xs_md5_hex(title, strlen(title)); | ||
| 1756 | |||
| 1757 | /* return [ list_id, list_title ] */ | ||
| 1758 | l = xs_list_append(l, xs_list_append(xs_list_new(), md5, title)); | ||
| 1759 | } | ||
| 1760 | } | ||
| 1761 | } | ||
| 1762 | |||
| 1763 | break; | ||
| 1764 | |||
| 1765 | case 1: /** create new list (list is the name) **/ | ||
| 1766 | { | ||
| 1767 | FILE *f; | ||
| 1768 | xs *dir = xs_fmt("%s/list/", user->basedir); | ||
| 1769 | xs *md5 = xs_md5_hex(list, strlen(list)); | ||
| 1770 | |||
| 1771 | mkdirx(dir); | ||
| 1772 | |||
| 1773 | xs *fn = xs_fmt("%s%s.id", dir, md5); | ||
| 1774 | |||
| 1775 | if ((f = fopen(fn, "w")) != NULL) { | ||
| 1776 | fprintf(f, "%s\n", list); | ||
| 1777 | fclose(f); | ||
| 1778 | } | ||
| 1779 | } | ||
| 1780 | |||
| 1781 | break; | ||
| 1782 | |||
| 1783 | case 2: /** delete list (list is md5 id) **/ | ||
| 1784 | break; | ||
| 1785 | |||
| 1786 | case 3: /** list content (list is md5 id) **/ | ||
| 1787 | break; | ||
| 1788 | } | ||
| 1789 | |||
| 1790 | return l; | ||
| 1791 | } | ||
| 1792 | |||
| 1793 | |||
| 1732 | /** static data **/ | 1794 | /** static data **/ |
| 1733 | 1795 | ||
| 1734 | static int _load_raw_file(const char *fn, xs_val **data, int *size, | 1796 | static int _load_raw_file(const char *fn, xs_val **data, int *size, |
| @@ -1767,9 +1767,27 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 1767 | else | 1767 | else |
| 1768 | if (strcmp(cmd, "/v1/lists") == 0) { /** **/ | 1768 | if (strcmp(cmd, "/v1/lists") == 0) { /** **/ |
| 1769 | /* snac does not support lists */ | 1769 | /* snac does not support lists */ |
| 1770 | *body = xs_dup("[]"); | 1770 | if (logged_in) { |
| 1771 | *ctype = "application/json"; | 1771 | xs *lol = list_maint(&snac1, NULL, 0); |
| 1772 | status = 200; | 1772 | xs *l = xs_list_new(); |
| 1773 | int c = 0; | ||
| 1774 | xs_list *li; | ||
| 1775 | |||
| 1776 | while (xs_list_next(lol, &li, &c)) { | ||
| 1777 | xs *d = xs_dict_new(); | ||
| 1778 | |||
| 1779 | d = xs_dict_append(d, "id", xs_list_get(li, 0)); | ||
| 1780 | d = xs_dict_append(d, "title", xs_list_get(li, 1)); | ||
| 1781 | d = xs_dict_append(d, "replies_policy", "list"); | ||
| 1782 | d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE)); | ||
| 1783 | |||
| 1784 | l = xs_list_append(l, d); | ||
| 1785 | } | ||
| 1786 | |||
| 1787 | *body = xs_json_dumps(l, 4); | ||
| 1788 | *ctype = "application/json"; | ||
| 1789 | status = 200; | ||
| 1790 | } | ||
| 1773 | } | 1791 | } |
| 1774 | else | 1792 | else |
| 1775 | if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/ | 1793 | if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/ |
| @@ -2631,6 +2649,29 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, | |||
| 2631 | else | 2649 | else |
| 2632 | status = 401; | 2650 | status = 401; |
| 2633 | } | 2651 | } |
| 2652 | else | ||
| 2653 | if (strcmp(cmd, "/v1/lists") == 0) { | ||
| 2654 | if (logged_in) { | ||
| 2655 | const char *title = xs_dict_get(args, "title"); | ||
| 2656 | |||
| 2657 | if (xs_type(title) == XSTYPE_STRING) { | ||
| 2658 | /* add the list */ | ||
| 2659 | list_maint(&snac, title, 1); | ||
| 2660 | |||
| 2661 | xs *out = xs_dict_new(); | ||
| 2662 | |||
| 2663 | out = xs_dict_append(out, "title", title); | ||
| 2664 | out = xs_dict_append(out, "replies_policy", xs_dict_get_def(args, "replies_policy", "list")); | ||
| 2665 | out = xs_dict_append(out, "exclusive", xs_stock(XSTYPE_FALSE)); | ||
| 2666 | |||
| 2667 | *body = xs_json_dumps(out, 4); | ||
| 2668 | *ctype = "application/json"; | ||
| 2669 | status = 200; | ||
| 2670 | } | ||
| 2671 | else | ||
| 2672 | status = 422; | ||
| 2673 | } | ||
| 2674 | } | ||
| 2634 | 2675 | ||
| 2635 | /* user cleanup */ | 2676 | /* user cleanup */ |
| 2636 | if (logged_in) | 2677 | if (logged_in) |
| @@ -174,6 +174,8 @@ int is_hidden(snac *snac, const char *id); | |||
| 174 | void tag_index(const char *id, const xs_dict *obj); | 174 | void tag_index(const char *id, const xs_dict *obj); |
| 175 | xs_list *tag_search(char *tag, int skip, int show); | 175 | xs_list *tag_search(char *tag, int skip, int show); |
| 176 | 176 | ||
| 177 | xs_list *list_maint(snac *user, const char *list, int op); | ||
| 178 | |||
| 177 | int actor_add(const char *actor, xs_dict *msg); | 179 | int actor_add(const char *actor, xs_dict *msg); |
| 178 | int actor_get(const char *actor, xs_dict **data); | 180 | int actor_get(const char *actor, xs_dict **data); |
| 179 | int actor_get_refresh(snac *user, const char *actor, xs_dict **data); | 181 | int actor_get_refresh(snac *user, const char *actor, xs_dict **data); |