Expand iOS messaging shell actions
This commit is contained in:
@@ -9,8 +9,15 @@ public protocol SessionBridge: Sendable {
|
||||
func loadFolders() async throws -> [Folder]
|
||||
func loadChats(folderId: Int32?) async throws -> [ChatSummary]
|
||||
func loadHistory(chatId: Int64) async throws -> [Message]
|
||||
func searchMessages(chatId: Int64, query: String) async throws -> [Message]
|
||||
func openProfile(chatId: Int64) async throws -> Profile
|
||||
func sendMessage(chatId: Int64, text: String, replyToMessageId: Int64?) async throws -> Message
|
||||
func editMessage(chatId: Int64, messageId: Int64, text: String) async throws -> Message
|
||||
func deleteMessages(chatId: Int64, messageIds: [Int64]) async throws
|
||||
func forwardMessages(toChatId: Int64, fromChatId: Int64, messageIds: [Int64]) async throws
|
||||
func react(chatId: Int64, messageId: Int64, reaction: String) async throws -> [Reaction]
|
||||
func pinnedMessages(chatId: Int64) async throws -> [Message]
|
||||
func copyPayload(chatId: Int64, messageId: Int64) async throws -> String
|
||||
}
|
||||
|
||||
public actor FakeSessionBridge: SessionBridge {
|
||||
@@ -93,6 +100,16 @@ public actor FakeSessionBridge: SessionBridge {
|
||||
messages[chatId] ?? []
|
||||
}
|
||||
|
||||
public func searchMessages(chatId: Int64, query: String) async throws -> [Message] {
|
||||
guard !query.isEmpty else {
|
||||
return messages[chatId] ?? []
|
||||
}
|
||||
return (messages[chatId] ?? []).filter {
|
||||
$0.text.localizedCaseInsensitiveContains(query)
|
||||
|| $0.senderName.localizedCaseInsensitiveContains(query)
|
||||
}
|
||||
}
|
||||
|
||||
public func openProfile(chatId: Int64) async throws -> Profile {
|
||||
let chat = chats.first { $0.id == chatId }
|
||||
let profile = Profile(
|
||||
@@ -125,4 +142,74 @@ public actor FakeSessionBridge: SessionBridge {
|
||||
events.append(.messageAdded(chatId, message))
|
||||
return message
|
||||
}
|
||||
|
||||
public func editMessage(chatId: Int64, messageId: Int64, text: String) async throws -> Message {
|
||||
guard var chatMessages = messages[chatId],
|
||||
let index = chatMessages.firstIndex(where: { $0.id == messageId })
|
||||
else {
|
||||
throw FakeBridgeError.messageNotFound
|
||||
}
|
||||
chatMessages[index].text = text
|
||||
chatMessages[index].editDate = Int32(Date().timeIntervalSince1970)
|
||||
messages[chatId] = chatMessages
|
||||
return chatMessages[index]
|
||||
}
|
||||
|
||||
public func deleteMessages(chatId: Int64, messageIds: [Int64]) async throws {
|
||||
messages[chatId]?.removeAll { messageIds.contains($0.id) }
|
||||
}
|
||||
|
||||
public func forwardMessages(toChatId: Int64, fromChatId: Int64, messageIds: [Int64]) async throws {
|
||||
let sourceMessages = (messages[fromChatId] ?? []).filter { messageIds.contains($0.id) }
|
||||
for source in sourceMessages {
|
||||
let forwarded = Message(
|
||||
id: nextMessageId,
|
||||
chatId: toChatId,
|
||||
senderName: "Me",
|
||||
text: source.text,
|
||||
isOutgoing: true,
|
||||
forwardSenderName: source.senderName
|
||||
)
|
||||
nextMessageId += 1
|
||||
messages[toChatId, default: []].append(forwarded)
|
||||
events.append(.messageAdded(toChatId, forwarded))
|
||||
}
|
||||
}
|
||||
|
||||
public func react(chatId: Int64, messageId: Int64, reaction: String) async throws -> [Reaction] {
|
||||
guard var chatMessages = messages[chatId],
|
||||
let index = chatMessages.firstIndex(where: { $0.id == messageId })
|
||||
else {
|
||||
throw FakeBridgeError.messageNotFound
|
||||
}
|
||||
if let reactionIndex = chatMessages[index].reactions.firstIndex(where: { $0.emoji == reaction }) {
|
||||
chatMessages[index].reactions.remove(at: reactionIndex)
|
||||
} else {
|
||||
chatMessages[index].reactions.append(Reaction(emoji: reaction, count: 1, isChosen: true))
|
||||
}
|
||||
messages[chatId] = chatMessages
|
||||
return chatMessages[index].reactions
|
||||
}
|
||||
|
||||
public func pinnedMessages(chatId: Int64) async throws -> [Message] {
|
||||
Array((messages[chatId] ?? []).prefix(1))
|
||||
}
|
||||
|
||||
public func copyPayload(chatId: Int64, messageId: Int64) async throws -> String {
|
||||
guard let message = messages[chatId]?.first(where: { $0.id == messageId }) else {
|
||||
throw FakeBridgeError.messageNotFound
|
||||
}
|
||||
return message.text
|
||||
}
|
||||
}
|
||||
|
||||
public enum FakeBridgeError: LocalizedError {
|
||||
case messageNotFound
|
||||
|
||||
public var errorDescription: String? {
|
||||
switch self {
|
||||
case .messageNotFound:
|
||||
"Message not found"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user