gajim-plural/gajim/common/modules/user_activity.py

106 lines
3.3 KiB
Python
Raw Normal View History

# This file is part of Gajim.
#
# Gajim is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# Gajim 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
# XEP-0108: User Activity
import logging
import nbxmpp
from gi.repository import GLib
from gajim.common.const import PEPEventType, ACTIVITIES
from gajim.common.exceptions import StanzaMalformed
2018-07-04 22:55:16 +02:00
from gajim.common.modules.pep import AbstractPEPModule, AbstractPEPData
log = logging.getLogger('gajim.c.m.user_activity')
2018-07-04 22:55:16 +02:00
class UserActivityData(AbstractPEPData):
2018-07-04 22:55:16 +02:00
type_ = PEPEventType.ACTIVITY
2018-07-04 22:55:16 +02:00
def __init__(self, activity):
self._pep_specific_data = activity
2018-07-04 22:55:16 +02:00
def asMarkupText(self):
pep = self._pep_specific_data
activity = pep['activity']
subactivity = pep['subactivity'] if 'subactivity' in pep else None
text = pep['text'] if 'text' in pep else None
if activity in ACTIVITIES:
# Translate standard activities
if subactivity in ACTIVITIES[activity]:
subactivity = ACTIVITIES[activity][subactivity]
activity = ACTIVITIES[activity]['category']
2018-07-04 22:55:16 +02:00
markuptext = '<b>' + GLib.markup_escape_text(activity)
if subactivity:
markuptext += ': ' + GLib.markup_escape_text(subactivity)
markuptext += '</b>'
if text:
markuptext += ' (%s)' % GLib.markup_escape_text(text)
return markuptext
2018-07-04 22:55:16 +02:00
class UserActivity(AbstractPEPModule):
2018-07-04 22:55:16 +02:00
name = 'activity'
namespace = nbxmpp.NS_ACTIVITY
pep_class = UserActivityData
store_publish = True
_log = log
2018-07-04 22:55:16 +02:00
def __init__(self, con):
AbstractPEPModule.__init__(self, con, con.name)
2018-07-04 22:55:16 +02:00
self.handlers = []
def _extract_info(self, item):
activity_dict = {}
2018-07-04 22:55:16 +02:00
activity_tag = item.getTag('activity', namespace=self.namespace)
if activity_tag is None:
raise StanzaMalformed('No activity node')
for child in activity_tag.getChildren():
name = child.getName().strip()
data = child.getData().strip()
if name == 'text':
activity_dict['text'] = data
else:
activity_dict['activity'] = name
for subactivity in child.getChildren():
subactivity_name = subactivity.getName().strip()
activity_dict['subactivity'] = subactivity_name
return activity_dict or None
2018-07-04 22:55:16 +02:00
def _build_node(self, data):
item = nbxmpp.Node('activity', {'xmlns': self.namespace})
2018-07-05 19:21:58 +02:00
if data is None:
return
activity, subactivity, message = data
if activity:
i = item.addChild(activity)
if subactivity:
i.addChild(subactivity)
if message:
i = item.addChild('text')
i.addData(message)
return item
def get_instance(*args, **kwargs):
return UserActivity(*args, **kwargs), 'UserActivity'