summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar default2022-09-20 10:49:24 +0200
committerGravatar default2022-09-20 10:49:24 +0200
commit9a01f731d76f5b14f98ecf022a7961e24a3360b9 (patch)
tree2be140104d36a764dea594c8351d1fade68c5234
parentAdded some timeline functions. (diff)
downloadsnac2-9a01f731d76f5b14f98ecf022a7961e24a3360b9.tar.gz
snac2-9a01f731d76f5b14f98ecf022a7961e24a3360b9.tar.xz
snac2-9a01f731d76f5b14f98ecf022a7961e24a3360b9.zip
More timeline work.
-rw-r--r--data.c66
-rw-r--r--main.c12
-rw-r--r--snac.h4
3 files changed, 73 insertions, 9 deletions
diff --git a/data.c b/data.c
index 7f0522d..b03a84f 100644
--- a/data.c
+++ b/data.c
@@ -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
272d_char *_timeline_fn(snac *snac, char *id) 272d_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
291d_char *timeline_get(snac *snac, char *id) 291d_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)
312void timeline_del(snac *snac, char *id) 312void 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
332d_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
349d_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}
diff --git a/main.c b/main.c
index f6fc5e2..4a9690b 100644
--- a/main.c
+++ b/main.c
@@ -27,6 +27,18 @@ int main(int argc, char *argv[])
27 } 27 }
28 28
29 { 29 {
30 xs *list = timeline_list(&snac);
31 char *p, *fn;
32
33 p = list;
34 while (xs_list_iter(&p, &fn)) {
35 xs *tle = timeline_get(&snac, fn);
36
37 printf("%s\n", xs_dict_get(tle, "id"));
38 }
39 }
40
41 {
30 xs *list = user_list(); 42 xs *list = user_list();
31 char *p, *uid; 43 char *p, *uid;
32 44
diff --git a/snac.h b/snac.h
index 52ce431..4ac832c 100644
--- a/snac.h
+++ b/snac.h
@@ -45,5 +45,7 @@ int follower_del(snac *snac, char *actor);
45int follower_check(snac *snac, char *actor); 45int follower_check(snac *snac, char *actor);
46d_char *follower_list(snac *snac); 46d_char *follower_list(snac *snac);
47 47
48d_char *timeline_get(snac *snac, char *id); 48d_char *timeline_find(snac *snac, char *id);
49void timeline_del(snac *snac, char *id); 49void timeline_del(snac *snac, char *id);
50d_char *timeline_get(snac *snac, char *fn);
51d_char *timeline_list(snac *snac);