Skip to content

Commit 0ec4785

Browse files
Merge pull request #999 from esl/muc-light-empty-message-handling
MUC Light empty message handling
2 parents 7fcf4c4 + 9e299d9 commit 0ec4785

File tree

3 files changed

+96
-10
lines changed

3 files changed

+96
-10
lines changed

Extensions/XMPPMUCLight/XMPPRoomLight.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
@property (readonly, nonatomic, strong, nonnull) XMPPJID *roomJID;
2323
@property (readonly, nonatomic, strong, nonnull) NSString *domain;
2424
@property (nonatomic, assign) BOOL shouldStoreAffiliationChangeMessages;
25+
@property (assign) BOOL shouldHandleMemberMessagesWithoutBody;
2526

2627
- (nonnull NSString *)roomname;
2728
- (nonnull NSString *)subject;

Extensions/XMPPMUCLight/XMPPRoomLight.m

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
@interface XMPPRoomLight() {
1717
BOOL shouldStoreAffiliationChangeMessages;
18+
BOOL shouldHandleMemberMessagesWithoutBody;
1819
NSString *roomname;
1920
NSString *subject;
2021
NSArray<NSXMLElement*> *knownMembersList;
@@ -105,6 +106,33 @@ - (void)setShouldStoreAffiliationChangeMessages:(BOOL)newValue
105106
dispatch_async(moduleQueue, block);
106107
}
107108

109+
- (BOOL)shouldHandleMemberMessagesWithoutBody
110+
{
111+
__block BOOL result;
112+
dispatch_block_t block = ^{ @autoreleasepool {
113+
result = shouldHandleMemberMessagesWithoutBody;
114+
}};
115+
116+
if (dispatch_get_specific(moduleQueueTag))
117+
block();
118+
else
119+
dispatch_sync(moduleQueue, block);
120+
121+
return result;
122+
}
123+
124+
- (void)setShouldHandleMemberMessagesWithoutBody:(BOOL)newValue
125+
{
126+
dispatch_block_t block = ^{ @autoreleasepool {
127+
shouldHandleMemberMessagesWithoutBody = newValue;
128+
}};
129+
130+
if (dispatch_get_specific(moduleQueueTag))
131+
block();
132+
else
133+
dispatch_async(moduleQueue, block);
134+
}
135+
108136
- (nonnull NSString *)roomname {
109137
@synchronized(roomname) {
110138
return [roomname copy];
@@ -668,9 +696,9 @@ - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
668696
// Is this a message we need to store (a chat message)?
669697
//
670698
// We store messages that from is full room-id@domain/user-who-sends-message
671-
// and that have something in the body
699+
// and that have something in the body (unless empty messages are allowed)
672700

673-
if ([from isFull] && [message isGroupChatMessageWithBody]) {
701+
if ([from isFull] && [message isGroupChatMessage] && (self.shouldHandleMemberMessagesWithoutBody || [message isMessageWithBody])) {
674702
[xmppRoomLightStorage handleIncomingMessage:message room:self];
675703
[multicastDelegate xmppRoomLight:self didReceiveMessage:message];
676704
}else if(destroyRoom){
@@ -700,7 +728,7 @@ - (void)xmppStream:(XMPPStream *)sender didSendMessage:(XMPPMessage *)message
700728
// A message to all recipients MUST be of type groupchat.
701729
// A message to an individual recipient would have a <body/>.
702730

703-
if ([message isGroupChatMessageWithBody]){
731+
if ([message isGroupChatMessage] && (self.shouldHandleMemberMessagesWithoutBody || [message isMessageWithBody])) {
704732
[xmppRoomLightStorage handleOutgoingMessage:message room:self];
705733
}
706734
}

Xcode/Testing-Shared/XMPPRoomLightCoreDataStorageTests.m

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ - (void)testReceiveMessageWithoutStorage{
3131
[roomLight addDelegate:self delegateQueue:dispatch_get_main_queue()];
3232
[roomLight activate:streamTest];
3333

34-
[streamTest fakeMessageResponse:[self fakeIncomingMessage]];
34+
[streamTest fakeMessageResponse:[self fakeIncomingMessageWithBody:YES]];
3535
[self waitForExpectationsWithTimeout:2 handler:^(NSError * _Nullable error) {
3636
if(error){
3737
XCTFail(@"Expectation Failed with error: %@", error);
@@ -53,7 +53,7 @@ - (void)testReceiveMessageWithStorage{
5353
[roomLight addDelegate:self delegateQueue:dispatch_get_main_queue()];
5454
[roomLight activate:streamTest];
5555

56-
[streamTest fakeMessageResponse:[self fakeIncomingMessage]];
56+
[streamTest fakeMessageResponse:[self fakeIncomingMessageWithBody:YES]];
5757

5858
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
5959
NSManagedObjectContext *context = [storage mainThreadManagedObjectContext];
@@ -135,6 +135,52 @@ - (void)testReceiveAffiliationMessageWithStorage {
135135
}];
136136
}
137137

138+
- (void)testReceiveMessageWithoutBody {
139+
self.checkDelegate = false;
140+
141+
XCTestExpectation *expectation = [self expectationWithDescription:@"receive message without body and correctly stored"];
142+
143+
XMPPRoomLightCoreDataStorage *storage = [[XMPPRoomLightCoreDataStorage alloc] initWithDatabaseFilename:@"testReceiveMessageWithoutBody.sqlite"
144+
storeOptions:nil];
145+
storage.autoRemovePreviousDatabaseFile = YES;
146+
147+
XMPPMockStream *streamTest = [[XMPPMockStream alloc] init];
148+
streamTest.myJID = [XMPPJID jidWithString:@"[email protected]"];
149+
XMPPJID *jid = [XMPPJID jidWithString:@"[email protected]"];
150+
XMPPRoomLight *roomLight = [[XMPPRoomLight alloc] initWithRoomLightStorage:storage jid:jid roomname:@"test" dispatchQueue:nil];
151+
roomLight.shouldHandleMemberMessagesWithoutBody = YES;
152+
[roomLight activate:streamTest];
153+
154+
[streamTest fakeMessageResponse:[self fakeIncomingMessageWithBody:NO]];
155+
156+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
157+
NSManagedObjectContext *context = [storage mainThreadManagedObjectContext];
158+
NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPRoomLightMessageCoreDataStorageObject"
159+
inManagedObjectContext:context];
160+
161+
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"roomJIDStr = %@", jid.full];
162+
163+
NSFetchRequest *request = [[NSFetchRequest alloc] init];
164+
request.entity = entity;
165+
request.predicate = predicate;
166+
167+
NSError *error;
168+
XMPPRoomLightMessageCoreDataStorageObject *roomMessage = [[context executeFetchRequest:request error:&error] firstObject];
169+
XCTAssertNil(error);
170+
XCTAssertEqualObjects(roomMessage.jid.full, @"[email protected]/[email protected]");
171+
XCTAssertNil(roomMessage.body);
172+
XCTAssertEqualObjects(roomMessage.nickname, @"[email protected]");
173+
174+
[expectation fulfill];
175+
});
176+
177+
[self waitForExpectationsWithTimeout:3 handler:^(NSError * _Nullable error) {
178+
if(error){
179+
XCTFail(@"Expectation Failed with error: %@", error);
180+
}
181+
}];
182+
}
183+
138184
- (void)testImportMessage {
139185
self.checkDelegate = false;
140186

@@ -148,7 +194,10 @@ - (void)testImportMessage {
148194
XMPPJID *jid = [XMPPJID jidWithString:@"[email protected]"];
149195
XMPPRoomLight *roomLight = [[XMPPRoomLight alloc] initWithRoomLightStorage:storage jid:jid roomname:@"test" dispatchQueue:nil];
150196

151-
[storage importRemoteArchiveMessage:[self fakeIncomingMessage] withTimestamp:[NSDate dateWithTimeIntervalSinceReferenceDate:0] inRoom:roomLight fromStream:streamTest];
197+
[storage importRemoteArchiveMessage:[self fakeIncomingMessageWithBody:YES]
198+
withTimestamp:[NSDate dateWithTimeIntervalSinceReferenceDate:0]
199+
inRoom:roomLight
200+
fromStream:streamTest];
152201

153202
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
154203
NSManagedObjectContext *context = [storage mainThreadManagedObjectContext];
@@ -194,8 +243,14 @@ - (void)testImportMessageUniquing {
194243
XMPPJID *jid = [XMPPJID jidWithString:@"[email protected]"];
195244
XMPPRoomLight *roomLight = [[XMPPRoomLight alloc] initWithRoomLightStorage:storage jid:jid roomname:@"test" dispatchQueue:nil];
196245

197-
[storage importRemoteArchiveMessage:[self fakeIncomingMessage] withTimestamp:[NSDate dateWithTimeIntervalSinceReferenceDate:0] inRoom:roomLight fromStream:streamTest];
198-
[storage importRemoteArchiveMessage:[self fakeIncomingMessage] withTimestamp:[NSDate dateWithTimeIntervalSinceReferenceDate:0] inRoom:roomLight fromStream:streamTest];
246+
[storage importRemoteArchiveMessage:[self fakeIncomingMessageWithBody:YES]
247+
withTimestamp:[NSDate dateWithTimeIntervalSinceReferenceDate:0]
248+
inRoom:roomLight
249+
fromStream:streamTest];
250+
[storage importRemoteArchiveMessage:[self fakeIncomingMessageWithBody:YES]
251+
withTimestamp:[NSDate dateWithTimeIntervalSinceReferenceDate:0]
252+
inRoom:roomLight
253+
fromStream:streamTest];
199254

200255
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
201256
NSManagedObjectContext *context = [storage mainThreadManagedObjectContext];
@@ -231,14 +286,16 @@ - (void)xmppRoomLight:(XMPPRoomLight *)sender didReceiveMessage:(XMPPMessage *)m
231286
}
232287
}
233288

234-
- (XMPPMessage *)fakeIncomingMessage{
289+
- (XMPPMessage *)fakeIncomingMessageWithBody:(BOOL)shouldIncludeBody {
235290
NSMutableString *s = [NSMutableString string];
236291
[s appendString: @"<message xmlns='jabber:client' \
237292
238293
239294
id='C7A969D8-C711-4516-9313-10EA9927B39B' \
240295
type='groupchat'>"];
241-
[s appendString: @" <body>Yo! 13</body>'"];
296+
if (shouldIncludeBody) {
297+
[s appendString: @"<body>Yo! 13</body>'"];
298+
}
242299
[s appendString: @"</message>"];
243300

244301
NSError *error;

0 commit comments

Comments
 (0)