diff options
| -rw-r--r-- | data.c | 56 | ||||
| -rw-r--r-- | html.c | 21 | ||||
| -rw-r--r-- | snac.h | 5 |
3 files changed, 77 insertions, 5 deletions
| @@ -178,7 +178,7 @@ float mtime(char *fn) | |||
| 178 | struct stat st; | 178 | struct stat st; |
| 179 | float r = 0.0; | 179 | float r = 0.0; |
| 180 | 180 | ||
| 181 | if (stat(fn, &st) != -1) | 181 | if (fn && stat(fn, &st) != -1) |
| 182 | r = (float)st.st_mtim.tv_sec; | 182 | r = (float)st.st_mtim.tv_sec; |
| 183 | 183 | ||
| 184 | return r; | 184 | return r; |
| @@ -275,6 +275,13 @@ d_char *follower_list(snac *snac) | |||
| 275 | } | 275 | } |
| 276 | 276 | ||
| 277 | 277 | ||
| 278 | float timeline_mtime(snac *snac) | ||
| 279 | { | ||
| 280 | xs *fn = xs_fmt("%s/timeline", snac->basedir); | ||
| 281 | return mtime(fn); | ||
| 282 | } | ||
| 283 | |||
| 284 | |||
| 278 | d_char *_timeline_find_fn(snac *snac, char *id) | 285 | d_char *_timeline_find_fn(snac *snac, char *id) |
| 279 | /* returns the file name of a timeline entry by its id */ | 286 | /* returns the file name of a timeline entry by its id */ |
| 280 | { | 287 | { |
| @@ -831,6 +838,53 @@ int static_get(snac *snac, char *id, d_char **data, int *size) | |||
| 831 | } | 838 | } |
| 832 | 839 | ||
| 833 | 840 | ||
| 841 | d_char *_history_fn(snac *snac, char *id) | ||
| 842 | /* gets the filename for the history */ | ||
| 843 | { | ||
| 844 | return xs_fmt("%s/history/%s", snac->basedir, id); | ||
| 845 | } | ||
| 846 | |||
| 847 | |||
| 848 | float history_mtime(snac *snac, char * id) | ||
| 849 | { | ||
| 850 | float t = 0.0; | ||
| 851 | xs *fn = _history_fn(snac, id); | ||
| 852 | |||
| 853 | if (fn != NULL) | ||
| 854 | t = mtime(fn); | ||
| 855 | |||
| 856 | return t; | ||
| 857 | } | ||
| 858 | |||
| 859 | |||
| 860 | void history_add(snac *snac, char *id, char *content, int size) | ||
| 861 | /* adds something to the history */ | ||
| 862 | { | ||
| 863 | xs *fn = _history_fn(snac, id); | ||
| 864 | FILE *f; | ||
| 865 | |||
| 866 | if ((f = fopen(fn, "w")) != NULL) { | ||
| 867 | fwrite(content, size, 1, f); | ||
| 868 | fclose(f); | ||
| 869 | } | ||
| 870 | } | ||
| 871 | |||
| 872 | |||
| 873 | d_char *history_get(snac *snac, char *id) | ||
| 874 | { | ||
| 875 | d_char *content = NULL; | ||
| 876 | xs *fn = _history_fn(snac, id); | ||
| 877 | FILE *f; | ||
| 878 | |||
| 879 | if ((f = fopen(fn, "r")) != NULL) { | ||
| 880 | content = xs_readall(f); | ||
| 881 | fclose(f); | ||
| 882 | } | ||
| 883 | |||
| 884 | return content; | ||
| 885 | } | ||
| 886 | |||
| 887 | |||
| 834 | void enqueue_input(snac *snac, char *msg, char *req, int retries) | 888 | void enqueue_input(snac *snac, char *msg, char *req, int retries) |
| 835 | /* enqueues an input message */ | 889 | /* enqueues an input message */ |
| 836 | { | 890 | { |
| @@ -713,11 +713,24 @@ int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char * | |||
| 713 | if (!login(&snac, req)) | 713 | if (!login(&snac, req)) |
| 714 | status = 401; | 714 | status = 401; |
| 715 | else { | 715 | else { |
| 716 | xs *list = timeline_list(&snac, 0xfffffff); | 716 | if (history_mtime(&snac, "_timeline.html") > timeline_mtime(&snac)) { |
| 717 | snac_debug(&snac, 1, xs_fmt("serving cached timeline")); | ||
| 717 | 718 | ||
| 718 | *body = html_timeline(&snac, list, 0); | 719 | *body = history_get(&snac, "_timeline.html"); |
| 719 | *b_size = strlen(*body); | 720 | *b_size = strlen(*body); |
| 720 | status = 200; | 721 | status = 200; |
| 722 | } | ||
| 723 | else { | ||
| 724 | snac_debug(&snac, 1, xs_fmt("building timeline")); | ||
| 725 | |||
| 726 | xs *list = timeline_list(&snac, 0xfffffff); | ||
| 727 | |||
| 728 | *body = html_timeline(&snac, list, 0); | ||
| 729 | *b_size = strlen(*body); | ||
| 730 | status = 200; | ||
| 731 | |||
| 732 | history_add(&snac, "_timeline.html", *body, *b_size); | ||
| 733 | } | ||
| 721 | } | 734 | } |
| 722 | } | 735 | } |
| 723 | else | 736 | else |
| @@ -60,6 +60,7 @@ int follower_del(snac *snac, char *actor); | |||
| 60 | int follower_check(snac *snac, char *actor); | 60 | int follower_check(snac *snac, char *actor); |
| 61 | d_char *follower_list(snac *snac); | 61 | d_char *follower_list(snac *snac); |
| 62 | 62 | ||
| 63 | float timeline_mtime(snac *snac); | ||
| 63 | int timeline_here(snac *snac, char *id); | 64 | int timeline_here(snac *snac, char *id); |
| 64 | d_char *_timeline_find_fn(snac *snac, char *id); | 65 | d_char *_timeline_find_fn(snac *snac, char *id); |
| 65 | d_char *timeline_find(snac *snac, char *id); | 66 | d_char *timeline_find(snac *snac, char *id); |
| @@ -84,6 +85,10 @@ int actor_get(snac *snac, char *actor, d_char **data); | |||
| 84 | 85 | ||
| 85 | int static_get(snac *snac, char *id, d_char **data, int *size); | 86 | int static_get(snac *snac, char *id, d_char **data, int *size); |
| 86 | 87 | ||
| 88 | float history_mtime(snac *snac, char *id); | ||
| 89 | void history_add(snac *snac, char *id, char *content, int size); | ||
| 90 | d_char *history_get(snac *snac, char *id); | ||
| 91 | |||
| 87 | void enqueue_input(snac *snac, char *msg, char *req, int retries); | 92 | void enqueue_input(snac *snac, char *msg, char *req, int retries); |
| 88 | void enqueue_output(snac *snac, char *msg, char *actor, int retries); | 93 | void enqueue_output(snac *snac, char *msg, char *actor, int retries); |
| 89 | 94 | ||