comparison sat/plugins/plugin_xep_0045.py @ 2718:bb6adaa580ee

plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved: - plugin xep-0313: new serialise method - : getArchives now returns an extra mam_response dict with "complete" and "stable" status - : MAMGet now return a new metadata dict before profile (with serialised RSM and MAM response)
author Goffi <goffi@goffi.org>
date Mon, 10 Dec 2018 20:34:45 +0100
parents b35c84ea73cf
children c6be5962752d
comparison
equal deleted inserted replaced
2717:e3f6de6ce320 2718:bb6adaa580ee
868 between=False, 868 between=False,
869 filters={u'last_stanza_id': True}, 869 filters={u'last_stanza_id': True},
870 profile=client.profile) 870 profile=client.profile)
871 if last_mess: 871 if last_mess:
872 stanza_id = last_mess[0][-1][u'stanza_id'] 872 stanza_id = last_mess[0][-1][u'stanza_id']
873 rsm_req = rsm.RSMRequest(after=stanza_id) 873 rsm_req = rsm.RSMRequest(max_=100, after=stanza_id)
874 else: 874 else:
875 log.info(u"We have no MAM archive for room {room_jid}.".format( 875 log.info(u"We have no MAM archive for room {room_jid}.".format(
876 room_jid=room_jid)) 876 room_jid=room_jid))
877 rsm_req = rsm.RSMRequest(max_=20) 877 # we don't want the whole archive if we have no archive yet
878 # as it can be huge
879 rsm_req = rsm.RSMRequest(max_=50)
878 880
879 mam_req = mam.MAMRequest(rsm_=rsm_req) 881 mam_req = mam.MAMRequest(rsm_=rsm_req)
880 mam_data = yield self._mam.getArchives(client, mam_req, 882 complete = False
881 service=room_jid) 883 count = 0
882 elt_list, rsm_response = mam_data 884 while not complete:
883 885 mam_data = yield self._mam.getArchives(client, mam_req,
884 if not elt_list: 886 service=room_jid)
887 elt_list, rsm_response, mam_response = mam_data
888 complete = mam_response[u"complete"]
889 # we update MAM request for next iteration
890 mam_req.rsm.after = rsm_response.last
891
892 if not elt_list:
893 break
894 else:
895 count += len(elt_list)
896
897 for mess_elt in elt_list:
898 try:
899 fwd_message_elt = self._mam.getMessageFromResult(
900 client, mess_elt, mam_req, service=room_jid)
901 except exceptions.DataError:
902 continue
903 if fwd_message_elt.getAttribute(u"to"):
904 log.warning(
905 u'Forwarded message element has a "to" attribute while it is '
906 u'forbidden by specifications')
907 fwd_message_elt[u"to"] = client.jid.full()
908 mess_data = client.messageProt.parseMessage(fwd_message_elt)
909 # we attache parsed message data to element, to avoid parsing
910 # again in _addToHistory
911 fwd_message_elt._mess_data = mess_data
912 # and we inject to MUC workflow
913 client._muc_client._onGroupChat(fwd_message_elt)
914
915 if not count:
885 log.info(_(u"No message received while offline in {room_jid}".format( 916 log.info(_(u"No message received while offline in {room_jid}".format(
886 room_jid=room_jid))) 917 room_jid=room_jid)))
887 else: 918 else:
888 log.info( 919 log.info(
889 _(u"We have received {num_mess} message(s) in {room_jid} while offline.") 920 _(u"We have received {num_mess} message(s) in {room_jid} while "
890 .format(num_mess=len(elt_list), room_jid=room_jid)) 921 u"offline.")
891 922 .format(num_mess=count, room_jid=room_jid))
892 for mess_elt in elt_list:
893 try:
894 fwd_message_elt = self._mam.getMessageFromResult(
895 client, mess_elt, mam_req, service=room_jid)
896 except exceptions.DataError:
897 continue
898 if fwd_message_elt.getAttribute(u"to"):
899 log.warning(
900 u'Forwarded message element has a "to" attribute while it is '
901 u'forbidden by specifications')
902 fwd_message_elt[u"to"] = client.jid.full()
903 mess_data = client.messageProt.parseMessage(fwd_message_elt)
904 # we attache parsed message data to element, to avoid parsing
905 # again in _addToHistory
906 fwd_message_elt._mess_data = mess_data
907 # and we inject to MUC workflow
908 client._muc_client._onGroupChat(fwd_message_elt)
909
910 923
911 # for legacy history, the following steps are done in receivedSubject but for MAM 924 # for legacy history, the following steps are done in receivedSubject but for MAM
912 # the order is different (we have to join then get MAM archive, so subject 925 # the order is different (we have to join then get MAM archive, so subject
913 # is received before archive), so we change state and add the callbacks here. 926 # is received before archive), so we change state and add the callbacks here.
914 self.changeRoomState(room, ROOM_STATE_LIVE) 927 self.changeRoomState(room, ROOM_STATE_LIVE)