Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message Archive Management handling remote messages by inferring direction instead. Adds MAM 2 support. #1229

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<plist version="1.0">
<dict>
<key>_XCCurrentVersionName</key>
<string>XMPPMessageArchiving.xcdatamodel</string>
<string>XMPPMessageArchiving 2.xcdatamodel</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22222" systemVersion="23A344" minimumToolsVersion="Xcode 7.3" sourceLanguage="Objective-C" userDefinedModelVersionIdentifier="">
<entity name="XMPPMessageArchiving_Contact_CoreDataObject" representedClassName="XMPPMessageArchiving_Contact_CoreDataObject" syncable="YES">
<attribute name="bareJid" optional="YES" transient="YES" syncable="YES"/>
<attribute name="bareJidStr" attributeType="String" indexed="YES" syncable="YES"/>
<attribute name="mostRecentMessageBody" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="mostRecentMessageOutgoing" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="NO" syncable="YES"/>
<attribute name="mostRecentMessageTimestamp" optional="YES" attributeType="Date" usesScalarValueType="NO" indexed="YES" syncable="YES"/>
<attribute name="streamBareJidStr" attributeType="String" indexed="YES" syncable="YES"/>
</entity>
<entity name="XMPPMessageArchiving_Message_CoreDataObject" representedClassName="XMPPMessageArchiving_Message_CoreDataObject" syncable="YES">
<attribute name="bareJid" optional="YES" transient="YES" syncable="YES"/>
<attribute name="bareJidStr" attributeType="String" indexed="YES" syncable="YES"/>
<attribute name="body" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="composing" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="NO" indexed="YES" syncable="YES"/>
<attribute name="message" optional="YES" transient="YES" syncable="YES"/>
<attribute name="messageId" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="messageStr" attributeType="String" syncable="YES"/>
<attribute name="outgoing" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="NO" indexed="YES" syncable="YES"/>
<attribute name="streamBareJidStr" attributeType="String" indexed="YES" syncable="YES"/>
<attribute name="thread" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="timestamp" attributeType="Date" usesScalarValueType="NO" indexed="YES" syncable="YES"/>
</entity>
</model>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#import "XMPPLogging.h"
#import "NSXMLElement+XEP_0203.h"
#import "XMPPMessage+XEP_0085.h"
#import "XMPPMessage.h"


#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
Expand Down Expand Up @@ -384,8 +386,15 @@ - (BOOL)configureWithParent:(XMPPMessageArchiving *)aParent queue:(dispatch_queu
return [super configureWithParent:aParent queue:queue];
}

- (void)archiveMessage:(XMPPMessage *)message outgoing:(BOOL)isOutgoing xmppStream:(XMPPStream *)xmppStream
- (void)archiveMessage:(XMPPMessage *)message xmppStream:(XMPPStream *)xmppStream
{
// Infer if message is incoming or outgoing (Fixes a bug when Message Archive Managment is used.)
// Obtain the full JID of the current user.
NSString *ownJid = [xmppStream.myJID bare];

// Check if the 'from' JID of the message matches the current user's JID, indicating it's an outgoing message.
BOOL isOutgoing = [[message from].bare isEqualToString:ownJid];

// Message should either have a body, or be a composing notification

NSString *messageBody = [[message elementForName:@"body"] stringValue];
Expand Down Expand Up @@ -416,13 +425,12 @@ - (void)archiveMessage:(XMPPMessage *)message outgoing:(BOOL)isOutgoing xmppStre
}
}

[self scheduleBlock:^{

NSManagedObjectContext *moc = [self managedObjectContext];
XMPPJID *myJid = [self myJIDForXMPPStream:xmppStream];

XMPPJID *messageJid = isOutgoing ? [message to] : [message from];

[self scheduleBlock:^{

NSManagedObjectContext *moc = [self managedObjectContext];
XMPPJID *myJid = [self myJIDForXMPPStream:xmppStream];
XMPPJID *messageJid = isOutgoing ? [message to] : [message from];

// Fetch-n-Update OR Insert new message

XMPPMessageArchiving_Message_CoreDataObject *archivedMessage =
Expand Down Expand Up @@ -456,9 +464,12 @@ - (void)archiveMessage:(XMPPMessage *)message outgoing:(BOOL)isOutgoing xmppStre

didCreateNewArchivedMessage = YES;
}

NSString *messageId = [[message attributeForName:@"id"] stringValue];

archivedMessage.message = message;
archivedMessage.body = messageBody;
archivedMessage.messageId = messageId;

archivedMessage.bareJid = [messageJid bareJID];
archivedMessage.streamBareJidStr = [myJid bare];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

@property (nonatomic, strong) NSString * streamBareJidStr;

@property (nonatomic, strong) NSString * messageId;

/**
* This method is called immediately before the object is inserted into the managedObjectContext.
* At this point, all normal properties have been set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ @implementation XMPPMessageArchiving_Message_CoreDataObject
@dynamic composing;
@dynamic timestamp;
@dynamic streamBareJidStr;
@dynamic messageId;

#pragma mark Transient message

Expand Down Expand Up @@ -73,6 +74,21 @@ - (void)setMessageStr:(NSString *)messageStr
[self didChangeValueForKey:@"messageStr"];
}

#pragma mark - MessageId

- (NSString *)messageId {
[self willAccessValueForKey:@"messageId"];
NSString *tmp = [self primitiveValueForKey:@"messageId"];
[self didAccessValueForKey:@"messageId"];
return tmp;
}

- (void)setMessageId:(NSString *)newMessageId {
[self willChangeValueForKey:@"messageId"];
[self setPrimitiveValue:newMessageId forKey:@"messageId"];
[self didChangeValueForKey:@"messageId"];
}

#pragma mark Transient bareJid

- (XMPPJID *)bareJid
Expand Down
2 changes: 1 addition & 1 deletion Extensions/XEP-0136/XMPPMessageArchiving.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
/**
*
**/
- (void)archiveMessage:(XMPPMessage *)message outgoing:(BOOL)isOutgoing xmppStream:(XMPPStream *)stream;
- (void)archiveMessage:(XMPPMessage *)message xmppStream:(XMPPStream *)stream;

@optional

Expand Down
21 changes: 14 additions & 7 deletions Extensions/XEP-0136/XMPPMessageArchiving.m
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ - (void)setPreferences:(NSXMLElement *)newPreferences
#pragma mark Utilities
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

- (BOOL)shouldArchiveMessage:(XMPPMessage *)message outgoing:(BOOL)isOutgoing xmppStream:(XMPPStream *)xmppStream
- (BOOL)shouldArchiveMessage:(XMPPMessage *)message xmppStream:(XMPPStream *)xmppStream
{
// XEP-0136 Section 2.9: Preferences precedence rules:
//
Expand Down Expand Up @@ -232,6 +232,13 @@ - (BOOL)shouldArchiveMessage:(XMPPMessage *)message outgoing:(BOOL)isOutgoing xm
// a JID value such as "localpart@example.com" matches that exact JID only rather than
// <localpart@example.com/*>.

// Infer if message is incoming or outgoing (Fixes a bug when Message Archive Managment is used.)
// Obtain the full JID of the current user.
NSString *ownJid = [xmppStream.myJID bare];

// Check if the 'from' JID of the message matches the current user's JID, indicating it's an outgoing message.
BOOL isOutgoing = [[message from].bare isEqualToString:ownJid];

XMPPJID *messageJid;
if (isOutgoing)
messageJid = [message to];
Expand Down Expand Up @@ -394,9 +401,9 @@ - (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
XMPPMessage *message = [XMPPMessage messageWithType:@"chat" to:to];
[message addChild:body];

if ([self shouldArchiveMessage:message outgoing:YES xmppStream:sender])
if ([self shouldArchiveMessage:message xmppStream:sender])
{
[xmppMessageArchivingStorage archiveMessage:message outgoing:YES xmppStream:sender];
[xmppMessageArchivingStorage archiveMessage:message xmppStream:sender];
}
}
}
Expand All @@ -412,19 +419,19 @@ - (void)xmppStream:(XMPPStream *)sender didSendMessage:(XMPPMessage *)message
{
XMPPLogTrace();

if ([self shouldArchiveMessage:message outgoing:YES xmppStream:sender])
if ([self shouldArchiveMessage:message xmppStream:sender])
{
[xmppMessageArchivingStorage archiveMessage:message outgoing:YES xmppStream:sender];
[xmppMessageArchivingStorage archiveMessage:message xmppStream:sender];
}
}

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
XMPPLogTrace();

if ([self shouldArchiveMessage:message outgoing:NO xmppStream:sender])
if ([self shouldArchiveMessage:message xmppStream:sender])
{
[xmppMessageArchivingStorage archiveMessage:message outgoing:NO xmppStream:sender];
[xmppMessageArchivingStorage archiveMessage:message xmppStream:sender];
}
}

Expand Down