diff options
| author | 2022-12-02 20:28:27 +0100 | |
|---|---|---|
| committer | 2022-12-02 20:28:27 +0100 | |
| commit | 67d57252ea094491420f20de244b74ff112382f2 (patch) | |
| tree | 1ce0d66f40f912c6d95643146e1dd40946f3781d | |
| parent | Fixed one post queries. (diff) | |
| download | snac2-67d57252ea094491420f20de244b74ff112382f2.tar.gz snac2-67d57252ea094491420f20de244b74ff112382f2.tar.xz snac2-67d57252ea094491420f20de244b74ff112382f2.zip | |
The old timeline is no longer maintained.
| -rw-r--r-- | data.c | 235 |
1 files changed, 11 insertions, 224 deletions
| @@ -847,146 +847,6 @@ d_char *_timeline_new_fn(snac *snac, char *id) | |||
| 847 | } | 847 | } |
| 848 | 848 | ||
| 849 | 849 | ||
| 850 | int _timeline_write(snac *snac, char *id, char *msg, char *parent, char *referrer) | ||
| 851 | /* writes a timeline entry and refreshes the ancestors */ | ||
| 852 | { | ||
| 853 | xs *fn = _timeline_new_fn(snac, id); | ||
| 854 | xs *pfn = NULL; | ||
| 855 | xs *p_msg = NULL; | ||
| 856 | FILE *f; | ||
| 857 | |||
| 858 | if (!xs_is_null(parent)) { | ||
| 859 | /* get the parent */ | ||
| 860 | pfn = _timeline_find_fn(snac, parent); | ||
| 861 | |||
| 862 | if (pfn != NULL && (f = fopen(pfn, "r")) != NULL) { | ||
| 863 | xs *j; | ||
| 864 | |||
| 865 | j = xs_readall(f); | ||
| 866 | fclose(f); | ||
| 867 | |||
| 868 | p_msg = xs_json_loads(j); | ||
| 869 | } | ||
| 870 | } | ||
| 871 | |||
| 872 | /* write the message */ | ||
| 873 | if ((f = fopen(fn, "w")) != NULL) { | ||
| 874 | xs *j = xs_json_dumps_pp(msg, 4); | ||
| 875 | |||
| 876 | fwrite(j, strlen(j), 1, f); | ||
| 877 | fclose(f); | ||
| 878 | |||
| 879 | snac_debug(snac, 1, xs_fmt("_timeline_write %s %s", id, fn)); | ||
| 880 | } | ||
| 881 | |||
| 882 | /* related to this user? link to local timeline */ | ||
| 883 | if (xs_startswith(id, snac->actor) || | ||
| 884 | (!xs_is_null(parent) && xs_startswith(parent, snac->actor)) || | ||
| 885 | (!xs_is_null(referrer) && xs_startswith(referrer, snac->actor))) { | ||
| 886 | xs *lfn = xs_replace(fn, "/timeline/", "/local/"); | ||
| 887 | link(fn, lfn); | ||
| 888 | |||
| 889 | snac_debug(snac, 1, xs_fmt("_timeline_write (local) %s %s", id, lfn)); | ||
| 890 | } | ||
| 891 | |||
| 892 | if (p_msg != NULL) { | ||
| 893 | /* update the parent, adding this id to its children list */ | ||
| 894 | xs *meta = xs_dup(xs_dict_get(p_msg, "_snac")); | ||
| 895 | xs *children = xs_dup(xs_dict_get(meta, "children")); | ||
| 896 | |||
| 897 | /* add the child if it's not already there */ | ||
| 898 | if (xs_list_in(children, id) == -1) | ||
| 899 | children = xs_list_append(children, id); | ||
| 900 | |||
| 901 | /* re-store */ | ||
| 902 | meta = xs_dict_set(meta, "children", children); | ||
| 903 | p_msg = xs_dict_set(p_msg, "_snac", meta); | ||
| 904 | |||
| 905 | xs *nfn = _timeline_new_fn(snac, parent); | ||
| 906 | |||
| 907 | if ((f = fopen(nfn, "w")) != NULL) { | ||
| 908 | xs *j = xs_json_dumps_pp(p_msg, 4); | ||
| 909 | |||
| 910 | fwrite(j, strlen(j), 1, f); | ||
| 911 | fclose(f); | ||
| 912 | |||
| 913 | unlink(pfn); | ||
| 914 | |||
| 915 | snac_debug(snac, 1, | ||
| 916 | xs_fmt("_timeline_write updated parent %s %s", parent, nfn)); | ||
| 917 | |||
| 918 | /* try to do the same with the local */ | ||
| 919 | xs *olfn = xs_replace(pfn, "/timeline/", "/local/"); | ||
| 920 | |||
| 921 | if (unlink(olfn) != -1 || xs_startswith(id, snac->actor)) { | ||
| 922 | xs *nlfn = xs_replace(nfn, "/timeline/", "/local/"); | ||
| 923 | |||
| 924 | link(nfn, nlfn); | ||
| 925 | |||
| 926 | snac_debug(snac, 1, | ||
| 927 | xs_fmt("_timeline_write updated parent (local) %s %s", parent, nlfn)); | ||
| 928 | } | ||
| 929 | } | ||
| 930 | else | ||
| 931 | return 0; | ||
| 932 | |||
| 933 | /* now iterate all parents up, just renaming the files */ | ||
| 934 | xs *grampa = xs_dup(xs_dict_get(meta, "parent")); | ||
| 935 | |||
| 936 | int max_levels = 20; | ||
| 937 | |||
| 938 | while (!xs_is_null(grampa)) { | ||
| 939 | xs *gofn = _timeline_find_fn(snac, grampa); | ||
| 940 | |||
| 941 | if (gofn == NULL) | ||
| 942 | break; | ||
| 943 | |||
| 944 | /* create the new filename */ | ||
| 945 | xs *gnfn = _timeline_new_fn(snac, grampa); | ||
| 946 | |||
| 947 | rename(gofn, gnfn); | ||
| 948 | |||
| 949 | snac_debug(snac, 1, | ||
| 950 | xs_fmt("_timeline_write updated grampa %s %s", grampa, gnfn)); | ||
| 951 | |||
| 952 | /* try to do the same with the local */ | ||
| 953 | xs *golfn = xs_replace(gofn, "/timeline/", "/local/"); | ||
| 954 | |||
| 955 | if (unlink(golfn) != -1) { | ||
| 956 | xs *gnlfn = xs_replace(gnfn, "/timeline/", "/local/"); | ||
| 957 | |||
| 958 | link(gnfn, gnlfn); | ||
| 959 | |||
| 960 | snac_debug(snac, 1, | ||
| 961 | xs_fmt("_timeline_write updated grampa (local) %s %s", parent, gnlfn)); | ||
| 962 | } | ||
| 963 | |||
| 964 | /* now open it and get its own parent */ | ||
| 965 | if ((f = fopen(gnfn, "r")) != NULL) { | ||
| 966 | xs *j = xs_readall(f); | ||
| 967 | fclose(f); | ||
| 968 | |||
| 969 | xs *g_msg = xs_json_loads(j); | ||
| 970 | char *meta = xs_dict_get(g_msg, "_snac"); | ||
| 971 | char *p = xs_dict_get(meta, "parent"); | ||
| 972 | |||
| 973 | xs_free(grampa); | ||
| 974 | grampa = xs_dup(p); | ||
| 975 | } | ||
| 976 | else | ||
| 977 | break; | ||
| 978 | |||
| 979 | if (--max_levels == 0) { | ||
| 980 | snac_debug(snac, 1, xs_dup("_timeline_write maximum grampa levels reached")); | ||
| 981 | break; | ||
| 982 | } | ||
| 983 | } | ||
| 984 | } | ||
| 985 | |||
| 986 | return 1; | ||
| 987 | } | ||
| 988 | |||
| 989 | |||
| 990 | void timeline_update_indexes(snac *snac, const char *id) | 850 | void timeline_update_indexes(snac *snac, const char *id) |
| 991 | /* updates the indexes */ | 851 | /* updates the indexes */ |
| 992 | { | 852 | { |
| @@ -1000,43 +860,22 @@ void timeline_update_indexes(snac *snac, const char *id) | |||
| 1000 | int timeline_add(snac *snac, char *id, char *o_msg, char *parent, char *referrer) | 860 | int timeline_add(snac *snac, char *id, char *o_msg, char *parent, char *referrer) |
| 1001 | /* adds a message to the timeline */ | 861 | /* adds a message to the timeline */ |
| 1002 | { | 862 | { |
| 1003 | xs *pfn = _timeline_find_fn(snac, id); | 863 | int ret = object_add(id, o_msg); |
| 1004 | int ret = 0; | 864 | timeline_update_indexes(snac, id); |
| 1005 | 865 | ||
| 1006 | if (pfn != NULL) { | 866 | snac_debug(snac, 1, xs_fmt("timeline_add %s", id)); |
| 1007 | snac_log(snac, xs_fmt("timeline_add refusing rewrite %s %s", id, pfn)); | ||
| 1008 | return 0; | ||
| 1009 | } | ||
| 1010 | |||
| 1011 | xs *msg = xs_dup(o_msg); | ||
| 1012 | xs *md; | ||
| 1013 | |||
| 1014 | /* add new metadata */ | ||
| 1015 | md = xs_json_loads("{" | ||
| 1016 | "\"children\": []," | ||
| 1017 | "\"liked_by\": []," | ||
| 1018 | "\"announced_by\": []," | ||
| 1019 | "\"version\": \"" USER_AGENT "\"," | ||
| 1020 | "\"referrer\": null," | ||
| 1021 | "\"parent\": null" | ||
| 1022 | "}"); | ||
| 1023 | |||
| 1024 | if (!xs_is_null(parent)) | ||
| 1025 | md = xs_dict_set(md, "parent", parent); | ||
| 1026 | 867 | ||
| 1027 | if (!xs_is_null(referrer)) | 868 | return ret; |
| 1028 | md = xs_dict_set(md, "referrer", referrer); | 869 | } |
| 1029 | |||
| 1030 | msg = xs_dict_set(msg, "_snac", md); | ||
| 1031 | 870 | ||
| 1032 | if ((ret = _timeline_write(snac, id, msg, parent, referrer))) { | ||
| 1033 | snac_debug(snac, 1, xs_fmt("timeline_add %s", id)); | ||
| 1034 | 871 | ||
| 1035 | object_add(id, o_msg); | 872 | void timeline_admire(snac *snac, char *o_msg, char *id, char *admirer, int like) |
| 1036 | timeline_update_indexes(snac, id); | 873 | /* updates a timeline entry with a new admiration */ |
| 1037 | } | 874 | { |
| 875 | object_admire(id, admirer, like); | ||
| 1038 | 876 | ||
| 1039 | return ret; | 877 | snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s", |
| 878 | like ? "Like" : "Announce", id, admirer)); | ||
| 1040 | } | 879 | } |
| 1041 | 880 | ||
| 1042 | 881 | ||
| @@ -1103,58 +942,6 @@ d_char *timeline_list(snac *snac, const char *idx_name, int max) | |||
| 1103 | } | 942 | } |
| 1104 | 943 | ||
| 1105 | 944 | ||
| 1106 | void timeline_admire(snac *snac, char *o_msg, char *id, char *admirer, int like) | ||
| 1107 | /* updates a timeline entry with a new admiration */ | ||
| 1108 | { | ||
| 1109 | xs *ofn = _timeline_find_fn(snac, id); | ||
| 1110 | FILE *f; | ||
| 1111 | |||
| 1112 | if (ofn != NULL && (f = fopen(ofn, "r")) != NULL) { | ||
| 1113 | xs *j1 = xs_readall(f); | ||
| 1114 | fclose(f); | ||
| 1115 | |||
| 1116 | xs *msg = xs_json_loads(j1); | ||
| 1117 | xs *meta = xs_dup(xs_dict_get(msg, "_snac")); | ||
| 1118 | xs *list; | ||
| 1119 | |||
| 1120 | if (like) | ||
| 1121 | list = xs_dup(xs_dict_get(meta, "liked_by")); | ||
| 1122 | else | ||
| 1123 | list = xs_dup(xs_dict_get(meta, "announced_by")); | ||
| 1124 | |||
| 1125 | /* add the admirer if it's not already there */ | ||
| 1126 | if (xs_list_in(list, admirer) == -1) | ||
| 1127 | list = xs_list_append(list, admirer); | ||
| 1128 | |||
| 1129 | /* set the admirer as the referrer (if not already set or it's us) */ | ||
| 1130 | if (!like && (xs_is_null(xs_dict_get(meta, "referrer")) || | ||
| 1131 | strcmp(admirer, snac->actor) == 0)) | ||
| 1132 | meta = xs_dict_set(meta, "referrer", admirer); | ||
| 1133 | |||
| 1134 | /* re-store */ | ||
| 1135 | if (like) | ||
| 1136 | meta = xs_dict_set(meta, "liked_by", list); | ||
| 1137 | else | ||
| 1138 | meta = xs_dict_set(meta, "announced_by", list); | ||
| 1139 | |||
| 1140 | msg = xs_dict_set(msg, "_snac", meta); | ||
| 1141 | |||
| 1142 | unlink(ofn); | ||
| 1143 | ofn = xs_replace_i(ofn, "/timeline/", "/local/"); | ||
| 1144 | unlink(ofn); | ||
| 1145 | |||
| 1146 | _timeline_write(snac, id, msg, xs_dict_get(meta, "parent"), like ? NULL : admirer); | ||
| 1147 | |||
| 1148 | snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s", | ||
| 1149 | like ? "Like" : "Announce", id, admirer)); | ||
| 1150 | } | ||
| 1151 | else | ||
| 1152 | snac_log(snac, xs_fmt("timeline_admire ignored for unknown object %s", id)); | ||
| 1153 | |||
| 1154 | object_admire(id, admirer, like); | ||
| 1155 | } | ||
| 1156 | |||
| 1157 | |||
| 1158 | /** following **/ | 945 | /** following **/ |
| 1159 | 946 | ||
| 1160 | /* this needs special treatment and cannot use the object db as is, | 947 | /* this needs special treatment and cannot use the object db as is, |