diff --git a/Session.xcodeproj/project.pbxproj b/Session.xcodeproj/project.pbxproj index 9241aa40b..b0f77c254 100644 --- a/Session.xcodeproj/project.pbxproj +++ b/Session.xcodeproj/project.pbxproj @@ -145,6 +145,7 @@ 7B7CB18E270D066F0079FF93 /* IncomingCallBanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B7CB18D270D066F0079FF93 /* IncomingCallBanner.swift */; }; 7B7CB190270FB2150079FF93 /* MiniCallView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B7CB18F270FB2150079FF93 /* MiniCallView.swift */; }; 7B7CB192271508AD0079FF93 /* CallRingTonePlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B7CB191271508AD0079FF93 /* CallRingTonePlayer.swift */; }; + 7B8D5FC428332600008324D9 /* VisibleMessage+Reaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B8D5FC328332600008324D9 /* VisibleMessage+Reaction.swift */; }; 7B93D06A27CF173D00811CB6 /* MessageRequestsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B93D06927CF173D00811CB6 /* MessageRequestsViewController.swift */; }; 7B93D06D27CF175800811CB6 /* MessageRequestsCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B93D06C27CF175800811CB6 /* MessageRequestsCell.swift */; }; 7B93D07027CF194000811CB6 /* ConfigurationMessage+Convenience.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B93D06E27CF194000811CB6 /* ConfigurationMessage+Convenience.swift */; }; @@ -1135,6 +1136,7 @@ 7B7CB18D270D066F0079FF93 /* IncomingCallBanner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IncomingCallBanner.swift; sourceTree = ""; }; 7B7CB18F270FB2150079FF93 /* MiniCallView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MiniCallView.swift; sourceTree = ""; }; 7B7CB191271508AD0079FF93 /* CallRingTonePlayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CallRingTonePlayer.swift; sourceTree = ""; }; + 7B8D5FC328332600008324D9 /* VisibleMessage+Reaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "VisibleMessage+Reaction.swift"; sourceTree = ""; }; 7B93D06927CF173D00811CB6 /* MessageRequestsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageRequestsViewController.swift; sourceTree = ""; }; 7B93D06C27CF175800811CB6 /* MessageRequestsCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageRequestsCell.swift; sourceTree = ""; }; 7B93D06E27CF194000811CB6 /* ConfigurationMessage+Convenience.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ConfigurationMessage+Convenience.swift"; sourceTree = ""; }; @@ -2525,6 +2527,7 @@ isa = PBXGroup; children = ( C3C2A74C2553A39700C340D1 /* VisibleMessage.swift */, + 7B8D5FC328332600008324D9 /* VisibleMessage+Reaction.swift */, C379DCF3256735770002D4EB /* VisibleMessage+Attachment.swift */, C3C2A7552553A3AB00C340D1 /* VisibleMessage+Quote.swift */, C3C2A75E2553A3C500C340D1 /* VisibleMessage+LinkPreview.swift */, @@ -4728,6 +4731,7 @@ C300A5FC2554B0A000555489 /* MessageReceiver.swift in Sources */, 7B1581E2271E743B00848B49 /* OWSSounds.swift in Sources */, C32C5A76256DBBCF003C73A2 /* SignalAttachment.swift in Sources */, + 7B8D5FC428332600008324D9 /* VisibleMessage+Reaction.swift in Sources */, C32C5CA4256DD1DC003C73A2 /* TSAccountManager.m in Sources */, C352A3892557876500338F3E /* JobQueue.swift in Sources */, C3BBE0B52554F0E10050F1E3 /* ProofOfWork.swift in Sources */, diff --git a/SessionMessagingKit/Messages/Visible Messages/VisibleMessage+Reaction.swift b/SessionMessagingKit/Messages/Visible Messages/VisibleMessage+Reaction.swift new file mode 100644 index 000000000..b1372fe7e --- /dev/null +++ b/SessionMessagingKit/Messages/Visible Messages/VisibleMessage+Reaction.swift @@ -0,0 +1,105 @@ + +public extension VisibleMessage { + + @objc(SNReaction) + class Reaction : NSObject, NSCoding { + public var timestamp: UInt64? + public var publicKey: String? + public var emoji: String? + public var kind: Kind? + + // MARK: Kind + public enum Kind : Int, CustomStringConvertible { + case react, remove + + static func fromProto(_ proto: SNProtoDataMessageReaction.SNProtoDataMessageReactionAction) -> Kind { + switch proto { + case .react: return .react + case .remove: return .remove + } + } + + func toProto() -> SNProtoDataMessageReaction.SNProtoDataMessageReactionAction { + switch self { + case .react: return .react + case .remove: return .remove + } + } + + public var description: String { + switch self { + case .react: return "react" + case .remove: return "remove" + } + } + } + + // MARK: Validation + public var isValid: Bool { timestamp != nil && publicKey != nil } + + // MARK: Initialization + public override init() { super.init() } + + internal init(timestamp: UInt64, publicKey: String, emoji: String?, kind: Kind?) { + self.timestamp = timestamp + self.publicKey = publicKey + self.emoji = emoji + self.kind = kind + } + + // MARK: Coding + public required init?(coder: NSCoder) { + if let timestamp = coder.decodeObject(forKey: "timestamp") as! UInt64? { self.timestamp = timestamp } + if let publicKey = coder.decodeObject(forKey: "authorId") as! String? { self.publicKey = publicKey } + if let emoji = coder.decodeObject(forKey: "emoji") as! String? { self.emoji = emoji } + if let rawKind = coder.decodeObject(forKey: "action") as! Int? { self.kind = Kind(rawValue: rawKind) } + } + + public func encode(with coder: NSCoder) { + coder.encode(timestamp, forKey: "timestamp") + coder.encode(publicKey, forKey: "authorId") + coder.encode(emoji, forKey: "emoji") + coder.encode(kind?.rawValue, forKey: "action") + } + + // MARK: Proto Conversion + public static func fromProto(_ proto: SNProtoDataMessageReaction) -> Reaction? { + let timestamp = proto.id + let publicKey = proto.author + let emoji = proto.emoji + let kind = Kind.fromProto(proto.action) + return Reaction(timestamp: timestamp, publicKey: publicKey, emoji: emoji, kind: kind) + } + + public func toProto() -> SNProtoDataMessageReaction? { + preconditionFailure("Use toProto(using:) instead.") + } + + public func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoDataMessageReaction? { + guard let timestamp = timestamp, let publicKey = publicKey, let kind = kind else { + SNLog("Couldn't construct reaction proto from: \(self).") + return nil + } + let reactionProto = SNProtoDataMessageReaction.builder(id: timestamp, author: publicKey, action: kind.toProto()) + if let emoji = emoji { reactionProto.setEmoji(emoji) } + do { + return try reactionProto.build() + } catch { + SNLog("Couldn't construct quote proto from: \(self).") + return nil + } + } + + // MARK: Description + public override var description: String { + """ + Reaction( + timestamp: \(timestamp?.description ?? "null"), + publicKey: \(publicKey ?? "null"), + emoji: \(emoji ?? "null"), + kind: \(kind?.description ?? "null") + ) + """ + } + } +}