Path: blob/main/SignalServiceKit/Messages/Interactions/TSErrorMessage.m
1 views
//1// Copyright 2017 Signal Messenger, LLC2// SPDX-License-Identifier: AGPL-3.0-only3//45#import "TSErrorMessage.h"6#import <SignalServiceKit/SignalServiceKit-Swift.h>78NS_ASSUME_NONNULL_BEGIN910#pragma mark -1112@interface TSErrorMessage ()1314@property (nonatomic, getter=wasRead) BOOL read;1516@end1718#pragma mark -1920@implementation TSErrorMessage2122- (NSUInteger)hash23{24NSUInteger result = [super hash];25result ^= (NSUInteger)self.errorType;26result ^= self.read;27result ^= self.recipientAddress.hash;28result ^= self.sender.hash;29result ^= self.wasIdentityVerified;30return result;31}3233- (BOOL)isEqual:(id)other34{35if (![super isEqual:other]) {36return NO;37}38TSErrorMessage *typedOther = (TSErrorMessage *)other;39if (self.errorType != typedOther.errorType) {40return NO;41}42if (self.read != typedOther.read) {43return NO;44}45if (![NSObject isObject:self.recipientAddress equalToObject:typedOther.recipientAddress]) {46return NO;47}48if (![NSObject isObject:self.sender equalToObject:typedOther.sender]) {49return NO;50}51if (self.wasIdentityVerified != typedOther.wasIdentityVerified) {52return NO;53}54return YES;55}5657- (instancetype)initErrorMessageWithBuilder:(TSErrorMessageBuilder *)errorMessageBuilder58{59self = [super initMessageWithBuilder:errorMessageBuilder];6061if (!self) {62return self;63}6465_errorType = errorMessageBuilder.errorType;66_sender = errorMessageBuilder.senderAddress;67_recipientAddress = errorMessageBuilder.recipientAddress;68_wasIdentityVerified = errorMessageBuilder.wasIdentityVerified;6970if (self.isDynamicInteraction) {71self.read = YES;72}7374return self;75}7677// --- CODE GENERATION MARKER7879// This snippet is generated by /Scripts/sds_codegen/sds_generate.py. Do not manually edit it, instead run80// `sds_codegen.sh`.8182// clang-format off8384- (instancetype)initWithGrdbId:(int64_t)grdbId85uniqueId:(NSString *)uniqueId86receivedAtTimestamp:(uint64_t)receivedAtTimestamp87sortId:(uint64_t)sortId88timestamp:(uint64_t)timestamp89uniqueThreadId:(NSString *)uniqueThreadId90body:(nullable NSString *)body91bodyRanges:(nullable MessageBodyRanges *)bodyRanges92contactShare:(nullable OWSContact *)contactShare93deprecated_attachmentIds:(nullable NSArray<NSString *> *)deprecated_attachmentIds94editState:(TSEditState)editState95expireStartedAt:(uint64_t)expireStartedAt96expireTimerVersion:(nullable NSNumber *)expireTimerVersion97expiresAt:(uint64_t)expiresAt98expiresInSeconds:(unsigned int)expiresInSeconds99giftBadge:(nullable OWSGiftBadge *)giftBadge100isGroupStoryReply:(BOOL)isGroupStoryReply101isPoll:(BOOL)isPoll102isSmsMessageRestoredFromBackup:(BOOL)isSmsMessageRestoredFromBackup103isViewOnceComplete:(BOOL)isViewOnceComplete104isViewOnceMessage:(BOOL)isViewOnceMessage105linkPreview:(nullable OWSLinkPreview *)linkPreview106messageSticker:(nullable MessageSticker *)messageSticker107quotedMessage:(nullable TSQuotedMessage *)quotedMessage108storedShouldStartExpireTimer:(BOOL)storedShouldStartExpireTimer109storyAuthorUuidString:(nullable NSString *)storyAuthorUuidString110storyReactionEmoji:(nullable NSString *)storyReactionEmoji111storyTimestamp:(nullable NSNumber *)storyTimestamp112wasRemotelyDeleted:(BOOL)wasRemotelyDeleted113errorType:(TSErrorMessageType)errorType114read:(BOOL)read115recipientAddress:(nullable SignalServiceAddress *)recipientAddress116sender:(nullable SignalServiceAddress *)sender117wasIdentityVerified:(BOOL)wasIdentityVerified118{119self = [super initWithGrdbId:grdbId120uniqueId:uniqueId121receivedAtTimestamp:receivedAtTimestamp122sortId:sortId123timestamp:timestamp124uniqueThreadId:uniqueThreadId125body:body126bodyRanges:bodyRanges127contactShare:contactShare128deprecated_attachmentIds:deprecated_attachmentIds129editState:editState130expireStartedAt:expireStartedAt131expireTimerVersion:expireTimerVersion132expiresAt:expiresAt133expiresInSeconds:expiresInSeconds134giftBadge:giftBadge135isGroupStoryReply:isGroupStoryReply136isPoll:isPoll137isSmsMessageRestoredFromBackup:isSmsMessageRestoredFromBackup138isViewOnceComplete:isViewOnceComplete139isViewOnceMessage:isViewOnceMessage140linkPreview:linkPreview141messageSticker:messageSticker142quotedMessage:quotedMessage143storedShouldStartExpireTimer:storedShouldStartExpireTimer144storyAuthorUuidString:storyAuthorUuidString145storyReactionEmoji:storyReactionEmoji146storyTimestamp:storyTimestamp147wasRemotelyDeleted:wasRemotelyDeleted];148149if (!self) {150return self;151}152153_errorType = errorType;154_read = read;155_recipientAddress = recipientAddress;156_sender = sender;157_wasIdentityVerified = wasIdentityVerified;158159return self;160}161162// clang-format on163164// --- CODE GENERATION MARKER165166- (OWSInteractionType)interactionType167{168return OWSInteractionType_Error;169}170171- (NSString *)previewTextWithTransaction:(DBReadTransaction *)transaction172{173switch (_errorType) {174case TSErrorMessageNoSession:175return OWSLocalizedString(@"ERROR_MESSAGE_NO_SESSION", @"");176case TSErrorMessageInvalidMessage:177return OWSLocalizedString(@"ERROR_MESSAGE_INVALID_MESSAGE", @"");178case TSErrorMessageInvalidVersion:179return OWSLocalizedString(@"ERROR_MESSAGE_INVALID_VERSION", @"");180case TSErrorMessageDuplicateMessage:181return OWSLocalizedString(@"ERROR_MESSAGE_DUPLICATE_MESSAGE", @"");182case TSErrorMessageInvalidKeyException:183return OWSLocalizedString(@"ERROR_MESSAGE_INVALID_KEY_EXCEPTION", @"");184case TSErrorMessageWrongTrustedIdentityKey:185return OWSLocalizedString(@"ERROR_MESSAGE_WRONG_TRUSTED_IDENTITY_KEY", @"");186case TSErrorMessageNonBlockingIdentityChange:187return [TSErrorMessage safetyNumberChangeDescriptionFor:self.recipientAddress tx:transaction];188case TSErrorMessageUnknownContactBlockOffer:189return OWSLocalizedString(@"UNKNOWN_CONTACT_BLOCK_OFFER",190@"Message shown in conversation view that offers to block an unknown user.");191case TSErrorMessageGroupCreationFailed:192return OWSLocalizedString(@"GROUP_CREATION_FAILED",193@"Message shown in conversation view that indicates there were issues with group creation.");194case TSErrorMessageSessionRefresh:195return OWSLocalizedString(196@"ERROR_MESSAGE_SESSION_REFRESH", @"Text notifying the user that their secure session has been reset");197case TSErrorMessageDecryptionFailure: {198if (self.sender) {199NSString *formatString = OWSLocalizedString(@"ERROR_MESSAGE_DECRYPTION_FAILURE",200@"Error message for a decryption failure. Embeds {{sender short name}}.");201NSString *senderName =202[SSKEnvironment.shared.contactManagerObjcRef shortDisplayNameStringForAddress:self.sender203transaction:transaction];204#pragma clang diagnostic push205#pragma clang diagnostic ignored "-Wformat-nonliteral"206return [[NSString alloc] initWithFormat:formatString, senderName];207#pragma clang diagnostic pop208} else {209return OWSLocalizedString(210@"ERROR_MESSAGE_DECRYPTION_FAILURE_UNKNOWN_SENDER", @"Error message for a decryption failure.");211}212}213default:214OWSFailDebug(@"failure: unknown error type");215break;216}217return OWSLocalizedString(@"ERROR_MESSAGE_UNKNOWN_ERROR", @"");218}219220#pragma mark - OWSReadTracking221222- (uint64_t)expireStartedAt223{224return 0;225}226227- (void)markAsReadAtTimestamp:(uint64_t)readTimestamp228thread:(TSThread *)thread229circumstance:(OWSReceiptCircumstance)circumstance230shouldClearNotifications:(BOOL)shouldClearNotifications231transaction:(DBWriteTransaction *)transaction232{233OWSAssertDebug(transaction);234235if (self.read) {236return;237}238239[self anyUpdateErrorMessageWithTransaction:transaction block:^(TSErrorMessage *message) { message.read = YES; }];240241// Ignore `circumstance` - we never send read receipts for error messages.242}243244@end245246NS_ASSUME_NONNULL_END247248249