annotate sat_pubsub/pgsql_storage.py @ 311:a776544d84e5

copyright update
author Goffi <goffi@goffi.org>
date Sun, 03 Jan 2016 18:28:34 +0100
parents 890b24b37b56
children 5d7c3787672e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
1 #!/usr/bin/python
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
2 #-*- coding: utf-8 -*-
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
3
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
4 """
311
a776544d84e5 copyright update
Goffi <goffi@goffi.org>
parents: 309
diff changeset
5 Copyright (c) 2012-2016 Jérôme Poisson
a776544d84e5 copyright update
Goffi <goffi@goffi.org>
parents: 309
diff changeset
6 Copyright (c) 2013-2016 Adrien Cossa
235
64f780413b82 fixed Ralph Meijer copyright years (last commit was in 2011, not in 2009)
Goffi <goffi@goffi.org>
parents: 234
diff changeset
7 Copyright (c) 2003-2011 Ralph Meijer
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
8
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
9
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
10 This program is free software: you can redistribute it and/or modify
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
11 it under the terms of the GNU Affero General Public License as published by
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
12 the Free Software Foundation, either version 3 of the License, or
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
13 (at your option) any later version.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
14
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
15 This program is distributed in the hope that it will be useful,
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
18 GNU Affero General Public License for more details.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
19
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
20 You should have received a copy of the GNU Affero General Public License
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
22 --
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
23
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
24 This program is based on Idavoll (http://idavoll.ik.nu/),
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
25 originaly written by Ralph Meijer (http://ralphm.net/blog/)
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
26 It is sublicensed under AGPL v3 (or any later version) as allowed by the original
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
27 license.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
28
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
29 --
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
30
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
31 Here is a copy of the original license:
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
32
235
64f780413b82 fixed Ralph Meijer copyright years (last commit was in 2011, not in 2009)
Goffi <goffi@goffi.org>
parents: 234
diff changeset
33 Copyright (c) 2003-2011 Ralph Meijer
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
34
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
35 Permission is hereby granted, free of charge, to any person obtaining
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
36 a copy of this software and associated documentation files (the
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
37 "Software"), to deal in the Software without restriction, including
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
38 without limitation the rights to use, copy, modify, merge, publish,
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
39 distribute, sublicense, and/or sell copies of the Software, and to
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
40 permit persons to whom the Software is furnished to do so, subject to
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
41 the following conditions:
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
42
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
43 The above copyright notice and this permission notice shall be
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
44 included in all copies or substantial portions of the Software.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
45
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
46 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
47 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
48 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
49 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
50 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
51 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
52 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
53
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
54 """
155
5191ba7c4df8 Work towards first release 0.5.0.
Ralph Meijer <ralphm@ik.nu>
parents: 148
diff changeset
55
262
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
56 import copy, logging
202
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
57
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
58 from zope.interface import implements
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
59
243
42048e37699e added experimental roster access_model (use remote_roster)
Goffi <goffi@goffi.org>
parents: 240
diff changeset
60 from twisted.internet import defer
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
61 from twisted.words.protocols.jabber import jid
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
62
271
232002e132db pgsql_backend unicode fix: parseXml was expecting str and getting unicode
Goffi <goffi@goffi.org>
parents: 270
diff changeset
63 from wokkel import generic
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
64 from wokkel.pubsub import Subscription
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
65
250
eb14b8d30cba fine tuning per-item permissions
Goffi <goffi@goffi.org>
parents: 249
diff changeset
66 from sat_pubsub import error, iidavoll, const
262
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
67 import psycopg2
270
f17034e4cf4a fixed unicode handling with psycopg2
Goffi <goffi@goffi.org>
parents: 265
diff changeset
68 import psycopg2.extensions
271
232002e132db pgsql_backend unicode fix: parseXml was expecting str and getting unicode
Goffi <goffi@goffi.org>
parents: 270
diff changeset
69 # we wants psycopg2 to return us unicode, not str
270
f17034e4cf4a fixed unicode handling with psycopg2
Goffi <goffi@goffi.org>
parents: 265
diff changeset
70 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
f17034e4cf4a fixed unicode handling with psycopg2
Goffi <goffi@goffi.org>
parents: 265
diff changeset
71 psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
244
3ecc94407e36 item access_model (not finished)
Goffi <goffi@goffi.org>
parents: 243
diff changeset
72
271
232002e132db pgsql_backend unicode fix: parseXml was expecting str and getting unicode
Goffi <goffi@goffi.org>
parents: 270
diff changeset
73 # 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
74 parseXml = lambda unicode_data: generic.parseXml(unicode_data.encode('utf-8'))
232002e132db pgsql_backend unicode fix: parseXml was expecting str and getting unicode
Goffi <goffi@goffi.org>
parents: 270
diff changeset
75
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
76
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
77 def withPEP(query, values, pep, recipient, pep_table=None):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
78 """Helper method to facilitate PEP management
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
79
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
80 @param query: SQL query basis
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
81 @param values: current values to replace in query
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
82 @param pep: True if we are in PEP mode
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
83 @param recipient: jid of the recipient
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
84 @param pep_table: added before pep if table need to be specified
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
85 @return: query + PEP AND check,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
86 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
87 """
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
88 pep_col_name = "{}pep".format(
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
89 '' if pep_table is None
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
90 else ".{}".format(pep_table))
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
91 if pep:
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
92 pep_check="AND {}=%s".format(pep_col_name)
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
93 values=list(values) + [recipient.userhost()]
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
94 else:
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
95 pep_check="AND {} IS NULL".format(pep_col_name)
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
96 return "{} {}".format(query, pep_check), values
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
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
99 class Storage:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
100
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
101 implements(iidavoll.IStorage)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
102
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
103 defaultConfig = {
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
104 'leaf': {
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
105 const.OPT_PERSIST_ITEMS: True,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
106 const.OPT_DELIVER_PAYLOADS: True,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
107 const.OPT_SEND_LAST_PUBLISHED_ITEM: 'on_sub',
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
108 const.OPT_ACCESS_MODEL: const.VAL_AMODEL_DEFAULT,
260
f0cd02c032b3 publish model management
Goffi <goffi@goffi.org>
parents: 259
diff changeset
109 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
110 },
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
111 'collection': {
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
112 const.OPT_DELIVER_PAYLOADS: True,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
113 const.OPT_SEND_LAST_PUBLISHED_ITEM: 'on_sub',
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
114 const.OPT_ACCESS_MODEL: const.VAL_AMODEL_DEFAULT,
260
f0cd02c032b3 publish model management
Goffi <goffi@goffi.org>
parents: 259
diff changeset
115 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
116 }
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
117 }
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
118
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
119 def __init__(self, dbpool):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
120 self.dbpool = dbpool
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
121
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
122 def _buildNode(self, row):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
123 """Build a note class from database result row"""
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
124 configuration = {}
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
125
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
126 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
127 raise error.NodeNotFound()
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
128
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
129 if row[2] == 'leaf':
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
130 configuration = {
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
131 'pubsub#persist_items': row[3],
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
132 'pubsub#deliver_payloads': row[4],
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
133 'pubsub#send_last_published_item': row[5],
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
134 const.OPT_ACCESS_MODEL:row[6],
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
135 const.OPT_PUBLISH_MODEL:row[7],
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
136 }
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
137 node = LeafNode(row[0], row[1], configuration)
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
138 node.dbpool = self.dbpool
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
139 return node
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
140 elif row[2] == 'collection':
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
141 configuration = {
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
142 'pubsub#deliver_payloads': row[4],
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
143 'pubsub#send_last_published_item': row[5],
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
144 const.OPT_ACCESS_MODEL: row[6],
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
145 const.OPT_PUBLISH_MODEL:row[7],
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
146 }
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
147 node = CollectionNode(row[0], row[1], configuration)
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
148 node.dbpool = self.dbpool
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
149 return node
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
150 else:
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
151 raise ValueError("Unknown node type !")
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
152
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
153 def getNodeById(self, nodeDbId):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
154 """Get node using database ID insted of pubsub identifier
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
155
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
156 @param nodeDbId(unicode): database ID
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
157 """
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
158 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
159
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
160
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
161 def _getNodeById(self, cursor, nodeDbId):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
162 cursor.execute("""SELECT node_id,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
163 node,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
164 node_type,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
165 persist_items,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
166 deliver_payloads,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
167 send_last_published_item,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
168 access_model,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
169 publish_model,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
170 pep
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
171 FROM nodes
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
172 WHERE node_id=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
173 (nodeDbId,))
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
174 row = cursor.fetchone()
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
175 return self._buildNode(row)
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
176
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
177 def getNode(self, nodeIdentifier, pep, recipient=None):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
178 return self.dbpool.runInteraction(self._getNode, nodeIdentifier, pep, recipient)
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
179
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
180
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
181 def _getNode(self, cursor, nodeIdentifier, pep, recipient):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
182 cursor.execute(*withPEP("""SELECT node_id,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
183 node,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
184 node_type,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
185 persist_items,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
186 deliver_payloads,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
187 send_last_published_item,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
188 access_model,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
189 publish_model,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
190 pep
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
191 FROM nodes
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
192 WHERE node=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
193 (nodeIdentifier,), pep, recipient))
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
194 row = cursor.fetchone()
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
195 return self._buildNode(row)
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
196
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
197 def getNodeIds(self, pep):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
198 d = self.dbpool.runQuery("""SELECT node from nodes WHERE pep is {}NULL"""
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
199 .format("NOT " if pep else ""))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
200 d.addCallback(lambda results: [r[0] for r in results])
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
201 return d
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
202
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
203
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
204 def createNode(self, nodeIdentifier, owner, config, pep, recipient=None):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
205 return self.dbpool.runInteraction(self._createNode, nodeIdentifier,
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
206 owner, config, pep, recipient)
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
207
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
208
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
209 def _createNode(self, cursor, nodeIdentifier, owner, config, pep, recipient):
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
210 if config['pubsub#node_type'] != 'leaf':
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
211 raise error.NoCollections()
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
212
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
213 owner = owner.userhost()
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
214
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
215 try:
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
216 cursor.execute("""INSERT INTO nodes
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
217 (node, node_type, persist_items,
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
218 deliver_payloads, send_last_published_item, access_model, publish_model, pep)
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
219 VALUES
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
220 (%s, 'leaf', %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
221 (nodeIdentifier,
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
222 config['pubsub#persist_items'],
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
223 config['pubsub#deliver_payloads'],
240
70c8bb90d75f added access_model to config, default to 'open'
Goffi <goffi@goffi.org>
parents: 235
diff changeset
224 config['pubsub#send_last_published_item'],
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
225 config[const.OPT_ACCESS_MODEL],
260
f0cd02c032b3 publish model management
Goffi <goffi@goffi.org>
parents: 259
diff changeset
226 config[const.OPT_PUBLISH_MODEL],
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
227 recipient.userhost() if pep else None
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
228 )
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
229 )
227
8540825f85e0 Replaced unmaintained pyPgSQL by Psycopg 2
Goffi <goffi@goffi.org>
parents: 206
diff changeset
230 except cursor._pool.dbapi.IntegrityError:
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
231 raise error.NodeExists()
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
232
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
233 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
234 (nodeIdentifier,), pep, recipient));
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
235 node_id = cursor.fetchone()[0]
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
236
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
237 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
238 (owner,))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
239
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
240 if not cursor.fetchone():
262
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
241 # 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
242 # 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
243 # 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
244 # "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
245 # 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
246 # about this issue: http://cssmay.com/question/tag/tag-psycopg2
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
247 cursor._connection.commit()
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
248 try:
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
249 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
250 (owner,))
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
251 except psycopg2.IntegrityError as e:
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
252 cursor._connection.rollback()
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
253 logging.warning("during node creation: %s" % e.message)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
254
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
255 cursor.execute("""INSERT INTO affiliations
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
256 (node_id, entity_id, affiliation)
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
257 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
258 (SELECT entity_id FROM entities
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
259 WHERE jid=%s) as e""",
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
260 (node_id, owner))
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
261
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
262 #TODO: manage JID access
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
263 if config[const.OPT_ACCESS_MODEL] == const.VAL_AMODEL_ROSTER:
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
264 if const.OPT_ROSTER_GROUPS_ALLOWED in config:
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
265 allowed_groups = config[const.OPT_ROSTER_GROUPS_ALLOWED]
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
266 else:
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
267 allowed_groups = []
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
268 for group in allowed_groups:
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
269 #TODO: check that group are actually in roster
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
270 cursor.execute("""INSERT INTO node_groups_authorized (node_id, groupname)
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
271 VALUES (%s,%s)""" , (node_id, group))
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
272
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
273 def deleteNodeByDbId(self, db_id):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
274 """Delete a node using directly its database id"""
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
275 return self.dbpool.runInteraction(self._deleteNodeByDbId, db_id)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
276
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
277 def _deleteNodeByDbId(self, cursor, db_id):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
278 cursor.execute("""DELETE FROM nodes WHERE node_id=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
279 (db_id,))
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
280
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
281 if cursor.rowcount != 1:
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
282 raise error.NodeNotFound()
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
283
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
284 def deleteNode(self, nodeIdentifier, pep, recipient=None):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
285 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
286
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
287
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
288 def _deleteNode(self, cursor, nodeIdentifier, pep, recipient):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
289 cursor.execute(*withPEP("""DELETE FROM nodes WHERE node=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
290 (nodeIdentifier,), pep, recipient))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
291
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
292 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
293 raise error.NodeNotFound()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
294
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
295 def getNodeGroups(self, nodeIdentifier, pep, recipient=None):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
296 return self.dbpool.runInteraction(self._getNodeGroups, nodeIdentifier, pep, recipient)
243
42048e37699e added experimental roster access_model (use remote_roster)
Goffi <goffi@goffi.org>
parents: 240
diff changeset
297
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
298 def _getNodeGroups(self, cursor, nodeIdentifier, pep, recipient):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
299 cursor.execute(*withPEP("SELECT groupname FROM node_groups_authorized NATURAL JOIN nodes WHERE node=%s",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
300 (nodeIdentifier,), pep, recipient))
243
42048e37699e added experimental roster access_model (use remote_roster)
Goffi <goffi@goffi.org>
parents: 240
diff changeset
301 rows = cursor.fetchall()
42048e37699e added experimental roster access_model (use remote_roster)
Goffi <goffi@goffi.org>
parents: 240
diff changeset
302
42048e37699e added experimental roster access_model (use remote_roster)
Goffi <goffi@goffi.org>
parents: 240
diff changeset
303 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
304
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
305 def getAffiliations(self, entity, pep, recipient=None):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
306 d = self.dbpool.runQuery(*withPEP("""SELECT node, affiliation FROM entities
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
307 NATURAL JOIN affiliations
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
308 NATURAL JOIN nodes
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
309 WHERE jid=%s""",
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
310 (entity.userhost(),), pep, recipient, 'nodes'))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
311 d.addCallback(lambda results: [tuple(r) for r in results])
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
312 return d
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
313
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
314
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
315 def getSubscriptions(self, entity, pep, recipient=None):
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
316 def toSubscriptions(rows):
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
317 subscriptions = []
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
318 for row in rows:
227
8540825f85e0 Replaced unmaintained pyPgSQL by Psycopg 2
Goffi <goffi@goffi.org>
parents: 206
diff changeset
319 subscriber = jid.internJID('%s/%s' % (row[1],
8540825f85e0 Replaced unmaintained pyPgSQL by Psycopg 2
Goffi <goffi@goffi.org>
parents: 206
diff changeset
320 row[2]))
8540825f85e0 Replaced unmaintained pyPgSQL by Psycopg 2
Goffi <goffi@goffi.org>
parents: 206
diff changeset
321 subscription = Subscription(row[0], subscriber, row[3])
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
322 subscriptions.append(subscription)
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
323 return subscriptions
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
324
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
325 d = self.dbpool.runQuery("""SELECT node, jid, resource, state
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
326 FROM entities
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
327 NATURAL JOIN subscriptions
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
328 NATURAL JOIN nodes
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
329 WHERE jid=%s AND nodes.pep=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
330 (entity.userhost(), recipient.userhost() if pep else None))
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
331 d.addCallback(toSubscriptions)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
332 return d
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
333
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
334
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
335 def getDefaultConfiguration(self, nodeType):
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
336 return self.defaultConfig[nodeType]
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
337
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 170
diff changeset
338
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
339
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
340 class Node:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
341
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
342 implements(iidavoll.INode)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
343
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
344 def __init__(self, nodeDbId, nodeIdentifier, config):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
345 self.nodeDbId = nodeDbId
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
346 self.nodeIdentifier = nodeIdentifier
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
347 self._config = config
243
42048e37699e added experimental roster access_model (use remote_roster)
Goffi <goffi@goffi.org>
parents: 240
diff changeset
348 self.owner = None;
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
349
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
350
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
351 def _checkNodeExists(self, cursor):
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
352 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
353 (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
354 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
355 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
356
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
357
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
358 def getType(self):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
359 return self.nodeType
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
360
243
42048e37699e added experimental roster access_model (use remote_roster)
Goffi <goffi@goffi.org>
parents: 240
diff changeset
361 def getNodeOwner(self):
42048e37699e added experimental roster access_model (use remote_roster)
Goffi <goffi@goffi.org>
parents: 240
diff changeset
362 if self.owner:
42048e37699e added experimental roster access_model (use remote_roster)
Goffi <goffi@goffi.org>
parents: 240
diff changeset
363 return defer.succeed(self.owner)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
364 d = self.dbpool.runQuery("""SELECT jid FROM nodes NATURAL JOIN affiliations NATURAL JOIN entities WHERE node_id=%s""", (self.nodeDbId,))
243
42048e37699e added experimental roster access_model (use remote_roster)
Goffi <goffi@goffi.org>
parents: 240
diff changeset
365 d.addCallback(lambda result: jid.JID(result[0][0]))
42048e37699e added experimental roster access_model (use remote_roster)
Goffi <goffi@goffi.org>
parents: 240
diff changeset
366 return d
42048e37699e added experimental roster access_model (use remote_roster)
Goffi <goffi@goffi.org>
parents: 240
diff changeset
367
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
368
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
369 def getConfiguration(self):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
370 return self._config
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
371
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
372
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
373 def setConfiguration(self, 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
374 config = copy.copy(self._config)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
375
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
376 for option in 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
377 if option in config:
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
378 config[option] = options[option]
180
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
379
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
380 d = self.dbpool.runInteraction(self._setConfiguration, config)
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
381 d.addCallback(self._setCachedConfiguration, config)
124
c4ee16bc48e5 Change Node.set_configuration() to set cached configuration in a callback.
Ralph Meijer <ralphm@ik.nu>
parents: 121
diff changeset
382 return d
c4ee16bc48e5 Change Node.set_configuration() to set cached configuration in a callback.
Ralph Meijer <ralphm@ik.nu>
parents: 121
diff changeset
383
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
384
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
385 def _setConfiguration(self, cursor, config):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
386 self._checkNodeExists(cursor)
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
387 cursor.execute("""UPDATE nodes SET persist_items=%s,
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
388 deliver_payloads=%s,
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
389 send_last_published_item=%s,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
390 access_model=%s,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
391 publish_model=%s
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
392 WHERE node_id=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
393 (config[const.OPT_PERSIST_ITEMS],
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
394 config[const.OPT_DELIVER_PAYLOADS],
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
395 config[const.OPT_SEND_LAST_PUBLISHED_ITEM],
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
396 config[const.OPT_ACCESS_MODEL],
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
397 config[const.OPT_PUBLISH_MODEL],
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
398 self.nodeDbId))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
399
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
400
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
401 def _setCachedConfiguration(self, void, 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
402 self._config = config
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
403
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
404
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
405 def getMetaData(self):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
406 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
407 config["pubsub#node_type"] = self.nodeType
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
408 return config
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
409
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
410
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
411 def getAffiliation(self, entity):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
412 return self.dbpool.runInteraction(self._getAffiliation, entity)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
413
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
414
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
415 def _getAffiliation(self, cursor, entity):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
416 self._checkNodeExists(cursor)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
417 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
418 NATURAL JOIN nodes
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
419 NATURAL JOIN entities
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
420 WHERE node_id=%s AND jid=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
421 (self.nodeDbId,
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
422 entity.userhost()))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
423
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
424 try:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
425 return cursor.fetchone()[0]
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
426 except TypeError:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
427 return None
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
428
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
429
240
70c8bb90d75f added access_model to config, default to 'open'
Goffi <goffi@goffi.org>
parents: 235
diff changeset
430 def getAccessModel(self):
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
431 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
432
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
433
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
434 def getSubscription(self, subscriber):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
435 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
436
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
437
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
438 def _getSubscription(self, cursor, subscriber):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
439 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
440
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
441 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
442 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
443
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
444 cursor.execute("""SELECT state FROM subscriptions
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
445 NATURAL JOIN nodes
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
446 NATURAL JOIN entities
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
447 WHERE node_id=%s AND jid=%s AND resource=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
448 (self.nodeDbId,
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
449 userhost,
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
450 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
451
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
452 row = cursor.fetchone()
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
453 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
454 return None
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
455 else:
227
8540825f85e0 Replaced unmaintained pyPgSQL by Psycopg 2
Goffi <goffi@goffi.org>
parents: 206
diff changeset
456 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
457
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
458
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
459 def getSubscriptions(self, state=None):
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
460 return self.dbpool.runInteraction(self._getSubscriptions, state)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
461
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
462
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
463 def _getSubscriptions(self, cursor, state):
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
464 self._checkNodeExists(cursor)
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
465
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
466 query = """SELECT node, jid, resource, state,
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
467 subscription_type, subscription_depth
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
468 FROM subscriptions
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
469 NATURAL JOIN nodes
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
470 NATURAL JOIN entities
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
471 WHERE node_id=%s"""
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
472 values = [self.nodeDbId]
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
473
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
474 if state:
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
475 query += " AND state=%s"
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
476 values.append(state)
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
477
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
478 cursor.execute(query, values)
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
479 rows = cursor.fetchall()
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
480
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
481 subscriptions = []
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
482 for row in rows:
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
483 subscriber = jid.JID(u'%s/%s' % (row[1], row[2]))
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
484
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
485 options = {}
227
8540825f85e0 Replaced unmaintained pyPgSQL by Psycopg 2
Goffi <goffi@goffi.org>
parents: 206
diff changeset
486 if row[4]:
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
487 options['pubsub#subscription_type'] = row[4];
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
488 if row[5]:
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
489 options['pubsub#subscription_depth'] = row[5];
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
490
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
491 subscriptions.append(Subscription(row[0], subscriber,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
492 row[3], options))
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
493
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
494 return subscriptions
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
495
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
496
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
497 def addSubscription(self, subscriber, state, config):
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
498 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
499 state, config)
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
500
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
501
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
502 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
503 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
504
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
505 userhost = subscriber.userhost()
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
506 resource = subscriber.resource or ''
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
507
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
508 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
509 subscription_depth = config.get('pubsub#subscription_depth')
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
510
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
511 try:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
512 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
227
8540825f85e0 Replaced unmaintained pyPgSQL by Psycopg 2
Goffi <goffi@goffi.org>
parents: 206
diff changeset
513 (userhost,))
8540825f85e0 Replaced unmaintained pyPgSQL by Psycopg 2
Goffi <goffi@goffi.org>
parents: 206
diff changeset
514 except cursor._pool.dbapi.IntegrityError:
8540825f85e0 Replaced unmaintained pyPgSQL by Psycopg 2
Goffi <goffi@goffi.org>
parents: 206
diff changeset
515 cursor._connection.rollback()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
516
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
517 try:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
518 cursor.execute("""INSERT INTO subscriptions
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
519 (node_id, entity_id, resource, state,
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
520 subscription_type, subscription_depth)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
521 SELECT %s, entity_id, %s, %s, %s, %s FROM
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
522 (SELECT entity_id FROM entities
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
523 WHERE jid=%s) AS ent_id""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
524 (self.nodeDbId,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
525 resource,
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
526 state,
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
527 subscription_type,
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
528 subscription_depth,
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
529 userhost))
227
8540825f85e0 Replaced unmaintained pyPgSQL by Psycopg 2
Goffi <goffi@goffi.org>
parents: 206
diff changeset
530 except cursor._pool.dbapi.IntegrityError:
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
531 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
532
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
533
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
534 def removeSubscription(self, subscriber):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
535 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
536 subscriber)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
537
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
538
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
539 def _removeSubscription(self, cursor, subscriber):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
540 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
541
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
542 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
543 resource = subscriber.resource or ''
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
544
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
545 cursor.execute("""DELETE FROM subscriptions WHERE
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
546 node_id=%s AND
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
547 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
548 WHERE jid=%s) AND
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
549 resource=%s""",
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
550 (self.nodeDbId,
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
551 userhost,
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
552 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
553 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
554 raise error.NotSubscribed()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
555
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
556 return None
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
557
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
558
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
559 def isSubscribed(self, entity):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
560 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
561
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
562
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
563 def _isSubscribed(self, cursor, entity):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
564 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
565
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
566 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
567 NATURAL JOIN subscriptions
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
568 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
569 WHERE entities.jid=%s
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
570 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
571 (entity.userhost(),
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
572 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
573
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
574 return cursor.fetchone() is not None
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
575
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
576
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
577 def getAffiliations(self):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
578 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
579
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
580
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
581 def _getAffiliations(self, cursor):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
582 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
583
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
584 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
585 NATURAL JOIN affiliations
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
586 NATURAL JOIN entities
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
587 WHERE node_id=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
588 (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
589 result = cursor.fetchall()
180
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
590
148
b03e5ad81173 Change all calls to jid.JID to jid.internJID to avoid redoing stringprep.
Ralph Meijer <ralphm@ik.nu>
parents: 146
diff changeset
591 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
592
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
593
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
594
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
595 class LeafNode(Node):
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
596
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
597 implements(iidavoll.ILeafNode)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
598
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
599 nodeType = 'leaf'
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
600
250
eb14b8d30cba fine tuning per-item permissions
Goffi <goffi@goffi.org>
parents: 249
diff changeset
601 def storeItems(self, item_data, publisher):
eb14b8d30cba fine tuning per-item permissions
Goffi <goffi@goffi.org>
parents: 249
diff changeset
602 return 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
603
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
604
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
605 def _storeItems(self, cursor, items_data, publisher):
250
eb14b8d30cba fine tuning per-item permissions
Goffi <goffi@goffi.org>
parents: 249
diff changeset
606 self._checkNodeExists(cursor)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
607 for item_data in items_data:
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
608 self._storeItem(cursor, item_data, publisher)
244
3ecc94407e36 item access_model (not finished)
Goffi <goffi@goffi.org>
parents: 243
diff changeset
609
250
eb14b8d30cba fine tuning per-item permissions
Goffi <goffi@goffi.org>
parents: 249
diff changeset
610
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
611 def _storeItem(self, cursor, item_data, publisher):
301
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
612 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
613 data = item.toXml()
262
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
614
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
615 cursor.execute("""UPDATE items SET date=now(), publisher=%s, data=%s
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
616 FROM nodes
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
617 WHERE nodes.node_id = items.node_id AND
301
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
618 nodes.node_id = %s and items.item=%s
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
619 RETURNING item_id""",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
620 (publisher.full(),
127
d3689da18ed2 Don't use encode('utf-8') on serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 124
diff changeset
621 data,
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
622 self.nodeDbId,
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
623 item["id"]))
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
624 if cursor.rowcount == 1:
301
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
625 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
626 self._storeCategories(cursor, item_id, item_data.categories, update=True)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
627 return
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
628
244
3ecc94407e36 item access_model (not finished)
Goffi <goffi@goffi.org>
parents: 243
diff changeset
629 cursor.execute("""INSERT INTO items (node_id, item, publisher, data, access_model)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
630 SELECT %s, %s, %s, %s, %s FROM nodes
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
631 WHERE node_id=%s
245
e11e99246be5 allowed groups from item_config are now stored
Goffi <goffi@goffi.org>
parents: 244
diff changeset
632 RETURNING item_id""",
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
633 (self.nodeDbId,
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
634 item["id"],
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
635 publisher.full(),
127
d3689da18ed2 Don't use encode('utf-8') on serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 124
diff changeset
636 data,
244
3ecc94407e36 item access_model (not finished)
Goffi <goffi@goffi.org>
parents: 243
diff changeset
637 access_model,
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
638 self.nodeDbId))
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
639
301
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
640 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
641 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
642
259
6fe7da6b4b32 node "roster" access model management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
643 if access_model == const.VAL_AMODEL_ROSTER:
250
eb14b8d30cba fine tuning per-item permissions
Goffi <goffi@goffi.org>
parents: 249
diff changeset
644 if const.OPT_ROSTER_GROUPS_ALLOWED in item_config:
eb14b8d30cba fine tuning per-item permissions
Goffi <goffi@goffi.org>
parents: 249
diff changeset
645 item_config.fields[const.OPT_ROSTER_GROUPS_ALLOWED].fieldType='list-multi' #XXX: needed to force list if there is only one value
eb14b8d30cba fine tuning per-item permissions
Goffi <goffi@goffi.org>
parents: 249
diff changeset
646 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
647 else:
e11e99246be5 allowed groups from item_config are now stored
Goffi <goffi@goffi.org>
parents: 244
diff changeset
648 allowed_groups = []
e11e99246be5 allowed groups from item_config are now stored
Goffi <goffi@goffi.org>
parents: 244
diff changeset
649 for group in allowed_groups:
e11e99246be5 allowed groups from item_config are now stored
Goffi <goffi@goffi.org>
parents: 244
diff changeset
650 #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
651 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
652 VALUES (%s,%s)""" , (item_id, group))
e11e99246be5 allowed groups from item_config are now stored
Goffi <goffi@goffi.org>
parents: 244
diff changeset
653
301
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
654 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
655 # 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
656 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
657 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
658 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
659
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
660 for category in categories:
05c875a13a62 categories are now stored in a dedicated table if item contain an atom entry:
Goffi <goffi@goffi.org>
parents: 297
diff changeset
661 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
662 VALUES (%s, %s)""", (item_id, category))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
663
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
664 def removeItems(self, itemIdentifiers):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
665 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
666
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
667
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
668 def _removeItems(self, cursor, itemIdentifiers):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
669 self._checkNodeExists(cursor)
180
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
670
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
671 deleted = []
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
672
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
673 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
674 cursor.execute("""DELETE FROM items WHERE
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
675 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
676 item=%s""",
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
677 (self.nodeDbId,
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
678 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
679
142
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 136
diff changeset
680 if cursor.rowcount:
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
681 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
682
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 136
diff changeset
683 return deleted
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
684
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
685
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
686 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
687 """ Get all authorised items
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
688 @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
689 @param unrestricted: if true, don't check permissions (i.e.: get all items)
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
690 @param maxItems: nb of items we want to tget
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
691 @param rsm_data: options for RSM feature handling (XEP-0059) as a
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
692 dictionnary of C{unicode} to C{unicode}.
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
693
265
89493845d3dd fix the method signatures of iidavoll.ILeafNode and memory_storage.LeafNode according to pgsql_storage.LeafNode
souliane <souliane@mailoo.org>
parents: 263
diff changeset
694 @return: list of (item, access_model, id) if unrestricted is True, else list of 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
695 """
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
696 if ext_data is None:
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
697 ext_data = {}
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
698 return self.dbpool.runInteraction(self._getItems, authorized_groups, unrestricted, maxItems, ext_data)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
699
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
700 def _getItems(self, cursor, authorized_groups, unrestricted, maxItems, ext_data):
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
701 # FIXME: simplify the query construction
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
702 self._checkNodeExists(cursor)
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
703
248
50f6ee966da8 item are gotten according to item's access model in getItems
Goffi <goffi@goffi.org>
parents: 247
diff changeset
704 if unrestricted:
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
705 query = ["SELECT data,items.access_model,item_id"]
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
706 source = """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
707 INNER JOIN items USING (node_id)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
708 WHERE node_id=%s"""
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
709 args = [self.nodeDbId]
248
50f6ee966da8 item are gotten according to item's access model in getItems
Goffi <goffi@goffi.org>
parents: 247
diff changeset
710 else:
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
711 query = ["SELECT data"]
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
712 groups = " or (items.access_model='roster' and groupname in %s)" if authorized_groups else ""
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
713 source = """FROM nodes
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
714 INNER JOIN items USING (node_id)
248
50f6ee966da8 item are gotten according to item's access model in getItems
Goffi <goffi@goffi.org>
parents: 247
diff changeset
715 LEFT JOIN item_groups_authorized USING (item_id)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
716 WHERE node_id=%s AND
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
717 (items.access_model='open'""" + groups + ")"
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
718
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
719 args = [self.nodeDbId]
249
aaf5e34ff765 fixed getItems when there are not authorized_groups + fixed default value for StoreItem
Goffi <goffi@goffi.org>
parents: 248
diff changeset
720 if authorized_groups:
aaf5e34ff765 fixed getItems when there are not authorized_groups + fixed default value for StoreItem
Goffi <goffi@goffi.org>
parents: 248
diff changeset
721 args.append(authorized_groups)
248
50f6ee966da8 item are gotten according to item's access model in getItems
Goffi <goffi@goffi.org>
parents: 247
diff changeset
722
282
7d54ff2eeaf2 actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents: 278
diff changeset
723 if 'filters' in ext_data: # MAM filters
7d54ff2eeaf2 actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents: 278
diff changeset
724 for filter_ in ext_data['filters']:
7d54ff2eeaf2 actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents: 278
diff changeset
725 if filter_.var == 'start':
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
726 source += " AND date>=%s"
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
727 args.append(filter_.value)
282
7d54ff2eeaf2 actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents: 278
diff changeset
728 if filter_.var == 'end':
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
729 source += " AND date<=%s"
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
730 args.append(filter_.value)
282
7d54ff2eeaf2 actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents: 278
diff changeset
731 if filter_.var == 'with':
7d54ff2eeaf2 actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents: 278
diff changeset
732 jid_s = filter_.value
7d54ff2eeaf2 actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents: 278
diff changeset
733 if '/' in jid_s:
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
734 source += " AND publisher=%s"
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
735 args.append(filter_.value)
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
736 else:
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
737 source += " AND publisher LIKE %s"
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
738 args.append(u"{}%".format(filter_.value))
282
7d54ff2eeaf2 actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents: 278
diff changeset
739
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
740 query.append(source)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
741 order = "DESC"
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
742
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
743 if 'rsm' in ext_data:
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
744 rsm = ext_data['rsm']
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
745 maxItems = rsm.max
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
746 if rsm.index is not None:
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
747 query.append("AND date<=(SELECT date " + source + " ORDER BY date DESC LIMIT 1 OFFSET %s)")
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
748 # FIXME: change the request so source is not used 2 times
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
749 # there is already a placeholder in source with node_id=%s, so we need to add self.noDbId in args
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
750 args.append(self.nodeDbId)
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
751 if authorized_groups:
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
752 args.append(authorized_groups)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
753 args.append(rsm.index)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
754 elif rsm.before is not None:
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
755 order = "ASC"
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
756 if rsm.before != '':
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
757 query.append("AND date>(SELECT date FROM items WHERE item=%s LIMIT 1)")
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
758 args.append(rsm.before)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
759 elif rsm.after:
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
760 query.append("AND date<(SELECT date FROM items WHERE item=%s LIMIT 1)")
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
761 args.append(rsm.after)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
762
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
763 query.append("ORDER BY date %s" % order)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
764
297
4115999d85e9 fixes behavior when maxItems is 0
souliane <souliane@mailoo.org>
parents: 294
diff changeset
765 if maxItems is not None:
248
50f6ee966da8 item are gotten according to item's access model in getItems
Goffi <goffi@goffi.org>
parents: 247
diff changeset
766 query.append("LIMIT %s")
50f6ee966da8 item are gotten according to item's access model in getItems
Goffi <goffi@goffi.org>
parents: 247
diff changeset
767 args.append(maxItems)
262
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
768
248
50f6ee966da8 item are gotten according to item's access model in getItems
Goffi <goffi@goffi.org>
parents: 247
diff changeset
769 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
770
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
771 result = 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
772 if unrestricted:
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
773 ret = []
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
774 for data in result:
271
232002e132db pgsql_backend unicode fix: parseXml was expecting str and getting unicode
Goffi <goffi@goffi.org>
parents: 270
diff changeset
775 item = generic.stripNamespace(parseXml(data[0]))
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
776 access_model = data[1]
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
777 item_id = data[2]
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
778 access_list = {}
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
779 if access_model == const.VAL_AMODEL_ROSTER: #TODO: jid 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
780 cursor.execute('SELECT groupname FROM item_groups_authorized WHERE item_id=%s', (item_id,))
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
781 access_list[const.OPT_ROSTER_GROUPS_ALLOWED] = [r[0] 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
782
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
783 ret.append((item, access_model, access_list))
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
784 return ret
271
232002e132db pgsql_backend unicode fix: parseXml was expecting str and getting unicode
Goffi <goffi@goffi.org>
parents: 270
diff changeset
785 items = [generic.stripNamespace(parseXml(r[0])) for r in result]
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
786 return items
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
787
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
788 def countItems(self, authorized_groups, unrestricted):
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
789 """ Count the accessible items.
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
790
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
791 @param authorized_groups: we want to get items that these groups can access.
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
792 @param unrestricted: if true, don't check permissions (i.e.: get all items).
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
793 @return: deferred that fires a C{int}.
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
794 """
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
795 return self.dbpool.runInteraction(self._countItems, authorized_groups, unrestricted)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
796
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
797 def _countItems(self, cursor, authorized_groups, unrestricted):
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
798 self._checkNodeExists(cursor)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
799
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
800 if unrestricted:
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
801 query = ["""SELECT count(item_id) FROM nodes
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
802 INNER JOIN items USING (node_id)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
803 WHERE node_id=%s"""]
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
804 args = [self.nodeDbId]
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
805 else:
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
806 query = ["""SELECT count(item_id) FROM nodes
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
807 INNER JOIN items USING (node_id)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
808 LEFT JOIN item_groups_authorized USING (item_id)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
809 WHERE node_id=%s AND
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
810 (items.access_model='open' """ +
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
811 ("or (items.access_model='roster' and groupname in %s)" if authorized_groups else '') +
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
812 ")"]
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
813
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
814 args = [self.nodeDbId]
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
815 if authorized_groups:
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
816 args.append(authorized_groups)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
817
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
818 cursor.execute(' '.join(query), args)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
819 return cursor.fetchall()[0][0]
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
820
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
821 def getIndex(self, authorized_groups, unrestricted, item):
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
822 """ Retrieve the index of the given item within the accessible window.
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
823
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
824 @param authorized_groups: we want to get items that these groups can access.
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
825 @param unrestricted: if true, don't check permissions (i.e.: get all items).
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
826 @param item: item identifier.
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
827 @return: deferred that fires a C{int}.
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
828 """
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
829 return self.dbpool.runInteraction(self._getIndex, authorized_groups, unrestricted, item)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
830
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
831 def _getIndex(self, cursor, authorized_groups, unrestricted, item):
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
832 self._checkNodeExists(cursor)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
833
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
834 if unrestricted:
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
835 query = ["""SELECT row_number FROM (
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
836 SELECT row_number() OVER (ORDER BY date DESC), item
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
837 FROM nodes INNER JOIN items USING (node_id)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
838 WHERE node_id=%s
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
839 ) as x
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
840 WHERE item=%s LIMIT 1"""]
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
841 args = [self.nodeDbId]
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
842 else:
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
843 query = ["""SELECT row_number FROM (
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
844 SELECT row_number() OVER (ORDER BY date DESC), item
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
845 FROM nodes INNER JOIN items USING (node_id)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
846 LEFT JOIN item_groups_authorized USING (item_id)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
847 WHERE node_id=%s AND
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
848 (items.access_model='open' """ +
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
849 ("or (items.access_model='roster' and groupname in %s)" if authorized_groups else '') +
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
850 """)) as x
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
851 WHERE item=%s LIMIT 1"""]
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
852
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
853 args = [self.nodeDbId]
278
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
854 if authorized_groups:
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
855 args.append(authorized_groups)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
856
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
857 args.append(item)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
858 cursor.execute(' '.join(query), args)
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
859
8a71486c3e95 implements RSM (XEP-0059)
souliane <souliane@mailoo.org>
parents: 276
diff changeset
860 return cursor.fetchall()[0][0]
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
861
248
50f6ee966da8 item are gotten according to item's access model in getItems
Goffi <goffi@goffi.org>
parents: 247
diff changeset
862 def getItemsById(self, authorized_groups, unrestricted, itemIdentifiers):
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
863 """ Get items which are in the given list
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
864 @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
865 @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
866 @param itemIdentifiers: list of ids of the items we want to get
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
867 @return: list of (item, access_model, access_list) if unrestricted is True, else list of items
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
868 access_list is managed as a dictionnary with same key as for item_config
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
869 """
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
870 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
871
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
872
248
50f6ee966da8 item are gotten according to item's access model in getItems
Goffi <goffi@goffi.org>
parents: 247
diff changeset
873 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
874 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
875 ret = []
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
876 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
877 for itemIdentifier in itemIdentifiers:
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
878 cursor.execute("""SELECT data,items.access_model,item_id FROM nodes
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
879 INNER JOIN items USING (node_id)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
880 WHERE node_id=%s AND item=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
881 (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
882 itemIdentifier))
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
883 result = cursor.fetchone()
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
884 if not result:
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
885 raise error.ItemNotFound()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
886
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
887 item = generic.stripNamespace(parseXml(result[0]))
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
888 access_model = result[1]
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
889 item_id = result[2]
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
890 access_list = {}
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
891 if access_model == const.VAL_AMODEL_ROSTER: #TODO: jid access_model
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
892 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
893 access_list[const.OPT_ROSTER_GROUPS_ALLOWED] = [r[0] for r in cursor.fetchall()]
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
894
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
895 ret.append((item, access_model, access_list))
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
896 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
897 for itemIdentifier in itemIdentifiers:
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
898 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
899 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
900 args.append(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
901 cursor.execute("""SELECT data FROM nodes
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
902 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
903 LEFT JOIN item_groups_authorized USING (item_id)
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
904 WHERE node_id=%s AND item=%s AND
262
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
905 (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
906 ("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
907 args)
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
908
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
909 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
910 if result:
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
911 ret.append(generic.stripNamespace(parseXml(result[0])))
262
7b821432d012 fixed node auto-create feature
souliane <souliane@mailoo.org>
parents: 260
diff changeset
912
252
25a1dc7181cc full items, with item-configuration, are returned if items are asked by the owner
Goffi <goffi@goffi.org>
parents: 250
diff changeset
913 return ret
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
914
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
915
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
916 def getItemsPublishers(self, itemIdentifiers):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
917 """Get the publishers for all given identifiers
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
918
308
087b705493a6 fixed publisher check on item publishing
Goffi <goffi@goffi.org>
parents: 301
diff changeset
919 @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
920 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
921 """
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
922 return self.dbpool.runInteraction(self._getItemsPublishers, itemIdentifiers)
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
923
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
924
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
925 def _getItemsPublishers(self, cursor, itemIdentifiers):
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
926 self._checkNodeExists(cursor)
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
927 ret = {}
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
928 for itemIdentifier in itemIdentifiers:
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
929 cursor.execute("""SELECT publisher FROM items
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
930 WHERE item=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
931 (itemIdentifier,))
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
932 result = cursor.fetchone()
308
087b705493a6 fixed publisher check on item publishing
Goffi <goffi@goffi.org>
parents: 301
diff changeset
933 if result:
087b705493a6 fixed publisher check on item publishing
Goffi <goffi@goffi.org>
parents: 301
diff changeset
934 ret[itemIdentifier] = jid.JID(result[0])
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
935 return ret
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
936
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
937
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
938 def purge(self):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
939 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
940
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
941
127
d3689da18ed2 Don't use encode('utf-8') on serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 124
diff changeset
942 def _purge(self, cursor):
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
943 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
944
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
945 cursor.execute("""DELETE FROM items WHERE
294
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
946 node_id=%s""",
df1edebb0466 PEP implementation, draft (huge patch sorry):
Goffi <goffi@goffi.org>
parents: 283
diff changeset
947 (self.nodeDbId,))
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 181
diff changeset
948
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
949
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
950 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
951
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
952 nodeType = 'collection'
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
953
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
954
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
955
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
956 class GatewayStorage(object):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
957 """
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
958 Memory based storage facility for the XMPP-HTTP gateway.
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
959 """
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
960
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
961 def __init__(self, dbpool):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
962 self.dbpool = dbpool
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
963
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
964
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
965 def _countCallbacks(self, cursor, service, nodeIdentifier):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
966 """
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
967 Count number of callbacks registered for a node.
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
968 """
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
969 cursor.execute("""SELECT count(*) FROM callbacks
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
970 WHERE service=%s and node=%s""",
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
971 (service.full(),
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
972 nodeIdentifier))
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
973 results = cursor.fetchall()
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
974 return results[0][0]
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
975
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
976
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
977 def addCallback(self, service, nodeIdentifier, callback):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
978 def interaction(cursor):
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
979 cursor.execute("""SELECT 1 as bool FROM callbacks
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
980 WHERE service=%s and node=%s and uri=%s""",
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
981 (service.full(),
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
982 nodeIdentifier,
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
983 callback))
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
984 if cursor.fetchall():
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
985 return
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
986
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
987 cursor.execute("""INSERT INTO callbacks
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
988 (service, node, uri) VALUES
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
989 (%s, %s, %s)""",
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
990 (service.full(),
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
991 nodeIdentifier,
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
992 callback))
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
993
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
994 return self.dbpool.runInteraction(interaction)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
995
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
996
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
997 def removeCallback(self, service, nodeIdentifier, callback):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
998 def interaction(cursor):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
999 cursor.execute("""DELETE FROM callbacks
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1000 WHERE service=%s and node=%s and uri=%s""",
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
1001 (service.full(),
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
1002 nodeIdentifier,
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
1003 callback))
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1004
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1005 if cursor.rowcount != 1:
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1006 raise error.NotSubscribed()
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1007
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1008 last = not self._countCallbacks(cursor, service, nodeIdentifier)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1009 return last
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1010
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1011 return self.dbpool.runInteraction(interaction)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1012
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1013 def getCallbacks(self, service, nodeIdentifier):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1014 def interaction(cursor):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1015 cursor.execute("""SELECT uri FROM callbacks
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1016 WHERE service=%s and node=%s""",
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
1017 (service.full(),
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
1018 nodeIdentifier))
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1019 results = cursor.fetchall()
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1020
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1021 if not results:
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1022 raise error.NoCallbacks()
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1023
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1024 return [result[0] for result in results]
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1025
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1026 return self.dbpool.runInteraction(interaction)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1027
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1028
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1029 def hasCallbacks(self, service, nodeIdentifier):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1030 def interaction(cursor):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1031 return bool(self._countCallbacks(cursor, service, nodeIdentifier))
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1032
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
1033 return self.dbpool.runInteraction(interaction)