Redo friend request handling

pull/15/head
Niels Andriesse 6 years ago
parent 0a69ac3fa3
commit 3161680432

@ -4,6 +4,11 @@
@objc weak var delegate: FriendRequestViewDelegate? @objc weak var delegate: FriendRequestViewDelegate?
private let kind: Kind private let kind: Kind
private var didAcceptRequest: Bool {
guard let message = message as? TSIncomingMessage else { preconditionFailure() }
return message.thread.friendRequestStatus == .friends
}
private var didDeclineRequest: Bool { private var didDeclineRequest: Bool {
guard let message = message as? TSIncomingMessage else { preconditionFailure() } guard let message = message as? TSIncomingMessage else { preconditionFailure() }
return message.thread.friendRequestStatus == .none return message.thread.friendRequestStatus == .none
@ -81,7 +86,9 @@
guard let message = message as? TSIncomingMessage else { preconditionFailure() } guard let message = message as? TSIncomingMessage else { preconditionFailure() }
buttonStackView.isHidden = didDeclineRequest buttonStackView.isHidden = didDeclineRequest
let text: String = { let text: String = {
if didDeclineRequest { if didAcceptRequest {
return String(format: NSLocalizedString("You've accepted %@'s friend request", comment: ""), message.authorId)
} else if didDeclineRequest {
return String(format: NSLocalizedString("You've declined %@'s friend request", comment: ""), message.authorId) return String(format: NSLocalizedString("You've declined %@'s friend request", comment: ""), message.authorId)
} else { } else {
return String(format: NSLocalizedString("%@ sent you a friend request", comment: ""), message.authorId) return String(format: NSLocalizedString("%@ sent you a friend request", comment: ""), message.authorId)

@ -124,18 +124,6 @@ NS_ASSUME_NONNULL_BEGIN
return outgoingMessage.messageState == TSOutgoingMessageStateFailed; return outgoingMessage.messageState == TSOutgoingMessageStateFailed;
} }
- (TSThreadFriendRequestStatus)friendRequestStatus
{
return [self.message.thread getFriendRequestStatus];
}
- (BOOL)isFriendRequest
{
TSThreadFriendRequestStatus friendRequestStatus = self.friendRequestStatus;
return friendRequestStatus == TSThreadFriendRequestStatusPendingSend || friendRequestStatus == TSThreadFriendRequestStatusRequestSent
|| friendRequestStatus == TSThreadFriendRequestStatusRequestReceived;
}
#pragma mark - Load #pragma mark - Load
- (void)loadForDisplay - (void)loadForDisplay
@ -218,10 +206,10 @@ NS_ASSUME_NONNULL_BEGIN
} }
} }
if (self.isFriendRequest) { if (self.message.isFriendRequest) {
// At this point, self.friendRequestStatus should be either TSThreadFriendRequestStatusRequestReceived, // At this point, self.message.thread.friendRequestStatus should be either TSThreadFriendRequestStatusRequestReceived,
// TSThreadFriendRequestStatusPendingSend or TSThreadFriendRequestStatusRequestSent // TSThreadFriendRequestStatusPendingSend or TSThreadFriendRequestStatusRequestSent
NSString *rawKind = self.friendRequestStatus == TSThreadFriendRequestStatusRequestReceived ? @"incoming" : @"outgoing"; NSString *rawKind = self.message.interactionType == OWSInteractionType_IncomingMessage ? @"incoming" : @"outgoing";
self.friendRequestView = [[FriendRequestView alloc] initWithRawKind:rawKind]; self.friendRequestView = [[FriendRequestView alloc] initWithRawKind:rawKind];
self.friendRequestView.message = self.message; self.friendRequestView.message = self.message;
self.friendRequestView.delegate = self.friendRequestViewDelegate; self.friendRequestView.delegate = self.friendRequestViewDelegate;
@ -372,7 +360,7 @@ NS_ASSUME_NONNULL_BEGIN
cellSize.width += self.sendFailureBadgeSize + self.sendFailureBadgeSpacing; cellSize.width += self.sendFailureBadgeSize + self.sendFailureBadgeSpacing;
} }
if (self.isFriendRequest) { if (self.message.isFriendRequest) {
cellSize.height += [FriendRequestView calculateHeightWithMessage:self.message conversationStyle:self.conversationStyle]; cellSize.height += [FriendRequestView calculateHeightWithMessage:self.message conversationStyle:self.conversationStyle];
} }
@ -454,7 +442,7 @@ NS_ASSUME_NONNULL_BEGIN
{ {
OWSAssertDebug(self.delegate); OWSAssertDebug(self.delegate);
if (self.isFriendRequest) { if (self.message.isFriendRequest) {
return; return;
} }

@ -1587,11 +1587,9 @@ typedef enum : NSUInteger {
#pragma mark - Updating #pragma mark - Updating
- (void)updateInputToolbar { - (void)updateInputToolbar {
TSThreadFriendRequestStatus friendRequestStatus = [self.thread getFriendRequestStatus]; BOOL hasPendingFriendRequest = self.thread.hasPendingFriendRequest;
BOOL isFriendRequestPending = friendRequestStatus == TSThreadFriendRequestStatusPendingSend || friendRequestStatus == TSThreadFriendRequestStatusRequestSent [self.inputToolbar setUserInteractionEnabled:!hasPendingFriendRequest];
|| friendRequestStatus == TSThreadFriendRequestStatusRequestReceived; NSString *placeholderText = hasPendingFriendRequest ? NSLocalizedString(@"Pending Friend Request...", "") : NSLocalizedString(@"New Message", "");
[self.inputToolbar setUserInteractionEnabled:!isFriendRequestPending];
NSString *placeholderText = isFriendRequestPending ? NSLocalizedString(@"Pending Friend Request...", "") : NSLocalizedString(@"New Message", "");
[self.inputToolbar setPlaceholderText:placeholderText]; [self.inputToolbar setPlaceholderText:placeholderText];
} }
@ -4306,6 +4304,7 @@ typedef enum : NSUInteger {
{ {
// Reset friend request status // Reset friend request status
self.thread.friendRequestStatus = TSThreadFriendRequestStatusNone; self.thread.friendRequestStatus = TSThreadFriendRequestStatusNone;
[self.thread save];
// Delete prekeys // Delete prekeys
NSString *contactID = self.thread.recipientIdentifiers.firstObject; NSString *contactID = self.thread.recipientIdentifiers.firstObject;
OWSPrimaryStorage *primaryStorage = SSKEnvironment.shared.primaryStorage; OWSPrimaryStorage *primaryStorage = SSKEnvironment.shared.primaryStorage;

@ -2577,4 +2577,5 @@
"Pending Friend Request..." = "Pending Friend Request..."; "Pending Friend Request..." = "Pending Friend Request...";
"New Message" = "New Message"; "New Message" = "New Message";
"Session" = "Session"; "Session" = "Session";
"You've declined %@'s friend request" = "You've declined %@'s friend request" "You've declined %@'s friend request" = "You've declined %@'s friend request";
"You've accepted %@'s friend request" = "You've accepted %@'s friend request";

@ -185,8 +185,8 @@ typedef void (^BuildOutgoingMessageCompletionBlock)(TSOutgoingMessage *savedMess
BOOL isVoiceMessage = (attachments.count == 1 && attachments.lastObject.isVoiceMessage); BOOL isVoiceMessage = (attachments.count == 1 && attachments.lastObject.isVoiceMessage);
// Loki: If we're not friends then always set the message to a friend request message // Loki: If we're not friends then always set the message to a friend request message
// If we're friends then the assumption is that we have the other user's pre-key bundle // If we're friends then the assumption is that we have the other user's prekey bundle
TSThreadFriendRequestStatus friendRequestStatus = [thread getFriendRequestStatusWithTransaction:transaction]; TSThreadFriendRequestStatus friendRequestStatus = thread.friendRequestStatus;
BOOL isFriend = friendRequestStatus == TSThreadFriendRequestStatusFriends; BOOL isFriend = friendRequestStatus == TSThreadFriendRequestStatusFriends;
NSString *messageClassAsString = isFriend ? @"TSOutgoingMessage" : @"OWSFriendRequestMessage"; NSString *messageClassAsString = isFriend ? @"TSOutgoingMessage" : @"OWSFriendRequestMessage";
Class messageClass = NSClassFromString(messageClassAsString); Class messageClass = NSClassFromString(messageClassAsString);

@ -53,7 +53,7 @@ typedef NS_ENUM(NSInteger, TSThreadFriendRequestStatus) {
@property (nonatomic) BOOL shouldThreadBeVisible; @property (nonatomic) BOOL shouldThreadBeVisible;
@property (nonatomic, readonly) NSDate *creationDate; @property (nonatomic, readonly) NSDate *creationDate;
@property (nonatomic, readonly) BOOL isArchivedByLegacyTimestampForSorting; @property (nonatomic, readonly) BOOL isArchivedByLegacyTimestampForSorting;
@property (nonatomic, readonly) TSThreadFriendRequestStatus friendRequestStatus; @property (nonatomic) TSThreadFriendRequestStatus friendRequestStatus;
/** /**
* Whether the object is a group thread or not. * Whether the object is a group thread or not.
@ -187,7 +187,11 @@ typedef NS_ENUM(NSInteger, TSThreadFriendRequestStatus) {
- (void)updateWithMutedUntilDate:(NSDate *)mutedUntilDate transaction:(YapDatabaseReadWriteTransaction *)transaction; - (void)updateWithMutedUntilDate:(NSDate *)mutedUntilDate transaction:(YapDatabaseReadWriteTransaction *)transaction;
@end #pragma mark - Loki Friend Request Handling
/// Shorthand for checking that `friendRequestStatus` is `TSThreadFriendRequestStatusPendingSend`, `TSThreadFriendRequestStatusRequestSent` or `TSThreadFriendRequestStatusRequestReceived`.
- (BOOL)hasPendingFriendRequest;
@end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

@ -696,11 +696,12 @@ ConversationColorName const kConversationColorName_Default = ConversationColorNa
}]; }];
} }
- (void)setFriendRequestStatus:(TSThreadFriendRequestStatus)friendRequestStatus #pragma mark - Loki Friend Request Handling
- (BOOL)hasPendingFriendRequest
{ {
[self.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { return self.friendRequestStatus == TSThreadFriendRequestStatusPendingSend || self.friendRequestStatus == TSThreadFriendRequestStatusRequestSent
_friendRequestStatus = friendRequestStatus; || self.friendRequestStatus == TSThreadFriendRequestStatusRequestReceived;
}];
} }
@end @end

@ -28,6 +28,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly, nullable) TSQuotedMessage *quotedMessage; @property (nonatomic, readonly, nullable) TSQuotedMessage *quotedMessage;
@property (nonatomic, readonly, nullable) OWSContact *contactShare; @property (nonatomic, readonly, nullable) OWSContact *contactShare;
@property (nonatomic, readonly, nullable) OWSLinkPreview *linkPreview; @property (nonatomic, readonly, nullable) OWSLinkPreview *linkPreview;
@property (nonatomic) BOOL isFriendRequest;
- (instancetype)initInteractionWithTimestamp:(uint64_t)timestamp inThread:(TSThread *)thread NS_UNAVAILABLE; - (instancetype)initInteractionWithTimestamp:(uint64_t)timestamp inThread:(TSThread *)thread NS_UNAVAILABLE;

@ -438,6 +438,8 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
}]; }];
} }
#pragma mark - Loki Friend Request Handling
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

@ -1442,6 +1442,15 @@ NS_ASSUME_NONNULL_BEGIN
return; return;
} }
if (envelope.type == SSKProtoEnvelopeTypeFriendRequest) {
thread.friendRequestStatus = TSThreadFriendRequestStatusRequestReceived;
[thread saveWithTransaction:transaction];
incomingMessage.isFriendRequest = YES;
} else if (incomingMessage.body == @"") { // Assumed to be an accept friend request message
thread.friendRequestStatus = TSThreadFriendRequestStatusFriends;
[thread saveWithTransaction:transaction];
}
[incomingMessage saveWithTransaction:transaction]; [incomingMessage saveWithTransaction:transaction];
// Any messages sent from the current user - from this device or another - should be automatically marked as read. // Any messages sent from the current user - from this device or another - should be automatically marked as read.

@ -1103,31 +1103,43 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
return messageSend.failure(error); return messageSend.failure(error);
} }
// Update the thread's friend request status if needed
if (messageType == TSFriendRequestMessageType) {
message.thread.friendRequestStatus = TSThreadFriendRequestStatusPendingSend;
}
// Update the state to show that proof of work is being calculated // Update the state to show that proof of work is being calculated
[self setIsCalculatingProofOfWorkForMessage:messageSend]; [self setIsCalculatingProofOfWorkForMessage:messageSend];
// Convert the message to a Loki message and send it using the Loki messaging API // Convert the message to a Loki message and send it using the Loki messaging API
NSDictionary *signalMessage = deviceMessages.firstObject; NSDictionary *signalMessage = deviceMessages.firstObject;
// Update the thread's friend request status if needed
NSInteger *messageType = ((NSNumber *)signalMessage[@"type"]).integerValue;
if (messageType == TSFriendRequestMessageType) {
message.thread.friendRequestStatus = TSThreadFriendRequestStatusPendingSend;
[message.thread save];
message.isFriendRequest = YES;
[message save];
}
BOOL isPoWRequired = YES; // TODO: Base on message type BOOL isPoWRequired = YES; // TODO: Base on message type
[[LokiAPI objc_sendSignalMessage:signalMessage to:recipient.recipientId timestamp:message.timestamp requiringPoW:isPoWRequired] [[LokiAPI objc_sendSignalMessage:signalMessage to:recipient.recipientId timestamp:message.timestamp requiringPoW:isPoWRequired]
.thenOn(OWSDispatch.sendingQueue, ^(id result) { .thenOn(OWSDispatch.sendingQueue, ^(id result) {
// Update the thread's friend request status if needed // Update the thread's friend request status if needed
if (messageType == TSFriendRequestMessageType) { if (messageType == TSFriendRequestMessageType) {
message.thread.friendRequestStatus = TSThreadFriendRequestStatusSent; message.thread.friendRequestStatus = TSThreadFriendRequestStatusRequestSent;
[message.thread save];
} else if (message.body == @"") { // Assumed to be an accept friend request message
message.thread.friendRequestStatus = TSThreadFriendRequestStatusFriends;
[message.thread save];
} }
// Invoke the completion handler
[self messageSendDidSucceed:messageSend [self messageSendDidSucceed:messageSend
deviceMessages:deviceMessages deviceMessages:deviceMessages
wasSentByUD:false wasSentByUD:false
wasSentByWebsocket:false]; wasSentByWebsocket:false];
}) })
.catchOn(OWSDispatch.sendingQueue, ^(NSError *error) { .catchOn(OWSDispatch.sendingQueue, ^(NSError *error) {
// Update the thread's friend request status if needed
if (messageType == TSFriendRequestMessageType) {
message.thread.friendRequestStatus = TSThreadFriendRequestStatusNone;
[message.thread save];
}
// Handle the error
NSUInteger statusCode = 0; NSUInteger statusCode = 0;
NSData *_Nullable responseData = nil; NSData *_Nullable responseData = nil;
if ([error.domain isEqualToString:TSNetworkManagerErrorDomain]) { if ([error.domain isEqualToString:TSNetworkManagerErrorDomain]) {

Loading…
Cancel
Save