annotate sat_pubsub/pgsql_storage.py @ 489:fa0d2a4783fa default tip

install: use PEP-440 compatible version in `setup`: setuptools from version v66 doesn't accept anymore non PEP-440 compatible version. This is a Q&D fix, move to `pyproject.toml` with proper versioning as for Libervia backend should be done sooner than later.
author Goffi <goffi@goffi.org>
date Fri, 12 Jan 2024 23:46:24 +0100
parents 39a6c0bc914b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
1 #!/usr/bin/env python3
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
2
440
074037832daf dates update
Goffi <goffi@goffi.org>
parents: 438
diff changeset
3 # Copyright (c) 2012-2021 Jérôme Poisson
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
4 # Copyright (c) 2013-2016 Adrien Cossa
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
5 # Copyright (c) 2003-2011 Ralph Meijer
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
6
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
7
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
8 # This program is free software: you can redistribute it and/or modify
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
9 # it under the terms of the GNU Affero General Public License as published by
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
10 # the Free Software Foundation, either version 3 of the License, or
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
11 # (at your option) any later version.
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
12
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
13 # This program is distributed in the hope that it will be useful,
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
16 # GNU Affero General Public License for more details.
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
17
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
18 # You should have received a copy of the GNU Affero General Public License
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
20 # --
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
21
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
22 # This program is based on Idavoll (http://idavoll.ik.nu/),
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
23 # originaly written by Ralph Meijer (http://ralphm.net/blog/)
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
24 # It is sublicensed under AGPL v3 (or any later version) as allowed by the original
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
25 # license.
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
26
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
27 # --
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
28
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
29 # Here is a copy of the original license:
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
30
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
31 # Copyright (c) 2003-2011 Ralph Meijer
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
32
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
33 # Permission is hereby granted, free of charge, to any person obtaining
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
34 # a copy of this software and associated documentation files (the
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
35 # "Software"), to deal in the Software without restriction, including
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
36 # without limitation the rights to use, copy, modify, merge, publish,
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
37 # distribute, sublicense, and/or sell copies of the Software, and to
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
38 # permit persons to whom the Software is furnished to do so, subject to
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
39 # the following conditions:
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
40
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
41 # The above copyright notice and this permission notice shall be
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
42 # included in all copies or substantial portions of the Software.
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
43
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
44 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
45 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
46 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
47 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
48 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
49 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
50 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
51
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
52 from typing import Optional, List
262
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
53 import copy, logging
467
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
54 from datetime import datetime, timezone
202
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
55
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
56 from zope.interface import implementer
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
57
344
8cf1be9572f8 pgsql: check schema version and exit with a message asking to upgrade if it's not the current one
Goffi <goffi@goffi.org>
parents: 341
diff changeset
58 from twisted.internet import reactor
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
59 from twisted.internet import defer
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
60 from twisted.words.protocols.jabber import jid
467
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
61 from twisted.words.xish import domish
323
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
62 from twisted.python import log
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
63
271
232002e132db pgsql_backend unicode fix: parseXml was expecting str and getting unicode
Goffi <goffi@goffi.org>
parents: 270
diff changeset
64 from wokkel import generic
429
0526073ff2ab pgsql: replaced ValueError by BadRequest:
Goffi <goffi@goffi.org>
parents: 424
diff changeset
65 from wokkel.pubsub import Subscription, BadRequest
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
66
317
34adc4a8aa64 new container module, with an ItemData container:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
67 from sat_pubsub import error
34adc4a8aa64 new container module, with an ItemData container:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
68 from sat_pubsub import iidavoll
34adc4a8aa64 new container module, with an ItemData container:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
69 from sat_pubsub import const
34adc4a8aa64 new container module, with an ItemData container:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
70 from sat_pubsub import container
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
71 from sat_pubsub import exceptions
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
72 import uuid
262
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
73 import psycopg2
270
f17034e4cf4a fixed unicode handling with psycopg2
Goffi <goffi@goffi.org>
parents: 265
diff changeset
74 import psycopg2.extensions
271
232002e132db pgsql_backend unicode fix: parseXml was expecting str and getting unicode
Goffi <goffi@goffi.org>
parents: 270
diff changeset
75 # we wants psycopg2 to return us unicode, not str
270
f17034e4cf4a fixed unicode handling with psycopg2
Goffi <goffi@goffi.org>
parents: 265
diff changeset
76 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
f17034e4cf4a fixed unicode handling with psycopg2
Goffi <goffi@goffi.org>
parents: 265
diff changeset
77 psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
244
3ecc94407e36 item access_model (not finished)
Goffi <goffi@goffi.org>
parents: 243
diff changeset
78
271
232002e132db pgsql_backend unicode fix: parseXml was expecting str and getting unicode
Goffi <goffi@goffi.org>
parents: 270
diff changeset
79 # parseXml manage str, but we get unicode
232002e132db pgsql_backend unicode fix: parseXml was expecting str and getting unicode
Goffi <goffi@goffi.org>
parents: 270
diff changeset
80 parseXml = lambda unicode_data: generic.parseXml(unicode_data.encode('utf-8'))
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
81 ITEMS_SEQ_NAME = 'node_{node_id}_seq'
330
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
82 PEP_COL_NAME = 'pep'
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
83 CURRENT_VERSION = '10'
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
84 # retrieve the maximum integer item id + 1
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
85 NEXT_ITEM_ID_QUERY = r"SELECT COALESCE(max(item::integer)+1,1) as val from items where node_id={node_id} and item ~ E'^\\d+$'"
271
232002e132db pgsql_backend unicode fix: parseXml was expecting str and getting unicode
Goffi <goffi@goffi.org>
parents: 270
diff changeset
86
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
87
330
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
88 def withPEP(query, values, pep, recipient):
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
89 """Helper method to facilitate PEP management
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
90
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
91 @param query: SQL query basis
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
92 @param values: current values to replace in query
364
8bd8be6815ab completed docstrings + use short notation for sets
Arnaud Joset <info@agayon.be>
parents: 357
diff changeset
93 @param pep(bool): True if we are in PEP mode
8bd8be6815ab completed docstrings + use short notation for sets
Arnaud Joset <info@agayon.be>
parents: 357
diff changeset
94 @param recipient(jid.JID): jid of the recipient
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
95 @return: query + PEP AND check,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
96 recipient's bare jid is added to value if needed
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
97 """
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
98 if pep:
330
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
99 pep_check="AND {}=%s".format(PEP_COL_NAME)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
100 values=list(values) + [recipient.userhost()]
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
101 else:
330
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
102 pep_check="AND {} IS NULL".format(PEP_COL_NAME)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
103 return "{} {}".format(query, pep_check), values
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
104
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
105
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
106 @implementer(iidavoll.IStorage)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
107 class Storage:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
108
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
109 fts_languages = ['generic']
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
110 defaultConfig = {
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
111 'leaf': {
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
112 const.OPT_PERSIST_ITEMS: True,
422
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
113 const.OPT_MAX_ITEMS: 'max',
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
114 const.OPT_DELIVER_PAYLOADS: True,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
115 const.OPT_SEND_LAST_PUBLISHED_ITEM: 'on_sub',
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
116 const.OPT_ACCESS_MODEL: const.VAL_AMODEL_DEFAULT,
260
f0cd02c032b3 publish model management
Goffi <goffi@goffi.org>
parents: 259
diff changeset
117 const.OPT_PUBLISH_MODEL: const.VAL_PMODEL_DEFAULT,
438
b5e1e8d93dd4 backend, pgsql: new `overwrite_policy` node setting:
Goffi <goffi@goffi.org>
parents: 434
diff changeset
118 const.OPT_OVERWRITE_POLICY: const.VAL_OWPOL_DEFAUT,
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
119 const.OPT_SERIAL_IDS: False,
403
1dc606612405 implemented experimental "consistent_publisher" option:
Goffi <goffi@goffi.org>
parents: 402
diff changeset
120 const.OPT_CONSISTENT_PUBLISHER: False,
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
121 const.OPT_FTS_LANGUAGE: const.VAL_FTS_GENERIC,
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
122 },
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
123 'collection': {
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
124 const.OPT_DELIVER_PAYLOADS: True,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
125 const.OPT_SEND_LAST_PUBLISHED_ITEM: 'on_sub',
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
126 const.OPT_ACCESS_MODEL: const.VAL_AMODEL_DEFAULT,
260
f0cd02c032b3 publish model management
Goffi <goffi@goffi.org>
parents: 259
diff changeset
127 const.OPT_PUBLISH_MODEL: const.VAL_PMODEL_DEFAULT,
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
128 }
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
129 }
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
130
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
131 def __init__(self, dbpool):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
132 self.dbpool = dbpool
344
8cf1be9572f8 pgsql: check schema version and exit with a message asking to upgrade if it's not the current one
Goffi <goffi@goffi.org>
parents: 341
diff changeset
133 d = self.dbpool.runQuery("SELECT value FROM metadata WHERE key='version'")
8cf1be9572f8 pgsql: check schema version and exit with a message asking to upgrade if it's not the current one
Goffi <goffi@goffi.org>
parents: 341
diff changeset
134 d.addCallbacks(self._checkVersion, self._versionEb)
8cf1be9572f8 pgsql: check schema version and exit with a message asking to upgrade if it's not the current one
Goffi <goffi@goffi.org>
parents: 341
diff changeset
135
8cf1be9572f8 pgsql: check schema version and exit with a message asking to upgrade if it's not the current one
Goffi <goffi@goffi.org>
parents: 341
diff changeset
136 def _checkVersion(self, row):
8cf1be9572f8 pgsql: check schema version and exit with a message asking to upgrade if it's not the current one
Goffi <goffi@goffi.org>
parents: 341
diff changeset
137 version = row[0].value
8cf1be9572f8 pgsql: check schema version and exit with a message asking to upgrade if it's not the current one
Goffi <goffi@goffi.org>
parents: 341
diff changeset
138 if version != CURRENT_VERSION:
8cf1be9572f8 pgsql: check schema version and exit with a message asking to upgrade if it's not the current one
Goffi <goffi@goffi.org>
parents: 341
diff changeset
139 logging.error("Bad database schema version ({current}), please upgrade to {needed}".format(
8cf1be9572f8 pgsql: check schema version and exit with a message asking to upgrade if it's not the current one
Goffi <goffi@goffi.org>
parents: 341
diff changeset
140 current=version, needed=CURRENT_VERSION))
8cf1be9572f8 pgsql: check schema version and exit with a message asking to upgrade if it's not the current one
Goffi <goffi@goffi.org>
parents: 341
diff changeset
141 reactor.stop()
8cf1be9572f8 pgsql: check schema version and exit with a message asking to upgrade if it's not the current one
Goffi <goffi@goffi.org>
parents: 341
diff changeset
142
8cf1be9572f8 pgsql: check schema version and exit with a message asking to upgrade if it's not the current one
Goffi <goffi@goffi.org>
parents: 341
diff changeset
143 def _versionEb(self, failure):
8cf1be9572f8 pgsql: check schema version and exit with a message asking to upgrade if it's not the current one
Goffi <goffi@goffi.org>
parents: 341
diff changeset
144 logging.error("Can't check schema version: {reason}".format(reason=failure))
8cf1be9572f8 pgsql: check schema version and exit with a message asking to upgrade if it's not the current one
Goffi <goffi@goffi.org>
parents: 341
diff changeset
145 reactor.stop()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
146
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
147 def _buildNode(self, row):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
148 """Build a note class from database result row"""
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
149 if not row:
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
150 raise error.NodeNotFound()
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
151
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
152 if row[2] == 'leaf':
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
153 configuration = {
422
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
154 const.OPT_PERSIST_ITEMS: row[3],
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
155 const.OPT_MAX_ITEMS: 'max' if row[4] == 0 else str(row[4]),
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
156 const.OPT_DELIVER_PAYLOADS: row[5],
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
157 const.OPT_SEND_LAST_PUBLISHED_ITEM: row[6],
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
158 const.OPT_ACCESS_MODEL:row[7],
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
159 const.OPT_PUBLISH_MODEL:row[8],
438
b5e1e8d93dd4 backend, pgsql: new `overwrite_policy` node setting:
Goffi <goffi@goffi.org>
parents: 434
diff changeset
160 const.OPT_OVERWRITE_POLICY: row[9],
b5e1e8d93dd4 backend, pgsql: new `overwrite_policy` node setting:
Goffi <goffi@goffi.org>
parents: 434
diff changeset
161 const.OPT_SERIAL_IDS:row[10],
b5e1e8d93dd4 backend, pgsql: new `overwrite_policy` node setting:
Goffi <goffi@goffi.org>
parents: 434
diff changeset
162 const.OPT_CONSISTENT_PUBLISHER:row[11],
b5e1e8d93dd4 backend, pgsql: new `overwrite_policy` node setting:
Goffi <goffi@goffi.org>
parents: 434
diff changeset
163 const.OPT_FTS_LANGUAGE: row[12],
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
164 }
438
b5e1e8d93dd4 backend, pgsql: new `overwrite_policy` node setting:
Goffi <goffi@goffi.org>
parents: 434
diff changeset
165 schema = row[13]
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
166 if schema is not None:
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
167 schema = parseXml(schema)
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
168 node = LeafNode(row[0], row[1], configuration, schema)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
169 node.dbpool = self.dbpool
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
170 return node
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
171 elif row[2] == 'collection':
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
172 configuration = {
422
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
173 const.OPT_DELIVER_PAYLOADS: row[5],
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
174 const.OPT_SEND_LAST_PUBLISHED_ITEM: row[6],
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
175 const.OPT_ACCESS_MODEL: row[7],
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
176 const.OPT_PUBLISH_MODEL:row[8],
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
177 }
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
178 node = CollectionNode(row[0], row[1], configuration, None)
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
179 node.dbpool = self.dbpool
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
180 return node
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
181 else:
429
0526073ff2ab pgsql: replaced ValueError by BadRequest:
Goffi <goffi@goffi.org>
parents: 424
diff changeset
182 raise BadRequest(text="Unknown node type !")
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
183
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
184 def getFTSLanguages(self):
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
185 """Get list of available languages for full text search"""
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
186 return self.dbpool.runInteraction(self._getFTSLanguages)
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
187
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
188 def _getFTSLanguages(self, cursor):
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
189 cursor.execute("SELECT cfgname FROM pg_ts_config")
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
190 result = [r.cfgname for r in cursor.fetchall()]
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
191 result.remove("simple")
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
192 result.insert(0, "generic")
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
193 Node.fts_languages = self.fts_languages = result
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
194 return result
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
195
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
196 def getNodeById(self, nodeDbId):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
197 """Get node using database ID insted of pubsub identifier
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
198
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
199 @param nodeDbId(unicode): database ID
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
200 """
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
201 return self.dbpool.runInteraction(self._getNodeById, nodeDbId)
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
202
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
203 def _getNodeById(self, cursor, nodeDbId):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
204 cursor.execute("""SELECT node_id,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
205 node,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
206 node_type,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
207 persist_items,
422
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
208 max_items,
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
209 deliver_payloads,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
210 send_last_published_item,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
211 access_model,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
212 publish_model,
438
b5e1e8d93dd4 backend, pgsql: new `overwrite_policy` node setting:
Goffi <goffi@goffi.org>
parents: 434
diff changeset
213 overwrite_policy,
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
214 serial_ids,
403
1dc606612405 implemented experimental "consistent_publisher" option:
Goffi <goffi@goffi.org>
parents: 402
diff changeset
215 consistent_publisher,
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
216 fts_language,
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
217 schema::text,
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
218 pep
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
219 FROM nodes
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
220 WHERE node_id=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
221 (nodeDbId,))
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
222 row = cursor.fetchone()
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
223 return self._buildNode(row)
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
224
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
225 def getNode(self, nodeIdentifier, pep, recipient=None):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
226 return self.dbpool.runInteraction(self._getNode, nodeIdentifier, pep, recipient)
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
227
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
228 def _getNode(self, cursor, nodeIdentifier, pep, recipient):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
229 cursor.execute(*withPEP("""SELECT node_id,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
230 node,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
231 node_type,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
232 persist_items,
422
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
233 max_items,
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
234 deliver_payloads,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
235 send_last_published_item,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
236 access_model,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
237 publish_model,
438
b5e1e8d93dd4 backend, pgsql: new `overwrite_policy` node setting:
Goffi <goffi@goffi.org>
parents: 434
diff changeset
238 overwrite_policy,
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
239 serial_ids,
403
1dc606612405 implemented experimental "consistent_publisher" option:
Goffi <goffi@goffi.org>
parents: 402
diff changeset
240 consistent_publisher,
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
241 fts_language,
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
242 schema::text,
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
243 pep
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
244 FROM nodes
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
245 WHERE node=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
246 (nodeIdentifier,), pep, recipient))
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
247 row = cursor.fetchone()
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
248 return self._buildNode(row)
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
249
349
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
250 def getNodeIds(self, pep, recipient, allowed_accesses=None):
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
251 """retrieve ids of existing nodes
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
252
364
8bd8be6815ab completed docstrings + use short notation for sets
Arnaud Joset <info@agayon.be>
parents: 357
diff changeset
253 @param pep(bool): True if it's a PEP request
8bd8be6815ab completed docstrings + use short notation for sets
Arnaud Joset <info@agayon.be>
parents: 357
diff changeset
254 @param recipient(jid.JID, None): recipient of the PEP request
349
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
255 @param allowed_accesses(None, set): only nodes with access
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
256 in this set will be returned
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
257 None to return all nodes
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
258 @return (list[unicode]): ids of nodes
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
259 """
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
260 if not pep:
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
261 query = "SELECT node from nodes WHERE pep is NULL"
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
262 values = []
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
263 else:
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
264 query = "SELECT node from nodes WHERE pep=%s"
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
265 values = [recipient.userhost()]
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
266
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
267 if allowed_accesses is not None:
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
268 query += "AND access_model IN %s"
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
269 values.append(tuple(allowed_accesses))
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
270
20b82fb8de02 backend: check nodes/items permission on disco#items:
Goffi <goffi@goffi.org>
parents: 347
diff changeset
271 d = self.dbpool.runQuery(query, values)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
272 d.addCallback(lambda results: [r[0] for r in results])
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
273 return d
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
274
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
275 def createNode(self, nodeIdentifier, owner, config, schema, pep, recipient=None):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
276 return self.dbpool.runInteraction(self._createNode, nodeIdentifier,
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
277 owner, config, schema, pep, recipient)
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
278
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
279 def _createNode(self, cursor, nodeIdentifier, owner, config, schema, pep, recipient):
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
280 if config['pubsub#node_type'] != 'leaf':
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
281 raise error.NoCollections()
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
282
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
283 owner = owner.userhost()
486
39a6c0bc914b psql: handle case when `max_items == max` on node creation
Goffi <goffi@goffi.org>
parents: 485
diff changeset
284 max_items = config[const.OPT_MAX_ITEMS]
39a6c0bc914b psql: handle case when `max_items == max` on node creation
Goffi <goffi@goffi.org>
parents: 485
diff changeset
285 max_items = 0 if max_items == "max" else int(max_items)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
286
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
287 try:
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
288 cursor.execute("""INSERT INTO nodes
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
289 (node,
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
290 node_type,
483
e814c98ef07a psql: fix `max_items` setting when creating a node
Goffi <goffi@goffi.org>
parents: 478
diff changeset
291 access_model,
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
292 persist_items,
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
293 deliver_payloads,
483
e814c98ef07a psql: fix `max_items` setting when creating a node
Goffi <goffi@goffi.org>
parents: 478
diff changeset
294 max_items,
438
b5e1e8d93dd4 backend, pgsql: new `overwrite_policy` node setting:
Goffi <goffi@goffi.org>
parents: 434
diff changeset
295 overwrite_policy,
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
296 serial_ids,
403
1dc606612405 implemented experimental "consistent_publisher" option:
Goffi <goffi@goffi.org>
parents: 402
diff changeset
297 consistent_publisher,
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
298 fts_language,
483
e814c98ef07a psql: fix `max_items` setting when creating a node
Goffi <goffi@goffi.org>
parents: 478
diff changeset
299 send_last_published_item,
e814c98ef07a psql: fix `max_items` setting when creating a node
Goffi <goffi@goffi.org>
parents: 478
diff changeset
300 publish_model,
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
301 schema,
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
302 pep)
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
303 VALUES
483
e814c98ef07a psql: fix `max_items` setting when creating a node
Goffi <goffi@goffi.org>
parents: 478
diff changeset
304 (%s, 'leaf', %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
305 (nodeIdentifier,
483
e814c98ef07a psql: fix `max_items` setting when creating a node
Goffi <goffi@goffi.org>
parents: 478
diff changeset
306 config[const.OPT_ACCESS_MODEL],
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
307 config['pubsub#persist_items'],
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
308 config['pubsub#deliver_payloads'],
486
39a6c0bc914b psql: handle case when `max_items == max` on node creation
Goffi <goffi@goffi.org>
parents: 485
diff changeset
309 max_items,
438
b5e1e8d93dd4 backend, pgsql: new `overwrite_policy` node setting:
Goffi <goffi@goffi.org>
parents: 434
diff changeset
310 config[const.OPT_OVERWRITE_POLICY],
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
311 config[const.OPT_SERIAL_IDS],
403
1dc606612405 implemented experimental "consistent_publisher" option:
Goffi <goffi@goffi.org>
parents: 402
diff changeset
312 config[const.OPT_CONSISTENT_PUBLISHER],
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
313 config[const.OPT_FTS_LANGUAGE],
483
e814c98ef07a psql: fix `max_items` setting when creating a node
Goffi <goffi@goffi.org>
parents: 478
diff changeset
314 config['pubsub#send_last_published_item'],
e814c98ef07a psql: fix `max_items` setting when creating a node
Goffi <goffi@goffi.org>
parents: 478
diff changeset
315 config[const.OPT_PUBLISH_MODEL],
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
316 schema,
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
317 recipient.userhost() if pep else None
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
318 )
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
319 )
332
31cbd8b9fa7f pgsql: node creation now return error.NodeExists in case of unique violation, and InvalidConfigurationOption else
Goffi <goffi@goffi.org>
parents: 331
diff changeset
320 except cursor._pool.dbapi.IntegrityError as e:
31cbd8b9fa7f pgsql: node creation now return error.NodeExists in case of unique violation, and InvalidConfigurationOption else
Goffi <goffi@goffi.org>
parents: 331
diff changeset
321 if e.pgcode == "23505":
31cbd8b9fa7f pgsql: node creation now return error.NodeExists in case of unique violation, and InvalidConfigurationOption else
Goffi <goffi@goffi.org>
parents: 331
diff changeset
322 # unique_violation
31cbd8b9fa7f pgsql: node creation now return error.NodeExists in case of unique violation, and InvalidConfigurationOption else
Goffi <goffi@goffi.org>
parents: 331
diff changeset
323 raise error.NodeExists()
31cbd8b9fa7f pgsql: node creation now return error.NodeExists in case of unique violation, and InvalidConfigurationOption else
Goffi <goffi@goffi.org>
parents: 331
diff changeset
324 else:
31cbd8b9fa7f pgsql: node creation now return error.NodeExists in case of unique violation, and InvalidConfigurationOption else
Goffi <goffi@goffi.org>
parents: 331
diff changeset
325 raise error.InvalidConfigurationOption()
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
326
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
327 cursor.execute(*withPEP("""SELECT node_id FROM nodes WHERE node=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
328 (nodeIdentifier,), pep, recipient));
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
329 node_id = cursor.fetchone()[0]
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
330
276
b757c29b20d7 import changes from idavoll changeset 233 (24be3a11ddbc), by Ralph Meijer and based on a patch by Nuno Santos
souliane <souliane@mailoo.org>
parents: 271
diff changeset
331 cursor.execute("""SELECT 1 as bool from entities where jid=%s""",
227
8540825f85e0 Replaced unmaintained pyPgSQL by Psycopg 2
Goffi <goffi@goffi.org>
parents: 206
diff changeset
332 (owner,))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
333
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
334 if not cursor.fetchone():
262
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
335 # XXX: we can NOT rely on the previous query! Commit is needed now because
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
336 # if the entry exists the next query will leave the database in a corrupted
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
337 # state: the solution is to rollback. I tried with other methods like
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
338 # "WHERE NOT EXISTS" but none of them worked, so the following solution
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
339 # looks like the sole - unless you have auto-commit on. More info
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
340 # about this issue: http://cssmay.com/question/tag/tag-psycopg2
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
341 cursor.connection.commit()
262
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
342 try:
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
343 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
344 (owner,))
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
345 except psycopg2.IntegrityError as e:
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
346 cursor.connection.rollback()
262
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
347 logging.warning("during node creation: %s" % e.message)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
348
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
349 cursor.execute("""INSERT INTO affiliations
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
350 (node_id, entity_id, affiliation)
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
351 SELECT %s, entity_id, 'owner' FROM
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
352 (SELECT entity_id FROM entities
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
353 WHERE jid=%s) as e""",
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
354 (node_id, owner))
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
355
330
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
356 if config[const.OPT_ACCESS_MODEL] == const.VAL_AMODEL_PUBLISHER_ROSTER:
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
357 if const.OPT_ROSTER_GROUPS_ALLOWED in config:
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
358 allowed_groups = config[const.OPT_ROSTER_GROUPS_ALLOWED]
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
359 else:
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
360 allowed_groups = []
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
361 for group in allowed_groups:
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
362 #TODO: check that group are actually in roster
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
363 cursor.execute("""INSERT INTO node_groups_authorized (node_id, groupname)
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
364 VALUES (%s,%s)""" , (node_id, group))
330
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
365 # XXX: affiliations can't be set on during node creation (at least not with XEP-0060 alone)
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
366 # so whitelist affiliations need to be done afterward
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
367
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
368 # no we may have to do extra things according to config options
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
369 default_conf = self.defaultConfig['leaf']
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
370 # XXX: trigger works on node creation because OPT_SERIAL_IDS is False in defaultConfig
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
371 # if this value is changed, the _configurationTriggers method should be adapted.
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
372 Node._configurationTriggers(cursor, node_id, default_conf, config)
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
373
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
374 def deleteNodeByDbId(self, db_id):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
375 """Delete a node using directly its database id"""
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
376 return self.dbpool.runInteraction(self._deleteNodeByDbId, db_id)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
377
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
378 def _deleteNodeByDbId(self, cursor, db_id):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
379 cursor.execute("""DELETE FROM nodes WHERE node_id=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
380 (db_id,))
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
381
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
382 if cursor.rowcount != 1:
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
383 raise error.NodeNotFound()
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
384
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
385 def deleteNode(self, nodeIdentifier, pep, recipient=None):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
386 return self.dbpool.runInteraction(self._deleteNode, nodeIdentifier, pep, recipient)
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
387
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
388 def _deleteNode(self, cursor, nodeIdentifier, pep, recipient):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
389 cursor.execute(*withPEP("""DELETE FROM nodes WHERE node=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
390 (nodeIdentifier,), pep, recipient))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
391
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
392 if cursor.rowcount != 1:
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
393 raise error.NodeNotFound()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
394
331
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
395 def getAffiliations(self, entity, nodeIdentifier, pep, recipient=None):
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
396 return self.dbpool.runInteraction(self._getAffiliations, entity, nodeIdentifier, pep, recipient)
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
397
331
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
398 def _getAffiliations(self, cursor, entity, nodeIdentifier, pep, recipient=None):
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
399 query = ["""SELECT node, affiliation FROM entities
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
400 NATURAL JOIN affiliations
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
401 NATURAL JOIN nodes
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
402 WHERE jid=%s"""]
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
403 args = [entity.userhost()]
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
404
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
405 if nodeIdentifier is not None:
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
406 query.append("AND node=%s")
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
407 args.append(nodeIdentifier)
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
408
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
409 cursor.execute(*withPEP(' '.join(query), args, pep, recipient))
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
410 rows = cursor.fetchall()
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
411 return [tuple(r) for r in rows]
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
412
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
413 def getSubscriptions(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
414 self,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
415 entity: jid.JID,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
416 nodeIdentifier: Optional[str] = None,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
417 public: Optional[bool] = None,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
418 pep: bool = False,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
419 recipient: Optional[jid.JID]=None
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
420 ) -> List[Subscription]:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
421 """Retrieve local subscriptions of an entity
357
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
422
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
423 @param entity(jid.JID): entity to check
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
424 @param nodeIdentifier(unicode, None): node identifier
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
425 None to retrieve all subscriptions
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
426 @param pep: True if we are in PEP mode
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
427 @param recipient: jid of the recipient
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
428 """
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
429
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
430 def toSubscriptions(rows):
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
431 subscriptions = []
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
432 for row in rows:
357
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
433 subscriber = jid.internJID('%s/%s' % (row.jid,
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
434 row.resource))
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
435 subscription = Subscription(row.node, subscriber, row.state)
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
436 subscriptions.append(subscription)
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
437 return subscriptions
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
438
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
439 query = ["""SELECT nodes.node,
357
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
440 jid,
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
441 resource,
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
442 state
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
443 FROM entities
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
444 NATURAL JOIN subscriptions
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
445 LEFT JOIN nodes ON nodes.node_id=subscriptions.node_id
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
446 WHERE jid=%s AND subscriptions.node_id IS NOT NULL"""]
357
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
447 args = [entity.userhost()]
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
448
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
449 if public is not None:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
450 query.append("AND subscriptions.public=%s")
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
451 args.append(public)
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
452
357
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
453 if nodeIdentifier is not None:
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
454 query.append("AND nodes.node=%s")
357
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
455 args.append(nodeIdentifier)
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
456
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
457 d = self.dbpool.runQuery(*withPEP(' '.join(query), args, pep, recipient))
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
458 d.addCallback(toSubscriptions)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
459 return d
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
460
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
461 async def getAllSubscriptions(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
462 self,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
463 entity: jid.JID,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
464 public: Optional[bool] = None
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
465 ):
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
466 query = """SELECT subscription_id::text as id,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
467 node,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
468 pep,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
469 ext_service,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
470 ext_node,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
471 state
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
472 FROM entities
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
473 NATURAL JOIN subscriptions
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
474 LEFT JOIN nodes ON nodes.node_id=subscriptions.node_id
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
475 WHERE jid=%s"""
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
476 args = [entity.userhost()]
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
477 if public is not None:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
478 query += "AND public=%s"
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
479 args.append(public)
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
480 rows = await self.dbpool.runQuery(query, args)
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
481 return [r._asdict() for r in rows]
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
482
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
483 def addExternalSubscription(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
484 self,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
485 entity: jid.JID,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
486 service: jid.JID,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
487 node: str,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
488 state: str,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
489 public: bool = False
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
490 ) -> defer.Deferred:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
491 """Store a subscription to an external node
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
492
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
493 @param entity: entity being subscribed
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
494 @param service: pubsub service hosting the node
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
495 @param node: pubsub node being subscribed to
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
496 @param state: state of the subscription
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
497 @param public: True if the subscription is publicly visible
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
498 """
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
499 return self.dbpool.runInteraction(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
500 self._addExternalSubscription,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
501 entity, service, node, state, public
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
502 )
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
503
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
504 def _addExternalSubscription(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
505 self,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
506 cursor,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
507 entity: jid.JID,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
508 service: jid.JID,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
509 node: str,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
510 state: str,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
511 public: bool
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
512 ) -> None:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
513
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
514 try:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
515 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
516 (entity.userhost(),))
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
517 except cursor._pool.dbapi.IntegrityError:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
518 cursor.connection.rollback()
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
519
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
520 cursor.execute("""INSERT INTO subscriptions
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
521 (ext_service, ext_node, entity_id, state, public)
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
522 SELECT %s, %s, entity_id, %s, %s FROM
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
523 (SELECT entity_id FROM entities
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
524 WHERE jid=%s) AS ent_id
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
525 ON CONFLICT(entity_id, ext_service, ext_node) DO UPDATE SET public=EXCLUDED.public""",
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
526 (service.full(),
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
527 node,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
528 state,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
529 public,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
530 entity.userhost()
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
531 ))
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
532
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
533 def removeExternalSubscription(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
534 self,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
535 entity: jid.JID,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
536 service: jid.JID,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
537 node: str,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
538 ) -> defer.Deferred:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
539 """Remove a subscription from an external node
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
540
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
541 @param entity: entity being unsubscribed
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
542 @param service: pubsub service hosting the node
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
543 @param node: pubsub node being unsubscribed to
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
544 """
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
545 return self.dbpool.runInteraction(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
546 self._removeExternalSubscription,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
547 entity, service, node
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
548 )
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
549
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
550 def _removeExternalSubscription(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
551 self,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
552 cursor,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
553 entity: jid.JID,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
554 service: jid.JID,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
555 node: str,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
556 ) -> None:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
557 cursor.execute("""DELETE FROM subscriptions WHERE
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
558 ext_service=%s AND
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
559 ext_node=%s AND
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
560 entity_id=(SELECT entity_id FROM entities
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
561 WHERE jid=%s)
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
562 """,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
563 (service.full(),
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
564 node,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
565 entity.userhost()))
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
566 if cursor.rowcount != 1:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
567 raise error.NotSubscribed()
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
568
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
569 def getDefaultConfiguration(self, nodeType):
366
81e6d4a516c3 storage (pgsql): getDefaultConfiguration now returns a shallow copy of default conf dict, to avoid modification.
Goffi <goffi@goffi.org>
parents: 365
diff changeset
570 return self.defaultConfig[nodeType].copy()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
571
337
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
572 def formatLastItems(self, result):
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
573 last_items = []
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
574 for pep_jid_s, node, data, item_access_model in result:
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
575 pep_jid = jid.JID(pep_jid_s)
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
576 item = generic.stripNamespace(parseXml(data))
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
577 last_items.append((pep_jid, node, item, item_access_model))
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
578 return last_items
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
579
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
580 def getLastItems(self, entities, nodes, node_accesses, item_accesses, pep):
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
581 """get last item for several nodes and entities in a single request"""
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 457
diff changeset
582 # TODO: manage other access model (whitelist, …)
337
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
583 if not entities or not nodes or not node_accesses or not item_accesses:
429
0526073ff2ab pgsql: replaced ValueError by BadRequest:
Goffi <goffi@goffi.org>
parents: 424
diff changeset
584 raise BadRequest(text="entities, nodes and accesses must not be empty")
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 457
diff changeset
585 if any(not {"open", "presence"}.issuperset(a)
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 457
diff changeset
586 for a in (node_accesses, item_accesses)):
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 457
diff changeset
587 raise NotImplementedError('only "open" and "presence" access model are handled for now')
337
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
588 if not pep:
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
589 raise NotImplementedError("getLastItems is only implemented for PEP at the moment")
351
2098295747fd pgsql: cast items.data to text when needed + db schema version bump.
Goffi <goffi@goffi.org>
parents: 349
diff changeset
590 d = self.dbpool.runQuery("""SELECT DISTINCT ON (node_id) pep, node, data::text, items.access_model
337
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
591 FROM items
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
592 NATURAL JOIN nodes
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
593 WHERE nodes.pep IN %s
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
594 AND node IN %s
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
595 AND nodes.access_model in %s
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
596 AND items.access_model in %s
454
7f1394bb96db psql: fix ordering by using windowed function `row_number`
Goffi <goffi@goffi.org>
parents: 451
diff changeset
597 ORDER BY node_id DESC, items.updated DESC, items.item_id DESC""",
337
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
598 (tuple([e.userhost() for e in entities]),
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
599 nodes,
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
600 node_accesses,
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
601 item_accesses))
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
602 d.addCallback(self.formatLastItems)
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
603 return d
57a3051ee435 storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
Goffi <goffi@goffi.org>
parents: 332
diff changeset
604
467
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
605 def getRosterCache(self):
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
606 return self.dbpool.runInteraction(self._getRosterCache)
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
607
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
608 def _getRosterCache(self, cursor):
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
609 cursor.execute(
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
610 "SELECT roster_id, jid, version, updated, roster::text FROM roster"
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
611 )
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
612 return [(r.roster_id, jid.JID(r.jid), r.version, r.updated.timestamp(),
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
613 parseXml(r.roster)) for r in cursor.fetchall()]
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
614
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
615 def setRosterCache(
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
616 self,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
617 jid_: jid.JID,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
618 version: str,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
619 updated: int,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
620 roster: domish.Element
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
621 ):
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
622 return self.dbpool.runInteraction(self._setRosterCache, jid_, version, updated, roster)
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
623
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
624 def _setRosterCache(self, cursor, jid_, version, updated, roster):
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
625 cursor.execute(
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
626 "INSERT INTO roster(jid, version, updated, roster) VALUES (%s, %s, %s, %s) "
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
627 "ON CONFLICT (jid) DO UPDATE SET version=EXCLUDED.version, "
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
628 "updated=EXCLUDED.updated, roster=EXCLUDED.roster",
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
629 (
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
630 jid_.userhost(),
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
631 version,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
632 datetime.fromtimestamp(updated, tz=timezone.utc),
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
633 roster.toXml()
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
634 )
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
635 )
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
636
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
637
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
638 @implementer(iidavoll.INode)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
639 class Node:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
640
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
641
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
642 def __init__(self, nodeDbId, nodeIdentifier, config, schema):
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
643 self.nodeDbId = nodeDbId
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
644 self.nodeIdentifier = nodeIdentifier
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
645 self._config = config
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
646 self._schema = schema
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
647
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
648 def _checkNodeExists(self, cursor):
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
649 cursor.execute("""SELECT 1 as exist FROM nodes WHERE node_id=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
650 (self.nodeDbId,))
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
651 if not cursor.fetchone():
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
652 raise error.NodeNotFound()
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
653
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
654 def getType(self):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
655 return self.nodeType
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
656
330
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
657 def getOwners(self):
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
658 d = self.dbpool.runQuery("""SELECT jid FROM nodes NATURAL JOIN affiliations NATURAL JOIN entities WHERE node_id=%s and affiliation='owner'""", (self.nodeDbId,))
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
659 d.addCallback(lambda rows: [jid.JID(r[0]) for r in rows])
243
42048e37699e added experimental roster access_model (use remote_roster)
Goffi <goffi@goffi.org>
parents: 240
diff changeset
660 return d
42048e37699e added experimental roster access_model (use remote_roster)
Goffi <goffi@goffi.org>
parents: 240
diff changeset
661
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
662 def getConfiguration(self):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
663 return self._config
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
664
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
665 def getNextId(self):
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
666 """return XMPP item id usable for next item to publish
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
667
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
668 the return value will be next int if serila_ids is set,
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
669 else an UUID will be returned
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
670 """
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
671 if self._config[const.OPT_SERIAL_IDS]:
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
672 d = self.dbpool.runQuery("SELECT nextval('{seq_name}')".format(
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
673 seq_name = ITEMS_SEQ_NAME.format(node_id=self.nodeDbId)))
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
674 d.addCallback(lambda rows: str(rows[0][0]))
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
675 return d
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
676 else:
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
677 return defer.succeed(str(uuid.uuid4()))
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
678
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
679 @staticmethod
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
680 def _configurationTriggers(cursor, node_id, old_config, new_config):
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
681 """trigger database relative actions needed when a config is changed
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
682
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
683 @param cursor(): current db cursor
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
684 @param node_id(unicode): database ID of the node
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
685 @param old_config(dict): config of the node before the change
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
686 @param new_config(dict): new options that will be changed
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
687 """
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
688 if const.OPT_SERIAL_IDS not in new_config:
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
689 return
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
690 serial_ids = new_config[const.OPT_SERIAL_IDS]
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
691 if serial_ids != old_config[const.OPT_SERIAL_IDS]:
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
692 # serial_ids option has been modified,
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
693 # we need to handle corresponding sequence
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
694
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
695 # XXX: we use .format in following queries because values
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
696 # are generated by ourself
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
697 seq_name = ITEMS_SEQ_NAME.format(node_id=node_id)
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
698 if serial_ids:
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
699 # the next query get the max value +1 of all XMPP items ids
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
700 # which are integers, and default to 1
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
701 cursor.execute(NEXT_ITEM_ID_QUERY.format(node_id=node_id))
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
702 next_val = cursor.fetchone()[0]
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
703 cursor.execute("DROP SEQUENCE IF EXISTS {seq_name}".format(seq_name = seq_name))
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
704 cursor.execute("CREATE SEQUENCE {seq_name} START {next_val} OWNED BY nodes.node_id".format(
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
705 seq_name = seq_name,
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
706 next_val = next_val))
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
707 else:
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
708 cursor.execute("DROP SEQUENCE IF EXISTS {seq_name}".format(seq_name = seq_name))
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
709
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
710 def setConfiguration(self, options):
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
711 to_delete = []
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
712 for option, value in options.items():
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
713 try:
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
714 if self._config[option] == value:
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
715 to_delete.append(option)
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
716 except KeyError:
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
717 raise BadRequest(text=f"Invalid option: {option!r}")
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
718
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
719 for option in to_delete:
434
f6a225476165 pgsql: fixed `setConfiguration` when an updated field value is the same as current one
Goffi <goffi@goffi.org>
parents: 433
diff changeset
720 del options.fields[option]
180
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
721
434
f6a225476165 pgsql: fixed `setConfiguration` when an updated field value is the same as current one
Goffi <goffi@goffi.org>
parents: 433
diff changeset
722 if not options.fields:
472
d993e8b0fd60 psql: return Deferred is not options field is specified in `setConfiguration`:
Goffi <goffi@goffi.org>
parents: 467
diff changeset
723 return defer.succeed(None)
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
724
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
725 if options.get(const.OPT_MAX_ITEMS) == "max":
424
3fce48c0a44d psql: fixed use of default value for max items in `setConfiguration`
Goffi <goffi@goffi.org>
parents: 422
diff changeset
726 # XXX: "max" is default value for config we must convert
3fce48c0a44d psql: fixed use of default value for max items in `setConfiguration`
Goffi <goffi@goffi.org>
parents: 422
diff changeset
727 # it to an interger. See backend's _doSetNodeConfiguration comment
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
728 options[const.OPT_MAX_ITEMS] = "0"
424
3fce48c0a44d psql: fixed use of default value for max items in `setConfiguration`
Goffi <goffi@goffi.org>
parents: 422
diff changeset
729
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
730 if ((const.OPT_FTS_LANGUAGE in options
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
731 and options[const.OPT_FTS_LANGUAGE] not in self.fts_languages)):
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
732 raise BadRequest(text=
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
733 f"invalid {const.OPT_FTS_LANGUAGE} value: "
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
734 f"{options[const.OPT_FTS_LANGUAGE]!r}"
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
735 )
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
736
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
737
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
738 d = self.dbpool.runInteraction(self._setConfiguration, options)
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
739 d.addCallback(self._updateCachedConfiguration, options)
124
c4ee16bc48e5 Change Node.set_configuration() to set cached configuration in a callback.
Ralph Meijer <ralphm@ik.nu>
parents: 121
diff changeset
740 return d
c4ee16bc48e5 Change Node.set_configuration() to set cached configuration in a callback.
Ralph Meijer <ralphm@ik.nu>
parents: 121
diff changeset
741
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
742 def _setConfiguration(self, cursor, options):
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
743 self._configurationTriggers(cursor, self.nodeDbId, self._config, options)
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
744 # options names all follow the scheme "pubsub#{col_name}"
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
745 col_names = (o[7:] for o in options)
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
746 values = ','.join(f"{name}=%s" for name in col_names)
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
747 cursor.execute(f"UPDATE nodes SET {values} WHERE node_id=%s",
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
748 (*options.values(), self.nodeDbId))
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
749 if cursor.rowcount == 0:
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
750 raise error.NodeNotFound()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
751
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
752 def _updateCachedConfiguration(self, __, options):
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
753 self._config.update(options)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
754
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
755 def getSchema(self):
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
756 return self._schema
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
757
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
758 def setSchema(self, schema):
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
759 d = self.dbpool.runInteraction(self._setSchema, schema)
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
760 d.addCallback(self._setCachedSchema, schema)
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
761 return d
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
762
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
763 def _setSchema(self, cursor, schema):
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
764 self._checkNodeExists(cursor)
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
765 cursor.execute("""UPDATE nodes SET schema=%s
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
766 WHERE node_id=%s""",
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
767 (schema.toXml() if schema else None,
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
768 self.nodeDbId))
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
769
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
770 def _setCachedSchema(self, __, schema):
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
771 self._schema = schema
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents: 351
diff changeset
772
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
773 def getMetaData(self):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
774 config = copy.copy(self._config)
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
775 config["pubsub#node_type"] = self.nodeType
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
776 return config
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
777
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
778 def getAffiliation(self, entity):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
779 return self.dbpool.runInteraction(self._getAffiliation, entity)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
780
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
781 def _getAffiliation(self, cursor, entity):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
782 self._checkNodeExists(cursor)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
783 cursor.execute("""SELECT affiliation FROM affiliations
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
784 NATURAL JOIN nodes
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
785 NATURAL JOIN entities
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
786 WHERE node_id=%s AND jid=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
787 (self.nodeDbId,
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
788 entity.userhost()))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
789
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
790 try:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
791 return cursor.fetchone()[0]
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
792 except TypeError:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
793 return None
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
794
240
70c8bb90d75f added access_model to config, default to 'open'
Goffi <goffi@goffi.org>
parents: 235
diff changeset
795 def getAccessModel(self):
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
796 return self._config[const.OPT_ACCESS_MODEL]
240
70c8bb90d75f added access_model to config, default to 'open'
Goffi <goffi@goffi.org>
parents: 235
diff changeset
797
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
798 def getSubscription(self, subscriber):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
799 return self.dbpool.runInteraction(self._getSubscription, subscriber)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
800
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
801 def _getSubscription(self, cursor, subscriber):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
802 self._checkNodeExists(cursor)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
803
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
804 userhost = subscriber.userhost()
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
805 resource = subscriber.resource or ''
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
806
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
807 cursor.execute("""SELECT state FROM subscriptions
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
808 LEFT JOIN nodes ON nodes.node_id=subscriptions.node_id
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
809 NATURAL JOIN entities
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
810 WHERE subscriptions.node_id=%s AND jid=%s AND resource=%s""",
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
811 (self.nodeDbId,
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
812 userhost,
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
813 resource))
276
b757c29b20d7 import changes from idavoll changeset 233 (24be3a11ddbc), by Ralph Meijer and based on a patch by Nuno Santos
souliane <souliane@mailoo.org>
parents: 271
diff changeset
814
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
815 row = cursor.fetchone()
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
816 if not row:
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
817 return None
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
818 else:
227
8540825f85e0 Replaced unmaintained pyPgSQL by Psycopg 2
Goffi <goffi@goffi.org>
parents: 206
diff changeset
819 return Subscription(self.nodeIdentifier, subscriber, row[0])
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
820
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
821 def getSubscriptions(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
822 self,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
823 state: Optional[str]=None,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
824 public: Optional[bool] = None
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
825 ) -> List[Subscription]:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
826 return self.dbpool.runInteraction(self._getSubscriptions, state, public)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
827
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
828 def _getSubscriptions(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
829 self,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
830 cursor,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
831 state: Optional[str],
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
832 public: Optional[bool] = None,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
833 ) -> List[Subscription]:
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
834 self._checkNodeExists(cursor)
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
835
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
836 query = ["""SELECT subscription_id::text, nodes.node, jid, resource, state,
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
837 subscription_type, subscription_depth
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
838 FROM subscriptions
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
839 LEFT JOIN nodes ON nodes.node_id=subscriptions.node_id
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
840 NATURAL JOIN entities
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
841 WHERE subscriptions.node_id=%s"""]
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
842 values = [self.nodeDbId]
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
843
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
844 if state:
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
845 query.append("AND state=%s")
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
846 values.append(state)
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
847
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
848 if public is not None:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
849 query.append("AND public=%s")
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
850 values.append(public)
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
851
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
852 cursor.execute(" ".join(query), values)
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
853 rows = cursor.fetchall()
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
854
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
855 subscriptions = []
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
856 for row in rows:
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
857 subscriber = jid.JID('%s/%s' % (row.jid, row.resource))
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
858
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
859 options = {}
357
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
860 if row.subscription_type:
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
861 options['pubsub#subscription_type'] = row.subscription_type;
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
862 if row.subscription_depth:
1167e48e5f52 handle single node on subscriptions request
Goffi <goffi@goffi.org>
parents: 356
diff changeset
863 options['pubsub#subscription_depth'] = row.subscription_depth;
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
864
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
865 subscription = Subscription(row.node, subscriber, row.state, options)
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
866 subscription.id = row.subscription_id
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
867 subscriptions.append(subscription)
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
868
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
869 return subscriptions
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
870
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
871 def addSubscription(self, subscriber, state, config):
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
872 return self.dbpool.runInteraction(self._addSubscription, subscriber,
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
873 state, config)
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
874
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
875 def _addSubscription(self, cursor, subscriber, state, config):
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
876 self._checkNodeExists(cursor)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
877
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
878 userhost = subscriber.userhost()
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
879 resource = subscriber.resource or ''
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
880
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
881 subscription_type = config.get('pubsub#subscription_type')
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
882 subscription_depth = config.get('pubsub#subscription_depth')
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
883 public = config.get("public", False)
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
884
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
885 try:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
886 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
227
8540825f85e0 Replaced unmaintained pyPgSQL by Psycopg 2
Goffi <goffi@goffi.org>
parents: 206
diff changeset
887 (userhost,))
8540825f85e0 Replaced unmaintained pyPgSQL by Psycopg 2
Goffi <goffi@goffi.org>
parents: 206
diff changeset
888 except cursor._pool.dbapi.IntegrityError:
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
889 cursor.connection.rollback()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
890
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
891 # the RETURNING trick to detect INSERT vs UPDATE comes from
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
892 # https://stackoverflow.com/a/47001830/4188764 thanks!
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
893 cursor.execute("""INSERT INTO subscriptions
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
894 (node_id, entity_id, resource, state,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
895 subscription_type, subscription_depth, public)
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
896 SELECT %s, entity_id, %s, %s, %s, %s, %s FROM
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
897 (SELECT entity_id FROM entities
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
898 WHERE jid=%s) AS ent_id
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
899 ON CONFLICT (entity_id, node_id, resource) DO UPDATE SET public=EXCLUDED.public
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
900 RETURNING (xmax = 0) AS inserted""",
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
901 (self.nodeDbId,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
902 resource,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
903 state,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
904 subscription_type,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
905 subscription_depth,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
906 public,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
907 userhost))
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
908
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
909 rows = cursor.fetchone()
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
910 if not rows.inserted:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
911 # this was an update, the subscription was already existing
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
912
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
913 # we have to explicitly commit, otherwise the exception raised rollbacks the
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
914 # transation
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 472
diff changeset
915 cursor.connection.commit()
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
916 raise error.SubscriptionExists()
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
917
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
918 def removeSubscription(self, subscriber):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
919 return self.dbpool.runInteraction(self._removeSubscription,
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
920 subscriber)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
921
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
922 def _removeSubscription(self, cursor, subscriber):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
923 self._checkNodeExists(cursor)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
924
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
925 userhost = subscriber.userhost()
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
926 resource = subscriber.resource or ''
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
927
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
928 cursor.execute("""DELETE FROM subscriptions WHERE
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
929 node_id=%s AND
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
930 entity_id=(SELECT entity_id FROM entities
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
931 WHERE jid=%s) AND
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
932 resource=%s""",
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
933 (self.nodeDbId,
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
934 userhost,
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
935 resource))
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
936 if cursor.rowcount != 1:
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 170
diff changeset
937 raise error.NotSubscribed()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
938
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
939 return None
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
940
341
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
941 def setSubscriptions(self, subscriptions):
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
942 return self.dbpool.runInteraction(self._setSubscriptions, subscriptions)
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
943
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
944 def _setSubscriptions(self, cursor, subscriptions):
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
945 self._checkNodeExists(cursor)
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
946
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
947 entities = self.getOrCreateEntities(cursor, [s.subscriber for s in subscriptions])
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
948 entities_map = {jid.JID(e.jid): e for e in entities}
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
949
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
950 # then we construct values for subscriptions update according to entity_id we just got
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
951 placeholders = ','.join(len(subscriptions) * ["%s"])
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
952 values = []
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
953 for subscription in subscriptions:
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
954 entity_id = entities_map[subscription.subscriber].entity_id
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
955 resource = subscription.subscriber.resource or ''
341
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
956 values.append((self.nodeDbId, entity_id, resource, subscription.state, None, None))
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
957 # we use upsert so new values are inserted and existing one updated. This feature is only available for PostgreSQL >= 9.5
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
958 cursor.execute("INSERT INTO subscriptions(node_id, entity_id, resource, state, subscription_type, subscription_depth) VALUES " + placeholders + " ON CONFLICT (entity_id, resource, node_id) DO UPDATE SET state=EXCLUDED.state", [v for v in values])
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
959
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
960 def isSubscribed(self, entity):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
961 return self.dbpool.runInteraction(self._isSubscribed, entity)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
962
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
963 def _isSubscribed(self, cursor, entity):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
964 self._checkNodeExists(cursor)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
965
276
b757c29b20d7 import changes from idavoll changeset 233 (24be3a11ddbc), by Ralph Meijer and based on a patch by Nuno Santos
souliane <souliane@mailoo.org>
parents: 271
diff changeset
966 cursor.execute("""SELECT 1 as bool FROM entities
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
967 NATURAL JOIN subscriptions
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
968 NATURAL JOIN nodes
146
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
969 WHERE entities.jid=%s
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
970 AND node_id=%s AND state='subscribed'""",
146
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
971 (entity.userhost(),
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
972 self.nodeDbId))
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
973
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
974 return cursor.fetchone() is not None
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
975
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
976 def getAffiliations(self):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
977 return self.dbpool.runInteraction(self._getAffiliations)
145
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
978
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
979 def _getAffiliations(self, cursor):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
980 self._checkNodeExists(cursor)
145
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
981
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
982 cursor.execute("""SELECT jid, affiliation FROM nodes
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
983 NATURAL JOIN affiliations
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
984 NATURAL JOIN entities
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
985 WHERE node_id=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
986 (self.nodeDbId,))
145
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
987 result = cursor.fetchall()
180
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
988
330
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
989 return {jid.internJID(r[0]): r[1] for r in result}
145
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
990
331
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
991 def getOrCreateEntities(self, cursor, entities_jids):
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
992 """Get entity_id from entities in entities table
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
993
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
994 Entities will be inserted it they don't exist
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
995 @param entities_jid(list[jid.JID]): entities to get or create
341
b49f75a26156 backend, pgsql: implemented subscriptionsGet and subscriptionsSet
Goffi <goffi@goffi.org>
parents: 337
diff changeset
996 @return list[record(entity_id,jid)]]: list of entity_id and jid (as plain string)
331
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
997 both existing and inserted entities are returned
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
998 """
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
999 # cf. http://stackoverflow.com/a/35265559
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1000 placeholders = ','.join(len(entities_jids) * ["(%s)"])
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1001 query = (
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1002 """
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1003 WITH
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1004 jid_values (jid) AS (
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1005 VALUES {placeholders}
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1006 ),
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1007 inserted (entity_id, jid) AS (
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1008 INSERT INTO entities (jid)
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1009 SELECT jid
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1010 FROM jid_values
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1011 ON CONFLICT DO NOTHING
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1012 RETURNING entity_id, jid
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1013 )
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1014 SELECT e.entity_id, e.jid
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1015 FROM entities e JOIN jid_values jv ON jv.jid = e.jid
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1016 UNION ALL
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1017 SELECT entity_id, jid
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1018 FROM inserted""".format(placeholders=placeholders))
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1019 cursor.execute(query, [j.userhost() for j in entities_jids])
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1020 return cursor.fetchall()
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1021
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1022 def setAffiliations(self, affiliations):
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1023 return self.dbpool.runInteraction(self._setAffiliations, affiliations)
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1024
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1025 def _setAffiliations(self, cursor, affiliations):
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1026 self._checkNodeExists(cursor)
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1027
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1028 entities = self.getOrCreateEntities(cursor, affiliations)
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1029
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1030 # then we construct values for affiliations update according to entity_id we just got
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1031 placeholders = ','.join(len(affiliations) * ["(%s,%s,%s)"])
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1032 values = []
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
1033 for e in entities:
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
1034 values.extend((e.entity_id, affiliations[jid.JID(e.jid)], self.nodeDbId))
331
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1035
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1036 # we use upsert so new values are inserted and existing one updated. This feature is only available for PostgreSQL >= 9.5
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1037 cursor.execute("INSERT INTO affiliations(entity_id,affiliation,node_id) VALUES " + placeholders + " ON CONFLICT (entity_id,node_id) DO UPDATE SET affiliation=EXCLUDED.affiliation", values)
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1038
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1039 def deleteAffiliations(self, entities):
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1040 return self.dbpool.runInteraction(self._deleteAffiliations, entities)
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1041
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1042 def _deleteAffiliations(self, cursor, entities):
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1043 """delete affiliations and subscriptions for this entity"""
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1044 self._checkNodeExists(cursor)
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1045 placeholders = ','.join(len(entities) * ["%s"])
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1046 cursor.execute("DELETE FROM affiliations WHERE node_id=%s AND entity_id in (SELECT entity_id FROM entities WHERE jid IN (" + placeholders + ")) RETURNING entity_id", [self.nodeDbId] + [e.userhost() for e in entities])
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1047
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1048 rows = cursor.fetchall()
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1049 placeholders = ','.join(len(rows) * ["%s"])
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1050 cursor.execute("DELETE FROM subscriptions WHERE node_id=%s AND entity_id in (" + placeholders + ")", [self.nodeDbId] + [r[0] for r in rows])
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1051
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1052 def getAuthorizedGroups(self):
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1053 return self.dbpool.runInteraction(self._getNodeGroups)
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1054
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1055 def _getAuthorizedGroups(self, cursor):
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1056 cursor.execute("SELECT groupname FROM node_groups_authorized NATURAL JOIN nodes WHERE node=%s",
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1057 (self.nodeDbId,))
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1058 rows = cursor.fetchall()
e93a9fd329d9 affiliations XMPP handling:
Goffi <goffi@goffi.org>
parents: 330
diff changeset
1059 return [row[0] for row in rows]
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
1060
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
1061
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
1062 @implementer(iidavoll.ILeafNode)
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
1063 class LeafNode(Node):
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
1064
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
1065
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
1066 nodeType = 'leaf'
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
1067
451
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1068 def getOrderBy(self, ext_data, direction='ASC'):
375
9a787881b824 implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents: 374
diff changeset
1069 """Return ORDER BY clause corresponding to Order By key in ext_data
9a787881b824 implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents: 374
diff changeset
1070
9a787881b824 implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents: 374
diff changeset
1071 @param ext_data (dict): extra data as used in getItems
9a787881b824 implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents: 374
diff changeset
1072 @param direction (unicode): ORDER BY direction (ASC or DESC)
9a787881b824 implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents: 374
diff changeset
1073 @return (unicode): ORDER BY clause to use
9a787881b824 implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents: 374
diff changeset
1074 """
9a787881b824 implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents: 374
diff changeset
1075 keys = ext_data.get('order_by')
9a787881b824 implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents: 374
diff changeset
1076 if not keys:
454
7f1394bb96db psql: fix ordering by using windowed function `row_number`
Goffi <goffi@goffi.org>
parents: 451
diff changeset
1077 return f"ORDER BY updated {direction}, item_id {direction}"
375
9a787881b824 implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents: 374
diff changeset
1078 cols_statmnt = []
9a787881b824 implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents: 374
diff changeset
1079 for key in keys:
9a787881b824 implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents: 374
diff changeset
1080 if key == 'creation':
457
7c9792f934a2 psql: use `created` colum to sort by creation:
Goffi <goffi@goffi.org>
parents: 456
diff changeset
1081 column = 'created'
375
9a787881b824 implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents: 374
diff changeset
1082 elif key == 'modification':
378
22832c1d2827 storage (pgsql): don't specify table name in ORDER BY clause as it can cause troubles in some requests.
Goffi <goffi@goffi.org>
parents: 375
diff changeset
1083 column = 'updated'
375
9a787881b824 implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents: 374
diff changeset
1084 else:
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
1085 log.msg("WARNING: Unknown order by key: {key}".format(key=key))
378
22832c1d2827 storage (pgsql): don't specify table name in ORDER BY clause as it can cause troubles in some requests.
Goffi <goffi@goffi.org>
parents: 375
diff changeset
1086 column = 'updated'
454
7f1394bb96db psql: fix ordering by using windowed function `row_number`
Goffi <goffi@goffi.org>
parents: 451
diff changeset
1087 cols_statmnt.append(f"{column} {direction}")
375
9a787881b824 implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents: 374
diff changeset
1088
457
7c9792f934a2 psql: use `created` colum to sort by creation:
Goffi <goffi@goffi.org>
parents: 456
diff changeset
1089 cols_statmnt.append(f"item_id {direction}")
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
1090 return "ORDER BY " + ",".join([col for col in cols_statmnt])
375
9a787881b824 implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents: 374
diff changeset
1091
393
728b08c0d000 storage (pgsql): fixed storage of multiple items
Goffi <goffi@goffi.org>
parents: 390
diff changeset
1092 @defer.inlineCallbacks
728b08c0d000 storage (pgsql): fixed storage of multiple items
Goffi <goffi@goffi.org>
parents: 390
diff changeset
1093 def storeItems(self, items_data, publisher):
728b08c0d000 storage (pgsql): fixed storage of multiple items
Goffi <goffi@goffi.org>
parents: 390
diff changeset
1094 # XXX: runInteraction doesn't seem to work when there are several "insert"
728b08c0d000 storage (pgsql): fixed storage of multiple items
Goffi <goffi@goffi.org>
parents: 390
diff changeset
1095 # or "update".
728b08c0d000 storage (pgsql): fixed storage of multiple items
Goffi <goffi@goffi.org>
parents: 390
diff changeset
1096 # Before the unpacking was done in _storeItems, but this was causing trouble
728b08c0d000 storage (pgsql): fixed storage of multiple items
Goffi <goffi@goffi.org>
parents: 390
diff changeset
1097 # in case of multiple items_data. So this has now be moved here.
728b08c0d000 storage (pgsql): fixed storage of multiple items
Goffi <goffi@goffi.org>
parents: 390
diff changeset
1098 # FIXME: investigate the issue with runInteraction
728b08c0d000 storage (pgsql): fixed storage of multiple items
Goffi <goffi@goffi.org>
parents: 390
diff changeset
1099 for item_data in items_data:
728b08c0d000 storage (pgsql): fixed storage of multiple items
Goffi <goffi@goffi.org>
parents: 390
diff changeset
1100 yield self.dbpool.runInteraction(self._storeItems, item_data, publisher)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
1101
393
728b08c0d000 storage (pgsql): fixed storage of multiple items
Goffi <goffi@goffi.org>
parents: 390
diff changeset
1102 def _storeItems(self, cursor, item_data, publisher):
250
eb14b8d30cba fine tuning per-item permissions
Goffi <goffi@goffi.org>
parents: 249
diff changeset
1103 self._checkNodeExists(cursor)
393
728b08c0d000 storage (pgsql): fixed storage of multiple items
Goffi <goffi@goffi.org>
parents: 390
diff changeset
1104 self._storeItem(cursor, item_data, publisher)
244
3ecc94407e36 item access_model (not finished)
Goffi <goffi@goffi.org>
parents: 243
diff changeset
1105
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1106 def _storeItem(self, cursor, item_data, publisher):
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1107 # first try to insert the item
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1108 # - if it fails (conflict), and the item is new and we have serial_ids options,
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1109 # current id will be recomputed using next item id query (note that is not perfect, as
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1110 # table is not locked and this can fail if two items are added at the same time
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1111 # but this can only happen with serial_ids and if future ids have been set by a client,
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1112 # this case should be rare enough to consider this situation acceptable)
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1113 # - if item insertion fail and the item is not new, we do an update
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1114 # - in other cases, exception is raised
301
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
1115 item, access_model, item_config = item_data.item, item_data.access_model, item_data.config
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
1116 data = item.toXml()
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1117 data_fts_cfg = self._config[const.OPT_FTS_LANGUAGE]
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1118 if data_fts_cfg == const.VAL_FTS_GENERIC:
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1119 data_fts_cfg = "simple"
262
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
1120
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1121 insert_query = (
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1122 "INSERT INTO items(node_id, item, publisher, data, access_model, "
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1123 "data_fts_cfg) VALUES (%s, %s, %s, %s, %s, %s) RETURNING item_id"
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1124 )
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1125 insert_data = [self.nodeDbId,
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1126 item["id"],
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1127 publisher.full(),
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1128 data,
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1129 access_model,
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1130 data_fts_cfg,
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1131 ]
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
1132
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1133 try:
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1134 cursor.execute(insert_query, insert_data)
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1135 except cursor._pool.dbapi.IntegrityError as e:
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1136 if e.pgcode != "23505":
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1137 # we only handle unique_violation, every other exception must be raised
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1138 raise e
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1139 cursor.connection.rollback()
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1140 # the item already exist
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1141 if item_data.new:
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1142 # the item is new
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1143 if self._config[const.OPT_SERIAL_IDS]:
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1144 # this can happen with serial_ids, if a item has been stored
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1145 # with a future id (generated by XMPP client)
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1146 cursor.execute(NEXT_ITEM_ID_QUERY.format(node_id=self.nodeDbId))
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1147 next_id = cursor.fetchone()[0]
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1148 # we update the sequence, so we can skip conflicting ids
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
1149 cursor.execute("SELECT setval('{seq_name}', %s)".format(
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1150 seq_name = ITEMS_SEQ_NAME.format(node_id=self.nodeDbId)), [next_id])
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1151 # and now we can retry the query with the new id
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
1152 item['id'] = insert_data[1] = str(next_id)
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1153 # item saved in DB must also be updated with the new id
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1154 insert_data[3] = item.toXml()
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1155 cursor.execute(insert_query, insert_data)
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1156 else:
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1157 # but if we have not serial_ids, we have a real problem
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1158 raise e
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1159 else:
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1160 # this is an update
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1161 cursor.execute("""UPDATE items SET updated=now(), publisher=%s, data=%s
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1162 WHERE node_id=%s AND items.item=%s
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1163 RETURNING item_id""",
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1164 (publisher.full(),
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1165 data,
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1166 self.nodeDbId,
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1167 item["id"]))
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1168 if cursor.rowcount != 1:
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1169 raise exceptions.InternalError("item has not been updated correctly")
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1170 item_id = cursor.fetchone()[0];
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1171 self._storeCategories(cursor, item_id, item_data.categories, update=True)
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1172 return
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
1173
301
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
1174 item_id = cursor.fetchone()[0];
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
1175 self._storeCategories(cursor, item_id, item_data.categories)
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
1176
330
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
1177 if access_model == const.VAL_AMODEL_PUBLISHER_ROSTER:
250
eb14b8d30cba fine tuning per-item permissions
Goffi <goffi@goffi.org>
parents: 249
diff changeset
1178 if const.OPT_ROSTER_GROUPS_ALLOWED in item_config:
422
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1179 # XXX: needed to force list if there is only one value
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1180 item_config.fields[const.OPT_ROSTER_GROUPS_ALLOWED].fieldType='list-multi'
250
eb14b8d30cba fine tuning per-item permissions
Goffi <goffi@goffi.org>
parents: 249
diff changeset
1181 allowed_groups = item_config[const.OPT_ROSTER_GROUPS_ALLOWED]
245
e11e99246be5 allowed groups from item_config are now stored
Goffi <goffi@goffi.org>
parents: 244
diff changeset
1182 else:
e11e99246be5 allowed groups from item_config are now stored
Goffi <goffi@goffi.org>
parents: 244
diff changeset
1183 allowed_groups = []
e11e99246be5 allowed groups from item_config are now stored
Goffi <goffi@goffi.org>
parents: 244
diff changeset
1184 for group in allowed_groups:
e11e99246be5 allowed groups from item_config are now stored
Goffi <goffi@goffi.org>
parents: 244
diff changeset
1185 #TODO: check that group are actually in roster
e11e99246be5 allowed groups from item_config are now stored
Goffi <goffi@goffi.org>
parents: 244
diff changeset
1186 cursor.execute("""INSERT INTO item_groups_authorized (item_id, groupname)
e11e99246be5 allowed groups from item_config are now stored
Goffi <goffi@goffi.org>
parents: 244
diff changeset
1187 VALUES (%s,%s)""" , (item_id, group))
330
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
1188 # TODO: whitelist access model
245
e11e99246be5 allowed groups from item_config are now stored
Goffi <goffi@goffi.org>
parents: 244
diff changeset
1189
422
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1190 max_items = self._config.get(const.OPT_MAX_ITEMS, 'max')
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1191
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1192 if max_items != 'max':
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1193 try:
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1194 max_items = int(self._config[const.OPT_MAX_ITEMS])
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1195 except ValueError:
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1196 log.err(f"Invalid max_items value: {max_items!r}")
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1197 else:
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1198 if max_items > 0:
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1199 # we delete all items above the requested max
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1200 cursor.execute(
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1201 "DELETE FROM items WHERE node_id=%s and item_id in (SELECT "
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1202 "item_id FROM items WHERE node_id=%s ORDER BY items.updated "
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1203 "DESC, items.item_id DESC OFFSET %s)" ,
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1204 (self.nodeDbId, self.nodeDbId, max_items)
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1205 )
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1206
c21f31355ab9 configuration: "max_items" option:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
1207
301
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
1208 def _storeCategories(self, cursor, item_id, categories, update=False):
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
1209 # TODO: handle canonical form
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
1210 if update:
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
1211 cursor.execute("""DELETE FROM item_categories
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
1212 WHERE item_id=%s""", (item_id,))
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
1213
402
724e39d596a9 psql: use a set for categories, to avoid duplicates (and constraint failure)
Goffi <goffi@goffi.org>
parents: 393
diff changeset
1214 # we use a set to avoid duplicates
724e39d596a9 psql: use a set for categories, to avoid duplicates (and constraint failure)
Goffi <goffi@goffi.org>
parents: 393
diff changeset
1215 for category in set(categories):
301
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
1216 cursor.execute("""INSERT INTO item_categories (item_id, category)
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
1217 VALUES (%s, %s)""", (item_id, category))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
1218
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
1219 def removeItems(self, itemIdentifiers):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1220 return self.dbpool.runInteraction(self._removeItems, itemIdentifiers)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
1221
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
1222 def _removeItems(self, cursor, itemIdentifiers):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
1223 self._checkNodeExists(cursor)
180
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
1224
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
1225 deleted = []
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
1226
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
1227 for itemIdentifier in itemIdentifiers:
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
1228 cursor.execute("""DELETE FROM items WHERE
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1229 node_id=%s AND
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
1230 item=%s""",
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1231 (self.nodeDbId,
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
1232 itemIdentifier))
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
1233
142
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 136
diff changeset
1234 if cursor.rowcount:
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
1235 deleted.append(itemIdentifier)
142
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 136
diff changeset
1236
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 136
diff changeset
1237 return deleted
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
1238
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
1239 def getItems(self, authorized_groups, unrestricted, maxItems=None, ext_data=None):
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1240 """ Get all authorised items
346
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1241
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1242 @param authorized_groups: we want to get items that these groups can access
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1243 @param unrestricted: if true, don't check permissions (i.e.: get all items)
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1244 @param maxItems: nb of items we want to get
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1245 @param ext_data: options for extra features like RSM and MAM
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
1246
317
34adc4a8aa64 new container module, with an ItemData container:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
1247 @return: list of container.ItemData
34adc4a8aa64 new container module, with an ItemData container:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
1248 if unrestricted is False, access_model and config will be None
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1249 """
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
1250 if ext_data is None:
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
1251 ext_data = {}
347
f33406fcab5c pgsql: fixed _getItems call
Goffi <goffi@goffi.org>
parents: 346
diff changeset
1252 return self.dbpool.runInteraction(self._getItems, authorized_groups, unrestricted, maxItems, ext_data, ids_only=False)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
1253
346
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1254 def getItemsIds(self, authorized_groups, unrestricted, maxItems=None, ext_data=None):
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1255 """ Get all authorised items ids
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1256
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1257 @param authorized_groups: we want to get items that these groups can access
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1258 @param unrestricted: if true, don't check permissions (i.e.: get all items)
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1259 @param maxItems: nb of items we want to get
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1260 @param ext_data: options for extra features like RSM and MAM
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1261
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1262 @return list(unicode): list of ids
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1263 """
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1264 if ext_data is None:
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1265 ext_data = {}
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1266 return self.dbpool.runInteraction(self._getItems, authorized_groups, unrestricted, maxItems, ext_data, ids_only=True)
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1267
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1268 def _appendSourcesAndFilters(self, query, args, authorized_groups, unrestricted, ext_data):
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1269 """append sources and filters to sql query requesting items and return ORDER BY
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
1270
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1271 arguments query, args, authorized_groups, unrestricted and ext_data are the same as for
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1272 _getItems
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1273 """
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1274 # SOURCES
454
7f1394bb96db psql: fix ordering by using windowed function `row_number`
Goffi <goffi@goffi.org>
parents: 451
diff changeset
1275 query.append("FROM items")
323
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1276
248
50f6ee966da8 item are gotten according to item's access model in getItems
Goffi <goffi@goffi.org>
parents: 247
diff changeset
1277 if unrestricted:
323
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1278 query_filters = ["WHERE node_id=%s"]
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1279 args.append(self.nodeDbId)
248
50f6ee966da8 item are gotten according to item's access model in getItems
Goffi <goffi@goffi.org>
parents: 247
diff changeset
1280 else:
323
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1281 query.append("LEFT JOIN item_groups_authorized USING (item_id)")
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1282 args.append(self.nodeDbId)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1283 if authorized_groups:
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1284 get_groups = " or (items.access_model='roster' and groupname in %s)"
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1285 args.append(authorized_groups)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1286 else:
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1287 get_groups = ""
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
1288
323
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1289 query_filters = ["WHERE node_id=%s AND (items.access_model='open'" + get_groups + ")"]
248
50f6ee966da8 item are gotten according to item's access model in getItems
Goffi <goffi@goffi.org>
parents: 247
diff changeset
1290
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1291 # FILTERS
282
7d54ff2eeaf2 actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents: 278
diff changeset
1292 if 'filters' in ext_data: # MAM filters
7d54ff2eeaf2 actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents: 278
diff changeset
1293 for filter_ in ext_data['filters']:
7d54ff2eeaf2 actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents: 278
diff changeset
1294 if filter_.var == 'start':
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1295 query_filters.append("AND created>=%s")
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1296 args.append(filter_.value)
323
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1297 elif filter_.var == 'end':
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1298 query_filters.append("AND created<=%s")
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1299 args.append(filter_.value)
323
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1300 elif filter_.var == 'with':
282
7d54ff2eeaf2 actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents: 278
diff changeset
1301 jid_s = filter_.value
7d54ff2eeaf2 actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents: 278
diff changeset
1302 if '/' in jid_s:
323
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1303 query_filters.append("AND publisher=%s")
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1304 args.append(filter_.value)
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1305 else:
323
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1306 query_filters.append("AND publisher LIKE %s")
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 406
diff changeset
1307 args.append("{}%".format(filter_.value))
430
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1308 elif filter_.var == const.MAM_FILTER_FTS:
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1309 fts_cfg = self._config[const.OPT_FTS_LANGUAGE]
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1310 if fts_cfg == const.VAL_FTS_GENERIC:
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1311 fts_cfg = "simple"
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1312 query_filters.append(
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1313 "AND data_fts @@ websearch_to_tsquery(%s, %s)"
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1314 )
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1315 args.append(fts_cfg)
5a0ada3b61ca Full-Text Search implementation:
Goffi <goffi@goffi.org>
parents: 429
diff changeset
1316 args.append(filter_.value)
323
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1317 elif filter_.var == const.MAM_FILTER_CATEGORY:
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1318 query.append("LEFT JOIN item_categories USING (item_id)")
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1319 query_filters.append("AND category=%s")
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1320 args.append(filter_.value)
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1321 else:
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1322 log.msg("WARNING: unknown filter: {}".format(filter_.encode('utf-8')))
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1323
8496af26be45 mam: added filtering by category
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1324 query.extend(query_filters)
282
7d54ff2eeaf2 actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents: 278
diff changeset
1325
451
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1326 return self.getOrderBy(ext_data, direction="ASC")
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1327
346
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1328 def _getItems(self, cursor, authorized_groups, unrestricted, maxItems, ext_data, ids_only):
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1329 self._checkNodeExists(cursor)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1330
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1331 if maxItems == 0:
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1332 return []
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1333
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1334 args = []
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1335
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1336 # SELECT
346
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1337 if ids_only:
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1338 query = ["SELECT item"]
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1339 else:
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1340 query = ["SELECT data::text,items.access_model,item_id,created,updated"]
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1341
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1342 query_order = self._appendSourcesAndFilters(query, args, authorized_groups, unrestricted, ext_data)
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
1343
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
1344 if 'rsm' in ext_data:
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
1345 rsm = ext_data['rsm']
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
1346 maxItems = rsm.max
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
1347 if rsm.index is not None:
319
a51947371625 pgsql storage: use item_id instead of date to order and select items
Goffi <goffi@goffi.org>
parents: 318
diff changeset
1348 # We need to know the item_id of corresponding to the index (offset) of the current query
a51947371625 pgsql storage: use item_id instead of date to order and select items
Goffi <goffi@goffi.org>
parents: 318
diff changeset
1349 # so we execute the query to look for the item_id
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1350 tmp_query = query[:]
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1351 tmp_args = args[:]
319
a51947371625 pgsql storage: use item_id instead of date to order and select items
Goffi <goffi@goffi.org>
parents: 318
diff changeset
1352 tmp_query[0] = "SELECT item_id"
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1353 tmp_query.append("{} LIMIT 1 OFFSET %s".format(query_order))
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1354 tmp_args.append(rsm.index)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1355 cursor.execute(' '.join(query), args)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1356 # FIXME: bad index is not managed yet
319
a51947371625 pgsql storage: use item_id instead of date to order and select items
Goffi <goffi@goffi.org>
parents: 318
diff changeset
1357 item_id = cursor.fetchall()[0][0]
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1358
319
a51947371625 pgsql storage: use item_id instead of date to order and select items
Goffi <goffi@goffi.org>
parents: 318
diff changeset
1359 # now that we have the id, we can use it
a51947371625 pgsql storage: use item_id instead of date to order and select items
Goffi <goffi@goffi.org>
parents: 318
diff changeset
1360 query.append("AND item_id<=%s")
a51947371625 pgsql storage: use item_id instead of date to order and select items
Goffi <goffi@goffi.org>
parents: 318
diff changeset
1361 args.append(item_id)
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
1362 elif rsm.before is not None:
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
1363 if rsm.before != '':
454
7f1394bb96db psql: fix ordering by using windowed function `row_number`
Goffi <goffi@goffi.org>
parents: 451
diff changeset
1364 query.insert(0,"SELECT * from (")
7f1394bb96db psql: fix ordering by using windowed function `row_number`
Goffi <goffi@goffi.org>
parents: 451
diff changeset
1365 query[1] += f",row_number() OVER ({query_order}) as row_number"
7f1394bb96db psql: fix ordering by using windowed function `row_number`
Goffi <goffi@goffi.org>
parents: 451
diff changeset
1366 query.append(f"{query_order}) as x")
450
96059231d58f pgsql: fix RSM before and after subqueries
Goffi <goffi@goffi.org>
parents: 440
diff changeset
1367 query.append(
456
b52ebc45b8e3 psql: fix RSM requests handling:
Goffi <goffi@goffi.org>
parents: 454
diff changeset
1368 "WHERE row_number<(SELECT row_number FROM (SELECT row_number() "
b52ebc45b8e3 psql: fix RSM requests handling:
Goffi <goffi@goffi.org>
parents: 454
diff changeset
1369 f"OVER ({query_order}) as row_number,item"
450
96059231d58f pgsql: fix RSM before and after subqueries
Goffi <goffi@goffi.org>
parents: 440
diff changeset
1370 )
456
b52ebc45b8e3 psql: fix RSM requests handling:
Goffi <goffi@goffi.org>
parents: 454
diff changeset
1371 self._appendSourcesAndFilters(
b52ebc45b8e3 psql: fix RSM requests handling:
Goffi <goffi@goffi.org>
parents: 454
diff changeset
1372 query, args, authorized_groups, unrestricted, ext_data
b52ebc45b8e3 psql: fix RSM requests handling:
Goffi <goffi@goffi.org>
parents: 454
diff changeset
1373 )
b52ebc45b8e3 psql: fix RSM requests handling:
Goffi <goffi@goffi.org>
parents: 454
diff changeset
1374 query.append(") as row_num_sub WHERE item=%s)")
b52ebc45b8e3 psql: fix RSM requests handling:
Goffi <goffi@goffi.org>
parents: 454
diff changeset
1375 args.append(rsm.before)
451
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1376 # we need to reverse order in a first query to get the right
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1377 # items
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1378 query.insert(0,"SELECT * from (")
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1379 query.append(self.getOrderBy(ext_data, direction='DESC'))
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1380 query.append("LIMIT %s) as x")
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1381 if maxItems is None:
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1382 # we default to 10 max when we request last page
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1383 log.msg(
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1384 "WARNING: getting last page with RSM without max, "
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1385 "we default to max=10"
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1386 )
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1387 args.append(10)
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1388 else:
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1389 args.append(maxItems)
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
1390 elif rsm.after:
454
7f1394bb96db psql: fix ordering by using windowed function `row_number`
Goffi <goffi@goffi.org>
parents: 451
diff changeset
1391 query.insert(0,"SELECT * from (")
7f1394bb96db psql: fix ordering by using windowed function `row_number`
Goffi <goffi@goffi.org>
parents: 451
diff changeset
1392 query[1] += f",row_number() OVER ({query_order}) as row_number"
7f1394bb96db psql: fix ordering by using windowed function `row_number`
Goffi <goffi@goffi.org>
parents: 451
diff changeset
1393 query.append(f"{query_order}) as x")
450
96059231d58f pgsql: fix RSM before and after subqueries
Goffi <goffi@goffi.org>
parents: 440
diff changeset
1394 query.append(
456
b52ebc45b8e3 psql: fix RSM requests handling:
Goffi <goffi@goffi.org>
parents: 454
diff changeset
1395 "WHERE row_number>(SELECT row_number FROM (SELECT row_number() "
b52ebc45b8e3 psql: fix RSM requests handling:
Goffi <goffi@goffi.org>
parents: 454
diff changeset
1396 f"OVER ({query_order}) as row_number,item"
450
96059231d58f pgsql: fix RSM before and after subqueries
Goffi <goffi@goffi.org>
parents: 440
diff changeset
1397 )
456
b52ebc45b8e3 psql: fix RSM requests handling:
Goffi <goffi@goffi.org>
parents: 454
diff changeset
1398 self._appendSourcesAndFilters(
b52ebc45b8e3 psql: fix RSM requests handling:
Goffi <goffi@goffi.org>
parents: 454
diff changeset
1399 query, args, authorized_groups, unrestricted, ext_data
b52ebc45b8e3 psql: fix RSM requests handling:
Goffi <goffi@goffi.org>
parents: 454
diff changeset
1400 )
b52ebc45b8e3 psql: fix RSM requests handling:
Goffi <goffi@goffi.org>
parents: 454
diff changeset
1401 query.append(") as row_num_sub WHERE item=%s)")
b52ebc45b8e3 psql: fix RSM requests handling:
Goffi <goffi@goffi.org>
parents: 454
diff changeset
1402 args.append(rsm.after)
451
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1403 else:
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1404 rsm = False
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
1405
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1406 query.append(query_order)
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
1407
297
4115999d85e9 fixes behavior when maxItems is 0
souliane <souliane@mailoo.org>
parents: 294
diff changeset
1408 if maxItems is not None:
451
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1409 if rsm == False:
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1410 # if we have legacy maxItems (i.e. not RSM max), we need to
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1411 # reverse order (maxItems get last items, cf.
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1412 # https://xmpp.org/extensions/xep-0060.html#subscriber-retrieve-requestrecent)
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1413 # in a first query to get the right items
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1414 query.insert(0,"SELECT * from (")
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1415 query.insert(len(query)-1, self.getOrderBy(ext_data, direction='DESC'))
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1416 query.insert(len(query)-1, "LIMIT %s) as x")
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1417 args.append(maxItems)
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1418 else:
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1419 query.append("LIMIT %s")
0e6e176cb572 pgsql: fix items order:
Goffi <goffi@goffi.org>
parents: 450
diff changeset
1420 args.append(maxItems)
262
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
1421
248
50f6ee966da8 item are gotten according to item's access model in getItems
Goffi <goffi@goffi.org>
parents: 247
diff changeset
1422 cursor.execute(' '.join(query), args)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
1423
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
1424 result = cursor.fetchall()
346
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1425 if unrestricted and not ids_only:
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1426 # with unrestricted query, we need to fill the access_list for a roster access items
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1427 ret = []
356
95c83899b5e9 pgsql: fixed bad data filling in getItemsData:
Goffi <goffi@goffi.org>
parents: 352
diff changeset
1428 for item_data in result:
95c83899b5e9 pgsql: fixed bad data filling in getItemsData:
Goffi <goffi@goffi.org>
parents: 352
diff changeset
1429 item = generic.stripNamespace(parseXml(item_data.data))
95c83899b5e9 pgsql: fixed bad data filling in getItemsData:
Goffi <goffi@goffi.org>
parents: 352
diff changeset
1430 access_model = item_data.access_model
95c83899b5e9 pgsql: fixed bad data filling in getItemsData:
Goffi <goffi@goffi.org>
parents: 352
diff changeset
1431 item_id = item_data.item_id
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1432 created = item_data.created
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1433 updated = item_data.updated
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1434 access_list = {}
330
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
1435 if access_model == const.VAL_AMODEL_PUBLISHER_ROSTER:
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1436 cursor.execute('SELECT groupname FROM item_groups_authorized WHERE item_id=%s', (item_id,))
356
95c83899b5e9 pgsql: fixed bad data filling in getItemsData:
Goffi <goffi@goffi.org>
parents: 352
diff changeset
1437 access_list[const.OPT_ROSTER_GROUPS_ALLOWED] = [r.groupname for r in cursor.fetchall()]
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1438
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1439 ret.append(container.ItemData(item, access_model, access_list, created=created, updated=updated))
330
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
1440 # TODO: whitelist item access model
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1441 return ret
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1442
346
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1443 if ids_only:
356
95c83899b5e9 pgsql: fixed bad data filling in getItemsData:
Goffi <goffi@goffi.org>
parents: 352
diff changeset
1444 return [r.item for r in result]
346
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
1445 else:
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1446 items_data = [container.ItemData(generic.stripNamespace(parseXml(r.data)), r.access_model, created=r.created, updated=r.updated) for r in result]
317
34adc4a8aa64 new container module, with an ItemData container:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
1447 return items_data
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
1448
248
50f6ee966da8 item are gotten according to item's access model in getItems
Goffi <goffi@goffi.org>
parents: 247
diff changeset
1449 def getItemsById(self, authorized_groups, unrestricted, itemIdentifiers):
330
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
1450 """Get items which are in the given list
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
1451
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1452 @param authorized_groups: we want to get items that these groups can access
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1453 @param unrestricted: if true, don't check permissions
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1454 @param itemIdentifiers: list of ids of the items we want to get
317
34adc4a8aa64 new container module, with an ItemData container:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
1455 @return: list of container.ItemData
34adc4a8aa64 new container module, with an ItemData container:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
1456 ItemData.config will contains access_list (managed as a dictionnary with same key as for item_config)
34adc4a8aa64 new container module, with an ItemData container:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
1457 if unrestricted is False, access_model and config will be None
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1458 """
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1459 return self.dbpool.runInteraction(self._getItemsById, authorized_groups, unrestricted, itemIdentifiers)
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
1460
248
50f6ee966da8 item are gotten according to item's access model in getItems
Goffi <goffi@goffi.org>
parents: 247
diff changeset
1461 def _getItemsById(self, cursor, authorized_groups, unrestricted, itemIdentifiers):
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
1462 self._checkNodeExists(cursor)
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1463 ret = []
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1464 if unrestricted: #we get everything without checking permissions
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1465 for itemIdentifier in itemIdentifiers:
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1466 cursor.execute("""SELECT data::text,items.access_model,item_id,created,updated FROM nodes
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1467 INNER JOIN items USING (node_id)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1468 WHERE node_id=%s AND item=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1469 (self.nodeDbId,
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1470 itemIdentifier))
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1471 result = cursor.fetchone()
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1472 if not result:
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1473 raise error.ItemNotFound()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
1474
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1475 item = generic.stripNamespace(parseXml(result[0]))
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1476 access_model = result[1]
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1477 item_id = result[2]
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1478 created= result[3]
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1479 updated= result[4]
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1480 access_list = {}
330
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
1481 if access_model == const.VAL_AMODEL_PUBLISHER_ROSTER:
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1482 cursor.execute('SELECT groupname FROM item_groups_authorized WHERE item_id=%s', (item_id,))
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1483 access_list[const.OPT_ROSTER_GROUPS_ALLOWED] = [r[0] for r in cursor.fetchall()]
330
82d1259b3e36 backend, pgsql storage: better items/notification handling, various fixes:
Goffi <goffi@goffi.org>
parents: 324
diff changeset
1484 #TODO: WHITELIST access_model
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1485
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1486 ret.append(container.ItemData(item, access_model, access_list, created=created, updated=updated))
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1487 else: #we check permission before returning items
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1488 for itemIdentifier in itemIdentifiers:
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1489 args = [self.nodeDbId, itemIdentifier]
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1490 if authorized_groups:
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1491 args.append(authorized_groups)
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1492 cursor.execute("""SELECT data::text, created, updated FROM nodes
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1493 INNER JOIN items USING (node_id)
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1494 LEFT JOIN item_groups_authorized USING (item_id)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1495 WHERE node_id=%s AND item=%s AND
262
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
1496 (items.access_model='open' """ +
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1497 ("or (items.access_model='roster' and groupname in %s)" if authorized_groups else '') + ")",
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1498 args)
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1499
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1500 result = cursor.fetchone()
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1501 if result:
367
a772f7dac930 backend, storage(pgsql): creation/update date + serial ids:
Goffi <goffi@goffi.org>
parents: 366
diff changeset
1502 ret.append(container.ItemData(generic.stripNamespace(parseXml(result[0])), created=result[1], updated=result[2]))
262
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
1503
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
1504 return ret
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
1505
318
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1506 def getItemsCount(self, authorized_groups, unrestricted, ext_data=None):
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1507 """Count expected number of items in a getItems query
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1508
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1509 @param authorized_groups: we want to get items that these groups can access
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1510 @param unrestricted: if true, don't check permissions (i.e.: get all items)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1511 @param ext_data: options for extra features like RSM and MAM
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1512 """
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1513 if ext_data is None:
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1514 ext_data = {}
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1515 return self.dbpool.runInteraction(self._getItemsCount, authorized_groups, unrestricted, ext_data)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1516
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1517 def _getItemsCount(self, cursor, authorized_groups, unrestricted, ext_data):
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1518 self._checkNodeExists(cursor)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1519 args = []
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1520
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1521 # SELECT
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1522 query = ["SELECT count(1)"]
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1523
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1524 self._appendSourcesAndFilters(query, args, authorized_groups, unrestricted, ext_data)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1525
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1526 cursor.execute(' '.join(query), args)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1527 return cursor.fetchall()[0][0]
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1528
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1529 def getItemsIndex(self, item_id, authorized_groups, unrestricted, ext_data=None):
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1530 """Get expected index of first item in the window of a getItems query
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1531
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1532 @param item_id: id of the item
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1533 @param authorized_groups: we want to get items that these groups can access
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1534 @param unrestricted: if true, don't check permissions (i.e.: get all items)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1535 @param ext_data: options for extra features like RSM and MAM
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1536 """
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1537 if ext_data is None:
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1538 ext_data = {}
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1539 return self.dbpool.runInteraction(self._getItemsIndex, item_id, authorized_groups, unrestricted, ext_data)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1540
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1541 def _getItemsIndex(self, cursor, item_id, authorized_groups, unrestricted, ext_data):
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1542 self._checkNodeExists(cursor)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1543 args = []
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1544
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1545 # SELECT
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1546 query = []
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1547
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1548 query_order = self._appendSourcesAndFilters(query, args, authorized_groups, unrestricted, ext_data)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1549
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1550 query_select = "SELECT row_number from (SELECT row_number() OVER ({}), item".format(query_order)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1551 query.insert(0, query_select)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1552 query.append(") as x WHERE item=%s")
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1553 args.append(item_id)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1554
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1555 cursor.execute(' '.join(query), args)
d13526c0eb32 RSM improvments/refactoring:
Goffi <goffi@goffi.org>
parents: 317
diff changeset
1556 # XXX: row_number start at 1, but we want that index start at 0
324
e73e42b4f6ff pgsql storage: getItemsIndex raise NodeNotFound in this make sense
Goffi <goffi@goffi.org>
parents: 323
diff changeset
1557 try:
e73e42b4f6ff pgsql storage: getItemsIndex raise NodeNotFound in this make sense
Goffi <goffi@goffi.org>
parents: 323
diff changeset
1558 return cursor.fetchall()[0][0] - 1
e73e42b4f6ff pgsql storage: getItemsIndex raise NodeNotFound in this make sense
Goffi <goffi@goffi.org>
parents: 323
diff changeset
1559 except IndexError:
e73e42b4f6ff pgsql storage: getItemsIndex raise NodeNotFound in this make sense
Goffi <goffi@goffi.org>
parents: 323
diff changeset
1560 raise error.NodeNotFound()
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1561
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1562 def getItemsPublishers(self, itemIdentifiers):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1563 """Get the publishers for all given identifiers
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1564
308
087b705493a6 fixed publisher check on item publishing
Goffi <goffi@goffi.org>
parents: 301
diff changeset
1565 @return (dict[unicode, jid.JID]): map of itemIdentifiers to publisher
087b705493a6 fixed publisher check on item publishing
Goffi <goffi@goffi.org>
parents: 301
diff changeset
1566 if item is not found, key is skipped in resulting dict
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1567 """
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1568 return self.dbpool.runInteraction(self._getItemsPublishers, itemIdentifiers)
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1569
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1570 def _getItemsPublishers(self, cursor, itemIdentifiers):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1571 self._checkNodeExists(cursor)
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1572 ret = {}
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1573 for itemIdentifier in itemIdentifiers:
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1574 cursor.execute("""SELECT publisher FROM items
390
fba96e95e329 storage (pgsql): fixed filtering in getItemsPublishers
Goffi <goffi@goffi.org>
parents: 384
diff changeset
1575 WHERE node_id=%s AND item=%s""",
fba96e95e329 storage (pgsql): fixed filtering in getItemsPublishers
Goffi <goffi@goffi.org>
parents: 384
diff changeset
1576 (self.nodeDbId, itemIdentifier,))
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1577 result = cursor.fetchone()
308
087b705493a6 fixed publisher check on item publishing
Goffi <goffi@goffi.org>
parents: 301
diff changeset
1578 if result:
087b705493a6 fixed publisher check on item publishing
Goffi <goffi@goffi.org>
parents: 301
diff changeset
1579 ret[itemIdentifier] = jid.JID(result[0])
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1580 return ret
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1581
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
1582 def purge(self):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1583 return self.dbpool.runInteraction(self._purge)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
1584
127
d3689da18ed2 Don't use encode('utf-8') on serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 124
diff changeset
1585 def _purge(self, cursor):
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
1586 self._checkNodeExists(cursor)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
1587
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
1588 cursor.execute("""DELETE FROM items WHERE
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1589 node_id=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
1590 (self.nodeDbId,))
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
1591
263
9dfd3890e646 added the constant FLAG_RETRACT_ALLOW_PUBLISHER to allow a publisher to retract an item he has published in a node of "open" publish model.
souliane <souliane@mailoo.org>
parents: 262
diff changeset
1592
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
1593 class CollectionNode(Node):
146
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
1594
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
1595 nodeType = 'collection'