Mercurial > libervia-backend
annotate src/plugins/plugin_exp_events.py @ 2263:90189f312fd3
core (stdui): added a FIXME to not use plugin/default_host in the future
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 21 Jun 2017 19:47:38 +0200 |
parents | e8641b7718dc |
children | b5befe7722d3 |
rev | line source |
---|---|
2231 | 1 #!/usr/bin/env python2 |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT plugin to detect language (experimental) | |
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from sat.core.i18n import _ | |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
21 from sat.core import exceptions |
2231 | 22 from sat.core.constants import Const as C |
23 from sat.core.log import getLogger | |
24 log = getLogger(__name__) | |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
25 from sat.tools import utils |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
26 from sat.tools.common import uri as uri_parse |
2231 | 27 from twisted.internet import defer |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
28 from twisted.words.protocols.jabber import jid, error |
2231 | 29 from twisted.words.xish import domish |
30 from wokkel import pubsub | |
31 | |
32 | |
33 PLUGIN_INFO = { | |
34 C.PI_NAME: "Event plugin", | |
35 C.PI_IMPORT_NAME: "events", | |
36 C.PI_TYPE: "EXP", | |
37 C.PI_PROTOCOLS: [], | |
38 C.PI_DEPENDENCIES: ["XEP-0060"], | |
39 C.PI_MAIN: "Events", | |
40 C.PI_HANDLER: "no", | |
41 C.PI_DESCRIPTION: _("""Experimental implementation of XMPP events management""") | |
42 } | |
43 | |
44 NS_EVENT = 'org.salut-a-toi.event:0' | |
45 | |
46 | |
47 class Events(object): | |
48 """Q&D module to handle event attendance answer, experimentation only""" | |
49 | |
50 def __init__(self, host): | |
51 log.info(_(u"Event plugin initialization")) | |
52 self.host = host | |
53 self._p = self.host.plugins["XEP-0060"] | |
54 host.bridge.addMethod("eventGet", ".plugin", | |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
55 in_sign='ssss', out_sign='(ia{ss})', |
2231 | 56 method=self._eventGet, |
57 async=True) | |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
58 host.bridge.addMethod("eventCreate", ".plugin", |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
59 in_sign='ia{ss}ssss', out_sign='s', |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
60 method=self._eventCreate, |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
61 async=True) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
62 host.bridge.addMethod("eventModify", ".plugin", |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
63 in_sign='sssia{ss}s', out_sign='', |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
64 method=self._eventModify, |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
65 async=True) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
66 host.bridge.addMethod("eventInviteeGet", ".plugin", |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
67 in_sign='sss', out_sign='a{ss}', |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
68 method=self._eventInviteeGet, |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
69 async=True) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
70 host.bridge.addMethod("eventInviteeSet", ".plugin", |
2231 | 71 in_sign='ssa{ss}s', out_sign='', |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
72 method=self._eventInviteeSet, |
2231 | 73 async=True) |
74 | |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
75 def _eventGet(self, service, node, id_=u'', profile_key=C.PROF_KEY_NONE): |
2231 | 76 service = jid.JID(service) if service else None |
77 node = node if node else NS_EVENT | |
78 client = self.host.getClient(profile_key) | |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
79 return self.eventGet(client, service, node, id_) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
80 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
81 @defer.inlineCallbacks |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
82 def eventGet(self, client, service, node, id_=NS_EVENT): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
83 """Retrieve event data |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
84 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
85 @param service(unicode, None): PubSub service |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
86 @param node(unicode): PubSub node of the event |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
87 @param id_(unicode): id_ with even data |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
88 @return (tuple[int, dict[unicode, unicode]): event data: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
89 - timestamp of the event |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
90 - event metadata where key can be: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
91 location: location of the event |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
92 image: URL of a picture to use to represent event |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
93 background-image: URL of a picture to use in background |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
94 an empty dict is returned if nothing has been answered yed |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
95 """ |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
96 if not id_: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
97 id_ = NS_EVENT |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
98 items, metadata = yield self._p.getItems(service, node, item_ids=[id_], profile_key=client.profile) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
99 try: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
100 event_elt = next(items[0].elements(NS_EVENT, u'event')) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
101 except IndexError: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
102 raise exceptions.NotFound(_(u"No event with this id has been found")) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
103 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
104 try: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
105 timestamp = utils.date_parse(next(event_elt.elements(NS_EVENT, "date"))) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
106 except StopIteration: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
107 timestamp = -1 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
108 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
109 data = {} |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
110 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
111 for key in (u'name',): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
112 try: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
113 data[key] = event_elt[key] |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
114 except KeyError: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
115 continue |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
116 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
117 for elt_name in (u'description',): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
118 try: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
119 elt = next(event_elt.elements(NS_EVENT, elt_name)) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
120 except StopIteration: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
121 continue |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
122 else: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
123 data[elt_name] = unicode(elt) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
124 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
125 for elt_name in (u'image', 'background-image'): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
126 try: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
127 image_elt = next(event_elt.elements(NS_EVENT, elt_name)) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
128 data[elt_name] = image_elt['src'] |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
129 except StopIteration: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
130 continue |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
131 except KeyError: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
132 log.warning(_(u'no src found for image')) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
133 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
134 for uri_type in (u'invitees', u'blog'): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
135 try: |
2246
e8641b7718dc
plugin events: fixed blog/invitees uri handling
Goffi <goffi@goffi.org>
parents:
2243
diff
changeset
|
136 elt = next(event_elt.elements(NS_EVENT, uri_type)) |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
137 uri = data[uri_type + u'_uri'] = elt['uri'] |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
138 uri_data = uri_parse.parseXMPPUri(uri) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
139 if uri_data[u'type'] != u'pubsub': |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
140 raise ValueError |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
141 except StopIteration: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
142 log.warning(_(u"no {uri_type} element found!").format(uri_type=uri_type)) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
143 except KeyError: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
144 log.warning(_(u"incomplete {uri_type} element").format(uri_type=uri_type)) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
145 except ValueError: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
146 log.warning(_(u"bad {uri_type} element").format(uri_type=uri_type)) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
147 else: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
148 data[uri_type + u'_service'] = uri_data[u'path'] |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
149 data[uri_type + u'_node'] = uri_data[u'node'] |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
150 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
151 for meta_elt in event_elt.elements(NS_EVENT, 'meta'): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
152 key = meta_elt[u'name'] |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
153 if key in data: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
154 log.warning(u'Ignoring conflicting meta element: {xml}'.format(xml=meta_elt.toXml())) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
155 continue |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
156 data[key] = unicode(meta_elt) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
157 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
158 defer.returnValue((timestamp, data)) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
159 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
160 def _eventCreate(self, timestamp, data, service, node, id_=u'', profile_key=C.PROF_KEY_NONE): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
161 service = jid.JID(service) if service else None |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
162 node = node if node else NS_EVENT |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
163 client = self.host.getClient(profile_key) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
164 return self.eventCreate(client, timestamp, data, service, node, id_ or NS_EVENT) |
2231 | 165 |
166 @defer.inlineCallbacks | |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
167 def eventCreate(self, client, timestamp, data, service, node=None, item_id=NS_EVENT): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
168 """Create or replace an event |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
169 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
170 @param service(jid.JID, None): PubSub service |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
171 @param node(unicode, None): PubSub node of the event |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
172 None will create instant node. |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
173 @param item_id(unicode): ID of the item to create. |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
174 @param timestamp(timestamp, None) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
175 @param data(dict[unicode, unicode]): data to update |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
176 dict will be cleared, do a copy if data are still needed |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
177 key can be: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
178 - name: name of the event |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
179 - description: details |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
180 - image: main picture of the event |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
181 - background-image: image to use as background |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
182 @return (unicode): created node |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
183 """ |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
184 if not item_id: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
185 raise ValueError(_(u"item_id must be set")) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
186 if not service: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
187 service = client.jid.userhostJID() |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
188 event_elt = domish.Element((NS_EVENT, 'event')) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
189 if timestamp is not None and timestamp != -1: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
190 formatted_date = utils.xmpp_date(timestamp) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
191 event_elt.addElement((NS_EVENT, 'date'), content=formatted_date) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
192 for key in (u'name',): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
193 if key in data: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
194 event_elt[key] = data.pop(key) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
195 for key in (u'description',): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
196 if key in data: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
197 event_elt.addElement((NS_EVENT, key), content=data.pop(key)) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
198 for key in (u'image', u'background-image'): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
199 if key in data: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
200 elt = event_elt.addElement((NS_EVENT, key)) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
201 elt['src'] = data.pop(key) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
202 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
203 # we first create the invitees and blog nodes (if not specified in data) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
204 for uri_type in (u'invitees', u'blog'): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
205 key = uri_type + u'_uri' |
2246
e8641b7718dc
plugin events: fixed blog/invitees uri handling
Goffi <goffi@goffi.org>
parents:
2243
diff
changeset
|
206 for to_delete in (u'service', u'node'): |
e8641b7718dc
plugin events: fixed blog/invitees uri handling
Goffi <goffi@goffi.org>
parents:
2243
diff
changeset
|
207 k = uri_type + u'_' + to_delete |
e8641b7718dc
plugin events: fixed blog/invitees uri handling
Goffi <goffi@goffi.org>
parents:
2243
diff
changeset
|
208 if k in data: |
e8641b7718dc
plugin events: fixed blog/invitees uri handling
Goffi <goffi@goffi.org>
parents:
2243
diff
changeset
|
209 del data[k] |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
210 if key not in data: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
211 # FIXME: affiliate invitees |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
212 uri_node = yield self._p.createNode(client, service) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
213 yield self._p.setConfiguration(client, service, uri_node, {self._p.OPT_ACCESS_MODEL: self._p.ACCESS_WHITELIST}) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
214 uri_service = service |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
215 else: |
2246
e8641b7718dc
plugin events: fixed blog/invitees uri handling
Goffi <goffi@goffi.org>
parents:
2243
diff
changeset
|
216 uri = data.pop(key) |
e8641b7718dc
plugin events: fixed blog/invitees uri handling
Goffi <goffi@goffi.org>
parents:
2243
diff
changeset
|
217 uri_data = uri_parse.parseXMPPUri(uri) |
e8641b7718dc
plugin events: fixed blog/invitees uri handling
Goffi <goffi@goffi.org>
parents:
2243
diff
changeset
|
218 if uri_data[u'type'] != u'pubsub': |
e8641b7718dc
plugin events: fixed blog/invitees uri handling
Goffi <goffi@goffi.org>
parents:
2243
diff
changeset
|
219 raise ValueError(_(u'The given URI is not valid: {uri}').format(uri=uri)) |
e8641b7718dc
plugin events: fixed blog/invitees uri handling
Goffi <goffi@goffi.org>
parents:
2243
diff
changeset
|
220 uri_service = jid.JID(uri_data[u'path']) |
e8641b7718dc
plugin events: fixed blog/invitees uri handling
Goffi <goffi@goffi.org>
parents:
2243
diff
changeset
|
221 uri_node = uri_data[u'node'] |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
222 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
223 elt = event_elt.addElement((NS_EVENT, uri_type)) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
224 elt['uri'] = uri_parse.buildXMPPUri('pubsub', path=uri_service.full(), node=uri_node) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
225 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
226 # remaining data are put in <meta> elements |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
227 for key in data.keys(): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
228 elt = event_elt.addElement((NS_EVENT, 'meta'), content = data.pop(key)) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
229 elt['name'] = key |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
230 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
231 item_elt = pubsub.Item(id=item_id, payload=event_elt) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
232 try: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
233 # TODO: check auto-create, no need to create node first if available |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
234 node = yield self._p.createNode(client, service, nodeIdentifier=node) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
235 except error.StanzaError as e: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
236 if e.condition == u'conflict': |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
237 log.debug(_(u"requested node already exists")) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
238 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
239 yield self._p.publish(service, node, items=[item_elt], profile_key=client.profile) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
240 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
241 defer.returnValue(node) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
242 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
243 def _eventModify(self, service, node, id_, timestamp_update, data_update, profile_key=C.PROF_KEY_NONE): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
244 service = jid.JID(service) if service else None |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
245 node = node if node else NS_EVENT |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
246 client = self.host.getClient(profile_key) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
247 return self.eventModify(client, service, node, id_ or NS_EVENT, timestamp_update or None, data_update) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
248 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
249 @defer.inlineCallbacks |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
250 def eventModify(self, client, service, node, id_=NS_EVENT, timestamp_update=None, data_update=None): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
251 """Update an event |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
252 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
253 Similar as create instead that it update existing item instead of |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
254 creating or replacing it. Params are the same as for [eventCreate]. |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
255 """ |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
256 event_timestamp, event_metadata = yield self.eventGet(client, service, node, id_) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
257 new_timestamp = event_timestamp if timestamp_update is None else timestamp_update |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
258 new_data = event_metadata |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
259 if data_update: |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
260 for k, v in data_update.iteritems(): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
261 new_data[k] = v |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
262 yield self.eventCreate(client, new_timestamp, new_data, service, node, id_) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
263 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
264 def _eventInviteeGet(self, service, node, profile_key): |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
265 service = jid.JID(service) if service else None |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
266 node = node if node else NS_EVENT |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
267 client = self.host.getClient(profile_key) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
268 return self.eventInviteeGet(client, service, node) |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
269 |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
270 @defer.inlineCallbacks |
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
271 def eventInviteeGet(self, client, service, node): |
2231 | 272 """Retrieve attendance from event node |
273 | |
274 @param service(unicode, None): PubSub service | |
275 @param node(unicode): PubSub node of the event | |
276 @return (dict): a dict with current attendance status, | |
277 an empty dict is returned if nothing has been answered yed | |
278 """ | |
279 items, metadata = yield self._p.getItems(service, node, item_ids=[client.jid.userhost()], profile_key=client.profile) | |
280 try: | |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
281 event_elt = next(items[0].elements(NS_EVENT, u'invitee')) |
2231 | 282 except IndexError: |
283 # no item found, event data are not set yet | |
284 defer.returnValue({}) | |
285 data = {} | |
286 for key in (u'attend', u'guests'): | |
287 try: | |
288 data[key] = event_elt[key] | |
289 except KeyError: | |
290 continue | |
291 defer.returnValue(data) | |
292 | |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
293 def _eventInviteeSet(self, service, node, event_data, profile_key): |
2231 | 294 service = jid.JID(service) if service else None |
295 node = node if node else NS_EVENT | |
296 client = self.host.getClient(profile_key) | |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
297 return self.eventInviteeSet(client, service, node, event_data) |
2231 | 298 |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
299 def eventInviteeSet(self, client, service, node, data): |
2231 | 300 """Set or update attendance data in event node |
301 | |
302 @param service(unicode, None): PubSub service | |
303 @param node(unicode): PubSub node of the event | |
304 @param data(dict[unicode, unicode]): data to update | |
305 key can be: | |
306 attend: one of "yes", "no", "maybe" | |
307 guests: an int | |
308 """ | |
2243
5e12fc5ae52a
plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents:
2231
diff
changeset
|
309 event_elt = domish.Element((NS_EVENT, 'invitee')) |
2231 | 310 for key in (u'attend', u'guests'): |
311 try: | |
312 event_elt[key] = data.pop(key) | |
313 except KeyError: | |
314 pass | |
315 item_elt = pubsub.Item(id=client.jid.userhost(), payload=event_elt) | |
316 return self._p.publish(service, node, items=[item_elt], profile_key=client.profile) |