content_search() also searches the instance timeline.

This commit is contained in:
default 2024-05-14 05:17:33 +02:00
parent ba00443b84
commit 281f934f74
2 changed files with 34 additions and 47 deletions

79
data.c
View File

@ -2508,73 +2508,56 @@ xs_list *content_search(snac *user, const char *regex,
time_t t = time(NULL) + max_secs; time_t t = time(NULL) + max_secs;
*timeout = 0; *timeout = 0;
/* iterate both timelines simultaneously */ /* iterate all timelines simultaneously */
xs *pub_tl = timeline_simple_list(user, "public", 0, XS_ALL); xs_list *tls[3] = {0};
int pub_c = 0; char *md5s[3] = {0};
char *pub_md5 = NULL; int c[3] = {0};
xs *priv_tl = priv ? timeline_simple_list(user, "private", 0, XS_ALL) : xs_list_new(); tls[0] = timeline_simple_list(user, "public", 0, XS_ALL); /* public */
int priv_c = 0; tls[1] = timeline_instance_list(0, XS_ALL); /* instance */
char *priv_md5 = NULL; tls[2] = priv ? timeline_simple_list(user, "private", 0, XS_ALL) : xs_list_new(); /* private or none */
/* first positioning */ /* first positioning */
xs_list_next(pub_tl, &pub_md5, &pub_c); for (int n = 0; n < 3; n++)
xs_list_next(priv_tl, &priv_md5, &priv_c); xs_list_next(tls[n], &md5s[n], &c[n]);
show += skip; show += skip;
while (show > 0) { while (show > 0) {
char *md5 = NULL;
enum { NONE, PUBLIC, PRIVATE } from = NONE;
/* timeout? */ /* timeout? */
if (time(NULL) > t) { if (time(NULL) > t) {
*timeout = 1; *timeout = 1;
break; break;
} }
if (pub_md5 == NULL) { /* find the newest post */
/* out of both lists? done */ int newest = -1;
if (priv_md5 == NULL) double mtime = 0.0;
break;
/* out of public: take element from the private timeline and advance */ for (int n = 0; n < 3; n++) {
from = PRIVATE; if (md5s[n] != NULL) {
} xs *fn = _object_fn_by_md5(md5s[n], "content_search");
else double mt = mtime(fn);
if (priv_md5 == NULL) {
/* out of private: take element from the public timeline and advance */
from = PUBLIC;
}
else {
/* candidates from both: choose one from the file dates */
xs *pub_fn = xs_fmt("%s/public/%s.json", user->basedir, pub_md5);
xs *priv_fn = xs_fmt("%s/private/%s.json", user->basedir, priv_md5);
if (mtime(pub_fn) < mtime(priv_fn)) if (mt > mtime) {
from = PRIVATE; newest = n;
else mtime = mt;
from = PUBLIC; }
}
} }
if (from == PUBLIC) { /* public */ if (newest == -1)
md5 = pub_md5;
if (!xs_list_next(pub_tl, &pub_md5, &pub_c))
pub_md5 = NULL;
}
else
if (from == PRIVATE) { /* private */
md5 = priv_md5;
if (!xs_list_next(priv_tl, &priv_md5, &priv_c))
priv_md5 = NULL;
}
if (md5 == NULL)
break; break;
char *md5 = md5s[newest];
/* advance the chosen timeline */
if (!xs_list_next(tls[newest], &md5s[newest], &c[newest]))
md5s[newest] = NULL;
xs *post = NULL; xs *post = NULL;
if (!valid_status(timeline_get_by_md5(user, md5, &post))) if (!valid_status(object_get_by_md5(md5, &post)))
continue; continue;
if (!xs_match(xs_dict_get_def(post, "type", "-"), POSTLIKE_OBJECT_TYPE)) if (!xs_match(xs_dict_get_def(post, "type", "-"), POSTLIKE_OBJECT_TYPE))
@ -2608,6 +2591,10 @@ xs_list *content_search(snac *user, const char *regex,
} }
} }
xs_free(tls[0]);
xs_free(tls[1]);
xs_free(tls[2]);
return r; return r;
} }

2
html.c
View File

@ -2604,7 +2604,7 @@ int html_get_handler(const xs_dict *req, const char *q_path,
else else
title = xs_fmt(L("Nothing found for '%s'"), q); title = xs_fmt(L("Nothing found for '%s'"), q);
*body = html_timeline(&snac, tl, 0, skip, tl_len, to || tl_len == show, title, page, 1); *body = html_timeline(&snac, tl, 0, skip, tl_len, to || tl_len == show, title, page, 0);
*b_size = strlen(*body); *b_size = strlen(*body);
status = 200; status = 200;
} }