Merge pull request #33 from loki-project/friend-request-changes

Friend request changes
This commit is contained in:
gamabuntan 2019-07-25 14:41:46 +10:00 committed by GitHub
commit bb65c87906
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 41 deletions

View file

@ -1,5 +1,6 @@
#import "LKEphemeralMessage.h"
#import <SignalCoreKit/NSDate+OWS.h>
#import <SignalServiceKit/SignalServiceKit-Swift.h>
@implementation LKEphemeralMessage
@ -8,6 +9,20 @@
expiresInSeconds:0 expireStartedAt:0 isVoiceMessage:NO groupMetaMessage:TSGroupMetaMessageUnspecified quotedMessage:nil contactShare:nil linkPreview:nil];
}
// An EphemeralMessage does not have any data message in the content
- (nullable NSData *)buildPlainTextData:(SignalRecipient *)recipient
{
NSError *error;
SSKProtoContentBuilder *contentBuilder = [self contentBuilder:recipient];
NSData *_Nullable contentData = [contentBuilder buildSerializedDataAndReturnError:&error];
if (error || !contentData) {
OWSFailDebug(@"could not serialize protobuf: %@", error);
return nil;
}
return contentData;
}
- (BOOL)shouldSyncTranscript { return NO; }
- (BOOL)shouldBeSaved { return NO; }

View file

@ -414,6 +414,9 @@ NS_ASSUME_NONNULL_BEGIN
return;
}
// Loki: Handle any friend request accepts if we need to
[self handleFriendRequestAcceptIfNeededWithEnvelope:envelope transaction:transaction];
if (envelope.content != nil) {
NSError *error;
SSKProtoContent *_Nullable contentProto = [SSKProtoContent parseData:plaintextData error:&error];
@ -1383,7 +1386,7 @@ NS_ASSUME_NONNULL_BEGIN
}
// Loki: Do this before the check below
[self handleFriendRequestIfNeededWithEnvelope:envelope message:incomingMessage thread:oldGroupThread transaction:transaction];
[self handleFriendRequestMessageIfNeededWithEnvelope:envelope message:incomingMessage thread:oldGroupThread transaction:transaction];
if (body.length == 0 && attachmentPointers.count < 1 && !contact) {
OWSLogWarn(@"ignoring empty incoming message from: %@ for group: %@ with timestamp: %lu",
@ -1458,7 +1461,7 @@ NS_ASSUME_NONNULL_BEGIN
}
// Loki: Do this before the check below
[self handleFriendRequestIfNeededWithEnvelope:envelope message:incomingMessage thread:thread transaction:transaction];
[self handleFriendRequestMessageIfNeededWithEnvelope:envelope message:incomingMessage thread:thread transaction:transaction];
if (body.length == 0 && attachmentPointers.count < 1 && !contact) {
OWSLogWarn(@"ignoring empty incoming message from: %@ with timestamp: %lu",
@ -1486,9 +1489,12 @@ NS_ASSUME_NONNULL_BEGIN
}
}
- (void)handleFriendRequestIfNeededWithEnvelope:(SSKProtoEnvelope *)envelope message:(TSIncomingMessage *)message thread:(TSThread *)thread transaction:(YapDatabaseReadWriteTransaction *)transaction
{
if (envelope.type == SSKProtoEnvelopeTypeFriendRequest) {
// The difference between this function and `handleFriendRequestAcceptIfNeededWithEnvelope:` is that this will setup the incoming message for display to the user
// While `handleFriendRequestAcceptIfNeededWithEnvelope:` handles friend request accepting logic and doesn't need a message
- (void)handleFriendRequestMessageIfNeededWithEnvelope:(SSKProtoEnvelope *)envelope message:(TSIncomingMessage *)message thread:(TSThread *)thread transaction:(YapDatabaseReadWriteTransaction *)transaction {
// Check if it's a friend request message
if (envelope.type != SSKProtoEnvelopeTypeFriendRequest) return;
if (thread.hasCurrentUserSentFriendRequest) {
// This can happen if Alice sent Bob a friend request, Bob declined, but then Bob changed his
// mind and sent a friend request to Alice. In this case we want Alice to auto-accept the request
@ -1506,8 +1512,8 @@ NS_ASSUME_NONNULL_BEGIN
[existingFriendRequestMessage saveFriendRequestStatus:LKMessageFriendRequestStatusAccepted withTransaction:transaction];
}
// The two lines below are equivalent to calling [ThreadUtil enqueueAcceptFriendRequestMessageInThread:thread]
LKEphemeralMessage *emptyMessage = [[LKEphemeralMessage alloc] initInThread:thread];
[self.messageSenderJobQueue addMessage:emptyMessage transaction:transaction];
LKEphemeralMessage *backgroundMessage = [[LKEphemeralMessage alloc] initInThread:thread];
[self.messageSenderJobQueue addMessage:backgroundMessage transaction:transaction];
} else if (!thread.isContactFriend) {
// Checking that the sender of the message isn't already a friend is necessary because otherwise
// the following situation can occur: Alice and Bob are friends. Bob loses his database and his
@ -1517,9 +1523,20 @@ NS_ASSUME_NONNULL_BEGIN
[thread saveFriendRequestStatus:LKThreadFriendRequestStatusRequestReceived withTransaction:transaction];
message.friendRequestStatus = LKMessageFriendRequestStatusPending; // Don't save yet. This is done in finalizeIncomingMessage:thread:envelope:transaction.
}
} else if (!thread.isContactFriend) {
// If the thread's friend request status is not LKThreadFriendRequestStatusFriends, but we're receiving a message,
// it must be a friend request accepted message. Declining a friend request doesn't send a message.
}
- (void)handleFriendRequestAcceptIfNeededWithEnvelope:(SSKProtoEnvelope *)envelope transaction:(YapDatabaseReadWriteTransaction *)transaction {
// If we get any other envelope type then we can assume that we had to use signal cipher decryption
// and that means we must have a session with the other person.
if (envelope.type == SSKProtoEnvelopeTypeFriendRequest) return;
// If we're already friends then there's no point in continuing
// TODO: We'll need to fix this up if we ever start using Sync messages
// Currently it'll use `envelope.source` but with sync messages we need to use the message sender id
TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:envelope.source transaction:transaction];
if (thread.isContactFriend) return;
// Become happy friends and go on great adventures
[thread saveFriendRequestStatus:LKThreadFriendRequestStatusFriends withTransaction:transaction];
TSOutgoingMessage *existingFriendRequestMessage = (TSOutgoingMessage *)[thread.lastInteraction as:TSOutgoingMessage.class];
if (existingFriendRequestMessage != nil && existingFriendRequestMessage.isFriendRequest) {
@ -1531,7 +1548,6 @@ NS_ASSUME_NONNULL_BEGIN
[self.messageSenderJobQueue addMessage:onlineMessage transaction:transaction];
}
}
}
- (void)finalizeIncomingMessage:(TSIncomingMessage *)incomingMessage
thread:(TSThread *)thread