diff options
| -rw-r--r-- | data.c | 68 | ||||
| -rw-r--r-- | snac.h | 4 |
2 files changed, 23 insertions, 49 deletions
| @@ -243,6 +243,8 @@ int object_get(const char *id, d_char **obj, const char *type) | |||
| 243 | else | 243 | else |
| 244 | *obj = NULL; | 244 | *obj = NULL; |
| 245 | 245 | ||
| 246 | srv_debug(2, xs_fmt("object_get %s %d", id, status)); | ||
| 247 | |||
| 246 | return status; | 248 | return status; |
| 247 | } | 249 | } |
| 248 | 250 | ||
| @@ -265,6 +267,8 @@ int object_add(const char *id, d_char *obj) | |||
| 265 | else | 267 | else |
| 266 | status = 500; | 268 | status = 500; |
| 267 | 269 | ||
| 270 | srv_debug(2, xs_fmt("object_add %s %d", id, status)); | ||
| 271 | |||
| 268 | return status; | 272 | return status; |
| 269 | } | 273 | } |
| 270 | 274 | ||
| @@ -938,57 +942,43 @@ d_char *_actor_fn(snac *snac, char *actor) | |||
| 938 | } | 942 | } |
| 939 | 943 | ||
| 940 | 944 | ||
| 941 | int actor_add(snac *snac, char *actor, char *msg) | 945 | int actor_add(snac *snac, const char *actor, d_char *msg) |
| 942 | /* adds an actor */ | 946 | /* adds an actor */ |
| 943 | { | 947 | { |
| 944 | int ret = 201; /* created */ | 948 | return object_add(actor, msg); |
| 945 | xs *fn = _actor_fn(snac, actor); | ||
| 946 | FILE *f; | ||
| 947 | |||
| 948 | if ((f = fopen(fn, "w")) != NULL) { | ||
| 949 | xs *j = xs_json_dumps_pp(msg, 4); | ||
| 950 | |||
| 951 | fwrite(j, 1, strlen(j), f); | ||
| 952 | fclose(f); | ||
| 953 | } | ||
| 954 | else | ||
| 955 | ret = 500; | ||
| 956 | |||
| 957 | snac_debug(snac, 2, xs_fmt("actor_add %s %s", actor, fn)); | ||
| 958 | |||
| 959 | // object_add(actor, msg); | ||
| 960 | |||
| 961 | return ret; | ||
| 962 | } | 949 | } |
| 963 | 950 | ||
| 964 | 951 | ||
| 965 | int actor_get(snac *snac, char *actor, d_char **data) | 952 | int actor_get(snac *snac, const char *actor, d_char **data) |
| 966 | /* returns an already downloaded actor */ | 953 | /* returns an already downloaded actor */ |
| 967 | { | 954 | { |
| 968 | xs *fn = _actor_fn(snac, actor); | 955 | int status = 200; |
| 969 | double t; | 956 | char *d; |
| 970 | double max_time; | ||
| 971 | int status; | ||
| 972 | FILE *f; | ||
| 973 | 957 | ||
| 974 | if (strcmp(actor, snac->actor) == 0) { | 958 | if (strcmp(actor, snac->actor) == 0) { |
| 959 | /* this actor */ | ||
| 975 | if (data) | 960 | if (data) |
| 976 | *data = msg_actor(snac); | 961 | *data = msg_actor(snac); |
| 977 | 962 | ||
| 978 | return 200; | 963 | return status; |
| 979 | } | 964 | } |
| 980 | 965 | ||
| 981 | t = mtime(fn); | 966 | /* read the object */ |
| 967 | if (!valid_status(status = object_get(actor, &d, "Person"))) | ||
| 968 | return status; | ||
| 982 | 969 | ||
| 983 | /* no mtime? there is nothing here */ | 970 | if (data) |
| 984 | if (t == 0.0) | 971 | *data = d; |
| 985 | return 404; | 972 | |
| 973 | xs *fn = _object_fn_by_id(actor); | ||
| 974 | double max_time; | ||
| 986 | 975 | ||
| 987 | /* maximum time for the actor data to be considered stale */ | 976 | /* maximum time for the actor data to be considered stale */ |
| 988 | max_time = 3600.0 * 36.0; | 977 | max_time = 3600.0 * 36.0; |
| 989 | 978 | ||
| 990 | if (t + max_time < (double) time(NULL)) { | 979 | if (mtime(fn) + max_time < (double) time(NULL)) { |
| 991 | /* actor data exists but also stinks */ | 980 | /* actor data exists but also stinks */ |
| 981 | FILE *f; | ||
| 992 | 982 | ||
| 993 | if ((f = fopen(fn, "a")) != NULL) { | 983 | if ((f = fopen(fn, "a")) != NULL) { |
| 994 | /* write a blank at the end to 'touch' the file */ | 984 | /* write a blank at the end to 'touch' the file */ |
| @@ -998,22 +988,6 @@ int actor_get(snac *snac, char *actor, d_char **data) | |||
| 998 | 988 | ||
| 999 | status = 205; /* "205: Reset Content" "110: Response Is Stale" */ | 989 | status = 205; /* "205: Reset Content" "110: Response Is Stale" */ |
| 1000 | } | 990 | } |
| 1001 | else { | ||
| 1002 | /* it's still valid */ | ||
| 1003 | status = 200; | ||
| 1004 | } | ||
| 1005 | |||
| 1006 | if (data) { | ||
| 1007 | if ((f = fopen(fn, "r")) != NULL) { | ||
| 1008 | xs *j = xs_readall(f); | ||
| 1009 | |||
| 1010 | fclose(f); | ||
| 1011 | |||
| 1012 | *data = xs_json_loads(j); | ||
| 1013 | } | ||
| 1014 | else | ||
| 1015 | status = 500; | ||
| 1016 | } | ||
| 1017 | 991 | ||
| 1018 | return status; | 992 | return status; |
| 1019 | } | 993 | } |
| @@ -80,8 +80,8 @@ void mute(snac *snac, char *actor); | |||
| 80 | void unmute(snac *snac, char *actor); | 80 | void unmute(snac *snac, char *actor); |
| 81 | int is_muted(snac *snac, char *actor); | 81 | int is_muted(snac *snac, char *actor); |
| 82 | 82 | ||
| 83 | int actor_add(snac *snac, char *actor, char *msg); | 83 | int actor_add(snac *snac, const char *actor, d_char *msg); |
| 84 | int actor_get(snac *snac, char *actor, d_char **data); | 84 | int actor_get(snac *snac, const char *actor, d_char **data); |
| 85 | 85 | ||
| 86 | int static_get(snac *snac, const char *id, d_char **data, int *size); | 86 | int static_get(snac *snac, const char *id, d_char **data, int *size); |
| 87 | void static_put(snac *snac, const char *id, const char *data, int size); | 87 | void static_put(snac *snac, const char *id, const char *data, int size); |