diff options
| author | 2022-09-20 10:02:00 +0200 | |
|---|---|---|
| committer | 2022-09-20 10:02:00 +0200 | |
| commit | 732654e73a5df0c9248726df7cf7b9c54ebfce15 (patch) | |
| tree | 049aa50bcc090ba5a042bf5f8f2db53e8f8bd2a7 /data.c | |
| parent | More follower data code. (diff) | |
| download | snac2-732654e73a5df0c9248726df7cf7b9c54ebfce15.tar.gz snac2-732654e73a5df0c9248726df7cf7b9c54ebfce15.tar.xz snac2-732654e73a5df0c9248726df7cf7b9c54ebfce15.zip | |
Added some timeline functions.
Diffstat (limited to 'data.c')
| -rw-r--r-- | data.c | 60 |
1 files changed, 60 insertions, 0 deletions
| @@ -267,3 +267,63 @@ d_char *follower_list(snac *snac) | |||
| 267 | 267 | ||
| 268 | return list; | 268 | return list; |
| 269 | } | 269 | } |
| 270 | |||
| 271 | |||
| 272 | d_char *_timeline_fn(snac *snac, char *id) | ||
| 273 | /* returns the file name of a timeline entry by its id */ | ||
| 274 | { | ||
| 275 | xs *md5 = xs_md5_hex(id, strlen(id)); | ||
| 276 | xs *spec = xs_fmt("%s/timeline/" "*-%s.json", snac->basedir, md5); | ||
| 277 | glob_t globbuf; | ||
| 278 | d_char *fn = NULL; | ||
| 279 | |||
| 280 | if (glob(spec, 0, NULL, &globbuf) == 0 && globbuf.gl_matchc) { | ||
| 281 | /* get just the first file */ | ||
| 282 | fn = xs_str_new(globbuf.gl_pathv[0]); | ||
| 283 | } | ||
| 284 | |||
| 285 | globfree(&globbuf); | ||
| 286 | |||
| 287 | return fn; | ||
| 288 | } | ||
| 289 | |||
| 290 | |||
| 291 | d_char *timeline_get(snac *snac, char *id) | ||
| 292 | /* gets a message from the timeline */ | ||
| 293 | { | ||
| 294 | xs *fn = _timeline_fn(snac, id); | ||
| 295 | xs *msg = NULL; | ||
| 296 | |||
| 297 | if (fn != NULL) { | ||
| 298 | FILE *f; | ||
| 299 | |||
| 300 | if ((f = fopen(fn, "r")) != NULL) { | ||
| 301 | xs *j = xs_readall(f); | ||
| 302 | |||
| 303 | msg = xs_json_loads(j); | ||
| 304 | fclose(f); | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | return msg; | ||
| 309 | } | ||
| 310 | |||
| 311 | |||
| 312 | void timeline_del(snac *snac, char *id) | ||
| 313 | /* deletes a message from the timeline */ | ||
| 314 | { | ||
| 315 | xs *fn = _timeline_fn(snac, id); | ||
| 316 | |||
| 317 | if (fn != NULL) { | ||
| 318 | xs *lfn = NULL; | ||
| 319 | |||
| 320 | unlink(fn); | ||
| 321 | snac_debug(snac, 1, xs_fmt("timeline_del %s", id)); | ||
| 322 | |||
| 323 | /* try to delete also from the local timeline */ | ||
| 324 | lfn = xs_replace(fn, "/timeline/", "/local/"); | ||
| 325 | |||
| 326 | if (unlink(lfn) != -1) | ||
| 327 | snac_debug(snac, 1, xs_fmt("timeline_del (local) %s", id)); | ||
| 328 | } | ||
| 329 | } | ||