Revision 8358d691

View differences:

ChatSecure/Classes/Controllers/XMPP/OTRXMPPManager.m
877 877
    
878 878
    [self goOnline];
879 879
    
880
    
881
    
880 882
    // Fetch latest vCard from server so we can update nickname
881 883
    //[self.xmppvCardTempModule fetchvCardTempForJID:self.JID ignoreStorage:YES];
882 884
    
883
    // [self.messageStorage.archiving retrieveMessageArchiveWithFields:nil withResultSet:nil];
885
    //NSDate *lastInteraction = OTRProtocolManager.shared.lastInteractionDate;
886
    [self.databaseConnection asyncReadWithBlock:^(YapDatabaseReadTransaction * _Nonnull transaction) {
887
        OTRXMPPAccount *account = [self.account refetchWithTransaction:transaction];
888
        [self.messageStorage.archiving fetchHistoryWithArchiveJID:nil userJID:nil since:account.lastHistoryFetchDate];
889
    }];
890
    //[self.messageStorage.archiving fetchHistoryWithArchiveJID:nil userJID:nil since:lastInteraction];
891
    
892
    //[self.messageStorage.archiving retrieveMessageArchiveWithFields:nil withResultSet:nil];
884 893
}
885 894

  
886 895
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
ChatSecure/Classes/Controllers/XMPP/Storage/MessageStorage.swift
39 39
        self.capabilities = capabilities
40 40
        self.carbons = XMPPMessageCarbons(dispatchQueue: dispatchQueue)
41 41
        self.archiving = XMPPMessageArchiveManagement(dispatchQueue: dispatchQueue)
42
        self.archiving.resultAutomaticPagingPageSize = NSNotFound
42 43
        self.fileTransfer = fileTransfer
43 44
        super.init(dispatchQueue: dispatchQueue)
44 45
        self.carbons.addDelegate(self, delegateQueue: self.moduleQueue)
......
106 107
    }
107 108
    
108 109
    /// It is a violation of the XMPP spec to discard messages with duplicate stanza elementIds. We must use XEP-0359 stanza-id only.
109
    private func isDuplicate(xmppMessage: XMPPMessage, stanzaId: String, buddyUniqueId: String, transaction: YapDatabaseReadTransaction) -> Bool {
110
    private func isDuplicate(message: OTRBaseMessage, transaction: YapDatabaseReadTransaction) -> Bool {
110 111
        var result = false
111
        transaction.enumerateMessages(elementId: nil, originId: nil, stanzaId: stanzaId) { (message, stop) in
112
        let buddyUniqueId = message.buddyUniqueId
113
        let oid = message.originId
114
        let sid = message.stanzaId
115
        if oid == nil, sid == nil {
116
            return false
117
        }
118
        transaction.enumerateMessages(elementId: nil, originId: oid, stanzaId: sid) { (message, stop) in
112 119
            if message.threadId == buddyUniqueId {
113 120
                result = true
114 121
                stop.pointee = true
......
156 163
            }
157 164
            
158 165
            // Bail out if we receive duplicate messages identified by XEP-0359
159
            if let stanzaId = message.stanzaId,
160
            self.isDuplicate(xmppMessage: xmppMessage, stanzaId: stanzaId, buddyUniqueId: buddy.uniqueId, transaction: transaction) {
166
            if self.isDuplicate(message: message, transaction: transaction) {
161 167
                DDLogWarn("Duplicate forwarded message received: \(xmppMessage)")
162 168
                return
163 169
            }
......
178 184
                                    body: String?,
179 185
                                    accountId: String,
180 186
                                    preSave: PreSave? = nil) {
181
        var incomingMessage: OTRIncomingMessage? = nil
187
        //var incomingMessage: OTRIncomingMessage? = nil
182 188
        connection.asyncReadWrite({ (transaction) in
183 189
            guard let account = OTRXMPPAccount.fetchObject(withUniqueID: accountId, transaction: transaction),
184 190
                let fromJID = message.from,
......
221 227
                return
222 228
            }
223 229
            
224
            incomingMessage = OTRIncomingMessage(xmppMessage: message, body: body, account: account, buddy: buddy, capabilities: self.capabilities)
230
            let incoming = OTRIncomingMessage(xmppMessage: message, body: body, account: account, buddy: buddy, capabilities: self.capabilities)
225 231
            
226 232
            // Check for duplicates
227
            if let stanzaId = incomingMessage?.stanzaId,
228
                self.isDuplicate(xmppMessage: message, stanzaId: stanzaId, buddyUniqueId: buddy.uniqueId, transaction: transaction) {
233
            if self.isDuplicate(message: incoming, transaction: transaction) {
229 234
                DDLogWarn("Duplicate message received: \(message)")
230 235
                return
231 236
            }
232
            guard let incoming = incomingMessage, let text = incoming.text, text.count > 0 else {
237
            guard let text = incoming.text, text.count > 0 else {
233 238
                // discard empty message text
234 239
                return
235 240
            }
......
297 302
}
298 303

  
299 304
extension MessageStorage: XMPPMessageArchiveManagementDelegate {
305
    public func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didFinishReceivingMessagesWith resultSet: XMPPResultSet) {
306
        connection.asyncReadWrite { (transaction) in
307
            guard let accountId = xmppMessageArchiveManagement.xmppStream?.accountId,
308
                let account = OTRXMPPAccount.fetchObject(withUniqueID: accountId, transaction: transaction)?.copyAsSelf() else {
309
                    return
310
            }
311
            account.lastHistoryFetchDate = Date()
312
            account.save(with: transaction)
313
        }
314
    }
315
    
300 316
    public func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didFailToReceiveMessages error: XMPPIQ) {
301 317
        DDLogError("Failed to receive messages \(error)")
302 318
    }
ChatSecure/Classes/Controllers/XMPP/XMPPManager.swift
9 9
import Foundation
10 10
import XMPPFramework
11 11

  
12
extension XMPPMessageArchiveManagement {
13
    public func fetchHistory(archiveJID: XMPPJID? = nil, userJID: XMPPJID? = nil, since: Date? = nil) {
12
public extension XMPPMessageArchiveManagement {
13
    @objc public func fetchHistory(archiveJID: XMPPJID? = nil, userJID: XMPPJID? = nil, since: Date? = nil) {
14 14
        var fields: [XMLElement] = []
15 15
        
16 16
        if let userJID = userJID {
......
24 24
            let start = XMPPMessageArchiveManagement.field(withVar: "start", type: nil, andValue: xmppDateString)
25 25
            fields.append(start)
26 26
        }
27
        retrieveMessageArchive(at: archiveJID, withFields: fields, with: nil)
27
        retrieveMessageArchive(at: archiveJID ?? xmppStream?.myJID?.bareJID, withFields: fields, with: nil)
28 28
    }
29 29
}
30 30

  
ChatSecure/Classes/Model/Yap Storage/Accounts/OTRXMPPAccount.h
21 21
@property (nonatomic, strong, nullable) NSString *pushPubsubEndpoint;
22 22
@property (nonatomic, strong, nullable) NSString *pushPubsubNode;
23 23

  
24
/** For usage with MAM */
25
@property (nonatomic, strong, nullable) NSDate *lastHistoryFetchDate;
26

  
24 27
+ (uint16_t)defaultPort;
25 28
+ (NSString *)newResource;
26 29

  
ChatSecure/Classes/View Controllers/OTRMessagesViewController.m
223 223
        [self scrollToBottomAnimated:animated];
224 224
    });
225 225
    self.loadingMessages = NO;
226
    [self fetchMessageHistory];
227 226
}
228 227

  
229 228
- (void)viewWillAppear:(BOOL)animated
......
323 322

  
324 323
#pragma - mark Setters & getters
325 324

  
326
- (void) fetchMessageHistory {
327
    [self.readOnlyDatabaseConnection asyncReadWithBlock:^(YapDatabaseReadTransaction * _Nonnull transaction) {
328
        id<OTRThreadOwner> thread = [self threadObjectWithTransaction:transaction];
329
        OTRXMPPManager *xmpp = [self xmppManagerWithTransaction:transaction];
330
        [xmpp fetchHistoryForThread:thread transaction:transaction];
331
    }];
332
}
333

  
334 325
- (OTRAttachmentPicker *)attachmentPicker
335 326
{
336 327
    if (!_attachmentPicker) {
ChatSecure/Info.plist
15 15
	<key>CFBundlePackageType</key>
16 16
	<string>APPL</string>
17 17
	<key>CFBundleShortVersionString</key>
18
	<string>4.2.2</string>
18
	<string>4.3.0</string>
19 19
	<key>CFBundleSignature</key>
20 20
	<string>????</string>
21 21
	<key>CFBundleURLTypes</key>
......
35 35
		</dict>
36 36
	</array>
37 37
	<key>CFBundleVersion</key>
38
	<string>111</string>
38
	<string>113</string>
39 39
	<key>FacebookAppID</key>
40 40
	<string>447241325394334</string>
41 41
	<key>FacebookDisplayName</key>
Submodules/XMPPFramework
1
Subproject commit f5f1cea4d4553fed05fe388a747c73398ca7568a
1
Subproject commit b1a3bad5431020171d2c01fbd1b55dcb65876ce3

Also available in: Unified diff