diff options
| author | 2022-09-28 07:05:23 +0200 | |
|---|---|---|
| committer | 2022-09-28 07:05:23 +0200 | |
| commit | fc6a12514643c9d19d0ddfbda37d79a0ae42f6d2 (patch) | |
| tree | 1f5b71660c4e2835f5f00ad6a53d454312809e5a /html.c | |
| parent | New function login() (untested). (diff) | |
| download | snac2-fc6a12514643c9d19d0ddfbda37d79a0ae42f6d2.tar.gz snac2-fc6a12514643c9d19d0ddfbda37d79a0ae42f6d2.tar.xz snac2-fc6a12514643c9d19d0ddfbda37d79a0ae42f6d2.zip | |
New function html_msg_icon().
Diffstat (limited to 'html.c')
| -rw-r--r-- | html.c | 147 |
1 files changed, 146 insertions, 1 deletions
| @@ -142,9 +142,154 @@ int login(snac *snac, char *headers) | |||
| 142 | } | 142 | } |
| 143 | 143 | ||
| 144 | 144 | ||
| 145 | d_char *html_msg_icon(snac *snac, d_char *s, char *msg) | ||
| 146 | { | ||
| 147 | char *actor_id; | ||
| 148 | xs *actor = NULL; | ||
| 149 | |||
| 150 | if ((actor_id = xs_dict_get(msg, "attributedTo")) == NULL) | ||
| 151 | actor_id = xs_dict_get(msg, "actor"); | ||
| 152 | |||
| 153 | if (actor_id && valid_status(actor_get(snac, actor_id, &actor))) { | ||
| 154 | xs *name = NULL; | ||
| 155 | xs *avatar = NULL; | ||
| 156 | char *v; | ||
| 157 | |||
| 158 | /* get the name */ | ||
| 159 | if ((v = xs_dict_get(actor, "name")) == NULL) { | ||
| 160 | if ((v = xs_dict_get(actor, "preferredUsername")) == NULL) { | ||
| 161 | v = "user"; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | name = xs_dup(v); | ||
| 166 | |||
| 167 | /* get the avatar */ | ||
| 168 | if ((v = xs_dict_get(actor, "icon")) != NULL && | ||
| 169 | (v = xs_dict_get(v, "url")) != NULL) { | ||
| 170 | avatar = xs_dup(v); | ||
| 171 | } | ||
| 172 | |||
| 173 | if (avatar == NULL) | ||
| 174 | avatar = xs_fmt("data:image/png;base64, %s", susie); | ||
| 175 | |||
| 176 | { | ||
| 177 | xs *s1 = xs_fmt("<p><img class=\"snac-avatar\" src=\"%s\"/>\n", avatar); | ||
| 178 | s = xs_str_cat(s, s1); | ||
| 179 | } | ||
| 180 | |||
| 181 | { | ||
| 182 | xs *s1 = xs_fmt("<a href=\"%s\" class=\"p-author h-card snac-author\">%s</a>", | ||
| 183 | actor, name); | ||
| 184 | s = xs_str_cat(s, s1); | ||
| 185 | } | ||
| 186 | |||
| 187 | if (strcmp(xs_dict_get(msg, "type"), "Note") == 0) { | ||
| 188 | xs *s1 = xs_fmt(" <a href=\"%s\">ยป</a>", xs_dict_get(msg, "id")); | ||
| 189 | s = xs_str_cat(s, s1); | ||
| 190 | } | ||
| 191 | |||
| 192 | if (!is_msg_public(snac, msg)) | ||
| 193 | s = xs_str_cat(s, " <span title=\"private\">🔒</span>"); | ||
| 194 | |||
| 195 | if ((v = xs_dict_get(msg, "published")) == NULL) | ||
| 196 | v = " "; | ||
| 197 | |||
| 198 | { | ||
| 199 | xs *s1 = xs_fmt("<br>\n<time class=\"dt-published snac-pubdate\">%s</time>\n", v); | ||
| 200 | s = xs_str_cat(s, s1); | ||
| 201 | } | ||
| 202 | |||
| 203 | s = xs_str_cat(s, "</div>\n"); | ||
| 204 | } | ||
| 205 | |||
| 206 | return s; | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | d_char *html_timeline(snac *snac, char *list, int local) | ||
| 211 | /* returns the HTML for the timeline */ | ||
| 212 | { | ||
| 213 | d_char *s = xs_str_new(NULL); | ||
| 214 | |||
| 215 | s = xs_str_cat(s, "<!DOCTYPE html>\n<html>\n"); | ||
| 216 | |||
| 217 | s = xs_str_cat(s, "<h1>HI</h1>\n"); | ||
| 218 | |||
| 219 | s = xs_str_cat(s, xs_fmt("len() == %d\n", xs_list_len(list))); | ||
| 220 | |||
| 221 | { | ||
| 222 | char *i = xs_list_get(list, 0); | ||
| 223 | xs *msg = timeline_get(snac, i); | ||
| 224 | |||
| 225 | s = html_msg_icon(snac, s, msg); | ||
| 226 | } | ||
| 227 | |||
| 228 | s = xs_str_cat(s, "</html>\n"); | ||
| 229 | |||
| 230 | return s; | ||
| 231 | } | ||
| 232 | |||
| 233 | |||
| 145 | int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype) | 234 | int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype) |
| 146 | { | 235 | { |
| 147 | int status = 0; | 236 | int status = 404; |
| 237 | snac snac; | ||
| 238 | char *uid, *p_path; | ||
| 239 | |||
| 240 | xs *l = xs_split_n(q_path, "/", 2); | ||
| 241 | |||
| 242 | uid = xs_list_get(l, 1); | ||
| 243 | if (!uid || !user_open(&snac, uid)) { | ||
| 244 | /* invalid user */ | ||
| 245 | srv_log(xs_fmt("html_get_handler bad user %s", uid)); | ||
| 246 | return 404; | ||
| 247 | } | ||
| 248 | |||
| 249 | p_path = xs_list_get(l, 2); | ||
| 250 | |||
| 251 | if (p_path == NULL) { | ||
| 252 | /* public timeline */ | ||
| 253 | xs *list = local_list(&snac, 0xfffffff); | ||
| 254 | |||
| 255 | *body = html_timeline(&snac, list, 1); | ||
| 256 | *b_size = strlen(*body); | ||
| 257 | status = 200; | ||
| 258 | } | ||
| 259 | else | ||
| 260 | if (strcmp(p_path, "admin") == 0) { | ||
| 261 | /* private timeline */ | ||
| 262 | |||
| 263 | if (!login(&snac, req)) | ||
| 264 | status = 401; | ||
| 265 | else { | ||
| 266 | xs *list = timeline_list(&snac, 0xfffffff); | ||
| 267 | |||
| 268 | *body = html_timeline(&snac, list, 0); | ||
| 269 | *b_size = strlen(*body); | ||
| 270 | status = 200; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | else | ||
| 274 | if (xs_startswith(p_path, "p/") == 0) { | ||
| 275 | /* a timeline with just one entry */ | ||
| 276 | } | ||
| 277 | else | ||
| 278 | if (xs_startswith(p_path, "s/") == 0) { | ||
| 279 | /* a static file */ | ||
| 280 | } | ||
| 281 | else | ||
| 282 | if (xs_startswith(p_path, "h/") == 0) { | ||
| 283 | /* an entry from the history */ | ||
| 284 | } | ||
| 285 | else | ||
| 286 | status = 404; | ||
| 287 | |||
| 288 | user_free(&snac); | ||
| 289 | |||
| 290 | if (valid_status(status)) { | ||
| 291 | *ctype = "text/html; charset=utf-8"; | ||
| 292 | } | ||
| 148 | 293 | ||
| 149 | return status; | 294 | return status; |
| 150 | } | 295 | } |