diff options
| author | 2022-11-18 11:08:20 +0100 | |
|---|---|---|
| committer | 2022-11-18 11:08:20 +0100 | |
| commit | c827b2641b1c696bcc5334e1de88c41a643b68c4 (patch) | |
| tree | 418c7401feacf4e61a8dac1fe0f3de572915168a | |
| parent | Updated documentation. (diff) | |
| download | snac2-c827b2641b1c696bcc5334e1de88c41a643b68c4.tar.gz snac2-c827b2641b1c696bcc5334e1de88c41a643b68c4.tar.xz snac2-c827b2641b1c696bcc5334e1de88c41a643b68c4.zip | |
Added rss on output.
| -rw-r--r-- | html.c | 91 |
1 files changed, 87 insertions, 4 deletions
| @@ -948,27 +948,47 @@ d_char *html_people(snac *snac) | |||
| 948 | 948 | ||
| 949 | int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype) | 949 | int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype) |
| 950 | { | 950 | { |
| 951 | char *accept = xs_dict_get(req, "accept"); | ||
| 951 | int status = 404; | 952 | int status = 404; |
| 952 | snac snac; | 953 | snac snac; |
| 953 | char *uid, *p_path; | 954 | xs *uid = NULL; |
| 955 | char *p_path; | ||
| 954 | int cache = 1; | 956 | int cache = 1; |
| 955 | char *v; | 957 | char *v; |
| 956 | 958 | ||
| 957 | xs *l = xs_split_n(q_path, "/", 2); | 959 | xs *l = xs_split_n(q_path, "/", 2); |
| 960 | v = xs_list_get(l, 1); | ||
| 961 | |||
| 962 | if (xs_is_null(v)) { | ||
| 963 | srv_log(xs_fmt("html_get_handler bad query '%s'", q_path)); | ||
| 964 | return 404; | ||
| 965 | } | ||
| 966 | |||
| 967 | uid = xs_dup(v); | ||
| 968 | |||
| 969 | /* rss? */ | ||
| 970 | if (xs_endswith(uid, ".rss")) { | ||
| 971 | uid = xs_crop(uid, 0, -4); | ||
| 972 | p_path = ".rss"; | ||
| 973 | } | ||
| 974 | else | ||
| 975 | p_path = xs_list_get(l, 2); | ||
| 958 | 976 | ||
| 959 | uid = xs_list_get(l, 1); | ||
| 960 | if (!uid || !user_open(&snac, uid)) { | 977 | if (!uid || !user_open(&snac, uid)) { |
| 961 | /* invalid user */ | 978 | /* invalid user */ |
| 962 | srv_log(xs_fmt("html_get_handler bad user %s", uid)); | 979 | srv_log(xs_fmt("html_get_handler bad user %s", uid)); |
| 963 | return 404; | 980 | return 404; |
| 964 | } | 981 | } |
| 965 | 982 | ||
| 983 | /* return the RSS if requested by Accept header */ | ||
| 984 | if (xs_str_in(accept, "text/xml") != -1 || | ||
| 985 | xs_str_in(accept, "application/rss+xml") != -1) | ||
| 986 | p_path = ".rss"; | ||
| 987 | |||
| 966 | /* check if server config variable 'disable_cache' is set */ | 988 | /* check if server config variable 'disable_cache' is set */ |
| 967 | if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE) | 989 | if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE) |
| 968 | cache = 0; | 990 | cache = 0; |
| 969 | 991 | ||
| 970 | p_path = xs_list_get(l, 2); | ||
| 971 | |||
| 972 | if (p_path == NULL) { | 992 | if (p_path == NULL) { |
| 973 | /* public timeline */ | 993 | /* public timeline */ |
| 974 | xs *h = xs_str_localtime(0, "%Y-%m.html"); | 994 | xs *h = xs_str_localtime(0, "%Y-%m.html"); |
| @@ -1069,6 +1089,69 @@ int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char * | |||
| 1069 | } | 1089 | } |
| 1070 | } | 1090 | } |
| 1071 | else | 1091 | else |
| 1092 | if (strcmp(p_path, ".rss") == 0) { | ||
| 1093 | /* public timeline in RSS format */ | ||
| 1094 | d_char *rss; | ||
| 1095 | xs *elems = local_list(&snac, 20); | ||
| 1096 | char *p, *v; | ||
| 1097 | |||
| 1098 | rss = xs_fmt( | ||
| 1099 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" | ||
| 1100 | "<rss version=\"0.91\">\n" | ||
| 1101 | "<channel>\n" | ||
| 1102 | "<title>%s</title>\n" | ||
| 1103 | "<language>en</language>\n" | ||
| 1104 | "<link>%s.rss</link>\n" | ||
| 1105 | "<description>%s</description>\n", | ||
| 1106 | snac.actor, | ||
| 1107 | snac.actor, | ||
| 1108 | snac.actor | ||
| 1109 | ); | ||
| 1110 | |||
| 1111 | p = elems; | ||
| 1112 | while (xs_list_iter(&p, &v)) { | ||
| 1113 | xs *msg = timeline_get(&snac, v); | ||
| 1114 | char *id = xs_dict_get(msg, "id"); | ||
| 1115 | |||
| 1116 | if (!xs_startswith(id, snac.actor)) | ||
| 1117 | continue; | ||
| 1118 | |||
| 1119 | xs *content = sanitize(xs_dict_get(msg, "content")); | ||
| 1120 | xs *title = xs_dup(content); | ||
| 1121 | |||
| 1122 | /* escape tags */ | ||
| 1123 | content = xs_replace_i(content, "<", "<"); | ||
| 1124 | content = xs_replace_i(content, ">", ">"); | ||
| 1125 | |||
| 1126 | /* cut title in the first tag start */ | ||
| 1127 | if ((v = strchr(title, '<')) != NULL) | ||
| 1128 | *v = '\0'; | ||
| 1129 | |||
| 1130 | if (strlen(title) > 40) { | ||
| 1131 | title = xs_crop(title, 0, 40); | ||
| 1132 | title = xs_str_cat(title, "..."); | ||
| 1133 | } | ||
| 1134 | |||
| 1135 | xs *s = xs_fmt( | ||
| 1136 | "<item>\n" | ||
| 1137 | "<title>%s</title>\n" | ||
| 1138 | "<link>%s</link>\n" | ||
| 1139 | "<description>%s</description>\n" | ||
| 1140 | "</item>\n", | ||
| 1141 | *title ? title : "...", id, content | ||
| 1142 | ); | ||
| 1143 | |||
| 1144 | rss = xs_str_cat(rss, s); | ||
| 1145 | } | ||
| 1146 | |||
| 1147 | rss = xs_str_cat(rss, "</channel>\n</rss>\n"); | ||
| 1148 | |||
| 1149 | *body = rss; | ||
| 1150 | *b_size = strlen(rss); | ||
| 1151 | *ctype = "application/rss+xml; charset=utf-8"; | ||
| 1152 | status = 200; | ||
| 1153 | } | ||
| 1154 | else | ||
| 1072 | status = 404; | 1155 | status = 404; |
| 1073 | 1156 | ||
| 1074 | user_free(&snac); | 1157 | user_free(&snac); |