🗝
summary refs log tree commit diff
path: root/ty.py
blob: e17c0463751296f6dc64ab234ea445740685ebe4 (plain) (blame)
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
from dataclasses import dataclass
from typing import List, Callable
from datetime import datetime
from enum import Enum

class Visibility(Enum):
    public = 1
    unlisted = 2
    followers = 3
    direct = 4

    @classmethod
    def from_db(cls, raw: str) -> "Visibility":
        match raw:
            case "public": return cls.public
            case "home": return cls.unlisted
            case "followers": return cls.followers
            case "specified": return cls.direct
            case _: raise ValueError(f"unknown visibility `{raw}`")


@dataclass
class FilterableNote:
    id: str
    mine: bool
    replies: List["FilterableNote"]
    quotes: List["FilterableNote"]
    when: datetime
    reactions: int
    renotes: int
    visibility: Visibility

    def thread(self) -> List["FilterableNote"]:
        acc = []
        for reply in self.replies:
            acc += reply.thread()
        for quote in self.quotes:
            acc += quote.thread()
        acc.append(self)
        return acc

    def thread_self(self) -> List["FilterableNote"]:
        acc = []
        for reply in self.replies:
            acc += reply.thread_self()
        for quote in self.quotes:
            acc += quote.thread_self()
        if self.mine:
            acc.append(self)
        return acc

    def to_dict(self):
        return {
            "id": self.id,
            "mine": self.mine,
            "replies": [note.to_dict() for note in self.replies],
            "quotes": [note.to_dict() for note in self.quotes],
            "when": self.when.isoformat(),
            "reactions": self.reactions,
            "renotes": self.renotes,
        }