comparison sat/plugins/plugin_misc_lists.py @ 3456:6deea0d8d0e7

plugin tickets, merge_requests: renamed "tickets" feature to "lists": this feature is more generic than only "tickets" for technical stuff, thus the name "lists" seems more appropriate.
author Goffi <goffi@goffi.org>
date Thu, 04 Feb 2021 21:05:21 +0100
parents sat/plugins/plugin_misc_tickets.py@bb0225aaf4e6
children
comparison
equal deleted inserted replaced
3455:c30b4b18d3b8 3456:6deea0d8d0e7
1 #!/usr/bin/env python3
2
3 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 from sat.core.i18n import _, D_
19 from sat.core.constants import Const as C
20 from twisted.internet import defer
21 from sat.tools.common import uri
22 import shortuuid
23 from sat.core.log import getLogger
24
25 log = getLogger(__name__)
26
27 # XXX: this plugin was formely named "tickets", thus the namespace keeps this
28 # name
29 APP_NS_TICKETS = "org.salut-a-toi.tickets:0"
30
31 PLUGIN_INFO = {
32 C.PI_NAME: _("Pubsub Lists"),
33 C.PI_IMPORT_NAME: "LISTS",
34 C.PI_TYPE: "EXP",
35 C.PI_PROTOCOLS: [],
36 C.PI_DEPENDENCIES: ["XEP-0060", "XEP-0346", "XEP-0277", "IDENTITY"],
37 C.PI_MAIN: "PubsubLists",
38 C.PI_HANDLER: "no",
39 C.PI_DESCRIPTION: _("""Pubsub lists management plugin"""),
40 }
41
42
43 class PubsubLists:
44
45 def __init__(self, host):
46 log.info(_("Pubsub lists plugin initialization"))
47 self.host = host
48 self._s = self.host.plugins["XEP-0346"]
49 self.namespace = self._s.getSubmittedNS(APP_NS_TICKETS)
50 host.registerNamespace("tickets", self.namespace)
51 self._p = self.host.plugins["XEP-0060"]
52 self._m = self.host.plugins["XEP-0277"]
53 host.bridge.addMethod(
54 "listGet",
55 ".plugin",
56 in_sign="ssiassa{ss}s",
57 out_sign="s",
58 method=lambda service, node, max_items, items_ids, sub_id, extra, profile_key:
59 self._s._get(
60 service,
61 node,
62 max_items,
63 items_ids,
64 sub_id,
65 extra,
66 default_node=self.namespace,
67 form_ns=APP_NS_TICKETS,
68 filters={
69 "author": self._s.valueOrPublisherFilter,
70 "created": self._s.dateFilter,
71 "updated": self._s.dateFilter,
72 },
73 profile_key=profile_key),
74 async_=True,
75 )
76 host.bridge.addMethod(
77 "listSet",
78 ".plugin",
79 in_sign="ssa{sas}ssss",
80 out_sign="s",
81 method=self._set,
82 async_=True,
83 )
84 host.bridge.addMethod(
85 "listSchemaGet",
86 ".plugin",
87 in_sign="sss",
88 out_sign="s",
89 method=lambda service, nodeIdentifier, profile_key: self._s._getUISchema(
90 service, nodeIdentifier, default_node=self.namespace,
91 profile_key=profile_key),
92 async_=True,
93 )
94
95 def _set(self, service, node, values, schema=None, item_id=None, extra='',
96 profile_key=C.PROF_KEY_NONE):
97 client, service, node, schema, item_id, extra = self._s.prepareBridgeSet(
98 service, node, schema, item_id, extra, profile_key
99 )
100 d = defer.ensureDeferred(self.set(
101 client, service, node, values, schema, item_id, extra, deserialise=True
102 ))
103 d.addCallback(lambda ret: ret or "")
104 return d
105
106 async def set(self, client, service, node, values, schema=None, item_id=None, extra=None,
107 deserialise=False, form_ns=APP_NS_TICKETS):
108 """Publish a tickets
109
110 @param node(unicode, None): Pubsub node to use
111 None to use default tickets node
112 @param values(dict[key(unicode), [iterable[object]|object]]): values of the ticket
113
114 if value is not iterable, it will be put in a list
115 'created' and 'updated' will be forced to current time:
116 - 'created' is set if item_id is None, i.e. if it's a new ticket
117 - 'updated' is set everytime
118 @param extra(dict, None): same as for [XEP-0060.sendItem] with additional keys:
119 - update(bool): if True, get previous item data to merge with current one
120 if True, item_id must be None
121 other arguments are same as for [self._s.sendDataFormItem]
122 @return (unicode): id of the created item
123 """
124 if not node:
125 node = self.namespace
126
127 if not item_id:
128 comments_service = await self._m.getCommentsService(client, service)
129
130 # we need to use uuid for comments node, because we don't know item id in
131 # advance (we don't want to set it ourselves to let the server choose, so we
132 # can have a nicer id if serial ids is activated)
133 comments_node = self._m.getCommentsNode(
134 node + "_" + str(shortuuid.uuid())
135 )
136 options = {
137 self._p.OPT_ACCESS_MODEL: self._p.ACCESS_OPEN,
138 self._p.OPT_PERSIST_ITEMS: 1,
139 self._p.OPT_MAX_ITEMS: -1,
140 self._p.OPT_DELIVER_PAYLOADS: 1,
141 self._p.OPT_SEND_ITEM_SUBSCRIBE: 1,
142 self._p.OPT_PUBLISH_MODEL: self._p.ACCESS_OPEN,
143 }
144 await self._p.createNode(client, comments_service, comments_node, options)
145 values["comments_uri"] = uri.buildXMPPUri(
146 "pubsub",
147 subtype="microblog",
148 path=comments_service.full(),
149 node=comments_node,
150 )
151
152 return await self._s.set(
153 client, service, node, values, schema, item_id, extra, deserialise, form_ns
154 )