Mercurial > libervia-backend
comparison sat/plugins/plugin_exp_list_of_interest.py @ 2933:47df940738a7
plugin list of interest: added an interestsList bridge method + handle thumb_url
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 03 May 2019 13:06:47 +0200 |
parents | 672e6be3290f |
children | 782612c7854b |
comparison
equal
deleted
inserted
replaced
2932:472fadadefe6 | 2933:47df940738a7 |
---|---|
21 from sat.core.constants import Const as C | 21 from sat.core.constants import Const as C |
22 from sat.core.log import getLogger | 22 from sat.core.log import getLogger |
23 from wokkel import disco, iwokkel, pubsub | 23 from wokkel import disco, iwokkel, pubsub |
24 from zope.interface import implements | 24 from zope.interface import implements |
25 from twisted.internet import defer | 25 from twisted.internet import defer |
26 from twisted.words.protocols.jabber import error as jabber_error | 26 from twisted.words.protocols.jabber import error as jabber_error, jid |
27 from twisted.words.protocols.jabber.xmlstream import XMPPHandler | 27 from twisted.words.protocols.jabber.xmlstream import XMPPHandler |
28 from twisted.words.xish import domish | 28 from twisted.words.xish import domish |
29 | 29 |
30 log = getLogger(__name__) | 30 log = getLogger(__name__) |
31 | 31 |
50 | 50 |
51 def __init__(self, host): | 51 def __init__(self, host): |
52 log.info(_(u"List of Interest plugin initialization")) | 52 log.info(_(u"List of Interest plugin initialization")) |
53 self.host = host | 53 self.host = host |
54 self._p = self.host.plugins["XEP-0060"] | 54 self._p = self.host.plugins["XEP-0060"] |
55 host.bridge.addMethod( | |
56 "interestsList", | |
57 ".plugin", | |
58 in_sign="ssss", | |
59 out_sign="aa{ss}", | |
60 method=self._listInterests, | |
61 async=True, | |
62 ) | |
55 | 63 |
56 def getHandler(self, client): | 64 def getHandler(self, client): |
57 return ListInterestHandler(self) | 65 return ListInterestHandler(self) |
58 | 66 |
59 @defer.inlineCallbacks | 67 @defer.inlineCallbacks |
71 if e.condition == u"conflict": | 79 if e.condition == u"conflict": |
72 log.debug(_(u"requested node already exists")) | 80 log.debug(_(u"requested node already exists")) |
73 | 81 |
74 @defer.inlineCallbacks | 82 @defer.inlineCallbacks |
75 def registerPubsub(self, client, namespace, service, node, item_id=None, | 83 def registerPubsub(self, client, namespace, service, node, item_id=None, |
76 creator=False, name=None, element=None): | 84 creator=False, name=None, element=None, extra=None): |
77 """Register an interesting element in personal list | 85 """Register an interesting element in personal list |
78 | 86 |
79 @param namespace(unicode): namespace of the interest | 87 @param namespace(unicode): namespace of the interest |
80 this is used as a cache, to avoid the need to retrieve the item only to get | 88 this is used as a cache, to avoid the need to retrieve the item only to get |
81 its namespace | 89 its namespace |
85 @param creator(bool): True if client's profile is the creator of the node | 93 @param creator(bool): True if client's profile is the creator of the node |
86 This is used a cache, to avoid the need to retrieve affiliations | 94 This is used a cache, to avoid the need to retrieve affiliations |
87 @param name(unicode, None): name of the interest | 95 @param name(unicode, None): name of the interest |
88 @param element(domish.Element, None): element to attach | 96 @param element(domish.Element, None): element to attach |
89 may be used to cache some extra data | 97 may be used to cache some extra data |
98 @param extra(dict, None): extra data, key can be: | |
99 - thumb_url: http(s) URL of a thumbnail | |
90 """ | 100 """ |
91 yield self.createNode(client) | 101 yield self.createNode(client) |
92 interest_elt = domish.Element((NS_LIST_INTEREST, u"interest")) | 102 interest_elt = domish.Element((NS_LIST_INTEREST, u"interest")) |
93 interest_elt[u"namespace"] = namespace | 103 interest_elt[u"namespace"] = namespace |
94 if name is not None: | 104 if name is not None: |
95 interest_elt[u'name'] = name | 105 interest_elt[u'name'] = name |
106 thumb_url = extra.get(u'thumb_url') | |
107 if thumb_url: | |
108 interest_elt[u'thumb_url'] = thumb_url | |
96 pubsub_elt = interest_elt.addElement(u"pubsub") | 109 pubsub_elt = interest_elt.addElement(u"pubsub") |
97 pubsub_elt[u"service"] = service.full() | 110 pubsub_elt[u"service"] = service.full() |
98 pubsub_elt[u"node"] = node | 111 pubsub_elt[u"node"] = node |
99 if item_id is not None: | 112 if item_id is not None: |
100 pubsub_elt[u"item"] = item_id | 113 pubsub_elt[u"item"] = item_id |
107 client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt] | 120 client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt] |
108 ) | 121 ) |
109 | 122 |
110 @defer.inlineCallbacks | 123 @defer.inlineCallbacks |
111 def registerFileSharing( | 124 def registerFileSharing( |
112 self, client, service, repos_type=None, namespace=None, path=None, name=None): | 125 self, client, service, repos_type=None, namespace=None, path=None, name=None, |
126 extra=None): | |
113 """Register an interesting file repository in personal list | 127 """Register an interesting file repository in personal list |
114 | 128 |
115 @param service(jid.JID): service of the file repository | 129 @param service(jid.JID): service of the file repository |
116 @param repos_type(unicode): type of the repository | 130 @param repos_type(unicode): type of the repository |
117 @param namespace(unicode, None): namespace of the repository | 131 @param namespace(unicode, None): namespace of the repository |
118 @param path(unicode, None): path of the repository | 132 @param path(unicode, None): path of the repository |
119 @param name(unicode, None): name of the repository | 133 @param name(unicode, None): name of the repository |
134 @param extra(dict, None): as ad for [registerPubsub] | |
120 """ | 135 """ |
136 if extra is None: | |
137 extra = {} | |
121 yield self.createNode(client) | 138 yield self.createNode(client) |
122 interest_elt = domish.Element((NS_LIST_INTEREST, u"interest")) | 139 interest_elt = domish.Element((NS_LIST_INTEREST, u"interest")) |
123 interest_elt[u"namespace"] = self.host.getNamespace(u"fis") | 140 interest_elt[u"namespace"] = self.host.getNamespace(u"fis") |
124 if name is not None: | 141 if name is not None: |
125 interest_elt[u'name'] = name | 142 interest_elt[u'name'] = name |
143 thumb_url = extra.get(u'thumb_url') | |
144 if thumb_url: | |
145 interest_elt[u'thumb_url'] = thumb_url | |
126 file_sharing_elt = interest_elt.addElement(u"file_sharing") | 146 file_sharing_elt = interest_elt.addElement(u"file_sharing") |
127 file_sharing_elt[u"service"] = service.full() | 147 file_sharing_elt[u"service"] = service.full() |
128 if repos_type is not None: | 148 if repos_type is not None: |
129 file_sharing_elt[u"type"] = repos_type | 149 file_sharing_elt[u"type"] = repos_type |
130 if namespace is not None: | 150 if namespace is not None: |
133 file_sharing_elt[u"path"] = path | 153 file_sharing_elt[u"path"] = path |
134 item_elt = pubsub.Item(payload=interest_elt) | 154 item_elt = pubsub.Item(payload=interest_elt) |
135 yield self._p.publish( | 155 yield self._p.publish( |
136 client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt] | 156 client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt] |
137 ) | 157 ) |
158 | |
159 def _listInterestsSerialise(self, interests_data): | |
160 interests = [] | |
161 for item_elt in interests_data[0]: | |
162 interest_data = {} | |
163 interest_elt = item_elt.interest | |
164 if interest_elt.hasAttribute(u'name'): | |
165 interest_data[u'name'] = interest_elt.getAttribute(u'name') | |
166 if interest_elt.hasAttribute(u'thumb_url'): | |
167 interest_data[u'thumb_url'] = interest_elt.getAttribute(u'thumb_url') | |
168 elt = interest_elt.firstChildElement() | |
169 if elt.uri != NS_LIST_INTEREST: | |
170 log.warning(u"unexpected child element, ignoring: {xml}".format( | |
171 xml = elt.toXml())) | |
172 continue | |
173 if elt.name == u'pubsub': | |
174 interest_data.update({ | |
175 u"type": u"pubsub", | |
176 u"service": elt[u'service'], | |
177 u"node": elt[u'node'], | |
178 }) | |
179 for attr in (u'item', u'creator'): | |
180 if elt.hasAttribute(attr): | |
181 interest_data[attr] = elt[attr] | |
182 elif elt.name == u'file_sharing': | |
183 interest_data.update({ | |
184 u"type": u"file_sharing", | |
185 u"service": elt[u'service'], | |
186 }) | |
187 if elt.hasAttribute(u'type'): | |
188 interest_data[u'subtype'] = elt[u'type'] | |
189 for attr in (u'namespace', u'path'): | |
190 if elt.hasAttribute(attr): | |
191 interest_data[attr] = elt[attr] | |
192 else: | |
193 log.warning(u"unknown element, ignoring: {xml}".format(xml=elt.toXml())) | |
194 continue | |
195 interests.append(interest_data) | |
196 | |
197 return interests | |
198 | |
199 def _listInterests(self, service, node, namespace, profile): | |
200 service = jid.JID(service) if service else None | |
201 node = node or None | |
202 namespace = namespace or None | |
203 client = self.host.getClient(profile) | |
204 d = self.listInterests(client, service, node, namespace) | |
205 d.addCallback(self._listInterestsSerialise) | |
206 return d | |
138 | 207 |
139 @defer.inlineCallbacks | 208 @defer.inlineCallbacks |
140 def listInterests(self, client, service=None, node=None, namespace=None): | 209 def listInterests(self, client, service=None, node=None, namespace=None): |
141 """Retrieve list of interests | 210 """Retrieve list of interests |
142 | 211 |