diff options
| author | 2022-09-25 17:42:39 +0200 | |
|---|---|---|
| committer | 2022-09-25 17:42:39 +0200 | |
| commit | bb70101feca64224759581d73404240bb4d35f5b (patch) | |
| tree | 364f29f6616d166df53bbc10a8b9026ea55ff8e0 | |
| parent | Some log tweaks. (diff) | |
| download | snac2-bb70101feca64224759581d73404240bb4d35f5b.tar.gz snac2-bb70101feca64224759581d73404240bb4d35f5b.tar.xz snac2-bb70101feca64224759581d73404240bb4d35f5b.zip | |
New function timeline_admire().
| -rw-r--r-- | activitypub.c | 11 | ||||
| -rw-r--r-- | data.c | 128 | ||||
| -rw-r--r-- | snac.h | 1 |
3 files changed, 104 insertions, 36 deletions
diff --git a/activitypub.c b/activitypub.c index ba45395..07cf5d7 100644 --- a/activitypub.c +++ b/activitypub.c | |||
| @@ -221,7 +221,16 @@ void process_message(snac *snac, char *msg, char *req) | |||
| 221 | if (strcmp(type, "Accept") == 0) { | 221 | if (strcmp(type, "Accept") == 0) { |
| 222 | } | 222 | } |
| 223 | else | 223 | else |
| 224 | if (strcmp(type, "Like") == 0 || strcmp(type, "Announce") == 0) { | 224 | */ |
| 225 | if (strcmp(type, "Like") == 0) { | ||
| 226 | if (xs_type(object) == XSTYPE_STRING) | ||
| 227 | timeline_admire(snac, object, actor, 1); | ||
| 228 | else | ||
| 229 | snac_debug(snac, 2, xs_fmt("xs_type for 'Like' object not string")); | ||
| 230 | } | ||
| 231 | else | ||
| 232 | /* | ||
| 233 | || strcmp(type, "Announce") == 0) { | ||
| 225 | } | 234 | } |
| 226 | else | 235 | else |
| 227 | if (strcmp(type, "Update") == 0) { | 236 | if (strcmp(type, "Update") == 0) { |
| @@ -391,35 +391,11 @@ d_char *_timeline_new_fn(snac *snac, char *id) | |||
| 391 | } | 391 | } |
| 392 | 392 | ||
| 393 | 393 | ||
| 394 | void timeline_add(snac *snac, char *id, char *o_msg, char *parent) | 394 | void _timeline_write(snac *snac, char *id, char *msg, char *parent) |
| 395 | /* adds a message to the timeline */ | 395 | /* writes a timeline entry and refreshes the ancestors */ |
| 396 | { | 396 | { |
| 397 | xs *pfn = _timeline_find_fn(snac, id); | ||
| 398 | FILE *f; | ||
| 399 | |||
| 400 | if (pfn != NULL) { | ||
| 401 | snac_log(snac, xs_fmt("timeline_add refusing rewrite %s %s", id, pfn)); | ||
| 402 | return; | ||
| 403 | } | ||
| 404 | |||
| 405 | /* build the new filename */ | ||
| 406 | xs *fn = _timeline_new_fn(snac, id); | 397 | xs *fn = _timeline_new_fn(snac, id); |
| 407 | xs *msg = xs_dup(o_msg); | 398 | FILE *f; |
| 408 | xs *md; | ||
| 409 | |||
| 410 | /* add metadata */ | ||
| 411 | md = xs_json_loads("{" | ||
| 412 | "\"children\": []," | ||
| 413 | "\"liked_by\": []," | ||
| 414 | "\"announced_by\": []," | ||
| 415 | "\"version\": \"snac/2.x\"," | ||
| 416 | "\"parent\": null" | ||
| 417 | "}"); | ||
| 418 | |||
| 419 | if (!xs_is_null(parent)) | ||
| 420 | md = xs_dict_set(md, "parent", parent); | ||
| 421 | |||
| 422 | msg = xs_dict_set(msg, "_snac", md); | ||
| 423 | 399 | ||
| 424 | if ((f = fopen(fn, "w")) != NULL) { | 400 | if ((f = fopen(fn, "w")) != NULL) { |
| 425 | xs *j = xs_json_dumps_pp(msg, 4); | 401 | xs *j = xs_json_dumps_pp(msg, 4); |
| @@ -427,7 +403,7 @@ void timeline_add(snac *snac, char *id, char *o_msg, char *parent) | |||
| 427 | fwrite(j, strlen(j), 1, f); | 403 | fwrite(j, strlen(j), 1, f); |
| 428 | fclose(f); | 404 | fclose(f); |
| 429 | 405 | ||
| 430 | snac_debug(snac, 1, xs_fmt("timeline_add %s %s", id, fn)); | 406 | snac_debug(snac, 1, xs_fmt("_timeline_write %s %s", id, fn)); |
| 431 | } | 407 | } |
| 432 | 408 | ||
| 433 | /* related to this user? link to local timeline */ | 409 | /* related to this user? link to local timeline */ |
| @@ -436,7 +412,7 @@ void timeline_add(snac *snac, char *id, char *o_msg, char *parent) | |||
| 436 | xs *lfn = xs_replace(fn, "/timeline/", "/local/"); | 412 | xs *lfn = xs_replace(fn, "/timeline/", "/local/"); |
| 437 | link(fn, lfn); | 413 | link(fn, lfn); |
| 438 | 414 | ||
| 439 | snac_debug(snac, 1, xs_fmt("timeline_add (local) %s %s", id, lfn)); | 415 | snac_debug(snac, 1, xs_fmt("_timeline_write (local) %s %s", id, lfn)); |
| 440 | } | 416 | } |
| 441 | 417 | ||
| 442 | if (!xs_is_null(parent)) { | 418 | if (!xs_is_null(parent)) { |
| @@ -459,8 +435,9 @@ void timeline_add(snac *snac, char *id, char *o_msg, char *parent) | |||
| 459 | xs *meta = xs_dup(xs_dict_get(p_msg, "_snac")); | 435 | xs *meta = xs_dup(xs_dict_get(p_msg, "_snac")); |
| 460 | xs *children = xs_dup(xs_dict_get(meta, "children")); | 436 | xs *children = xs_dup(xs_dict_get(meta, "children")); |
| 461 | 437 | ||
| 462 | /* add the child */ | 438 | /* add the child if it's not already there */ |
| 463 | children = xs_list_append(children, id); | 439 | if (xs_list_in(children, id) == -1) |
| 440 | children = xs_list_append(children, id); | ||
| 464 | 441 | ||
| 465 | /* re-store */ | 442 | /* re-store */ |
| 466 | meta = xs_dict_set(meta, "children", children); | 443 | meta = xs_dict_set(meta, "children", children); |
| @@ -476,7 +453,8 @@ void timeline_add(snac *snac, char *id, char *o_msg, char *parent) | |||
| 476 | 453 | ||
| 477 | unlink(pfn); | 454 | unlink(pfn); |
| 478 | 455 | ||
| 479 | snac_debug(snac, 2, xs_fmt("updated parent %s %s", parent, nfn)); | 456 | snac_debug(snac, 1, |
| 457 | xs_fmt("_timeline_write updated parent %s %s", parent, nfn)); | ||
| 480 | 458 | ||
| 481 | /* try to do the same with the local */ | 459 | /* try to do the same with the local */ |
| 482 | xs *olfn = xs_replace(pfn, "/timeline/", "/local/"); | 460 | xs *olfn = xs_replace(pfn, "/timeline/", "/local/"); |
| @@ -486,7 +464,8 @@ void timeline_add(snac *snac, char *id, char *o_msg, char *parent) | |||
| 486 | 464 | ||
| 487 | link(nfn, nlfn); | 465 | link(nfn, nlfn); |
| 488 | 466 | ||
| 489 | snac_debug(snac, 2, xs_fmt("updated parent (local) %s %s", parent, nlfn)); | 467 | snac_debug(snac, 1, |
| 468 | xs_fmt("_timeline_write updated parent (local) %s %s", parent, nlfn)); | ||
| 490 | } | 469 | } |
| 491 | } | 470 | } |
| 492 | else | 471 | else |
| @@ -506,7 +485,8 @@ void timeline_add(snac *snac, char *id, char *o_msg, char *parent) | |||
| 506 | 485 | ||
| 507 | rename(gofn, gnfn); | 486 | rename(gofn, gnfn); |
| 508 | 487 | ||
| 509 | snac_debug(snac, 2, xs_fmt("updated grampa %s %s", grampa, gnfn)); | 488 | snac_debug(snac, 2, |
| 489 | xs_fmt("_timeline_write updated grampa %s %s", grampa, gnfn)); | ||
| 510 | 490 | ||
| 511 | /* try to do the same with the local */ | 491 | /* try to do the same with the local */ |
| 512 | xs *golfn = xs_replace(gofn, "/timeline/", "/local/"); | 492 | xs *golfn = xs_replace(gofn, "/timeline/", "/local/"); |
| @@ -516,7 +496,8 @@ void timeline_add(snac *snac, char *id, char *o_msg, char *parent) | |||
| 516 | 496 | ||
| 517 | link(gnfn, gnlfn); | 497 | link(gnfn, gnlfn); |
| 518 | 498 | ||
| 519 | snac_debug(snac, 2, xs_fmt("updated grampa (local) %s %s", parent, gnlfn)); | 499 | snac_debug(snac, 2, |
| 500 | xs_fmt("_timeline_write updated grampa (local) %s %s", parent, gnlfn)); | ||
| 520 | } | 501 | } |
| 521 | 502 | ||
| 522 | /* now open it and get its own parent */ | 503 | /* now open it and get its own parent */ |
| @@ -540,6 +521,83 @@ void timeline_add(snac *snac, char *id, char *o_msg, char *parent) | |||
| 540 | } | 521 | } |
| 541 | 522 | ||
| 542 | 523 | ||
| 524 | void timeline_add(snac *snac, char *id, char *o_msg, char *parent) | ||
| 525 | /* adds a message to the timeline */ | ||
| 526 | { | ||
| 527 | xs *pfn = _timeline_find_fn(snac, id); | ||
| 528 | |||
| 529 | if (pfn != NULL) { | ||
| 530 | snac_log(snac, xs_fmt("timeline_add refusing rewrite %s %s", id, pfn)); | ||
| 531 | return; | ||
| 532 | } | ||
| 533 | |||
| 534 | xs *msg = xs_dup(o_msg); | ||
| 535 | xs *md; | ||
| 536 | |||
| 537 | /* add new metadata */ | ||
| 538 | md = xs_json_loads("{" | ||
| 539 | "\"children\": []," | ||
| 540 | "\"liked_by\": []," | ||
| 541 | "\"announced_by\": []," | ||
| 542 | "\"version\": \"snac/2.x\"," | ||
| 543 | "\"parent\": null" | ||
| 544 | "}"); | ||
| 545 | |||
| 546 | if (!xs_is_null(parent)) | ||
| 547 | md = xs_dict_set(md, "parent", parent); | ||
| 548 | |||
| 549 | msg = xs_dict_set(msg, "_snac", md); | ||
| 550 | |||
| 551 | _timeline_write(snac, id, msg, parent); | ||
| 552 | } | ||
| 553 | |||
| 554 | |||
| 555 | |||
| 556 | void timeline_admire(snac *snac, char *id, char *admirer, int like) | ||
| 557 | /* updates a timeline entry with a new admiration */ | ||
| 558 | { | ||
| 559 | xs *ofn = _timeline_find_fn(snac, id); | ||
| 560 | FILE *f; | ||
| 561 | |||
| 562 | if (ofn != NULL && (f = fopen(ofn, "r")) != NULL) { | ||
| 563 | int changed = 0; | ||
| 564 | xs *j1 = xs_readall(f); | ||
| 565 | fclose(f); | ||
| 566 | |||
| 567 | xs *msg = xs_json_loads(j1); | ||
| 568 | xs *meta = xs_dup(xs_dict_get(msg, "_snac")); | ||
| 569 | xs *list; | ||
| 570 | |||
| 571 | if (like) | ||
| 572 | list = xs_dup(xs_dict_get(meta, "liked_by")); | ||
| 573 | else | ||
| 574 | list = xs_dup(xs_dict_get(meta, "announced_by")); | ||
| 575 | |||
| 576 | /* add the admirer if it's not already there */ | ||
| 577 | if (xs_list_in(list, admirer) == -1) { | ||
| 578 | list = xs_list_append(list, admirer); | ||
| 579 | changed = 1; | ||
| 580 | } | ||
| 581 | |||
| 582 | if (changed) { | ||
| 583 | /* re-store */ | ||
| 584 | if (like) | ||
| 585 | meta = xs_dict_set(meta, "liked_by", list); | ||
| 586 | else | ||
| 587 | meta = xs_dict_set(meta, "announced_by", list); | ||
| 588 | |||
| 589 | msg = xs_dict_set(msg, "_snac", meta); | ||
| 590 | |||
| 591 | xs *j1 = xs_json_dumps_pp(msg, 4); | ||
| 592 | |||
| 593 | unlink(ofn); | ||
| 594 | |||
| 595 | _timeline_write(snac, id, msg, xs_dict_get(meta, "parent")); | ||
| 596 | } | ||
| 597 | } | ||
| 598 | } | ||
| 599 | |||
| 600 | |||
| 543 | d_char *_following_fn(snac *snac, char *actor) | 601 | d_char *_following_fn(snac *snac, char *actor) |
| 544 | { | 602 | { |
| 545 | xs *md5 = xs_md5_hex(actor, strlen(actor)); | 603 | xs *md5 = xs_md5_hex(actor, strlen(actor)); |
| @@ -56,6 +56,7 @@ void timeline_del(snac *snac, char *id); | |||
| 56 | d_char *timeline_get(snac *snac, char *fn); | 56 | d_char *timeline_get(snac *snac, char *fn); |
| 57 | d_char *timeline_list(snac *snac); | 57 | d_char *timeline_list(snac *snac); |
| 58 | void timeline_add(snac *snac, char *id, char *msg, char *parent); | 58 | void timeline_add(snac *snac, char *id, char *msg, char *parent); |
| 59 | void timeline_admire(snac *snac, char *id, char *admirer, int like); | ||
| 59 | 60 | ||
| 60 | int following_add(snac *snac, char *actor, char *msg); | 61 | int following_add(snac *snac, char *actor, char *msg); |
| 61 | int following_del(snac *snac, char *actor); | 62 | int following_del(snac *snac, char *actor); |