New function _timeline_parent().

This commit is contained in:
default 2022-09-22 14:46:23 +02:00
parent 28c7961e67
commit b5769aca9b

77
data.c
View file

@ -381,6 +381,80 @@ d_char *timeline_list(snac *snac)
}
void _timeline_parent(snac *snac, char *parent, char *child)
/* add child to parent's children list */
{
if (parent != NULL) {
xs *pfn = _timeline_find_fn(snac, parent);
if (pfn != NULL) {
FILE *f;
if ((f = fopen(pfn, "r")) != NULL) {
xs *ji, *msg;
ji = xs_readall(f);
fclose(f);
msg = xs_json_loads(ji);
if (msg != NULL) {
xs *meta;
xs *children;
/* get the children list */
meta = xs_dict_get(msg, "_snac");
children = xs_dict_get(meta, "children");
/* add */
children = xs_list_append(children, child);
/* re-store */
meta = xs_dict_set(meta, "children", children);
msg = xs_dict_set(msg, "_snac", meta);
xs *jo = xs_json_dumps_pp(msg, 4);
xs *ntid = tid(0);
xs *md5 = xs_md5_hex(parent, strlen(parent));
xs *nfn = xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5);
if ((f = fopen(nfn, "w")) != NULL) {
fwrite(jo, strlen(jo), 1, f);
fclose(f);
snac_debug(snac, 1,
xs_fmt("_timeline_parent updated %s %s", parent, nfn));
unlink(pfn);
/* generated by this user? link to local timeline */
if (xs_startswith(parent, snac->actor)) {
xs *lfn = xs_replace(nfn, "/timeline/", "/local/");
xs *olfn = xs_replace(pfn, "/timeline/", "/local/");
link(nfn, lfn);
unlink(olfn);
snac_debug(snac, 1,
xs_fmt("_timeline_parent (local) updated %s %s", parent, lfn));
}
/* retry with grampa */
_timeline_parent(snac, xs_dict_get(meta, "parent"), parent);
}
else
snac_log(snac, xs_fmt("_timeline_parent error writing %s %s", parent, nfn));
}
else
snac_log(snac, xs_fmt("_timeline_parent error reading %s %s", parent, pfn));
}
else
snac_log(snac, xs_fmt("_timeline_parent error opening %s %s", parent, pfn));
}
}
}
void timeline_add(snac *snac, char *id, char *msg, char *parent)
/* adds a message to the timeline */
{
@ -427,6 +501,9 @@ void timeline_add(snac *snac, char *id, char *msg, char *parent)
snac_debug(snac, 1, xs_fmt("timeline_add (local) %s %s", id, lfn));
}
/* relink the parent */
_timeline_parent(snac, parent, id);
}