diff options
| author | 2024-05-08 10:20:25 +0200 | |
|---|---|---|
| committer | 2024-05-08 10:20:25 +0200 | |
| commit | 3ab733cdf5a71b9a27399e8336e0c236c13d67fb (patch) | |
| tree | 81e9885f2bffabcaf849f170888eaa0be40a4234 | |
| parent | Merge branch 'master' of grunfink-codeberg:grunfink/snac2 (diff) | |
| download | snac2-3ab733cdf5a71b9a27399e8336e0c236c13d67fb.tar.gz snac2-3ab733cdf5a71b9a27399e8336e0c236c13d67fb.tar.xz snac2-3ab733cdf5a71b9a27399e8336e0c236c13d67fb.zip | |
New function search_by_content().
| -rw-r--r-- | data.c | 58 | ||||
| -rw-r--r-- | main.c | 18 | ||||
| -rw-r--r-- | snac.h | 3 |
3 files changed, 79 insertions, 0 deletions
| @@ -2488,6 +2488,64 @@ void notify_clear(snac *snac) | |||
| 2488 | } | 2488 | } |
| 2489 | 2489 | ||
| 2490 | 2490 | ||
| 2491 | /** searches **/ | ||
| 2492 | |||
| 2493 | xs_list *search_by_content(snac *user, const xs_list *timeline, | ||
| 2494 | const char *regex, int timeout) | ||
| 2495 | /* returns a list of posts which content matches the regex */ | ||
| 2496 | { | ||
| 2497 | xs_list *r = xs_list_new(); | ||
| 2498 | |||
| 2499 | if (timeout == 0) | ||
| 2500 | timeout = 3; | ||
| 2501 | |||
| 2502 | int c = 0; | ||
| 2503 | char *v; | ||
| 2504 | |||
| 2505 | time_t t = time(NULL) + timeout; | ||
| 2506 | |||
| 2507 | while (xs_list_next(timeline, &v, &c)) { | ||
| 2508 | xs *post = NULL; | ||
| 2509 | |||
| 2510 | /* timeout? */ | ||
| 2511 | if (time(NULL) > t) | ||
| 2512 | break; | ||
| 2513 | |||
| 2514 | int status; | ||
| 2515 | |||
| 2516 | if (user) | ||
| 2517 | status = timeline_get_by_md5(user, v, &post); | ||
| 2518 | else | ||
| 2519 | status = object_get_by_md5(v, &post); | ||
| 2520 | |||
| 2521 | if (!valid_status(status)) | ||
| 2522 | continue; | ||
| 2523 | |||
| 2524 | /* must be a Note */ | ||
| 2525 | if (strcmp(xs_dict_get_def(post, "type", ""), "Note")) | ||
| 2526 | continue; | ||
| 2527 | |||
| 2528 | char *content = xs_dict_get(post, "content"); | ||
| 2529 | |||
| 2530 | if (xs_is_null(content)) | ||
| 2531 | continue; | ||
| 2532 | |||
| 2533 | /* strip HTML */ | ||
| 2534 | xs *c = xs_regex_replace(content, "<[^>]+>", " "); | ||
| 2535 | c = xs_regex_replace_i(c, " {2,}", " "); | ||
| 2536 | c = xs_tolower_i(c); | ||
| 2537 | |||
| 2538 | /* apply regex */ | ||
| 2539 | xs *l = xs_regex_select_n(c, regex, 1); | ||
| 2540 | |||
| 2541 | if (xs_list_len(l)) | ||
| 2542 | r = xs_list_append(r, v); | ||
| 2543 | } | ||
| 2544 | |||
| 2545 | return r; | ||
| 2546 | } | ||
| 2547 | |||
| 2548 | |||
| 2491 | /** the queue **/ | 2549 | /** the queue **/ |
| 2492 | 2550 | ||
| 2493 | static xs_dict *_enqueue_put(const char *fn, xs_dict *msg) | 2551 | static xs_dict *_enqueue_put(const char *fn, xs_dict *msg) |
| @@ -44,6 +44,7 @@ int usage(void) | |||
| 44 | printf("limit {basedir} {uid} {actor} Limits an actor (drops their announces)\n"); | 44 | printf("limit {basedir} {uid} {actor} Limits an actor (drops their announces)\n"); |
| 45 | printf("unlimit {basedir} {uid} {actor} Unlimits an actor\n"); | 45 | printf("unlimit {basedir} {uid} {actor} Unlimits an actor\n"); |
| 46 | printf("verify_links {basedir} {uid} Verifies a user's links (in the metadata)\n"); | 46 | printf("verify_links {basedir} {uid} Verifies a user's links (in the metadata)\n"); |
| 47 | printf("search {basedir} {uid} {regex} Searches posts by content\n"); | ||
| 47 | 48 | ||
| 48 | return 1; | 49 | return 1; |
| 49 | } | 50 | } |
| @@ -374,6 +375,23 @@ int main(int argc, char *argv[]) | |||
| 374 | return 0; | 375 | return 0; |
| 375 | } | 376 | } |
| 376 | 377 | ||
| 378 | if (strcmp(cmd, "search") == 0) { /** **/ | ||
| 379 | xs *tl = timeline_simple_list(&snac, "private", 0, XS_ALL); | ||
| 380 | |||
| 381 | /* 'url' contains the regex */ | ||
| 382 | xs *r = search_by_content(&snac, tl, url, 10); | ||
| 383 | |||
| 384 | int c = 0; | ||
| 385 | char *v; | ||
| 386 | |||
| 387 | /* print results as standalone links */ | ||
| 388 | while (xs_list_next(r, &v, &c)) { | ||
| 389 | printf("%s/admin/p/%s\n", snac.actor, v); | ||
| 390 | } | ||
| 391 | |||
| 392 | return 0; | ||
| 393 | } | ||
| 394 | |||
| 377 | if (strcmp(cmd, "ping") == 0) { /** **/ | 395 | if (strcmp(cmd, "ping") == 0) { /** **/ |
| 378 | xs *actor_o = NULL; | 396 | xs *actor_o = NULL; |
| 379 | 397 | ||
| @@ -179,6 +179,9 @@ xs_list *list_timeline(snac *user, const char *list, int skip, int show); | |||
| 179 | xs_val *list_content(snac *user, const char *list_id, const char *actor_md5, int op); | 179 | xs_val *list_content(snac *user, const char *list_id, const char *actor_md5, int op); |
| 180 | void list_distribute(snac *user, const char *who, const xs_dict *post); | 180 | void list_distribute(snac *user, const char *who, const xs_dict *post); |
| 181 | 181 | ||
| 182 | xs_list *search_by_content(snac *user, const xs_list *timeline, | ||
| 183 | const char *regex, int timeout); | ||
| 184 | |||
| 182 | int actor_add(const char *actor, xs_dict *msg); | 185 | int actor_add(const char *actor, xs_dict *msg); |
| 183 | int actor_get(const char *actor, xs_dict **data); | 186 | int actor_get(const char *actor, xs_dict **data); |
| 184 | int actor_get_refresh(snac *user, const char *actor, xs_dict **data); | 187 | int actor_get_refresh(snac *user, const char *actor, xs_dict **data); |