diff options
| author | 2022-09-22 14:46:23 +0200 | |
|---|---|---|
| committer | 2022-09-22 14:46:23 +0200 | |
| commit | b5769aca9b8e93a725631563a32c7b98cba1db4e (patch) | |
| tree | bcb10f4c4a6f9982e3e5bf08523edb23083d3165 /data.c | |
| parent | New macro valid_status(). (diff) | |
| download | snac2-b5769aca9b8e93a725631563a32c7b98cba1db4e.tar.gz snac2-b5769aca9b8e93a725631563a32c7b98cba1db4e.tar.xz snac2-b5769aca9b8e93a725631563a32c7b98cba1db4e.zip | |
New function _timeline_parent().
Diffstat (limited to 'data.c')
| -rw-r--r-- | data.c | 77 |
1 files changed, 77 insertions, 0 deletions
| @@ -381,6 +381,80 @@ d_char *timeline_list(snac *snac) | |||
| 381 | } | 381 | } |
| 382 | 382 | ||
| 383 | 383 | ||
| 384 | void _timeline_parent(snac *snac, char *parent, char *child) | ||
| 385 | /* add child to parent's children list */ | ||
| 386 | { | ||
| 387 | if (parent != NULL) { | ||
| 388 | xs *pfn = _timeline_find_fn(snac, parent); | ||
| 389 | |||
| 390 | if (pfn != NULL) { | ||
| 391 | FILE *f; | ||
| 392 | |||
| 393 | if ((f = fopen(pfn, "r")) != NULL) { | ||
| 394 | xs *ji, *msg; | ||
| 395 | |||
| 396 | ji = xs_readall(f); | ||
| 397 | fclose(f); | ||
| 398 | |||
| 399 | msg = xs_json_loads(ji); | ||
| 400 | |||
| 401 | if (msg != NULL) { | ||
| 402 | xs *meta; | ||
| 403 | xs *children; | ||
| 404 | |||
| 405 | /* get the children list */ | ||
| 406 | meta = xs_dict_get(msg, "_snac"); | ||
| 407 | children = xs_dict_get(meta, "children"); | ||
| 408 | |||
| 409 | /* add */ | ||
| 410 | children = xs_list_append(children, child); | ||
| 411 | |||
| 412 | /* re-store */ | ||
| 413 | meta = xs_dict_set(meta, "children", children); | ||
| 414 | msg = xs_dict_set(msg, "_snac", meta); | ||
| 415 | |||
| 416 | xs *jo = xs_json_dumps_pp(msg, 4); | ||
| 417 | xs *ntid = tid(0); | ||
| 418 | xs *md5 = xs_md5_hex(parent, strlen(parent)); | ||
| 419 | xs *nfn = xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5); | ||
| 420 | |||
| 421 | if ((f = fopen(nfn, "w")) != NULL) { | ||
| 422 | fwrite(jo, strlen(jo), 1, f); | ||
| 423 | fclose(f); | ||
| 424 | |||
| 425 | snac_debug(snac, 1, | ||
| 426 | xs_fmt("_timeline_parent updated %s %s", parent, nfn)); | ||
| 427 | |||
| 428 | unlink(pfn); | ||
| 429 | |||
| 430 | /* generated by this user? link to local timeline */ | ||
| 431 | if (xs_startswith(parent, snac->actor)) { | ||
| 432 | xs *lfn = xs_replace(nfn, "/timeline/", "/local/"); | ||
| 433 | xs *olfn = xs_replace(pfn, "/timeline/", "/local/"); | ||
| 434 | |||
| 435 | link(nfn, lfn); | ||
| 436 | unlink(olfn); | ||
| 437 | |||
| 438 | snac_debug(snac, 1, | ||
| 439 | xs_fmt("_timeline_parent (local) updated %s %s", parent, lfn)); | ||
| 440 | } | ||
| 441 | |||
| 442 | /* retry with grampa */ | ||
| 443 | _timeline_parent(snac, xs_dict_get(meta, "parent"), parent); | ||
| 444 | } | ||
| 445 | else | ||
| 446 | snac_log(snac, xs_fmt("_timeline_parent error writing %s %s", parent, nfn)); | ||
| 447 | } | ||
| 448 | else | ||
| 449 | snac_log(snac, xs_fmt("_timeline_parent error reading %s %s", parent, pfn)); | ||
| 450 | } | ||
| 451 | else | ||
| 452 | snac_log(snac, xs_fmt("_timeline_parent error opening %s %s", parent, pfn)); | ||
| 453 | } | ||
| 454 | } | ||
| 455 | } | ||
| 456 | |||
| 457 | |||
| 384 | void timeline_add(snac *snac, char *id, char *msg, char *parent) | 458 | void timeline_add(snac *snac, char *id, char *msg, char *parent) |
| 385 | /* adds a message to the timeline */ | 459 | /* adds a message to the timeline */ |
| 386 | { | 460 | { |
| @@ -427,6 +501,9 @@ void timeline_add(snac *snac, char *id, char *msg, char *parent) | |||
| 427 | 501 | ||
| 428 | snac_debug(snac, 1, xs_fmt("timeline_add (local) %s %s", id, lfn)); | 502 | snac_debug(snac, 1, xs_fmt("timeline_add (local) %s %s", id, lfn)); |
| 429 | } | 503 | } |
| 504 | |||
| 505 | /* relink the parent */ | ||
| 506 | _timeline_parent(snac, parent, id); | ||
| 430 | } | 507 | } |
| 431 | 508 | ||
| 432 | 509 | ||