-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.c
1527 lines (1119 loc) · 33.5 KB
/
data.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* snac - A simple, minimalistic ActivityPub instance */
/* copyright (c) 2022 grunfink - MIT license */
#include "xs.h"
#include "xs_io.h"
#include "xs_json.h"
#include "xs_openssl.h"
#include "xs_glob.h"
#include "xs_set.h"
#include "snac.h"
#include <time.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <fcntl.h>
double db_layout = 2.7;
int db_upgrade(d_char **error);
int srv_open(char *basedir, int auto_upgrade)
/* opens a server */
{
int ret = 0;
xs *cfg_file = NULL;
FILE *f;
d_char *error = NULL;
srv_basedir = xs_str_new(basedir);
if (xs_endswith(srv_basedir, "/"))
srv_basedir = xs_crop(srv_basedir, 0, -1);
cfg_file = xs_fmt("%s/server.json", basedir);
if ((f = fopen(cfg_file, "r")) == NULL)
error = xs_fmt("ERROR: cannot opening '%s'", cfg_file);
else {
xs *cfg_data;
/* read full config file */
cfg_data = xs_readall(f);
fclose(f);
/* parse */
srv_config = xs_json_loads(cfg_data);
if (srv_config == NULL)
error = xs_fmt("ERROR: cannot parse '%s'", cfg_file);
else {
char *host;
char *prefix;
char *dbglvl;
host = xs_dict_get(srv_config, "host");
prefix = xs_dict_get(srv_config, "prefix");
dbglvl = xs_dict_get(srv_config, "dbglevel");
if (host == NULL || prefix == NULL)
error = xs_str_new("ERROR: cannot get server data");
else {
srv_baseurl = xs_fmt("https://%s%s", host, prefix);
dbglevel = (int) xs_number_get(dbglvl);
if ((dbglvl = getenv("DEBUG")) != NULL) {
dbglevel = atoi(dbglvl);
error = xs_fmt("DEBUG level set to %d from environment", dbglevel);
}
if (auto_upgrade)
ret = db_upgrade(&error);
else {
if (xs_number_get(xs_dict_get(srv_config, "layout")) < db_layout)
error = xs_fmt("ERROR: disk layout changed - execute 'snac upgrade' first");
else
ret = 1;
}
}
}
}
if (error != NULL)
srv_log(error);
/* disabled temporarily; messages can't be sent (libcurl issue?) */
#if 0
#ifdef __OpenBSD__
srv_debug(2, xs_fmt("Calling unveil()"));
unveil(basedir, "rwc");
unveil("/usr/sbin", "x");
unveil(NULL, NULL);
#endif /* __OpenBSD__ */
#endif
return ret;
}
void srv_free(void)
{
xs_free(srv_basedir);
xs_free(srv_config);
xs_free(srv_baseurl);
}
void user_free(snac *snac)
/* frees a user snac */
{
xs_free(snac->uid);
xs_free(snac->basedir);
xs_free(snac->config);
xs_free(snac->key);
xs_free(snac->actor);
xs_free(snac->md5);
}
int user_open(snac *snac, const char *uid)
/* opens a user */
{
int ret = 0;
memset(snac, '\0', sizeof(struct _snac));
if (validate_uid(uid)) {
xs *cfg_file;
FILE *f;
snac->uid = xs_str_new(uid);
snac->basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
cfg_file = xs_fmt("%s/user.json", snac->basedir);
if ((f = fopen(cfg_file, "r")) != NULL) {
xs *cfg_data;
/* read full config file */
cfg_data = xs_readall(f);
fclose(f);
if ((snac->config = xs_json_loads(cfg_data)) != NULL) {
xs *key_file = xs_fmt("%s/key.json", snac->basedir);
if ((f = fopen(key_file, "r")) != NULL) {
xs *key_data;
key_data = xs_readall(f);
fclose(f);
if ((snac->key = xs_json_loads(key_data)) != NULL) {
snac->actor = xs_fmt("%s/%s", srv_baseurl, uid);
snac->md5 = xs_md5_hex(snac->actor, strlen(snac->actor));
ret = 1;
}
else
srv_log(xs_fmt("cannot parse '%s'", key_file));
}
else
srv_log(xs_fmt("error opening '%s'", key_file));
}
else
srv_log(xs_fmt("cannot parse '%s'", cfg_file));
}
else
srv_debug(2, xs_fmt("error opening '%s'", cfg_file));
}
else
srv_log(xs_fmt("invalid user '%s'", uid));
if (!ret)
user_free(snac);
return ret;
}
d_char *user_list(void)
/* returns the list of user ids */
{
xs *spec = xs_fmt("%s/user/" "*", srv_basedir);
return xs_glob(spec, 1, 0);
}
double mtime_nl(const char *fn, int *n_link)
/* returns the mtime and number of links of a file or directory, or 0.0 */
{
struct stat st;
double r = 0.0;
int n = 0;
if (fn && stat(fn, &st) != -1) {
r = (double) st.st_mtim.tv_sec;
n = st.st_nlink;
}
if (n_link)
*n_link = n;
return r;
}
/** database 2.1+ **/
/** indexes **/
int index_add_md5(const char *fn, const char *md5)
/* adds an md5 to an index */
{
int status = 201; /* Created */
FILE *f;
if ((f = fopen(fn, "a")) != NULL) {
flock(fileno(f), LOCK_EX);
/* ensure the position is at the end after getting the lock */
fseek(f, 0, SEEK_END);
fprintf(f, "%s\n", md5);
fclose(f);
}
else
status = 500;
return status;
}
int index_add(const char *fn, const char *id)
/* adds an id to an index */
{
xs *md5 = xs_md5_hex(id, strlen(id));
return index_add_md5(fn, md5);
}
int index_del_md5(const char *fn, const char *md5)
/* deletes an md5 from an index */
{
int status = 404;
FILE *i, *o;
if ((i = fopen(fn, "r")) != NULL) {
flock(fileno(i), LOCK_EX);
xs *nfn = xs_fmt("%s.new", fn);
char line[256];
if ((o = fopen(nfn, "w")) != NULL) {
while (fgets(line, sizeof(line), i) != NULL) {
line[32] = '\0';
if (memcmp(line, md5, 32) != 0)
fprintf(o, "%s\n", line);
}
fclose(o);
xs *ofn = xs_fmt("%s.bak", fn);
link(fn, ofn);
rename(nfn, fn);
}
else
status = 500;
fclose(i);
}
else
status = 500;
return status;
}
int index_del(const char *fn, const char *id)
/* deletes an id from an index */
{
xs *md5 = xs_md5_hex(id, strlen(id));
return index_del_md5(fn, md5);
}
int index_in_md5(const char *fn, const char *md5)
/* checks if the md5 is already in the index */
{
FILE *f;
int ret = 0;
if ((f = fopen(fn, "r")) != NULL) {
flock(fileno(f), LOCK_SH);
char line[256];
while (!ret && fgets(line, sizeof(line), f) != NULL) {
line[32] = '\0';
if (strcmp(line, md5) == 0)
ret = 1;
}
fclose(f);
}
return ret;
}
int index_in(const char *fn, const char *id)
/* checks if the object id is already in the index */
{
xs *md5 = xs_md5_hex(id, strlen(id));
return index_in_md5(fn, md5);
}
int index_first(const char *fn, char *line, int size)
/* reads the first entry of an index */
{
FILE *f;
int ret = 0;
if ((f = fopen(fn, "r")) != NULL) {
flock(fileno(f), LOCK_SH);
if (fgets(line, size, f) != NULL) {
line[32] = '\0';
ret = 1;
}
fclose(f);
}
return ret;
}
int index_len(const char *fn)
/* returns the number of elements in an index */
{
struct stat st;
int len = 0;
if (stat(fn, &st) != -1)
len = st.st_size / 33;
return len;
}
d_char *index_list(const char *fn, int max)
/* returns an index as a list */
{
d_char *list = NULL;
FILE *f;
int n = 0;
if ((f = fopen(fn, "r")) != NULL) {
flock(fileno(f), LOCK_SH);
char line[256];
list = xs_list_new();
while (n < max && fgets(line, sizeof(line), f) != NULL) {
line[32] = '\0';
list = xs_list_append(list, line);
n++;
}
fclose(f);
}
return list;
}
d_char *index_list_desc(const char *fn, int skip, int show)
/* returns an index as a list, in reverse order */
{
d_char *list = NULL;
FILE *f;
int n = 0;
if ((f = fopen(fn, "r")) != NULL) {
flock(fileno(f), LOCK_SH);
char line[256];
list = xs_list_new();
/* move to the end minus one entry (or more, if skipping entries) */
if (!fseek(f, 0, SEEK_END) && !fseek(f, (skip + 1) * -33, SEEK_CUR)) {
while (n < show && fgets(line, sizeof(line), f) != NULL) {
line[32] = '\0';
list = xs_list_append(list, line);
n++;
/* move backwards 2 entries */
if (fseek(f, -66, SEEK_CUR) == -1)
break;
}
}
fclose(f);
}
return list;
}
/** objects **/
d_char *_object_fn_by_md5(const char *md5)
{
xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
mkdir(bfn, 0755);
return xs_fmt("%s/%s.json", bfn, md5);
}
d_char *_object_fn(const char *id)
{
xs *md5 = xs_md5_hex(id, strlen(id));
return _object_fn_by_md5(md5);
}
int object_here_by_md5(char *id)
/* checks if an object is already downloaded */
{
xs *fn = _object_fn_by_md5(id);
return mtime(fn) > 0.0;
}
int object_here(char *id)
/* checks if an object is already downloaded */
{
xs *fn = _object_fn(id);
return mtime(fn) > 0.0;
}
int object_get_by_md5(const char *md5, d_char **obj, const char *type)
/* returns a stored object, optionally of the requested type */
{
int status = 404;
xs *fn = _object_fn_by_md5(md5);
FILE *f;
if ((f = fopen(fn, "r")) != NULL) {
flock(fileno(f), LOCK_SH);
xs *j = xs_readall(f);
fclose(f);
*obj = xs_json_loads(j);
if (*obj) {
status = 200;
/* specific type requested? */
if (!xs_is_null(type)) {
char *v = xs_dict_get(*obj, "type");
if (xs_is_null(v) || strcmp(v, type) != 0) {
status = 404;
*obj = xs_free(*obj);
}
}
}
}
else
*obj = NULL;
return status;
}
int object_get(const char *id, d_char **obj, const char *type)
/* returns a stored object, optionally of the requested type */
{
xs *md5 = xs_md5_hex(id, strlen(id));
return object_get_by_md5(md5, obj, type);
}
int _object_add(const char *id, d_char *obj, int ow)
/* stores an object */
{
int status = 201; /* Created */
xs *fn = _object_fn(id);
FILE *f;
if (!ow && mtime(fn) > 0.0) {
/* object already here */
srv_debug(1, xs_fmt("object_add object already here %s", id));
return 204; /* No content */
}
if ((f = fopen(fn, "w")) != NULL) {
flock(fileno(f), LOCK_EX);
xs *j = xs_json_dumps_pp(obj, 4);
fwrite(j, strlen(j), 1, f);
fclose(f);
/* does this object has a parent? */
char *in_reply_to = xs_dict_get(obj, "inReplyTo");
if (!xs_is_null(in_reply_to) && *in_reply_to) {
/* update the children index of the parent */
xs *c_idx = _object_fn(in_reply_to);
c_idx = xs_replace_i(c_idx, ".json", "_c.idx");
if (!index_in(c_idx, id)) {
index_add(c_idx, id);
srv_debug(1, xs_fmt("object_add added child %s to %s", id, c_idx));
}
else
srv_debug(1, xs_fmt("object_add %s child already in %s", id, c_idx));
/* create a one-element index with the parent */
xs *p_idx = xs_replace(fn, ".json", "_p.idx");
index_add(p_idx, in_reply_to);
srv_debug(1, xs_fmt("object_add added parent %s to %s", in_reply_to, p_idx));
}
}
else
status = 500;
srv_debug(1, xs_fmt("object_add %s %s %d", id, fn, status));
return status;
}
int object_add(const char *id, d_char *obj)
/* stores an object */
{
return _object_add(id, obj, 0);
}
int object_add_ow(const char *id, d_char *obj)
/* stores an object (overwriting allowed) */
{
return _object_add(id, obj, 1);
}
int object_del_by_md5(const char *md5)
/* deletes an object by its md5 */
{
int status = 404;
xs *fn = _object_fn_by_md5(md5);
if (fn != NULL && unlink(fn) != -1) {
status = 200;
/* also delete associated indexes */
xs *spec = xs_dup(fn);
spec = xs_replace_i(spec, ".json", "*.idx");
xs *files = xs_glob(spec, 0, 0);
char *p, *v;
p = files;
while (xs_list_iter(&p, &v)) {
srv_debug(1, xs_fmt("object_del index %s", v));
unlink(v);
}
}
srv_debug(1, xs_fmt("object_del %s %d", fn, status));
return status;
}
int object_del(const char *id)
/* deletes an object */
{
xs *md5 = xs_md5_hex(id, strlen(id));
return object_del_by_md5(md5);
}
int object_del_if_unref(const char *id)
/* deletes an object if its n_links < 2 */
{
xs *fn = _object_fn(id);
int n_links;
int ret = 0;
if (mtime_nl(fn, &n_links) > 0.0 && n_links < 2)
ret = object_del(id);
return ret;
}
d_char *_object_index_fn(const char *id, const char *idxsfx)
/* returns the filename of an object's index */
{
d_char *fn = _object_fn(id);
return xs_replace_i(fn, ".json", idxsfx);
}
int object_likes_len(const char *id)
/* returns the number of likes (without reading the index) */
{
xs *fn = _object_index_fn(id, "_l.idx");
return index_len(fn);
}
int object_announces_len(const char *id)
/* returns the number of announces (without reading the index) */
{
xs *fn = _object_index_fn(id, "_a.idx");
return index_len(fn);
}
d_char *object_children(const char *id)
/* returns the list of an object's children */
{
xs *fn = _object_index_fn(id, "_c.idx");
return index_list(fn, XS_ALL);
}
d_char *object_likes(const char *id)
{
xs *fn = _object_index_fn(id, "_l.idx");
return index_list(fn, XS_ALL);
}
d_char *object_announces(const char *id)
{
xs *fn = _object_index_fn(id, "_a.idx");
return index_list(fn, XS_ALL);
}
int object_parent(const char *id, char *buf, int size)
/* returns the object parent, if any */
{
xs *fn = _object_fn_by_md5(id);
fn = xs_replace_i(fn, ".json", "_p.idx");
return index_first(fn, buf, size);
}
int object_admire(const char *id, const char *actor, int like)
/* actor likes or announces this object */
{
int status = 200;
xs *fn = _object_fn(id);
fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
if (!index_in(fn, actor)) {
status = index_add(fn, actor);
srv_debug(1, xs_fmt("object_admire (%s) %s %s", like ? "Like" : "Announce", actor, fn));
}
return status;
}
int _object_user_cache(snac *snac, const char *id, const char *cachedir, int del)
/* adds or deletes from a user cache */
{
xs *ofn = _object_fn(id);
xs *l = xs_split(ofn, "/");
xs *cfn = xs_fmt("%s/%s/%s", snac->basedir, cachedir, xs_list_get(l, -1));
xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir);
int ret;
if (del) {
if ((ret = unlink(cfn)) != -1)
index_del(idx, id);
}
else {
if ((ret = link(ofn, cfn)) != -1)
index_add(idx, id);
}
return ret;
}
int object_user_cache_add(snac *snac, const char *id, const char *cachedir)
/* caches an object into a user cache */
{
return _object_user_cache(snac, id, cachedir, 0);
}
int object_user_cache_del(snac *snac, const char *id, const char *cachedir)
/* deletes an object from a user cache */
{
return _object_user_cache(snac, id, cachedir, 1);
}
int object_user_cache_in(snac *snac, const char *id, const char *cachedir)
/* checks if an object is stored in a cache */
{
xs *md5 = xs_md5_hex(id, strlen(id));
xs *cfn = xs_fmt("%s/%s/%s.json", snac->basedir, cachedir, md5);
return !!(mtime(cfn) != 0.0);
}
d_char *object_user_cache_list(snac *snac, const char *cachedir, int max)
/* returns the objects in a cache as a list */
{
xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir);
return index_list(idx, max);
}
/** specialized functions **/
/** followers **/
int follower_add(snac *snac, const char *actor)
/* adds a follower */
{
int ret = object_user_cache_add(snac, actor, "followers");
snac_debug(snac, 2, xs_fmt("follower_add %s", actor));
return ret == -1 ? 500 : 200;
}
int follower_del(snac *snac, const char *actor)
/* deletes a follower */
{
int ret = object_user_cache_del(snac, actor, "followers");
snac_debug(snac, 2, xs_fmt("follower_del %s", actor));
return ret == -1 ? 404 : 200;
}
int follower_check(snac *snac, const char *actor)
/* checks if someone is a follower */
{
return object_user_cache_in(snac, actor, "followers");
}
d_char *follower_list(snac *snac)
/* returns the list of followers */
{
xs *list = object_user_cache_list(snac, "followers", XS_ALL);
d_char *fwers = xs_list_new();
char *p, *v;
/* resolve the list of md5 to be a list of actors */
p = list;
while (xs_list_iter(&p, &v)) {
xs *a_obj = NULL;
if (valid_status(object_get_by_md5(v, &a_obj, NULL))) {
char *actor = xs_dict_get(a_obj, "id");
if (!xs_is_null(actor))
fwers = xs_list_append(fwers, actor);
}
}
return fwers;
}
/** timeline **/
double timeline_mtime(snac *snac)
{
xs *fn = xs_fmt("%s/private.idx", snac->basedir);
return mtime(fn);
}
int timeline_del(snac *snac, char *id)
/* deletes a message from the timeline */
{
/* delete from the user's caches */
object_user_cache_del(snac, id, "public");
object_user_cache_del(snac, id, "private");
/* try to delete the object if it's not used elsewhere */
return object_del_if_unref(id);
}
void timeline_update_indexes(snac *snac, const char *id)
/* updates the indexes */
{
object_user_cache_add(snac, id, "private");
if (xs_startswith(id, snac->actor)) {
xs *msg = NULL;
if (valid_status(object_get(id, &msg, NULL))) {
/* if its ours and is public, also store in public */
if (is_msg_public(snac, msg))
object_user_cache_add(snac, id, "public");
}
}
}
int timeline_add(snac *snac, char *id, char *o_msg, char *parent, char *referrer)
/* adds a message to the timeline */
{
int ret = object_add(id, o_msg);
timeline_update_indexes(snac, id);
snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
return ret;
}
void timeline_admire(snac *snac, char *o_msg, char *id, char *admirer, int like)
/* updates a timeline entry with a new admiration */
{
/* if we are admiring this, add to both timelines */
if (!like && strcmp(admirer, snac->actor) == 0) {
object_user_cache_add(snac, id, "public");
object_user_cache_add(snac, id, "private");
}
object_admire(id, admirer, like);
snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
like ? "Like" : "Announce", id, admirer));
}
d_char *timeline_top_level(d_char *list)
/* returns the top level md5 entries from this index */
{
xs_set seen;
char *p, *v;
xs_set_init(&seen);
p = list;
while (xs_list_iter(&p, &v)) {
char line[256] = "";
strcpy(line, v);
for (;;) {
char line2[256];
/* if it doesn't have a parent, use this */
if (!object_parent(line, line2, sizeof(line2)))
break;
/* well, there is a parent... but if it's not there, use this */
if (!object_here_by_md5(line2))
break;
/* it's here! try again with its own parent */
strcpy(line, line2);
}
xs_set_add(&seen, line);
}
return xs_set_result(&seen);
}
d_char *timeline_simple_list(snac *snac, const char *idx_name, int skip, int show)
/* returns a timeline (with all entries) */
{
int c_max;
/* maximum number of items in the timeline */
c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
/* never more timeline entries than the configured maximum */
if (show > c_max)
show = c_max;
xs *idx = xs_fmt("%s/%s.idx", snac->basedir, idx_name);
return index_list_desc(idx, skip, show);
}
d_char *timeline_list(snac *snac, const char *idx_name, int skip, int show)
/* returns a timeline (only top level entries) */
{
xs *list = timeline_simple_list(snac, idx_name, skip, show);
return timeline_top_level(list);
}
/** following **/
/* this needs special treatment and cannot use the object db as is,
with a link to a cached author, because we need the Follow object
in case we need to unfollow (Undo + original Follow) */
d_char *_following_fn(snac *snac, char *actor)
{
xs *md5 = xs_md5_hex(actor, strlen(actor));
return xs_fmt("%s/following/%s.json", snac->basedir, md5);
}
int following_add(snac *snac, char *actor, char *msg)
/* adds to the following list */
{
int ret = 201; /* created */
xs *fn = _following_fn(snac, actor);
FILE *f;
if ((f = fopen(fn, "w")) != NULL) {
xs *j = xs_json_dumps_pp(msg, 4);
fwrite(j, 1, strlen(j), f);
fclose(f);
}
else
ret = 500;
snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
return ret;
}
int following_del(snac *snac, char *actor)
/* we're not following this actor any longer */
{
xs *fn = _following_fn(snac, actor);
unlink(fn);
snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
return 200;
}
int following_check(snac *snac, char *actor)
/* checks if we are following this actor */
{
xs *fn = _following_fn(snac, actor);
return !!(mtime(fn) != 0.0);
}
int following_get(snac *snac, char *actor, d_char **data)
/* returns the 'Follow' object */
{
xs *fn = _following_fn(snac, actor);
FILE *f;
int status = 200;
if ((f = fopen(fn, "r")) != NULL) {
xs *j = xs_readall(f);
fclose(f);
*data = xs_json_loads(j);
}
else
status = 404;
return status;
}