Mercurial > libervia-backend
diff src/plugins/plugin_exp_events.py @ 2231:230fc5b609a8
plugin event: first draft:
basic plugin to manage event attendance
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 27 Apr 2017 00:55:02 +0200 |
parents | |
children | 5e12fc5ae52a |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/plugins/plugin_exp_events.py Thu Apr 27 00:55:02 2017 +0200 @@ -0,0 +1,112 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# SAT plugin to detect language (experimental) +# Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +from sat.core.i18n import _ +from sat.core.constants import Const as C +from sat.core.log import getLogger +log = getLogger(__name__) +from twisted.internet import defer +from twisted.words.protocols.jabber import jid +from twisted.words.xish import domish +from wokkel import pubsub + + +PLUGIN_INFO = { + C.PI_NAME: "Event plugin", + C.PI_IMPORT_NAME: "events", + C.PI_TYPE: "EXP", + C.PI_PROTOCOLS: [], + C.PI_DEPENDENCIES: ["XEP-0060"], + C.PI_MAIN: "Events", + C.PI_HANDLER: "no", + C.PI_DESCRIPTION: _("""Experimental implementation of XMPP events management""") +} + +NS_EVENT = 'org.salut-a-toi.event:0' + + +class Events(object): + """Q&D module to handle event attendance answer, experimentation only""" + + def __init__(self, host): + log.info(_(u"Event plugin initialization")) + self.host = host + self._p = self.host.plugins["XEP-0060"] + host.bridge.addMethod("eventGet", ".plugin", + in_sign='sss', out_sign='a{ss}', + method=self._eventGet, + async=True) + host.bridge.addMethod("eventSet", ".plugin", + in_sign='ssa{ss}s', out_sign='', + method=self._eventSet, + async=True) + + def _eventGet(self, service, node, profile_key): + service = jid.JID(service) if service else None + node = node if node else NS_EVENT + client = self.host.getClient(profile_key) + return self.eventGet(client, service, node) + + @defer.inlineCallbacks + def eventGet(self, client, service, node): + """Retrieve attendance from event node + + @param service(unicode, None): PubSub service + @param node(unicode): PubSub node of the event + @return (dict): a dict with current attendance status, + an empty dict is returned if nothing has been answered yed + """ + items, metadata = yield self._p.getItems(service, node, item_ids=[client.jid.userhost()], profile_key=client.profile) + try: + event_elt = next(items[0].elements((NS_EVENT, u'event'))) + except IndexError: + # no item found, event data are not set yet + defer.returnValue({}) + data = {} + for key in (u'attend', u'guests'): + try: + data[key] = event_elt[key] + except KeyError: + continue + defer.returnValue(data) + + def _eventSet(self, service, node, event_data, profile_key): + service = jid.JID(service) if service else None + node = node if node else NS_EVENT + client = self.host.getClient(profile_key) + return self.eventSet(client, service, node, event_data) + + def eventSet(self, client, service, node, data): + """Set or update attendance data in event node + + @param service(unicode, None): PubSub service + @param node(unicode): PubSub node of the event + @param data(dict[unicode, unicode]): data to update + key can be: + attend: one of "yes", "no", "maybe" + guests: an int + """ + event_elt = domish.Element((NS_EVENT, 'event')) + for key in (u'attend', u'guests'): + try: + event_elt[key] = data.pop(key) + except KeyError: + pass + item_elt = pubsub.Item(id=client.jid.userhost(), payload=event_elt) + return self._p.publish(service, node, items=[item_elt], profile_key=client.profile)