summaryrefslogtreecommitdiffstats
path: root/api/views/timelines.py
blob: b14586f95f54ef927f27d92ca946d3e7d1a30f27 (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
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
from activities.models import Post, PostInteraction, TimelineEvent
from api import schemas
from api.decorators import identity_required
from api.pagination import MastodonPaginator
from api.views.base import api_router
from users.models import Identity


@api_router.get("/v1/timelines/home", response=list[schemas.Status])
@identity_required
def home(
    request,
    max_id: str | None = None,
    since_id: str | None = None,
    min_id: str | None = None,
    limit: int = 20,
):
    paginator = MastodonPaginator(Post)
    queryset = (
        TimelineEvent.objects.filter(
            identity=request.identity,
            type__in=[TimelineEvent.Types.post],
        )
        .select_related("subject_post", "subject_post__author")
        .prefetch_related("subject_post__attachments")
        .order_by("-created")
    )
    events = paginator.paginate(
        queryset,
        min_id=min_id,
        max_id=max_id,
        since_id=since_id,
        limit=limit,
    )
    interactions = PostInteraction.get_event_interactions(events, request.identity)
    return [
        event.subject_post.to_mastodon_json(interactions=interactions)
        for event in events
    ]


@api_router.get("/v1/timelines/public", response=list[schemas.Status])
@identity_required
def public(
    request,
    local: bool = False,
    remote: bool = False,
    only_media: bool = False,
    max_id: str | None = None,
    since_id: str | None = None,
    min_id: str | None = None,
    limit: int = 20,
):
    queryset = (
        Post.objects.public()
        .filter(author__restriction=Identity.Restriction.none)
        .select_related("author")
        .prefetch_related("attachments")
        .order_by("-created")
    )
    if local:
        queryset = queryset.filter(local=True)
    elif remote:
        queryset = queryset.filter(local=False)
    if only_media:
        queryset = queryset.filter(attachments__id__isnull=True)
    paginator = MastodonPaginator(Post)
    posts = paginator.paginate(
        queryset,
        min_id=min_id,
        max_id=max_id,
        since_id=since_id,
        limit=limit,
    )
    interactions = PostInteraction.get_post_interactions(posts, request.identity)
    return [post.to_mastodon_json(interactions=interactions) for post in posts]


@api_router.get("/v1/timelines/tag/{hashtag}", response=list[schemas.Status])
@identity_required
def hashtag(
    request,
    hashtag: str,
    local: bool = False,
    only_media: bool = False,
    max_id: str | None = None,
    since_id: str | None = None,
    min_id: str | None = None,
    limit: int = 20,
):
    if limit > 40:
        limit = 40
    queryset = (
        Post.objects.public()
        .filter(author__restriction=Identity.Restriction.none)
        .tagged_with(hashtag)
        .select_related("author")
        .prefetch_related("attachments")
        .order_by("-created")
    )
    if local:
        queryset = queryset.filter(local=True)
    if only_media:
        queryset = queryset.filter(attachments__id__isnull=True)
    paginator = MastodonPaginator(Post)
    posts = paginator.paginate(
        queryset,
        min_id=min_id,
        max_id=max_id,
        since_id=since_id,
        limit=limit,
    )
    interactions = PostInteraction.get_post_interactions(posts, request.identity)
    return [post.to_mastodon_json(interactions=interactions) for post in posts]


@api_router.get("/v1/conversations", response=list[schemas.Status])
@identity_required
def conversations(
    request,
    max_id: str | None = None,
    since_id: str | None = None,
    min_id: str | None = None,
    limit: int = 20,
):
    # We don't implement this yet
    return []