summaryrefslogtreecommitdiff
path: root/mastoapi.c
diff options
context:
space:
mode:
authorGravatar default2023-04-12 18:44:15 +0200
committerGravatar default2023-04-12 18:44:15 +0200
commit668f69dca90c3449a19ab21482a8b198ea11adae (patch)
tree1adc8b417a61d980407cba0f88148136643d6713 /mastoapi.c
parentUpdated RELEASE_NOTES. (diff)
downloadsnac2-668f69dca90c3449a19ab21482a8b198ea11adae.tar.gz
snac2-668f69dca90c3449a19ab21482a8b198ea11adae.tar.xz
snac2-668f69dca90c3449a19ab21482a8b198ea11adae.zip
Added the mastodon personal timeline.
Diffstat (limited to 'mastoapi.c')
-rw-r--r--mastoapi.c81
1 files changed, 60 insertions, 21 deletions
diff --git a/mastoapi.c b/mastoapi.c
index eec4119..cc48928 100644
--- a/mastoapi.c
+++ b/mastoapi.c
@@ -643,23 +643,23 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
643 xs_dict *args = xs_dict_get(req, "q_vars"); 643 xs_dict *args = xs_dict_get(req, "q_vars");
644 xs *cmd = xs_replace(q_path, "/api/v1", ""); 644 xs *cmd = xs_replace(q_path, "/api/v1", "");
645 645
646 snac snac = {0}; 646 snac snac1 = {0};
647 int logged_in = process_auth_token(&snac, req);; 647 int logged_in = process_auth_token(&snac1, req);
648 648
649 if (strcmp(cmd, "/accounts/verify_credentials") == 0) { 649 if (strcmp(cmd, "/accounts/verify_credentials") == 0) {
650 if (logged_in) { 650 if (logged_in) {
651 xs *acct = xs_dict_new(); 651 xs *acct = xs_dict_new();
652 652
653 acct = xs_dict_append(acct, "id", xs_dict_get(snac.config, "uid")); 653 acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
654 acct = xs_dict_append(acct, "username", xs_dict_get(snac.config, "uid")); 654 acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
655 acct = xs_dict_append(acct, "acct", xs_dict_get(snac.config, "uid")); 655 acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
656 acct = xs_dict_append(acct, "display_name", xs_dict_get(snac.config, "name")); 656 acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
657 acct = xs_dict_append(acct, "created_at", xs_dict_get(snac.config, "published")); 657 acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
658 acct = xs_dict_append(acct, "note", xs_dict_get(snac.config, "bio")); 658 acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
659 acct = xs_dict_append(acct, "url", snac.actor); 659 acct = xs_dict_append(acct, "url", snac1.actor);
660 660
661 xs *avatar = NULL; 661 xs *avatar = NULL;
662 char *av = xs_dict_get(snac.config, "avatar"); 662 char *av = xs_dict_get(snac1.config, "avatar");
663 663
664 if (xs_is_null(av) || *av == '\0') 664 if (xs_is_null(av) || *av == '\0')
665 avatar = xs_fmt("%s/susie.png", srv_baseurl); 665 avatar = xs_fmt("%s/susie.png", srv_baseurl);
@@ -677,6 +677,45 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
677 } 677 }
678 } 678 }
679 else 679 else
680 if (xs_startswith(cmd, "/accounts/") && xs_endswith(cmd, "/statuses")) {
681 /* the public list of posts of a user */
682 xs *l = xs_split(cmd, "/");
683
684 if (xs_list_len(l) == 4) {
685 snac snac2;
686 const char *uid = xs_list_get(l, 2);
687
688 if (user_open(&snac2, uid)) {
689 xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
690 xs *out = xs_list_new();
691 xs_list *p = timeline;
692 xs_str *v;
693
694 while (xs_list_iter(&p, &v)) {
695 xs *msg = NULL;
696
697 if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
698 /* add only posts by the author */
699 if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
700 xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
701 xs *st = mastoapi_status(&snac2, msg);
702
703 out = xs_list_append(out, st);
704 }
705 }
706 }
707
708 *body = xs_json_dumps_pp(out, 4);
709 *ctype = "application/json";
710 status = 200;
711
712 user_free(&snac2);
713 }
714 else
715 srv_debug(0, xs_fmt("mastoapi account statuses: bad user '%s'", uid));
716 }
717 }
718 else
680 if (strcmp(cmd, "/timelines/home") == 0) { 719 if (strcmp(cmd, "/timelines/home") == 0) {
681 /* the private timeline */ 720 /* the private timeline */
682 if (logged_in) { 721 if (logged_in) {
@@ -693,7 +732,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
693 if (limit == 0) 732 if (limit == 0)
694 limit = 20; 733 limit = 20;
695 734
696 xs *timeline = timeline_simple_list(&snac, "private", 0, XS_ALL); 735 xs *timeline = timeline_simple_list(&snac1, "private", 0, XS_ALL);
697 736
698 xs *out = xs_list_new(); 737 xs *out = xs_list_new();
699 xs_list *p = timeline; 738 xs_list *p = timeline;
@@ -724,7 +763,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
724 } 763 }
725 764
726 /* get the entry */ 765 /* get the entry */
727 if (!valid_status(timeline_get_by_md5(&snac, v, &msg))) 766 if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
728 continue; 767 continue;
729 768
730 /* discard non-Notes */ 769 /* discard non-Notes */
@@ -732,7 +771,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
732 continue; 771 continue;
733 772
734 /* convert the Note into a Mastodon status */ 773 /* convert the Note into a Mastodon status */
735 xs *st = mastoapi_status(&snac, msg); 774 xs *st = mastoapi_status(&snac1, msg);
736 775
737 if (st != NULL) 776 if (st != NULL)
738 out = xs_list_append(out, st); 777 out = xs_list_append(out, st);
@@ -879,10 +918,10 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
879 /* skip the 'fake' part of the id */ 918 /* skip the 'fake' part of the id */
880 id = MID_TO_MD5(id); 919 id = MID_TO_MD5(id);
881 920
882 if (valid_status(timeline_get_by_md5(&snac, id, &msg))) { 921 if (valid_status(timeline_get_by_md5(&snac1, id, &msg))) {
883 if (op == NULL) { 922 if (op == NULL) {
884 /* return the status itself */ 923 /* return the status itself */
885 out = mastoapi_status(&snac, msg); 924 out = mastoapi_status(&snac1, msg);
886 } 925 }
887 else 926 else
888 if (strcmp(op, "context") == 0) { 927 if (strcmp(op, "context") == 0) {
@@ -899,8 +938,8 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
899 while (object_parent(pid, pid, sizeof(pid))) { 938 while (object_parent(pid, pid, sizeof(pid))) {
900 xs *m2 = NULL; 939 xs *m2 = NULL;
901 940
902 if (valid_status(timeline_get_by_md5(&snac, pid, &m2))) { 941 if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
903 xs *st = mastoapi_status(&snac, m2); 942 xs *st = mastoapi_status(&snac1, m2);
904 anc = xs_list_append(anc, st); 943 anc = xs_list_append(anc, st);
905 } 944 }
906 else 945 else
@@ -914,8 +953,8 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
914 while (xs_list_iter(&p, &v)) { 953 while (xs_list_iter(&p, &v)) {
915 xs *m2 = NULL; 954 xs *m2 = NULL;
916 955
917 if (valid_status(timeline_get_by_md5(&snac, v, &m2))) { 956 if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
918 xs *st = mastoapi_status(&snac, m2); 957 xs *st = mastoapi_status(&snac1, m2);
919 des = xs_list_append(des, st); 958 des = xs_list_append(des, st);
920 } 959 }
921 } 960 }
@@ -944,7 +983,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
944 xs *actor2 = NULL; 983 xs *actor2 = NULL;
945 984
946 if (valid_status(object_get_by_md5(v, &actor2))) { 985 if (valid_status(object_get_by_md5(v, &actor2))) {
947 xs *acct2 = mastoapi_account(&snac, actor2); 986 xs *acct2 = mastoapi_account(&snac1, actor2);
948 987
949 out = xs_list_append(out, acct2); 988 out = xs_list_append(out, acct2);
950 } 989 }
@@ -964,7 +1003,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
964 1003
965 /* user cleanup */ 1004 /* user cleanup */
966 if (logged_in) 1005 if (logged_in)
967 user_free(&snac); 1006 user_free(&snac1);
968 1007
969 return status; 1008 return status;
970} 1009}