2022-04-21 08:42:35 +02:00
|
|
|
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
2020-11-07 23:00:10 +01:00
|
|
|
|
2022-04-21 08:42:35 +02:00
|
|
|
import Foundation
|
|
|
|
import GRDB
|
2022-12-02 04:00:10 +01:00
|
|
|
import SessionSnodeKit
|
2022-04-21 08:42:35 +02:00
|
|
|
import SessionUtilitiesKit
|
2020-11-07 23:00:10 +01:00
|
|
|
|
2022-04-21 08:42:35 +02:00
|
|
|
public extension Message {
|
2022-12-16 06:51:08 +01:00
|
|
|
enum Destination: Codable, Hashable {
|
|
|
|
case contact(publicKey: String)
|
|
|
|
case closedGroup(groupPublicKey: String)
|
2022-02-11 06:48:16 +01:00
|
|
|
case openGroup(
|
|
|
|
roomToken: String,
|
|
|
|
server: String,
|
|
|
|
whisperTo: String? = nil,
|
|
|
|
whisperMods: Bool = false,
|
2022-03-04 06:17:03 +01:00
|
|
|
fileIds: [String]? = nil
|
2022-02-11 06:48:16 +01:00
|
|
|
)
|
2022-02-25 01:59:29 +01:00
|
|
|
case openGroupInbox(server: String, openGroupPublicKey: String, blindedPublicKey: String)
|
2022-12-08 04:21:38 +01:00
|
|
|
|
2022-11-27 22:32:32 +01:00
|
|
|
public static func from(
|
2022-06-08 06:29:51 +02:00
|
|
|
_ db: Database,
|
|
|
|
thread: SessionThread,
|
|
|
|
fileIds: [String]? = nil
|
|
|
|
) throws -> Message.Destination {
|
2022-04-21 08:42:35 +02:00
|
|
|
switch thread.variant {
|
2022-06-08 06:29:51 +02:00
|
|
|
case .contact:
|
2022-06-09 10:37:44 +02:00
|
|
|
if SessionId.Prefix(from: thread.id) == .blinded {
|
|
|
|
guard let lookup: BlindedIdLookup = try? BlindedIdLookup.fetchOne(db, id: thread.id) else {
|
2022-06-08 06:29:51 +02:00
|
|
|
preconditionFailure("Attempting to send message to blinded id without the Open Group information")
|
|
|
|
}
|
|
|
|
|
|
|
|
return .openGroupInbox(
|
2022-06-09 10:37:44 +02:00
|
|
|
server: lookup.openGroupServer,
|
|
|
|
openGroupPublicKey: lookup.openGroupPublicKey,
|
2022-06-08 06:29:51 +02:00
|
|
|
blindedPublicKey: thread.id
|
|
|
|
)
|
2022-02-25 01:59:29 +01:00
|
|
|
}
|
|
|
|
|
2022-12-16 06:51:08 +01:00
|
|
|
return .contact(publicKey: thread.id)
|
2022-02-25 01:59:29 +01:00
|
|
|
|
2023-02-20 02:56:48 +01:00
|
|
|
case .legacyGroup, .group:
|
2022-12-16 06:51:08 +01:00
|
|
|
return .closedGroup(groupPublicKey: thread.id)
|
2022-02-11 06:48:16 +01:00
|
|
|
|
2023-02-20 02:56:48 +01:00
|
|
|
case .community:
|
2022-04-21 08:42:35 +02:00
|
|
|
guard let openGroup: OpenGroup = try thread.openGroup.fetchOne(db) else {
|
2022-05-30 05:04:26 +02:00
|
|
|
throw StorageError.objectNotFound
|
2022-04-21 08:42:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-09 10:37:44 +02:00
|
|
|
return .openGroup(roomToken: openGroup.roomToken, server: openGroup.server, fileIds: fileIds)
|
2020-11-25 06:15:16 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-25 09:03:09 +02:00
|
|
|
|
|
|
|
func with(fileIds: [String]) -> Message.Destination {
|
|
|
|
// Only Open Group messages support receiving the 'fileIds'
|
|
|
|
switch self {
|
|
|
|
case .openGroup(let roomToken, let server, let whisperTo, let whisperMods, _):
|
|
|
|
return .openGroup(
|
|
|
|
roomToken: roomToken,
|
|
|
|
server: server,
|
|
|
|
whisperTo: whisperTo,
|
|
|
|
whisperMods: whisperMods,
|
|
|
|
fileIds: fileIds
|
|
|
|
)
|
|
|
|
|
|
|
|
default: return self
|
|
|
|
}
|
|
|
|
}
|
2020-11-07 23:00:10 +01:00
|
|
|
}
|
|
|
|
}
|