Respond to CR.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 2a5a0929e6
commit 13a6657991

@ -108,6 +108,10 @@ NSString *const TSAccountManager_LocalRegistrationIdKey = @"TSStorageLocalRegist
[[NSNotificationCenter defaultCenter] postNotificationNameAsync:kNSNotificationName_RegistrationStateDidChange [[NSNotificationCenter defaultCenter] postNotificationNameAsync:kNSNotificationName_RegistrationStateDidChange
object:nil object:nil
userInfo:nil]; userInfo:nil];
// Warm these cached values.
[self isRegistered];
[self localNumber];
} }
+ (nullable NSString *)localNumber + (nullable NSString *)localNumber

@ -307,23 +307,20 @@ NS_ASSUME_NONNULL_BEGIN
} }
if (dataMessage.hasGroup) { if (dataMessage.hasGroup) {
TSGroupThread *_Nullable gThread = TSGroupThread *_Nullable groupThread =
[TSGroupThread threadWithGroupId:dataMessage.group.id transaction:transaction]; [TSGroupThread threadWithGroupId:dataMessage.group.id transaction:transaction];
BOOL unknownGroup = NO;
if (gThread == nil && dataMessage.group.type != OWSSignalServiceProtosGroupContextTypeUpdate) { if (!groupThread) {
unknownGroup = YES; // Unknown group.
} if (dataMessage.group.type == OWSSignalServiceProtosGroupContextTypeUpdate) {
if (unknownGroup) { // Accept group updates for unknown groups.
if (dataMessage.group.type == OWSSignalServiceProtosGroupContextTypeRequestInfo) { } else if (dataMessage.group.type == OWSSignalServiceProtosGroupContextTypeDeliver) {
DDLogInfo(@"%@ Ignoring group info request for unknown group from: %@", self.tag, envelope.source); [self sendGroupInfoRequest:dataMessage.group.id envelope:envelope transaction:transaction];
return; return;
} else if (dataMessage.group.type == OWSSignalServiceProtosGroupContextTypeQuit) { } else {
DDLogInfo(@"%@ Ignoring group quit for unknown group from: %@", self.tag, envelope.source); DDLogInfo(@"%@ Ignoring group message for unknown group from: %@", self.tag, envelope.source);
return; return;
} }
[self sendGroupInfoRequest:dataMessage.group.id envelope:envelope transaction:transaction];
return;
} }
} }
@ -846,13 +843,6 @@ NS_ASSUME_NONNULL_BEGIN
NSString *body = dataMessage.body; NSString *body = dataMessage.body;
NSData *groupId = dataMessage.hasGroup ? dataMessage.group.id : nil; NSData *groupId = dataMessage.hasGroup ? dataMessage.group.id : nil;
__block TSIncomingMessage *_Nullable incomingMessage;
__block TSThread *thread;
// Do this outside of a transaction to avoid deadlock
OWSAssert([TSAccountManager isRegistered]);
NSString *localNumber = [TSAccountManager localNumber];
if (dataMessage.group.type == OWSSignalServiceProtosGroupContextTypeRequestInfo) { if (dataMessage.group.type == OWSSignalServiceProtosGroupContextTypeRequestInfo) {
[self handleGroupInfoRequest:envelope dataMessage:dataMessage transaction:transaction]; [self handleGroupInfoRequest:envelope dataMessage:dataMessage transaction:transaction];
return nil; return nil;
@ -866,9 +856,9 @@ NS_ASSUME_NONNULL_BEGIN
// We distinguish between the old group state (if any) and the new group state. // We distinguish between the old group state (if any) and the new group state.
TSGroupThread *_Nullable oldGroupThread = [TSGroupThread threadWithGroupId:groupId transaction:transaction]; TSGroupThread *_Nullable oldGroupThread = [TSGroupThread threadWithGroupId:groupId transaction:transaction];
if (oldGroupThread) { if (oldGroupThread) {
// Don't trust other clients; ensure all known group members leave the group // Don't trust other clients; ensure all known group members remain in the
// _unless_ it is a "quit" message in which case we should explicitly remove // group unless it is a "quit" message in which case we should only remove
// just the quiting member below. // the quiting member below.
[newMemberIds addObjectsFromArray:oldGroupThread.groupModel.groupMemberIds]; [newMemberIds addObjectsFromArray:oldGroupThread.groupModel.groupMemberIds];
} }
@ -880,7 +870,7 @@ NS_ASSUME_NONNULL_BEGIN
TSGroupModel *newGroupModel = [[TSGroupModel alloc] initWithTitle:dataMessage.group.name TSGroupModel *newGroupModel = [[TSGroupModel alloc] initWithTitle:dataMessage.group.name
memberIds:[newMemberIds.allObjects mutableCopy] memberIds:[newMemberIds.allObjects mutableCopy]
image:nil image:oldGroupThread.groupModel.groupImage
groupId:dataMessage.group.id]; groupId:dataMessage.group.id];
NSString *updateGroupInfo = [newGroupThread.groupModel getInfoStringAboutUpdateTo:newGroupModel NSString *updateGroupInfo = [newGroupThread.groupModel getInfoStringAboutUpdateTo:newGroupModel
contactsManager:self.contactsManager]; contactsManager:self.contactsManager];
@ -891,8 +881,7 @@ NS_ASSUME_NONNULL_BEGIN
inThread:newGroupThread inThread:newGroupThread
messageType:TSInfoMessageTypeGroupUpdate messageType:TSInfoMessageTypeGroupUpdate
customMessage:updateGroupInfo] saveWithTransaction:transaction]; customMessage:updateGroupInfo] saveWithTransaction:transaction];
thread = newGroupThread; return nil;
break;
} }
case OWSSignalServiceProtosGroupContextTypeQuit: { case OWSSignalServiceProtosGroupContextTypeQuit: {
if (!oldGroupThread) { if (!oldGroupThread) {
@ -910,12 +899,11 @@ NS_ASSUME_NONNULL_BEGIN
inThread:oldGroupThread inThread:oldGroupThread
messageType:TSInfoMessageTypeGroupUpdate messageType:TSInfoMessageTypeGroupUpdate
customMessage:updateGroupInfo] saveWithTransaction:transaction]; customMessage:updateGroupInfo] saveWithTransaction:transaction];
thread = oldGroupThread; return nil;
break;
} }
case OWSSignalServiceProtosGroupContextTypeDeliver: { case OWSSignalServiceProtosGroupContextTypeDeliver: {
if (!oldGroupThread) { if (!oldGroupThread) {
OWSFail(@"%@ ignoring quit group message from unknown group.", self.tag); OWSFail(@"%@ ignoring deliver group message from unknown group.", self.tag);
return nil; return nil;
} }
@ -933,17 +921,21 @@ NS_ASSUME_NONNULL_BEGIN
envelopeAddress(envelope), envelopeAddress(envelope),
groupId, groupId,
(unsigned long)timestamp); (unsigned long)timestamp);
incomingMessage = [[TSIncomingMessage alloc] initWithTimestamp:timestamp TSIncomingMessage *incomingMessage =
inThread:oldGroupThread [[TSIncomingMessage alloc] initWithTimestamp:timestamp
authorId:envelope.source inThread:oldGroupThread
sourceDeviceId:envelope.sourceDevice authorId:envelope.source
messageBody:body sourceDeviceId:envelope.sourceDevice
attachmentIds:attachmentIds messageBody:body
expiresInSeconds:dataMessage.expireTimer]; attachmentIds:attachmentIds
expiresInSeconds:dataMessage.expireTimer];
[incomingMessage saveWithTransaction:transaction]; [self finalizeIncomingMessage:incomingMessage
thread = oldGroupThread; thread:oldGroupThread
break; envelope:envelope
dataMessage:dataMessage
attachmentIds:attachmentIds
transaction:transaction];
return incomingMessage;
} }
default: { default: {
DDLogWarn(@"%@ Ignoring unknown group message type: %d", self.tag, (int)dataMessage.group.type); DDLogWarn(@"%@ Ignoring unknown group message type: %d", self.tag, (int)dataMessage.group.type);
@ -963,69 +955,97 @@ NS_ASSUME_NONNULL_BEGIN
self.tag, self.tag,
envelopeAddress(envelope), envelopeAddress(envelope),
(unsigned long)timestamp); (unsigned long)timestamp);
TSContactThread *cThread = [TSContactThread getOrCreateThreadWithContactId:envelope.source TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:envelope.source
transaction:transaction transaction:transaction
relay:envelope.relay]; relay:envelope.relay];
incomingMessage = [[TSIncomingMessage alloc] initWithTimestamp:timestamp TSIncomingMessage *incomingMessage = [[TSIncomingMessage alloc] initWithTimestamp:timestamp
inThread:cThread inThread:thread
authorId:[cThread contactIdentifier] authorId:[thread contactIdentifier]
sourceDeviceId:envelope.sourceDevice sourceDeviceId:envelope.sourceDevice
messageBody:body messageBody:body
attachmentIds:attachmentIds attachmentIds:attachmentIds
expiresInSeconds:dataMessage.expireTimer]; expiresInSeconds:dataMessage.expireTimer];
[self finalizeIncomingMessage:incomingMessage
[incomingMessage saveWithTransaction:transaction]; thread:thread
thread = cThread; envelope:envelope
dataMessage:dataMessage
attachmentIds:attachmentIds
transaction:transaction];
return incomingMessage;
} }
}
- (void)finalizeIncomingMessage:(TSIncomingMessage *)incomingMessage
thread:(TSThread *)thread
envelope:(OWSSignalServiceProtosEnvelope *)envelope
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
attachmentIds:(NSArray<NSString *> *)attachmentIds
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssert(thread); OWSAssert(thread);
OWSAssert(incomingMessage); OWSAssert(incomingMessage);
OWSAssert(envelope);
OWSAssert(dataMessage);
OWSAssert(attachmentIds);
OWSAssert(transaction);
if (thread && incomingMessage) { OWSAssert([TSAccountManager isRegistered]);
// Any messages sent from the current user - from this device or another - should be NSString *localNumber = [TSAccountManager localNumber];
// automatically marked as read. NSString *body = dataMessage.body;
BOOL shouldMarkMessageAsRead = [envelope.source isEqualToString:localNumber]; uint64_t timestamp = envelope.timestamp;
if (shouldMarkMessageAsRead) {
// Don't send a read receipt for messages sent by ourselves. if (!thread) {
[incomingMessage markAsReadWithTransaction:transaction sendReadReceipt:NO updateExpiration:YES]; OWSFail(@"%@ Can't finalize without thread", self.tag);
} return;
}
if (!incomingMessage) {
OWSFail(@"%@ Can't finalize missing message", self.tag);
return;
}
DDLogDebug(@"%@ shouldMarkMessageAsRead: %d (%@)", self.tag, shouldMarkMessageAsRead, envelope.source); [incomingMessage saveWithTransaction:transaction];
// Other clients allow attachments to be sent along with body, we want the text displayed as a separate // Any messages sent from the current user - from this device or another - should be
// message // automatically marked as read.
if ([attachmentIds count] > 0 && body != nil && body.length > 0) { BOOL shouldMarkMessageAsRead = [envelope.source isEqualToString:localNumber];
// We want the text to be displayed under the attachment. if (shouldMarkMessageAsRead) {
uint64_t textMessageTimestamp = timestamp + 1; // Don't send a read receipt for messages sent by ourselves.
TSIncomingMessage *textMessage = [[TSIncomingMessage alloc] initWithTimestamp:textMessageTimestamp [incomingMessage markAsReadWithTransaction:transaction sendReadReceipt:NO updateExpiration:YES];
inThread:thread }
authorId:envelope.source
sourceDeviceId:envelope.sourceDevice
messageBody:body
attachmentIds:@[]
expiresInSeconds:dataMessage.expireTimer];
DDLogDebug(@"%@ incoming extra text message: %@", self.tag, incomingMessage.debugDescription);
[textMessage saveWithTransaction:transaction];
}
// In case we already have a read receipt for this new message (this happens sometimes). DDLogDebug(@"%@ shouldMarkMessageAsRead: %d (%@)", self.tag, shouldMarkMessageAsRead, envelope.source);
[OWSReadReceiptManager.sharedManager applyEarlyReadReceiptsForIncomingMessage:incomingMessage
transaction:transaction]; // Other clients allow attachments to be sent along with body, we want the text displayed as a separate
// message
if ([attachmentIds count] > 0 && body != nil && body.length > 0) {
// We want the text to be displayed under the attachment.
uint64_t textMessageTimestamp = timestamp + 1;
TSIncomingMessage *textMessage = [[TSIncomingMessage alloc] initWithTimestamp:textMessageTimestamp
inThread:thread
authorId:envelope.source
sourceDeviceId:envelope.sourceDevice
messageBody:body
attachmentIds:@[]
expiresInSeconds:dataMessage.expireTimer];
DDLogDebug(@"%@ incoming extra text message: %@", self.tag, incomingMessage.debugDescription);
[textMessage saveWithTransaction:transaction];
}
[OWSDisappearingMessagesJob becomeConsistentWithConfigurationForMessage:incomingMessage // In case we already have a read receipt for this new message (this happens sometimes).
contactsManager:self.contactsManager]; [OWSReadReceiptManager.sharedManager applyEarlyReadReceiptsForIncomingMessage:incomingMessage
transaction:transaction];
// Update thread preview in inbox [OWSDisappearingMessagesJob becomeConsistentWithConfigurationForMessage:incomingMessage
[thread touchWithTransaction:transaction]; contactsManager:self.contactsManager];
[[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForIncomingMessage:incomingMessage // Update thread preview in inbox
inThread:thread [thread touchWithTransaction:transaction];
contactsManager:self.contactsManager
transaction:transaction];
}
return incomingMessage; [[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForIncomingMessage:incomingMessage
inThread:thread
contactsManager:self.contactsManager
transaction:transaction];
} }
#pragma mark - helpers #pragma mark - helpers

Loading…
Cancel
Save