aboutsummaryrefslogtreecommitdiff
blob: c8d9c221f591729a45020cbb00350376840af5df (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
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
128
129
130
131
132
133
134
135
from peewee import *

TOOT_VISIBILITY_PUBLIC = "public"
TOOT_VISIBILITY_UNLISTED = "unlisted"
TOOT_VISIBILITY_PRIVATE = "private"


db = SqliteDatabase("checkinbot.db")
db.connect(reuse_if_open=True)


class BaseModel(Model):
    class Meta:
        database = db


class User(BaseModel):
    telegram_user_id = CharField(unique=True, primary_key=True)
    access_key = CharField(max_length=256)
    home_instance = CharField(max_length=256)
    home_instance_type = CharField(max_length=128, default="mastodon")
    state = CharField(max_length=128)
    client_id = CharField(max_length=128)
    client_secret = CharField(max_length=128)
    toot_visibility = CharField(max_length=128, default=TOOT_VISIBILITY_PRIVATE)


def update_user_visibility(telegram_user_id: str, visibility: str) -> int:
    with db.connection_context():
        return User.update(toot_visibility=visibility).where(
            User.telegram_user_id == telegram_user_id
        ).execute()


def get_user_by_id(telegram_user_id: str) -> dict:
    with db.connection_context():
        try:
            user = User.get(User.telegram_user_id == telegram_user_id)
            return {
                "telegram_user_id": user.telegram_user_id,
                "access_key": user.access_key,
                "home_instance": user.home_instance,
                "home_instance_type": user.home_instance_type,
                "state": user.state,
                "client_id": user.client_id,
                "client_secret": user.client_secret,
                "toot_visibility": user.toot_visibility,
            }
        except DoesNotExist:
            return {}


def get_user_by_state(state: str) -> dict:
    with db.connection_context():
        try:
            user = User.get(User.state == state)
            return {
                "telegram_user_id": user.telegram_user_id,
                "access_key": user.access_key,
                "home_instance": user.home_instance,
                "home_instance_type": user.home_instance_type,
                "state": user.state,
                "client_id": user.client_id,
                "client_secret": user.client_secret,
                "toot_visibility": user.toot_visibility,
            }
        except DoesNotExist:
            return {}


def get_user_access_key(telegram_user_id: str) -> str:
    with db.connection_context():
        try:
            user = User.get(User.telegram_user_id == telegram_user_id)
            return user.access_key
        except DoesNotExist:
            return ""


def delete_user_by_id(telegram_user_id: str) -> int:
    with db.connection_context():
        return User.delete().where(User.telegram_user_id == telegram_user_id).execute()


def get_user_home_instance(telegram_user_id: str) -> dict:
    with db.connection_context():
        try:
            user = User.get(User.telegram_user_id == telegram_user_id)
            return {
                "home_instance": user.home_instance,
                "home_instance_type": user.home_instance_type,
                "default_visibility": user.toot_visibility,
            }
        except DoesNotExist:
            return {}


class Location(BaseModel):
    fsq_id = CharField(unique=True, primary_key=True)
    name = CharField(max_length=128)
    locality = CharField(max_length=128)
    region = CharField(max_length=128)
    latitude = CharField(max_length=128)
    longitude = CharField(max_length=128)


def get_poi_by_fsq_id(fsq_id) -> dict:
    with db.connection_context():
        try:
            poi = Location.get(Location.fsq_id == fsq_id)
            return {
                "name": poi.name,
                "locality": poi.locality,
                "region": poi.region,
                "latitude": poi.latitude,
                "longitude": poi.longitude,
            }
        except DoesNotExist:
            return {}


def create_or_update_poi(poi: dict) -> int:
    with db.connection_context():
        return Location.insert(
            fsq_id=poi["fsq_id"],
            name=poi["name"],
            locality=poi["locality"],
            region=poi["region"],
            latitude=poi["latitude"],
            longitude=poi["longitude"],
        ).on_conflict_replace().execute()


with db.connection_context():
    db.create_tables([User, Location])
Powered by cgit v1.2.3 (git 2.41.0)