Files
simplex-chat/apps/ios/SimpleXChat/ChatUtils.swift
T
Evgeny @ SimpleX Chat 348c42a640 ui, core, mcp: show Claude as an agent and User as a person (#8179)
* plan: mcp-agent-profile

* plan: mcp-agent-profile, answers Q1 (b) and Q2 (b)

* plan: mcp-agent-profile, lists render on GitHub

* mcp: User has the peer type of a person

* ui: show the peer type placeholder of members and contacts

* plan: mcp-agent-profile, answers to the user's notes

* ui: compare link icons with peerTypeIcon in the desktop test

* ui: snapshot references of the peer type placeholders

* android: peer type placeholder of a caller on the lock screen

* ios: agent icon of a link in the compose context at text size

* plan: mcp-agent-profile, review fixes

* ios: snapshot references with the compose context line of a link

* core: send a change of the peer type alone to groups

* mcp: User's peer type reaches existing session groups

* plan: mcp-agent-profile, Q1 (a)

* mcp: mark the agent profile item delivered in the roadmap
2026-11-14 09:12:01 +00:00

116 lines
3.9 KiB
Swift

//
// ChatUtils.swift
// SimpleXChat
//
// Created by Levitating Pineapple on 15/07/2024.
// Copyright © 2024 SimpleX Chat. All rights reserved.
//
import Foundation
import SwiftUI
public protocol ChatLike {
var chatInfo: ChatInfo { get }
}
extension ChatLike {
public func groupFeatureEnabled(_ feature: GroupFeature) -> Bool {
if case let .group(groupInfo, _) = self.chatInfo {
let p = groupInfo.fullGroupPreferences
return switch feature {
case .timedMessages: p.timedMessages.on
case .directMessages: p.directMessages.on(for: groupInfo.membership)
case .fullDelete: p.fullDelete.on
case .reactions: p.reactions.on
case .voice: p.voice.on(for: groupInfo.membership)
case .files: p.files.on(for: groupInfo.membership)
case .simplexLinks: p.simplexLinks.on(for: groupInfo.membership)
case .history: p.history.on
case .support: p.support.on
case .reports: p.reports.on
case .signMessages: p.signMessages.on
}
} else {
return true
}
}
public func prohibitedByPref(
hasSimplexLink: Bool,
isMediaOrFileAttachment: Bool,
isVoice: Bool
) -> Bool {
// preference checks should match checks in compose view
let simplexLinkProhibited = hasSimplexLink && !groupFeatureEnabled(.simplexLinks)
let fileProhibited = isMediaOrFileAttachment && !groupFeatureEnabled(.files)
let voiceProhibited = isVoice && !chatInfo.featureEnabled(.voice)
return switch chatInfo {
case .direct: voiceProhibited
case .group: simplexLinkProhibited || fileProhibited || voiceProhibited
case .local: false
case .contactRequest: false
case .contactConnection: false
case .invalidJSON: false
}
}
}
public func filterChatsToForwardTo<C: ChatLike>(chats: [C], includeLocal: Bool = true) -> [C] {
var filteredChats = chats.filter { c in
c.chatInfo.chatType != .local && canForwardToChat(c.chatInfo)
}
if includeLocal, let privateNotes = chats.first(where: { $0.chatInfo.chatType == .local }) {
filteredChats.insert(privateNotes, at: 0)
}
return filteredChats
}
public func foundChat(_ chat: ChatLike, _ searchStr: String) -> Bool {
let cInfo = chat.chatInfo
return switch cInfo {
case let .direct(contact):
viewNameContains(cInfo, searchStr) ||
contact.profile.displayName.localizedLowercase.contains(searchStr) ||
contact.fullName.localizedLowercase.contains(searchStr)
default:
viewNameContains(cInfo, searchStr)
}
func viewNameContains(_ cInfo: ChatInfo, _ s: String) -> Bool {
cInfo.chatViewName.localizedLowercase.contains(s)
}
}
private func canForwardToChat(_ cInfo: ChatInfo) -> Bool {
switch cInfo {
case let .direct(contact): cInfo.sendMsgEnabled && !contact.sendMsgToConnect
case .group: cInfo.sendMsgEnabled
case .local: cInfo.sendMsgEnabled
case .contactRequest: false
case .contactConnection: false
case .invalidJSON: false
}
}
public func chatIcon(_ cInfo: ChatInfo) -> Image {
switch cInfo {
case let .direct(contact): contact.chatIcon
case let .group(groupInfo, _): Image(systemName: groupInfo.chatIconName)
case .local: Image(systemName: "folder.circle.fill")
case let .contactRequest(contactRequest): peerTypeIcon(contactRequest.profile.peerType)
default: Image(systemName: "circle.fill")
}
}
public func hasSimplexLink(_ text: String?) -> Bool {
if let text, let parsedMsg = parseSimpleXMarkdown(text) {
parsedMsgHasSimplexLink(parsedMsg)
} else {
false
}
}
public func parsedMsgHasSimplexLink(_ parsedMsg: [FormattedText]) -> Bool {
parsedMsg.contains(where: { ft in ft.format?.isSimplexLink ?? false })
}