Philipp Hörist pushed to branch master at gajim / gajim

Commits:

5 changed files:

Changes:

  • gajim/common/connection_handlers.py
    ... ... @@ -268,7 +268,7 @@ class ConnectionHandlersBase:
    268 268
             if obj.conn.name != self.name:
    
    269 269
                 return
    
    270 270
     
    
    271
    -        self._check_for_mam_compliance(obj.jid, obj.unique_id)
    
    271
    +        self._check_for_mam_compliance(obj.jid, obj.stanza_id)
    
    272 272
     
    
    273 273
             if (app.config.should_log(obj.conn.name, obj.jid) and
    
    274 274
                     obj.msgtxt and obj.nick):
    
    ... ... @@ -282,8 +282,10 @@ class ConnectionHandlersBase:
    282 282
                                             message=obj.msgtxt,
    
    283 283
                                             contact_name=obj.nick,
    
    284 284
                                             additional_data=obj.additional_data,
    
    285
    -                                        stanza_id=obj.unique_id)
    
    285
    +                                        stanza_id=obj.stanza_id)
    
    286 286
                 app.logger.set_room_last_message_time(obj.room_jid, obj.timestamp)
    
    287
    +            self.get_module('MAM').save_archive_id(
    
    288
    +                obj.room_jid, obj.stanza_id, obj.timestamp)
    
    287 289
     
    
    288 290
         # process and dispatch an error message
    
    289 291
         def dispatch_error_message(self, msg, msgtxt, session, frm, tim):
    

  • gajim/common/logger.py
    ... ... @@ -1429,10 +1429,11 @@ class Logger:
    1429 1429
             exists = self.get_archive_timestamp(jid)
    
    1430 1430
             if not exists:
    
    1431 1431
                 sql = '''INSERT INTO last_archive_message VALUES (?, ?, ?, ?)'''
    
    1432
    -            self._con.execute(sql, (jid_id,
    
    1433
    -                                   kwargs.get('last_mam_id', None),
    
    1434
    -                                   kwargs.get('oldest_mam_timestamp', None),
    
    1435
    -                                   kwargs.get('last_muc_timestamp', None)))
    
    1432
    +            self._con.execute(sql, (
    
    1433
    +                jid_id,
    
    1434
    +                kwargs.get('last_mam_id', None),
    
    1435
    +                kwargs.get('oldest_mam_timestamp', None),
    
    1436
    +                kwargs.get('last_muc_timestamp', None)))
    
    1436 1437
             else:
    
    1437 1438
                 args = ' = ?, '.join(kwargs.keys()) + ' = ?'
    
    1438 1439
                 sql = '''UPDATE last_archive_message SET {}
    

  • gajim/common/modules/mam.py
    ... ... @@ -47,6 +47,9 @@ class MAM:
    47 47
             self.archiving_namespace = None
    
    48 48
             self._mam_query_ids = {}
    
    49 49
     
    
    50
    +        # Holds archive jids where catch up was successful
    
    51
    +        self._catch_up_finished = []
    
    52
    +
    
    50 53
         def pass_disco(self, from_, identities, features, data, node):
    
    51 54
             if nbxmpp.NS_MAM_2 in features:
    
    52 55
                 self.archiving_namespace = nbxmpp.NS_MAM_2
    
    ... ... @@ -369,6 +372,9 @@ class MAM:
    369 372
                 start_date = datetime.utcnow() - timedelta(days=7)
    
    370 373
                 log.info('First start: query archive start: %s', start_date)
    
    371 374
                 query = self._get_archive_query(query_id, start=start_date)
    
    375
    +
    
    376
    +        if own_jid in self._catch_up_finished:
    
    377
    +            self._catch_up_finished.remove(own_jid)
    
    372 378
             self._send_archive_query(query, query_id, start_date)
    
    373 379
     
    
    374 380
         def request_archive_on_muc_join(self, jid):
    
    ... ... @@ -388,6 +394,9 @@ class MAM:
    388 394
                 start_date = datetime.utcnow() - timedelta(days=1)
    
    389 395
                 log.info('First join: query archive %s from: %s', jid, start_date)
    
    390 396
                 query = self._get_archive_query(query_id, jid=jid, start=start_date)
    
    397
    +
    
    398
    +        if jid in self._catch_up_finished:
    
    399
    +            self._catch_up_finished.remove(jid)
    
    391 400
             self._send_archive_query(query, query_id, start_date, groupchat=True)
    
    392 401
     
    
    393 402
         def _send_archive_query(self, query, query_id, start_date=None,
    
    ... ... @@ -408,11 +417,13 @@ class MAM:
    408 417
             last = set_.getTagData('last')
    
    409 418
             if last is None:
    
    410 419
                 log.info('End of MAM query, no items retrieved')
    
    420
    +            self._catch_up_finished.append(jid)
    
    411 421
                 self._mam_query_ids.pop(jid)
    
    412 422
                 return
    
    413 423
     
    
    414 424
             complete = fin.getAttr('complete')
    
    415
    -        app.logger.set_archive_timestamp(jid, last_mam_id=last)
    
    425
    +        app.logger.set_archive_timestamp(
    
    426
    +            jid, last_mam_id=last, last_muc_timestamp=None)
    
    416 427
             if complete != 'true':
    
    417 428
                 self._mam_query_ids.pop(jid)
    
    418 429
                 query_id = self._get_query_id(jid)
    
    ... ... @@ -425,6 +436,8 @@ class MAM:
    425 436
                         jid,
    
    426 437
                         last_mam_id=last,
    
    427 438
                         oldest_mam_timestamp=start_date.timestamp())
    
    439
    +
    
    440
    +            self._catch_up_finished.append(jid)
    
    428 441
                 log.info('End of MAM query, last mam id: %s', last)
    
    429 442
     
    
    430 443
         def request_archive_interval(self, start_date, end_date, after=None,
    
    ... ... @@ -517,6 +530,17 @@ class MAM:
    517 530
             query.setAttr('queryid', query_id)
    
    518 531
             return iq
    
    519 532
     
    
    533
    +    def save_archive_id(self, jid, stanza_id, timestamp):
    
    534
    +        if stanza_id is None:
    
    535
    +            return
    
    536
    +        if jid is None:
    
    537
    +            jid = self._con.get_own_jid().getStripped()
    
    538
    +        if jid not in self._catch_up_finished:
    
    539
    +            return
    
    540
    +        log.info('Save: %s: %s, %s', jid, stanza_id, timestamp)
    
    541
    +        app.logger.set_archive_timestamp(
    
    542
    +            jid, last_mam_id=stanza_id, last_muc_timestamp=timestamp)
    
    543
    +
    
    520 544
         def request_mam_preferences(self):
    
    521 545
             log.info('Request MAM preferences')
    
    522 546
             iq = nbxmpp.Iq('get', self.archiving_namespace)
    

  • gajim/common/modules/message.py
    ... ... @@ -109,8 +109,8 @@ class Message:
    109 109
             jid, resource = app.get_room_and_nick_from_fjid(fjid)
    
    110 110
     
    
    111 111
             # Check for duplicates
    
    112
    -        unique_id = self._get_unique_id(stanza, forwarded, sent,
    
    113
    -                                        self_message, muc_pm)
    
    112
    +        stanza_id, origin_id = self._get_unique_id(
    
    113
    +            stanza, forwarded, sent, self_message, muc_pm)
    
    114 114
     
    
    115 115
             # Check groupchat messages for duplicates,
    
    116 116
             # We do this because of MUC History messages
    
    ... ... @@ -121,8 +121,9 @@ class Message:
    121 121
                     archive_jid = self._con.get_own_jid().getStripped()
    
    122 122
                 if app.logger.find_stanza_id(self._account,
    
    123 123
                                              archive_jid,
    
    124
    -                                         unique_id,
    
    125
    -                                         groupchat=type_ == 'groupchat'):
    
    124
    +                                         stanza_id,
    
    125
    +                                         origin_id,
    
    126
    +                                         type_ == 'groupchat'):
    
    126 127
                     return
    
    127 128
     
    
    128 129
             thread_id = stanza.getThread()
    
    ... ... @@ -177,7 +178,8 @@ class Message:
    177 178
                 'fjid': fjid,
    
    178 179
                 'jid': jid,
    
    179 180
                 'resource': resource,
    
    180
    -            'unique_id': unique_id,
    
    181
    +            'stanza_id': stanza_id,
    
    182
    +            'unique_id': stanza_id or origin_id,
    
    181 183
                 'mtype': type_,
    
    182 184
                 'msgtxt': msgtxt,
    
    183 185
                 'thread_id': thread_id,
    
    ... ... @@ -215,7 +217,6 @@ class Message:
    215 217
                 'form_node': parse_form(event.stanza),
    
    216 218
                 'xhtml': parse_xhtml(event.stanza),
    
    217 219
                 'chatstate': parse_chatstate(event.stanza),
    
    218
    -            'stanza_id': event.unique_id
    
    219 220
             }
    
    220 221
             parse_oob(event.stanza, event.additional_data)
    
    221 222
     
    
    ... ... @@ -235,7 +236,7 @@ class Message:
    235 236
                     None,
    
    236 237
                     conn=self._con,
    
    237 238
                     msg_obj=event,
    
    238
    -                stanza_id=event.unique_id))
    
    239
    +                stanza_id=event.stanza_id))
    
    239 240
                 return
    
    240 241
     
    
    241 242
             app.nec.push_incoming_event(
    
    ... ... @@ -245,30 +246,19 @@ class Message:
    245 246
         def _get_unique_id(self, stanza, forwarded, sent, self_message, muc_pm):
    
    246 247
             if stanza.getType() == 'groupchat':
    
    247 248
                 # TODO: Disco the MUC check if 'urn:xmpp:mam:2' is announced
    
    248
    -            return self._get_stanza_id(stanza)
    
    249
    +            return self._get_stanza_id(stanza), None
    
    249 250
     
    
    250 251
             if stanza.getType() != 'chat':
    
    251
    -            return
    
    252
    +            return None, None
    
    252 253
     
    
    253 254
             # Messages we receive live
    
    254 255
             if self._con.get_module('MAM').archiving_namespace != nbxmpp.NS_MAM_2:
    
    255 256
                 # Only mam:2 ensures valid stanza-id
    
    256
    -            return
    
    257
    +            return None, None
    
    257 258
     
    
    258
    -        if forwarded:
    
    259
    -            if sent:
    
    260
    -                if self_message or muc_pm:
    
    261
    -                    return stanza.getOriginID()
    
    262
    -                return self._get_stanza_id(stanza)
    
    263
    -            else:
    
    264
    -                if muc_pm:
    
    265
    -                    return stanza.getOriginID()
    
    266
    -                return self._get_stanza_id(stanza)
    
    267
    -
    
    268
    -        # Normal Message
    
    269
    -        if self_message or muc_pm:
    
    270
    -            return stanza.getOriginID()
    
    271
    -        return self._get_stanza_id(stanza)
    
    259
    +        if self_message:
    
    260
    +            return self._get_stanza_id(stanza), stanza.getOriginID()
    
    261
    +        return self._get_stanza_id(stanza), None
    
    272 262
     
    
    273 263
         def _get_stanza_id(self, stanza):
    
    274 264
             stanza_id, by = stanza.getStanzaIDAttrs()
    

  • gajim/session.py
    ... ... @@ -139,6 +139,9 @@ class ChatControlSession(object):
    139 139
                     additional_data=obj.additional_data,
    
    140 140
                     stanza_id=obj.unique_id)
    
    141 141
     
    
    142
    +        self.conn.get_module('MAM').save_archive_id(
    
    143
    +            None, obj.stanza_id, obj.timestamp)
    
    144
    +
    
    142 145
             if obj.muc_pm and not obj.gc_control:
    
    143 146
                 # This is a carbon of a PM from a MUC we are not currently
    
    144 147
                 # joined. We log it silently without notification.