aboutsummaryrefslogtreecommitdiff
path: root/neighborhood.c
blob: 964a63349ff636adf3dbe84f16fee395f143f3c1 (plain)
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
/* snac - A simple, minimalistic ActivityPub instance */
/* copyright (c) 2022 - 2026 grunfink et al. / MIT license */

/* BEGIN neighborhood */
/* neighborhood feed: feed of public posts from servers listed in server.json
   under the "neighborhood" key. */

#include "xs.h"
#include "xs_set.h"

#include "snac.h"

xs_str *neighborhood_index_fn(void)
/* returns the filename of the neighborhood index */
{
    return xs_fmt("%s/neighborhood.idx", srv_basedir);
}


int neighborhood_contains_actor(const char *actor)
/* 1 if the actor's host is in srv_config["neighborhood"], 0 otherwise */
{
    if (!xs_is_string(actor) || *actor == '\0')
        return 0;

    const xs_list *neighborhoods = xs_dict_get(srv_config, "neighborhood");
    if (xs_type(neighborhoods) != XSTYPE_LIST)
        return 0;

    /* extract host: third segment of "https://host/..." */
    xs *parts = xs_split(actor, "/");
    const char *raw_host = xs_list_get(parts, 2);
    if (!xs_is_string(raw_host) || *raw_host == '\0')
        return 0;

    xs *host = xs_tolower_i(xs_dup(raw_host));

    const xs_str *v;
    int c = 0;
    while (xs_list_next(neighborhoods, &v, &c)) {
        if (!xs_is_string(v))
            continue;
        xs *candidate = xs_tolower_i(xs_dup(v));
        if (strcmp(host, candidate) == 0)
            return 1;
    }

    return 0;
}


void neighborhood_update_indexes(const char *id, const xs_dict *msg)
/* adds id to neighborhood.idx when the author's host is allowlisted and the post is public */
{
    if (!xs_is_string(id) || msg == NULL)
        return;

    if (xs_type(xs_dict_get(srv_config, "neighborhood")) != XSTYPE_LIST)
        return;

    if (get_msg_visibility(msg) != SCOPE_PUBLIC)
        return;

    const char *atto = get_atto(msg);
    if (!neighborhood_contains_actor(atto))
        return;

    xs *fn = neighborhood_index_fn();
    if (!index_in(fn, id))
        index_add(fn, id);
}


xs_list *timeline_neighborhood_list(int skip, int show)
/* returns the timeline for the neighborhood */
{
    xs *idx = neighborhood_index_fn();
    xs *lst = index_list_desc(idx, skip, show);

    /* make the list unique */
    xs_set rep;
    xs_set_init(&rep);
    const char *md5;

    xs_list_foreach(lst, md5)
        xs_set_add(&rep, md5);

    return xs_set_result(&rep);
}
/* END neighborhood */