diff options
Diffstat (limited to 'data.c')
| -rw-r--r-- | data.c | 66 |
1 files changed, 58 insertions, 8 deletions
| @@ -246,12 +246,12 @@ d_char *follower_list(snac *snac) | |||
| 246 | 246 | ||
| 247 | if (glob(spec, 0, NULL, &globbuf) == 0) { | 247 | if (glob(spec, 0, NULL, &globbuf) == 0) { |
| 248 | int n; | 248 | int n; |
| 249 | char *p; | 249 | char *fn; |
| 250 | 250 | ||
| 251 | for (n = 0; (p = globbuf.gl_pathv[n]) != NULL; n++) { | 251 | for (n = 0; (fn = globbuf.gl_pathv[n]) != NULL; n++) { |
| 252 | FILE *f; | 252 | FILE *f; |
| 253 | 253 | ||
| 254 | if ((f = fopen(p, "r")) != NULL) { | 254 | if ((f = fopen(fn, "r")) != NULL) { |
| 255 | xs *j = xs_readall(f); | 255 | xs *j = xs_readall(f); |
| 256 | xs *o = xs_json_loads(j); | 256 | xs *o = xs_json_loads(j); |
| 257 | 257 | ||
| @@ -269,7 +269,7 @@ d_char *follower_list(snac *snac) | |||
| 269 | } | 269 | } |
| 270 | 270 | ||
| 271 | 271 | ||
| 272 | d_char *_timeline_fn(snac *snac, char *id) | 272 | d_char *_timeline_find_fn(snac *snac, char *id) |
| 273 | /* returns the file name of a timeline entry by its id */ | 273 | /* returns the file name of a timeline entry by its id */ |
| 274 | { | 274 | { |
| 275 | xs *md5 = xs_md5_hex(id, strlen(id)); | 275 | xs *md5 = xs_md5_hex(id, strlen(id)); |
| @@ -288,10 +288,10 @@ d_char *_timeline_fn(snac *snac, char *id) | |||
| 288 | } | 288 | } |
| 289 | 289 | ||
| 290 | 290 | ||
| 291 | d_char *timeline_get(snac *snac, char *id) | 291 | d_char *timeline_find(snac *snac, char *id) |
| 292 | /* gets a message from the timeline */ | 292 | /* gets a message from the timeline by id */ |
| 293 | { | 293 | { |
| 294 | xs *fn = _timeline_fn(snac, id); | 294 | xs *fn = _timeline_find_fn(snac, id); |
| 295 | xs *msg = NULL; | 295 | xs *msg = NULL; |
| 296 | 296 | ||
| 297 | if (fn != NULL) { | 297 | if (fn != NULL) { |
| @@ -312,7 +312,7 @@ d_char *timeline_get(snac *snac, char *id) | |||
| 312 | void timeline_del(snac *snac, char *id) | 312 | void timeline_del(snac *snac, char *id) |
| 313 | /* deletes a message from the timeline */ | 313 | /* deletes a message from the timeline */ |
| 314 | { | 314 | { |
| 315 | xs *fn = _timeline_fn(snac, id); | 315 | xs *fn = _timeline_find_fn(snac, id); |
| 316 | 316 | ||
| 317 | if (fn != NULL) { | 317 | if (fn != NULL) { |
| 318 | xs *lfn = NULL; | 318 | xs *lfn = NULL; |
| @@ -327,3 +327,53 @@ void timeline_del(snac *snac, char *id) | |||
| 327 | snac_debug(snac, 1, xs_fmt("timeline_del (local) %s", id)); | 327 | snac_debug(snac, 1, xs_fmt("timeline_del (local) %s", id)); |
| 328 | } | 328 | } |
| 329 | } | 329 | } |
| 330 | |||
| 331 | |||
| 332 | d_char *timeline_get(snac *snac, char *fn) | ||
| 333 | /* gets a timeline entry by file name */ | ||
| 334 | { | ||
| 335 | d_char *d = NULL; | ||
| 336 | FILE *f; | ||
| 337 | |||
| 338 | if ((f = fopen(fn, "r")) != NULL) { | ||
| 339 | xs *j = xs_readall(f); | ||
| 340 | |||
| 341 | d = xs_json_loads(j); | ||
| 342 | fclose(f); | ||
| 343 | } | ||
| 344 | |||
| 345 | return d; | ||
| 346 | } | ||
| 347 | |||
| 348 | |||
| 349 | d_char *timeline_list(snac *snac) | ||
| 350 | /* returns a list of the timeline filenames */ | ||
| 351 | { | ||
| 352 | d_char *list; | ||
| 353 | xs *spec = xs_fmt("%s/timeline/" "*.json", snac->basedir); | ||
| 354 | glob_t globbuf; | ||
| 355 | int max; | ||
| 356 | |||
| 357 | /* maximum number of items in the timeline */ | ||
| 358 | max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries")); | ||
| 359 | |||
| 360 | list = xs_list_new(); | ||
| 361 | |||
| 362 | /* get the list in reverse order */ | ||
| 363 | if (glob(spec, 0, NULL, &globbuf) == 0) { | ||
| 364 | int n; | ||
| 365 | |||
| 366 | if (max > globbuf.gl_matchc) | ||
| 367 | max = globbuf.gl_matchc; | ||
| 368 | |||
| 369 | for (n = 0; n < max; n++) { | ||
| 370 | char *fn = globbuf.gl_pathv[globbuf.gl_matchc - n - 1]; | ||
| 371 | |||
| 372 | list = xs_list_append(list, fn); | ||
| 373 | } | ||
| 374 | } | ||
| 375 | |||
| 376 | globfree(&globbuf); | ||
| 377 | |||
| 378 | return list; | ||
| 379 | } | ||