diff options
| author | 2024-04-29 08:29:18 +0200 | |
|---|---|---|
| committer | 2024-04-29 08:29:18 +0200 | |
| commit | 9a13e330f12aaf0fc1535e922efbf315308d252b (patch) | |
| tree | e16476d4490adbbb57072fa0936c92bfdd1134d5 | |
| parent | Start of list support. (diff) | |
| download | penes-snac2-9a13e330f12aaf0fc1535e922efbf315308d252b.tar.gz penes-snac2-9a13e330f12aaf0fc1535e922efbf315308d252b.tar.xz penes-snac2-9a13e330f12aaf0fc1535e922efbf315308d252b.zip | |
More work in lists.
| -rw-r--r-- | data.c | 62 | ||||
| -rw-r--r-- | mastoapi.c | 51 | ||||
| -rw-r--r-- | snac.h | 2 |
3 files changed, 90 insertions, 25 deletions
| @@ -1731,9 +1731,9 @@ xs_list *tag_search(char *tag, int skip, int show) | |||
| 1731 | 1731 | ||
| 1732 | /** lists **/ | 1732 | /** lists **/ |
| 1733 | 1733 | ||
| 1734 | xs_list *list_maint(snac *user, const char *list, int op) | 1734 | xs_val *list_maint(snac *user, const char *list, int op) |
| 1735 | { | 1735 | { |
| 1736 | xs_list *l = NULL; | 1736 | xs_val *l = NULL; |
| 1737 | 1737 | ||
| 1738 | switch (op) { | 1738 | switch (op) { |
| 1739 | case 0: /** list of lists **/ | 1739 | case 0: /** list of lists **/ |
| @@ -1752,10 +1752,12 @@ xs_list *list_maint(snac *user, const char *list, int op) | |||
| 1752 | fclose(f); | 1752 | fclose(f); |
| 1753 | 1753 | ||
| 1754 | title = xs_strip_i(title); | 1754 | title = xs_strip_i(title); |
| 1755 | xs *md5 = xs_md5_hex(title, strlen(title)); | 1755 | |
| 1756 | xs *v2 = xs_replace(v, ".id", ""); | ||
| 1757 | xs *l2 = xs_split(v2, "/"); | ||
| 1756 | 1758 | ||
| 1757 | /* return [ list_id, list_title ] */ | 1759 | /* return [ list_id, list_title ] */ |
| 1758 | l = xs_list_append(l, xs_list_append(xs_list_new(), md5, title)); | 1760 | l = xs_list_append(l, xs_list_append(xs_list_new(), xs_list_get(l2, -1), title)); |
| 1759 | } | 1761 | } |
| 1760 | } | 1762 | } |
| 1761 | } | 1763 | } |
| @@ -1764,26 +1766,58 @@ xs_list *list_maint(snac *user, const char *list, int op) | |||
| 1764 | 1766 | ||
| 1765 | case 1: /** create new list (list is the name) **/ | 1767 | case 1: /** create new list (list is the name) **/ |
| 1766 | { | 1768 | { |
| 1767 | FILE *f; | 1769 | xs *lol = list_maint(user, NULL, 0); |
| 1768 | xs *dir = xs_fmt("%s/list/", user->basedir); | 1770 | int c = 0; |
| 1769 | xs *md5 = xs_md5_hex(list, strlen(list)); | 1771 | xs_list *v; |
| 1772 | int add = 1; | ||
| 1770 | 1773 | ||
| 1771 | mkdirx(dir); | 1774 | /* check if this list name already exists */ |
| 1775 | while (xs_list_next(lol, &v, &c)) { | ||
| 1776 | if (strcmp(xs_list_get(v, 1), list) == 0) { | ||
| 1777 | add = 0; | ||
| 1778 | break; | ||
| 1779 | } | ||
| 1780 | } | ||
| 1772 | 1781 | ||
| 1773 | xs *fn = xs_fmt("%s%s.id", dir, md5); | 1782 | if (add) { |
| 1783 | FILE *f; | ||
| 1784 | xs *dir = xs_fmt("%s/list/", user->basedir); | ||
| 1785 | xs *id = xs_fmt("%010x", time(NULL)); | ||
| 1774 | 1786 | ||
| 1775 | if ((f = fopen(fn, "w")) != NULL) { | 1787 | mkdirx(dir); |
| 1776 | fprintf(f, "%s\n", list); | 1788 | |
| 1777 | fclose(f); | 1789 | xs *fn = xs_fmt("%s%s.id", dir, id); |
| 1790 | |||
| 1791 | if ((f = fopen(fn, "w")) != NULL) { | ||
| 1792 | fprintf(f, "%s\n", list); | ||
| 1793 | fclose(f); | ||
| 1794 | } | ||
| 1795 | |||
| 1796 | l = xs_stock(XSTYPE_TRUE); | ||
| 1778 | } | 1797 | } |
| 1798 | else | ||
| 1799 | l = xs_stock(XSTYPE_FALSE); | ||
| 1779 | } | 1800 | } |
| 1780 | 1801 | ||
| 1781 | break; | 1802 | break; |
| 1782 | 1803 | ||
| 1783 | case 2: /** delete list (list is md5 id) **/ | 1804 | case 2: /** delete list (list is the id) **/ |
| 1805 | { | ||
| 1806 | if (xs_is_hex(list)) { | ||
| 1807 | xs *fn = xs_fmt("%s/list/%s.id", user->basedir, list); | ||
| 1808 | unlink(fn); | ||
| 1809 | |||
| 1810 | fn = xs_replace_i(fn, ".id", ".lst"); | ||
| 1811 | unlink(fn); | ||
| 1812 | |||
| 1813 | fn = xs_replace_i(fn, ".list", ".idx"); | ||
| 1814 | unlink(fn); | ||
| 1815 | } | ||
| 1816 | } | ||
| 1817 | |||
| 1784 | break; | 1818 | break; |
| 1785 | 1819 | ||
| 1786 | case 3: /** list content (list is md5 id) **/ | 1820 | case 3: /** list content (list is the id) **/ |
| 1787 | break; | 1821 | break; |
| 1788 | } | 1822 | } |
| 1789 | 1823 | ||
| @@ -1766,7 +1766,6 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 1766 | } | 1766 | } |
| 1767 | else | 1767 | else |
| 1768 | if (strcmp(cmd, "/v1/lists") == 0) { /** **/ | 1768 | if (strcmp(cmd, "/v1/lists") == 0) { /** **/ |
| 1769 | /* snac does not support lists */ | ||
| 1770 | if (logged_in) { | 1769 | if (logged_in) { |
| 1771 | xs *lol = list_maint(&snac1, NULL, 0); | 1770 | xs *lol = list_maint(&snac1, NULL, 0); |
| 1772 | xs *l = xs_list_new(); | 1771 | xs *l = xs_list_new(); |
| @@ -2656,17 +2655,22 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, | |||
| 2656 | 2655 | ||
| 2657 | if (xs_type(title) == XSTYPE_STRING) { | 2656 | if (xs_type(title) == XSTYPE_STRING) { |
| 2658 | /* add the list */ | 2657 | /* add the list */ |
| 2659 | list_maint(&snac, title, 1); | ||
| 2660 | |||
| 2661 | xs *out = xs_dict_new(); | 2658 | xs *out = xs_dict_new(); |
| 2662 | 2659 | ||
| 2663 | out = xs_dict_append(out, "title", title); | 2660 | if (xs_type(list_maint(&snac, title, 1)) == XSTYPE_TRUE) { |
| 2664 | out = xs_dict_append(out, "replies_policy", xs_dict_get_def(args, "replies_policy", "list")); | 2661 | out = xs_dict_append(out, "title", title); |
| 2665 | out = xs_dict_append(out, "exclusive", xs_stock(XSTYPE_FALSE)); | 2662 | out = xs_dict_append(out, "replies_policy", xs_dict_get_def(args, "replies_policy", "list")); |
| 2663 | out = xs_dict_append(out, "exclusive", xs_stock(XSTYPE_FALSE)); | ||
| 2664 | |||
| 2665 | status = 200; | ||
| 2666 | } | ||
| 2667 | else { | ||
| 2668 | out = xs_dict_append(out, "error", "cannot create list"); | ||
| 2669 | status = 422; | ||
| 2670 | } | ||
| 2666 | 2671 | ||
| 2667 | *body = xs_json_dumps(out, 4); | 2672 | *body = xs_json_dumps(out, 4); |
| 2668 | *ctype = "application/json"; | 2673 | *ctype = "application/json"; |
| 2669 | status = 200; | ||
| 2670 | } | 2674 | } |
| 2671 | else | 2675 | else |
| 2672 | status = 422; | 2676 | status = 422; |
| @@ -2691,16 +2695,43 @@ int mastoapi_delete_handler(const xs_dict *req, const char *q_path, | |||
| 2691 | (void)b_size; | 2695 | (void)b_size; |
| 2692 | (void)ctype; | 2696 | (void)ctype; |
| 2693 | 2697 | ||
| 2698 | int status = 404; | ||
| 2699 | |||
| 2694 | if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/")) | 2700 | if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/")) |
| 2695 | return 0; | 2701 | return 0; |
| 2696 | 2702 | ||
| 2697 | srv_debug(1, xs_fmt("mastoapi_delete_handler %s", q_path)); | 2703 | snac snac = {0}; |
| 2704 | int logged_in = process_auth_token(&snac, req); | ||
| 2705 | |||
| 2698 | xs *cmd = xs_replace_n(q_path, "/api", "", 1); | 2706 | xs *cmd = xs_replace_n(q_path, "/api", "", 1); |
| 2707 | |||
| 2699 | if (xs_startswith(cmd, "/v1/push/subscription") || xs_startswith(cmd, "/v2/push/subscription")) { /** **/ | 2708 | if (xs_startswith(cmd, "/v1/push/subscription") || xs_startswith(cmd, "/v2/push/subscription")) { /** **/ |
| 2700 | // pretend we deleted it, since it doesn't exist anyway | 2709 | // pretend we deleted it, since it doesn't exist anyway |
| 2701 | return 200; | 2710 | status = 200; |
| 2702 | } | 2711 | } |
| 2703 | return 0; | 2712 | else |
| 2713 | if (xs_startswith(cmd, "/v1/lists/")) { | ||
| 2714 | if (logged_in) { | ||
| 2715 | xs *l = xs_split(cmd, "/"); | ||
| 2716 | char *id = xs_list_get(l, -1); | ||
| 2717 | |||
| 2718 | if (xs_is_hex(id)) { | ||
| 2719 | list_maint(&snac, id, 2); | ||
| 2720 | } | ||
| 2721 | |||
| 2722 | status = 200; | ||
| 2723 | } | ||
| 2724 | else | ||
| 2725 | status = 401; | ||
| 2726 | } | ||
| 2727 | |||
| 2728 | /* user cleanup */ | ||
| 2729 | if (logged_in) | ||
| 2730 | user_free(&snac); | ||
| 2731 | |||
| 2732 | srv_debug(1, xs_fmt("mastoapi_delete_handler %s %d", q_path, status)); | ||
| 2733 | |||
| 2734 | return status; | ||
| 2704 | } | 2735 | } |
| 2705 | 2736 | ||
| 2706 | 2737 | ||
| @@ -174,7 +174,7 @@ 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); | 177 | xs_val *list_maint(snac *user, const char *list, int op); |
| 178 | 178 | ||
| 179 | int actor_add(const char *actor, xs_dict *msg); | 179 | int actor_add(const char *actor, xs_dict *msg); |
| 180 | int actor_get(const char *actor, xs_dict **data); | 180 | int actor_get(const char *actor, xs_dict **data); |