comparison sat_pubsub/mam.py @ 316:cca47e9977a5

mam: minor improvments (module import and some checks)
author Goffi <goffi@goffi.org>
date Sun, 03 Jan 2016 18:33:22 +0100
parents a776544d84e5
children 54d90c73b8b5
comparison
equal deleted inserted replaced
315:720d73e30bf7 316:cca47e9977a5
1 #!/usr/bin/python 1 #!/usr/bin/python
2 #-*- coding: utf-8 -*- 2 #-*- coding: utf-8 -*-
3 3
4 # Copyright (c) 2016 Jérôme Poisson
4 # Copyright (c) 2015-2016 Adrien Cossa 5 # Copyright (c) 2015-2016 Adrien Cossa
5 # 6 #
6 # This program is free software: you can redistribute it and/or modify 7 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by 8 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or 9 # the Free Software Foundation, either version 3 of the License, or
33 from wokkel.pubsub import NS_PUBSUB_EVENT 34 from wokkel.pubsub import NS_PUBSUB_EVENT
34 35
35 from dateutil import parser 36 from dateutil import parser
36 37
37 # TODO: change this when RSM and MAM are in wokkel 38 # TODO: change this when RSM and MAM are in wokkel
38 from sat.tmp.wokkel.rsm import RSMRequest 39 from sat.tmp.wokkel import rsm
39 from sat.tmp.wokkel.mam import MAMPrefs, IMAMResource 40 from sat.tmp.wokkel import mam
40 41
41 NS_CLIENT = 'jabber:client' 42 NS_CLIENT = 'jabber:client'
42 43
43 44
44 class MAMResource(object): 45 class MAMResource(object):
45 46
46 implements(IMAMResource) 47 implements(mam.IMAMResource)
47 48
48 def __init__(self, backend): 49 def __init__(self, backend):
49 self.backend = backend 50 self.backend = backend
50 51
51 def onArchiveRequest(self, mam, requestor): 52 def onArchiveRequest(self, mam, requestor):
55 @type mam: L{MAMQueryReques<wokkel.mam.MAMQueryRequest>} 56 @type mam: L{MAMQueryReques<wokkel.mam.MAMQueryRequest>}
56 57
57 @param requestor: JID of the requestor. 58 @param requestor: JID of the requestor.
58 @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>} 59 @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>}
59 60
60 @return: The RSM answer. 61 @return: A tuple with list of message data (id, element, data) and RSM element
61 @rtype: L{RSMResponse<wokkel.rsm.RSMResponse>} 62 @rtype: C{tuple}
62 """ 63 """
64 # FIXME: bad result ordering
63 ext_data = {} 65 ext_data = {}
64 if mam.form: 66 if mam.form:
65 ext_data['filters'] = mam.form.fields.values() 67 ext_data['filters'] = mam.form.fields.values()
66 if mam.rsm is None: 68 if mam.rsm is None:
67 mam.rsm = RSMRequest(const.VAL_RSM_MAX_DEFAULT) 69 mam.rsm = rsm.RSMRequest(const.VAL_RSM_MAX_DEFAULT)
68 ext_data['rsm'] = mam.rsm 70 ext_data['rsm'] = mam.rsm
69 71
70 d = self.backend.getItems(mam.node, requestor, mam.rsm.max, None, ext_data) 72 d = self.backend.getItems(mam.node, requestor, mam.rsm.max, None, ext_data)
71 73
72 def make_message(elt): 74 def make_message(elt):
80 82
81 def cb(elts): 83 def cb(elts):
82 msg_data = [] 84 msg_data = []
83 rsm_elt = None 85 rsm_elt = None
84 for elt in elts: 86 for elt in elts:
85 if elt.name == 'set': 87 if elt.name == 'set' and elt.uri == rsm.NS_RSM:
88 assert rsm_elt is None
86 rsm_elt = elt 89 rsm_elt = elt
87 elif elt.name == 'item': 90 elif elt.name == 'item':
91 # FIXME: this is not good as it is dependant on payload
92 # TODO: remove this and use date field in database
88 date = parser.parse(''.join(elt.entry.published.children)) 93 date = parser.parse(''.join(elt.entry.published.children))
89 msg_data.append([elt['id'], make_message(elt), date]) 94 msg_data.append([elt['id'], make_message(elt), date])
90 return (msg_data, rsm_elt) 95 return (msg_data, rsm_elt)
91 96
92 d.addErrback(PubSubResourceFromBackend._mapErrors) 97 d.addErrback(PubSubResourceFromBackend._mapErrors)
93 return d.addCallback(cb) 98 d.addCallback(cb)
99 return d
94 100
95 def onPrefsGetRequest(self, requestor): 101 def onPrefsGetRequest(self, requestor):
96 """ 102 """
97 103
98 @param requestor: JID of the requestor. 104 @param requestor: JID of the requestor.
100 106
101 @return: The current settings. 107 @return: The current settings.
102 @rtype: L{wokkel.mam.MAMPrefs} 108 @rtype: L{wokkel.mam.MAMPrefs}
103 """ 109 """
104 # TODO: return the actual current settings 110 # TODO: return the actual current settings
105 return MAMPrefs() 111 return mam.MAMPrefs()
106 112
107 def onPrefsSetRequest(self, prefs, requestor): 113 def onPrefsSetRequest(self, prefs, requestor):
108 """ 114 """
109 115
110 @param prefs: The new settings to set. 116 @param prefs: The new settings to set.
115 121
116 @return: The settings that have actually been set. 122 @return: The settings that have actually been set.
117 @rtype: L{wokkel.mam.MAMPrefs} 123 @rtype: L{wokkel.mam.MAMPrefs}
118 """ 124 """
119 # TODO: set the new settings and return them 125 # TODO: set the new settings and return them
120 return MAMPrefs() 126 return mam.MAMPrefs()