Welcome to mirror list, hosted at ThFree Co, Russian Federation.

events.py « storage « common « gajim - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d54de33d27319536ac1f1ac29f1c31f019fb7487 (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
128
129
130
131
132
133
134
135
136
137
138
139
# This file is part of Gajim.
#
# Gajim is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# Gajim is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.

from __future__ import annotations

from typing import Any
from typing import NamedTuple

import dataclasses
import json
import logging
import sqlite3
from collections import namedtuple

from nbxmpp.protocol import JID

from gajim.common import events
from gajim.common.storage.base import Encoder
from gajim.common.storage.base import SqliteStorage
from gajim.common.types import ChatContactT

EVENTS_SQL_STATEMENT = '''
    CREATE TABLE events (
            account TEXT,
            jid TEXT,
            event TEXT,
            timestamp REAL,
            data TEXT);

    CREATE INDEX idx_account_jid ON events(account, jid);

    PRAGMA user_version=1;
    '''

EVENT_CLASSES: dict[str, Any] = {
    'muc-nickname-changed': events.MUCNicknameChanged,
    'muc-room-config-changed': events.MUCRoomConfigChanged,
    'muc-room-config-finished': events.MUCRoomConfigFinished,
    'muc-room-presence-error': events.MUCRoomPresenceError,
    'muc-room-kicked': events.MUCRoomKicked,
    'muc-room-destroyed': events.MUCRoomDestroyed,
    'muc-user-joined': events.MUCUserJoined,
    'muc-user-left': events.MUCUserLeft,
    'muc-user-role-changed': events.MUCUserRoleChanged,
    'muc-user-affiliation-changed': events.MUCUserAffiliationChanged,
    'muc-user-status-show-changed': events.MUCUserStatusShowChanged,
}

log = logging.getLogger('gajim.c.storage.events')


class EventRow(NamedTuple):
    account: str
    jid: JID
    event: str
    timestamp: float
    data: dict[str, Any]


class EventStorage(SqliteStorage):
    def __init__(self):
        SqliteStorage.__init__(self,
                               log,
                               None,
                               EVENTS_SQL_STATEMENT)

    def init(self, **kwargs: Any) -> None:
        SqliteStorage.init(self,
                           detect_types=sqlite3.PARSE_COLNAMES)
        self._con.row_factory = self._namedtuple_factory

    @staticmethod
    def _namedtuple_factory(cursor: sqlite3.Cursor,
                            row: tuple[Any, ...]) -> NamedTuple:

        assert cursor.description is not None
        fields = [col[0] for col in cursor.description]  # pyright: ignore
        Row = namedtuple('Row', fields)  # pyright: ignore
        return Row(*row)

    def _migrate(self) -> None:
        pass

    def store(self,
              contact: ChatContactT,
              event: Any
              ) -> None:

        event_dict = dataclasses.asdict(event)
        name = event_dict.pop('name')
        timestamp = event_dict.pop('timestamp')

        insert_sql = '''
            INSERT INTO events(account, jid, event, timestamp, data)
            VALUES(?, ?, ?, ?, ?)'''

        self._con.execute(insert_sql, (contact.account,
                                       contact.jid,
                                       name,
                                       timestamp,
                                       json.dumps(event_dict, cls=Encoder)))

    def load(self,
             contact: ChatContactT,
             before: bool,
             timestamp: float,
             n_lines: int,
             ) -> list[events.ApplicationEvent]:

        if before:
            time_order = 'AND timestamp < ? ORDER BY timestamp DESC'
        else:
            time_order = 'AND timestamp > ? ORDER BY timestamp ASC'

        insert_sql = '''
            SELECT account, jid, event, timestamp, data as "data [JSON]"
            FROM events WHERE account=? AND jid=? {time_order}
            LIMIT ?'''.format(time_order=time_order)  # noqa: UP032

        event_list: list[events.ApplicationEvent] = []
        for row in self._con.execute(insert_sql, (contact.account,
                                                  contact.jid,
                                                  timestamp,
                                                  n_lines)).fetchall():
            event_class = EVENT_CLASSES[row.event]
            event_list.append(event_class(**row.data,
                                          timestamp=row.timestamp))
        return event_list