Mercurial > libervia-backend
annotate libervia/backend/memory/sqla_mapping.py @ 4306:94e0968987cd
plugin XEP-0033: code modernisation, improve delivery, data validation:
- Code has been rewritten using Pydantic models and `async` coroutines for data validation
and cleaner element parsing/generation.
- Delivery has been completely rewritten. It now works even if server doesn't support
multicast, and send to local multicast service first. Delivering to local multicast
service first is due to bad support of XEP-0033 in server (notably Prosody which has an
incomplete implementation), and the current impossibility to detect if a sub-domain
service handles fully multicast or only for local domains. This is a workaround to have
a good balance between backward compatilibity and use of bandwith, and to make it work
with the incoming email gateway implementation (the gateway will only deliver to
entities of its own domain).
- disco feature checking now uses `async` corountines. `host` implementation still use
Deferred return values for compatibility with legacy code.
rel 450
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 26 Sep 2024 16:12:01 +0200 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # Libervia: an XMPP client |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
3593
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
19 from datetime import datetime |
3796
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
20 import enum |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
21 import json |
3593
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
22 import time |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
23 from typing import Any, Dict |
3796
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
24 |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from sqlalchemy import ( |
3796
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
26 Boolean, |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
27 Column, |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
28 DDL, |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
29 DateTime, |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
30 Enum, |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
31 Float, |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
32 ForeignKey, |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
33 Index, |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
34 Integer, |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
35 JSON, |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
36 MetaData, |
4152
23d21daed216
core (memory/sqla_mapping): add a `version_id` column to detect race conditions.
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
37 text, |
3796
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
38 Text, |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
39 UniqueConstraint, |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
40 event, |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 from sqlalchemy.orm import declarative_base, relationship |
3796
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
43 from sqlalchemy.sql.functions import now |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 from sqlalchemy.types import TypeDecorator |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 from twisted.words.protocols.jabber import jid |
3593
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
46 from wokkel import generic |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
48 from libervia.backend.core.constants import Const as C |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
49 |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
3583
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
51 Base = declarative_base( |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
52 metadata=MetaData( |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
53 naming_convention={ |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
54 "ix": "ix_%(column_0_label)s", |
3583
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
55 "uq": "uq_%(table_name)s_%(column_0_name)s", |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
56 "ck": "ck_%(table_name)s_%(constraint_name)s", |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
57 "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
58 "pk": "pk_%(table_name)s", |
3583
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
59 } |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
60 ) |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
61 ) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 # keys which are in message data extra but not stored in extra field this is |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 # because those values are stored in separate fields |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
64 NOT_IN_EXTRA = ("origin_id", "stanza_id", "received_timestamp", "update_uid") |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
65 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
66 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
67 class Profiles(dict): |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
68 def __init__(self, *args, **kwargs): |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
69 super().__init__(*args, **kwargs) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
70 self.id_to_profile = {v: k for k, v in self.items()} |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
71 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
72 def __setitem__(self, key, value): |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
73 super().__setitem__(key, value) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
74 self.id_to_profile[value] = key |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
75 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
76 def __delitem__(self, key): |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
77 del self.id_to_profile[self[key]] |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
78 super().__delitem__(key) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
79 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
80 def update(self, *args, **kwargs): |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
81 super().update(*args, **kwargs) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
82 self.id_to_profile = {v: k for k, v in self.items()} |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
83 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
84 def clear(self): |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
85 super().clear() |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
86 self.id_to_profile.clear() |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
87 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
88 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
89 profiles = Profiles() |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
90 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
91 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
92 def get_profile_by_id(profile_id): |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
93 return profiles.id_to_profile.get(profile_id) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 |
3593
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
96 class SyncState(enum.Enum): |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
97 #: synchronisation is currently in progress |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
98 IN_PROGRESS = 1 |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
99 #: synchronisation is done |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
100 COMPLETED = 2 |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
101 #: something wrong happened during synchronisation, won't sync |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
102 ERROR = 3 |
3663
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
103 #: synchronisation won't be done even if a syncing analyser matches |
3593
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
104 NO_SYNC = 4 |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
105 |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
106 |
3744
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
107 class SubscriptionState(enum.Enum): |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
108 SUBSCRIBED = 1 |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
109 PENDING = 2 |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
110 |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
111 |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
112 class NotificationType(enum.Enum): |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
113 chat = "chat" |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
114 blog = "blog" |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
115 calendar = "calendar" |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
116 file = "file" |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
117 call = "call" |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
118 service = "service" |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
119 other = "other" |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
120 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
121 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
122 class NotificationStatus(enum.Enum): |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
123 new = "new" |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
124 read = "read" |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
125 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
126 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
127 class NotificationPriority(enum.IntEnum): |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
128 LOW = 10 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
129 MEDIUM = 20 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
130 HIGH = 30 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
131 URGENT = 40 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
132 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
133 |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 class Json(TypeDecorator): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 """Handle JSON field in DB independant way""" |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
136 |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 # Blob is used on SQLite but gives errors when used here, while Text works fine |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 impl = Text |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 cache_ok = True |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 def process_bind_param(self, value, dialect): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 if value is None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 return None |
4212 | 144 return json.dumps(value, ensure_ascii=False) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 def process_result_value(self, value, dialect): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 if value is None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 return None |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 return json.loads(value) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 class JsonDefaultDict(Json): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 """Json type which convert NULL to empty dict instead of None""" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 def process_result_value(self, value, dialect): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 if value is None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 return {} |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 return json.loads(value) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 |
3593
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
161 class Xml(TypeDecorator): |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
162 impl = Text |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
163 cache_ok = True |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
164 |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
165 def process_bind_param(self, value, dialect): |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
166 if value is None: |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
167 return None |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
168 return value.toXml() |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
169 |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
170 def process_result_value(self, value, dialect): |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
171 if value is None: |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
172 return None |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
173 return generic.parseXml(value.encode()) |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
174 |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
175 |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 class JID(TypeDecorator): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 """Store twisted JID in text fields""" |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
178 |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 impl = Text |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 cache_ok = True |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 def process_bind_param(self, value, dialect): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 if value is None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 return None |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 return value.full() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 def process_result_value(self, value, dialect): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 if value is None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 return None |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 return jid.JID(value) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 class Profile(Base): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 __tablename__ = "profiles" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 |
3581
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
196 id = Column( |
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
197 Integer, |
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
198 primary_key=True, |
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
199 nullable=True, |
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
200 ) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 name = Column(Text, unique=True) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 params = relationship("ParamInd", back_populates="profile", passive_deletes=True) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 private_data = relationship( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 "PrivateInd", back_populates="profile", passive_deletes=True |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 private_bin_data = relationship( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 "PrivateIndBin", back_populates="profile", passive_deletes=True |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 class Component(Base): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 __tablename__ = "components" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 |
3581
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
215 profile_id = Column( |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
216 ForeignKey("profiles.id", ondelete="CASCADE"), nullable=True, primary_key=True |
3581
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
217 ) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 entry_point = Column(Text, nullable=False) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 profile = relationship("Profile") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 class History(Base): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 __tablename__ = "history" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 __table_args__ = ( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 UniqueConstraint("profile_id", "stanza_id", "source", "dest"), |
3796
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
226 UniqueConstraint("profile_id", "origin_id", "source", name="uq_origin_id"), |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 Index("history__profile_id_timestamp", "profile_id", "timestamp"), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 Index( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 "history__profile_id_received_timestamp", "profile_id", "received_timestamp" |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
230 ), |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 |
3583
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
233 uid = Column(Text, primary_key=True) |
4152
23d21daed216
core (memory/sqla_mapping): add a `version_id` column to detect race conditions.
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
234 # FIXME: version_id is only needed for changes in `extra` column. It would maybe be |
23d21daed216
core (memory/sqla_mapping): add a `version_id` column to detect race conditions.
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
235 # better to use separate table for `extra` data instead. |
23d21daed216
core (memory/sqla_mapping): add a `version_id` column to detect race conditions.
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
236 version_id = Column(Integer, nullable=False, server_default=text("1")) |
3796
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
237 origin_id = Column(Text) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 stanza_id = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 update_uid = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 profile_id = Column(ForeignKey("profiles.id", ondelete="CASCADE")) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 source = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 dest = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 source_res = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
244 dest_res = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 timestamp = Column(Float, nullable=False) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 received_timestamp = Column(Float) |
3583
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
247 type = Column( |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
248 Enum( |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
249 "chat", |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
250 "error", |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
251 "groupchat", |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
252 "headline", |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
253 "normal", |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
254 # info is not XMPP standard, but used to keep track of info like join/leave |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
255 # in a MUC |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
256 "info", |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
257 name="message_type", |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
258 create_constraint=True, |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
259 ), |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
260 nullable=False, |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
261 ) |
4212 | 262 extra = Column(JSON) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
263 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
264 profile = relationship("Profile") |
4161
2074b2bbe616
core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents:
4152
diff
changeset
|
265 messages = relationship( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
266 "Message", backref="history", cascade="all, delete-orphan", passive_deletes=True |
4161
2074b2bbe616
core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents:
4152
diff
changeset
|
267 ) |
2074b2bbe616
core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents:
4152
diff
changeset
|
268 subjects = relationship( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
269 "Subject", backref="history", cascade="all, delete-orphan", passive_deletes=True |
4161
2074b2bbe616
core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents:
4152
diff
changeset
|
270 ) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
271 thread = relationship( |
4161
2074b2bbe616
core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents:
4152
diff
changeset
|
272 "Thread", |
2074b2bbe616
core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents:
4152
diff
changeset
|
273 uselist=False, |
2074b2bbe616
core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents:
4152
diff
changeset
|
274 back_populates="history", |
2074b2bbe616
core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents:
4152
diff
changeset
|
275 cascade="all, delete-orphan", |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
276 passive_deletes=True, |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
277 ) |
4152
23d21daed216
core (memory/sqla_mapping): add a `version_id` column to detect race conditions.
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
278 __mapper_args__ = {"version_id_col": version_id} |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
279 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
280 def __init__(self, *args, **kwargs): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
281 source_jid = kwargs.pop("source_jid", None) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
282 if source_jid is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
283 kwargs["source"] = source_jid.userhost() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
284 kwargs["source_res"] = source_jid.resource |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
285 dest_jid = kwargs.pop("dest_jid", None) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
286 if dest_jid is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
287 kwargs["dest"] = dest_jid.userhost() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
288 kwargs["dest_res"] = dest_jid.resource |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
289 super().__init__(*args, **kwargs) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
290 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
291 @property |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
292 def source_jid(self) -> jid.JID: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
293 return jid.JID(f"{self.source}/{self.source_res or ''}") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
294 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
295 @source_jid.setter |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
296 def source_jid(self, source_jid: jid.JID) -> None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
297 self.source = source_jid.userhost |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
298 self.source_res = source_jid.resource |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
299 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
300 @property |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
301 def dest_jid(self): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
302 return jid.JID(f"{self.dest}/{self.dest_res or ''}") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
303 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
304 @dest_jid.setter |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
305 def dest_jid(self, dest_jid: jid.JID) -> None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
306 self.dest = dest_jid.userhost |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
307 self.dest_res = dest_jid.resource |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
308 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
309 def __repr__(self): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
310 dt = datetime.fromtimestamp(self.timestamp) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
311 return f"History<{self.source_jid.full()}->{self.dest_jid.full()} [{dt}]>" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
312 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
313 def serialise(self): |
4026
fe4725bf42fb
core (memory/sqla): be sure to have a dict when serialising History.extra:
Goffi <goffi@goffi.org>
parents:
3796
diff
changeset
|
314 extra = self.extra or {} |
3796
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
315 if self.origin_id is not None: |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
316 extra["origin_id"] = self.origin_id |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
317 if self.stanza_id is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
318 extra["stanza_id"] = self.stanza_id |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
319 if self.update_uid is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
320 extra["update_uid"] = self.update_uid |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
321 if self.received_timestamp is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
322 extra["received_timestamp"] = self.received_timestamp |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
323 if self.thread is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
324 extra["thread"] = self.thread.thread_id |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
325 if self.thread.parent_id is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
326 extra["thread_parent"] = self.thread.parent_id |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
327 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
328 return { |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
329 "from": ( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
330 f"{self.source}/{self.source_res}" if self.source_res else self.source |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
331 ), |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
332 "to": f"{self.dest}/{self.dest_res}" if self.dest_res else self.dest, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
333 "uid": self.uid, |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
334 "message": {m.language or "": m.message for m in self.messages}, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
335 "subject": {m.language or "": m.subject for m in self.subjects}, |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
336 "type": self.type, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
337 "extra": extra, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
338 "timestamp": self.timestamp, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
339 } |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
340 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
341 def as_tuple(self): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
342 d = self.serialise() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
343 return ( |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
344 d["uid"], |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
345 d["timestamp"], |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
346 d["from"], |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
347 d["to"], |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
348 d["message"], |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
349 d["subject"], |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
350 d["type"], |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
351 d["extra"], |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
352 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
353 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
354 @staticmethod |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
355 def debug_collection(history_collection): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
356 for idx, history in enumerate(history_collection): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
357 history.debug_msg(idx) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
358 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
359 def debug_msg(self, idx=None): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
360 """Print messages""" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
361 dt = datetime.fromtimestamp(self.timestamp) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
362 if idx is not None: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
363 dt = f"({idx}) {dt}" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
364 parts = [] |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
365 parts.append(f"[{dt}]<{self.source_jid.full()}->{self.dest_jid.full()}> ") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
366 for message in self.messages: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
367 if message.language: |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
368 parts.append(f"[{message.language}] ") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
369 parts.append(f"{message.message}\n") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
370 print("".join(parts)) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
371 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
372 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
373 class Message(Base): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
374 __tablename__ = "message" |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
375 __table_args__ = (Index("message__history_uid", "history_uid"),) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
376 |
3581
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
377 id = Column( |
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
378 Integer, |
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
379 primary_key=True, |
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
380 ) |
3796
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
381 history_uid = Column(ForeignKey("history.uid", ondelete="CASCADE"), nullable=False) |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
382 message = Column(Text, nullable=False) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
383 language = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
384 |
3796
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
385 def serialise(self) -> Dict[str, Any]: |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
386 s = {} |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
387 if self.message: |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
388 s["message"] = str(self.message) |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
389 if self.language: |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
390 s["language"] = str(self.language) |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
391 return s |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
392 |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
393 def __repr__(self): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
394 lang_str = f"[{self.language}]" if self.language else "" |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
395 msg = f"{self.message[:20]}…" if len(self.message) > 20 else self.message |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
396 content = f"{lang_str}{msg}" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
397 return f"Message<{content}>" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
398 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
399 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
400 class Subject(Base): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
401 __tablename__ = "subject" |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
402 __table_args__ = (Index("subject__history_uid", "history_uid"),) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
403 |
3581
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
404 id = Column( |
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
405 Integer, |
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
406 primary_key=True, |
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
407 ) |
3796
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
408 history_uid = Column(ForeignKey("history.uid", ondelete="CASCADE"), nullable=False) |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
409 subject = Column(Text, nullable=False) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
410 language = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
411 |
3796
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
412 def serialise(self) -> Dict[str, Any]: |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
413 s = {} |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
414 if self.subject: |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
415 s["subject"] = str(self.subject) |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
416 if self.language: |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
417 s["language"] = str(self.language) |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
418 return s |
24c1c06c865b
core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents:
3744
diff
changeset
|
419 |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
420 def __repr__(self): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
421 lang_str = f"[{self.language}]" if self.language else "" |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
422 msg = f"{self.subject[:20]}…" if len(self.subject) > 20 else self.subject |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
423 content = f"{lang_str}{msg}" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
424 return f"Subject<{content}>" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
425 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
426 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
427 class Thread(Base): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
428 __tablename__ = "thread" |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
429 __table_args__ = (Index("thread__history_uid", "history_uid"),) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
430 |
3581
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
431 id = Column( |
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
432 Integer, |
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
433 primary_key=True, |
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
434 ) |
84ea57a8d6b3
memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
435 history_uid = Column(ForeignKey("history.uid", ondelete="CASCADE")) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
436 thread_id = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
437 parent_id = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
438 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
439 history = relationship("History", uselist=False, back_populates="thread") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
440 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
441 def __repr__(self): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
442 return f"Thread<{self.thread_id} [parent: {self.parent_id}]>" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
443 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
444 |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
445 class Notification(Base): |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
446 __tablename__ = "notifications" |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
447 __table_args__ = (Index("notifications_profile_id_status", "profile_id", "status"),) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
448 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
449 id = Column(Integer, primary_key=True, autoincrement=True) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
450 timestamp = Column(Float, nullable=False, default=time.time) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
451 expire_at = Column(Float, nullable=True) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
452 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
453 profile_id = Column( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
454 ForeignKey("profiles.id", ondelete="CASCADE"), index=True, nullable=True |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
455 ) |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
456 profile = relationship("Profile") |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
457 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
458 type = Column(Enum(NotificationType), nullable=False) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
459 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
460 title = Column(Text, nullable=True) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
461 body_plain = Column(Text, nullable=False) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
462 body_rich = Column(Text, nullable=True) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
463 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
464 requires_action = Column(Boolean, default=False) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
465 priority = Column(Integer, default=NotificationPriority.MEDIUM.value) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
466 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
467 extra_data = Column(JSON) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
468 status = Column(Enum(NotificationStatus), default=NotificationStatus.new) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
469 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
470 def serialise(self) -> dict[str, str | float | bool | int | dict]: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
471 """ |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
472 Serialises the Notification instance to a dictionary. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
473 """ |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
474 result = {} |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
475 for column in self.__table__.columns: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
476 value = getattr(self, column.name) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
477 if value is not None: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
478 if column.name in ("type", "status"): |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
479 result[column.name] = value.name |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
480 elif column.name == "id": |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
481 result[column.name] = str(value) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
482 elif column.name == "profile_id": |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
483 if value is None: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
484 result["profile"] = C.PROF_KEY_ALL |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
485 else: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
486 result["profile"] = get_profile_by_id(value) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
487 else: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
488 result[column.name] = value |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
489 return result |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
490 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
491 |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
492 class ParamGen(Base): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
493 __tablename__ = "param_gen" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
494 |
3583
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
495 category = Column(Text, primary_key=True) |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
496 name = Column(Text, primary_key=True) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
497 value = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
498 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
499 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
500 class ParamInd(Base): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
501 __tablename__ = "param_ind" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
502 |
3583
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
503 category = Column(Text, primary_key=True) |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
504 name = Column(Text, primary_key=True) |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
505 profile_id = Column(ForeignKey("profiles.id", ondelete="CASCADE"), primary_key=True) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
506 value = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
507 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
508 profile = relationship("Profile", back_populates="params") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
509 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
510 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
511 class PrivateGen(Base): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
512 __tablename__ = "private_gen" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
513 |
3583
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
514 namespace = Column(Text, primary_key=True) |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
515 key = Column(Text, primary_key=True) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
516 value = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
517 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
518 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
519 class PrivateInd(Base): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
520 __tablename__ = "private_ind" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
521 |
3583
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
522 namespace = Column(Text, primary_key=True) |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
523 key = Column(Text, primary_key=True) |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
524 profile_id = Column(ForeignKey("profiles.id", ondelete="CASCADE"), primary_key=True) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
525 value = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
526 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
527 profile = relationship("Profile", back_populates="private_data") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
528 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
529 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
530 class PrivateGenBin(Base): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
531 __tablename__ = "private_gen_bin" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
532 |
3583
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
533 namespace = Column(Text, primary_key=True) |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
534 key = Column(Text, primary_key=True) |
4212 | 535 value = Column(JSON) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
536 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
537 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
538 class PrivateIndBin(Base): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
539 __tablename__ = "private_ind_bin" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
540 |
3583
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
541 namespace = Column(Text, primary_key=True) |
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
542 key = Column(Text, primary_key=True) |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
543 profile_id = Column(ForeignKey("profiles.id", ondelete="CASCADE"), primary_key=True) |
4212 | 544 value = Column(JSON) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
545 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
546 profile = relationship("Profile", back_populates="private_bin_data") |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
547 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
548 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
549 class File(Base): |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
550 __tablename__ = "files" |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
551 __table_args__ = ( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
552 Index("files__profile_id_owner_parent", "profile_id", "owner", "parent"), |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
553 Index( |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
554 "files__profile_id_owner_media_type_media_subtype", |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
555 "profile_id", |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
556 "owner", |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
557 "media_type", |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
558 "media_subtype", |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
559 ), |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
560 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
561 |
3583
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
562 id = Column(Text, primary_key=True) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
563 public_id = Column(Text, unique=True) |
3583
16ade4ad63f3
core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents:
3581
diff
changeset
|
564 version = Column(Text, primary_key=True) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
565 parent = Column(Text, nullable=False) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
566 type = Column( |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
567 Enum("file", "directory", name="file_type", create_constraint=True), |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
568 nullable=False, |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
569 server_default="file", |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
570 ) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
571 file_hash = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
572 hash_algo = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
573 name = Column(Text, nullable=False) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
574 size = Column(Integer) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
575 namespace = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
576 media_type = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
577 media_subtype = Column(Text) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
578 created = Column(Float, nullable=False) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
579 modified = Column(Float) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
580 owner = Column(JID) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
581 access = Column(JsonDefaultDict) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
582 extra = Column(JsonDefaultDict) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
583 profile_id = Column(ForeignKey("profiles.id", ondelete="CASCADE")) |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
584 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
585 profile = relationship("Profile") |
3593
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
586 |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
587 |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
588 class PubsubNode(Base): |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
589 __tablename__ = "pubsub_nodes" |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
590 __table_args__ = (UniqueConstraint("profile_id", "service", "name"),) |
3593
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
591 |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
592 id = Column(Integer, primary_key=True) |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
593 profile_id = Column(ForeignKey("profiles.id", ondelete="CASCADE")) |
3593
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
594 service = Column(JID) |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
595 name = Column(Text, nullable=False) |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
596 subscribed = Column( |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
597 Boolean(create_constraint=True, name="subscribed_bool"), nullable=False |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
598 ) |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
599 analyser = Column(Text) |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
600 sync_state = Column( |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
601 Enum( |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
602 SyncState, |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
603 name="sync_state", |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
604 create_constraint=True, |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
605 ), |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
606 nullable=True, |
3593
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
607 ) |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
608 sync_state_updated = Column(Float, nullable=False, default=time.time()) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
609 type_ = Column(Text, name="type", nullable=True) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
610 subtype = Column(Text, nullable=True) |
3593
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
611 extra = Column(JSON) |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
612 |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
613 items = relationship("PubsubItem", back_populates="node", passive_deletes=True) |
3744
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
614 subscriptions = relationship("PubsubSub", back_populates="node", passive_deletes=True) |
3593
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
615 |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
616 def __str__(self): |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
617 return f"Pubsub node {self.name!r} at {self.service}" |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
618 |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
619 |
3744
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
620 class PubsubSub(Base): |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
621 """Subscriptions to pubsub nodes |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
622 |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
623 Used by components managing a pubsub service |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
624 """ |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
625 |
3744
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
626 __tablename__ = "pubsub_subs" |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
627 __table_args__ = (UniqueConstraint("node_id", "subscriber"),) |
3744
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
628 |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
629 id = Column(Integer, primary_key=True) |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
630 node_id = Column(ForeignKey("pubsub_nodes.id", ondelete="CASCADE"), nullable=False) |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
631 subscriber = Column(JID) |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
632 state = Column( |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
633 Enum( |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
634 SubscriptionState, |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
635 name="state", |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
636 create_constraint=True, |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
637 ), |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
638 nullable=True, |
3744
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
639 ) |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
640 |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
641 node = relationship("PubsubNode", back_populates="subscriptions") |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
642 |
658ddbabaf36
core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents:
3663
diff
changeset
|
643 |
3593
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
644 class PubsubItem(Base): |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
645 __tablename__ = "pubsub_items" |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
646 __table_args__ = (UniqueConstraint("node_id", "name"),) |
3593
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
647 id = Column(Integer, primary_key=True) |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
648 node_id = Column(ForeignKey("pubsub_nodes.id", ondelete="CASCADE"), nullable=False) |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
649 name = Column(Text, nullable=False) |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
650 data = Column(Xml, nullable=False) |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
651 created = Column(DateTime, nullable=False, server_default=now()) |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
652 updated = Column(DateTime, nullable=False, server_default=now(), onupdate=now()) |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
653 parsed = Column(JSON) |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
654 |
cb8d0e8b917f
core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents:
3583
diff
changeset
|
655 node = relationship("PubsubNode", back_populates="items") |
3663
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
656 |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
657 |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
658 ## Full-Text Search |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
659 |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
660 # create |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
661 |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
662 |
3663
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
663 @event.listens_for(PubsubItem.__table__, "after_create") |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
664 def fts_create(target, connection, **kw): |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
665 """Full-Text Search table creation""" |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
666 if connection.engine.name == "sqlite": |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
667 # Using SQLite FTS5 |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
668 queries = [ |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
669 "CREATE VIRTUAL TABLE pubsub_items_fts " |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
670 "USING fts5(data, content=pubsub_items, content_rowid=id)", |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
671 "CREATE TRIGGER pubsub_items_fts_sync_ins AFTER INSERT ON pubsub_items BEGIN" |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
672 " INSERT INTO pubsub_items_fts(rowid, data) VALUES (new.id, new.data);" |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
673 "END", |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
674 "CREATE TRIGGER pubsub_items_fts_sync_del AFTER DELETE ON pubsub_items BEGIN" |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
675 " INSERT INTO pubsub_items_fts(pubsub_items_fts, rowid, data) " |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
676 "VALUES('delete', old.id, old.data);" |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
677 "END", |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
678 "CREATE TRIGGER pubsub_items_fts_sync_upd AFTER UPDATE ON pubsub_items BEGIN" |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
679 " INSERT INTO pubsub_items_fts(pubsub_items_fts, rowid, data) VALUES" |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
680 "('delete', old.id, old.data);" |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
681 " INSERT INTO pubsub_items_fts(rowid, data) VALUES(new.id, new.data);" |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
682 "END", |
3663
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
683 ] |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
684 for q in queries: |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
685 connection.execute(DDL(q)) |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
686 |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
687 |
3663
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
688 # drop |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
689 |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4095
diff
changeset
|
690 |
3663
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
691 @event.listens_for(PubsubItem.__table__, "before_drop") |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
692 def fts_drop(target, connection, **kw): |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
693 "Full-Text Search table drop" "" |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
694 if connection.engine.name == "sqlite": |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
695 # Using SQLite FTS5 |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
696 queries = [ |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
697 "DROP TRIGGER IF EXISTS pubsub_items_fts_sync_ins", |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
698 "DROP TRIGGER IF EXISTS pubsub_items_fts_sync_del", |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
699 "DROP TRIGGER IF EXISTS pubsub_items_fts_sync_upd", |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
700 "DROP TABLE IF EXISTS pubsub_items_fts", |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
701 ] |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
702 for q in queries: |
162866ca4be7
db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents:
3639
diff
changeset
|
703 connection.execute(DDL(q)) |