diff options
| author | 2022-09-22 18:50:39 +0200 | |
|---|---|---|
| committer | 2022-09-22 18:50:39 +0200 | |
| commit | 4f4f321a535126c5f9602a152a5b9e395e8f918e (patch) | |
| tree | b943b8eb3cfbee60a92ba0c8117c5ce08250ecf9 /data.c | |
| parent | httpd serves susie.png. (diff) | |
| download | snac2-4f4f321a535126c5f9602a152a5b9e395e8f918e.tar.gz snac2-4f4f321a535126c5f9602a152a5b9e395e8f918e.tar.xz snac2-4f4f321a535126c5f9602a152a5b9e395e8f918e.zip | |
Added some actor functions.
Diffstat (limited to 'data.c')
| -rw-r--r-- | data.c | 77 |
1 files changed, 77 insertions, 0 deletions
| @@ -636,6 +636,83 @@ void enqueue_output(snac *snac, char *actor, char *msg, int retries) | |||
| 636 | } | 636 | } |
| 637 | 637 | ||
| 638 | 638 | ||
| 639 | d_char *_actor_fn(snac *snac, char *actor) | ||
| 640 | /* returns the file name for an actor */ | ||
| 641 | { | ||
| 642 | xs *md5 = xs_md5_hex(actor, strlen(actor)); | ||
| 643 | return xs_fmt("%s/actors/%s.json", snac->basedir, md5); | ||
| 644 | } | ||
| 645 | |||
| 646 | |||
| 647 | int actor_add(snac *snac, char *actor, char *msg) | ||
| 648 | /* adds a follower */ | ||
| 649 | { | ||
| 650 | int ret = 201; /* created */ | ||
| 651 | xs *fn = _actor_fn(snac, actor); | ||
| 652 | FILE *f; | ||
| 653 | |||
| 654 | if ((f = fopen(fn, "w")) != NULL) { | ||
| 655 | xs *j = xs_json_dumps_pp(msg, 4); | ||
| 656 | |||
| 657 | fwrite(j, 1, strlen(j), f); | ||
| 658 | fclose(f); | ||
| 659 | } | ||
| 660 | else | ||
| 661 | ret = 500; | ||
| 662 | |||
| 663 | snac_debug(snac, 2, xs_fmt("actor_add %s %s", actor, fn)); | ||
| 664 | |||
| 665 | return ret; | ||
| 666 | } | ||
| 667 | |||
| 668 | |||
| 669 | int actor_get(snac *snac, char *actor, char **data) | ||
| 670 | /* returns an already downloaded actor */ | ||
| 671 | { | ||
| 672 | xs *fn = _actor_fn(snac, actor); | ||
| 673 | float t; | ||
| 674 | float max_time; | ||
| 675 | int status; | ||
| 676 | FILE *f; | ||
| 677 | |||
| 678 | t = mtime(fn); | ||
| 679 | |||
| 680 | /* no mtime? there is nothing here */ | ||
| 681 | if (t == 0.0) | ||
| 682 | return 404; | ||
| 683 | |||
| 684 | /* maximum time for the actor data to be considered stale */ | ||
| 685 | max_time = 3600.0 * 36.0; | ||
| 686 | |||
| 687 | if (t + max_time < (float) time(NULL)) { | ||
| 688 | /* actor data exists but also stinks */ | ||
| 689 | status = 202; | ||
| 690 | |||
| 691 | if ((f = fopen(fn, "a")) != NULL) { | ||
| 692 | /* write a blank at the end to 'touch' the file */ | ||
| 693 | fwrite(" ", 1, 1, f); | ||
| 694 | fclose(f); | ||
| 695 | } | ||
| 696 | } | ||
| 697 | else { | ||
| 698 | /* it's still valid */ | ||
| 699 | status = 200; | ||
| 700 | } | ||
| 701 | |||
| 702 | if ((f = fopen(fn, "r")) != NULL) { | ||
| 703 | xs *j = xs_readall(f); | ||
| 704 | |||
| 705 | fclose(f); | ||
| 706 | |||
| 707 | *data = xs_json_loads(j); | ||
| 708 | } | ||
| 709 | else | ||
| 710 | status = 500; | ||
| 711 | |||
| 712 | return status; | ||
| 713 | } | ||
| 714 | |||
| 715 | |||
| 639 | d_char *queue(snac *snac) | 716 | d_char *queue(snac *snac) |
| 640 | /* returns a list with filenames that can be dequeued */ | 717 | /* returns a list with filenames that can be dequeued */ |
| 641 | { | 718 | { |