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

storage.py « archive « storage « common « gajim - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: db910cf675cde2d4caa38e42728c4c32c48d558b (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
# 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

import calendar
import datetime
import logging
import sqlite3 as sqlite
import time
from collections import namedtuple
from collections.abc import Iterator
from functools import partial

from nbxmpp import JID

from gajim.common import app
from gajim.common import configpaths
from gajim.common.const import MAX_MESSAGE_CORRECTION_DELAY
from gajim.common.modules.contacts import GroupchatContact
from gajim.common.storage.archive.const import MessageState
from gajim.common.storage.archive.const import MessageType
from gajim.common.storage.archive.migration_v8 import MigrationV8
from gajim.common.storage.archive.statements import ARCHIVE_CREATE_STMT
from gajim.common.storage.archive.statements import FIND_CORRECTED_MSG_STMT
from gajim.common.storage.archive.statements import \
    FIND_CORRECTED_MSG_WITH_RES_STMT
from gajim.common.storage.archive.statements import GET_CONVERATION_JIDS_STMT
from gajim.common.storage.archive.statements import GET_CONVERSATION_STMT
from gajim.common.storage.archive.statements import GET_CORRECTIONS_STMT
from gajim.common.storage.archive.statements import \
    GET_CORRECTIONS_WITH_RES_STMT
from gajim.common.storage.archive.statements import \
    GET_DAYS_CONTAINING_MESSAGES_STMT
from gajim.common.storage.archive.statements import GET_FIRST_MESSAGE_TS_STMT
from gajim.common.storage.archive.statements import GET_LAST_MESSAGE_TS_STMT
from gajim.common.storage.archive.statements import GET_MAM_ARCHIVE_STATE_STMT
from gajim.common.storage.archive.statements import GET_MESSAGE_META_STMT
from gajim.common.storage.archive.statements import JID_INSERT_STMT
from gajim.common.storage.archive.statements import RESET_ARCHIVE_STATE_STMT
from gajim.common.storage.archive.structs import DbChatJoinedData
from gajim.common.storage.archive.structs import DbConversationJoinedData
from gajim.common.storage.archive.structs import DbConversationJoinedRowData
from gajim.common.storage.archive.structs import DbCorrectionRowData
from gajim.common.storage.archive.structs import DbFiletransferRowData
from gajim.common.storage.archive.structs import DbGroupchatJoinedData
from gajim.common.storage.archive.structs import DbInsertCorrectionRowData
from gajim.common.storage.archive.structs import DbInsertRowDataBase
from gajim.common.storage.archive.structs import DbMamArchiveStateRowData
from gajim.common.storage.archive.structs import DbPrivateMessageJoinedData
from gajim.common.storage.archive.structs import DbUpsertRowDataBase
from gajim.common.storage.base import SqliteStorage
from gajim.common.storage.base import timeit
from gajim.common.storage.base import VALUE_MISSING

from gajim.gtk.db_migration import DBMigration

CURRENT_USER_VERSION = 7

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


def _messages_factory(cursor: sqlite.Cursor, row: tuple[Any, ...]) -> Any:
    fields = [col[0] for col in cursor.description]

    match row[6]:
        case MessageType.CHAT:
            return DbChatJoinedData.from_row(fields, row)
        case MessageType.GROUPCHAT:
            return DbGroupchatJoinedData.from_row(fields, row)
        case MessageType.PM:
            return DbPrivateMessageJoinedData.from_row(fields, row)
        case _:
            raise ValueError


def _row_factory(
    row_class: Any,
    cursor: sqlite.Cursor,
    row: tuple[Any, ...]
) -> Any:

    fields = [col[0] for col in cursor.description]
    kv = dict(zip(fields, row, strict=True))
    return row_class.from_query(kv)


def namedtuple_row(cursor: sqlite.Cursor, row: tuple[Any, ...]) -> Any:
    fields = [column[0] for column in cursor.description]
    cls = namedtuple('Row', fields)  # pyright: ignore
    return cls._make(row)


class MessageArchiveStorage(SqliteStorage):
    def __init__(self, in_memory: bool = False):
        path = None if in_memory else configpaths.get('LOG_DB')
        SqliteStorage.__init__(self,
                               log,
                               path,
                               ARCHIVE_CREATE_STMT % CURRENT_USER_VERSION)

        self._jid_eks: dict[JID | str, int] = {}
        self._accounts_eks: dict[str, int] = {}

    def init(self, **kwargs: Any) -> None:
        SqliteStorage.init(self,
                           detect_types=sqlite.PARSE_COLNAMES)

        self._set_journal_mode('WAL')
        self._enable_foreign_keys()
        self._enable_secure_delete()

        self._con.row_factory = namedtuple_row

        self._con.create_function('like', 1, self._like)

        self._load_jids()

    def _migrate(self) -> None:
        user_version = self.user_version
        if user_version == 0:
            # All migrations from 0.16.9 until 1.0.0
            statements = [
                'ALTER TABLE logs ADD COLUMN "account_id" INTEGER',
                'ALTER TABLE logs ADD COLUMN "stanza_id" TEXT',
                'ALTER TABLE logs ADD COLUMN "encryption" TEXT',
                'ALTER TABLE logs ADD COLUMN "encryption_state" TEXT',
                'ALTER TABLE logs ADD COLUMN "marker" INTEGER',
                'ALTER TABLE logs ADD COLUMN "additional_data" TEXT',
                '''CREATE TABLE IF NOT EXISTS last_archive_message(
                    jid_id INTEGER PRIMARY KEY UNIQUE,
                    last_mam_id TEXT,
                    oldest_mam_timestamp TEXT,
                    last_muc_timestamp TEXT
                    )''',

                '''CREATE INDEX IF NOT EXISTS idx_logs_stanza_id
                   ON logs(stanza_id)''',
                'PRAGMA user_version=1'
            ]

            self._execute_multiple(statements)

        if user_version < 2:
            statements = [
                ('ALTER TABLE last_archive_message '
                 'ADD COLUMN "sync_threshold" INTEGER'),
                'PRAGMA user_version=2'
            ]
            self._execute_multiple(statements)

        if user_version < 3:
            statements = [
                'ALTER TABLE logs ADD COLUMN "message_id" TEXT',
                'PRAGMA user_version=3'
            ]
            self._execute_multiple(statements)

        if user_version < 4:
            statements = [
                'ALTER TABLE logs ADD COLUMN "error" TEXT',
                'PRAGMA user_version=4'
            ]
            self._execute_multiple(statements)

        if user_version < 5:
            statements = [
                '''CREATE INDEX IF NOT EXISTS idx_logs_message_id
                   ON logs (message_id)''',
                'PRAGMA user_version=5'
            ]
            self._execute_multiple(statements)

        if user_version < 7:
            statements = [
                'ALTER TABLE logs ADD COLUMN "real_jid" TEXT',
                'ALTER TABLE logs ADD COLUMN "occupant_id" TEXT',
                'PRAGMA user_version=7'
            ]
            self._execute_multiple(statements)

        if user_version < 8:
            if self._path is not None:
                DBMigration(MigrationV8, self._con, self._path)

    @staticmethod
    def _like(search_str: str) -> str:
        return f'%{search_str}%'

    def _get_cursor(
        self,
        factory: Any = None,
        row_class: Any = None
    ) -> sqlite.Cursor:

        cursor = self._con.cursor()

        if row_class is not None:
            factory = partial(factory, row_class)
        cursor.row_factory = factory
        return cursor

    @timeit
    def _load_jids(self) -> None:
        rows = self._con.execute('SELECT entitykey, jid FROM jid').fetchall()
        for row in rows:
            self._jid_eks[row.jid] = row.entitykey

    def _get_account_ek(self, account: str) -> int:
        account_ek = self._accounts_eks.get(account)
        if account_ek is not None:
            return account_ek

        jid = app.get_jid_from_account(account)

        select_stmt = 'SELECT entitykey FROM account WHERE jid = ?'
        result = self._con.execute(select_stmt, (jid,)).fetchone()
        if result is not None:
            self._accounts_eks[account] = result.entitykey
            return result.entitykey

        insert_stmt = 'INSERT INTO account(jid) VALUES (?)'
        entitykey = self._con.execute(insert_stmt, (jid,)).lastrowid
        assert entitykey is not None
        self._accounts_eks[account] = entitykey
        self._delayed_commit()

        return entitykey

    def _get_jid_ek(self, jid: JID | str) -> int:
        entitykey = self._jid_eks.get(jid)
        if entitykey is not None:
            return entitykey

        entitykey = self._con.execute(JID_INSERT_STMT, {'jid': jid}).lastrowid
        assert entitykey is not None
        self._jid_eks[jid] = entitykey
        return entitykey

    def _get_occupant_ek(self,
                         account_ek: int,
                         jid_ek: int,
                         occupant_id: str) -> int:
        # TODO cache calls?
        stmt = '''
            SELECT entitykey FROM occupant WHERE
                fk_accounts_ek = ? AND
                fk_jids_ek = ? AND
                id = ?
        '''

        row = self._con.execute(
            stmt, (account_ek, jid_ek, occupant_id)).fetchone()
        assert row is not None
        return row.entitykey

    def _get_foreign_keys(
        self,
        db_row_data: Any
    ) -> dict[str, int | None]:

        account = getattr(db_row_data, 'account', None)
        if account is None:
            account_ek = None
        else:
            account_ek = self._get_account_ek(account)

        remote_jid = getattr(db_row_data, 'remote_jid', None)
        if remote_jid is None:
            jid_ek = None
        else:
            jid_ek = self._get_jid_ek(remote_jid)

        real_jid_ek = None
        real_jid = getattr(db_row_data, 'real_jid', None)
        if real_jid not in (None, VALUE_MISSING):
            real_jid_ek = self._get_jid_ek(real_jid)

        return {
            'fk_account_ek': account_ek,
            'fk_jid_ek': jid_ek,
            'fk_real_jid_ek': real_jid_ek,
        }

    def get_conversation_jids(self, account: str) -> list[JID]:
        account_ek = self._get_account_ek(account)

        rows = self._con.execute(
            GET_CONVERATION_JIDS_STMT, (account_ek, )).fetchall()
        return [row.remote_jid for row in rows]

    @timeit
    def get_conversation_before_after(self,
                                      account: str,
                                      jid: JID,
                                      before: bool,
                                      timestamp: float,
                                      n_lines: int
                                      ) -> list[DbConversationJoinedData]:
        '''
        Load n_lines lines of conversation with jid before or after timestamp

        :param account:         The account

        :param jid:             The jid for which we request the conversation

        :param before:          bool for direction (before or after timestamp)

        :param timestamp:       timestamp

        :param nlines:          number of rows to get

        returns a list of namedtuples
        '''

        account_ek = self._get_account_ek(account)
        jid_ek = self._get_jid_ek(jid)

        if before:
            time_order = ('message.timestamp < ? ORDER BY '
                          'message.timestamp DESC, message.entitykey DESC')
        else:
            time_order = ('message.timestamp > ? ORDER BY '
                          'message.timestamp ASC, message.entitykey ASC')

        stmt = f'''
            {GET_CONVERSATION_STMT}
            message.fk_jid_ek = ? AND
            message.fk_account_ek = ? AND
            {time_order}
            LIMIT ?
        '''
        cursor = self._get_cursor(_messages_factory)
        return cursor.execute(
            stmt, (jid_ek, account_ek, timestamp, n_lines)).fetchall()

    @timeit
    def get_message_with_entitykey(
        self,
        entitykey: int
    ) -> DbConversationJoinedData:

        stmt = f'''
            {GET_CONVERSATION_STMT}
            message.entitykey = ?
        '''
        cursor = self._get_cursor(_messages_factory)
        return cursor.execute(stmt, (entitykey,)).fetchone()

    def get_row_with_entitykey(self, table: str, entitykey: int) -> Any:
        stmt = f'SELECT * FROM {table} WHERE entitykey=?'
        return self._con.execute(stmt, (entitykey,)).fetchone()

    @timeit
    def get_last_conversation_row(self,
                                  account: str,
                                  jid: JID
                                  ) -> DbConversationJoinedData | None:
        '''
        Load the last line of a conversation with jid for account.
        Loads messages, but no status messages or error messages.

        :param account:         The account

        :param jid:             The jid for which we request the conversation

        returns a namedtuple or None
        '''

        account_ek = self._get_account_ek(account)
        jid_ek = self._get_jid_ek(jid)

        stmt = f'''
            {GET_CONVERSATION_STMT}
            message.fk_jid_ek = ? AND
            message.fk_account_ek = ?
            ORDER BY message.timestamp DESC
        '''
        cursor = self._get_cursor(_messages_factory)
        return cursor.execute(stmt, (jid_ek, account_ek)).fetchone()

    @timeit
    def get_last_correctable_message(self,
                                     account: str,
                                     jid: JID,
                                     message_id: str
                                     ) -> DbConversationJoinedData | None:
        '''
        Load the last correctable message of a conversation by message_id.
        Conditions: max 5 min old
        '''
        min_time = time.time() - MAX_MESSAGE_CORRECTION_DELAY

        account_ek = self._get_account_ek(account)
        jid_ek = self._get_jid_ek(jid)

        # TODO there is no index for message_id

        stmt = f'''
            {GET_CONVERSATION_STMT}
            message.fk_jid_ek = ? AND
            message.fk_account_ek = ? AND
            message.message_id = ? AND
            message.timestamp > ?
            ORDER BY message.timestamp DESC
        '''
        cursor = self._get_cursor(_messages_factory)
        return cursor.execute(
            stmt, (jid_ek, account_ek, message_id, min_time)).fetchone()

    def get_corrected_message_entitykey(
        self,
        m_type: int,
        corrected_data: DbInsertCorrectionRowData
    ) -> int | None:

        fk_account_ek = self._get_account_ek(corrected_data.account)
        fk_jid_ek = self._get_jid_ek(corrected_data.remote_jid)

        params = corrected_data.asdict()
        params['fk_jid_ek'] = fk_jid_ek
        params['fk_account_ek'] = fk_account_ek

        stmt = FIND_CORRECTED_MSG_STMT
        if (m_type == MessageType.GROUPCHAT and
                corrected_data.fk_occupant_ek is None):
            stmt = FIND_CORRECTED_MSG_WITH_RES_STMT

        cursor = self._get_cursor()
        result = cursor.execute(stmt, params).fetchone()
        if result is None:
            return None
        return result[0]

    def get_corrections(
        self,
        joined_data: DbConversationJoinedRowData
    ) -> list[DbCorrectionRowData]:

        occupant_ek = None
        if joined_data.occupants is not None:
            occupant_ek = joined_data.occupants.entitykey

        resource = None
        if (joined_data.m_type == MessageType.GROUPCHAT and
                joined_data.occupants is None):
            resource = joined_data.resource

        params = {
            'fk_jid_ek': joined_data.remote_ek,
            'fk_account_ek': joined_data.account_ek,
            'message_id': joined_data.message_id,
            'resource': resource,
            'fk_occupant_ek': occupant_ek,
            'direction': joined_data.direction
        }

        stmt = GET_CORRECTIONS_STMT
        if resource is not None:
            stmt = GET_CORRECTIONS_WITH_RES_STMT

        cursor = self._get_cursor(_row_factory, row_class=DbCorrectionRowData)
        return cursor.execute(stmt, params).fetchall()


    def get_filetransfers(self, message_ek: int) -> list[DbFiletransferRowData]:
        cursor = self._get_cursor(_row_factory, row_class=DbFiletransferRowData)
        stmt = DbFiletransferRowData.get_select_stmt()
        return cursor.execute(stmt, (message_ek,)).fetchall()


    @timeit
    def search_archive(self,
                       account: str | None,
                       jid: str | None,
                       query: str,
                       from_users: list[str] | None = None,
                       before: datetime.datetime | None = None,
                       after: datetime.datetime | None = None
                       ) -> Iterator[DbConversationJoinedData]:
        '''
        Search the conversation log for messages containing the `query` string.

        The search can either span the complete log for the given
        `account` and `jid` or be restricted to a single day by
        specifying `date`.

        :param account: The account

        :param jid: The jid for which we request the conversation

        :param query: A search string

        :param from_users: A list of usernames or None

        :param before: A datetime.datetime instance or None

        :param after: A datetime.datetime instance or None

        returns a list of namedtuples
        '''
        if before is None:
            before_ts = datetime.datetime.now().timestamp()
        else:
            before_ts = before.timestamp()

        after_ts = 0
        if after is not None:
            after_ts = after.timestamp()

        contact_stmt = ''
        if account is not None and jid is not None:
            contact_stmt = '''
                message.fk_jid_ek = ? AND
                message.fk_account_ek = ? AND'''

        if from_users is None:
            users_query_stmt = ''
        else:
            users_query_stmt = 'UPPER(message.resource) IN (?) AND'

        stmt = f'''
            {GET_CONVERSATION_STMT}
            {contact_stmt}
            {users_query_stmt}
            IFNULL(correction.corrected_message, message.message)
            LIKE like(?) AND
            message.timestamp BETWEEN ? AND ?
            ORDER BY message.timestamp DESC, message.entitykey
            '''

        cursor = self._get_cursor(_messages_factory)
        if from_users is None:
            if contact_stmt:
                assert account is not None
                assert jid is not None
                account_ek = self._get_account_ek(account)
                jid_ek = self._get_jid_ek(jid)
                cursor.execute(
                    stmt, (jid_ek, account_ek, query, after_ts, before_ts))
            else:
                cursor.execute(
                    stmt, (query, after_ts, before_ts))
            while True:
                results = cursor.fetchmany(25)
                if not results:
                    break
                for result in results:
                    yield result
            return

        users = ','.join([user.upper() for user in from_users])
        if contact_stmt:
            assert account is not None
            assert jid is not None
            account_ek = self._get_account_ek(account)
            jid_ek = self._get_jid_ek(jid)
            cursor.execute(
                stmt, (jid_ek, account_ek, users, query, after_ts, before_ts))
        else:
            cursor.execute(
                stmt, (users, query, after_ts, before_ts))
        while True:
            results = cursor.fetchmany(25)
            if not results:
                break
            for result in results:
                yield result

    @timeit
    def get_days_containing_messages(self,
                                     account: str,
                                     jid: str,
                                     year: int,
                                     month: int
                                     ) -> list[int]:
        '''
        Get days in month of year where messages for account/jid exist
        '''
        account_ek = self._get_account_ek(account)
        jid_ek = self._get_jid_ek(jid)

        # Calculate the start and end datetime of the month
        date = datetime.datetime(year, month, 1)
        days = calendar.monthrange(year, month)[1] - 1
        delta = datetime.timedelta(
            days=days, hours=23, minutes=59, seconds=59, microseconds=999999)
        start_ts = date.timestamp()
        end_ts = (date + delta).timestamp()

        stmt = f'''
            {GET_DAYS_CONTAINING_MESSAGES_STMT}
            message.fk_jid_ek = ? AND
            message.fk_account_ek = ? AND
            message.timestamp BETWEEN ? AND ?
            ORDER BY message.timestamp
            '''
        return [row.day for row in self._con.execute(
            stmt, (jid_ek, account_ek, start_ts, end_ts)).fetchall()]

    @timeit
    def get_last_history_ts(self,
                            account: str,
                            jid: str
                            ) -> float | None:
        '''
        Get the timestamp of the last message we received for the jid
        '''
        account_ek = self._get_account_ek(account)
        jid_ek = self._get_jid_ek(jid)
        stmt = f'''
            {GET_LAST_MESSAGE_TS_STMT}
            message.fk_jid_ek = ? AND
            message.fk_account_ek = ?
            '''
        # fetchone() returns always at least one Row with all
        # attributes set to None because of the MAX() function
        return self._con.execute(
            stmt, (jid_ek, account_ek)).fetchone().timestamp

    @timeit
    def get_first_history_ts(self,
                             account: str,
                             jid: str
                             ) -> float | None:
        '''
        Get the timestamp of the first message we received for the jid
        '''
        account_ek = self._get_account_ek(account)
        jid_ek = self._get_jid_ek(jid)
        stmt = f'''
            {GET_FIRST_MESSAGE_TS_STMT}
            message.fk_jid_ek = ? AND
            message.fk_account_ek = ?
            '''
        # fetchone() returns always at least one Row with all
        # attributes set to None because of the MIN() function
        return self._con.execute(
            stmt, (jid_ek, account_ek)).fetchone().timestamp

    @timeit
    def get_first_message_meta_for_date(self,
                                        account: str,
                                        jid: str,
                                        date: datetime.datetime
                                        ) -> tuple[int, float] | None:
        '''
        Load meta data (entitykey, timestamp) for the first message of
        a specific date
        '''
        account_ek = self._get_account_ek(account)
        jid_ek = self._get_jid_ek(jid)
        delta = datetime.timedelta(
            hours=23, minutes=59, seconds=59, microseconds=999999)
        date_ts = date.timestamp()
        delta_ts = (date + delta).timestamp()
        stmt = f'''
            {GET_MESSAGE_META_STMT}
            message.fk_jid_ek = ? AND
            message.fk_account_ek = ? AND
            message.timestamp BETWEEN ? AND ?
            ORDER BY message.timestamp ASC, message.entitykey ASC
        '''
        return self._con.execute(
            stmt, (jid_ek, account_ek, date_ts, delta_ts)).fetchone()

    @timeit
    def date_has_history(self,
                         account: str,
                         jid: str,
                         date: datetime.datetime
                         ) -> float | None:
        '''
        Get a single meta row of a message for 'jid'
        in time range of one day
        '''
        account_ek = self._get_account_ek(account)
        jid_ek = self._get_jid_ek(jid)
        delta = datetime.timedelta(
            hours=23, minutes=59, seconds=59, microseconds=999999)
        start_ts = date.timestamp()
        end_ts = (date + delta).timestamp()
        stmt = f'''
            {GET_MESSAGE_META_STMT}
            message.fk_jid_ek = ? AND
            message.fk_account_ek = ? AND
            message.timestamp BETWEEN ? AND ?
            '''
        return self._con.execute(
            stmt, (jid_ek, account_ek, start_ts, end_ts)).fetchone()

    def _insert_row(
        self,
        row_data: DbInsertRowDataBase,
        with_entitykey: int | None = None,
        raise_on_conflict: bool = True,
        ignore_on_conflict: bool = False,
    ) -> int:

        foreign_keys = self._get_foreign_keys(row_data)

        values = row_data.asdict()
        values.update(foreign_keys)
        if with_entitykey is not None:
            values['entitykey'] = with_entitykey

        log.info('Insert into %s: %s', row_data.get_table_name(), values)
        try:
            cursor = self._con.execute(row_data.get_insert_stmt(), values)
        except sqlite.IntegrityError as error:
            if ignore_on_conflict:
                self._log.debug('Ignore failed insert because of contraint')
                return -1

            if raise_on_conflict:
                raise error

            row = self._con.execute(
                row_data.get_select_stmt(), values).fetchone()
            return row.entitykey

        entitykey = cursor.lastrowid
        assert entitykey is not None
        return entitykey

    def insert_row(
        self,
        row_data: DbInsertRowDataBase,
        dependent_row_data: list[DbInsertRowDataBase] | None = None,
        raise_on_conflict: bool = True,
        ignore_on_conflict: bool = False,
    ) -> int:

        if dependent_row_data is None:
            dependent_row_data = []

        entitykey = self._insert_row(
            row_data,
            raise_on_conflict=raise_on_conflict,
            ignore_on_conflict=ignore_on_conflict)
        if entitykey == -1:
            return -1

        for row_data in dependent_row_data:
            self._insert_row(row_data, with_entitykey=entitykey)

        self._delayed_commit()
        return entitykey

    def upsert_row(self, row_data: DbUpsertRowDataBase) -> int:
        foreign_keys = self._get_foreign_keys(row_data)

        values = row_data.asdict()
        values.update(foreign_keys)

        log.info('Upsert into %s: %s', row_data.get_table_name(), values)
        row = self._con.execute(row_data.get_upsert_stmt(), values).fetchone()
        if row is None:
            row = self._con.execute(
                row_data.get_select_stmt(), values).fetchone()

        self._delayed_commit()
        return row.entitykey

    @timeit
    def get_recent_muc_nicks(self, contact: GroupchatContact) -> set[str]:
        account_ek = self._get_account_ek(contact.account)
        jid_ek = self._get_jid_ek(contact.jid)

        sql = '''
            SELECT resource
            FROM message WHERE
            fk_jid_ek = ? AND
            fk_account_ek = ?
            ORDER BY timestamp DESC'''

        results = self._con.execute(sql, (jid_ek, account_ek)).fetchmany(50)

        nicknames: set[str] = set()
        for row in results:
            if row.resource is None:
                continue
            nicknames.add(row.resource)

        return nicknames

    def get_mam_archive_state(
        self,
        account: str,
        jid: JID
    ) -> DbMamArchiveStateRowData | None:

        account_ek = self._get_account_ek(account)
        jid_ek = self._get_jid_ek(jid)

        cursor = self._get_cursor(_row_factory, DbMamArchiveStateRowData)
        return cursor.execute(
            GET_MAM_ARCHIVE_STATE_STMT, (jid_ek, account_ek)).fetchone()

    def reset_mam_archive_state(self, account: str, jid: str) -> None:

        account_ek = self._get_account_ek(account)
        jid_ek = self._get_jid_ek(jid)

        self._con.execute(RESET_ARCHIVE_STATE_STMT, (jid_ek, account_ek))
        self._delayed_commit()

    def _delete_messages(self, find_stmt: str, params: Any) -> None:
        delete_stmts = [
            f'DELETE FROM message WHERE entitykey IN ({find_stmt})',
        ]

        for stmt in delete_stmts:
            self._con.execute(stmt, params)

    def delete_pending_message(
        self,
        account: str,
        remote_jid: JID,
        message_id: str
    ) -> int | None:

        account_ek = self._get_account_ek(account)
        jid_ek = self._get_jid_ek(remote_jid)

        delete_stmt = '''
            DELETE FROM message
            WHERE
                state = ? AND
                message_id = ? AND
                fk_jid_ek = ? AND
                fk_account_ek = ?
            RETURNING entitykey
        '''

        results = self._con.execute(delete_stmt, (
            MessageState.PENDING,
            message_id,
            jid_ek,
            account_ek)).fetchall()

        if results:
            assert len(results) == 1
            self._delayed_commit()
            return results[0].entitykey

    def delete_message(self, entitykey: int) -> None:

        # TODO Delete Corrections
        delete_stmts = [
            'DELETE FROM message WHERE entitykey = ?',
        ]

        for stmt in delete_stmts:
            self._con.execute(stmt, (entitykey,))
        self._commit()

    def remove_history(self, account: str, jid: JID) -> None:
        '''
        Remove history for a specific chat.
        '''

        account_ek = self._get_account_ek(account)
        jid_ek = self._get_jid_ek(jid)

        statements = [
            'DELETE FROM message WHERE fk_jid_ek = ? and fk_account_ek = ?',
            'DELETE FROM error WHERE fk_jid_ek = ? and fk_account_ek = ?',
            'DELETE FROM reaction WHERE fk_jid_ek = ? and fk_account_ek = ?',
            'DELETE FROM retraction WHERE fk_jid_ek = ? and fk_account_ek = ?',
            'DELETE FROM moderation WHERE fk_jid_ek = ? and fk_account_ek = ?',
            'DELETE FROM correction WHERE fk_jid_ek = ? and fk_account_ek = ?',
            'DELETE FROM marker WHERE fk_jid_ek = ? and fk_account_ek = ?',
        ]

        for stmt in statements:
            self._con.execute(stmt, (jid_ek, account_ek))

        self._commit()

        log.info('Removed history for: %s', jid)

    def remove_all_history(self) -> None:
        '''
        Remove all messages for all accounts
        '''
        statements = [
            'DELETE FROM error',
            'DELETE FROM reaction',
            'DELETE FROM retraction',
            'DELETE FROM moderation',
            'DELETE FROM correction',
            'DELETE FROM marker',
            'DELETE FROM message',
        ]
        self._execute_multiple(statements)
        log.info('Removed all chat history')

    def remove_all_from_account(self, account: str) -> None:
        account_ek = self._get_account_ek(account)
        self._con.execute(
            'DELETE FROM account WHERE entitykey = ?', (account_ek,))
        self._commit()

    def cleanup_chat_history(self) -> None:
        '''
        Remove messages from account where messages are older than max_age
        '''
        for account in app.settings.get_accounts():
            max_age = app.settings.get_account_setting(
                account, 'chat_history_max_age')
            if max_age == -1:
                continue

            account_ek = self._get_account_ek(account)
            now = time.time()
            point_in_time = now - int(max_age)

            find_stmt = '''
                SELECT entitykey FROM message
                WHERE fk_account_ek = ? and timestamp < ?
            '''

            results = self._con.execute(
                find_stmt, (account_ek, point_in_time)).fetchall()
            if not results:
                continue

            self._delete_messages(find_stmt, (account_ek, point_in_time))
            self._commit()
            log.info('Removed %s old messages for %s', len(results), account)

    def get_messages_for_export(self,
                                account: str,
                                remote_jid: JID
                                ) -> Iterator[DbConversationJoinedData]:

        account_ek = self._get_account_ek(account)
        jid_ek = self._get_jid_ek(remote_jid)

        stmt = f'''
            {GET_CONVERSATION_STMT}
            message.fk_jid_ek = ? AND
            message.fk_account_ek = ?
            ORDER BY message.timestamp ASC
        '''

        cursor = self._con.execute(stmt, (jid_ek, account_ek))
        while True:
            results = cursor.fetchmany(10)
            if not results:
                break
            yield from results