From f503b6283447c9d7af5ecb4e79183f533d84f642 Mon Sep 17 00:00:00 2001 From: Adrian Moennich Date: Wed, 8 Oct 2014 14:25:14 +0200 Subject: [PATCH] Add log retrieval/deletion functions --- chat/indico_chat/models/chatrooms.py | 2 +- chat/indico_chat/plugin.py | 6 ++- chat/indico_chat/xmpp.py | 65 +++++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/chat/indico_chat/models/chatrooms.py b/chat/indico_chat/models/chatrooms.py index 7c0c13f..e8b358d 100644 --- a/chat/indico_chat/models/chatrooms.py +++ b/chat/indico_chat/models/chatrooms.py @@ -198,6 +198,6 @@ class ChatroomEventAssociation(db.Model): if not self.chatroom.events: db.session.delete(self.chatroom) db.session.flush() - delete_room(self.chatroom.jid, reason) + delete_room(self.chatroom, reason) return True return False diff --git a/chat/indico_chat/plugin.py b/chat/indico_chat/plugin.py index d35b7cf..d766dc9 100644 --- a/chat/indico_chat/plugin.py +++ b/chat/indico_chat/plugin.py @@ -19,6 +19,7 @@ from __future__ import unicode_literals from flask_pluginengine import render_plugin_template from wtforms import ValidationError from wtforms.fields.core import BooleanField +from wtforms.fields.html5 import URLField from wtforms.fields.simple import TextField, TextAreaField from wtforms.validators import DataRequired @@ -50,7 +51,10 @@ class SettingsForm(IndicoForm): notify_admins = BooleanField('Notify admins', description="Should chat administrators receive email notifications?") notify_emails = EmailListField('Notification emails', description="Additional email addresses to sent notifications to (one per line)") - # TODO: log retrieval URL + log_url = URLField('Log URL', description='You can set this to the URL of the ' + 'jabber-logs app, ' + 'running on the jabber server to let event managers can retrieve chat ' + 'logs for rooms on that server.') chat_links = MultipleItemsField('Chatroom links', fields=(('title', 'Title'), ('link', 'Link')), description="Links to join the chatroom. You can use the placeholders {room} for " "the room name and {server} for the MUC server.") diff --git a/chat/indico_chat/xmpp.py b/chat/indico_chat/xmpp.py index d2421d9..8e95f24 100644 --- a/chat/indico_chat/xmpp.py +++ b/chat/indico_chat/xmpp.py @@ -16,9 +16,12 @@ from __future__ import unicode_literals +import posixpath import re +import requests from flask import current_app +from requests.exceptions import RequestException from sleekxmpp import ClientXMPP from sleekxmpp.exceptions import IqError @@ -40,7 +43,7 @@ def create_room(room): muc.configureRoom(room.jid, _set_form_values(xmpp, room)) Logger.get('plugin.chat').info('Creating room {}'.format(room.jid)) - return _execute_xmpp(_create_room) + _execute_xmpp(_create_room) def update_room(room): @@ -52,18 +55,19 @@ def update_room(room): muc.configureRoom(room.jid, _set_form_values(xmpp, room, muc.getRoomConfig(room.jid))) Logger.get('plugin.chat').info('Updating room {}'.format(room.jid)) - return _execute_xmpp(_update_room) + _execute_xmpp(_update_room) -def delete_room(jid, reason=''): +def delete_room(room, reason=''): """Deletes a MUC room from the XMPP server.""" def _delete_room(xmpp): muc = xmpp.plugin['xep_0045'] - muc.destroy(jid, reason=reason) + muc.destroy(room.jid, reason=reason) - Logger.get('plugin.chat').info('Deleting room {}'.format(jid)) - return _execute_xmpp(_delete_room) + Logger.get('plugin.chat').info('Deleting room {}'.format(room.jid)) + _execute_xmpp(_delete_room) + delete_logs(room) def get_room_config(jid): @@ -205,3 +209,52 @@ def _execute_xmpp(connected_callback): raise result[1] return result[0] + + +def retrieve_logs(room, start_date=None, end_date=None): + """Retrieves chat logs + + :param room: the `Chatroom` + :param start_date: the earliest date to get logs for + :param end_date: the latest date to get logs for + :return: logs in html format + """ + from indico_chat.plugin import ChatPlugin + + base_url = ChatPlugin.settings.get('log_url') + if not base_url or room.custom_server: + return None + + params = {'cr': room.jid} + if start_date: + params['sdate'] = start_date.strftime('%Y-%m-%d') + if end_date: + params['sdate'] = end_date.strftime('%Y-%m-%d') + + try: + response = requests.get(base_url, params=params) + except RequestException: + Logger.get('plugin.chat').exception('Could not retrieve logs for {}'.format(room.jid)) + return None + if response.headers.get('content-type') == 'application/json': + Logger.get('plugin.chat').warning('Could not retrieve logs for {}: {}'.format(room.jid, + response.json().get('error'))) + return None + return response.text + + +def delete_logs(room): + """Deletes chat logs""" + from indico_chat.plugin import ChatPlugin + + base_url = ChatPlugin.settings.get('log_url') + if not base_url or room.custom_server: + return + + try: + response = requests.get(posixpath.join(base_url, 'delete'), params={'cr': room.jid}).json() + except (RequestException, ValueError): + Logger.get('plugin.chat').exception('Could not delete logs for {}'.format(room.jid)) + return + if not response.get('success'): + Logger.get('plugin.chat').warning('Could not delete logs for {}: {}'.format(room.jid), response.get('error'))