diff options
| -rw-r--r-- | data.c | 76 |
1 files changed, 76 insertions, 0 deletions
| @@ -190,6 +190,80 @@ double mtime(char *fn) | |||
| 190 | } | 190 | } |
| 191 | 191 | ||
| 192 | 192 | ||
| 193 | /** object database 2.1+ **/ | ||
| 194 | |||
| 195 | d_char *_object_fn_by_md5(const char *md5) | ||
| 196 | { | ||
| 197 | xs *bfn = xs_fmt("%s/object/%c%c/", srv_basedir, md5[0], md5[1]); | ||
| 198 | |||
| 199 | mkdir(bfn, 0755); | ||
| 200 | |||
| 201 | return xs_fmt("%s/%s.json", bfn, md5); | ||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | d_char *_object_fn_by_id(const char *id) | ||
| 206 | { | ||
| 207 | xs *md5 = xs_md5_hex(id, strlen(id)); | ||
| 208 | |||
| 209 | return _object_fn_by_md5(md5); | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | int object_get(const char *id, d_char **obj, const char *type) | ||
| 214 | /* returns a loaded object, optionally of the requested type */ | ||
| 215 | { | ||
| 216 | int status = 404; | ||
| 217 | xs *fn = _object_fn_by_id(id); | ||
| 218 | FILE *f; | ||
| 219 | |||
| 220 | if ((f = fopen(fn, "r")) != NULL) { | ||
| 221 | xs *j = xs_readall(f); | ||
| 222 | fclose(f); | ||
| 223 | |||
| 224 | *obj = xs_json_loads(j); | ||
| 225 | |||
| 226 | if (*obj) { | ||
| 227 | status = 200; | ||
| 228 | |||
| 229 | /* specific type requested? */ | ||
| 230 | if (!xs_is_null(type)) { | ||
| 231 | char *v = xs_dict_get(*obj, "type"); | ||
| 232 | |||
| 233 | if (xs_is_null(v) || strcmp(v, type) != 0) { | ||
| 234 | status = 404; | ||
| 235 | *obj = xs_free(*obj); | ||
| 236 | } | ||
| 237 | } | ||
| 238 | } | ||
| 239 | } | ||
| 240 | else | ||
| 241 | *obj = NULL; | ||
| 242 | |||
| 243 | return status; | ||
| 244 | } | ||
| 245 | |||
| 246 | |||
| 247 | int object_add(const char *id, d_char *obj) | ||
| 248 | /* stores an object */ | ||
| 249 | { | ||
| 250 | int status = 201; /* Created */ | ||
| 251 | xs *fn = _object_fn_by_id(id); | ||
| 252 | FILE *f; | ||
| 253 | |||
| 254 | if ((f = fopen(fn, "w")) != NULL) { | ||
| 255 | xs *j = xs_json_dumps_pp(obj, 4); | ||
| 256 | |||
| 257 | fwrite(j, strlen(j), 1, f); | ||
| 258 | fclose(f); | ||
| 259 | } | ||
| 260 | else | ||
| 261 | status = 500; | ||
| 262 | |||
| 263 | return status; | ||
| 264 | } | ||
| 265 | |||
| 266 | |||
| 193 | d_char *_follower_fn(snac *snac, char *actor) | 267 | d_char *_follower_fn(snac *snac, char *actor) |
| 194 | { | 268 | { |
| 195 | xs *md5 = xs_md5_hex(actor, strlen(actor)); | 269 | xs *md5 = xs_md5_hex(actor, strlen(actor)); |
| @@ -877,6 +951,8 @@ int actor_add(snac *snac, char *actor, char *msg) | |||
| 877 | 951 | ||
| 878 | snac_debug(snac, 2, xs_fmt("actor_add %s %s", actor, fn)); | 952 | snac_debug(snac, 2, xs_fmt("actor_add %s %s", actor, fn)); |
| 879 | 953 | ||
| 954 | object_add(actor, msg); | ||
| 955 | |||
| 880 | return ret; | 956 | return ret; |
| 881 | } | 957 | } |
| 882 | 958 | ||