diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000000..8487f0a53c --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,552 @@ +# Release History + +## v6.5 + +30 April, 2026 + +Public channels - speak freely! +- Reliability: many relays per channel. +- Ownership: you can run your own relays. +- Security: owners hold channel keys. +- Privacy: for owners and subscribers. + +Easier to invite your friends: we made connecting simpler for new users. + +Safe web links: +- opt-in to send link previews. +- use SOCKS proxy for previews (if enabled). +- prevent hyperlink phishing. +- remove link tracking. + +Non-profit governance: to make SimpleX Network last. + +Read more on April 30 at 20:00 UTC: https://simplex.chat/blog/20260430-simplex-channels-v6-5-consortium-crowdfunding-freedom-of-speech.html + +## v6.4 + +15 July, 2025 + +- Connect faster: message instantly once you tap Connect. +- Review group members: chat with new members before they join. +- Chat with admins: send your private feedback to group owners. +- New group role: Moderator - can remove messages and block members. +- Improved message delivery - less traffic on mobile networks. + +Read about the new UX for making connections in the blog post: https://simplex.chat/blog/20250703-simplex-network-protocol-extension-for-securely-connecting-people.html + +## v6.3 + +7 March, 2025 + +Better groups. +- Mention members and get notified when mentioned. +- Send private reports to moderators. +- Delete, block and change role for multiple members at once (Android and desktop only). +- Faster sending messages and faster deletion. + +Better chat navigation +- Organize chats into lists to keep track of what's important. +- Jump to found and forwarded messages. + +Better privacy and security. +- Private media file names. +- Message expiration in chats. + +Read more on March 8: https://simplex.chat/blog/20250308-simplex-chat-v6-3-new-user-experience-safety-in-public-groups.html + +## v6.2 + +7 December, 2024 + +- SimpleX Chat and Flux (https://runonflux.com) made an agreement to include servers operated by Flux into the app – to improve metadata privacy. +- Business chats – your customers' privacy. +- Improved user experience of chats: + - Open chat on the first unread message. + - Jump to quoted messages anywhere in the conversation. + - See who reacted to messages. +- Improved iOS push notifications. + +Read more on December 10: https://simplex.chat/blog/20241210-simplex-network-v6-2-servers-by-flux-business-chats.html + +## v6.1 + +12 October, 2024 + +Better security: +- SimpleX protocols reviewed by Trail of Bits. +- security improvements (don't worry, there is nothing critical there). + +Better calls: +- you can switch audio and video during the call +- share the screen from desktop app. + +Better iOS notifications: +- improved delivery, reduced traffic usage. +- more improvements are coming soon! + +Better user experience: +- switch chat profile for 1-time invitations. +- customizable message shape. +- better message dates. +- forward up to 20 messages at once. +- delete or moderate up to 200 messages. + +The protocols review by Trail of Bits and release announcement will be published on October 14 afternoon here: https://simplex.chat/blog/20241014-simplex-network-v6-1-security-review-better-calls-user-experience.html + +## v6.0 + +11 August, 2024 + +New chat experience: +- connect to your friends faster. +- archive contacts to chat later. +- delete up to 20 messages at once. +- increase font size. +- new chat themes on iOS - same as on Android and desktop in the previous version. +- reachable chat toolbar - use the app with one hand. + +New media options: +- share from other apps (iOS). +- play from the chat list. +- blur for better privacy. + +Private routing: it protects your IP address and connections and is now enabled by default. + +Connection and servers information: to control your network status and usage. + +Read more on 8/14: https://simplex.chat/blog/20240814-simplex-chat-vision-funding-v6-private-routing-new-user-experience.html + +## v5.8 + +3 June, 2024 + +- private message routing to protect IP addresses (opt-in in this version). +- protect IP address when receiving files. +- chat themes with wallpapers - set themes for all chats app-wide, per chat profile and per conversation - Android and desktop apps. +- some groups permissions can now be granted to admins only. +- improved message and file delivery with reduced battery usage. +- Persian interface language - Android and desktop apps. + +Read more: https://simplex.chat/blog/20240604-simplex-chat-v5.8-private-message-routing-chat-themes.html + +## v5.7 + +26 April, 2024 + +- quantum resistant end-to-end encryption – will be enabled for all direct chats! +- forward and save messages and files, without revealing the source. +- improved calls: in-call sounds when connecting calls, better support for bluetooth headphones. +- customizable shapes of profile images - from square to circle. +- more reliable network connection. + +Lithuanian UI language in Android and desktop apps - thanks to our users! + +Read more: https://simplex.chat/blog/20240426-simplex-legally-binding-transparency-v5-7-better-user-experience.html + +## v5.6 + +21 March, 2024 + +1. **Quantum resistant end-to-end encryption** in direct chats (BETA). + It can be enabled for the new contacts by *Post-quantum E2EE* toggle in dev tools, and for the existing contacts - both users need to tap *Allow PQ encryption* in contact information page (and the toggle in dev tools should be enable for this button to be available). + Once quantum resistant shared secret is agreed, there will be a message indicating it - it takes about 2-3 messages from each side to be sent in turns before it gets enabled. + Read more about end-to-end encryption in SimpleX Chat here: https://simplex.chat/blog/20240314-simplex-chat-v5-6-quantum-resistance-signal-double-ratchet-algorithm.html + +2. **App data migration**. + As suggested by one of SimpleX Chat users in our [users group](simplex:/contact#/?v=1-4&smp=smp%3A%2F%2FPQUV2eL0t7OStZOoAsPEV2QYWt4-xilbakvGUGOItUo%3D%40smp6.simplex.im%2Fos8FftfoV8zjb2T89fUEjJtF7y64p5av%23%2F%3Fv%3D1-2%26dh%3DMCowBQYDK2VuAyEAQqMgh0fw2lPhjn3PDIEfAKA_E0-gf8Hr8zzhYnDivRs%253D%26srv%3Dbylepyau3ty4czmn77q4fglvperknl4bi2eb2fdy2bh4jxtf32kf73yd.onion&data=%7B%22type%22%3A%22group%22%2C%22groupLinkId%22%3A%22lBPiveK2mjfUH43SN77R0w%3D%3D%22%7D), you can now migrate all data from one device to a new app installation by uploading it to the configured XFTP relays and then scanning QR code from the new device – choose *Migrate to another device* from the app settings and *Migrate from another device* on the first screen after installing the app. + +3. **Use the app during the audio and video calls**. + Now you can continue using the app, with small video if it's a video call. + +Also in this version: +- admins can block a member for all other members. +- much faster leaving and deleting groups. +- filtering chats no longer includes muted chats with unread messages. +- reduced memory usage when sending large files. +- desktop: scrollbars in all views with the scrolling - finally! +- iOS: + - fixed rendering glitches with messages and context menus. + - added Hungarian interface language. + +The blog post with the announcement is coming on 3/23/2024. + +## v5.5 + +23 January, 2024 + +- private notes - with encrypted files and media. +- paste link to connect - search bar now accepts invitation links. +- optional recent history in groups. +- improved message delivery - with reduced battery usage. +- reveal secrets in messages by tapping them. +- all files in local app storage are encrypted by default. +- allow deleting the last visible user profile. +- do not share contact address in member profile. +- many fixes! + +Also, we added Hungarian (Android only) and Turkish interface - thanks to the users and Weblate (https://github.com/simplex-chat/simplex-chat/tree/stable#help-translating-simplex-chat). + +## v5.4 + +25 November, 2023 + +- Link mobile and desktop apps via secure quantum-resistant protocol. +- Better groups: + - Faster to join and more reliable. + - Create groups with incognito profile. + - Block group members to hide their messages. + - Prohibit files and media in a group. +- Better calls: + - Connect faster and more stable (still far from great). + - Screen sharing in video calls in desktop app. +- Other improvements: + - profile names now allow spaces. + - when you delete contacts, they are optionally notified. + - previously used and your own SimpleX links are recognised by the app. + - many fixes and improvements. + +## v5.3 + +22 September, 2023 + +All apps (Android, iOS, desktop): +- encrypt local files in app storage (except videos). +- improved groups: + - delivery receipts (up to 20 members). + - send direct messages to members even after contact is deleted. + - faster and more stable. +- simplified incognito mode. +- new privacy settings: show last messages & save draft. +- faster app loading. +- reduced memory usage by 40%. +- fixed bug preventing group members connecting (it will only help the new connections). +- iOS app fixes: + - playing videos on full screen. + - screen reader for messages. + - fixed most background crashes. + +Also, 6 new interface languages added by the users: Arabic*, Bulgarian, Finnish, Hebrew*, Thai and Ukrainian! + +\* Android and desktop only + +## v5.2 + +22 July, 2023 + +- message delivery receipts – with opt out per contact! +- filter favorite and unread chats. +- keep your connections working after restoring from backup. +- share your address with group members via your chat profile. +- improved disappearing messages. +- a bit more usable groups. +- chat preference to prohibit message reactions. +- restart and shutdown buttons. +- more stable message delivery. + +Read more: https://simplex.chat/blog/20230722-simplex-chat-v5-2-message-delivery-receipts.html + +## v5.1 + +22 May, 2023 + +Mobile apps: +- message reactions 🚀 +- self-destruct passcode +- improved messages: + - voice messages up to 5 minutes. + - custom time to disappear - can be set just for one message. + - message editing history. +- setting to disable audio/video calls per contact. +- welcome message visible in group profile. + +Android only: +- new design and custom themes for Android - you can share them! +- configurable SOCKS proxy port. +- improved calls on lock screen. +- fixes for sending files. +- locale-dependent formatting of time and date. + +Also, the users have added Japanese and Portuguese (Brazil) interfaces (the latter is available on Android only) - huge thanks! + +## v5.0 + +20 April, 2023 + +- send videos and files up to 1gb - the recipient must have at least version 4.6.1. +- you can self-host XFTP servers and configure the app to use your servers. +- passcode as an alternative to system/device authentication. +- support for IPv6 server addresses. +- configurable SOCKS proxy host and port in Android app. + +Also we added Polish interface language – [thanks to the users and Weblate](https://github.com/simplex-chat/simplex-chat#help-translating-simplex-chat). + +See more details in this post: https://simplex.chat/blog/20230422-simplex-chat-vision-funding-v5-videos-files-passcode.html + +## v4.6 + +25 March, 2023 + +Mobile apps: + +- hidden chat profiles – you can protect them with a password! +- audio/video calls: + - iOS: completely re-implemented using WebRTC native library and iOS CallKit. Calls now work when the app is in background, and can be answered when the app is fully stopped. + - Android: added support for bluetooth headphones, volume control in video calls, proximity sensor turns off screen in audio calls. +- group moderation. Admins now can delete member messages and disable members (by assigning "observer" role). +- group welcome message to show to the new users when they join. +- reduced battery usage, particularly when sending messages to large groups. +- Chinese and Spanish interface. + +Android app now supports Android 8+ (API 26+), and also supports 32 bit/ARMv7a devices via a separate APK. If you don't know which APK you need, try simplex.apk first. You can check your device CPU in z-cpu app. + +Terminal / CLI app: + +- hidden profiles are supported. +- improved help, with all supported commands included. + +## v4.5 + +3 February, 2023 + +- multiple chat profiles: use different names, avatars and transport isolation. +- transport isolation: separate transport connections are used for each chat profile (default) or for each connection (BETA – enable dev tools to make this option available in Network & Servers.) +- message draft: the last message text and any attachments are now preserved when you leave the conversation (while the app is running). +- private filenames: to protect your timezone, image and voice message files now use UTC time. + +## v4.4 + +31 December, 2022 + +- disappearing messages - with mutual agreement! +- live messages – they update for all recipients as you type them, every few seconds. +- connection security code verification, for contacts and group members – protect from MITM attack (e.g. invitation link substitution). +- performance improvements - faster UI loading, faster group deletion, etc. + +Mobile apps: +- French language support in the UI! + +iOS app: +- send animated images and "stickers" (e.g., from GIF and PNG files and from 3rd party keyboards) + +## v4.3 + +4 December, 2022 + +Mobile apps: +- instant voice messages! +- irreversible deletion of sent messages on recipients devices (depends on chat preferences) +- an option to hide the app screen in the recent apps, and also prevent the screenshots on Android +- add SMP servers by scanning QR code, support for server passwords (with the new version 4.0 of SMP server) +- improved privacy and security of SimpleX invitation links in the app + +## v4.2 + +6 November, 2022 + +- fixed issues from security audit! +- group links - group admins can create the links for new members to join +- auto-accept contact requests + configure to accept incognito and welcome message +- change group member role +- mark chat as unread +- on Android: + - support for image/gif/sticker keyboards + - fix keyboard bug with backspace + +Beta features (enable Developer tools): +- manually switch contact or member to another address / server +- receive files faster (enable in Privacy settings) + +## v4.1 + +13 October, 2022 + +Changes: +- automatic message deletion (set TTL per-chat or globally) +- change group member roles +- send multiple images at once +- connection aliases and information view +- share text and files from other apps into SimpleX (Android) +- image gallery (Android) +- scroll to quoted message (Android) +- German translations +- improved connection stability and performance + +## v4.0 + +24 September, 2022 + +Changes: + +Local database encryption with passphrase on iOS, Android, Linux, Mac! + +Mobile apps: +- configurable WebRTC ICE servers - see https://github.com/simplex-chat/simplex-chat/blob/stable/docs/WEBRTC.md +- improved stability of establishing direct and group connections, files transfers and message reception. +- support for animated images on Android +- German language UI +- deleting files and media + +Terminal app: +- disable messages and notifications per contact / group + +For developers: +- [TypeScript SDK for integrating with SimpleX Chat](#typescript-sdk-for-integrating-with-simplex-chat) (e.g., chat bots or chat assistants). + +## v3.2 + +20 August, 2022 + +Changes: +- use .onion addresses of the servers (if available) when Tor is used – it is based on a separate setting on iOS. +- endless scrolling and search in chats +- UI improvements +- reduced Android APK size (from 200 to 46Mb) + +## v3.1 + +6 August, 2022 + +Mobile apps: +- secret chat groups! +- support accessing SimpleX messaging servers via Orbot (both iOS and Android) +- new app icons +- advanced network settings +- improved battery usage and traffic + +Terminal app: +- support SOCKS5 proxy +- `/info` command to show information and servers for contacts and group members: use `/info ` for contact and `/info # ` for member information. + +## v3.0 + +9 July, 2022 + +Changes: + +Chat core: +- support for push notifications on iOS +- support for database export/import in mobile clients + +Terminal client: +- automatically accept contact requests and sending reply message with `/auto_accept on` and `/auto_accept on ` coomands + +Mobile clients: +- instant push notifications for iOS (the sending clients have to be upgraded too for notifications to work), +- e2e encrypted WebRTC audio/video calls, +- export and import of chat database, allowing to move the chat profile to another device, +- improved privacy and performance of the protocol. + +Please see [this post](https://github.com/simplex-chat/simplex-chat/blob/stable/blog/20220711-simplex-chat-v3-released-ios-notifications-audio-video-calls-database-export-import-protocol-improvements.md) for more details. + +## v2.2 + +1 June, 2022 + +Changes: +- WebRTC calls integrated with CallKit (iOS) +- call notifications and alerts (Android) +- local authentication / app lock (both platforms) +- call settings and invitation timeouts +- privacy settings: auto-accepting images, link previews +- paste image from clipboard (iOS) +- SMP servers settings page (iOS) + +## v2.1 + +21 May, 2022 + +New commands for terminal users: +- /clear - delete all messages in a conversation +- /image - send file as image for mobile clients +- /fforward - forward file to another conversation +- /image_forward - forward image to another conversation + +## v2.0 + +11 May, 2022 + +For terminal users: +- /tail command to show the last messages from a given chat or from all chats + +## v1.6 + +16 April, 2022 + +Changes: +- Improved stability of network connection. +- The new protocol to exchange files, in preparation to support images, files and groups in mobile apps. It makes sending files to groups much more efficient, and allows attaching files to the text messages. This version is backwards and forwards compatible, so you can exchange the files with the previous version. It will not be possible to receive the files sent from the next version (1.7) in the previous version (1.5) - please upgrade. +- **Up arrow** key in the terminal can be used to edit the last message you sent. +- CLI option to execute a single command / send one message, e.g. to use in CI to notify about the build completion, or for any other scenario. +- Library support + [chat bot examples](https://github.com/simplex-chat/simplex-chat/tree/stable/apps) to create SimpleX Chat chat bots. + +## v1.5 + +3 April, 2022 + +Edit, delete and reply to messages, in the mobile apps and from the terminal. + +## v1.4 + +26 March, 2022 + +Changes: +- message edit and delete in mobile apps +- profile images +- TCP keep-alive replacing SMP protocol pings (improved connection stability) +- bug fixes for chat scrolling and empty chat views + +## v1.3 + +26 February, 2022 + +Changes: +- markdown support in messages (both platforms) +- user addresses (Android) +- group member names shown in messages +- display name validation +- asynchronous message processing (improved performance) +- search in chats +- Android app UI redesign (welcome page, help view, dark mode fixes) + +## v1.2 + +14 February, 2022 + +Changes: +- message sent/unread status indicators (iOS) +- search in chats +- auto-accept contact requests option +- deduplicate contact requests +- iOS public beta launch +- connection stability fixes + +## v1.1 + +2 February, 2022 + +- TLS 1.3 support. +- Terminal app is now also a backend for our new mobile app - public access to our new iOS app via TestFlight is coming soon! +- The code base now includes an iOS app preview. + +## v1.0 + +12 January, 2022 + +### The most private and secure chat and application platform + +We are building a new platform for distributed Internet applications where privacy of the messages _and_ the network matter. [SimpleX Chat](https://github.com/simplex-chat/simplex-chat) is our first application, a messaging application built on the SimpleX platform. + +### What is SimpleX? + +There is currently no messaging application other than SimpleX Chat that guarantees metadata privacy - who is communicating with whom and when. SimpleX is designed to not use any permanent users identities to protect meta-data privacy. See [SimpleX overview](https://github.com/simplex-chat/simplexmq/blob/master/protocol/overview-tjr.md) for more details. + +### SimpleX protocol changes + +Best possible E2E encryption - the only messenger using two-layer E2E encryption, with one layer using double ratchet protocol that provides forward secrecy and break-in recovery, and additional encryption layer providing meta-data protection. See more details about encryption algorithms in [SimpleXMQ change log](https://github.com/simplex-chat/simplexmq/blob/master/CHANGELOG.md#100). + +Performance and space efficiency improvements - protocol overhead is reduced from circa 15% to 3.7% thanks to binary encoding, and performance is substantially improved due to more efficient cryptographic algorithms. + +Shorter invitation and contact links due to switching from long RSA to much shorter Curve448/25519 keys - for example, you can connect to the team via [team's SimpleX Chat contact address](https://simplex.chat/contact#/?v=1&smp=smp%3A%2F%2FPQUV2eL0t7OStZOoAsPEV2QYWt4-xilbakvGUGOItUo%3D%40smp6.simplex.im%2FK1rslx-m5bpXVIdMZg9NLUZ_8JBm8xTt%23MCowBQYDK2VuAyEALDeVe-sG8mRY22LsXlPgiwTNs9dbiLrNuA7f3ZMAJ2w%3D) (you need to use it in terminal app) or just by using `/simplex` command in the chat. + +This [this post](https://github.com/simplex-chat/simplex-chat/blob/master/blog/20220112-simplex-chat-v1-released.md) for more information. + diff --git a/apps/ios/Shared/Model/AppAPITypes.swift b/apps/ios/Shared/Model/AppAPITypes.swift index c93cc233f5..547c2b7000 100644 --- a/apps/ios/Shared/Model/AppAPITypes.swift +++ b/apps/ios/Shared/Model/AppAPITypes.swift @@ -73,6 +73,7 @@ enum ChatCommand: ChatCmdProtocol { case apiNewGroup(userId: Int64, incognito: Bool, groupProfile: GroupProfile) case apiNewPublicGroup(userId: Int64, incognito: Bool, relayIds: [Int64], groupProfile: GroupProfile) case apiGetGroupRelays(groupId: Int64) + case apiAddGroupRelays(groupId: Int64, relayIds: [Int64]) case apiAddMember(groupId: Int64, contactId: Int64, memberRole: GroupMemberRole) case apiJoinGroup(groupId: Int64) case apiAcceptMember(groupId: Int64, groupMemberId: Int64, memberRole: GroupMemberRole) @@ -275,6 +276,7 @@ enum ChatCommand: ChatCmdProtocol { case let .apiNewGroup(userId, incognito, groupProfile): return "/_group \(userId) incognito=\(onOff(incognito)) \(encodeJSON(groupProfile))" case let .apiNewPublicGroup(userId, incognito, relayIds, groupProfile): return "/_public group \(userId) incognito=\(onOff(incognito)) \(relayIds.map(String.init).joined(separator: ",")) \(encodeJSON(groupProfile))" case let .apiGetGroupRelays(groupId): return "/_get relays #\(groupId)" + case let .apiAddGroupRelays(groupId, relayIds): return "/_add relays #\(groupId) \(relayIds.map(String.init).joined(separator: ","))" case let .apiAddMember(groupId, contactId, memberRole): return "/_add #\(groupId) \(contactId) \(memberRole)" case let .apiJoinGroup(groupId): return "/_join #\(groupId)" case let .apiAcceptMember(groupId, groupMemberId, memberRole): return "/_accept member #\(groupId) \(groupMemberId) \(memberRole.rawValue)" @@ -468,6 +470,7 @@ enum ChatCommand: ChatCmdProtocol { case .apiNewGroup: return "apiNewGroup" case .apiNewPublicGroup: return "apiNewPublicGroup" case .apiGetGroupRelays: return "apiGetGroupRelays" + case .apiAddGroupRelays: return "apiAddGroupRelays" case .apiAddMember: return "apiAddMember" case .apiJoinGroup: return "apiJoinGroup" case .apiAcceptMember: return "apiAcceptMember" @@ -944,6 +947,8 @@ enum ChatResponse2: Decodable, ChatAPIResult { case publicGroupCreated(user: UserRef, groupInfo: GroupInfo, groupLink: GroupLink, groupRelays: [GroupRelay]) case publicGroupCreationFailed(user: UserRef, addRelayResults: [AddRelayResult]) case groupRelays(user: UserRef, groupInfo: GroupInfo, groupRelays: [GroupRelay]) + case groupRelaysAdded(user: UserRef, groupInfo: GroupInfo, groupLink: GroupLink, groupRelays: [GroupRelay]) + case groupRelaysAddFailed(user: UserRef, addRelayResults: [AddRelayResult]) case sentGroupInvitation(user: UserRef, groupInfo: GroupInfo, contact: Contact, member: GroupMember) case userAcceptedGroupSent(user: UserRef, groupInfo: GroupInfo, hostContact: Contact?) case userDeletedMembers(user: UserRef, groupInfo: GroupInfo, members: [GroupMember], withMessages: Bool) @@ -997,6 +1002,8 @@ enum ChatResponse2: Decodable, ChatAPIResult { case .publicGroupCreated: "publicGroupCreated" case .publicGroupCreationFailed: "publicGroupCreationFailed" case .groupRelays: "groupRelays" + case .groupRelaysAdded: "groupRelaysAdded" + case .groupRelaysAddFailed: "groupRelaysAddFailed" case .sentGroupInvitation: "sentGroupInvitation" case .userAcceptedGroupSent: "userAcceptedGroupSent" case .userDeletedMembers: "userDeletedMembers" @@ -1046,6 +1053,8 @@ enum ChatResponse2: Decodable, ChatAPIResult { case let .publicGroupCreated(u, groupInfo, groupLink, groupRelays): return withUser(u, "groupInfo: \(groupInfo)\ngroupLink: \(groupLink)\ngroupRelays: \(groupRelays)") case let .publicGroupCreationFailed(u, addRelayResults): return withUser(u, "addRelayResults: \(addRelayResults)") case let .groupRelays(u, groupInfo, groupRelays): return withUser(u, "groupInfo: \(groupInfo)\ngroupRelays: \(groupRelays)") + case let .groupRelaysAdded(u, groupInfo, groupLink, groupRelays): return withUser(u, "groupInfo: \(groupInfo)\ngroupLink: \(groupLink)\ngroupRelays: \(groupRelays)") + case let .groupRelaysAddFailed(u, addRelayResults): return withUser(u, "addRelayResults: \(addRelayResults)") case let .sentGroupInvitation(u, groupInfo, contact, member): return withUser(u, "groupInfo: \(groupInfo)\ncontact: \(contact)\nmember: \(member)") case let .userAcceptedGroupSent(u, groupInfo, hostContact): return withUser(u, "groupInfo: \(groupInfo)\nhostContact: \(String(describing: hostContact))") case let .userDeletedMembers(u, groupInfo, members, withMessages): return withUser(u, "groupInfo: \(groupInfo)\nmembers: \(members)\nwithMessages: \(withMessages)") diff --git a/apps/ios/Shared/Model/ChatModel.swift b/apps/ios/Shared/Model/ChatModel.swift index 9c23ac6307..a1d28b8e22 100644 --- a/apps/ios/Shared/Model/ChatModel.swift +++ b/apps/ios/Shared/Model/ChatModel.swift @@ -344,9 +344,12 @@ class ChannelRelaysModel: ObservableObject { } func updateRelay(_ groupInfo: GroupInfo, _ relay: GroupRelay) { - if groupId == groupInfo.groupId, - let i = groupRelays.firstIndex(where: { $0.groupRelayId == relay.groupRelayId }) { - groupRelays[i] = relay + if groupId == groupInfo.groupId { + if let i = groupRelays.firstIndex(where: { $0.groupRelayId == relay.groupRelayId }) { + groupRelays[i] = relay + } else { + groupRelays.append(relay) + } } } diff --git a/apps/ios/Shared/Model/SimpleXAPI.swift b/apps/ios/Shared/Model/SimpleXAPI.swift index 85bb8a30b4..ea2de31569 100644 --- a/apps/ios/Shared/Model/SimpleXAPI.swift +++ b/apps/ios/Shared/Model/SimpleXAPI.swift @@ -1891,6 +1891,22 @@ func apiGetGroupRelays(_ groupId: Int64) async -> [GroupRelay] { return [] } +enum AddGroupRelaysResult { + case added(GroupInfo, GroupLink, [GroupRelay]) + case addFailed([AddRelayResult]) +} + +func apiAddGroupRelays(_ groupId: Int64, relayIds: [Int64]) async throws -> AddGroupRelaysResult? { + let r: APIResult? = await chatApiSendCmdWithRetry(.apiAddGroupRelays(groupId: groupId, relayIds: relayIds)) + switch r { + case let .result(.groupRelaysAdded(_, groupInfo, groupLink, groupRelays)): + return .added(groupInfo, groupLink, groupRelays) + case let .result(.groupRelaysAddFailed(_, addRelayResults)): + return .addFailed(addRelayResults) + default: if let r { throw r.unexpected } else { return nil } + } +} + func apiAddMember(_ groupId: Int64, _ contactId: Int64, _ memberRole: GroupMemberRole) async throws -> GroupMember { let r: ChatResponse2 = try await chatSendCmd(.apiAddMember(groupId: groupId, contactId: contactId, memberRole: memberRole)) if case let .sentGroupInvitation(_, _, _, member) = r { return member } diff --git a/apps/ios/Shared/Views/Chat/ChatItem/CILinkView.swift b/apps/ios/Shared/Views/Chat/ChatItem/CILinkView.swift index b3fdd3f8e3..4eb187bcac 100644 --- a/apps/ios/Shared/Views/Chat/ChatItem/CILinkView.swift +++ b/apps/ios/Shared/Views/Chat/ChatItem/CILinkView.swift @@ -14,6 +14,7 @@ import SimpleXChat struct CILinkView: View { @EnvironmentObject var theme: AppTheme let linkPreview: LinkPreview + let maxWidth: CGFloat @State private var blurred: Bool = UserDefaults.standard.integer(forKey: DEFAULT_PRIVACY_MEDIA_BLUR_RADIUS) > 0 var body: some View { @@ -21,7 +22,8 @@ struct CILinkView: View { if let uiImage = imageFromBase64(linkPreview.image) { Image(uiImage: uiImage) .resizable() - .aspectRatio(1 / heightRatio(uiImage.size), contentMode: .fill) + .scaledToFill() + .frame(width: maxWidth, height: maxWidth * heightRatio(uiImage.size)) .clipped() .modifier(PrivacyBlur(blurred: $blurred)) .if(!blurred) { v in @@ -116,7 +118,7 @@ struct LargeLinkPreview_Previews: PreviewProvider { description: "", image: "data:image/jpg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAuKADAAQAAAABAAAAYAAAAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+/8AAEQgAYAC4AwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/bAEMAAQEBAQEBAgEBAgMCAgIDBAMDAwMEBgQEBAQEBgcGBgYGBgYHBwcHBwcHBwgICAgICAkJCQkJCwsLCwsLCwsLC//bAEMBAgICAwMDBQMDBQsIBggLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLC//dAAQADP/aAAwDAQACEQMRAD8A/v4ooooAKKKKACiiigAooooAKK+CP2vP+ChXwZ/ZPibw7dMfEHi2VAYdGs3G9N33TO/IiU9hgu3ZSOa/NzXNL/4KJ/td6JJ49+NXiq2+Cvw7kG/ZNKbDMLcjKblmfI/57SRqewrwMdxBRo1HQoRdWqt1HaP+KT0j838j7XKOCMXiqEcbjKkcPh5bSne8/wDr3BXlN+is+5+43jb45/Bf4bs0fj/xZpGjSL1jvL2KF/8AvlmDfpXjH/DfH7GQuPsv/CydD35x/wAfIx+fT9a/AO58D/8ABJj4UzvF4v8AFfif4l6mp/evpkfkWzP3w2Isg+omb61X/wCF0/8ABJr/AI9f+FQeJPL6ed9vbzPrj7ZivnavFuIT+KhHyc5Sf3wjY+7w/hlgZQv7PF1P70aUKa+SqTUvwP6afBXx2+CnxIZYvAHi3R9ZkfpHZ3sUz/8AfKsW/SvVq/lItvBf/BJX4rTLF4V8UeJ/hpqTH91JqUfn2yv2y2JcD3MqfUV9OaFon/BRH9krQ4vH3wI8XW3xq+HkY3+XDKb/ABCvJxHuaZMDr5Ergd1ruwvFNVrmq0VOK3lSkp29Y6SS+R5GY+HGGi1DD4qVKo9oYmm6XN5RqK9Nvsro/obor4A/ZC/4KH/Bv9qxV8MLnw54vjU+bo9443SFPvG3k4EoHdcB17rjmvv+vqcHjaGKpKth5qUX1X9aPyZ+b5rlOMy3ESwmOpOFRdH+aezT6NXTCiiiuo84KKKKACiiigCC6/49pP8AdP8AKuOrsbr/AI9pP90/yrjqAP/Q/v4ooooAKKKKACiiigAr8tf+ChP7cWs/BEWfwD+A8R1P4k+JQkUCQr5rWUc52o+zndNIf9Up4H324wD9x/tDfGjw/wDs9fBnX/i/4jAeHRrZpI4c4M87YWKIe7yFV9gc9q/n6+B3iOb4GfCLxL/wU1+Oypq3jzxndT2nhK2uBwZptyvcBeoQBSq4xthjwPvivluIs0lSthKM+WUk5Sl/JBbtebekfM/R+BOHaeIcszxVL2kISUKdP/n7WlrGL/uxXvT8u6uizc6b8I/+CbmmRePPi9HD8Q/j7rifbktLmTz7bSGm582ZzktITyX++5+5tX5z5L8LPgv+0X/wVH12+8ZfEbxneW/2SRxB9o02eTSosdY4XRlgjYZGV++e5Jr8xvF3i7xN4+8UX/jXxney6jquqTNcXVzMcvJI5ySfQdgBwBgDgV+sP/BPX9jj9oL9oXw9H4tuvG2s+DfAVlM8VsthcyJLdSBsyCBNwREDZ3SEHLcBTgkfmuX4j+0MXHB06LdBXagna/8AenK6u+7el9Ej9+zvA/2Jls81r4uMcY7J1px5lHf93ShaVo9FFJNq8pMyPil/wRs/aj8D6dLq3gq70vxdHECxgtZGtrogf3UmAQn2EmT2r8rPEPh3xB4R1u58M+KrGfTdRsnMdxa3MbRTROOzKwBBr+674VfCnTfhNoI0DTtX1jWFAGZtYvpL2U4934X/AICAK8V/aW/Yf/Z9/areHUvibpkkerWsRhg1KxkMFyqHkBiMrIAeQJFYDJxjJr6bNPD+nOkqmAfLP+WTuvk7XX4/I/PeHvG6tSxDo5zH2lLpUhHll6uN7NelmvPY/iir2T4KftA/GD9njxMvir4Q65caTPkGWFTutrgD+GaE/I4+oyOxB5r2n9tb9jTxj+x18RYvD+pTtqmgaqrS6VqezZ5qpjfHIBwsseRuA4IIYdcD4yr80q0sRgcQ4SvCpB+jT8mvzP6Bw2JwOcYGNany1aFRdVdNdmn22aauno9T9tLO0+D/APwUr02Txd8NI4Ph38ftGT7b5NtIYLXWGh58yJwQVkBGd/8ArEP3i6fMP0R/4J7ftw6/8YZ7z9nb9oGJtN+JPhoPFIJ18p75IPlclegnj/5aKOGHzrxnH8rPhXxT4j8D+JbHxj4QvZdO1TTJkuLW5hba8UqHIIP8x0I4PFfsZ8bPEdx+0N8FvDv/AAUl+CgXSfiJ4EuYLXxZBbDALw4CXO0clMEZznMLlSf3Zr7PJM+nzyxUF+9ir1IrRVILeVtlOO+lrr5n5RxfwbRdKGXVXfDzfLRm9ZUKr+GDlq3RqP3UnfllZfy2/ptorw/9m/43aF+0X8FNA+L+gARpq1uGnhByYLlCUmiP+44IHqMHvXuFfsNGtCrTjVpu8ZJNPyZ/LWKwtXDVp4evG04Nxa7NOzX3hRRRWhzhRRRQBBdf8e0n+6f5Vx1djdf8e0n+6f5Vx1AH/9H+/iiiigAooooAKKKKAPw9/wCCvXiPWviH4q+F/wCyN4XlKT+K9TS6uQvoXFvAT7AvI3/AQe1fnF/wVO+IOnXfxx034AeDj5Xhv4ZaXb6TawKfkE7Ro0rY6bgvlofdT61+h3xNj/4Tv/gtd4Q0W/8Anh8P6THLGp6Ax21xOD/324Nfg3+0T4kufGH7QHjjxRdtukvte1GXJ9PPcKPwAAr8a4pxUpLEz6zq8n/btOK0+cpX9Uf1d4c5bCDy+lbSlh3W/wC38RNq/qoQcV5M8fjiaeRYEOGchR9TxX9svw9+GHijSvgB4I+Gnwr1ceGbGztYY728gijluhbohLLAJVeJZJJCN0jo+0Zwu4gj+JgO8REsf3l+YfUV/bf8DNVm+Mv7KtkNF1CTTZ9Z0d4Ir2D/AFls9zF8sidPmj3hhz1Fel4YyhGtiHpzWjur6e9f9Dw/H9VXQwFvgvUv62hb8Oa3zPoDwfp6aPoiaONXuNaa1Zo3ubp43nLDqrmJEXI/3QfWukmjMsTRBihYEbl6jPcZ7ivxk/4JMf8ABOv9ob9hBvFdr8ZvGOma9Yak22wttLiYGV2kMkl1dzSIkkkzcKisX8tSwDYNfs/X7Bj6NOlXlCjUU4/zJWv8j+ZsNUnOmpThyvtufj/+1Z8Hf2bPi58PviF8Avh/4wl1j4iaBZjXG0m71qfU7i3u4FMqt5VxLL5LzR70Kx7AVfJXAXH8sysGUMOh5r+vzwl+wD+y78KP2wPEX7bGn6xqFv4g8QmWa70+fUFGlrdTRmGS4EGATIY2dRvdlXe+0DPH83Nh+x58bPFev3kljpSaVYPcymGS+kEX7oudp2DL/dx/DX4Z4xZxkmCxGHxdTGRTlG0ueUU7q3S93a7S69Oh/SngTnNSjgcZhMc1CnCSlC70966dr/4U7Lq79T5Kr9MP+CWfxHsNH+P138EPF2JvDfxL0640a9gc/I0vls0Rx6kb4x/v1x3iz9hmHwV4KuPFHiLxlaWkltGzt5sBSAsBkIHL7iT0GFJJ7V8qfAnxLc+D/jd4N8V2bFJdP1vT5wR/szoT+YyK/NeD+Lcvx+Ijisuq88ackpPlklruveSvdX2ufsmavC5zlWKw9CV7xaTs1aSV4tXS1Ukmrdj9/P8Agkfrus/DD4ifFP8AY/8AEkrPJ4Z1F7y1DeiSG3mI9m2wv/wI1+5Ffhd4Ki/4Qf8A4Lb+INM0/wCSHxDpDySqOhL2cMx/8fizX7o1/RnC7ccLPDP/AJdTnBeid1+DP5M8RkqmZUselZ4ijSqv1lG0vvcWwooor6Q+BCiiigCC6/49pP8AdP8AKuOrsbr/AI9pP90/yrjqAP/S/v4ooooAKKKKACiiigD8LfiNIfBP/BbLwpq9/wDJDr2kJHGTwCZLS4gH/j0eK/Bj9oPw7c+Evj3428M3ilZLHXtRiIPoJ3x+Ywa/fL/grnoWsfDPx98K/wBrzw5EzyeGNSS0uSvokguYQfZtsy/8CFfnB/wVP+HNho/7QFp8bvCeJvDnxK0231mznQfI0vlqsoz6kbJD/v1+M8U4WUViYW1hV5/+3akVr/4FG3qz+r/DnMYTeX1b6VcP7L/t/Dzenq4Tcl5I/M2v6yP+CR3j4eLP2XbLRZZN0uku9sRnp5bMB/45sr+Tev3u/wCCJXj7yNW8T/DyZ+C6XUak9pUw36xD865uAcV7LNFTf24tfd736Hd405d9Y4cddLWlOMvk7wf/AKUvuP6Kq/P/APaa+InjJfF8vge3lez06KONgIyVM+8ZJYjkgHIx045r9AK/Gr/gsB8UPHXwg8N+AvFfgV4oWmv7u3uTJEsiyL5SsiNkZxkMeCDmvU8bsgzPN+Fa+FyrEujUUot6tKcdnBtapO6fny2ejZ/OnAOFWJzqjheVOU+ZK+yaTlfr2t8z85td/b18H6D4n1DQLrw5fSLY3Elv5okRWcxsVJKMAVyR0yTivEPHf7f3jjVFe18BaXb6PGeBPcH7RN9QMBAfqGrFP7UPwj8c3f2/4y/DuzvbxgA93ZNtd8dyGwT+Lmuvh/aP/ZT8IxC58EfD0y3Y5UzwxKAf99mlP5Cv49wvCeBwUoc3D9Sday3qRlTb73c7Wf8Aej8j+rKWVUKLV8vlKf8AiTj/AOlW+9Hw74w8ceNvHl8NX8bajc6jK2SjTsSo/wBxeFUf7orovgf4dufF3xp8H+F7NS0uoa3p8Cgf7c6A/pW98avjx4q+NmoW0mswW9jY2G/7LaWy4WPfjJLHlicD0HoBX13/AMEtPhrZeI/2jH+L3inEPh34cWE+t31w/wBxJFRliBPqPmkH/XOv3fhXCVa/1ahUoRoybV4RacYq/dKK0jq7Ky1s3uezm+PeByeviqkFBxhK0U767RirJattLTqz9H/CMg8af8Futd1DT/ni8P6OySsOxSyiiP8A49Niv3Qr8NP+CS+j6t8V/iv8V/2wdfiZD4i1B7K0LDtLJ9olUf7imFfwr9y6/oLhe88LUxPSrUnNejdl+CP5G8RWqeY0cAnd4ejSpP8AxRjd/c5NBRRRX0h8CFFFFAEF1/x7Sf7p/lXHV2N1/wAe0n+6f5Vx1AH/0/7+KKKKACiiigAooooA8M/aT+B+iftGfBLxB8INcIjGrWxFvORnyLmMh4ZB/uSAE46jI71+AfwU8N3H7SXwL8Qf8E5fjFt0r4kfD65nuvCstycbmhz5ltuPVcE4x1idWHEdf031+UX/AAUL/Yj8T/FG/sv2mP2c5H074keGtkoFufLe+jg5Taennx9Ezw6/Ie2PleI8slUtjKUOZpOM4/zwe6X96L1j5/cfpPAXEMKF8rxNX2cZSU6VR7Uq0dE3/cmvcn5dldn8r/iXw3r/AIN8Q3vhPxXZy6fqemzPb3VtMNskUsZwysPY/n1HFfe3/BL3x/8A8IP+1bptvK+2HVbeSBvdoyso/RWH419SX8fwg/4Kc6QmleIpLfwB8f8ASI/ssiXCGC11kwfLtZSNwkGMbceZH0w6Dj88tM+HvxW/ZK/aO8OQ/FvR7nQ7uw1OElpV/czQs+x2ilGUkUqTypPvivy3DYWWX46hjaT56HOrSXa+ql/LK26fy0P6LzDMYZ3lGMynEx9ni/ZyvTfV2bjKD+3BtJqS9HZn9gnxB/aM+Cvwp8XWXgj4ja/Bo+o6hB9ogW5DrG0ZYoCZNvlr8wI+Zh0r48/4KkfDey+NP7GOqeIPDUsV7L4elh1u0khYOskcOVl2MCQcwu5GDyRXwx/wVBnbVPH3gjxGeVvPDwUt2LxzOW/9Cr87tO8PfFXVdPisbDS9avNImbzLNILa4mtXfo5j2KULZwDjmvqs+4srKvi8rqYfnjays2nqlq9JX3v0P4FwfiDisjzqNanQU3RnGUbNq9rOz0ej207nxZovhrV9enMNhHwpwztwq/U+vt1qrrWlT6JqUumXBDNHj5l6EEZr7U+IHhHxF8JvEUHhL4j2Umiald2sV/Hb3Q8t2hnztbB75BDKfmVgQQCK8e0f4N/E349/FRvBvwh0a41y+YRq/kD91ECPvSyHCRqPVmFfl8aNZ1vYcj59rWd79rbn9T+HPjFnnEPE1WhmmEWEwKw8qkVJNbSppTdSSimmpO1ko2a3aueH+H/D+ueLNds/DHhi0lv9R1CZLe2toV3SSyyHCqoHUk1+yfxl8N3X7Ln7P+h/8E9/hOF1X4nfEm4gufFDWp3FBMR5dqGHRTgLzx5au5wJKtaZZ/B7/gmFpBhsJLbx78fdVi+zwQWyma00UzjbgAfMZDnGMCSToAiElvv/AP4J7fsS+LPh5q15+1H+0q76h8R/Em+ZUuSHksI5/vFj0E8g4YDiNPkH8VfeZJkVTnlhYfxpK02tqUHur7c8trdFfzt9dxdxjQ9lDMKi/wBlpvmpRejxFVfDK26o03713bmla2yv90/sw/ArRv2bvgboHwh0crK2mQZup1GPPu5Tvmk9fmcnGei4HavfKKK/YaFGFGnGlTVoxSSXkj+WMXi6uKr1MTXlec25N923dsKKKK1OcKKKKAILr/j2k/3T/KuOrsbr/j2k/wB0/wAq46gD/9T+/iiiigAooooAKKKKACiiigD87P2wf+Ccnwm/ahmbxvosh8K+NY8NHq1onyzOn3ftEYK7yMcSKVkX1IAFfnT4m8f/ALdv7L+gyfDn9rjwFb/GLwFD8q3ssf2srGOjfaAjspA6GeMMOzV/RTRXz+N4eo1akq+Hm6VR7uNrS/xRekvzPuMo45xOGoQweOpRxFCPwqd1KH/XuorSh8m0uiPwz0L/AIKEf8E3vi6miH4saHd6Xc6B5gs4tWs3vYIPNILAGFpA65UcSLxjgCvtS1/4KT/sLWVlHFZePrCGCJAqRJa3K7VHQBRFxj0xXv8A48/Zc/Zx+J0z3Xj3wPoupzyHLTS2cfnE+8iqH/WvGP8Ah23+w953n/8ACu9PznOPMn2/98+bj9K5oYTOqMpSpyoyb3k4yjJ2015Xqac/BNSbrPD4mlKW6hKlJf8AgUkpP5n5zfta/tof8Ex/jPq+k+IPHelan491HQlljtI7KGWyikWUqSkryNCzJlcgc4JPHNcZ4V+Iv7c37TGgJ8N/2Ovh7bfB7wHN8pvoo/shMZ4LfaSiMxx1MERf/ar9sPAn7LH7N3wxmS68B+BtF02eM5WaOzjMwI9JGBf9a98AAGBWSyDF16kquKrqPN8Xso8rfrN3lY9SXG+WYPDww2W4SdRQ+B4io5xjre6pRtTvfW+up+cv7H//AATg+FX7MdynjzxHMfFnjeTLvqt2vyQO/wB77OjFtpOeZGLSH1AOK/Rqiivo8FgaGEpKjh4KMV/V33fmz4LNs5xuZ4h4rHVXOb6vouyWyS6JJIKKKK6zzAooooAKKKKAILr/AI9pP90/yrjq7G6/49pP90/yrjqAP//Z" ) - CILinkView(linkPreview: preview) + CILinkView(linkPreview: preview, maxWidth: 360) .previewLayout(.fixed(width: 360, height: 200)) } } diff --git a/apps/ios/Shared/Views/Chat/ChatItem/FramedItemView.swift b/apps/ios/Shared/Views/Chat/ChatItem/FramedItemView.swift index 112aa33c31..d09289c1d5 100644 --- a/apps/ios/Shared/Views/Chat/ChatItem/FramedItemView.swift +++ b/apps/ios/Shared/Views/Chat/ChatItem/FramedItemView.swift @@ -167,7 +167,7 @@ struct FramedItemView: View { case let .report(text, reason): ciMsgContentView(chatItem, txtPrefix: reason.attrString) case let .link(_, preview): - CILinkView(linkPreview: preview) + CILinkView(linkPreview: preview, maxWidth: maxWidth) ciMsgContentView(chatItem) case let .chat(text, chatLink, ownerSig): let hasText = text != chatLink.connLinkStr diff --git a/apps/ios/Shared/Views/Chat/ChatView.swift b/apps/ios/Shared/Views/Chat/ChatView.swift index e165c01710..66148034df 100644 --- a/apps/ios/Shared/Views/Chat/ChatView.swift +++ b/apps/ios/Shared/Views/Chat/ChatView.swift @@ -745,7 +745,7 @@ struct ChatView: View { ChannelRelaysModel.shared.set(groupId: groupInfo.groupId, groupRelays: relays) } } - } else { + } else if groupInfo.membership.memberCurrent { Task { if let gInfo = await apiGetUpdatedGroupLinkData(groupInfo.groupId) { await MainActor.run { @@ -2175,8 +2175,14 @@ struct ChatView: View { ) } .confirmationDialog("Delete message?", isPresented: $showDeleteMessage, titleVisibility: .visible) { - Button("Delete for me", role: .destructive) { - deleteMessage(.cidmInternal, moderate: false) + if publicGroupEditor(chat) { + Button("Delete from history", role: .destructive) { + deleteMessage(.cidmHistory, moderate: false) + } + } else { + Button("Delete for me", role: .destructive) { + deleteMessage(.cidmInternal, moderate: false) + } } if let di = deletingItem, di.meta.deletable && !di.localNote && !di.isReport { Button(broadcastDeleteButtonText(chat), role: .destructive) { @@ -2185,8 +2191,14 @@ struct ChatView: View { } } .confirmationDialog(deleteMessagesTitle, isPresented: $showDeleteMessages, titleVisibility: .visible) { - Button("Delete for me", role: .destructive) { - deleteMessages(chat, deletingItems, moderate: false) + if publicGroupEditor(chat) { + Button("Delete from history", role: .destructive) { + deleteMessages(chat, deletingItems, .cidmHistory, moderate: false) + } + } else { + Button("Delete for me", role: .destructive) { + deleteMessages(chat, deletingItems, moderate: false) + } } } .confirmationDialog(archivingReports?.count == 1 ? "Archive report?" : "Archive \(archivingReports?.count ?? 0) reports?", isPresented: $showArchivingReports, titleVisibility: .visible) { @@ -2817,6 +2829,9 @@ struct ChatView: View { } } catch { logger.error("ChatView.deleteMessage error: \(error)") + await MainActor.run { + showAlert(NSLocalizedString("Error deleting message", comment: "alert title"), message: responseError(error)) + } } } } @@ -2963,6 +2978,14 @@ class FloatingButtonModel: ObservableObject { } +private func publicGroupEditor(_ chat: Chat) -> Bool { + if case let .group(groupInfo, _) = chat.chatInfo { + groupInfo.useRelays && groupInfo.membership.memberRole >= .moderator + } else { + false + } +} + private func broadcastDeleteButtonText(_ chat: Chat) -> LocalizedStringKey { chat.chatInfo.featureEnabled(.fullDelete) ? "Delete for everyone" : "Mark deleted for everyone" } @@ -3010,6 +3033,9 @@ private func deleteMessages(_ chat: Chat, _ deletingItems: [Int64], _ mode: CIDe await onSuccess() } catch { logger.error("ChatView.deleteMessages error: \(error.localizedDescription)") + await MainActor.run { + showAlert(NSLocalizedString("Error deleting message", comment: "alert title"), message: responseError(error)) + } } } } diff --git a/apps/ios/Shared/Views/Chat/ComposeMessage/ComposeView.swift b/apps/ios/Shared/Views/Chat/ComposeMessage/ComposeView.swift index 334abd76ee..5c57a46129 100644 --- a/apps/ios/Shared/Views/Chat/ComposeMessage/ComposeView.swift +++ b/apps/ios/Shared/Views/Chat/ComposeMessage/ComposeView.swift @@ -395,13 +395,13 @@ struct ComposeView: View { if let gInfo = chat.chatInfo.groupInfo, gInfo.useRelays, ![.memRejected, .memLeft, .memRemoved, .memGroupDeleted].contains(gInfo.membership.memberStatus) { if gInfo.membership.memberRole == .owner { - if let s = ownerState, s.activeCount < s.relays.count { + if let s = ownerState, s.relays.isEmpty || s.activeCount < s.relays.count { ownerChannelRelayBar(relays: s.relays, activeCount: s.activeCount, failedCount: s.failedCount, removedCount: s.removedCount) } } else { let hostnames = (chatModel.channelRelayHostnames[gInfo.groupId] ?? []).sorted() let relayMembers = chatModel.groupMembers - .filter { $0.wrapped.memberRole == .relay } + .filter { $0.wrapped.memberRole == .relay && ![.memRemoved, .memGroupDeleted].contains($0.wrapped.memberStatus) } .sorted { hostFromRelayLink($0.wrapped.relayLink ?? "") < hostFromRelayLink($1.wrapped.relayLink ?? "") } let showProgress = !gInfo.nextConnectPrepared || composeState.inProgress let removedCount = relayMembers.filter { relayMemberRemoved($0.wrapped.memberStatus) }.count @@ -409,7 +409,7 @@ struct ComposeView: View { let failedCount = relayMembers.filter { !relayMemberRemoved($0.wrapped.memberStatus) && $0.wrapped.activeConn?.connFailedErr != nil }.count let resolvedCount = connectedCount + removedCount + failedCount let total = relayMembers.count > 0 ? relayMembers.count : hostnames.count - if total > 0, removedCount + failedCount > 0 || resolvedCount < total { + if total == 0 || removedCount + failedCount > 0 || resolvedCount < total { subscriberChannelRelayBar( hostnames: hostnames, relayMembers: relayMembers, @@ -735,9 +735,9 @@ struct ComposeView: View { gInfo.membership.memberRole == .owner, ![.memLeft, .memRemoved, .memGroupDeleted].contains(gInfo.membership.memberStatus) else { return nil } - let relays = channelRelaysModel.groupId == gInfo.groupId - ? channelRelaysModel.groupRelays : [] - guard !relays.isEmpty else { return nil } + guard channelRelaysModel.groupId == gInfo.groupId else { return nil } + let relays = channelRelaysModel.groupRelays + guard !relays.isEmpty else { return ([], 0, 0, 0, true) } let relayMembers = relays.map { relay in (relay, chatModel.groupMembers.first(where: { $0.wrapped.groupMemberId == relay.groupMemberId })?.wrapped) } @@ -763,7 +763,11 @@ struct ComposeView: View { if !allBroken && activeCount + failedCount + removedCount < total { RelayProgressIndicator(active: activeCount, total: total) } - if allBroken { + if total == 0 { + Text("No relays") + Image(systemName: "exclamationmark.triangle") + .foregroundColor(.orange) + } else if allBroken { if removedCount == total { Text("All relays removed") } else if failedCount == total { @@ -793,7 +797,7 @@ struct ComposeView: View { } if relayListExpanded { if allBroken { - Text("Adding relays will be supported later.") + Text("Add relays to restore message delivery.") .frame(maxWidth: .infinity, alignment: .leading) .font(.caption) .foregroundColor(theme.colors.secondary) @@ -843,7 +847,11 @@ struct ComposeView: View { let allBroken = connectedCount == 0 && errorCount == total VStack(spacing: 0) { relayBarHeader { - if allBroken { + if total == 0 { + Text("No relays") + Image(systemName: "exclamationmark.triangle") + .foregroundColor(.orange) + } else if allBroken { if removedCount == total { Text("All relays removed") } else if failedCount == total { diff --git a/apps/ios/Shared/Views/Chat/Group/AddGroupRelayView.swift b/apps/ios/Shared/Views/Chat/Group/AddGroupRelayView.swift new file mode 100644 index 0000000000..82b89beaa5 --- /dev/null +++ b/apps/ios/Shared/Views/Chat/Group/AddGroupRelayView.swift @@ -0,0 +1,161 @@ +// +// AddGroupRelayView.swift +// SimpleX (iOS) +// +// Created by simplex on 29.04.2026. +// Copyright © 2026 SimpleX Chat. All rights reserved. +// + +import SwiftUI +import SimpleXChat + +struct AddGroupRelayView: View { + var groupInfo: GroupInfo + var existingRelayIds: Set + var onRelayAdded: () -> Void + @EnvironmentObject var theme: AppTheme + @Environment(\.dismiss) var dismiss + @State private var availableRelays: [(relayId: Int64, relay: UserChatRelay, operatorName: String?)] = [] + @State private var selectedRelayIds: Set = [] + @State private var isLoading = true + @State private var isAdding = false + + var body: some View { + NavigationView { + List { + if isLoading { + Section { + ProgressView() + .frame(maxWidth: .infinity) + } + } else if availableRelays.isEmpty { + Section { + Text("No available relays") + .foregroundColor(theme.colors.secondary) + } + } else { + Section { + ForEach(availableRelays, id: \.relayId) { item in + relayCheckRow(item.relayId, item.relay, operatorName: item.operatorName) + } + } + } + } + .modifier(ThemedBackground(grouped: true)) + .navigationTitle("Add relays") + .navigationBarTitleDisplayMode(.inline) + .toolbar { + ToolbarItem(placement: .cancellationAction) { + Button("Cancel") { dismiss() } + } + ToolbarItem(placement: .confirmationAction) { + Button("Add") { addSelectedRelays() } + .disabled(selectedRelayIds.isEmpty || isAdding) + } + } + } + .task { await loadAvailableRelays() } + } + + private func relayCheckRow(_ relayId: Int64, _ relay: UserChatRelay, operatorName: String?) -> some View { + let selected = selectedRelayIds.contains(relayId) + return Button { + if selected { + selectedRelayIds.remove(relayId) + } else { + selectedRelayIds.insert(relayId) + } + } label: { + HStack { + VStack(alignment: .leading) { + Text(chatRelayDisplayName(relay)) + .foregroundColor(theme.colors.onBackground) + .lineLimit(1) + if let opName = operatorName { + Text(opName) + .font(.caption) + .foregroundColor(theme.colors.secondary) + .lineLimit(1) + } + } + Spacer() + Image(systemName: selected ? "checkmark.circle.fill" : "circle") + .foregroundColor(selected ? theme.colors.primary : Color(uiColor: .tertiaryLabel).asAnotherColorFromSecondary(theme)) + } + } + } + + private func loadAvailableRelays() async { + do { + let servers = try await getUserServers() + var relays: [(relayId: Int64, relay: UserChatRelay, operatorName: String?)] = [] + for op in servers { + if let oper = op.operator, oper.enabled != true { continue } + let opName: String? = op.operator?.operatorTag != nil ? op.operator?.tradeName : nil + for relay in op.chatRelays { + if relay.enabled && !relay.deleted, + let relayId = relay.chatRelayId, + !existingRelayIds.contains(relayId) { + relays.append((relayId, relay, opName)) + } + } + } + await MainActor.run { + availableRelays = relays + isLoading = false + } + } catch { + logger.error("loadAvailableRelays error: \(responseError(error))") + await MainActor.run { + isLoading = false + } + } + } + + private func addSelectedRelays() { + let relayIds = Array(selectedRelayIds) + guard !relayIds.isEmpty else { return } + isAdding = true + Task { + do { + guard let result = try await apiAddGroupRelays(groupInfo.groupId, relayIds: relayIds) else { + await MainActor.run { isAdding = false } + return + } + await MainActor.run { + isAdding = false + switch result { + case let .added(gInfo, _, relays): + ChannelRelaysModel.shared.set(groupId: gInfo.groupId, groupRelays: relays) + onRelayAdded() + dismiss() + case let .addFailed(results): + let successIds = Set(results.filter { $0.relayError == nil }.compactMap { $0.relay.chatRelayId }) + if !successIds.isEmpty { + selectedRelayIds.subtract(successIds) + availableRelays.removeAll { successIds.contains($0.relayId) } + onRelayAdded() + } + let errorLines = results.filter { $0.relayError != nil } + .map { "\(chatRelayDisplayName($0.relay)): \($0.relayError.map { connErrorText($0) } ?? "")" } + let successNames = results.filter { $0.relayError == nil } + .map { chatRelayDisplayName($0.relay) } + var msg = errorLines.joined(separator: "\n") + if !successNames.isEmpty { + msg += "\n" + String.localizedStringWithFormat(NSLocalizedString("Relays added: %@.", comment: "alert message"), successNames.joined(separator: ", ")) + } + showAlert( + NSLocalizedString("Error adding relays", comment: "alert title"), + message: msg + ) + } + } + } catch { + await MainActor.run { + isAdding = false + showAlert(NSLocalizedString("Error adding relays", comment: "alert title"), message: responseError(error)) + } + } + } + } +} diff --git a/apps/ios/Shared/Views/Chat/Group/ChannelRelaysView.swift b/apps/ios/Shared/Views/Chat/Group/ChannelRelaysView.swift index c88d639199..6600cec47b 100644 --- a/apps/ios/Shared/Views/Chat/Group/ChannelRelaysView.swift +++ b/apps/ios/Shared/Views/Chat/Group/ChannelRelaysView.swift @@ -14,24 +14,49 @@ struct ChannelRelaysView: View { var groupInfo: GroupInfo @EnvironmentObject var chatModel: ChatModel @EnvironmentObject var theme: AppTheme - @State private var groupRelays: [GroupRelay] = [] + @ObservedObject private var channelRelaysModel = ChannelRelaysModel.shared + @State private var showAddRelay = false + + private var groupRelays: [GroupRelay] { + channelRelaysModel.groupId == groupInfo.groupId ? channelRelaysModel.groupRelays : [] + } var body: some View { List { relaysList() + // TODO [relays] re-enable when relay management ships + // if groupInfo.isOwner { + // Section { + // Button { + // showAddRelay = true + // } label: { + // Label("Add relay", systemImage: "plus") + // } + // } + // } } + // TODO [relays] re-enable when relay management ships + // .sheet(isPresented: $showAddRelay) { + // let existingRelayIds = Set(groupRelays.filter { $0.relayStatus != .rsInactive }.compactMap { $0.userChatRelay.chatRelayId }) + // AddGroupRelayView(groupInfo: groupInfo, existingRelayIds: existingRelayIds) { + // Task { await chatModel.loadGroupMembers(groupInfo) } + // } + // } .onAppear { Task { await chatModel.loadGroupMembers(groupInfo) if groupInfo.isOwner { - groupRelays = await apiGetGroupRelays(groupInfo.groupId) + let relays = await apiGetGroupRelays(groupInfo.groupId) + await MainActor.run { + ChannelRelaysModel.shared.set(groupId: groupInfo.groupId, groupRelays: relays) + } } } } } @ViewBuilder private func relaysList() -> some View { - let relayMembers = chatModel.groupMembers.filter { $0.wrapped.memberRole == .relay } + let relayMembers = chatModel.groupMembers.filter { $0.wrapped.memberRole == .relay && $0.wrapped.memberStatus != .memRemoved && $0.wrapped.memberStatus != .memGroupDeleted } if relayMembers.isEmpty { Section { Text("No chat relays") @@ -40,7 +65,7 @@ struct ChannelRelaysView: View { } else { Section { ForEach(relayMembers) { member in - NavigationLink { + let link = NavigationLink { GroupMemberInfoView( groupInfo: groupInfo, chat: chat, @@ -55,6 +80,20 @@ struct ChannelRelaysView: View { : subscriberRelayStatusText(member.wrapped) relayMemberRow(member.wrapped, statusText: statusText) } + // TODO [relays] re-enable when relay management ships + // if groupInfo.isOwner && member.wrapped.canBeRemoved(groupInfo: groupInfo) { + // link.swipeActions(edge: .trailing) { + // Button { + // showRemoveMemberAlert(groupInfo, member.wrapped) + // } label: { + // Label("Remove relay", systemImage: "trash") + // } + // .tint(.red) + // } + // } else { + // link + // } + link } } footer: { Text("Chat relays forward messages to channel subscribers.") diff --git a/apps/ios/Shared/Views/Chat/Group/GroupChatInfoView.swift b/apps/ios/Shared/Views/Chat/Group/GroupChatInfoView.swift index 21685fccd1..eee9500b3b 100644 --- a/apps/ios/Shared/Views/Chat/Group/GroupChatInfoView.swift +++ b/apps/ios/Shared/Views/Chat/Group/GroupChatInfoView.swift @@ -924,26 +924,54 @@ struct GroupChatInfoView: View { } func showRemoveMemberAlert(_ groupInfo: GroupInfo, _ mem: GroupMember, dismiss: DismissAction? = nil) { - showAlert( - groupInfo.useRelays - ? NSLocalizedString("Remove subscriber?", comment: "alert title") - : NSLocalizedString("Remove member?", comment: "alert title"), - message: - groupInfo.useRelays - ? NSLocalizedString("Subscriber will be removed from channel - this cannot be undone!", comment: "alert message") - : groupInfo.businessChat == nil - ? NSLocalizedString("Member will be removed from group - this cannot be undone!", comment: "alert message") - : NSLocalizedString("Member will be removed from chat - this cannot be undone!", comment: "alert message"), - actions: {[ - UIAlertAction(title: NSLocalizedString("Remove", comment: "alert action"), style: .destructive) { _ in - removeMember(groupInfo, mem, withMessages: false, dismiss: dismiss) - }, - UIAlertAction(title: NSLocalizedString("Remove and delete messages", comment: "alert action"), style: .destructive) { _ in - removeMember(groupInfo, mem, withMessages: true, dismiss: dismiss) - }, - cancelAlertAction - ]} - ) + if mem.memberRole == .relay { + let isLastActive = groupInfo.useRelays && mem.memberCurrent && { + let activeRelays = ChatModel.shared.groupMembers.filter { $0.wrapped.memberRole == .relay && $0.wrapped.memberCurrent } + return activeRelays.count <= 1 + }() + showAlert( + NSLocalizedString("Remove relay?", comment: "alert title"), + message: isLastActive + ? NSLocalizedString("This is the last active relay. Removing it will prevent message delivery to subscribers.", comment: "alert message") + : NSLocalizedString("Relay will be removed from channel - this cannot be undone!", comment: "alert message"), + actions: {[ + UIAlertAction(title: NSLocalizedString("Remove", comment: "alert action"), style: .destructive) { _ in + removeMember(groupInfo, mem, withMessages: false, dismiss: dismiss) + }, + cancelAlertAction + ]} + ) + } else if groupInfo.useRelays { + showAlert( + NSLocalizedString("Remove subscriber?", comment: "alert title"), + message: NSLocalizedString("Subscriber will be removed from channel - this cannot be undone!", comment: "alert message"), + actions: {[ + UIAlertAction(title: NSLocalizedString("Remove", comment: "alert action"), style: .destructive) { _ in + removeMember(groupInfo, mem, withMessages: false, dismiss: dismiss) + }, + UIAlertAction(title: NSLocalizedString("Remove and delete messages", comment: "alert action"), style: .destructive) { _ in + removeMember(groupInfo, mem, withMessages: true, dismiss: dismiss) + }, + cancelAlertAction + ]} + ) + } else { + showAlert( + NSLocalizedString("Remove member?", comment: "alert title"), + message: groupInfo.businessChat == nil + ? NSLocalizedString("Member will be removed from group - this cannot be undone!", comment: "alert message") + : NSLocalizedString("Member will be removed from chat - this cannot be undone!", comment: "alert message"), + actions: {[ + UIAlertAction(title: NSLocalizedString("Remove", comment: "alert action"), style: .destructive) { _ in + removeMember(groupInfo, mem, withMessages: false, dismiss: dismiss) + }, + UIAlertAction(title: NSLocalizedString("Remove and delete messages", comment: "alert action"), style: .destructive) { _ in + removeMember(groupInfo, mem, withMessages: true, dismiss: dismiss) + }, + cancelAlertAction + ]} + ) + } } func removeMember(_ groupInfo: GroupInfo, _ mem: GroupMember, withMessages: Bool, dismiss: DismissAction?) { diff --git a/apps/ios/Shared/Views/Chat/Group/GroupMemberInfoView.swift b/apps/ios/Shared/Views/Chat/Group/GroupMemberInfoView.swift index 4dff86f7bb..883a768d97 100644 --- a/apps/ios/Shared/Views/Chat/Group/GroupMemberInfoView.swift +++ b/apps/ios/Shared/Views/Chat/Group/GroupMemberInfoView.swift @@ -641,13 +641,12 @@ struct GroupMemberInfoView: View { blockForAllButton(mem) } } - // TODO [relays] removing relay should also remove its link from group link data; - // TODO - removing last relay should be prohibited or show warning + // TODO [relays] re-enable when relay management ships if canRemove && mem.memberRole != .relay { - if mem.memberStatus == .memRemoved || mem.memberStatus == .memLeft { - deleteMemberMessagesButton(mem) - } else { + if mem.memberStatus != .memRemoved && (mem.memberStatus != .memLeft || mem.memberRole == .relay) { removeMemberButton(mem) + } else if mem.memberRole != .relay { + deleteMemberMessagesButton(mem) } } } @@ -705,7 +704,10 @@ struct GroupMemberInfoView: View { Button(role: .destructive) { showRemoveMemberAlert(groupInfo, mem, dismiss: dismiss) } label: { - Label(groupInfo.useRelays ? "Remove subscriber" : "Remove member", systemImage: "trash") + let text = mem.memberRole == .relay ? "Remove relay" + : groupInfo.useRelays ? "Remove subscriber" + : "Remove member" + Label(text, systemImage: "trash") .foregroundColor(.red) } } diff --git a/apps/ios/Shared/Views/NewChat/AddChannelView.swift b/apps/ios/Shared/Views/NewChat/AddChannelView.swift index 477f7eef8e..32d6e7fe2c 100644 --- a/apps/ios/Shared/Views/NewChat/AddChannelView.swift +++ b/apps/ios/Shared/Views/NewChat/AddChannelView.swift @@ -338,6 +338,9 @@ struct AddChannelView: View { .compactSectionSpacing() Section { + Button("Cancel and delete channel", role: .destructive) { + showCancelChannelAlert(gInfo) + } Button("Continue") { if activeCount >= total { showLinkStep = true @@ -365,11 +368,6 @@ struct AddChannelView: View { } .navigationTitle("Creating channel") .navigationBarBackButtonHidden(true) - .toolbar { - ToolbarItem(placement: .navigationBarTrailing) { - Button("Delete channel") { showCancelChannelAlert(gInfo) } - } - } .onDisappear { if !showLinkStep && m.creatingChannelId == gInfo.id { showCancelChannelAlert(gInfo) @@ -481,7 +479,7 @@ func relayDisplayName(_ relay: GroupRelay) -> String { return "relay \(relay.groupRelayId)" } -private func chatRelayDisplayName(_ relay: UserChatRelay) -> String { +func chatRelayDisplayName(_ relay: UserChatRelay) -> String { if !relay.displayName.isEmpty { return relay.displayName } return relay.address } @@ -489,7 +487,7 @@ private func chatRelayDisplayName(_ relay: UserChatRelay) -> String { func relayStatusIndicator(_ status: RelayStatus, connFailed: Bool = false, memberStatus: GroupMemberStatus? = nil) -> some View { let removed = memberStatus.map { [.memLeft, .memRemoved, .memGroupDeleted].contains($0) } ?? false let color: Color = connFailed || removed ? .red : (status == .rsActive ? .green : .yellow) - let text: LocalizedStringKey = connFailed ? "failed" : memberStatus == .memLeft ? "removed by operator" : status.text + let text: LocalizedStringKey = connFailed ? "failed" : memberStatus == .memLeft ? "removed by operator" : removed ? "removed" : status.text return HStack(spacing: 4) { Circle() .fill(color) diff --git a/apps/ios/SimpleX.xcodeproj/project.pbxproj b/apps/ios/SimpleX.xcodeproj/project.pbxproj index 8c2e63bb59..a43f84f153 100644 --- a/apps/ios/SimpleX.xcodeproj/project.pbxproj +++ b/apps/ios/SimpleX.xcodeproj/project.pbxproj @@ -169,6 +169,7 @@ 648679AB2BC96A74006456E7 /* ChatItemForwardingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 648679AA2BC96A74006456E7 /* ChatItemForwardingView.swift */; }; 6495D7042F48CFC50060512B /* ChannelMembersView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6495D7032F48CFC50060512B /* ChannelMembersView.swift */; }; 6495D7062F48CFFD0060512B /* ChannelRelaysView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6495D7052F48CFFD0060512B /* ChannelRelaysView.swift */; }; + 6495D7082F48D0000060512B /* AddGroupRelayView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6495D7072F48D0000060512B /* AddGroupRelayView.swift */; }; 649BCDA0280460FD00C3A862 /* ComposeImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 649BCD9F280460FD00C3A862 /* ComposeImageView.swift */; }; 649BCDA22805D6EF00C3A862 /* CIImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 649BCDA12805D6EF00C3A862 /* CIImageView.swift */; }; 64A779F62DBFB9F200FDEF2F /* MemberAdmissionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64A779F52DBFB9F200FDEF2F /* MemberAdmissionView.swift */; }; @@ -546,6 +547,7 @@ 6493D667280ED77F007A76FB /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = ""; }; 6495D7032F48CFC50060512B /* ChannelMembersView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelMembersView.swift; sourceTree = ""; }; 6495D7052F48CFFD0060512B /* ChannelRelaysView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelRelaysView.swift; sourceTree = ""; }; + 6495D7072F48D0000060512B /* AddGroupRelayView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddGroupRelayView.swift; sourceTree = ""; }; 649BCD9F280460FD00C3A862 /* ComposeImageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeImageView.swift; sourceTree = ""; }; 649BCDA12805D6EF00C3A862 /* CIImageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CIImageView.swift; sourceTree = ""; }; 64A779F52DBFB9F200FDEF2F /* MemberAdmissionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemberAdmissionView.swift; sourceTree = ""; }; @@ -1173,6 +1175,7 @@ 64A779FD2DC3AFF200FDEF2F /* MemberSupportChatToolbar.swift */, 6495D7032F48CFC50060512B /* ChannelMembersView.swift */, 6495D7052F48CFFD0060512B /* ChannelRelaysView.swift */, + 6495D7072F48D0000060512B /* AddGroupRelayView.swift */, ); path = Group; sourceTree = ""; @@ -1633,6 +1636,7 @@ 8C9BC2652C240D5200875A27 /* ThemeModeEditor.swift in Sources */, 647B15E82F4C8D2500EB431E /* AddChannelView.swift in Sources */, 6495D7062F48CFFD0060512B /* ChannelRelaysView.swift in Sources */, + 6495D7082F48D0000060512B /* AddGroupRelayView.swift in Sources */, 5CB346E92869E8BA001FD2EF /* PushEnvironment.swift in Sources */, 5C55A91F283AD0E400C4E99E /* CallManager.swift in Sources */, 649BCDA22805D6EF00C3A862 /* CIImageView.swift in Sources */, diff --git a/apps/ios/SimpleXChat/ChatTypes.swift b/apps/ios/SimpleXChat/ChatTypes.swift index 1dfa477c91..a5a35ba5c0 100644 --- a/apps/ios/SimpleXChat/ChatTypes.swift +++ b/apps/ios/SimpleXChat/ChatTypes.swift @@ -4091,6 +4091,7 @@ public enum CIDeleteMode: String, Decodable, Hashable { case cidmBroadcast = "broadcast" case cidmInternal = "internal" case cidmInternalMark = "internalMark" + case cidmHistory = "history" } protocol ItemContent { diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt index a745542602..1b1d403521 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt @@ -92,6 +92,7 @@ object ChannelRelaysModel { if (groupId.value == groupInfo.groupId) { val i = groupRelays.indexOfFirst { it.groupRelayId == relay.groupRelayId } if (i >= 0) groupRelays[i] = relay + else groupRelays.add(relay) } } @@ -3734,7 +3735,8 @@ sealed class CIForwardedFrom { enum class CIDeleteMode(val deleteMode: String) { @SerialName("internal") cidmInternal("internal"), @SerialName("internalMark") cidmInternalMark("internalMark"), - @SerialName("broadcast") cidmBroadcast("broadcast"); + @SerialName("broadcast") cidmBroadcast("broadcast"), + @SerialName("history") cidmHistory("history"); } interface ItemContent { diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt index 4d396c117e..e23b76b025 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt @@ -91,6 +91,13 @@ enum class SimplexLinkMode { } } +enum class CloseBehavior { + Ask, Quit, MinimizeToTray; + companion object { val default = Ask } +} + +class HintPref(val reset: () -> Unit, val isUnchanged: () -> Boolean) + // Spec: spec/state.md#AppPreferences class AppPreferences { // deprecated, remove in 2024 @@ -99,6 +106,7 @@ class AppPreferences { SHARED_PREFS_NOTIFICATIONS_MODE, if (!runServiceInBackground.get()) NotificationsMode.OFF else NotificationsMode.default ) { NotificationsMode.values().firstOrNull { it.name == this } } + val closeBehavior: SharedPreference = mkSafeEnumPreference(SHARED_PREFS_DESKTOP_CLOSE_BEHAVIOR, CloseBehavior.default) val notificationPreviewMode = mkStrPreference(SHARED_PREFS_NOTIFICATION_PREVIEW_MODE, NotificationPreviewMode.default.name) val canAskToEnableNotifications = mkBoolPreference(SHARED_PREFS_CAN_ASK_TO_ENABLE_NOTIFICATIONS, true) val backgroundServiceNoticeShown = mkBoolPreference(SHARED_PREFS_SERVICE_NOTICE_SHOWN, false) @@ -257,17 +265,23 @@ class AppPreferences { val oneHandUI = mkBoolPreference(SHARED_PREFS_ONE_HAND_UI, true) val chatBottomBar = mkBoolPreference(SHARED_PREFS_CHAT_BOTTOM_BAR, true) - val hintPreferences: List, Boolean>> = listOf( - laNoticeShown to false, - oneHandUICardShown to false, - addressCreationCardShown to false, - liveMessageAlertShown to false, - showHiddenProfilesNotice to true, - showMuteProfileAlert to true, - showReportsInSupportChatAlert to true, - showDeleteConversationNotice to true, - showDeleteContactNotice to true, - privacyLinkPreviewsShowAlert to true, + val hintPreferences: List = listOf( + hintPref(laNoticeShown, false), + hintPref(oneHandUICardShown, false), + hintPref(addressCreationCardShown, false), + hintPref(liveMessageAlertShown, false), + hintPref(showHiddenProfilesNotice, true), + hintPref(showMuteProfileAlert, true), + hintPref(showReportsInSupportChatAlert, true), + hintPref(showDeleteConversationNotice, true), + hintPref(showDeleteContactNotice, true), + hintPref(privacyLinkPreviewsShowAlert, true), + hintPref(closeBehavior, CloseBehavior.default), + ) + + private fun hintPref(pref: SharedPreference, default: T) = HintPref( + reset = { pref.set(default) }, + isUnchanged = { pref.state.value == default }, ) private fun mkIntPreference(prefName: String, default: Int) = @@ -479,6 +493,7 @@ class AppPreferences { private const val SHARED_PREFS_CONNECT_REMOTE_VIA_MULTICAST_AUTO = "ConnectRemoteViaMulticastAuto" private const val SHARED_PREFS_OFFER_REMOTE_MULTICAST = "OfferRemoteMulticast" private const val SHARED_PREFS_DESKTOP_WINDOW_STATE = "DesktopWindowState" + private const val SHARED_PREFS_DESKTOP_CLOSE_BEHAVIOR = "DesktopCloseBehavior" private const val SHARED_PREFS_SHOW_DELETE_CONVERSATION_NOTICE = "showDeleteConversationNotice" private const val SHARED_PREFS_SHOW_DELETE_CONTACT_NOTICE = "showDeleteContactNotice" private const val SHARED_PREFS_SHOW_SENT_VIA_RPOXY = "showSentViaProxy" @@ -1198,14 +1213,14 @@ object ChatController { suspend fun apiDeleteChatItems(rh: Long?, type: ChatType, id: Long, scope: GroupChatScope?, itemIds: List, mode: CIDeleteMode): List? { val r = sendCmd(rh, CC.ApiDeleteChatItem(type, id, scope, itemIds, mode)) if (r is API.Result && r.res is CR.ChatItemsDeleted) return r.res.chatItemDeletions - Log.e(TAG, "apiDeleteChatItem bad response: ${r.responseType} ${r.details}") + apiErrorAlert("apiDeleteChatItems", generalGetString(MR.strings.error_deleting_message), r) return null } suspend fun apiDeleteMemberChatItems(rh: Long?, groupId: Long, itemIds: List): List? { val r = sendCmd(rh, CC.ApiDeleteMemberChatItem(groupId, itemIds)) if (r is API.Result && r.res is CR.ChatItemsDeleted) return r.res.chatItemDeletions - Log.e(TAG, "apiDeleteMemberChatItem bad response: ${r.responseType} ${r.details}") + apiErrorAlert("apiDeleteMemberChatItems", generalGetString(MR.strings.error_deleting_message), r) return null } @@ -2163,6 +2178,19 @@ object ChatController { return emptyList() } + sealed class AddGroupRelaysResult { + data class Added(val groupInfo: GroupInfo, val groupLink: GroupLink, val groupRelays: List): AddGroupRelaysResult() + data class AddFailed(val addRelayResults: List): AddGroupRelaysResult() + } + + suspend fun apiAddGroupRelays(groupId: Long, relayIds: List): AddGroupRelaysResult? { + val r = sendCmdWithRetry(null, CC.ApiAddGroupRelays(groupId, relayIds)) + if (r is API.Result && r.res is CR.GroupRelaysAdded) return AddGroupRelaysResult.Added(r.res.groupInfo, r.res.groupLink, r.res.groupRelays) + if (r is API.Result && r.res is CR.GroupRelaysAddFailed) return AddGroupRelaysResult.AddFailed(r.res.addRelayResults) + if (r != null) throw Exception("${r.responseType}: ${r.details}") + return null + } + suspend fun apiAddMember(rh: Long?, groupId: Long, contactId: Long, memberRole: GroupMemberRole): GroupMember? { val r = sendCmd(rh, CC.ApiAddMember(groupId, contactId, memberRole)) if (r is API.Result && r.res is CR.SentGroupInvitation) return r.res.member @@ -3666,6 +3694,7 @@ sealed class CC { class ApiNewGroup(val userId: Long, val incognito: Boolean, val groupProfile: GroupProfile): CC() class ApiNewPublicGroup(val userId: Long, val incognito: Boolean, val relayIds: List, val groupProfile: GroupProfile): CC() class ApiGetGroupRelays(val groupId: Long): CC() + class ApiAddGroupRelays(val groupId: Long, val relayIds: List): CC() class ApiAddMember(val groupId: Long, val contactId: Long, val memberRole: GroupMemberRole): CC() class ApiJoinGroup(val groupId: Long): CC() class ApiAcceptMember(val groupId: Long, val groupMemberId: Long, val memberRole: GroupMemberRole): CC() @@ -3870,6 +3899,7 @@ sealed class CC { is ApiNewGroup -> "/_group $userId incognito=${onOff(incognito)} ${json.encodeToString(groupProfile)}" is ApiNewPublicGroup -> "/_public group $userId incognito=${onOff(incognito)} ${relayIds.joinToString(",")} ${json.encodeToString(groupProfile)}" is ApiGetGroupRelays -> "/_get relays #$groupId" + is ApiAddGroupRelays -> "/_add relays #$groupId ${relayIds.joinToString(",")}" is ApiAddMember -> "/_add #$groupId $contactId ${memberRole.memberRole}" is ApiJoinGroup -> "/_join #$groupId" is ApiAcceptMember -> "/_accept member #$groupId $groupMemberId ${memberRole.memberRole}" @@ -4053,6 +4083,7 @@ sealed class CC { is ApiNewGroup -> "apiNewGroup" is ApiNewPublicGroup -> "apiNewPublicGroup" is ApiGetGroupRelays -> "apiGetGroupRelays" + is ApiAddGroupRelays -> "apiAddGroupRelays" is ApiAddMember -> "apiAddMember" is ApiJoinGroup -> "apiJoinGroup" is ApiAcceptMember -> "apiAcceptMember" @@ -6402,6 +6433,8 @@ sealed class CR { @Serializable @SerialName("publicGroupCreated") class PublicGroupCreated(val user: UserRef, val groupInfo: GroupInfo, val groupLink: GroupLink, val groupRelays: List): CR() @Serializable @SerialName("publicGroupCreationFailed") class PublicGroupCreationFailed(val user: UserRef, val addRelayResults: List): CR() @Serializable @SerialName("groupRelays") class GroupRelays(val user: UserRef, val groupInfo: GroupInfo, val groupRelays: List): CR() + @Serializable @SerialName("groupRelaysAdded") class GroupRelaysAdded(val user: UserRef, val groupInfo: GroupInfo, val groupLink: GroupLink, val groupRelays: List): CR() + @Serializable @SerialName("groupRelaysAddFailed") class GroupRelaysAddFailed(val user: UserRef, val addRelayResults: List): CR() @Serializable @SerialName("sentGroupInvitation") class SentGroupInvitation(val user: UserRef, val groupInfo: GroupInfo, val contact: Contact, val member: GroupMember): CR() @Serializable @SerialName("userAcceptedGroupSent") class UserAcceptedGroupSent (val user: UserRef, val groupInfo: GroupInfo, val hostContact: Contact? = null): CR() @Serializable @SerialName("groupLinkConnecting") class GroupLinkConnecting (val user: UserRef, val groupInfo: GroupInfo, val hostMember: GroupMember): CR() @@ -6591,6 +6624,8 @@ sealed class CR { is PublicGroupCreated -> "publicGroupCreated" is PublicGroupCreationFailed -> "publicGroupCreationFailed" is GroupRelays -> "groupRelays" + is GroupRelaysAdded -> "groupRelaysAdded" + is GroupRelaysAddFailed -> "groupRelaysAddFailed" is SentGroupInvitation -> "sentGroupInvitation" is UserAcceptedGroupSent -> "userAcceptedGroupSent" is GroupLinkConnecting -> "groupLinkConnecting" @@ -6773,6 +6808,8 @@ sealed class CR { is PublicGroupCreated -> withUser(user, "groupInfo: $groupInfo\ngroupLink: $groupLink\ngroupRelays: $groupRelays") is PublicGroupCreationFailed -> withUser(user, "addRelayResults: $addRelayResults") is GroupRelays -> withUser(user, "groupInfo: $groupInfo\ngroupRelays: $groupRelays") + is GroupRelaysAdded -> withUser(user, "groupInfo: $groupInfo\ngroupLink: $groupLink\ngroupRelays: $groupRelays") + is GroupRelaysAddFailed -> withUser(user, "addRelayResults: $addRelayResults") is SentGroupInvitation -> withUser(user, "groupInfo: $groupInfo\ncontact: $contact\nmember: $member") is UserAcceptedGroupSent -> json.encodeToString(groupInfo) is GroupLinkConnecting -> withUser(user, "groupInfo: $groupInfo\nhostMember: $hostMember") diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt index 114edeee3d..f42969a73f 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt @@ -211,7 +211,7 @@ fun ChatView( withContext(Dispatchers.Main) { ChannelRelaysModel.set(cInfo.groupInfo.groupId, relays) } - } else { + } else if (cInfo.groupInfo.membership.memberCurrent) { val gInfo = chatModel.controller.apiGetUpdatedGroupLinkData(chatRh, cInfo.groupInfo.groupId) if (gInfo != null) { withContext(Dispatchers.Main) { @@ -317,6 +317,7 @@ fun ChatView( itemIds.sorted(), questionText = questionText, forAll = canDeleteForAll, + editorial = publicGroupEditor(chatInfo), deleteMessages = { ids, forAll -> deleteMessages(chatRh, chatInfo, ids, forAll, moderate = false) { selectedChatItems.value = null @@ -3351,7 +3352,9 @@ private fun deleteMessages(chatRh: Long?, chatInfo: ChatInfo, itemIds: List +fun publicGroupEditor(chatInfo: ChatInfo): Boolean = + chatInfo is ChatInfo.Group && chatInfo.groupInfo.useRelays && chatInfo.groupInfo.membership.memberRole >= GroupMemberRole.Moderator + private fun keyForItem(item: ChatItem): ChatViewItemKey = ChatViewItemKey(item.id, item.meta.createdAt.toEpochMilliseconds()) private fun ViewConfiguration.bigTouchSlop(slop: Float = 50f) = object: ViewConfiguration { diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ComposeView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ComposeView.kt index 51128646e7..d0782f6bb4 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ComposeView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ComposeView.kt @@ -1543,14 +1543,14 @@ fun ComposeView( ) { if (gInfo.membership.memberRole == GroupMemberRole.Owner) { ownerRelayState?.let { s -> - if (s.activeCount < s.relays.size) { + if (s.relays.isEmpty() || s.activeCount < s.relays.size) { OwnerChannelRelayBar(chatModel, s.relays, s.activeCount, s.failedCount, s.removedCount, relayListExpanded) } } } else { val hostnames = (chatModel.channelRelayHostnames[gInfo.groupId] ?: emptyList()).sorted() val relayMembers = chatModel.groupMembers.value - .filter { it.memberRole == GroupMemberRole.Relay } + .filter { it.memberRole == GroupMemberRole.Relay && it.memberStatus !in listOf(GroupMemberStatus.MemRemoved, GroupMemberStatus.MemGroupDeleted) } .sortedBy { hostFromRelayLink(it.relayLink ?: "") } val showProgress = !gInfo.nextConnectPrepared || composeState.value.inProgress val removedCount = relayMembers.count { relayMemberRemoved(it.memberStatus) } @@ -1558,7 +1558,7 @@ fun ComposeView( val failedCount = relayMembers.count { !relayMemberRemoved(it.memberStatus) && it.activeConn?.connFailedErr != null } val resolvedCount = connectedCount + removedCount + failedCount val total = if (relayMembers.isNotEmpty()) relayMembers.size else hostnames.size - if (total > 0 && (removedCount + failedCount > 0 || resolvedCount < total)) { + if (total == 0 || removedCount + failedCount > 0 || resolvedCount < total) { SubscriberChannelRelayBar(hostnames, relayMembers, connectedCount, removedCount, failedCount, total, showProgress, relayListExpanded) } } @@ -1756,7 +1756,15 @@ private fun OwnerChannelRelayBar( if (!allBroken && activeCount + failedCount + removedCount < total) { RelayProgressIndicator(active = activeCount, total = total) } - if (allBroken) { + if (total == 0) { + Text(generalGetString(MR.strings.relay_bar_no_relays), color = MaterialTheme.colors.secondary) + Icon( + painterResource(MR.images.ic_warning), + contentDescription = null, + tint = WarningOrange, + modifier = Modifier.size(18.dp) + ) + } else if (allBroken) { val statusText = if (removedCount == total) { generalGetString(MR.strings.relay_bar_all_relays_removed) } else if (failedCount == total) { @@ -1842,7 +1850,15 @@ private fun SubscriberChannelRelayBar( val allBroken = connectedCount == 0 && errorCount == total Column(Modifier.background(MaterialTheme.colors.surface)) { RelayBarHeader(relayListExpanded) { - if (allBroken) { + if (total == 0) { + Text(generalGetString(MR.strings.relay_bar_no_relays), color = MaterialTheme.colors.secondary) + Icon( + painterResource(MR.images.ic_warning), + contentDescription = null, + tint = WarningOrange, + modifier = Modifier.size(18.dp) + ) + } else if (allBroken) { val statusText = if (removedCount == total) { generalGetString(MR.strings.relay_bar_all_relays_removed) } else if (failedCount == total) { @@ -1990,7 +2006,7 @@ private fun ownerRelayState(chat: Chat, chatModel: ChatModel): OwnerRelayState? gInfo.membership.memberStatus in listOf(GroupMemberStatus.MemLeft, GroupMemberStatus.MemRemoved, GroupMemberStatus.MemGroupDeleted) ) return null val relays = if (ChannelRelaysModel.groupId.value == gInfo.groupId) ChannelRelaysModel.groupRelays.toList() else emptyList() - if (relays.isEmpty()) return null + if (relays.isEmpty()) return OwnerRelayState(emptyList(), 0, 0, 0, true) val relayMembers = relays.map { relay -> relay to chatModel.groupMembers.value.firstOrNull { it.groupMemberId == relay.groupMemberId } } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/TextSelection.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/TextSelection.kt index dee39dde7c..d85488cefc 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/TextSelection.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/TextSelection.kt @@ -36,6 +36,7 @@ import androidx.compose.ui.unit.IntSize import androidx.compose.ui.unit.dp import chat.simplex.common.model.* import chat.simplex.common.platform.* +import chat.simplex.common.views.chat.item.itemPrefixText import chat.simplex.common.views.chat.item.itemSegmentDisplayText import chat.simplex.common.views.helpers.generalGetString import chat.simplex.res.MR @@ -52,8 +53,10 @@ val LocalItemContext = compositionLocalOf { ItemContext() } data class SelectionRange( val startIndex: Int, + val startItemId: Long, val startOffset: Int, val endIndex: Int, + val endItemId: Long, val endOffset: Int ) @@ -79,11 +82,13 @@ class SelectionManager { var viewportPosition by mutableStateOf(Offset.Zero) var focusCharRect by mutableStateOf(Rect.Zero) // X: absolute window, Y: relative to item var listState: State? = null + var mergedItemsState: State? = null var onCopySelection: (() -> Unit)? = null private var autoScrollJob: Job? = null fun startSelection(startIndex: Int, anchorY: Float, anchorX: Float) { - range = SelectionRange(startIndex, -1, startIndex, -1) + val id = mergedItemsState?.value?.items?.getOrNull(startIndex)?.newest()?.item?.id ?: return + range = SelectionRange(startIndex, id, -1, startIndex, id, -1) selectionState = SelectionState.Selecting anchorWindowY = anchorY anchorWindowX = anchorX @@ -96,7 +101,8 @@ class SelectionManager { fun updateFocusIndex(index: Int) { val r = range ?: return - range = r.copy(endIndex = index) + val id = mergedItemsState?.value?.items?.getOrNull(index)?.newest()?.item?.id ?: return + range = r.copy(endIndex = index, endItemId = id) } fun updateFocusOffset(offset: Int, charRect: Rect = Rect.Zero) { @@ -175,6 +181,15 @@ class SelectionManager { updateFocusIndex(idx) } + fun resyncIndices() { + val r = range ?: return + val items = mergedItemsState?.value?.items ?: return + val newStartIndex = items.indexOfFirst { it.newest().item.id == r.startItemId } + val newEndIndex = items.indexOfFirst { it.newest().item.id == r.endItemId } + if (newStartIndex < 0 || newEndIndex < 0) clearSelection() + else range = r.copy(startIndex = newStartIndex, endIndex = newEndIndex) + } + fun updateAutoScroll(draggingDown: Boolean, pointerY: Float, scope: CoroutineScope) { val edgeDistance = if (draggingDown) viewportBottom - pointerY else pointerY - viewportTop if (edgeDistance !in 0f..AUTO_SCROLL_ZONE_PX) { @@ -240,15 +255,22 @@ fun selectedRange(range: SelectionRange?, index: Int): IntRange? { } // Extracts source text for the selected range within one item. -// Selection offsets are in display-text space. For transformed segments (mentions, links with showText), -// the full source is emitted if any part is selected. For untransformed segments, partial substring works. +// Selection offsets are in display-text space (which includes any leading itemPrefixText). +// For transformed segments (mentions, links with showText), the full source is emitted if any part +// is selected. For untransformed segments, partial substring works. private fun selectedItemCopiedText(ci: ChatItem, sel: IntRange, linkMode: SimplexLinkMode): String { - val formattedText = ci.formattedText ?: return ci.text.substring( - sel.first.coerceAtMost(ci.text.length), - (sel.last + 1).coerceAtMost(ci.text.length) - ) + val prefix = itemPrefixText(ci) val sb = StringBuilder() - var displayOffset = 0 + if (sel.first < prefix.length) { + sb.append(prefix, sel.first, minOf(prefix.length, sel.last + 1)) + } + val formattedText = ci.formattedText ?: run { + val start = (sel.first - prefix.length).coerceAtLeast(0).coerceAtMost(ci.text.length) + val end = (sel.last + 1 - prefix.length).coerceAtMost(ci.text.length) + if (start < end) sb.append(ci.text, start, end) + return sb.toString() + } + var displayOffset = prefix.length for (ft in formattedText) { val segDisplay = itemSegmentDisplayText(ft, ci, linkMode) val displayEnd = displayOffset + segDisplay.length @@ -269,7 +291,7 @@ private fun selectedItemCopiedText(ci: ChatItem, sel: IntRange, linkMode: Simple // Snaps a boundary offset to include full transformed segments. private fun snapOffset(ci: ChatItem, offset: Int, linkMode: SimplexLinkMode, expandRight: Boolean): Int { val formattedText = ci.formattedText ?: return offset - var displayOffset = 0 + var displayOffset = itemPrefixText(ci).length for (ft in formattedText) { val segDisplay = itemSegmentDisplayText(ft, ci, linkMode) val displayEnd = displayOffset + segDisplay.length @@ -312,11 +334,15 @@ fun BoxScope.SelectionHandler( } manager.listState = listState + manager.mergedItemsState = mergedItems manager.onCopySelection = { clipboard.setText(AnnotatedString(manager.getSelectedCopiedText(mergedItems.value.items, revealedItems.value, linkMode))) showToast(generalGetString(MR.strings.copied)) } + // Resync after the items list mutates (new message arrives, item deleted). + SideEffect { manager.resyncIndices() } + return Modifier .focusRequester(focusRequester) .focusable() diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/AddGroupRelayView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/AddGroupRelayView.kt new file mode 100644 index 0000000000..d0c2486069 --- /dev/null +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/AddGroupRelayView.kt @@ -0,0 +1,237 @@ +package chat.simplex.common.views.chat.group + +import SectionBottomSpacer +import SectionCustomFooter +import SectionDividerSpaced +import SectionItemView +import SectionView +import androidx.compose.foundation.layout.* +import androidx.compose.material.* +import androidx.compose.runtime.* +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import chat.simplex.common.model.* +import chat.simplex.common.platform.* +import chat.simplex.common.ui.theme.* +import chat.simplex.common.views.helpers.* +import chat.simplex.common.views.newchat.chatRelayDisplayName +import chat.simplex.common.views.usersettings.SettingsActionItem +import chat.simplex.res.MR +import dev.icerock.moko.resources.compose.painterResource +import kotlinx.coroutines.launch + +data class AvailableRelay( + val relayId: Long, + val relay: UserChatRelay, + val operatorName: String? +) + +@Composable +fun AddGroupRelayView( + groupInfo: GroupInfo, + existingRelayIds: Set, + onRelayAdded: () -> Unit, + close: () -> Unit +) { + var availableRelays by remember { mutableStateOf>(emptyList()) } + var selectedRelayIds by remember { mutableStateOf>(emptySet()) } + var isLoading by remember { mutableStateOf(true) } + var isAdding by remember { mutableStateOf(false) } + val scope = rememberCoroutineScope() + + BackHandler(onBack = close) + + LaunchedEffect(Unit) { + try { + val servers = ChatController.getUserServers(null) + if (servers != null) { + val relays = mutableListOf() + for (op in servers) { + if (op.operator != null && op.operator.enabled != true) continue + val opName: String? = if (op.operator?.operatorTag != null) op.operator.tradeName else null + for (relay in op.chatRelays) { + val relayId = relay.chatRelayId + if (relay.enabled && !relay.deleted && relayId != null && relayId !in existingRelayIds) { + relays.add(AvailableRelay(relayId, relay, opName)) + } + } + } + availableRelays = relays + } + } catch (e: Exception) { + Log.e(TAG, "loadAvailableRelays error: ${e.message}") + } + isLoading = false + } + + AddGroupRelayLayout( + availableRelays = availableRelays, + selectedRelayIds = selectedRelayIds, + isLoading = isLoading, + isAdding = isAdding, + onToggleRelay = { relayId -> + selectedRelayIds = if (relayId in selectedRelayIds) selectedRelayIds - relayId else selectedRelayIds + relayId + }, + onAddRelays = { + val relayIds = selectedRelayIds.toList() + if (relayIds.isEmpty()) return@AddGroupRelayLayout + isAdding = true + scope.launch { + addSelectedRelays(groupInfo, relayIds, selectedRelayIds, availableRelays, onRelayAdded, close) { newSelectedIds, newAvailableRelays -> + selectedRelayIds = newSelectedIds + availableRelays = newAvailableRelays + isAdding = false + } + } + } + ) +} + +@Composable +private fun AddGroupRelayLayout( + availableRelays: List, + selectedRelayIds: Set, + isLoading: Boolean, + isAdding: Boolean, + onToggleRelay: (Long) -> Unit, + onAddRelays: () -> Unit +) { + ColumnWithScrollBar { + AppBarTitle(generalGetString(MR.strings.add_relays_title)) + + if (isLoading) { + Box(Modifier.fillMaxWidth().padding(vertical = DEFAULT_PADDING), contentAlignment = Alignment.Center) { + CircularProgressIndicator() + } + } else if (availableRelays.isEmpty()) { + SectionView { + SectionItemView(padding = PaddingValues(horizontal = DEFAULT_PADDING)) { + Text( + generalGetString(MR.strings.no_available_relays), + color = MaterialTheme.colors.secondary + ) + } + } + } else { + SectionView { + AddRelaysButton( + onClick = onAddRelays, + disabled = selectedRelayIds.isEmpty() || isAdding + ) + } + SectionCustomFooter { + val count = selectedRelayIds.size + Text( + if (count == 0) generalGetString(MR.strings.no_relays_selected) + else String.format(generalGetString(MR.strings.num_relays_selected), count), + color = MaterialTheme.colors.secondary, + lineHeight = 18.sp, + fontSize = 14.sp + ) + } + SectionDividerSpaced(maxTopPadding = true) + SectionView(generalGetString(MR.strings.select_relays).uppercase()) { + availableRelays.forEach { item -> + val selected = item.relayId in selectedRelayIds + SectionItemView( + click = { onToggleRelay(item.relayId) }, + padding = PaddingValues(horizontal = DEFAULT_PADDING, vertical = 4.dp) + ) { + Column(Modifier.weight(1f)) { + Text( + chatRelayDisplayName(item.relay), + maxLines = 1, + color = MaterialTheme.colors.onBackground + ) + if (item.operatorName != null) { + Text( + item.operatorName, + fontSize = 12.sp, + maxLines = 1, + color = MaterialTheme.colors.secondary + ) + } + } + Spacer(Modifier.width(8.dp)) + Icon( + painterResource(if (selected) MR.images.ic_check_circle_filled else MR.images.ic_circle), + contentDescription = null, + tint = if (selected) MaterialTheme.colors.primary else MaterialTheme.colors.secondary, + modifier = Modifier.size(24.dp) + ) + } + } + } + } + SectionBottomSpacer() + } +} + +@Composable +private fun AddRelaysButton(onClick: () -> Unit, disabled: Boolean) { + SettingsActionItem( + painterResource(MR.images.ic_check), + generalGetString(MR.strings.add_relays_title), + click = onClick, + textColor = MaterialTheme.colors.primary, + iconColor = MaterialTheme.colors.primary, + disabled = disabled, + ) +} + +private suspend fun addSelectedRelays( + groupInfo: GroupInfo, + relayIds: List, + selectedRelayIds: Set, + availableRelays: List, + onRelayAdded: () -> Unit, + close: () -> Unit, + updateState: (Set, List) -> Unit +) { + try { + val result = ChatController.apiAddGroupRelays(groupInfo.groupId, relayIds) + if (result == null) { + updateState(selectedRelayIds, availableRelays) + return + } + when (result) { + is ChatController.AddGroupRelaysResult.Added -> { + ChannelRelaysModel.set(groupId = result.groupInfo.groupId, groupRelays = result.groupRelays) + onRelayAdded() + close() + } + is ChatController.AddGroupRelaysResult.AddFailed -> { + val results = result.addRelayResults + val successIds = results.filter { it.relayError == null }.mapNotNull { it.relay.chatRelayId }.toSet() + var newSelectedIds = selectedRelayIds + var newAvailableRelays = availableRelays + if (successIds.isNotEmpty()) { + newSelectedIds = selectedRelayIds - successIds + newAvailableRelays = availableRelays.filter { it.relayId !in successIds } + onRelayAdded() + } + val errorLines = results.filter { it.relayError != null } + .map { "${chatRelayDisplayName(it.relay)}: ${it.relayError?.let { e -> ChatController.connErrorText(e) } ?: ""}" } + val successNames = results.filter { it.relayError == null } + .map { chatRelayDisplayName(it.relay) } + var msg = errorLines.joinToString("\n") + if (successNames.isNotEmpty()) { + msg += "\n" + String.format(generalGetString(MR.strings.relays_added_format), successNames.joinToString(", ")) + } + AlertManager.shared.showAlertMsg( + title = generalGetString(MR.strings.error_adding_relays), + text = msg + ) + updateState(newSelectedIds, newAvailableRelays) + } + } + } catch (e: Exception) { + AlertManager.shared.showAlertMsg( + title = generalGetString(MR.strings.error_adding_relays), + text = e.message ?: "" + ) + updateState(selectedRelayIds, availableRelays) + } +} diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/ChannelRelaysView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/ChannelRelaysView.kt index 39ebe5afd8..891753aed8 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/ChannelRelaysView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/ChannelRelaysView.kt @@ -2,6 +2,7 @@ package chat.simplex.common.views.chat.group import SectionBottomSpacer import SectionItemView +import SectionItemViewLongClickable import SectionTextFooter import SectionView import androidx.compose.foundation.layout.* @@ -16,9 +17,11 @@ import androidx.compose.ui.unit.sp import chat.simplex.common.model.* import chat.simplex.common.platform.* import chat.simplex.common.ui.theme.* +import chat.simplex.common.views.chat.item.ItemAction import chat.simplex.common.views.chatlist.setGroupMembers import chat.simplex.common.views.helpers.* import chat.simplex.res.MR +import dev.icerock.moko.resources.compose.painterResource @Composable fun ChannelRelaysView( @@ -29,16 +32,18 @@ fun ChannelRelaysView( showMemberInfo: (GroupMember, GroupRelay?) -> Unit ) { BackHandler(onBack = close) - var groupRelays by remember { mutableStateOf>(emptyList()) } + val groupRelays = ChannelRelaysModel.groupRelays LaunchedEffect(Unit) { setGroupMembers(rhId, groupInfo, chatModel) if (groupInfo.isOwner) { - groupRelays = chatModel.controller.apiGetGroupRelays(groupInfo.groupId) + val relays = chatModel.controller.apiGetGroupRelays(groupInfo.groupId) + ChannelRelaysModel.set(groupId = groupInfo.groupId, groupRelays = relays) } } ChannelRelaysLayout( + rhId = rhId, groupInfo = groupInfo, chatModel = chatModel, groupRelays = groupRelays, @@ -48,13 +53,14 @@ fun ChannelRelaysView( @Composable private fun ChannelRelaysLayout( + rhId: Long?, groupInfo: GroupInfo, chatModel: ChatModel, groupRelays: List, showMemberInfo: (GroupMember, GroupRelay?) -> Unit ) { val relayMembers = remember { chatModel.groupMembers }.value - .filter { it.memberRole == GroupMemberRole.Relay } + .filter { it.memberRole == GroupMemberRole.Relay && it.memberStatus != GroupMemberStatus.MemRemoved && it.memberStatus != GroupMemberStatus.MemGroupDeleted } ColumnWithScrollBar { AppBarTitle(generalGetString(MR.strings.channel_relays_title)) @@ -74,11 +80,24 @@ private fun ChannelRelaysLayout( if (index > 0) { Divider() } - SectionItemView( + val showMenu = remember { mutableStateOf(false) } + SectionItemViewLongClickable( click = { showMemberInfo(member, groupRelays.firstOrNull { it.groupMemberId == member.groupMemberId }) }, + longClick = { showMenu.value = true }, minHeight = 54.dp, padding = PaddingValues(horizontal = DEFAULT_PADDING) ) { + // TODO [relays] re-enable when relay management ships + /* + if (groupInfo.isOwner && member.canBeRemoved(groupInfo)) { + DefaultDropdownMenu(showMenu) { + ItemAction(generalGetString(MR.strings.button_remove_relay), painterResource(MR.images.ic_delete), color = MaterialTheme.colors.error, onClick = { + removeMemberAlert(rhId, groupInfo, member) + showMenu.value = false + }) + } + } + */ val statusText = if (groupInfo.isOwner) { ownerRelayStatusText(member, groupRelays) } else { @@ -90,6 +109,35 @@ private fun ChannelRelaysLayout( } SectionTextFooter(generalGetString(MR.strings.chat_relays_forward_messages)) } + // TODO [relays] re-enable when relay management ships + /* + if (groupInfo.isOwner) { + SectionView { + SectionItemView(click = { + val existingRelayIds = groupRelays.filter { it.relayStatus != RelayStatus.RsInactive }.mapNotNull { it.userChatRelay.chatRelayId }.toSet() + ModalManager.end.showModalCloseable(true) { close -> + AddGroupRelayView( + groupInfo = groupInfo, + existingRelayIds = existingRelayIds, + onRelayAdded = { withBGApi { setGroupMembers(rhId, groupInfo, chatModel) } }, + close = close + ) + } + }, padding = PaddingValues(horizontal = DEFAULT_PADDING)) { + Icon( + painterResource(MR.images.ic_add), + contentDescription = null, + tint = MaterialTheme.colors.primary + ) + Spacer(Modifier.width(4.dp)) + Text( + generalGetString(MR.strings.add_relay_button), + color = MaterialTheme.colors.primary + ) + } + } + } + */ SectionBottomSpacer() } } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupChatInfoView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupChatInfoView.kt index 2c3a6c713b..0f64479359 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupChatInfoView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupChatInfoView.kt @@ -239,39 +239,88 @@ fun leaveGroupDialog(rhId: Long?, groupInfo: GroupInfo, chatModel: ChatModel, cl ) } -private fun removeMemberAlert(rhId: Long?, groupInfo: GroupInfo, mem: GroupMember) { - val titleId = if (groupInfo.useRelays) MR.strings.button_remove_subscriber_question - else MR.strings.button_remove_member_question - val messageId = if (groupInfo.useRelays) - MR.strings.subscriber_will_be_removed_from_channel_cannot_be_undone - else if (groupInfo.businessChat == null) - MR.strings.member_will_be_removed_from_group_cannot_be_undone - else - MR.strings.member_will_be_removed_from_chat_cannot_be_undone - AlertManager.shared.showAlertDialogButtonsColumn( - generalGetString(titleId), - generalGetString(messageId), - buttons = { - Column { - SectionItemView({ - AlertManager.shared.hideAlert() - removeMembers(rhId, groupInfo, listOf(mem.groupMemberId), withMessages = false) - }) { - Text(generalGetString(MR.strings.remove_member_confirmation), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = Color.Red) +fun removeMemberAlert(rhId: Long?, groupInfo: GroupInfo, mem: GroupMember) { + if (mem.memberRole == GroupMemberRole.Relay) { + val isLastActive = groupInfo.useRelays && mem.memberCurrent && run { + val activeRelays = ChatModel.groupMembers.value.filter { it.memberRole == GroupMemberRole.Relay && it.memberCurrent } + activeRelays.size <= 1 + } + val message = if (isLastActive) generalGetString(MR.strings.last_active_relay_warning) + else generalGetString(MR.strings.relay_will_be_removed_from_channel) + AlertManager.shared.showAlertDialogButtonsColumn( + generalGetString(MR.strings.button_remove_relay_question), + message, + buttons = { + Column { + SectionItemView({ + AlertManager.shared.hideAlert() + removeMembers(rhId, groupInfo, listOf(mem.groupMemberId), withMessages = false) + }) { + Text(generalGetString(MR.strings.remove_member_confirmation), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = Color.Red) + } + SectionItemView({ + AlertManager.shared.hideAlert() + }) { + Text(generalGetString(MR.strings.cancel_verb), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = MaterialTheme.colors.primary) + } } - SectionItemView({ - AlertManager.shared.hideAlert() - removeMembers(rhId, groupInfo, listOf(mem.groupMemberId), withMessages = true) - }) { - Text(generalGetString(MR.strings.remove_member_delete_messages_confirmation), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = Color.Red) + }) + } else if (groupInfo.useRelays) { + AlertManager.shared.showAlertDialogButtonsColumn( + generalGetString(MR.strings.button_remove_subscriber_question), + generalGetString(MR.strings.subscriber_will_be_removed_from_channel_cannot_be_undone), + buttons = { + Column { + SectionItemView({ + AlertManager.shared.hideAlert() + removeMembers(rhId, groupInfo, listOf(mem.groupMemberId), withMessages = false) + }) { + Text(generalGetString(MR.strings.remove_member_confirmation), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = Color.Red) + } + SectionItemView({ + AlertManager.shared.hideAlert() + removeMembers(rhId, groupInfo, listOf(mem.groupMemberId), withMessages = true) + }) { + Text(generalGetString(MR.strings.remove_member_delete_messages_confirmation), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = Color.Red) + } + SectionItemView({ + AlertManager.shared.hideAlert() + }) { + Text(generalGetString(MR.strings.cancel_verb), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = MaterialTheme.colors.primary) + } } - SectionItemView({ - AlertManager.shared.hideAlert() - }) { - Text(generalGetString(MR.strings.cancel_verb), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = MaterialTheme.colors.primary) + }) + } else { + val titleId = MR.strings.button_remove_member_question + val messageId = if (groupInfo.businessChat == null) + MR.strings.member_will_be_removed_from_group_cannot_be_undone + else + MR.strings.member_will_be_removed_from_chat_cannot_be_undone + AlertManager.shared.showAlertDialogButtonsColumn( + generalGetString(titleId), + generalGetString(messageId), + buttons = { + Column { + SectionItemView({ + AlertManager.shared.hideAlert() + removeMembers(rhId, groupInfo, listOf(mem.groupMemberId), withMessages = false) + }) { + Text(generalGetString(MR.strings.remove_member_confirmation), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = Color.Red) + } + SectionItemView({ + AlertManager.shared.hideAlert() + removeMembers(rhId, groupInfo, listOf(mem.groupMemberId), withMessages = true) + }) { + Text(generalGetString(MR.strings.remove_member_delete_messages_confirmation), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = Color.Red) + } + SectionItemView({ + AlertManager.shared.hideAlert() + }) { + Text(generalGetString(MR.strings.cancel_verb), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = MaterialTheme.colors.primary) + } } - } - }) + }) + } } private fun removeMembersAlert(rhId: Long?, groupInfo: GroupInfo, memberIds: List, onSuccess: () -> Unit = {}) { diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupLinkView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupLinkView.kt index c0b107b5dd..57ba0fbd88 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupLinkView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupLinkView.kt @@ -281,8 +281,13 @@ fun GroupLinkLayout( ) } if (creatingGroup && close != null) { - Spacer(Modifier.height(DEFAULT_PADDING_HALF)) - ContinueButton(close) + SettingsActionItem( + painterResource(MR.images.ic_check), + stringResource(MR.strings.continue_to_next_step), + click = close, + iconColor = MaterialTheme.colors.primary, + textColor = MaterialTheme.colors.primary, + ) } } } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupMemberInfoView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupMemberInfoView.kt index 1bc6f038f3..28cbb663a6 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupMemberInfoView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupMemberInfoView.kt @@ -242,34 +242,86 @@ fun GroupMemberInfoView( } fun removeMemberDialog(rhId: Long?, groupInfo: GroupInfo, member: GroupMember, chatModel: ChatModel, close: (() -> Unit)? = null) { - val messageId = if (groupInfo.businessChat == null) - MR.strings.member_will_be_removed_from_group_cannot_be_undone - else - MR.strings.member_will_be_removed_from_chat_cannot_be_undone - AlertManager.shared.showAlertDialogButtonsColumn( - generalGetString(MR.strings.button_remove_member_question), - generalGetString(messageId), - buttons = { - Column { - SectionItemView({ - AlertManager.shared.hideAlert() - removeMember(rhId, groupInfo, member, withMessages = false, chatModel, close) - }) { - Text(generalGetString(MR.strings.remove_member_confirmation), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = Color.Red) + if (member.memberRole == GroupMemberRole.Relay) { + val isLastActive = groupInfo.useRelays && run { + val activeRelays = chatModel.groupMembers.value.filter { it.memberRole == GroupMemberRole.Relay && it.memberCurrent } + activeRelays.size <= 1 + } + val message = if (isLastActive) generalGetString(MR.strings.last_active_relay_warning) + else generalGetString(MR.strings.relay_will_be_removed_from_channel) + AlertManager.shared.showAlertDialogButtonsColumn( + generalGetString(MR.strings.button_remove_relay_question), + message, + buttons = { + Column { + SectionItemView({ + AlertManager.shared.hideAlert() + removeMember(rhId, groupInfo, member, withMessages = false, chatModel, close) + }) { + Text(generalGetString(MR.strings.remove_member_confirmation), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = Color.Red) + } + SectionItemView({ + AlertManager.shared.hideAlert() + }) { + Text(generalGetString(MR.strings.cancel_verb), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = MaterialTheme.colors.primary) + } } - SectionItemView({ - AlertManager.shared.hideAlert() - removeMember(rhId, groupInfo, member, withMessages = true, chatModel, close) - }) { - Text(generalGetString(MR.strings.remove_member_delete_messages_confirmation), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = Color.Red) + }) + } else if (groupInfo.useRelays) { + AlertManager.shared.showAlertDialogButtonsColumn( + generalGetString(MR.strings.button_remove_subscriber_question), + generalGetString(MR.strings.subscriber_will_be_removed_from_channel_cannot_be_undone), + buttons = { + Column { + SectionItemView({ + AlertManager.shared.hideAlert() + removeMember(rhId, groupInfo, member, withMessages = false, chatModel, close) + }) { + Text(generalGetString(MR.strings.remove_member_confirmation), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = Color.Red) + } + SectionItemView({ + AlertManager.shared.hideAlert() + removeMember(rhId, groupInfo, member, withMessages = true, chatModel, close) + }) { + Text(generalGetString(MR.strings.remove_member_delete_messages_confirmation), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = Color.Red) + } + SectionItemView({ + AlertManager.shared.hideAlert() + }) { + Text(generalGetString(MR.strings.cancel_verb), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = MaterialTheme.colors.primary) + } } - SectionItemView({ - AlertManager.shared.hideAlert() - }) { - Text(generalGetString(MR.strings.cancel_verb), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = MaterialTheme.colors.primary) + }) + } else { + val messageId = if (groupInfo.businessChat == null) + MR.strings.member_will_be_removed_from_group_cannot_be_undone + else + MR.strings.member_will_be_removed_from_chat_cannot_be_undone + AlertManager.shared.showAlertDialogButtonsColumn( + generalGetString(MR.strings.button_remove_member_question), + generalGetString(messageId), + buttons = { + Column { + SectionItemView({ + AlertManager.shared.hideAlert() + removeMember(rhId, groupInfo, member, withMessages = false, chatModel, close) + }) { + Text(generalGetString(MR.strings.remove_member_confirmation), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = Color.Red) + } + SectionItemView({ + AlertManager.shared.hideAlert() + removeMember(rhId, groupInfo, member, withMessages = true, chatModel, close) + }) { + Text(generalGetString(MR.strings.remove_member_delete_messages_confirmation), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = Color.Red) + } + SectionItemView({ + AlertManager.shared.hideAlert() + }) { + Text(generalGetString(MR.strings.cancel_verb), Modifier.fillMaxWidth(), textAlign = TextAlign.Center, color = MaterialTheme.colors.primary) + } } - } - }) + }) + } } fun deleteMemberMessagesDialog(rhId: Long?, groupInfo: GroupInfo, member: GroupMember, chatModel: ChatModel, close: (() -> Unit)? = null) { @@ -368,6 +420,7 @@ fun GroupMemberInfoLayout( @Composable fun ModeratorDestructiveSection() { val canBlockForAll = member.canBlockForAll(groupInfo) + // TODO [relays] re-enable when relay management ships val canRemove = member.canBeRemoved(groupInfo) && member.memberRole != GroupMemberRole.Relay if (canBlockForAll || canRemove) { SectionDividerSpaced(maxBottomPadding = false) @@ -380,10 +433,10 @@ fun GroupMemberInfoLayout( } } if (canRemove) { - if (member.memberStatus == GroupMemberStatus.MemRemoved || member.memberStatus == GroupMemberStatus.MemLeft) { + if (member.memberStatus != GroupMemberStatus.MemRemoved && (member.memberStatus != GroupMemberStatus.MemLeft || member.memberRole == GroupMemberRole.Relay)) { + RemoveMemberButton(groupInfo.useRelays, member.memberRole == GroupMemberRole.Relay, removeMember) + } else if (member.memberRole != GroupMemberRole.Relay) { DeleteMemberMessagesButton(deleteMemberMessages) - } else { - RemoveMemberButton(groupInfo.useRelays, removeMember) } } } @@ -753,8 +806,10 @@ fun UnblockForAllButton(onClick: () -> Unit) { } @Composable -fun RemoveMemberButton(useRelays: Boolean = false, onClick: () -> Unit) { - val label = if (useRelays) MR.strings.button_remove_subscriber else MR.strings.button_remove_member +fun RemoveMemberButton(useRelays: Boolean = false, isRelay: Boolean = false, onClick: () -> Unit) { + val label = if (isRelay) MR.strings.button_remove_relay + else if (useRelays) MR.strings.button_remove_subscriber + else MR.strings.button_remove_member SettingsActionItem( painterResource(MR.images.ic_delete), stringResource(label), diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/CIImageView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/CIImageView.kt index 064b5370bc..8bfbea9fa6 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/CIImageView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/CIImageView.kt @@ -182,7 +182,7 @@ fun CIImageView( .then( if (!smallView) { val w = if (previewBitmap.width * 0.97 <= previewBitmap.height) imageViewFullWidth() * 0.75f else DEFAULT_MAX_IMAGE_WIDTH - Modifier.width(w).aspectRatio(previewBitmap.width.toFloat() / previewBitmap.height.toFloat()) + Modifier.width(w).aspectRatio((previewBitmap.width.toFloat() / previewBitmap.height.toFloat()).coerceAtLeast(1f / 2.33f)) } else Modifier ) .desktopModifyBlurredState(!smallView, blurred, showMenu), diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/ChatItemView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/ChatItemView.kt index 15b0a12822..64288d9055 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/ChatItemView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/ChatItemView.kt @@ -374,7 +374,7 @@ fun ChatItemView( @Composable fun DeleteItemMenu() { DefaultDropdownMenu(showMenu) { - DeleteItemAction(chatsCtx, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) + DeleteItemAction(chatsCtx, cInfo, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) if (cItem.canBeDeletedForSelf) { Divider() SelectItemAction(showMenu, selectChatItem) @@ -392,7 +392,7 @@ fun ChatItemView( if (cItem.chatDir !is CIDirection.GroupSnd && cInfo.groupInfo.membership.memberRole >= GroupMemberRole.Moderator) { ArchiveReportItemAction(cItem.id, cInfo.groupInfo.membership.memberActive, showMenu, archiveReports) } - DeleteItemAction(chatsCtx, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages, buttonText = stringResource(MR.strings.delete_report)) + DeleteItemAction(chatsCtx, cInfo, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages, buttonText = stringResource(MR.strings.delete_report)) Divider() SelectItemAction(showMenu, selectChatItem) } @@ -482,7 +482,7 @@ fun ChatItemView( CancelFileItemAction(cItem.file.fileId, showMenu, cancelFile = cancelFile, cancelAction = cItem.file.cancelAction) } if (!(live && cItem.meta.isLive) && !preview) { - DeleteItemAction(chatsCtx, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) + DeleteItemAction(chatsCtx, cInfo, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) } if (cItem.chatDir !is CIDirection.GroupSnd) { val groupInfo = cItem.memberToModerate(cInfo)?.first @@ -508,7 +508,7 @@ fun ChatItemView( ExpandItemAction(revealed, showMenu, reveal) } ItemInfoAction(cInfo, cItem, showItemDetails, showMenu) - DeleteItemAction(chatsCtx, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) + DeleteItemAction(chatsCtx, cInfo, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) if (cItem.canBeDeletedForSelf) { Divider() SelectItemAction(showMenu, selectChatItem) @@ -518,7 +518,7 @@ fun ChatItemView( cItem.isDeletedContent -> { DefaultDropdownMenu(showMenu) { ItemInfoAction(cInfo, cItem, showItemDetails, showMenu) - DeleteItemAction(chatsCtx, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) + DeleteItemAction(chatsCtx, cInfo, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) if (cItem.canBeDeletedForSelf) { Divider() SelectItemAction(showMenu, selectChatItem) @@ -532,7 +532,7 @@ fun ChatItemView( } else { ExpandItemAction(revealed, showMenu, reveal) } - DeleteItemAction(chatsCtx, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) + DeleteItemAction(chatsCtx, cInfo, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) if (cItem.canBeDeletedForSelf) { Divider() SelectItemAction(showMenu, selectChatItem) @@ -541,7 +541,7 @@ fun ChatItemView( } else -> { DefaultDropdownMenu(showMenu) { - DeleteItemAction(chatsCtx, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) + DeleteItemAction(chatsCtx, cInfo, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) if (selectedChatItems.value == null) { Divider() SelectItemAction(showMenu, selectChatItem) @@ -558,7 +558,7 @@ fun ChatItemView( RevealItemAction(revealed, showMenu, reveal) } ItemInfoAction(cInfo, cItem, showItemDetails, showMenu) - DeleteItemAction(chatsCtx, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) + DeleteItemAction(chatsCtx, cInfo, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) if (cItem.canBeDeletedForSelf) { Divider() SelectItemAction(showMenu, selectChatItem) @@ -587,7 +587,7 @@ fun ChatItemView( DeletedItemView(cItem, cInfo.timedMessagesTTL, showViaProxy = showViaProxy, showTimestamp = showTimestamp) DefaultDropdownMenu(showMenu) { ItemInfoAction(cInfo, cItem, showItemDetails, showMenu) - DeleteItemAction(chatsCtx, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) + DeleteItemAction(chatsCtx, cInfo, cItem, revealed, showMenu, questionText = deleteMessageQuestionText(), deleteMessage, deleteMessages) if (cItem.canBeDeletedForSelf) { Divider() SelectItemAction(showMenu, selectChatItem) @@ -661,7 +661,7 @@ fun ChatItemView( ExpandItemAction(revealed, showMenu, reveal) } ItemInfoAction(cInfo, cItem, showItemDetails, showMenu) - DeleteItemAction(chatsCtx, cItem, revealed, showMenu, questionText = generalGetString(MR.strings.delete_message_cannot_be_undone_warning), deleteMessage, deleteMessages) + DeleteItemAction(chatsCtx, cInfo, cItem, revealed, showMenu, questionText = generalGetString(MR.strings.delete_message_cannot_be_undone_warning), deleteMessage, deleteMessages) if (cItem.canBeDeletedForSelf) { Divider() SelectItemAction(showMenu, selectChatItem) @@ -866,6 +866,7 @@ fun ItemInfoAction( @Composable fun DeleteItemAction( chatsCtx: ChatModel.ChatsContext, + cInfo: ChatInfo, cItem: ChatItem, revealed: State, showMenu: MutableState, @@ -898,13 +899,13 @@ fun DeleteItemAction( deleteMessages = { ids, _ -> deleteMessages(ids) } ) } else { - deleteMessageAlertDialog(cItem, questionText, deleteMessage = deleteMessage) + deleteMessageAlertDialog(cItem, questionText, cInfo, deleteMessage = deleteMessage) } } else { - deleteMessageAlertDialog(cItem, questionText, deleteMessage = deleteMessage) + deleteMessageAlertDialog(cItem, questionText, cInfo, deleteMessage = deleteMessage) } } else { - deleteMessageAlertDialog(cItem, questionText, deleteMessage = deleteMessage) + deleteMessageAlertDialog(cItem, questionText, cInfo, deleteMessage = deleteMessage) } }, color = Color.Red @@ -1371,7 +1372,9 @@ fun cancelFileAlertDialog(fileId: Long, cancelFile: (Long) -> Unit, cancelAction ) } -fun deleteMessageAlertDialog(chatItem: ChatItem, questionText: String, deleteMessage: (Long, CIDeleteMode) -> Unit) { +fun deleteMessageAlertDialog(chatItem: ChatItem, questionText: String, chatInfo: ChatInfo, deleteMessage: (Long, CIDeleteMode) -> Unit) { + val canDeleteForEveryone = chatItem.meta.deletable && !chatItem.localNote && !chatItem.isReport + val editorial = publicGroupEditor(chatInfo) AlertManager.shared.showAlertDialogButtons( title = generalGetString(MR.strings.delete_message__question), text = questionText, @@ -1382,11 +1385,18 @@ fun deleteMessageAlertDialog(chatItem: ChatItem, questionText: String, deleteMes .padding(horizontal = 8.dp, vertical = 2.dp), horizontalArrangement = Arrangement.Center, ) { - TextButton(onClick = { - deleteMessage(chatItem.id, CIDeleteMode.cidmInternal) - AlertManager.shared.hideAlert() - }) { Text(stringResource(MR.strings.for_me_only), color = MaterialTheme.colors.error) } - if (chatItem.meta.deletable && !chatItem.localNote && !chatItem.isReport) { + if (editorial) { + TextButton(onClick = { + deleteMessage(chatItem.id, CIDeleteMode.cidmHistory) + AlertManager.shared.hideAlert() + }) { Text(stringResource(MR.strings.from_history), color = MaterialTheme.colors.error) } + } else { + TextButton(onClick = { + deleteMessage(chatItem.id, CIDeleteMode.cidmInternal) + AlertManager.shared.hideAlert() + }) { Text(stringResource(MR.strings.for_me_only), color = MaterialTheme.colors.error) } + } + if (canDeleteForEveryone) { Spacer(Modifier.padding(horizontal = 4.dp)) TextButton(onClick = { deleteMessage(chatItem.id, CIDeleteMode.cidmBroadcast) @@ -1398,7 +1408,7 @@ fun deleteMessageAlertDialog(chatItem: ChatItem, questionText: String, deleteMes ) } -fun deleteMessagesAlertDialog(itemIds: List, questionText: String, forAll: Boolean, deleteMessages: (List, Boolean) -> Unit) { +fun deleteMessagesAlertDialog(itemIds: List, questionText: String, forAll: Boolean, editorial: Boolean = false, deleteMessages: (List, Boolean) -> Unit) { AlertManager.shared.showAlertDialogButtons( title = generalGetString(MR.strings.delete_messages__question).format(itemIds.size), text = questionText, @@ -1412,7 +1422,7 @@ fun deleteMessagesAlertDialog(itemIds: List, questionText: String, forAll: TextButton(onClick = { deleteMessages(itemIds, false) AlertManager.shared.hideAlert() - }) { Text(stringResource(MR.strings.for_me_only), color = MaterialTheme.colors.error) } + }) { Text(stringResource(if (editorial) MR.strings.from_history else MR.strings.for_me_only), color = MaterialTheme.colors.error) } if (forAll) { TextButton(onClick = { diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/FramedItemView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/FramedItemView.kt index f2788715fe..f55c49fdd1 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/FramedItemView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/FramedItemView.kt @@ -365,7 +365,7 @@ fun FramedItemView( is MsgContent.MCReport -> { val prefix = buildAnnotatedString { withStyle(SpanStyle(color = Color.Red, fontStyle = FontStyle.Italic)) { - append(if (mc.text.isEmpty()) mc.reason.text else "${mc.reason.text}: ") + append(itemPrefixText(ci)) } } CIMarkdownText(chatsCtx, ci, chat, chatTTL, linkMode, uriHandler, onLinkLongClick, showViaProxy = showViaProxy, showTimestamp = showTimestamp, prefix = prefix) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/TextItemView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/TextItemView.kt index 15d67d8e20..3358a23e1e 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/TextItemView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/TextItemView.kt @@ -85,6 +85,13 @@ fun itemDisplayText(ci: ChatItem, linkMode: SimplexLinkMode): String { return formattedText.joinToString("") { itemSegmentDisplayText(it, ci, linkMode) } } +// Display-only prefix rendered before ci.text (e.g. "Spam: " for reports). +// Renderers and selection code MUST share this string — otherwise selection offsets drift from screen. +fun itemPrefixText(ci: ChatItem): String = when (val mc = ci.content.msgContent) { + is MsgContent.MCReport -> if (mc.text.isEmpty()) mc.reason.text else "${mc.reason.text}: " + else -> "" +} + // Text transformations in MarkdownText must match itemSegmentDisplayText above @Composable fun MarkdownText ( diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatPreviewView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatPreviewView.kt index 346e9bac95..d749865e10 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatPreviewView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatPreviewView.kt @@ -255,11 +255,11 @@ fun ChatPreviewView( ci.content.msgContent is MsgContent.MCChat -> null else -> ci.formattedText } - val prefix = when (val mc = ci.content.msgContent) { + val prefix = when (ci.content.msgContent) { is MsgContent.MCReport -> buildAnnotatedString { withStyle(SpanStyle(color = Color.Red, fontStyle = FontStyle.Italic)) { - append(if (text.isEmpty()) mc.reason.text else "${mc.reason.text}: ") + append(itemPrefixText(ci)) } } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/newchat/AddChannelView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/newchat/AddChannelView.kt index 941d232776..09372636ab 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/newchat/AddChannelView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/newchat/AddChannelView.kt @@ -404,11 +404,6 @@ private fun ProgressStepView( ModalView( close = { showCancelAlert() }, showClose = false, - endButtons = { - TextButton(onClick = { showCancelAlert() }) { - Text(generalGetString(MR.strings.button_delete_channel)) - } - } ) { ColumnWithScrollBar { AppBarTitle(generalGetString(MR.strings.creating_channel)) @@ -481,9 +476,16 @@ private fun ProgressStepView( Spacer(Modifier.height(16.dp)) SectionView { + SettingsActionItem( + painterResource(MR.images.ic_delete), + generalGetString(MR.strings.button_cancel_and_delete_channel), + click = { showCancelAlert() }, + textColor = Color.Red, + iconColor = Color.Red, + ) val enabled = activeCount > 0 SettingsActionItem( - painterResource(MR.images.ic_link), + painterResource(MR.images.ic_check), generalGetString(MR.strings.continue_to_next_step), click = { if (activeCount >= total) { @@ -586,7 +588,7 @@ fun relayDisplayName(relay: GroupRelay): String { return "relay ${relay.groupRelayId}" } -private fun chatRelayDisplayName(relay: UserChatRelay): String { +fun chatRelayDisplayName(relay: UserChatRelay): String { if (relay.displayName.isNotEmpty()) return relay.displayName return relay.address } @@ -595,7 +597,7 @@ private fun chatRelayDisplayName(relay: UserChatRelay): String { fun RelayStatusIndicator(status: RelayStatus, connFailed: Boolean = false, memberStatus: GroupMemberStatus? = null) { val removed = memberStatus in listOf(GroupMemberStatus.MemLeft, GroupMemberStatus.MemRemoved, GroupMemberStatus.MemGroupDeleted) val color = if (connFailed || removed) Color.Red else if (status == RelayStatus.RsActive) Color.Green else WarningYellow - val text = if (connFailed) generalGetString(MR.strings.relay_status_failed) else if (memberStatus == GroupMemberStatus.MemLeft) generalGetString(MR.strings.relay_conn_status_removed_by_operator) else status.text + val text = if (connFailed) generalGetString(MR.strings.relay_status_failed) else if (memberStatus == GroupMemberStatus.MemLeft) generalGetString(MR.strings.relay_conn_status_removed_by_operator) else if (removed) generalGetString(MR.strings.relay_conn_status_removed) else status.text Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/usersettings/SettingsView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/usersettings/SettingsView.kt index c826b1dc51..a02d67265d 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/usersettings/SettingsView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/usersettings/SettingsView.kt @@ -295,14 +295,10 @@ fun ChatLockItem( } private fun resetHintPreferences() { - for ((pref, def) in appPreferences.hintPreferences) { - pref.set(def) - } + appPreferences.hintPreferences.forEach { it.reset() } } -fun unchangedHintPreferences(): Boolean = appPreferences.hintPreferences.all { (pref, def) -> - pref.state.value == def -} +fun unchangedHintPreferences(): Boolean = appPreferences.hintPreferences.all { it.isUnchanged() } @Composable fun AppVersionItem(showVersion: () -> Unit) { diff --git a/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml b/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml index b21f1b5627..7304625945 100644 --- a/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml +++ b/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml @@ -207,6 +207,7 @@ Error deleting group Error deleting private notes Error deleting contact request + Error deleting message Error deleting pending contact connection Error changing address Error aborting address change @@ -424,6 +425,7 @@ The messages will be marked as moderated for all members. Delete for me For everyone + From history Stop file Stop sending file? Sending file will be stopped. @@ -1887,6 +1889,7 @@ you: %1$s Delete group Delete channel + Cancel and delete channel Delete chat Delete group? Delete channel? @@ -2985,6 +2988,7 @@ deleted failed removed by operator + removed new invited accepted @@ -3006,7 +3010,8 @@ %1$d/%2$d relays connected, %3$d failed %1$d/%2$d relays connected, %3$d removed %1$d/%2$d relays connected - Adding relays will be supported later. + No relays + Add relays to restore message delivery. Waiting for channel owner to add relays. @@ -3021,6 +3026,10 @@ Subscribers use relay link to connect to the channel.\nRelay address was used to set up this relay for the channel. You connected to the channel via this relay link. Remove subscriber + Remove relay + Remove relay? + Relay will be removed from channel - this cannot be undone! + This is the last active relay. Removing it will prevent message delivery to subscribers. Block subscriber for all? @@ -3040,6 +3049,15 @@ Your profile %1$s will be shared with channel relays and subscribers.\nRelays can access channel messages. Configure relays failed + Add + Add relay + Add relays + No available relays + Error adding relays + Relays added: %1$s. + Select relays + No relays selected + %d relay(s) selected Relay connection failed Not all relays connected Wait @@ -3063,4 +3081,16 @@ Link preview will be requested via SOCKS proxy. DNS lookup may still happen locally via your DNS resolver. Enable Disable + + + Minimize to tray? + If you choose Close, messages won\'t be received.\nYou can change it later in Appearance settings. + Close the app + Minimize to tray + Show SimpleX + Quit SimpleX + SimpleX + SimpleX — %d unread + Minimize to tray when closing window + Keep SimpleX running in the background to receive messages. \ No newline at end of file diff --git a/apps/multiplatform/common/src/commonMain/resources/MR/images/ic_simplex_tray_dot.svg b/apps/multiplatform/common/src/commonMain/resources/MR/images/ic_simplex_tray_dot.svg new file mode 100644 index 0000000000..7e77b31444 --- /dev/null +++ b/apps/multiplatform/common/src/commonMain/resources/MR/images/ic_simplex_tray_dot.svg @@ -0,0 +1,46 @@ + + + + + + + + + + + + + diff --git a/apps/multiplatform/common/src/commonMain/resources/MR/images/ic_simplex_tray_dot_light.svg b/apps/multiplatform/common/src/commonMain/resources/MR/images/ic_simplex_tray_dot_light.svg new file mode 100644 index 0000000000..ea9417f047 --- /dev/null +++ b/apps/multiplatform/common/src/commonMain/resources/MR/images/ic_simplex_tray_dot_light.svg @@ -0,0 +1,46 @@ + + + + + + + + + + + + + diff --git a/apps/multiplatform/common/src/commonMain/resources/MR/images/ic_simplex_tray_light.svg b/apps/multiplatform/common/src/commonMain/resources/MR/images/ic_simplex_tray_light.svg new file mode 100644 index 0000000000..5e8c346fdd --- /dev/null +++ b/apps/multiplatform/common/src/commonMain/resources/MR/images/ic_simplex_tray_light.svg @@ -0,0 +1,44 @@ + + + + + + + + + + + diff --git a/apps/multiplatform/common/src/commonTest/kotlin/chat/simplex/app/ProviderForGalleryTest.kt b/apps/multiplatform/common/src/commonTest/kotlin/chat/simplex/app/ProviderForGalleryTest.kt new file mode 100644 index 0000000000..f9311ea6a9 --- /dev/null +++ b/apps/multiplatform/common/src/commonTest/kotlin/chat/simplex/app/ProviderForGalleryTest.kt @@ -0,0 +1,67 @@ +package chat.simplex.app + +import chat.simplex.common.model.* +import chat.simplex.common.platform.chatModel +import chat.simplex.common.views.chat.providerForGallery +import kotlinx.datetime.Clock +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals + +// Regression for PR #6869: scrollToStart() must not rewrite initialChatId. +class ProviderForGalleryTest { + + // Synthetic items pass canShowMedia only when chatModel.connectedToRemote() is true. + @BeforeTest + fun connectChatModelToRemote() { + chatModel.currentRemoteHost.value = RemoteHostInfo( + remoteHostId = 0L, + hostDeviceName = "", + storePath = "", + bindAddress_ = null, + bindPort_ = null, + sessionState = null, + ) + } + + @AfterTest + fun resetChatModel() { + chatModel.currentRemoteHost.value = null + } + + @Test + fun testScrollToStartPreservesAnchor() { + val items = listOf(imageItem(1L), imageItem(2L), imageItem(3L)) + var scrolledTo: Int? = null + val provider = providerForGallery(items, cItemId = 3L) { scrolledTo = it } + + provider.currentPageChanged(provider.initialIndex - 1) + provider.scrollToStart() + provider.onDismiss(0) + + assertEquals(1, scrolledTo) + } + + // Pins the onDismiss early-return contract that testScrollToStartPreservesAnchor + // relies on to read the anchor back through the scrollTo callback. + @Test + fun testOnDismissOnActiveItemDoesNotScroll() { + val items = listOf(imageItem(1L), imageItem(2L), imageItem(3L)) + var scrolledTo: Int? = null + val provider = providerForGallery(items, cItemId = 3L) { scrolledTo = it } + + provider.onDismiss(provider.initialIndex) + + assertEquals(null, scrolledTo) + } + + private fun imageItem(id: Long): ChatItem = + ChatItem( + chatDir = CIDirection.DirectRcv(), + meta = CIMeta.getSample(id, Clock.System.now(), text = ""), + content = CIContent.RcvMsgContent(MsgContent.MCImage(text = "", image = "")), + reactions = emptyList(), + file = CIFile.getSample(fileId = id, fileName = "img-$id.jpg", filePath = "img-$id.jpg"), + ) +} diff --git a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopApp.kt b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopApp.kt index 136a883035..2ae4aed8e2 100644 --- a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopApp.kt +++ b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopApp.kt @@ -31,8 +31,11 @@ import kotlin.system.exitProcess val simplexWindowState = SimplexWindowState() fun showApp() { - val closedByError = mutableStateOf(true) - while (closedByError.value) { + // Probe SystemTray off the EDT — the lazy's first read would otherwise block the + // EDT during composition; JDK-8322750's GNOME detection forks a subprocess. + trayIsAvailable + while (true) { + val closedByError = mutableStateOf(false) application(exitProcessOnExit = false) { CompositionLocalProvider( LocalWindowExceptionHandlerFactory provides WindowExceptionHandlerFactory { window -> @@ -43,8 +46,9 @@ fun showApp() { shareText = true ) Log.e(TAG, "App crashed, thread name: " + Thread.currentThread().name + ", exception: " + e.stackTraceToString()) - window.dispatchEvent(WindowEvent(window, WindowEvent.WINDOW_CLOSING)) + // Must precede dispatchEvent — handleCloseRequest reads this flag. closedByError.value = true + window.dispatchEvent(WindowEvent(window, WindowEvent.WINDOW_CLOSING)) includeMoreFailedComposables() // If the left side of screen has open modal, it's probably caused the crash if (ModalManager.start.hasModalsOpen()) { @@ -73,9 +77,11 @@ fun showApp() { } } ) { + SimplexTray() AppWindow(closedByError) } } + if (!closedByError.value) break } exitProcess(0) } @@ -115,7 +121,7 @@ private fun ApplicationScope.AppWindow(closedByError: MutableState) { simplexWindowState.windowState = windowState // Reload all strings in all @Composable's after language change at runtime if (remember { ChatController.appPrefs.appLanguage.state }.value != "") { - Window(state = windowState, icon = painterResource(MR.images.ic_simplex), onCloseRequest = { closedByError.value = false; exitApplication() }, onKeyEvent = { + Window(state = windowState, visible = simplexWindowState.windowVisible.value, icon = painterResource(MR.images.ic_simplex), onCloseRequest = { handleCloseRequest(closedByError) }, onKeyEvent = { if (it.key == Key.Escape && it.type == KeyEventType.KeyUp) { simplexWindowState.backstack.lastOrNull()?.invoke() != null } else { @@ -224,6 +230,30 @@ private fun ApplicationScope.AppWindow(closedByError: MutableState) { } } +// Not invoked for macOS Cmd+Q — that goes through AWT's default QuitHandler and +// exits the process directly. Intentional: Cmd+Q is canonical "always quit" on macOS. +private fun ApplicationScope.handleCloseRequest(closedByError: MutableState) { + // Crash dispatch — bypass user-facing policy and exit; outer loop will restart. + if (closedByError.value) { + exitApplication() + return + } + val pref = ChatController.appPrefs.closeBehavior + when (pref.get()) { + CloseBehavior.Quit -> exitApplication() + CloseBehavior.MinimizeToTray -> if (trayIsAvailable) { + simplexWindowState.windowVisible.value = false + } else exitApplication() + CloseBehavior.Ask -> if (trayIsAvailable) { + requestCloseBehavior() + } else { + // Tray unavailable — Minimize is not a real option; remember Quit and exit. + pref.set(CloseBehavior.Quit) + exitApplication() + } + } +} + class SimplexWindowState { lateinit var windowState: WindowState val backstack = mutableStateListOf<() -> Unit>() @@ -232,6 +262,7 @@ class SimplexWindowState { val saveDialog = DialogState() val toasts = mutableStateListOf>() var windowFocused = mutableStateOf(true) + val windowVisible = mutableStateOf(true) var window: ComposeWindow? = null } diff --git a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopTray.kt b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopTray.kt new file mode 100644 index 0000000000..3f35c10c9c --- /dev/null +++ b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopTray.kt @@ -0,0 +1,127 @@ +package chat.simplex.common + +import SectionItemView +import androidx.compose.foundation.layout.* +import androidx.compose.material.* +import androidx.compose.runtime.* +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.text.AnnotatedString +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.window.* +import chat.simplex.common.model.ChatModel +import chat.simplex.common.model.CloseBehavior +import chat.simplex.common.model.ChatController.appPrefs +import chat.simplex.common.platform.Log +import chat.simplex.common.platform.TAG +import chat.simplex.common.ui.theme.isInDarkTheme +import chat.simplex.common.views.helpers.AlertManager +import chat.simplex.common.views.helpers.generalGetString +import chat.simplex.res.MR +import dev.icerock.moko.resources.compose.painterResource +import dev.icerock.moko.resources.compose.stringResource +import java.awt.AWTException +import java.awt.SystemTray +import java.awt.TrayIcon +import java.awt.image.BufferedImage + +// Probed once at startup. False on stock GNOME ≥ JDK 21.0.3 per JDK-8322750, and +// also when SystemTray.add() fails despite isSupported() returning true (an older +// JDK pattern Compose-MP does not catch). When false: the Appearance toggle is +// hidden, the first-close dialog is skipped (Ask migrates silently to Quit), and +// the close handler treats MinimizeToTray as Quit. +val trayIsAvailable: Boolean by lazy { + if (!SystemTray.isSupported()) return@lazy false + try { + val tray = SystemTray.getSystemTray() + val probe = TrayIcon(BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)) + tray.add(probe) + tray.remove(probe) + true + } catch (e: AWTException) { + Log.w(TAG, "SystemTray probe failed: ${e.stackTraceToString()}") + false + } catch (e: SecurityException) { + Log.w(TAG, "SystemTray probe denied: ${e.stackTraceToString()}") + false + } +} + +fun showWindow() { + simplexWindowState.windowVisible.value = true + simplexWindowState.window?.toFront() + simplexWindowState.window?.requestFocus() +} + +@Composable +fun ApplicationScope.SimplexTray() { + if (!trayIsAvailable) return + if (remember { appPrefs.closeBehavior.state }.value != CloseBehavior.MinimizeToTray) return + // Sum of per-profile unread (UserInfo.unreadCount, the same field UserPicker renders + // per row). Skip muted profiles unless they're the active one. + val unread by remember { + derivedStateOf { + ChatModel.users.sumOf { + if (!it.user.showNtfs && !it.user.activeUser) 0 else it.unreadCount + } + } + } + val iconRes = if (unread > 0) { + if (isInDarkTheme()) MR.images.ic_simplex_tray_dot_light else MR.images.ic_simplex_tray_dot + } else { + if (isInDarkTheme()) MR.images.ic_simplex_tray_light else MR.images.ic_simplex + } + val tooltip = + if (unread > 0) stringResource(MR.strings.tray_tooltip_unread, unread) + else stringResource(MR.strings.tray_tooltip) + Tray( + icon = painterResource(iconRes), + tooltip = tooltip, + onAction = ::showWindow, + menu = { + Item(stringResource(MR.strings.tray_show), onClick = ::showWindow) + Separator() + Item(stringResource(MR.strings.tray_quit), onClick = { exitApplication() }) + } + ) +} + +// Renders in the main app window via AlertManager (same surface as e.g. the link +// previews confirmation). Lambdas close over the calling ApplicationScope; if the +// app crashes while the dialog is open, the crash handler's alert replaces it, so +// stale closures never get clicked. +fun ApplicationScope.requestCloseBehavior() { + val pref = appPrefs.closeBehavior + AlertManager.shared.showAlertDialogButtonsColumn( + title = generalGetString(MR.strings.close_behavior_dialog_title), + text = AnnotatedString(generalGetString(MR.strings.close_behavior_dialog_text)), + buttons = { + Column { + SectionItemView({ + AlertManager.shared.hideAlert() + pref.set(CloseBehavior.Quit) + exitApplication() + }) { + Text( + stringResource(MR.strings.close_behavior_dialog_close), + Modifier.fillMaxWidth(), + textAlign = TextAlign.Center, + color = Color.Red + ) + } + SectionItemView({ + AlertManager.shared.hideAlert() + pref.set(CloseBehavior.MinimizeToTray) + simplexWindowState.windowVisible.value = false + }) { + Text( + stringResource(MR.strings.close_behavior_dialog_minimize), + Modifier.fillMaxWidth(), + textAlign = TextAlign.Center, + color = MaterialTheme.colors.primary + ) + } + } + } + ) +} diff --git a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/PlatformTextField.desktop.kt b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/PlatformTextField.desktop.kt index 41964b7d18..557dabd2e4 100644 --- a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/PlatformTextField.desktop.kt +++ b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/PlatformTextField.desktop.kt @@ -86,8 +86,8 @@ actual fun PlatformTextField( // Different padding here is for a text that is considered RTL with non-RTL locale set globally. // In this case padding from right side should be bigger val startEndPadding = if (cs.message.text.isEmpty() && showVoiceButton && isRtlByCharacters && isLtrGlobally) 95.dp else 50.dp - val startPadding = if (isRtlByCharacters && isLtrGlobally) startEndPadding else 0.dp - val endPadding = if (isRtlByCharacters && isLtrGlobally) 0.dp else startEndPadding + val startPadding = 0.dp + val endPadding = startEndPadding val padding = PaddingValues(startPadding, 12.dp, endPadding, 0.dp) var textFieldValueState by remember { mutableStateOf(TextFieldValue(text = cs.message.text, selection = cs.message.selection)) } val textFieldValue = textFieldValueState.copy(text = cs.message.text, selection = cs.message.selection) diff --git a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/views/call/CallView.desktop.kt b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/views/call/CallView.desktop.kt index ed2f6e7859..20fe6a48a3 100644 --- a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/views/call/CallView.desktop.kt +++ b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/views/call/CallView.desktop.kt @@ -17,6 +17,7 @@ import org.nanohttpd.protocols.http.response.Response.newFixedLengthResponse import org.nanohttpd.protocols.http.response.Status import org.nanohttpd.protocols.websockets.* import java.io.IOException +import java.net.BindException import java.net.URI private const val SERVER_HOST = "localhost" @@ -157,17 +158,18 @@ fun WebRTCController(callCommand: SnapshotStateList, onResponse: ( if (call != null) withBGApi { chatModel.callManager.endCall(call) } } val server = remember { - try { - uriHandler.openUri("http://${SERVER_HOST}:$SERVER_PORT/simplex/call/") - } catch (e: Exception) { - Log.e(TAG, "Unable to open browser: ${e.stackTraceToString()}") - AlertManager.shared.showAlertMsg( - title = generalGetString(MR.strings.unable_to_open_browser_title), - text = generalGetString(MR.strings.unable_to_open_browser_desc) - ) - endCall() + startServer(onResponse).apply { + try { + uriHandler.openUri("http://${SERVER_HOST}:${listeningPort}/simplex/call/") + } catch (e: Exception) { + Log.e(TAG, "Unable to open browser: ${e.stackTraceToString()}") + AlertManager.shared.showAlertMsg( + title = generalGetString(MR.strings.unable_to_open_browser_title), + text = generalGetString(MR.strings.unable_to_open_browser_desc) + ) + endCall() + } } - startServer(onResponse) } fun processCommand(cmd: WCallCommand) { val apiCall = WVAPICall(command = cmd) @@ -206,8 +208,8 @@ fun WebRTCController(callCommand: SnapshotStateList, onResponse: ( } } -fun startServer(onResponse: (WVAPIMessage) -> Unit): NanoWSD { - val server = object: NanoWSD(SERVER_HOST, SERVER_PORT) { +fun startServer(onResponse: (WVAPIMessage) -> Unit, port: Int = SERVER_PORT): NanoWSD { + val server = object: NanoWSD(SERVER_HOST, port) { override fun openWebSocket(session: IHTTPSession): WebSocket = MyWebSocket(onResponse, session) fun resourcesToResponse(path: String): Response { @@ -231,7 +233,14 @@ fun startServer(onResponse: (WVAPIMessage) -> Unit): NanoWSD { } } } - server.start(60_000_000) + try { + server.start(60_000_000) + } catch (e: BindException) { + if (port == 0) throw e + Log.w(TAG, "Call server port $port is busy, using a random port: ${e.message}") + server.stop() + return startServer(onResponse, port = 0) + } return server } diff --git a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/views/usersettings/Appearance.desktop.kt b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/views/usersettings/Appearance.desktop.kt index c270bddb73..66be736fca 100644 --- a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/views/usersettings/Appearance.desktop.kt +++ b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/views/usersettings/Appearance.desktop.kt @@ -3,6 +3,7 @@ package chat.simplex.common.views.usersettings import SectionBottomSpacer import SectionDividerSpaced import SectionSpacer +import SectionTextFooter import SectionView import androidx.compose.foundation.* import androidx.compose.foundation.layout.* @@ -18,7 +19,9 @@ import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.* import chat.simplex.common.model.ChatController.appPrefs import chat.simplex.common.model.ChatModel +import chat.simplex.common.model.CloseBehavior import chat.simplex.common.model.SharedPreference +import chat.simplex.common.trayIsAvailable import chat.simplex.common.platform.* import chat.simplex.common.ui.theme.DEFAULT_PADDING import chat.simplex.common.views.helpers.* @@ -65,6 +68,11 @@ fun AppearanceScope.AppearanceLayout( SectionDividerSpaced() ThemesSection(systemDarkTheme) + if (trayIsAvailable) { + SectionDividerSpaced() + MinimizeToTraySection() + } + SectionDividerSpaced() AppToolbarsSection() @@ -84,6 +92,21 @@ fun AppearanceScope.AppearanceLayout( } } +@Composable +private fun MinimizeToTraySection() { + val pref = remember { appPrefs.closeBehavior.state } + val on = pref.value == CloseBehavior.MinimizeToTray + SectionView { + PreferenceToggle( + stringResource(MR.strings.appearance_minimize_to_tray), + checked = on, + ) { checked -> + appPrefs.closeBehavior.set(if (checked) CloseBehavior.MinimizeToTray else CloseBehavior.Quit) + } + } + SectionTextFooter(stringResource(MR.strings.appearance_minimize_to_tray_desc)) +} + @Composable fun DensityScaleSection() { val localDensityScale = remember { mutableStateOf(appPrefs.densityScale.get()) } diff --git a/apps/multiplatform/spec/services/calls.md b/apps/multiplatform/spec/services/calls.md index a8d056ebea..bea1d37f3a 100644 --- a/apps/multiplatform/spec/services/calls.md +++ b/apps/multiplatform/spec/services/calls.md @@ -119,9 +119,9 @@ The `actual` platform implementation of `ActiveCallView()` and supporting compos Desktop calls run WebRTC in the system browser, not an embedded WebView: -- **NanoWSD server** ([line 209](../../common/src/desktopMain/kotlin/chat/simplex/common/views/call/CallView.desktop.kt#L209)): `startServer()` creates a `NanoWSD` instance bound to `localhost:50395`. The server serves `call.html` from JAR resources at `/assets/www/desktop/call.html` for the path `/simplex/call/`. All other paths serve resources from `/assets/www/`. +- **NanoWSD server** ([line 209](../../common/src/desktopMain/kotlin/chat/simplex/common/views/call/CallView.desktop.kt#L209)): `startServer()` creates a `NanoWSD` instance bound to `localhost:50395`. If that port is already in use it falls back to an OS-assigned free port (`port 0`); `WebRTCController` reads `server.listeningPort` for the browser URL. The server serves `call.html` from JAR resources at `/assets/www/desktop/call.html` for the path `/simplex/call/`. All other paths serve resources from `/assets/www/`. - **WebSocket communication** ([line 238](../../common/src/desktopMain/kotlin/chat/simplex/common/views/call/CallView.desktop.kt#L238)): `MyWebSocket` handles WebSocket frames from the browser. `onMessage` deserializes JSON into `WVAPIMessage` and forwards to the response handler. `onClose` triggers `WCallResponse.End`. -- **WebRTCController** ([line 153](../../common/src/desktopMain/kotlin/chat/simplex/common/views/call/CallView.desktop.kt#L153)): Opens `http://localhost:50395/simplex/call/` via `LocalUriHandler`. Processes `WCallCommand` queue by sending JSON over WebSocket to all active connections. On dispose, sends `WCallCommand.End` and stops the server. +- **WebRTCController** ([line 153](../../common/src/desktopMain/kotlin/chat/simplex/common/views/call/CallView.desktop.kt#L153)): Starts the server, then opens `http://localhost:/simplex/call/` (normally `50395`) via `LocalUriHandler`. Processes `WCallCommand` queue by sending JSON over WebSocket to all active connections. On dispose, sends `WCallCommand.End` and stops the server. - **SendStateUpdates** ([line 137](../../common/src/desktopMain/kotlin/chat/simplex/common/views/call/CallView.desktop.kt#L137)): Sends `WCallCommand.Description` with call state and encryption info text to the browser for display. - **ActiveCallView** ([line 28](../../common/src/desktopMain/kotlin/chat/simplex/common/views/call/CallView.desktop.kt#L28)): Handles `WCallResponse` messages identically to Android (same state machine), plus a `WCallCommand.Permission` message on `Capabilities` error for browser permission denial guidance. diff --git a/apps/simplex-support-bot/bot.test.ts b/apps/simplex-support-bot/bot.test.ts index d390835f46..a1afd7a660 100644 --- a/apps/simplex-support-bot/bot.test.ts +++ b/apps/simplex-support-bot/bot.test.ts @@ -1,9 +1,14 @@ import {describe, test, expect, beforeEach, vi} from "vitest" +import {mkdtempSync, writeFileSync} from "fs" +import {tmpdir} from "os" +import {join} from "path" +import {core} from "simplex-chat" import {SupportBot} from "./src/bot.js" import {CardManager} from "./src/cards.js" import {parseConfig} from "./src/config.js" import {GrokApiClient} from "./src/grok.js" -import {welcomeMessage, queueMessage, grokActivatedMessage, teamLockedMessage} from "./src/messages.js" +import {loadGrokContext} from "./src/context.js" +import {welcomeMessage, queueMessage, grokActivatedMessage, teamLockedMessage, teamAlreadyInvitedMessage} from "./src/messages.js" // Silence console output during tests vi.spyOn(console, "log").mockImplementation(() => {}) @@ -83,15 +88,39 @@ class MockChatApi { async apiListMembers(groupId: number) { return this.members.get(groupId) || [] } - async apiGetChat(_chatType: string, chatId: number, _count: number) { + async apiGetChat(chatType: string, chatId: number, _count: number) { + if (chatType === ChatType.Direct) { + // Tests don't exercise direct lookups; throw the same shape production + // would so getContact() resolves to null instead of synthesizing a contact. + throw new core.ChatAPIError("contact not found", { + type: "errorStore", + storeError: {type: "contactNotFound", contactId: chatId}, + } as any) + } + const baseGroupInfo = this.groups.get(chatId) + if (!baseGroupInfo) { + // Mirror production behavior: the real apiGetChat throws "groupNotFound" + // for an unknown id; getGroupInfo() catches and returns null. + throw new core.ChatAPIError("group not found", { + type: "errorStore", + storeError: {type: "groupNotFound", groupId: chatId}, + } as any) + } const items = this.chatItems.get(chatId) || [] - const groupInfo = this.groups.get(chatId) + const groupInfo = {...baseGroupInfo, customData: this.customData.get(chatId)} return { - chatInfo: {type: "group", groupInfo: groupInfo || makeGroupInfo(chatId)}, + chatInfo: {type: "group", groupInfo}, chatItems: items, chatStats: {unreadCount: 0, unreadMentions: 0, reportsCount: 0, minUnreadItemId: 0, unreadChat: false}, } } + async apiGetChats(_userId: number, _pagination: any, _query?: any, _pcc?: boolean) { + return [...this.groups.values()].map(g => ({ + chatInfo: {type: "group", groupInfo: {...g, customData: this.customData.get(g.groupId)}}, + chatItems: [], + chatStats: {unreadCount: 0, unreadMentions: 0, reportsCount: 0, minUnreadItemId: 0, unreadChat: false}, + })) + } async apiListGroups(_userId: number) { return [...this.groups.values()].map(g => ({...g, customData: this.customData.get(g.groupId)})) } @@ -187,13 +216,15 @@ const GROK_LOCAL_GROUP_ID = 200 const CUSTOMER_ID = "customer-1" // Commands passed into SupportBot; matches what index.ts constructs when -// Grok is enabled. Tests that disable grokApi still pass the full list -// because the ctor doesn't care; the value is pushed to a group's -// groupPreferences on the first sendToGroup() call. +// Grok is enabled. The ctor uses this to decide which `/keyword` messages +// from customers are commands vs. plain text — tests that disable grokApi +// should pass a list that excludes "grok" to mirror production wiring (see +// index.ts where `grokEnabled` gates that entry). const DESIRED_COMMANDS = [ {type: "command" as const, keyword: "grok", label: "Ask Grok"}, {type: "command" as const, keyword: "team", label: "Switch to team"}, ] +const DESIRED_COMMANDS_NO_GROK = [DESIRED_COMMANDS[1]] // ─── Member factories ─── @@ -638,7 +669,7 @@ describe("/grok Activation", () => { await joinPromise await bot.flush() expectMemberAdded(CUSTOMER_GROUP_ID, GROK_CONTACT_ID) - expectSentToGroup(CUSTOMER_GROUP_ID, "You are chatting with Grok") + expectSentToGroup(CUSTOMER_GROUP_ID, grokActivatedMessage) }) test("/grok as first message → WELCOME→GROK directly, no queue message", async () => { @@ -646,7 +677,7 @@ describe("/grok Activation", () => { await bot.onNewChatItems(customerMessage("/grok")) await joinPromise await bot.flush() - expectSentToGroup(CUSTOMER_GROUP_ID, "You are chatting with Grok") + expectSentToGroup(CUSTOMER_GROUP_ID, grokActivatedMessage) expectNotSentToGroup(CUSTOMER_GROUP_ID, "The team will reply to your message") expect(chat.sentTo(TEAM_GROUP_ID).length).toBeGreaterThan(0) }) @@ -654,7 +685,7 @@ describe("/grok Activation", () => { test("/grok in TEAM → rejected with teamLockedMessage", async () => { await reachTeam() await bot.onNewChatItems(customerMessage("/grok")) - expectSentToGroup(CUSTOMER_GROUP_ID, "team mode") + expectSentToGroup(CUSTOMER_GROUP_ID, teamLockedMessage) }) test("/grok when grokContactId is null → grokUnavailableMessage", async () => { @@ -704,6 +735,28 @@ describe("Grok Conversation", () => { expect(grokApi.calls.length).toBe(0) }) + test("Grok answers messages containing a slash mid-word", async () => { + // Regression: an unanchored regex in ciBotCommand once parsed `/read` + // inside "follow/read" as a command, causing Grok to skip the message. + grokApi.willRespond("We post on X and Mastodon.") + await bot.onGrokNewChatItems(grokViewCustomerMessage( + "What social media do you use? Anything I can follow/read for updates?" + )) + expect(grokApi.calls.length).toBe(1) + expect(grokApi.calls[0].message).toBe( + "What social media do you use? Anything I can follow/read for updates?" + ) + }) + + test("Grok answers an unknown slash-prefixed message", async () => { + // `/help` is not in desiredCommands, so it should be treated as plain + // text and reach Grok rather than being silently dropped. + grokApi.willRespond("Sure, here's what I can do.") + await bot.onGrokNewChatItems(grokViewCustomerMessage("/help me with groups")) + expect(grokApi.calls.length).toBe(1) + expect(grokApi.calls[0].message).toBe("/help me with groups") + }) + test("Grok per-message: history includes prior Grok sent response as assistant", async () => { addCustomerMessageToHistory("How do I create a group?", GROK_LOCAL_GROUP_ID) addBotMessage("To create a group, tap + then New Group.", GROK_LOCAL_GROUP_ID) @@ -841,6 +894,52 @@ describe("Grok Conversation", () => { }) }) +describe("Grok requests /team", () => { + beforeEach(() => setup()) + + test("Grok per-message reply containing /team → team added, teamAddedMessage sent, reply still sent", async () => { + await reachGrok() + await bot.flush() + grokApi.willRespond("I can't help with billing — please send /team for a human.") + addCustomerMessageToHistory("Can you refund me?", GROK_LOCAL_GROUP_ID) + await bot.onGrokNewChatItems(grokViewCustomerMessage("Can you refund me?")) + + expectAnySent("I can't help with billing") + expectMemberAdded(CUSTOMER_GROUP_ID, TEAM_MEMBER_1_ID) + expectMemberAdded(CUSTOMER_GROUP_ID, TEAM_MEMBER_2_ID) + expectSentToGroup(CUSTOMER_GROUP_ID, "We will reply within") + }) + + test("Grok per-message reply without /team → no team members added", async () => { + await reachGrok() + await bot.flush() + grokApi.willRespond("To create a group, tap +, then New Group.") + addCustomerMessageToHistory("How do I create a group?", GROK_LOCAL_GROUP_ID) + await bot.onGrokNewChatItems(grokViewCustomerMessage("How do I create a group?")) + + expect(chat.added.some(a => a.groupId === CUSTOMER_GROUP_ID && a.contactId === TEAM_MEMBER_1_ID)).toBe(false) + }) + + test("/team in Grok's initial reply after /grok → escalates", async () => { + await reachQueue() + addBotMessage("The team will reply to your message") + // Customer's question visible in Grok's view → activateGrok reads it for the initial reply + chat.groups.set(GROK_LOCAL_GROUP_ID, makeGroupInfo(GROK_LOCAL_GROUP_ID)) + addCustomerMessageToHistory("I'm really stuck, please help", GROK_LOCAL_GROUP_ID) + grokApi.willRespond("That sounds urgent — send /team to reach a person.") + + const grokJoinPromise = simulateGrokJoinSuccess() + await bot.onNewChatItems(customerMessage("/grok")) + await grokJoinPromise + await bot.flush() + + expectAnySent("That sounds urgent") + expectMemberAdded(CUSTOMER_GROUP_ID, TEAM_MEMBER_1_ID) + expectMemberAdded(CUSTOMER_GROUP_ID, TEAM_MEMBER_2_ID) + expectSentToGroup(CUSTOMER_GROUP_ID, "We will reply within") + }) +}) + describe("/team Activation", () => { beforeEach(() => setup()) @@ -864,7 +963,7 @@ describe("/team Activation", () => { addBotMessage("We will reply within 24 hours.") chat.members.set(CUSTOMER_GROUP_ID, [makeTeamMember(TEAM_MEMBER_1_ID, "Alice")]) await bot.onNewChatItems(customerMessage("/team")) - expectSentToGroup(CUSTOMER_GROUP_ID, "already been invited") + expectSentToGroup(CUSTOMER_GROUP_ID, teamAlreadyInvitedMessage) }) test("/team with no team members → noTeamMembersMessage", async () => { @@ -898,7 +997,7 @@ describe("One-Way Gate", () => { test("/grok after gate → teamLockedMessage", async () => { await reachTeam() await bot.onNewChatItems(customerMessage("/grok")) - expectSentToGroup(CUSTOMER_GROUP_ID, "team mode") + expectSentToGroup(CUSTOMER_GROUP_ID, teamLockedMessage) }) test("customer text in TEAM → card update scheduled, no bot reply", async () => { @@ -948,6 +1047,17 @@ describe("One-Way Gate with Grok Disabled", () => { // Grok should not respond (grokApi is null) expect(grokApi.calls.length).toBe(0) }) + + test("Grok disabled: customer /grok is treated as text and queued", async () => { + // When Grok is disabled, index.ts excludes "grok" from desiredCommands, + // so /grok from a customer parses as an unknown command → routed as + // plain text → first-message-in-WELCOME transitions to QUEUE. + setup() + bot = new SupportBot(chat as any, null, config as any, MAIN_USER_ID, null, DESIRED_COMMANDS_NO_GROK) + bot.cards = cards + await bot.onNewChatItems(customerMessage("/grok")) + expectSentToGroup(CUSTOMER_GROUP_ID, "The team will reply to your message") + }) }) describe("Team Member Lifecycle", () => { @@ -1456,7 +1566,7 @@ describe("Error Handling", () => { // Only the "Inviting Grok" message is sent — no activated/unavailable result expect(chat.sent.length).toBe(sentBefore + 1) expectSentToGroup(CUSTOMER_GROUP_ID, "Inviting Grok") - expectNotSentToGroup(CUSTOMER_GROUP_ID, "You are chatting with Grok") + expectNotSentToGroup(CUSTOMER_GROUP_ID, grokActivatedMessage) expectNotSentToGroup(CUSTOMER_GROUP_ID, "temporarily unavailable") }) @@ -1646,7 +1756,7 @@ describe("Grok Join Flow", () => { await bot.flush() expect(chat.joined).toContain(GROK_LOCAL_GROUP_ID) - expectSentToGroup(CUSTOMER_GROUP_ID, "You are chatting with Grok") + expectSentToGroup(CUSTOMER_GROUP_ID, grokActivatedMessage) }) test("per-message responses suppressed during activateGrok initial response", async () => { @@ -1794,7 +1904,7 @@ describe("End-to-End Flows", () => { await joinPromise await bot.flush() - expectSentToGroup(CUSTOMER_GROUP_ID, "You are chatting with Grok") + expectSentToGroup(CUSTOMER_GROUP_ID, grokActivatedMessage) expectNotSentToGroup(CUSTOMER_GROUP_ID, "The team will reply to your message") expect(chat.sentTo(TEAM_GROUP_ID).length).toBeGreaterThan(0) }) @@ -1834,8 +1944,8 @@ describe("Message Templates", () => { expect(grokActivatedMessage).toContain("chatting with Grok") }) - test("teamLockedMessage mentions team mode", () => { - expect(teamLockedMessage).toContain("team mode") + test("teamLockedMessage tells customer the team will handle the conversation", () => { + expect(teamLockedMessage).toContain("team") }) test("queueMessage mentions hours", () => { @@ -2402,7 +2512,7 @@ describe("GrokApiClient HTTP timeout", () => { new Response(JSON.stringify({choices: [{message: {content: "ok"}}]}), {status: 200}), ) - const client = new GrokApiClient("test-key", "system prompt") + const client = new GrokApiClient("test-key", [{role: "system", content: "system prompt"}]) await client.chat([], "hello") expect(timeoutSpy).toHaveBeenCalledWith(60_000) @@ -2479,3 +2589,118 @@ describe("Command sync in sendToGroup", () => { expect(prefs.reactions).toEqual({enable: "on"}) }) }) + +// loadGrokContext: documented behavior is "plain text → single system +// message". A `.yaml` / `.yml` extension is an undocumented alternative +// that parses the harness transcript format and surfaces only `system` +// and `assistant` turns; `user` entries are dropped so they don't merge +// with the customer's runtime message. +describe("loadGrokContext", () => { + const dir = mkdtempSync(join(tmpdir(), "support-bot-context-")) + const writeFile = (name: string, content: string): string => { + const p = join(dir, name) + writeFileSync(p, content) + return p + } + + test("plain text (.txt) → single system message with full file content", () => { + const path = writeFile("ctx.txt", "You are Grok.\n\nBe concise.") + expect(loadGrokContext(path)).toEqual([ + {role: "system", content: "You are Grok.\n\nBe concise."}, + ]) + }) + + test("no extension → treated as plain text", () => { + const path = writeFile("plain", "raw context") + expect(loadGrokContext(path)).toEqual([{role: "system", content: "raw context"}]) + }) + + test(".md → treated as plain text (does not look like YAML)", () => { + const path = writeFile("ctx.md", "# Heading\n\nbody") + expect(loadGrokContext(path)).toEqual([ + {role: "system", content: "# Heading\n\nbody"}, + ]) + }) + + test(".yaml → parses transcript and keeps only system + assistant turns", () => { + const path = writeFile("ctx.yaml", + "- role: system\n message: Be terse.\n" + + "- role: user\n message: What is async?\n" + + "- role: assistant\n message: Cooperative concurrency.\n", + ) + expect(loadGrokContext(path)).toEqual([ + {role: "system", content: "Be terse."}, + {role: "assistant", content: "Cooperative concurrency."}, + ]) + }) + + test(".yml extension also triggers YAML parsing", () => { + const path = writeFile("ctx.yml", + "- role: system\n message: hi\n", + ) + expect(loadGrokContext(path)).toEqual([{role: "system", content: "hi"}]) + }) + + test("YAML parsing is case-insensitive on extension", () => { + const path = writeFile("ctx.YAML", + "- role: system\n message: hi\n", + ) + expect(loadGrokContext(path)).toEqual([{role: "system", content: "hi"}]) + }) + + test("YAML preserves multi-line literal block scalars verbatim", () => { + const path = writeFile("multiline.yaml", + "- role: assistant\n message: |\n line one\n line two\n", + ) + expect(loadGrokContext(path)).toEqual([ + {role: "assistant", content: "line one\nline two\n"}, + ]) + }) + + test("YAML with only user-role entries → empty array", () => { + const path = writeFile("only-user.yaml", + "- role: user\n message: a\n" + + "- role: user\n message: b\n", + ) + expect(loadGrokContext(path)).toEqual([]) + }) + + test("empty YAML file → empty array", () => { + const path = writeFile("empty.yaml", "") + expect(loadGrokContext(path)).toEqual([]) + }) + + test("YAML non-list top level throws", () => { + const path = writeFile("not-list.yaml", "role: system\nmessage: x\n") + expect(() => loadGrokContext(path)).toThrow(/top-level must be a list/) + }) + + test("YAML entry with unknown role throws", () => { + const path = writeFile("bad-role.yaml", "- role: bogus\n message: x\n") + expect(() => loadGrokContext(path)).toThrow(/entry 0 has invalid role/) + }) + + test("YAML entry missing role throws", () => { + const path = writeFile("no-role.yaml", "- message: x\n") + expect(() => loadGrokContext(path)).toThrow(/entry 0 has invalid role/) + }) + + test("YAML entry with non-string message throws", () => { + const path = writeFile("bad-message.yaml", "- role: user\n message: 42\n") + expect(() => loadGrokContext(path)).toThrow(/entry 0 has non-string message/) + }) + + test("YAML entry that is not a mapping throws", () => { + const path = writeFile("bad-entry.yaml", "- just a string\n- role: user\n message: x\n") + expect(() => loadGrokContext(path)).toThrow(/entry 0 is not a mapping/) + }) + + test("malformed YAML throws", () => { + const path = writeFile("malformed.yaml", "- role: user\n message: [unclosed\n") + expect(() => loadGrokContext(path)).toThrow(/failed to parse YAML/) + }) + + test("missing file throws ENOENT", () => { + expect(() => loadGrokContext(join(dir, "does-not-exist.yaml"))).toThrow() + }) +}) diff --git a/apps/simplex-support-bot/package-lock.json b/apps/simplex-support-bot/package-lock.json index 1569c18309..eddbcb2dff 100644 --- a/apps/simplex-support-bot/package-lock.json +++ b/apps/simplex-support-bot/package-lock.json @@ -9,10 +9,11 @@ "version": "0.1.0", "license": "AGPL-3.0", "dependencies": { - "@simplex-chat/types": "^0.5.0", + "@simplex-chat/types": "^0.6.0", "async-mutex": "^0.5.0", "commander": "^14.0.3", - "simplex-chat": "^6.5.0" + "simplex-chat": "^6.5.1", + "yaml": "^2.8.4" }, "devDependencies": { "@types/node": "^22.0.0", @@ -782,9 +783,9 @@ ] }, "node_modules/@simplex-chat/types": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/@simplex-chat/types/-/types-0.5.0.tgz", - "integrity": "sha512-f680CRlf+O8WfIaPb7wxVj3PB8mTIOE+HqmetCSe0NBheVAjU3ovg3+zkrWwDlavrHuCLbb7Gmeu4HyNtjDfog==", + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/@simplex-chat/types/-/types-0.6.0.tgz", + "integrity": "sha512-QVYvaRsS6TnS+IROjNkekZYvhvy8QoA8vKCuGe9E6lplXDVutJo9tdDOSWS9NDdtwxT1wRZ29zN4xEZEEG/NHw==", "license": "AGPL-3.0", "dependencies": { "typescript": "^5.9.2" @@ -1675,13 +1676,13 @@ } }, "node_modules/simplex-chat": { - "version": "6.5.0", - "resolved": "https://registry.npmjs.org/simplex-chat/-/simplex-chat-6.5.0.tgz", - "integrity": "sha512-QFGI734HhYJ7trSrEKiZ2mbodI0V8CLDGEv2+yt5zsg0FqftxSpFik6zUSezTRZtN1M8WmSlT44qlEt2a1fXQw==", + "version": "6.5.1", + "resolved": "https://registry.npmjs.org/simplex-chat/-/simplex-chat-6.5.1.tgz", + "integrity": "sha512-1cv91iMCqtP+9R1PQM3NKazocoUInKT2/06pIOuzORD5/VzulR6cMZnEQmaT02Jz+8FVkEKTsaAIJfVP6/tJmw==", "hasInstallScript": true, "license": "AGPL-3.0", "dependencies": { - "@simplex-chat/types": "^0.5.0", + "@simplex-chat/types": "^0.6.0", "extract-zip": "^2.0.1", "fast-deep-equal": "^3.1.3", "node-addon-api": "^8.5.0" @@ -1995,6 +1996,21 @@ "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", "license": "ISC" }, + "node_modules/yaml": { + "version": "2.8.4", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.8.4.tgz", + "integrity": "sha512-ml/JPOj9fOQK8RNnWojA67GbZ0ApXAUlN2UQclwv2eVgTgn7O9gg9o7paZWKMp4g0H3nTLtS9LVzhkpOFIKzog==", + "license": "ISC", + "bin": { + "yaml": "bin.mjs" + }, + "engines": { + "node": ">= 14.6" + }, + "funding": { + "url": "https://github.com/sponsors/eemeli" + } + }, "node_modules/yauzl": { "version": "2.10.0", "resolved": "https://registry.npmjs.org/yauzl/-/yauzl-2.10.0.tgz", diff --git a/apps/simplex-support-bot/package.json b/apps/simplex-support-bot/package.json index ee57762465..8541056aa5 100644 --- a/apps/simplex-support-bot/package.json +++ b/apps/simplex-support-bot/package.json @@ -8,10 +8,11 @@ "start": "node dist/index.js" }, "dependencies": { - "@simplex-chat/types": "^0.5.0", + "@simplex-chat/types": "^0.6.0", "async-mutex": "^0.5.0", "commander": "^14.0.3", - "simplex-chat": "^6.5.0" + "simplex-chat": "^6.5.1", + "yaml": "^2.8.4" }, "devDependencies": { "@types/node": "^22.0.0", diff --git a/apps/simplex-support-bot/src/bot.ts b/apps/simplex-support-bot/src/bot.ts index 9bfb44d93d..9b534381de 100644 --- a/apps/simplex-support-bot/src/bot.ts +++ b/apps/simplex-support-bot/src/bot.ts @@ -8,7 +8,23 @@ import { teamAlreadyInvitedMessage, teamLockedMessage, noTeamMembersMessage, grokUnavailableMessage, grokErrorMessage, grokNoHistoryMessage, } from "./messages.js" -import {profileMutex, log, logError} from "./util.js" +import {profileMutex, log, logError, getGroupInfo} from "./util.js" + +// Collects the keyword of every "command" entry in the bot's registered +// commands tree, descending into "menu" entries. Used to distinguish real +// commands from arbitrary text that happens to start with `/` (e.g. URLs, +// "/help" the user invented). +function commandKeywords(commands: T.ChatBotCommand[]): Set { + const out = new Set() + const visit = (cmds: T.ChatBotCommand[]): void => { + for (const c of cmds) { + if (c.type === "command") out.add(c.keyword) + else if (c.type === "menu") visit(c.commands) + } + } + visit(commands) + return out +} // True for any non-terminal status — invited but not yet accepted, through // connected. Used to decide whether a contact is already in the group so we @@ -62,6 +78,11 @@ export class SupportBot { // send to each group. private syncedGroups = new Set() + // Keywords from desiredCommands. A customer message is treated as a + // command only when its parsed keyword is in this set; anything else + // (URLs, "/help", arbitrary slashes) is routed as plain text. + private readonly customerKeywords: ReadonlySet + constructor( private chat: api.ChatApi, private grokApi: GrokApiClient | null, @@ -71,6 +92,12 @@ export class SupportBot { private desiredCommands: T.ChatBotCommand[], ) { this.cards = new CardManager(chat, config, mainUserId, config.cardFlushSeconds * 1000) + this.customerKeywords = commandKeywords(desiredCommands) + } + + private customerCommand(chatItem: T.ChatItem): util.BotCommand | undefined { + const cmd = util.ciBotCommand(chatItem) + return cmd && this.customerKeywords.has(cmd.keyword) ? cmd : undefined } private get grokEnabled(): boolean { @@ -357,7 +384,7 @@ export class SupportBot { if (chatInfo.type !== "group") continue if (chatItem.chatDir.type !== "groupRcv") continue if (!util.ciContentText(chatItem)?.trim()) continue - if (util.ciBotCommand(chatItem)) continue + if (this.customerCommand(chatItem)) continue const bc = chatInfo.groupInfo.businessChat if (!bc) continue if (chatItem.chatDir.groupMember.memberId !== bc.customerId) continue @@ -444,9 +471,7 @@ export class SupportBot { // 8. Customer message → derive state and dispatch const state = await this.cards.deriveState(groupId) - const rawCmd = util.ciBotCommand(chatItem) - // When Grok is disabled, ignore /grok so it behaves like an unknown command - const cmd = rawCmd?.keyword === "grok" && !this.grokEnabled ? null : rawCmd + const cmd = this.customerCommand(chatItem) const text = util.ciContentText(chatItem)?.trim() || null switch (state) { @@ -547,7 +572,7 @@ export class SupportBot { if (!text) return // ignore non-text // Ignore bot commands - if (util.ciBotCommand(chatItem)) return + if (this.customerCommand(chatItem)) return // Only respond in business groups (survives restart without in-memory maps) const bc = groupInfo.businessChat @@ -569,7 +594,7 @@ export class SupportBot { history.push({role: "assistant", content: histText}) } else if (histCi.chatDir.type === "groupRcv" && histCi.chatDir.groupMember.memberId === bc.customerId - && !util.ciBotCommand(histCi)) { + && !this.customerCommand(histCi)) { history.push({role: "user", content: histText}) } } @@ -587,6 +612,9 @@ export class SupportBot { await this.withGrokProfile(() => this.chat.apiSendTextMessage([T.ChatType.Group, grokGroupId], response) ) + + // Grok asked for the team → escalate as if the customer sent /team + if (mainGroupId !== undefined && response.includes("/team")) await this.activateTeam(mainGroupId) } catch (err) { logError(`Grok per-message error for grokGroup ${grokGroupId}`, err) try { @@ -706,7 +734,7 @@ export class SupportBot { if (ci.chatDir.type !== "groupRcv") continue if (!grokBc || ci.chatDir.groupMember.memberId !== grokBc.customerId) continue const t = util.ciContentText(ci)?.trim() - if (t && !util.ciBotCommand(ci)) customerMessages.push(t) + if (t && !this.customerCommand(ci)) customerMessages.push(t) } if (customerMessages.length === 0) { @@ -722,6 +750,9 @@ export class SupportBot { await this.withGrokProfile(() => this.chat.apiSendTextMessage([T.ChatType.Group, grokLocalGId], response) ) + + // Grok asked for the team → escalate as if the customer sent /team + if (response.includes("/team")) await this.activateTeam(groupId) } catch (err) { logError(`Grok initial response failed for group ${groupId}`, err) await this.sendToGroup(groupId, grokUnavailableMessage) @@ -795,10 +826,7 @@ export class SupportBot { private async handleJoinCommand(targetGroupId: number, senderContactId: number): Promise { // Validate target is a business group - const groups = await this.withMainProfile(() => - this.chat.apiListGroups(this.mainUserId) - ) - const targetGroup = groups.find(g => g.groupId === targetGroupId) + const targetGroup = await this.withMainProfile(() => getGroupInfo(this.chat, targetGroupId)) if (!targetGroup?.businessChat) { await this.sendToGroup(this.config.teamGroup.id, `Error: group ${targetGroupId} is not a business chat`) return diff --git a/apps/simplex-support-bot/src/cards.ts b/apps/simplex-support-bot/src/cards.ts index 3d27c036e9..feea986551 100644 --- a/apps/simplex-support-bot/src/cards.ts +++ b/apps/simplex-support-bot/src/cards.ts @@ -2,7 +2,7 @@ import {T} from "@simplex-chat/types" import {api, util} from "simplex-chat" import {Mutex} from "async-mutex" import {Config} from "./config.js" -import {profileMutex, log, logError} from "./util.js" +import {profileMutex, log, logError, getGroupInfo} from "./util.js" // State derivation types export type ConversationState = "WELCOME" | "QUEUE" | "GROK" | "TEAM-PENDING" | "TEAM" @@ -117,8 +117,7 @@ export class CardManager { // Dispatches to create-path when cardItemId is absent so a failed createCard retries. private async flushOne(groupId: number): Promise { - const groups = await this.withMainProfile(() => this.chat.apiListGroups(this.mainUserId)) - const groupInfo = groups.find(g => g.groupId === groupId) + const groupInfo = await this.withMainProfile(() => getGroupInfo(this.chat, groupId)) if (!groupInfo) return const data = groupInfo.customData as Record | undefined if (typeof data?.cardItemId === "number") { @@ -129,12 +128,22 @@ export class CardManager { } async refreshAllCards(): Promise { - const groups = await this.withMainProfile(() => this.chat.apiListGroups(this.mainUserId)) + // Scan the most recently active 1000 chats. Active cards live on + // recently-active customer chats by definition — a card stays open + // while the conversation is in flight. If the bot has been offline + // long enough that an active card has fallen outside this window, the + // card refreshes lazily on the next customer message (which moves the + // chat back into the recent window). + const chats = await this.withMainProfile(() => + this.chat.apiGetChats(this.mainUserId, {type: "last", count: 1000}) + ) const activeCards: {groupId: number; cardItemId: number}[] = [] - for (const group of groups) { - const customData = group.customData as Record | undefined + for (const c of chats) { + if (c.chatInfo.type !== "group") continue + const groupInfo = c.chatInfo.groupInfo + const customData = groupInfo.customData as Record | undefined if (customData && typeof customData.cardItemId === "number" && !customData.complete) { - activeCards.push({groupId: group.groupId, cardItemId: customData.cardItemId}) + activeCards.push({groupId: groupInfo.groupId, cardItemId: customData.cardItemId}) } } if (activeCards.length === 0) return @@ -210,8 +219,7 @@ export class CardManager { // --- Custom data --- async getRawCustomData(groupId: number): Promise | null> { - const groups = await this.withMainProfile(() => this.chat.apiListGroups(this.mainUserId)) - const group = groups.find(g => g.groupId === groupId) + const group = await this.withMainProfile(() => getGroupInfo(this.chat, groupId)) if (!group?.customData) return null const data = group.customData as Record const result: Partial = {} @@ -247,9 +255,7 @@ export class CardManager { // --- Internal --- private async updateCard(groupId: number): Promise { - // Read customData and groupInfo in one apiListGroups call - const groups = await this.withMainProfile(() => this.chat.apiListGroups(this.mainUserId)) - const groupInfo = groups.find(g => g.groupId === groupId) + const groupInfo = await this.withMainProfile(() => getGroupInfo(this.chat, groupId)) if (!groupInfo) return const customData = groupInfo.customData as Record | undefined diff --git a/apps/simplex-support-bot/src/context.ts b/apps/simplex-support-bot/src/context.ts new file mode 100644 index 0000000000..81f30117e9 --- /dev/null +++ b/apps/simplex-support-bot/src/context.ts @@ -0,0 +1,59 @@ +import {readFileSync} from "fs" +import {parse as parseYaml} from "yaml" +import {GrokMessage} from "./grok.js" + +const ALLOWED_ROLES: ReadonlySet = new Set(["system", "user", "assistant"]) +// Roles surfaced from a YAML transcript. `user` entries from the file are +// validated but dropped — the customer's runtime message is the only +// `user` content sent to Grok. +const PREPEND_ROLES: ReadonlySet = new Set(["system", "assistant"]) + +// Loads --context-file. The flag is documented as "text file with Grok +// system context"; a `.yaml` / `.yml` extension is an undocumented +// alternative that switches to a multi-turn transcript in the harness +// format (a flat list of `{role, message}` entries). +export function loadGrokContext(path: string): GrokMessage[] { + const text = readFileSync(path, "utf-8") + return isYamlPath(path) ? parseYamlTranscript(path, text) : [{role: "system", content: text}] +} + +function isYamlPath(path: string): boolean { + const lower = path.toLowerCase() + return lower.endsWith(".yaml") || lower.endsWith(".yml") +} + +// Parses the harness transcript format. Returns only `system` and +// `assistant` turns; `user` entries are intentionally excluded so they +// don't merge with the customer's runtime message. Malformed YAML, +// unknown roles, or non-string messages throw — operator-supplied +// configuration should fail-fast at startup, not silently degrade. +function parseYamlTranscript(path: string, text: string): GrokMessage[] { + let raw: unknown + try { + raw = parseYaml(text) + } catch (e) { + throw new Error(`${path}: failed to parse YAML: ${(e as Error).message}`) + } + if (raw === null || raw === undefined) return [] + if (!Array.isArray(raw)) { + throw new Error(`${path}: top-level must be a list, got ${typeof raw}`) + } + const context: GrokMessage[] = [] + for (let i = 0; i < raw.length; i++) { + const entry = raw[i] + if (entry === null || typeof entry !== "object" || Array.isArray(entry)) { + throw new Error(`${path}: entry ${i} is not a mapping`) + } + const {role, message} = entry as {role?: unknown; message?: unknown} + if (typeof role !== "string" || !ALLOWED_ROLES.has(role as GrokMessage["role"])) { + throw new Error(`${path}: entry ${i} has invalid role: ${JSON.stringify(role)}`) + } + if (typeof message !== "string") { + throw new Error(`${path}: entry ${i} has non-string message`) + } + if (PREPEND_ROLES.has(role as GrokMessage["role"])) { + context.push({role: role as GrokMessage["role"], content: message}) + } + } + return context +} diff --git a/apps/simplex-support-bot/src/grok.ts b/apps/simplex-support-bot/src/grok.ts index b03108439a..967f20902c 100644 --- a/apps/simplex-support-bot/src/grok.ts +++ b/apps/simplex-support-bot/src/grok.ts @@ -7,11 +7,11 @@ export interface GrokMessage { export class GrokApiClient { private readonly apiKey: string - private readonly systemPrompt: string + private readonly initialContext: readonly GrokMessage[] - constructor(apiKey: string, systemPrompt: string) { + constructor(apiKey: string, initialContext: readonly GrokMessage[]) { this.apiKey = apiKey - this.systemPrompt = systemPrompt + this.initialContext = initialContext } async chatRaw(messages: GrokMessage[]): Promise { @@ -22,7 +22,7 @@ export class GrokApiClient { "Authorization": `Bearer ${this.apiKey}`, }, body: JSON.stringify({ - model: "grok-3-mini", + model: "grok-latest", messages, temperature: 0.3, max_tokens: 1024, @@ -45,9 +45,9 @@ export class GrokApiClient { } async chat(history: GrokMessage[], userMessage: string): Promise { - log(`Grok API call: ${history.length} history msgs, user msg ${userMessage.length} chars`) + log(`Grok API call: ${this.initialContext.length} context msgs, ${history.length} history msgs, user msg ${userMessage.length} chars`) return this.chatRaw([ - {role: "system", content: this.systemPrompt}, + ...this.initialContext, ...history, {role: "user", content: userMessage}, ]) diff --git a/apps/simplex-support-bot/src/index.ts b/apps/simplex-support-bot/src/index.ts index cc1dd0538c..c99b1f5842 100644 --- a/apps/simplex-support-bot/src/index.ts +++ b/apps/simplex-support-bot/src/index.ts @@ -3,9 +3,10 @@ import {api, bot, util} from "simplex-chat" import {T} from "@simplex-chat/types" import {parseConfig} from "./config.js" import {SupportBot} from "./bot.js" -import {GrokApiClient} from "./grok.js" +import {GrokApiClient, GrokMessage} from "./grok.js" +import {loadGrokContext} from "./context.js" import {welcomeMessage} from "./messages.js" -import {profileMutex, log, logError} from "./util.js" +import {profileMutex, log, logError, getGroupInfo, getContact} from "./util.js" interface BotState { teamGroupId?: number @@ -163,14 +164,12 @@ async function main(): Promise { await chat.apiSetAutoAcceptMemberContacts(mainUser.userId, true) log("Auto-accept member contacts enabled") - // Step 5: List contacts, resolve Grok contact - const contacts = await chat.apiListContacts(mainUser.userId) - log(`Contacts connected: ${contacts.length || "(none)"}`) - + // Step 5: Resolve Grok contact by ID. Avoid apiListContacts — it loads + // every contact in one response and OOMs the native binding on large DBs. // Always restore grokContactId so the one-way gate can find and remove // Grok members even when Grok API is disabled. if (typeof state.grokContactId === "number") { - const found = contacts.find(c => c.contactId === state.grokContactId) + const found = await getContact(chat, state.grokContactId) if (found) { config.grokContactId = found.contactId log(`Grok contact from state: ID=${config.grokContactId}`) @@ -210,14 +209,13 @@ async function main(): Promise { } } - // Step 6: Resolve team group + // Step 6: Resolve team group by ID. Avoid apiListGroups — it loads every + // group in one response and OOMs the native binding on large DBs. log("Resolving team group...") - const groups = await chat.apiListGroups(mainUser.userId) - - let existingGroup: T.GroupInfo | undefined + let existingGroup: T.GroupInfo | null = null if (typeof state.teamGroupId === "number") { - existingGroup = groups.find(g => g.groupId === state.teamGroupId) + existingGroup = await getGroupInfo(chat, state.teamGroupId) if (existingGroup) { config.teamGroup.id = existingGroup.groupId log(`Team group from state: ${config.teamGroup.id}:${existingGroup.groupProfile.displayName}`) @@ -302,13 +300,13 @@ async function main(): Promise { inviteLinkTimer.unref() } - // Step 9: Validate team members + // Step 9: Validate team members (lookup by ID, one round-trip per member) if (config.teamMembers.length > 0) { log("Validating team members...") for (const member of config.teamMembers) { - const contact = contacts.find(c => c.contactId === member.id) + const contact = await getContact(chat, member.id) if (!contact) { - console.error(`Team member not found: ID=${member.id}. Available: ${contacts.map(c => `${c.contactId}:${c.profile.displayName}`).join(", ") || "(none)"}`) + console.error(`Team member not found: ID=${member.id}`) process.exit(1) } if (contact.profile.displayName !== member.name) { @@ -322,16 +320,22 @@ async function main(): Promise { // Load Grok context and build API client only if enabled let grokApi: GrokApiClient | null = null if (grokEnabled) { - let contextFile = "" + let initialContext: GrokMessage[] = [] if (config.contextFile) { try { - contextFile = readFileSync(config.contextFile, "utf-8") - log(`Loaded Grok context: ${contextFile.length} chars from ${config.contextFile}`) - } catch { - log(`Warning: context file not found: ${config.contextFile}`) + initialContext = loadGrokContext(config.contextFile) + log(`Loaded Grok context: ${initialContext.length} message(s) from ${config.contextFile}`) + } catch (err) { + const e = err as NodeJS.ErrnoException + if (e.code === "ENOENT") { + log(`Warning: context file not found: ${config.contextFile}`) + } else { + logError(`Failed to load Grok context file ${config.contextFile}`, err) + throw err + } } } - grokApi = new GrokApiClient(config.grokApiKey!, contextFile) + grokApi = new GrokApiClient(config.grokApiKey!, initialContext) } // Create SupportBot diff --git a/apps/simplex-support-bot/src/util.ts b/apps/simplex-support-bot/src/util.ts index 288a48d673..f9a2319610 100644 --- a/apps/simplex-support-bot/src/util.ts +++ b/apps/simplex-support-bot/src/util.ts @@ -1,7 +1,36 @@ import {Mutex} from "async-mutex" +import {api, core} from "simplex-chat" +import {T} from "@simplex-chat/types" export const profileMutex = new Mutex() +export function isChatNotFound(err: unknown, kind: "group" | "contact"): boolean { + if (!(err instanceof core.ChatAPIError)) return false + if (err.chatError?.type !== "errorStore") return false + const seType = err.chatError.storeError.type + return kind === "group" ? seType === "groupNotFound" : seType === "contactNotFound" +} + +export async function getGroupInfo(chat: api.ChatApi, groupId: number): Promise { + try { + const c = await chat.apiGetChat(T.ChatType.Group, groupId, 0) + return c.chatInfo.type === "group" ? c.chatInfo.groupInfo : null + } catch (err) { + if (isChatNotFound(err, "group")) return null + throw err + } +} + +export async function getContact(chat: api.ChatApi, contactId: number): Promise { + try { + const c = await chat.apiGetChat(T.ChatType.Direct, contactId, 0) + return c.chatInfo.type === "direct" ? c.chatInfo.contact : null + } catch (err) { + if (isChatNotFound(err, "contact")) return null + throw err + } +} + export function isWeekend(timezone: string): boolean { const day = new Intl.DateTimeFormat("en-US", {timeZone: timezone, weekday: "short"}).format(new Date()) return day === "Sat" || day === "Sun" diff --git a/apps/simplex-support-bot/test/__mocks__/simplex-chat.js b/apps/simplex-support-bot/test/__mocks__/simplex-chat.js index 64c9246f27..92e05a4178 100644 --- a/apps/simplex-support-bot/test/__mocks__/simplex-chat.js +++ b/apps/simplex-support-bot/test/__mocks__/simplex-chat.js @@ -9,7 +9,7 @@ function ciContentText(chatItem) { function ciBotCommand(chatItem) { const text = ciContentText(chatItem)?.trim() if (text) { - const r = text.match(/\/([^\s]+)(.*)/) + const r = text.match(/^\/([^\s]+)(.*)/) if (r && r.length >= 3) return {keyword: r[1], params: r[2].trim()} } return undefined @@ -19,8 +19,18 @@ function contactAddressStr(link) { return link.connShortLink || link.connFullLink } +// Mirrors core.ChatAPIError so isChatNotFound's instanceof check passes when +// MockChatApi throws. Tests should construct these directly. +class ChatAPIError extends Error { + constructor(message, chatError) { + super(message) + this.chatError = chatError + } +} + module.exports = { api: {ChatApi: {}}, bot: {}, + core: {ChatAPIError}, util: {ciContentText, ciBotCommand, contactAddressStr}, } diff --git a/blog/20260430-simplex-channels-v6-5-consortium-crowdfunding-freedom-of-speech.md b/blog/20260430-simplex-channels-v6-5-consortium-crowdfunding-freedom-of-speech.md index 64ebc03b2e..4a63cb87ca 100644 --- a/blog/20260430-simplex-channels-v6-5-consortium-crowdfunding-freedom-of-speech.md +++ b/blog/20260430-simplex-channels-v6-5-consortium-crowdfunding-freedom-of-speech.md @@ -52,7 +52,7 @@ We've seen open-source privacy-focussed projects die without funding, or worse & So we're building both: a governance structure and a real business. The governance protects the network neutrality. The commercial model funds the network and makes our and other businesses on the network profitable, ensuring their independence. Neither works without the other. -We recently published [a preliminary design of commercial model](https://simplex.chat/vouchers/) — private Community Credits that fund servers, development, and governance without surveillance or speculation. The full investment case will be published when crowdfunding launches. +We recently published [a preliminary design of commercial model](https://simplex.chat/credits/) — private Community Credits that fund servers, development, and governance without surveillance or speculation. The full investment case will be published when crowdfunding launches. You can *register your interest* to participate in crowdfunding here: https://simplexchat.typeform.com/crowdfunding diff --git a/bots/api/COMMANDS.md b/bots/api/COMMANDS.md index 5ca2c4260a..2d804ccaa9 100644 --- a/bots/api/COMMANDS.md +++ b/bots/api/COMMANDS.md @@ -32,6 +32,7 @@ This file is generated automatically. - [APINewGroup](#apinewgroup) - [APINewPublicGroup](#apinewpublicgroup) - [APIGetGroupRelays](#apigetgrouprelays) +- [APIAddGroupRelays](#apiaddgrouprelays) - [APIUpdateGroupProfile](#apiupdategroupprofile) [Group link commands](#group-link-commands) @@ -51,6 +52,7 @@ This file is generated automatically. [Chat commands](#chat-commands) - [APIListContacts](#apilistcontacts) - [APIListGroups](#apilistgroups) +- [APIGetChats](#apigetchats) - [APIDeleteChat](#apideletechat) - [APISetGroupCustomData](#apisetgroupcustomdata) - [APISetContactCustomData](#apisetcontactcustomdata) @@ -293,7 +295,7 @@ Send messages. ``` ```python -'/_send ' + str(sendRef) + (' live=on' if liveMessage else '') + ((' ttl=' + str(ttl)) if ttl is not None else '') + ' json ' + json.dumps(composedMessages) # Python +'/_send ' + ChatRef_cmd_string(sendRef) + (' live=on' if liveMessage else '') + ((' ttl=' + str(ttl)) if ttl is not None else '') + ' json ' + json.dumps(composedMessages) # Python ``` **Responses**: @@ -333,7 +335,7 @@ Update message. ``` ```python -'/_update item ' + str(chatRef) + ' ' + str(chatItemId) + (' live=on' if liveMessage else '') + ' json ' + json.dumps(updatedMessage) # Python +'/_update item ' + ChatRef_cmd_string(chatRef) + ' ' + str(chatItemId) + (' live=on' if liveMessage else '') + ' json ' + json.dumps(updatedMessage) # Python ``` **Responses**: @@ -372,7 +374,7 @@ Delete message. **Syntax**: ``` -/_delete item [,...] broadcast|internal|internalMark +/_delete item [,...] broadcast|internal|internalMark|history ``` ```javascript @@ -380,7 +382,7 @@ Delete message. ``` ```python -'/_delete item ' + str(chatRef) + ' ' + ','.join(map(str, chatItemIds)) + ' ' + str(deleteMode) # Python +'/_delete item ' + ChatRef_cmd_string(chatRef) + ' ' + ','.join(map(str, chatItemIds)) + ' ' + str(deleteMode) # Python ``` **Responses**: @@ -462,7 +464,7 @@ Add/remove message reaction. ``` ```python -'/_reaction ' + str(chatRef) + ' ' + str(chatItemId) + ' ' + ('on' if add else 'off') + ' ' + json.dumps(reaction) # Python +'/_reaction ' + ChatRef_cmd_string(chatRef) + ' ' + str(chatItemId) + ' ' + ('on' if add else 'off') + ' ' + json.dumps(reaction) # Python ``` **Responses**: @@ -1033,6 +1035,51 @@ ChatCmdError: Command error (only used in WebSockets API). --- +### APIAddGroupRelays + +Add relays to group. + +*Network usage*: interactive. + +**Parameters**: +- groupId: int64 +- relayIds: [int64] + +**Syntax**: + +``` +/_add relays # [,...] +``` + +```javascript +'/_add relays #' + groupId + ' ' + relayIds.join(',') // JavaScript +``` + +```python +'/_add relays #' + str(groupId) + ' ' + ','.join(map(str, relayIds)) # Python +``` + +**Responses**: + +GroupRelaysAdded: Group relays added. +- type: "groupRelaysAdded" +- user: [User](./TYPES.md#user) +- groupInfo: [GroupInfo](./TYPES.md#groupinfo) +- groupLink: [GroupLink](./TYPES.md#grouplink) +- groupRelays: [[GroupRelay](./TYPES.md#grouprelay)] + +GroupRelaysAddFailed: Group relays add failed. +- type: "groupRelaysAddFailed" +- user: [User](./TYPES.md#user) +- addRelayResults: [[AddRelayResult](./TYPES.md#addrelayresult)] + +ChatCmdError: Command error (only used in WebSockets API). +- type: "chatCmdError" +- chatError: [ChatError](./TYPES.md#chaterror) + +--- + + ### APIUpdateGroupProfile Update group profile. @@ -1339,7 +1386,7 @@ Connect via prepared SimpleX link. The link can be 1-time invitation link, conta ``` ```python -'/_connect ' + str(userId) + ((' ' + str(preparedLink_)) if preparedLink_ is not None else '') # Python +'/_connect ' + str(userId) + ((' ' + CreatedConnLink_cmd_string(preparedLink_)) if preparedLink_ is not None else '') # Python ``` **Responses**: @@ -1574,6 +1621,46 @@ ChatCmdError: Command error (only used in WebSockets API). --- +### APIGetChats + +Get chat previews. Supports time-based pagination — use this instead of APIListContacts / APIListGroups when scanning at scale (those load every record into memory and fail on large databases). + +*Network usage*: no. + +**Parameters**: +- userId: int64 +- pendingConnections: bool +- pagination: [PaginationByTime](./TYPES.md#paginationbytime) +- query: [ChatListQuery](./TYPES.md#chatlistquery) + +**Syntax**: + +``` +/_get chats [ pcc=on] +``` + +```javascript +'/_get chats ' + userId + (pendingConnections ? ' pcc=on' : '') + ' ' + PaginationByTime.cmdString(pagination) + ' ' + JSON.stringify(query) // JavaScript +``` + +```python +'/_get chats ' + str(userId) + (' pcc=on' if pendingConnections else '') + ' ' + PaginationByTime_cmd_string(pagination) + ' ' + json.dumps(query) # Python +``` + +**Responses**: + +ApiChats: Chat previews (paginated). Use this instead of CRContactsList / CRGroupsList when scanning at scale.. +- type: "apiChats" +- user: [User](./TYPES.md#user) +- chats: [[AChat](./TYPES.md#achat)] + +ChatCmdError: Command error (only used in WebSockets API). +- type: "chatCmdError" +- chatError: [ChatError](./TYPES.md#chaterror) + +--- + + ### APIDeleteChat Delete chat. @@ -1595,7 +1682,7 @@ Delete chat. ``` ```python -'/_delete ' + str(chatRef) + ' ' + str(chatDeleteMode) # Python +'/_delete ' + ChatRef_cmd_string(chatRef) + ' ' + ChatDeleteMode_cmd_string(chatDeleteMode) # Python ``` **Responses**: diff --git a/bots/api/TYPES.md b/bots/api/TYPES.md index ceb38939b1..4bd924dc31 100644 --- a/bots/api/TYPES.md +++ b/bots/api/TYPES.md @@ -41,6 +41,7 @@ This file is generated automatically. - [ChatInfo](#chatinfo) - [ChatItem](#chatitem) - [ChatItemDeletion](#chatitemdeletion) +- [ChatListQuery](#chatlistquery) - [ChatPeerType](#chatpeertype) - [ChatRef](#chatref) - [ChatSettings](#chatsettings) @@ -136,6 +137,7 @@ This file is generated automatically. - [NewUser](#newuser) - [NoteFolder](#notefolder) - [OwnerVerification](#ownerverification) +- [PaginationByTime](#paginationbytime) - [PendingContactConnection](#pendingcontactconnection) - [PrefEnabled](#prefenabled) - [Preferences](#preferences) @@ -587,6 +589,7 @@ ChatBanner: - "broadcast" - "internal" - "internalMark" +- "history" --- @@ -1327,6 +1330,22 @@ Message deletion result. - toChatItem: [AChatItem](#achatitem)? +--- + +## ChatListQuery + +**Discriminated union type**: + +Filters: +- type: "filters" +- favorite: bool +- unread: bool + +Search: +- type: "search" +- search: string + + --- ## ChatPeerType @@ -1358,7 +1377,7 @@ ChatType.cmdString(chatType) + chatId + (chatScope ? GroupChatScope.cmdString(ch ``` ```python -str(chatType) + str(chatId) + ((str(chatScope)) if chatScope is not None else '') # Python +ChatType_cmd_string(chatType) + str(chatId) + ((GroupChatScope_cmd_string(chatScope)) if chatScope is not None else '') # Python ``` @@ -2893,6 +2912,31 @@ Failed: - reason: string +--- + +## PaginationByTime + +**Discriminated union type**: + +Last: +- type: "last" +- count: int + +**Syntax**: + +``` +count= +``` + +```javascript +'count=' + count // JavaScript +``` + +```python +'count=' + str(count) # Python +``` + + --- ## PendingContactConnection diff --git a/bots/src/API/Docs/Commands.hs b/bots/src/API/Docs/Commands.hs index ae8ce7c05b..b3eaf96837 100644 --- a/bots/src/API/Docs/Commands.hs +++ b/bots/src/API/Docs/Commands.hs @@ -119,6 +119,7 @@ chatCommandsDocsData = ("APINewGroup", [], "Create group.", ["CRGroupCreated", "CRChatCmdError"], [], Nothing, "/_group " <> Param "userId" <> OnOffParam "incognito" "incognito" (Just False) <> " " <> Json "groupProfile"), ("APINewPublicGroup", [], "Create public group.", ["CRPublicGroupCreated", "CRPublicGroupCreationFailed", "CRChatCmdError"], [], Just UNInteractive, "/_public group " <> Param "userId" <> OnOffParam "incognito" "incognito" (Just False) <> " " <> Join ',' "relayIds" <> " " <> Json "groupProfile"), ("APIGetGroupRelays", [], "Get group relays.", ["CRGroupRelays", "CRChatCmdError"], [], Nothing, "/_get relays #" <> Param "groupId"), + ("APIAddGroupRelays", [], "Add relays to group.", ["CRGroupRelaysAdded", "CRGroupRelaysAddFailed", "CRChatCmdError"], [], Just UNInteractive, "/_add relays #" <> Param "groupId" <> " " <> Join ',' "relayIds"), ("APIUpdateGroupProfile", [], "Update group profile.", ["CRGroupUpdated", "CRChatCmdError"], [], Just UNBackground, "/_group_profile #" <> Param "groupId" <> " " <> Json "groupProfile") ] ), @@ -133,6 +134,7 @@ chatCommandsDocsData = ( "Connection commands", "These commands may be used to create connections. Most bots do not need to use them - bot users will connect via bot address with auto-accept enabled.", [ ("APIAddContact", [], "Create 1-time invitation link.", ["CRInvitation", "CRChatCmdError"], [], Just UNInteractive, "/_connect " <> Param "userId" <> OnOffParam "incognito" "incognito" (Just False)), + -- `Maybe` in `connectionLink :: Maybe AConnectionLink` is used to signal link parsing error to the runtime (the handler returns CEInvalidConnReq on Nothing); it is NOT API-level optionality. The parameter is required from callers. ("APIConnectPlan", [], "Determine SimpleX link type and if the bot is already connected via this link.", ["CRConnectionPlan", "CRChatCmdError"], [], Just UNInteractive, "/_connect plan " <> Param "userId" <> " " <> Param "connectionLink"), ("APIConnect", [], "Connect via prepared SimpleX link. The link can be 1-time invitation link, contact address or group link.", ["CRSentConfirmation", "CRContactAlreadyExists", "CRSentInvitation", "CRChatCmdError"], [], Just UNInteractive, "/_connect " <> Param "userId" <> Optional "" (" " <> Param "$0") "preparedLink_"), ("Connect", [], "Connect via SimpleX link as string in the active user profile.", ["CRSentConfirmation", "CRContactAlreadyExists", "CRSentInvitation", "CRChatCmdError"], [], Just UNInteractive, "/connect" <> Optional "" (" " <> Param "$0") "connLink_"), @@ -144,6 +146,7 @@ chatCommandsDocsData = "Commands to list and delete conversations.", [ ("APIListContacts", [], "Get contacts.", ["CRContactsList", "CRChatCmdError"], [], Nothing, "/_contacts " <> Param "userId"), ("APIListGroups", [], "Get groups.", ["CRGroupsList", "CRChatCmdError"], [], Nothing, "/_groups " <> Param "userId" <> Optional "" (" @" <> Param "$0") "contactId_" <> Optional "" (" " <> Param "$0") "search"), + ("APIGetChats", [], "Get chat previews. Supports time-based pagination — use this instead of APIListContacts / APIListGroups when scanning at scale (those load every record into memory and fail on large databases).", ["CRApiChats", "CRChatCmdError"], [], Nothing, "/_get chats " <> Param "userId" <> OnOffParam "pcc" "pendingConnections" (Just False) <> " " <> Param "pagination" <> " " <> Json "query"), ("APIDeleteChat", [], "Delete chat.", ["CRContactDeleted", "CRContactConnectionDeleted", "CRGroupDeletedUser", "CRChatCmdError"], [], Just UNBackground, "/_delete " <> Param "chatRef" <> " " <> Param "chatDeleteMode"), ("APISetGroupCustomData", [], "Set group custom data.", ["CRCmdOk", "CRChatCmdError"], [], Nothing, "/_set custom #" <> Param "groupId" <> Optional "" (" " <> Json "$0") "customData"), ("APISetContactCustomData", [], "Set contact custom data.", ["CRCmdOk", "CRChatCmdError"], [], Nothing, "/_set custom @" <> Param "contactId" <> Optional "" (" " <> Json "$0") "customData"), @@ -357,7 +360,6 @@ undocumentedCommands = "APIGetChatItemInfo", "APIGetChatItems", "APIGetChatItemTTL", - "APIGetChats", "APIGetChatTags", "APIGetConnNtfMessages", "APIGetContactCode", diff --git a/bots/src/API/Docs/Generate.hs b/bots/src/API/Docs/Generate.hs index 99886bf222..8bc4cbe6ee 100644 --- a/bots/src/API/Docs/Generate.hs +++ b/bots/src/API/Docs/Generate.hs @@ -74,7 +74,7 @@ syntaxText r syntax = "\n**Syntax**:\n" <> "\n```\n" <> docSyntaxText r syntax <> "\n```\n" <> (if isConst syntax then "" else "\n```javascript\n" <> jsSyntaxText False "" r syntax <> " // JavaScript\n```\n") - <> (if isConst syntax then "" else "\n```python\n" <> pySyntaxText r syntax <> " # Python\n```\n") + <> (if isConst syntax then "" else "\n```python\n" <> pySyntaxText "" r syntax <> " # Python\n```\n") camelToSpace :: String -> String camelToSpace [] = [] diff --git a/bots/src/API/Docs/Generate/Python.hs b/bots/src/API/Docs/Generate/Python.hs new file mode 100644 index 0000000000..a144aa4376 --- /dev/null +++ b/bots/src/API/Docs/Generate/Python.hs @@ -0,0 +1,322 @@ +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} + +module API.Docs.Generate.Python where + +import API.Docs.Commands +import API.Docs.Events +import API.Docs.Generate +import API.Docs.Responses +import API.Docs.Syntax +import API.Docs.Syntax.Types +import API.Docs.Types +import API.TypeInfo +import Data.Char (isAlphaNum, toUpper) +import qualified Data.List.NonEmpty as L +import Data.Text (Text) +import qualified Data.Text as T + +commandsCodeFile :: FilePath +commandsCodeFile = "./packages/simplex-chat-python/src/simplex_chat/types/_commands.py" + +responsesCodeFile :: FilePath +responsesCodeFile = "./packages/simplex-chat-python/src/simplex_chat/types/_responses.py" + +eventsCodeFile :: FilePath +eventsCodeFile = "./packages/simplex-chat-python/src/simplex_chat/types/_events.py" + +typesCodeFile :: FilePath +typesCodeFile = "./packages/simplex-chat-python/src/simplex_chat/types/_types.py" + +-- | Replace dashes with underscores so Python identifiers stay valid. +pyIdent :: String -> Text +pyIdent = T.replace "-" "_" . T.pack + +-- | Python class name for a union member tag. +pyConstrName :: String -> Text +pyConstrName = pyIdent . fstToUpper + +commandsCodeText :: Text +commandsCodeText = + ("# API Commands\n# " <> autoGenerated <> "\n") + <> "from __future__ import annotations\n" + <> "import json\n" + <> "from typing import NotRequired, TypedDict\n" + <> "from . import _types as T\n" + <> "from . import _responses as CR\n" + <> foldMap commandCatCode chatCommandsDocs + where + commandCatCode CCCategory {categoryName, categoryDescr, commands} = + (T.pack $ "\n# " <> categoryName <> "\n# " <> categoryDescr <> "\n") + <> foldMap commandCode commands + where + commandCode CCDoc {commandType = ATUnionMember tag params, commandDescr, syntax, responses, network} = + ("\n# " <> commandDescr <> "\n") + <> ("# Network usage: " <> networkUsage network <> ".\n") + <> classDef + <> (if syntax == "" then "" else cmdStringFunc) + <> respAliasLine + where + constrName = T.pack $ fstToUpper tag + classDef = + ("class " <> constrName <> "(TypedDict):\n") + <> bodyOrPass (fieldsCodePy " " "T." params) + <> "\n" + cmdStringFunc = + ("\ndef " <> constrName <> "_cmd_string(self: " <> constrName <> ") -> str:\n") + <> " return " <> pySelfSyntaxText "T." (fstToUpper tag, params) syntax <> "\n" + respAliasLine = + "\n" <> constrName <> "_Response = " <> respUnion <> "\n" + respUnion = unionAliasRhs "" (responseRef . responseType) responses + responseRef (ATUnionMember rtag _) = "CR." <> pyConstrName rtag + +responsesCodeText :: Text +responsesCodeText = + ("# API Responses\n# " <> autoGenerated <> "\n") + <> pythonImports + <> unionTypeCodePy moduleMember "T." "ChatResponse" chatRespConstrs + where + chatRespConstrs = L.fromList $ map responseType chatResponsesDocs + +eventsCodeText :: Text +eventsCodeText = + ("# API Events\n# " <> autoGenerated <> "\n") + <> pythonImports + <> unionTypeCodePy moduleMember "T." "ChatEvent" chatEventConstrs + where + chatEventConstrs = L.fromList $ concatMap catEvents chatEventsDocs + catEvents CECategory {mainEvents, otherEvents} = map eventType $ mainEvents ++ otherEvents + +typesCodeText :: Text +typesCodeText = + ("# API Types\n# " <> autoGenerated <> "\n") + <> "from __future__ import annotations\n" + <> "from typing import Literal, NotRequired, TypedDict\n" + <> foldMap typeCode chatTypesDocs + where + typeCode ctd@CTDoc {typeDef = APITypeDef {typeName' = name, typeDef}, typeDescr} = + (if T.null typeDescr then "" else "\n# " <> typeDescr <> "\n") + <> typeDefCode + <> typeCmdStringCode ctd + where + name' = T.pack name + enumValue m = case name of + "ConnectionMode" -> map toUpper m + "FileProtocol" -> map toUpper m + _ -> m + typeDefCode = case typeDef of + ATDRecord fields -> + ("\nclass " <> name' <> "(TypedDict):\n") + <> bodyOrPass (fieldsCodePy " " "" fields) + ATDEnum cs -> + "\n" <> name' <> " = Literal[" + <> T.intercalate ", " (map (\m -> "\"" <> T.pack (enumValue m) <> "\"") $ L.toList cs) + <> "]\n" + ATDUnion cs -> unionTypeCodePy typeMember "" name cs + +-- | For types with non-empty `typeSyntax`, emit a top-level +-- `_cmd_string(self: ) -> str` helper that mirrors the +-- Choice/Param expression. Records access fields via `self['']`; +-- enums and unions dispatch on `self` (a literal string) or `self['type']` +-- respectively. Required so generated `_commands.py` produces valid CLI +-- syntax for ChatRef/ChatType/ChatDeleteMode/GroupChatScope/PaginationByTime +-- params instead of stringifying the wire dict. +typeCmdStringCode :: CTDoc -> Text +typeCmdStringCode CTDoc {typeDef = td@APITypeDef {typeName' = name, typeDef}, typeSyntax} + | typeSyntax == "" = "" + | otherwise = + "\n\ndef " <> T.pack name <> "_cmd_string(self: " <> T.pack name <> ") -> str:\n" + <> " return " <> body <> ignore <> "\n" + where + body = pyTypeSyntaxText "" (name, fields) typeSyntax + -- Unions and enums use self/self['type'] to dispatch. Pyright cannot + -- narrow TypedDict access by string-literal key, so suppress per-branch + -- complaints with one ignore on the return. + ignore = case typeDef of + ATDUnion _ -> " # type: ignore[typeddict-item]" + _ -> "" + -- typeFields mirrors TS funcCode: include `self` so Choice "self" + -- resolves; for unions add `type` and flatten member fields. + self = APIRecordField "self" (ATDef td) + fields = case typeDef of + ATDRecord fs -> fs + ATDUnion ms -> + self : APIRecordField "type" tagType : concatMap (\(ATUnionMember _ fs) -> fs) (L.toList ms) + where + tagType = ATDef $ APITypeDef (name <> ".type") $ ATDEnum tags + tags = L.map (\(ATUnionMember tag _) -> tag) ms + ATDEnum _ -> [self] + +-- | Like `pySelfSyntaxText` but excludes `self` from the param-rewrite list +-- so `self == 'tag'` (enum dispatch) and `self['type']` (union dispatch) +-- survive verbatim. Used only for type-level cmd_string functions inside +-- @_types.py@, where peer type cmd_string calls don't need a namespace. +pyTypeSyntaxText :: String -> TypeAndFields -> Expr -> Text +pyTypeSyntaxText typeNamespace r expr = + rewriteParams accessors (pySyntaxText typeNamespace r expr) + where + accessors = filter ((/= "self") . fst) (paramAccessors r) + +-- | Member class name within the multi-type @_types.py@ module: prefix the +-- tag with the union type name so members from different unions don't +-- collide. +typeMember :: String -> String -> Text +typeMember typeName tag = T.pack typeName <> "_" <> pyIdent tag + +-- | Member class name within a single-union module (responses/events): just +-- the PascalCase tag, so commands can reference them as @CR.@. +moduleMember :: String -> String -> Text +moduleMember _ tag = pyConstrName tag + +-- | Common imports for the responses/events modules. +pythonImports :: Text +pythonImports = + "from __future__ import annotations\n" + <> "from typing import Literal, NotRequired, TypedDict\n" + <> "from . import _types as T\n" + +-- | Render a tagged-union type: one TypedDict per member, plus union alias +-- and `_Tag` Literal alias. The member class names are produced by +-- @memberName@ given the union type name and the member tag. +unionTypeCodePy :: + (String -> String -> Text) -> + Text -> + String -> + L.NonEmpty ATUnionMember -> + Text +unionTypeCodePy memberName typesNamespace name cs = + foldMap memberClass (L.toList cs) + <> "\n" <> name' <> " = " <> unionAliasRhs name' constrTypeRef (L.toList cs) + <> "\n" <> name' <> "_Tag = Literal[" <> tagLiterals <> "]\n" + where + name' = T.pack name + constrTypeRef (ATUnionMember tag _) = memberName name tag + tagLiterals = T.intercalate ", " $ map (\(ATUnionMember tag _) -> "\"" <> T.pack tag <> "\"") $ L.toList cs + memberClass (ATUnionMember tag fields) = + ("\nclass " <> memberName name tag <> "(TypedDict):\n") + <> (" type: Literal[\"" <> T.pack tag <> "\"]\n") + <> fieldsCodePy " " typesNamespace fields + +-- | Render the right-hand side of a union alias: either inline (one line) or +-- multi-line wrapped in parentheses with `|` separators between alternatives. +unionAliasRhs :: Text -> (a -> Text) -> [a] -> Text +unionAliasRhs lhs constr cs + | T.length (lhs <> " = " <> oneLine) <= 100 = oneLine <> "\n" + | otherwise = "(\n" <> T.intercalate "\n" (map (" " <>) lines') <> "\n)\n" + where + oneLine = T.intercalate " | " cs' + lines' = case cs' of + [] -> [] + (h : t) -> h : map ("| " <>) t + cs' = map constr cs + +-- | Emit a body of `pass` if there are no fields, otherwise the rendered +-- fields as-is. +bodyOrPass :: Text -> Text +bodyOrPass body + | T.null body = " pass\n" + | otherwise = body + +-- | Render record fields for a TypedDict body. Each field becomes +-- `: [ # ]`. Optional fields wrap the type in +-- `NotRequired[...]`. +fieldsCodePy :: Text -> Text -> [APIRecordField] -> Text +fieldsCodePy indent namespace = foldMap render + where + render (APIRecordField name t) = + indent <> T.pack name <> ": " <> wrapOptional t (typeText t) <> typeComment t <> "\n" + wrapOptional t inner = case t of + ATOptional _ -> "NotRequired[" <> inner <> "]" + _ -> inner + typeText = \case + ATPrim (PT t) -> primName t + ATDef (APITypeDef t _) -> quoted (namespace <> T.pack t) + ATRef t -> quoted (namespace <> T.pack t) + ATOptional t -> typeText t + ATArray {elemType} -> "list[" <> typeText elemType <> "]" + ATMap (PT k) v -> "dict[" <> primName k <> ", " <> typeText v <> "]" + primName = \case + TBool -> "bool" + TString -> "str" + TInt -> "int" + TInt64 -> "int" + TWord32 -> "int" + TDouble -> "float" + TJSONObject -> "dict[str, object]" + TUTCTime -> "str" + t -> T.pack t + quoted s = "\"" <> s <> "\"" + typeComment t = let c = typeComment' t in if T.null c then "" else " # " <> c + typeComment' = \case + ATPrim (PT t) -> typeComment_ t + ATOptional inner -> typeComment' inner + ATArray {elemType, nonEmpty} + | nonEmpty -> if T.null c then "non-empty" else c <> ", non-empty" + | otherwise -> c + where + c = typeComment' elemType + ATMap (PT k) v -> + let kc = typeComment_ k + vc = typeComment' v + tc t c = if T.null c then t else c + in if T.null kc && T.null vc then "" else tc (primName k) kc <> " : " <> tc (typeText v) vc + _ -> "" + typeComment_ = \case + TInt -> "int" + TInt64 -> "int64" + TWord32 -> "word32" + TDouble -> "double" + TUTCTime -> "ISO-8601 timestamp" + _ -> "" + +-- | Wrap `pySyntaxText` so each parameter access uses `self['']`. The +-- output of `pySyntaxText` references params as bare Python identifiers +-- (e.g. `str(userId)`); we rewrite those identifiers — but only outside +-- string literals — into TypedDict subscript accesses. The +-- @typeNamespace@ is prepended to any `_cmd_string(...)` calls +-- emitted for params whose type has its own syntax (e.g. @"T."@ from +-- @_commands.py@, or @""@ from within @_types.py@). +-- +-- Unlike the JS variant, we do NOT collapse adjacent string literals via +-- `T.replace "' + '" ""`: that pattern incorrectly matches `' ' + ','` +-- (the space-then-comma sequence between a literal and `','.join(...)`), +-- producing `' ,'.join(...)` which uses ` ,` as the join separator and +-- swallows the leading space. The `intercalate " + "` output is correct +-- without further string fixups. +pySelfSyntaxText :: String -> TypeAndFields -> Expr -> Text +pySelfSyntaxText typeNamespace r expr = + rewriteParams (paramAccessors r) (pySyntaxText typeNamespace r expr) + +-- | Map field name to the Python access expression: `self['']` for +-- required fields, `self.get('')` for optional ones (since +-- TypedDict's `NotRequired` allows the key to be absent and `[...]` would +-- raise `KeyError`). Used by the rewriter so the same name is substituted +-- consistently in Optional `is not None` checks and in the value position. +paramAccessors :: TypeAndFields -> [(String, String)] +paramAccessors (_, fields) = map mk fields + where + mk (APIRecordField n t) = (n, accessor n t) + accessor n = \case + ATOptional _ -> "self.get('" ++ n ++ "')" + _ -> "self['" ++ n ++ "']" + +-- | Replace bare identifiers (matching a key in @accessors@) with the +-- corresponding accessor expression, skipping characters inside +-- single-quoted string literals and respecting identifier word boundaries. +rewriteParams :: [(String, String)] -> Text -> Text +rewriteParams accessors = T.pack . go False . T.unpack + where + go _ [] = [] + -- Toggle in/out of single-quoted string on every unescaped quote. + go inStr ('\'' : rest) = '\'' : go (not inStr) rest + go True (c : rest) = c : go True rest + go False s@(c : rest) + | isIdentStart c = case takeIdent s of + (ident, after) -> case lookup ident accessors of + Just expr -> expr ++ go False after + Nothing -> ident ++ go False after + | otherwise = c : go False rest + isIdentStart c = isAlphaNum c || c == '_' + takeIdent = span (\c -> isAlphaNum c || c == '_') diff --git a/bots/src/API/Docs/Responses.hs b/bots/src/API/Docs/Responses.hs index c3ab85ece6..55f12f0a0a 100644 --- a/bots/src/API/Docs/Responses.hs +++ b/bots/src/API/Docs/Responses.hs @@ -71,6 +71,8 @@ chatResponsesDocsData = ("CRPublicGroupCreated", ""), ("CRPublicGroupCreationFailed", ""), ("CRGroupRelays", ""), + ("CRGroupRelaysAdded", ""), + ("CRGroupRelaysAddFailed", ""), ("CRGroupMembers", ""), ("CRGroupUpdated", ""), ("CRGroupsList", "Groups"), @@ -95,9 +97,9 @@ chatResponsesDocsData = ("CRUserDeletedMembers", "Members deleted"), ("CRUserProfileUpdated", "User profile updated"), ("CRUserProfileNoChange", "User profile was not changed"), - ("CRUsersList", "Users") + ("CRUsersList", "Users"), + ("CRApiChats", "Chat previews (paginated). Use this instead of CRContactsList / CRGroupsList when scanning at scale.") -- ("CRApiChat", "Chat and messages"), - -- ("CRApiChats", "Chats with the most recent messages"), -- ("CRChatCleared", ""), -- ("CRChatItemInfo", "Message information"), -- ("CRChatItems", "The most recent messages"), @@ -120,7 +122,6 @@ undocumentedResponses = "CRAgentWorkersDetails", "CRAgentWorkersSummary", "CRApiChat", - "CRApiChats", "CRAppSettings", "CRArchiveExported", "CRArchiveImported", diff --git a/bots/src/API/Docs/Syntax.hs b/bots/src/API/Docs/Syntax.hs index f96ec03b02..64c848f310 100644 --- a/bots/src/API/Docs/Syntax.hs +++ b/bots/src/API/Docs/Syntax.hs @@ -157,8 +157,8 @@ escapeChar c s | c `elem` s = concatMap (\c' -> if c' == c then ['\\', c] else [c]) s | otherwise = s -pySyntaxText :: TypeAndFields -> Expr -> Text -pySyntaxText r = T.pack . go Nothing True +pySyntaxText :: String -> TypeAndFields -> Expr -> Text +pySyntaxText typeNamespace r = T.pack . go Nothing True where go param top = \case Concat exs -> intercalate " + " $ map (go param False) $ L.toList exs @@ -167,7 +167,13 @@ pySyntaxText r = T.pack . go Nothing True withParamType r param p $ \case ATPrim (PT TString) -> paramName param p ATOptional (ATPrim (PT TString)) -> paramName param p + ATDef td -> toStringSyntax td + ATOptional (ATDef td) -> toStringSyntax td _ -> "str(" <> paramName param p <> ")" + where + toStringSyntax (APITypeDef typeName _) + | typeHasSyntax typeName = typeNamespace <> typeName <> "_cmd_string(" <> paramName param p <> ")" + | otherwise = "str(" <> paramName param p <> ")" Optional exN exJ p -> open <> "(" <> go (Just p) False exJ <> ") if " <> n <> " is not None else " <> nothing <> close where n = paramName param p diff --git a/bots/src/API/Docs/Types.hs b/bots/src/API/Docs/Types.hs index 933858c5cc..be4a55835a 100644 --- a/bots/src/API/Docs/Types.hs +++ b/bots/src/API/Docs/Types.hs @@ -374,11 +374,11 @@ chatTypesDocsData = (sti @UserPwdHash, STRecord, "", [], "", ""), (sti @XFTPErrorType, STUnion, "", [], "", ""), (sti @XFTPRcvFile, STRecord, "", [], "", ""), - (sti @XFTPSndFile, STRecord, "", [], "", "") + (sti @XFTPSndFile, STRecord, "", [], "", ""), -- (sti @DatabaseError, STUnion, "DB", [], "", ""), -- (sti @ChatItemInfo, STRecord, "", [], "", ""), -- (sti @ChatItemVersion, STRecord, "", [], "", ""), - -- (sti @ChatListQuery, STUnion, "CLQ", [], "", ""), + (sti @ChatListQuery, STUnion, "CLQ", [], "", ""), -- (sti @ChatName, STRecord, "", [], "", ""), -- (sti @ChatPagination, STRecord, "CP", [], "", ""), -- (sti @ConnectionStats, STRecord, "", [], "", ""), @@ -387,7 +387,10 @@ chatTypesDocsData = -- (sti @MemberReaction, STRecord, "", [], "", ""), -- (sti @MsgContentTag, (STEnum' $ dropPfxSfx "MC" '_'), "", ["MCUnknown_"], "", ""), -- (sti @NavigationInfo, STRecord, "", [], "", ""), - -- (sti @PaginationByTime, STRecord, "", [], "", ""), + -- PTAfter / PTBefore are hidden — bots only need "tail last N chats". + -- The wire format is parsed by paginationByTimeP in + -- src/Simplex/Chat/Library/Commands.hs. + (sti @PaginationByTime, STUnion1, "PT", ["PTAfter", "PTBefore"], "count=" <> Param "count", "") -- (sti @RcvQueueInfo, STRecord, "", [], "", ""), -- (sti @RcvSwitchStatus, STEnum, "", [], "", ""), -- incorrect -- (sti @SendRef, STRecord, "", [], "", ""), @@ -589,7 +592,7 @@ deriving instance Generic XFTPSndFile -- deriving instance Generic DatabaseError -- deriving instance Generic ChatItemInfo -- deriving instance Generic ChatItemVersion --- deriving instance Generic ChatListQuery +deriving instance Generic ChatListQuery -- deriving instance Generic ChatName -- deriving instance Generic ChatPagination -- deriving instance Generic ConnectionStats @@ -599,7 +602,7 @@ deriving instance Generic XFTPSndFile -- deriving instance Generic MemberReaction -- deriving instance Generic MsgContentTag -- deriving instance Generic NavigationInfo --- deriving instance Generic PaginationByTime +deriving instance Generic PaginationByTime -- deriving instance Generic RcvQueueInfo -- deriving instance Generic RcvSwitchStatus -- deriving instance Generic SendRef diff --git a/docs/DONATIONS.md b/docs/DONATIONS.md index b3046ee90b..f7a6e61d7b 100644 --- a/docs/DONATIONS.md +++ b/docs/DONATIONS.md @@ -11,6 +11,8 @@ We are prioritizing users' privacy and security - it would be impossible without Our pledge to our users is that SimpleX protocols are and will remain open, and in public domain, - so anybody can build the future implementations of the clients and the servers. We are building SimpleX platform based on the same principles as email and web, but much more private and secure. +To ensure network independence and neutrality, we are currently finalizing the launch of [SimpleX Network Consortium](https://simplexnetwork.org/) - an agreement between SimpleX Network Foundation that is being formed as 501.c3 non-profit and SimpleX Chat company. + Your donations help us raise more funds - any amount, even the price of the cup of coffee, makes a big difference for us. Please donate via: @@ -25,6 +27,6 @@ Thank you, Evgeny, SimpleX Chat founder -## SimpleX Community Vouchers +## SimpleX Community Credits -Please comment on our plan to make SimpleX network sustainable and get a free access pass (an NFT) for early testing: https://simplex.chat/vouchers +Please comment on our plan to make SimpleX network sustainable: https://simplex.chat/credits diff --git a/docs/protocol/channels-protocol.md b/docs/protocol/channels-protocol.md index 979ab2c85b..b6b9b3ee5b 100644 --- a/docs/protocol/channels-protocol.md +++ b/docs/protocol/channels-protocol.md @@ -9,6 +9,7 @@ For architecture, design rationale, security properties, and threat model, see [ - [Protocol](#protocol) - [Channel creation](#channel-creation) - [Relay acceptance](#relay-acceptance) + - [Relay addition](#relay-addition) - [Subscriber connection](#subscriber-connection) - [Message signing](#message-signing) - [Message forwarding](#message-forwarding) @@ -57,6 +58,20 @@ When a relay receives an invitation to serve a channel, it validates the channel TODO: Periodic monitoring where the relay retrieves channel link data to verify its relay link is still listed is planned but not yet implemented. +### Relay addition + +When the owner adds a relay to an existing channel: + +1. **Acceptance.** The new relay accepts the invitation following the [Relay acceptance](#relay-acceptance) flow. The owner promotes the relay to active when the channel link's updated relay list is confirmed. + +2. **Announce.** If the channel has at least one subscriber, the owner sends `x.grp.relay.new` (carrying the new relay's short link) to every other currently-connected relay of the channel. + +3. **Forward.** Each relay forwards `x.grp.relay.new` to its subscribers. The relay does not create a member record for the announced relay — relays do not connect to other relays of the same channel. + +4. **Connect.** On receipt, the subscriber resolves the announced short link and connects to the new relay asynchronously. + +The announce is an optimisation. When it does not reach a subscriber — because the channel had no subscribers at announce time, because an older client or relay sits in the path, or because of a transient network failure — the subscriber reaches the same end state on the next channel open via its relay sync against the channel's link data. + ### Subscriber connection A subscriber joins a channel through the following flow: @@ -89,6 +104,7 @@ Messages that alter the channel's roster, profile, or administrative state are c | `x.grp.mem.del` | Remove member | Required | | `x.grp.mem.role` | Change member role | Required | | `x.grp.mem.restrict` | Restrict member | Required | +| `x.grp.relay.new` | Announce new relay to subscribers | Required | | `x.grp.leave` | Leave channel | Required (unverified allowed between subscribers) | | `x.info` | Update member profile | Required (unverified allowed between subscribers) | | `x.msg.new` | Content message | Not signed | diff --git a/packages/simplex-chat-client/types/typescript/package.json b/packages/simplex-chat-client/types/typescript/package.json index 329003a6b8..1d5eb5197c 100644 --- a/packages/simplex-chat-client/types/typescript/package.json +++ b/packages/simplex-chat-client/types/typescript/package.json @@ -1,6 +1,6 @@ { "name": "@simplex-chat/types", - "version": "0.5.0", + "version": "0.6.0", "description": "TypeScript types for SimpleX Chat bot libraries", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/packages/simplex-chat-client/types/typescript/src/commands.ts b/packages/simplex-chat-client/types/typescript/src/commands.ts index 9c5c31ceb2..f8aa6e445d 100644 --- a/packages/simplex-chat-client/types/typescript/src/commands.ts +++ b/packages/simplex-chat-client/types/typescript/src/commands.ts @@ -372,6 +372,21 @@ export namespace APIGetGroupRelays { } } +// Add relays to group. +// Network usage: interactive. +export interface APIAddGroupRelays { + groupId: number // int64 + relayIds: number[] // int64, non-empty +} + +export namespace APIAddGroupRelays { + export type Response = CR.GroupRelaysAdded | CR.GroupRelaysAddFailed | CR.ChatCmdError + + export function cmdString(self: APIAddGroupRelays): string { + return '/_add relays #' + self.groupId + ' ' + self.relayIds.join(',') + } +} + // Update group profile. // Network usage: background. export interface APIUpdateGroupProfile { @@ -575,6 +590,23 @@ export namespace APIListGroups { } } +// Get chat previews. Supports time-based pagination — use this instead of APIListContacts / APIListGroups when scanning at scale (those load every record into memory and fail on large databases). +// Network usage: no. +export interface APIGetChats { + userId: number // int64 + pendingConnections: boolean + pagination: T.PaginationByTime + query: T.ChatListQuery +} + +export namespace APIGetChats { + export type Response = CR.ApiChats | CR.ChatCmdError + + export function cmdString(self: APIGetChats): string { + return '/_get chats ' + self.userId + (self.pendingConnections ? ' pcc=on' : '') + ' ' + T.PaginationByTime.cmdString(self.pagination) + ' ' + JSON.stringify(self.query) + } +} + // Delete chat. // Network usage: background. export interface APIDeleteChat { diff --git a/packages/simplex-chat-client/types/typescript/src/responses.ts b/packages/simplex-chat-client/types/typescript/src/responses.ts index 02aa29444b..e4284bf87e 100644 --- a/packages/simplex-chat-client/types/typescript/src/responses.ts +++ b/packages/simplex-chat-client/types/typescript/src/responses.ts @@ -30,6 +30,8 @@ export type ChatResponse = | CR.PublicGroupCreated | CR.PublicGroupCreationFailed | CR.GroupRelays + | CR.GroupRelaysAdded + | CR.GroupRelaysAddFailed | CR.GroupMembers | CR.GroupUpdated | CR.GroupsList @@ -55,6 +57,7 @@ export type ChatResponse = | CR.UserProfileUpdated | CR.UserProfileNoChange | CR.UsersList + | CR.ApiChats export namespace CR { export type Tag = @@ -84,6 +87,8 @@ export namespace CR { | "publicGroupCreated" | "publicGroupCreationFailed" | "groupRelays" + | "groupRelaysAdded" + | "groupRelaysAddFailed" | "groupMembers" | "groupUpdated" | "groupsList" @@ -109,6 +114,7 @@ export namespace CR { | "userProfileUpdated" | "userProfileNoChange" | "usersList" + | "apiChats" interface Interface { type: Tag @@ -273,6 +279,20 @@ export namespace CR { groupRelays: T.GroupRelay[] } + export interface GroupRelaysAdded extends Interface { + type: "groupRelaysAdded" + user: T.User + groupInfo: T.GroupInfo + groupLink: T.GroupLink + groupRelays: T.GroupRelay[] + } + + export interface GroupRelaysAddFailed extends Interface { + type: "groupRelaysAddFailed" + user: T.User + addRelayResults: T.AddRelayResult[] + } + export interface GroupMembers extends Interface { type: "groupMembers" user: T.User @@ -443,4 +463,10 @@ export namespace CR { type: "usersList" users: T.UserInfo[] } + + export interface ApiChats extends Interface { + type: "apiChats" + user: T.User + chats: T.AChat[] + } } diff --git a/packages/simplex-chat-client/types/typescript/src/types.ts b/packages/simplex-chat-client/types/typescript/src/types.ts index eed9d5edc1..64a8b49502 100644 --- a/packages/simplex-chat-client/types/typescript/src/types.ts +++ b/packages/simplex-chat-client/types/typescript/src/types.ts @@ -527,6 +527,7 @@ export enum CIDeleteMode { Broadcast = "broadcast", Internal = "internal", InternalMark = "internalMark", + History = "history", } export type CIDeleted = CIDeleted.Deleted | CIDeleted.Blocked | CIDeleted.BlockedByAdmin | CIDeleted.Moderated @@ -1577,6 +1578,27 @@ export interface ChatItemDeletion { toChatItem?: AChatItem } +export type ChatListQuery = ChatListQuery.Filters | ChatListQuery.Search + +export namespace ChatListQuery { + export type Tag = "filters" | "search" + + interface Interface { + type: Tag + } + + export interface Filters extends Interface { + type: "filters" + favorite: boolean + unread: boolean + } + + export interface Search extends Interface { + type: "search" + search: string + } +} + export enum ChatPeerType { Human = "human", Bot = "bot", @@ -3190,6 +3212,25 @@ export namespace OwnerVerification { } } +export type PaginationByTime = PaginationByTime.Last + +export namespace PaginationByTime { + export type Tag = "last" + + interface Interface { + type: Tag + } + + export interface Last extends Interface { + type: "last" + count: number // int + } + + export function cmdString(self: PaginationByTime): string { + return 'count=' + self.count + } +} + export interface PendingContactConnection { pccConnId: number // int64 pccAgentConnId: string diff --git a/packages/simplex-chat-nodejs/README.md b/packages/simplex-chat-nodejs/README.md index 2132c47a79..739b41b34e 100644 --- a/packages/simplex-chat-nodejs/README.md +++ b/packages/simplex-chat-nodejs/README.md @@ -14,7 +14,7 @@ Please share your use cases and implementations. ## Quick start: a simple bot ``` -npm i simplex-chat@6.5.0-beta.10 +npm i simplex-chat@6.5.1 ``` Simple bot that replies with squares of numbers you send to it: diff --git a/packages/simplex-chat-nodejs/package.json b/packages/simplex-chat-nodejs/package.json index 05f9c0e7d7..c5cc255722 100644 --- a/packages/simplex-chat-nodejs/package.json +++ b/packages/simplex-chat-nodejs/package.json @@ -1,6 +1,6 @@ { "name": "simplex-chat", - "version": "6.5.0", + "version": "6.5.1", "main": "dist/index.js", "types": "dist/index.d.ts", "files": [ @@ -24,7 +24,7 @@ "docs": "typedoc" }, "dependencies": { - "@simplex-chat/types": "^0.5.0", + "@simplex-chat/types": "^0.6.0", "extract-zip": "^2.0.1", "fast-deep-equal": "^3.1.3", "node-addon-api": "^8.5.0" diff --git a/packages/simplex-chat-nodejs/src/api.ts b/packages/simplex-chat-nodejs/src/api.ts index f1337e6753..0d3339df9a 100644 --- a/packages/simplex-chat-nodejs/src/api.ts +++ b/packages/simplex-chat-nodejs/src/api.ts @@ -764,6 +764,25 @@ export class ChatApi { throw new ChatCommandError("error listing groups", r) } + /** + * Get chat previews (paginated). + * Network usage: no. + * + * Prefer this over apiListContacts / apiListGroups for any scan: those + * methods load every record into memory in a single response and will fail + * on large databases. + */ + async apiGetChats( + userId: number, + pagination: T.PaginationByTime, + query: T.ChatListQuery = {type: "filters", favorite: false, unread: false}, + pendingConnections = false, + ): Promise { + const r = await this.sendChatCmd(CC.APIGetChats.cmdString({userId, pendingConnections, pagination, query})) + if (r.type === "apiChats") return r.chats + throw new ChatCommandError("error getting chats", r) + } + /** * Delete chat. * Network usage: background. diff --git a/packages/simplex-chat-nodejs/src/download-libs.js b/packages/simplex-chat-nodejs/src/download-libs.js index 25d6cc8a85..5c1b70cda0 100644 --- a/packages/simplex-chat-nodejs/src/download-libs.js +++ b/packages/simplex-chat-nodejs/src/download-libs.js @@ -4,7 +4,7 @@ const path = require('path'); const extract = require('extract-zip'); const GITHUB_REPO = 'simplex-chat/simplex-chat-libs'; -const RELEASE_TAG = 'v6.5.0'; +const RELEASE_TAG = 'v6.5.1'; const BACKEND = (process.env.SIMPLEX_BACKEND || process.env.npm_config_simplex_backend || 'sqlite').toLowerCase(); if (BACKEND !== 'sqlite' && BACKEND !== 'postgres') { diff --git a/packages/simplex-chat-nodejs/src/util.ts b/packages/simplex-chat-nodejs/src/util.ts index 52e7843a95..f7365e731c 100644 --- a/packages/simplex-chat-nodejs/src/util.ts +++ b/packages/simplex-chat-nodejs/src/util.ts @@ -78,7 +78,7 @@ export interface BotCommand { export function ciBotCommand(chatItem: T.ChatItem): BotCommand | undefined { const msg = ciContentText(chatItem)?.trim() if (msg) { - const r = msg.match(/\/([^\s]+)(.*)/) + const r = msg.match(/^\/([^\s]+)(.*)/) if (r && r.length >= 3) { return {keyword: r[1], params: r[2].trim()} } diff --git a/packages/simplex-chat-nodejs/tests/util.test.ts b/packages/simplex-chat-nodejs/tests/util.test.ts new file mode 100644 index 0000000000..4fe3140edc --- /dev/null +++ b/packages/simplex-chat-nodejs/tests/util.test.ts @@ -0,0 +1,37 @@ +import {T} from "@simplex-chat/types" +import {ciBotCommand} from "../src/util" + +function rcvText(text: string): T.ChatItem { + return {content: {type: "rcvMsgContent", msgContent: {type: "text", text}}} as T.ChatItem +} + +describe("ciBotCommand", () => { + it("parses a command at the start of the message", () => { + expect(ciBotCommand(rcvText("/grok hello"))).toEqual({keyword: "grok", params: "hello"}) + }) + + it("returns undefined for a slash in the middle of a word", () => { + expect(ciBotCommand(rcvText("What follow/read blog posts?"))).toBeUndefined() + }) + + it("returns undefined for a slash after a space", () => { + expect(ciBotCommand(rcvText("see /home for details"))).toBeUndefined() + }) + + it("strips leading whitespace before matching", () => { + expect(ciBotCommand(rcvText(" /grok ask this"))).toEqual({keyword: "grok", params: "ask this"}) + }) + + it("returns command with empty params when only the keyword is present", () => { + expect(ciBotCommand(rcvText("/team"))).toEqual({keyword: "team", params: ""}) + }) + + it("returns undefined for plain text without slash", () => { + expect(ciBotCommand(rcvText("hello there"))).toBeUndefined() + }) + + it("returns undefined for non-text chat item content", () => { + const ci = {content: {type: "rcvDeleted"}} as T.ChatItem + expect(ciBotCommand(ci)).toBeUndefined() + }) +}) diff --git a/packages/simplex-chat-python/.gitignore b/packages/simplex-chat-python/.gitignore new file mode 100644 index 0000000000..5d5acffbb9 --- /dev/null +++ b/packages/simplex-chat-python/.gitignore @@ -0,0 +1,22 @@ +# Python build / cache artifacts — never commit these +__pycache__/ +*.py[cod] +*$py.class +*.egg-info/ +build/ +dist/ +.pytest_cache/ +.ruff_cache/ +.mypy_cache/ +.pyright_cache/ + +# Virtual environments +.venv/ +.venv-*/ +venv/ + +# Lazy-downloaded native libs (handled at runtime by _native._resolve_libs_dir) +libs/ + +# Local override for SIMPLEX_LIBS_DIR work, etc. +.env diff --git a/packages/simplex-chat-python/LICENSE b/packages/simplex-chat-python/LICENSE new file mode 100644 index 0000000000..0ad25db4bd --- /dev/null +++ b/packages/simplex-chat-python/LICENSE @@ -0,0 +1,661 @@ + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +our General Public Licenses are intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + + A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + + The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + + An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing under +this license. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU Affero General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Remote Network Interaction; Use with the GNU General Public License. + + Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your version +supports such interaction) an opportunity to receive the Corresponding +Source of your version by providing access to the Corresponding Source +from a network server at no charge, through some standard or customary +means of facilitating copying of software. This Corresponding Source +shall include the Corresponding Source for any work covered by version 3 +of the GNU General Public License that is incorporated pursuant to the +following paragraph. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the work with which it is combined will remain governed by version +3 of the GNU General Public License. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU Affero General Public License from time to time. Such new versions +will be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU Affero General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU Affero General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU Affero General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for the +specific requirements. + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU AGPL, see +. diff --git a/packages/simplex-chat-python/README.md b/packages/simplex-chat-python/README.md new file mode 100644 index 0000000000..b86886d591 --- /dev/null +++ b/packages/simplex-chat-python/README.md @@ -0,0 +1,70 @@ +# SimpleX Chat Python library + +Python 3.11+ client for [SimpleX Chat](https://simplex.chat) bots. Equivalent to the [Node.js library](https://www.npmjs.com/package/simplex-chat). + +## Install + +```bash +pip install simplex-chat +``` + +The native `libsimplex` is downloaded lazily on first use. To pre-fetch: + +```bash +python -m simplex_chat install # sqlite (default) +python -m simplex_chat install --backend postgres # linux-x86_64 only +``` + +## Quick start + +```python +import re +from simplex_chat import Bot, BotProfile, Message, SqliteDb, TextMessage + +bot = Bot( + profile=BotProfile(display_name="Squaring bot"), + db=SqliteDb(file_prefix="./squaring_bot"), + welcome="Send me a number, I'll square it.", +) + +@bot.on_message(content_type="text", text=re.compile(r"^-?\d+(\.\d+)?$")) +async def square(msg: TextMessage) -> None: + n = float(msg.text or "0") + await msg.reply(f"{n} * {n} = {n * n}") + +@bot.on_message(content_type="text") +async def fallback(msg: Message) -> None: + await msg.reply("Send me a number, like 7 or 3.14.") + +if __name__ == "__main__": + bot.run() +``` + +`bot.run()` blocks. The connection address is logged on startup — paste it into a SimpleX client to talk to the bot. `Ctrl+C` to stop. + +Three decorators: `@bot.on_message(...)`, `@bot.on_command(name)`, `@bot.on_event(tag)`. Message handlers are first-match-wins in registration order, so register specific filters first and catch-alls last. + +See [`examples/squaring_bot.py`](./examples/squaring_bot.py) for the full example. + +## Development + +```bash +uv venv && source .venv/bin/activate +uv pip install -e '.[dev]' +ruff check && pyright && pytest tests/ +``` + +Wire types under `src/simplex_chat/types/_*.py` are generated. Regenerate with `cabal test simplex-chat-test --test-options='--match Python'`. + +## Release + +Manual for now. Bump `_version.py:__version__`, build a wheel, upload to PyPI: + +```bash +uv build --wheel +uv publish --token "$PYPI_TOKEN" +``` + +## License + +[AGPL-3.0](./LICENSE) diff --git a/packages/simplex-chat-python/examples/squaring_bot.py b/packages/simplex-chat-python/examples/squaring_bot.py new file mode 100644 index 0000000000..296b51347e --- /dev/null +++ b/packages/simplex-chat-python/examples/squaring_bot.py @@ -0,0 +1,52 @@ +"""Squaring bot — replies to every number with its square. + +Run with the simplex-chat package installed: + + python examples/squaring_bot.py + +Sends `n * n = ...` for any text message that parses as a number; falls +back to a hint for non-number messages; responds to `/help` with usage. +""" + +from __future__ import annotations + +import re + +from simplex_chat import ( + Bot, + BotCommand, + BotProfile, + Message, + ParsedCommand, + SqliteDb, + TextMessage, +) + +bot = Bot( + profile=BotProfile(display_name="Squaring bot"), + db=SqliteDb(file_prefix="./squaring_bot"), + welcome="Send me a number, I'll square it.", + commands=[BotCommand(keyword="help", label="Show help")], +) + +NUMBER_RE = re.compile(r"^-?\d+(\.\d+)?$") + + +@bot.on_message(content_type="text", text=NUMBER_RE) +async def square(msg: TextMessage) -> None: + n = float(msg.text or "0") + await msg.reply(f"{n} * {n} = {n * n}") + + +@bot.on_message(content_type="text") +async def fallback(msg: Message) -> None: + await msg.reply("Send me a number, like 7 or 3.14.") + + +@bot.on_command("help") +async def help_cmd(msg: Message, _cmd: ParsedCommand) -> None: + await msg.reply("Send a number, I'll square it.") + + +if __name__ == "__main__": + bot.run() diff --git a/packages/simplex-chat-python/pyproject.toml b/packages/simplex-chat-python/pyproject.toml new file mode 100644 index 0000000000..76f22cdabe --- /dev/null +++ b/packages/simplex-chat-python/pyproject.toml @@ -0,0 +1,58 @@ +[build-system] +requires = ["hatchling>=1.24"] +build-backend = "hatchling.build" + +[project] +name = "simplex-chat" +description = "SimpleX Chat Python library for chat bots" +readme = "README.md" +license = "AGPL-3.0-only" +authors = [{name = "SimpleX Chat"}] +requires-python = ">=3.11" +keywords = ["simplex", "messenger", "chat", "privacy", "security", "bots"] +classifiers = [ + "Development Status :: 4 - Beta", + "License :: OSI Approved :: GNU Affero General Public License v3", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Topic :: Communications :: Chat", +] +dynamic = ["version"] + +[project.urls] +Homepage = "https://github.com/simplex-chat/simplex-chat/tree/stable/packages/simplex-chat-python" +Issues = "https://github.com/simplex-chat/simplex-chat/issues" + +[project.optional-dependencies] +test = ["pytest>=8", "pytest-asyncio>=0.23"] +dev = ["pytest>=8", "pytest-asyncio>=0.23", "pyright>=1.1.380", "ruff>=0.6"] + +[tool.hatch.version] +path = "src/simplex_chat/_version.py" + +[tool.hatch.build.targets.wheel] +packages = ["src/simplex_chat"] + +[tool.pytest.ini_options] +asyncio_mode = "auto" + +[tool.ruff] +line-length = 100 +target-version = "py311" + +[tool.ruff.format] +# `src/simplex_chat/types/*.py` are generated by the Haskell codegen +# (bots/src/API/Docs/Generate/Python.hs). Re-formatting them locally +# would diverge from the generator's output and break `cabal test +# simplex-chat-test --match Python`. Lint still applies — only format +# is suppressed. +exclude = ["src/simplex_chat/types/_*.py"] + +[tool.pyright] +# Same rationale: the generated cmd_string helpers use `self.get('x')` +# call pairs that pyright cannot narrow across (`is not None` followed +# by re-access). Hand-written code is still strictly checked. +include = ["src/simplex_chat"] +exclude = ["src/simplex_chat/types/_*.py", "**/__pycache__", "**/.venv*"] diff --git a/packages/simplex-chat-python/src/simplex_chat/__init__.py b/packages/simplex-chat-python/src/simplex_chat/__init__.py new file mode 100644 index 0000000000..dfafef123a --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/__init__.py @@ -0,0 +1,59 @@ +"""SimpleX Chat — Python client library for chat bots.""" + +from ._version import __version__ +from .api import ChatApi, ChatCommandError, ConnReqType, Db, PostgresDb, SqliteDb +from .bot import ( + Bot, + BotCommand, + BotProfile, + ChatMessage, + CommandHandler, + EventHandler, + FileMessage, + ImageMessage, + LinkMessage, + Message, + MessageHandler, + Middleware, + ParsedCommand, + ReportMessage, + TextMessage, + UnknownMessage, + VideoMessage, + VoiceMessage, +) +from .core import ChatAPIError, ChatInitError, CryptoArgs, MigrationConfirmation +from . import util as util # re-export the util namespace + +__all__ = [ + "__version__", + "Bot", + "BotCommand", + "BotProfile", + "ChatAPIError", + "ChatApi", + "ChatCommandError", + "ChatInitError", + "ChatMessage", + "CommandHandler", + "ConnReqType", + "CryptoArgs", + "Db", + "EventHandler", + "FileMessage", + "ImageMessage", + "LinkMessage", + "Message", + "MessageHandler", + "Middleware", + "MigrationConfirmation", + "ParsedCommand", + "PostgresDb", + "ReportMessage", + "SqliteDb", + "TextMessage", + "UnknownMessage", + "VideoMessage", + "VoiceMessage", + "util", +] diff --git a/packages/simplex-chat-python/src/simplex_chat/__main__.py b/packages/simplex-chat-python/src/simplex_chat/__main__.py new file mode 100644 index 0000000000..2fa4f3cd37 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/__main__.py @@ -0,0 +1,35 @@ +"""CLI: ``python -m simplex_chat install [--backend=sqlite|postgres]``.""" + +from __future__ import annotations + +import argparse +import sys + +from . import _native + + +def main(argv: list[str] | None = None) -> int: + p = argparse.ArgumentParser(prog="simplex_chat") + sub = p.add_subparsers(dest="command", required=True) + install = sub.add_parser("install", help="Pre-fetch libsimplex into the user cache") + install.add_argument( + "--backend", + choices=["sqlite", "postgres"], + default="sqlite", + help="which libsimplex variant to download (default: sqlite)", + ) + args = p.parse_args(argv) + # `args.command` is always set: `add_subparsers(required=True)` makes + # argparse exit before reaching this point if no subcommand is given. + assert args.command == "install" + try: + path = _native._resolve_libs_dir(args.backend) + print(f"libsimplex installed at: {path}") + return 0 + except Exception as e: + print(f"install failed: {e}", file=sys.stderr) + return 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/packages/simplex-chat-python/src/simplex_chat/_native.py b/packages/simplex-chat-python/src/simplex_chat/_native.py new file mode 100644 index 0000000000..313c606883 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/_native.py @@ -0,0 +1,257 @@ +"""Native libsimplex loader: platform detection, lazy download, ctypes setup. + +Internal — users interact with `Bot` / `ChatApi`, never with this module. +""" + +from __future__ import annotations + +import ctypes +import errno +import os +import platform +import sys +import tempfile +import threading +import urllib.request +import zipfile +from ctypes import POINTER, c_char_p, c_int, c_uint8, c_void_p +from pathlib import Path +from typing import Literal + +from ._version import LIBS_VERSION + +Backend = Literal["sqlite", "postgres"] + +_GITHUB_REPO = "simplex-chat/simplex-chat-libs" + +_PLATFORM_MAP = { + "linux": ("linux", {"x86_64": "x86_64", "aarch64": "aarch64"}), + "darwin": ("macos", {"x86_64": "x86_64", "arm64": "aarch64"}), + "win32": ("windows", {"AMD64": "x86_64", "x86_64": "x86_64"}), +} + +_LIBNAME = {"linux": "libsimplex.so", "darwin": "libsimplex.dylib", "win32": "libsimplex.dll"} + +SUPPORTED = ( + "linux-x86_64", + "linux-aarch64", + "macos-x86_64", + "macos-aarch64", + "windows-x86_64", +) + + +def _platform_tag() -> str: + info = _PLATFORM_MAP.get(sys.platform) + if not info: + raise RuntimeError(f"Unsupported platform: {sys.platform}") + sysname, archs = info + arch = archs.get(platform.machine()) + if not arch: + raise RuntimeError(f"Unsupported architecture: {sys.platform}/{platform.machine()}") + tag = f"{sysname}-{arch}" + if tag not in SUPPORTED: + raise RuntimeError(f"Unsupported combination: {tag}; supported: {SUPPORTED}") + return tag + + +def _libname() -> str: + return _LIBNAME[sys.platform] + + +def _libs_url(backend: Backend) -> str: + suffix = "-postgres" if backend == "postgres" else "" + return ( + f"https://github.com/{_GITHUB_REPO}/releases/download/" + f"v{LIBS_VERSION}/simplex-chat-libs-{_platform_tag()}{suffix}.zip" + ) + + +def _cache_root() -> Path: + if sys.platform == "darwin": + return Path.home() / "Library" / "Caches" / "simplex-chat" + if sys.platform == "win32": + return Path(os.environ["LOCALAPPDATA"]) / "simplex-chat" + base = os.environ.get("XDG_CACHE_HOME") or str(Path.home() / ".cache") + return Path(base) / "simplex-chat" + + +def _resolve_libs_dir(backend: Backend) -> Path: + if override := os.environ.get("SIMPLEX_LIBS_DIR"): + return Path(override) + if backend == "postgres" and _platform_tag() != "linux-x86_64": + raise RuntimeError( + "postgres backend is only supported on linux-x86_64; " + f"current platform is {_platform_tag()}" + ) + target = _cache_root() / f"v{LIBS_VERSION}" / backend + if not (target / _libname()).exists(): + _download(target, backend) + return target + + +_DOWNLOAD_CHUNK = 1 << 16 # 64 KiB + + +def _stream_to_file(url: str, dest: Path, *, timeout: float = 60.0) -> None: + """Stream `url` → `dest`, printing a carriage-return progress bar. + + `timeout` is per-request; we don't touch `socket.setdefaulttimeout` + so other socket users in the same process aren't affected. + """ + with urllib.request.urlopen(url, timeout=timeout) as resp: # noqa: S310 - https://github.com/... + total = int(resp.headers.get("Content-Length") or 0) + received = 0 + with dest.open("wb") as out: + while chunk := resp.read(_DOWNLOAD_CHUNK): + out.write(chunk) + received += len(chunk) + if total > 0: + pct = min(100, received * 100 // total) + msg = f"\r download: {received >> 20} / {total >> 20} MiB ({pct}%)" + else: + msg = f"\r download: {received >> 20} MiB" + print(msg, end="", file=sys.stderr, flush=True) + print("", file=sys.stderr, flush=True) # newline after final progress line + + +def _download(target: Path, backend: Backend) -> None: + """Download libs zip → atomic rename into `target`. Concurrent processes safe. + + Atomicity strategy: each process extracts to its own sibling tempdir on the same + filesystem, then `os.rename` the `libs/` subdir to `target`. POSIX `os.rename` + onto a NON-EXISTENT path is atomic; if the target exists (another process won + the race), `os.rename` fails on most platforms — we then verify the winner has + what we need and proceed. NEVER rmtree the target: that creates a TOCTOU + window where another process is reading/loading the file we're deleting. + """ + target.parent.mkdir(parents=True, exist_ok=True) + url = _libs_url(backend) + print( + f"Downloading libsimplex ({_platform_tag()}, {backend}) v{LIBS_VERSION} from {url} ...", + file=sys.stderr, + flush=True, + ) + with tempfile.TemporaryDirectory(dir=target.parent) as tmp: + zip_path = Path(tmp) / "libs.zip" + _stream_to_file(url, zip_path, timeout=60.0) + with zipfile.ZipFile(zip_path) as zf: + zf.extractall(tmp) + # zip layout: /libs/libsimplex.* + libHS*.* + extracted_libs = Path(tmp) / "libs" + if not extracted_libs.is_dir(): + raise RuntimeError(f"libs/ missing from {_libs_url(backend)}") + try: + os.rename(extracted_libs, target) + except OSError as e: + # EEXIST / ENOTEMPTY mean another process won the race — fall through + # and check that the winner left a usable libsimplex behind. Anything + # else (ENOSPC, EACCES, EROFS, Windows codes mapped to None) is a real + # failure and must propagate. Same VERSION cached → same content → + # safe to proceed once we've confirmed the file is there. + if e.errno not in (errno.EEXIST, errno.ENOTEMPTY): + raise + if not (target / _libname()).exists(): + raise RuntimeError( + f"another process partially populated {target} but libsimplex " + f"is missing; remove the directory manually and retry" + ) from e + + +_lock = threading.Lock() +_lib: ctypes.CDLL | None = None +_libc: ctypes.CDLL | None = None +_backend: Backend | None = None + + +def _load_libc() -> ctypes.CDLL: + if sys.platform == "win32": + return ctypes.CDLL("msvcrt") + return ctypes.CDLL(None) # libc on POSIX is the process's own symbol table + + +def _setup_signatures(lib: ctypes.CDLL) -> None: + """Declare argtypes/restype for the 8 chat_* functions exported by libsimplex. + + All result strings come back as raw c_void_p so the caller can free them + after copying — matches HandleCResult in cpp/simplex.cc:157-165. + """ + lib.chat_migrate_init.argtypes = [c_char_p, c_char_p, c_char_p, POINTER(c_void_p)] + lib.chat_migrate_init.restype = c_void_p + lib.chat_close_store.argtypes = [c_void_p] + lib.chat_close_store.restype = c_void_p + lib.chat_send_cmd.argtypes = [c_void_p, c_char_p] + lib.chat_send_cmd.restype = c_void_p + lib.chat_recv_msg_wait.argtypes = [c_void_p, c_int] + lib.chat_recv_msg_wait.restype = c_void_p + # chat_write_file's payload is treated read-only by libsimplex; passing + # `bytes` via c_char_p avoids the from_buffer_copy doubling. ctypes pins + # the bytes buffer for the duration of the call. + lib.chat_write_file.argtypes = [c_void_p, c_char_p, c_char_p, c_int] + lib.chat_write_file.restype = c_void_p + lib.chat_read_file.argtypes = [c_char_p, c_char_p, c_char_p] + lib.chat_read_file.restype = POINTER(c_uint8) + lib.chat_encrypt_file.argtypes = [c_void_p, c_char_p, c_char_p] + lib.chat_encrypt_file.restype = c_void_p + lib.chat_decrypt_file.argtypes = [c_char_p, c_char_p, c_char_p, c_char_p] + lib.chat_decrypt_file.restype = c_void_p + + +def _hs_init(lib: ctypes.CDLL) -> None: + """Initialize the Haskell runtime exactly once. Mirrors cpp/simplex.cc:13-32.""" + if sys.platform == "win32": + argv_strs = [b"simplex", b"+RTS", b"-A64m", b"-H64m", b"--install-signal-handlers=no"] + else: + argv_strs = [ + b"simplex", + b"+RTS", + b"-A64m", + b"-H64m", + b"-xn", + b"--install-signal-handlers=no", + ] + argc = c_int(len(argv_strs)) + arr = (c_char_p * (len(argv_strs) + 1))(*argv_strs, None) + arr_ptr = ctypes.byref(ctypes.cast(arr, POINTER(c_char_p))) + lib.hs_init_with_rtsopts.argtypes = [POINTER(c_int), POINTER(POINTER(c_char_p))] + lib.hs_init_with_rtsopts.restype = None + lib.hs_init_with_rtsopts(ctypes.byref(argc), arr_ptr) + + +def lib_for(backend: Backend) -> ctypes.CDLL: + """Resolve, load, and initialize libsimplex for the given backend. + + Idempotent for the same backend; raises if called with a different backend. + Concurrent calls serialize on the module-level lock. + """ + global _lib, _libc, _backend + with _lock: + if _lib is not None: + if _backend != backend: + raise RuntimeError( + f"libsimplex already loaded with backend={_backend!r}; " + f"cannot switch to {backend!r} in the same process" + ) + return _lib + libs_dir = _resolve_libs_dir(backend) + lib = ctypes.CDLL(str(libs_dir / _libname())) + _setup_signatures(lib) + _hs_init(lib) + _libc = _load_libc() + _lib = lib + _backend = backend + return lib + + +def libc() -> ctypes.CDLL: + """libc — needed by `core` to free Haskell-allocated result strings.""" + if _libc is None: + raise RuntimeError("lib_for() must be called before libc()") + return _libc + + +def lib() -> ctypes.CDLL: + """Loaded libsimplex handle. Raises if `lib_for()` has not been called.""" + if _lib is None: + raise RuntimeError("lib_for() must be called before lib()") + return _lib diff --git a/packages/simplex-chat-python/src/simplex_chat/_version.py b/packages/simplex-chat-python/src/simplex_chat/_version.py new file mode 100644 index 0000000000..0468b65dd9 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/_version.py @@ -0,0 +1,9 @@ +"""Single source of truth for both the Python package version and the +simplex-chat-libs release tag we depend on. + +Bump both together for normal releases. For wrapper-only fixes use a PEP 440 +post-release: __version__ = "6.5.1.post1", LIBS_VERSION unchanged. +""" + +__version__ = "6.5.1" # PEP 440 — read by hatchling for wheel metadata +LIBS_VERSION = "6.5.1" # simplex-chat-libs release tag (no 'v' prefix) diff --git a/packages/simplex-chat-python/src/simplex_chat/api.py b/packages/simplex-chat-python/src/simplex_chat/api.py new file mode 100644 index 0000000000..8f116c903f --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/api.py @@ -0,0 +1,704 @@ +"""Low-level escape-hatch API. Most users go through `Bot` instead.""" + +from __future__ import annotations + +import json +from dataclasses import dataclass +from typing import Any, Literal + +from . import _native, core, util +from .core import MigrationConfirmation +from .types import CC, CEvt, CR, T + +# Mirrors Node `ConnReqType` enum (api.ts:15-18) — the two possible outcomes +# of `api_connect` / `api_connect_active_user` depending on the link kind. +ConnReqType = Literal["invitation", "contact"] + + +@dataclass(slots=True) +class SqliteDb: + file_prefix: str + encryption_key: str | None = None + + +@dataclass(slots=True) +class PostgresDb: + connection_string: str + schema_prefix: str | None = None + + +Db = SqliteDb | PostgresDb + + +def _db_to_migrate_args(db: Db) -> tuple[str, str, _native.Backend]: + """Returns (path-or-prefix, key-or-conn, backend).""" + if isinstance(db, SqliteDb): + return (db.file_prefix, db.encryption_key or "", "sqlite") + if isinstance(db, PostgresDb): + return (db.schema_prefix or "", db.connection_string, "postgres") + raise TypeError(f"Unknown db: {db!r}") + + +class ChatCommandError(Exception): + def __init__(self, message: str, response: CR.ChatResponse): + super().__init__(message) + self.response = response + + +class ChatApi: + def __init__(self, ctrl: int): + self._ctrl: int | None = ctrl + self._started = False + + @classmethod + async def init( + cls, + db: Db, + confirm: MigrationConfirmation = MigrationConfirmation.YES_UP, + ) -> "ChatApi": + path_or_prefix, key_or_conn, backend = _db_to_migrate_args(db) + # Trigger lazy lib load with the right backend BEFORE chat_migrate_init. + _native.lib_for(backend) + ctrl = await core.chat_migrate_init(path_or_prefix, key_or_conn, confirm) + return cls(ctrl) + + @property + def ctrl(self) -> int: + """Opaque controller pointer. Raises if `close()` has been called.""" + if self._ctrl is None: + raise RuntimeError("ChatApi controller not initialized (close() called?)") + return self._ctrl + + @property + def initialized(self) -> bool: + """True until `close()` is called. Mirrors Node `ChatApi.initialized`.""" + return self._ctrl is not None + + @property + def started(self) -> bool: + """True between `start_chat()` and the next `stop_chat()` / `close()`.""" + return self._started + + async def start_chat(self) -> None: + r = await self.send_chat_cmd( + CC.StartChat_cmd_string({"mainApp": True, "enableSndFiles": True}) + ) + if r.get("type") not in ("chatStarted", "chatRunning"): + raise ChatCommandError("error starting chat", r) + self._started = True + + async def stop_chat(self) -> None: + r = await self.send_chat_cmd("/_stop") + if r.get("type") != "chatStopped": + raise ChatCommandError("error stopping chat", r) + self._started = False + + async def close(self) -> None: + await core.chat_close_store(self.ctrl) + self._ctrl = None + self._started = False + + async def send_chat_cmd(self, cmd: str) -> CR.ChatResponse: + return await core.chat_send_cmd(self.ctrl, cmd) + + async def recv_chat_event(self, wait_us: int = 500_000) -> CEvt.ChatEvent | None: + return await core.chat_recv_msg_wait(self.ctrl, wait_us) + + # ------------------------------------------------------------------ # + # Address commands + # ------------------------------------------------------------------ # + + async def api_create_user_address(self, user_id: int) -> T.CreatedConnLink: + r = await self.send_chat_cmd(CC.APICreateMyAddress_cmd_string({"userId": user_id})) + if r["type"] == "userContactLinkCreated": + return r["connLinkContact"] + raise ChatCommandError("error creating user address", r) + + async def api_delete_user_address(self, user_id: int) -> None: + r = await self.send_chat_cmd(CC.APIDeleteMyAddress_cmd_string({"userId": user_id})) + if r["type"] != "userContactLinkDeleted": + raise ChatCommandError("error deleting user address", r) + + async def api_get_user_address(self, user_id: int) -> T.UserContactLink | None: + try: + r = await self.send_chat_cmd(CC.APIShowMyAddress_cmd_string({"userId": user_id})) + if r["type"] == "userContactLink": + return r["contactLink"] + raise ChatCommandError("error loading user address", r) + except core.ChatAPIError as e: + ce = e.chat_error + if ( + ce is not None + and ce.get("type") == "errorStore" + and ce.get("storeError", {}).get("type") == "userContactLinkNotFound" + ): + return None + raise + + async def api_set_profile_address( + self, user_id: int, enable: bool + ) -> T.UserProfileUpdateSummary: + r = await self.send_chat_cmd( + CC.APISetProfileAddress_cmd_string({"userId": user_id, "enable": enable}) + ) + if r["type"] == "userProfileUpdated": + return r["updateSummary"] + raise ChatCommandError("error setting profile address", r) + + async def api_set_address_settings(self, user_id: int, settings: T.AddressSettings) -> None: + r = await self.send_chat_cmd( + CC.APISetAddressSettings_cmd_string({"userId": user_id, "settings": settings}) + ) + if r["type"] != "userContactLinkUpdated": + raise ChatCommandError("error changing user contact address settings", r) + + # ------------------------------------------------------------------ # + # Message commands + # ------------------------------------------------------------------ # + + async def api_send_messages( + self, + chat: list | T.ChatRef | T.ChatInfo, + messages: list[T.ComposedMessage], + live_message: bool = False, + ) -> list[T.AChatItem]: + if isinstance(chat, list): + send_ref: T.ChatRef = {"chatType": chat[0], "chatId": chat[1]} + elif "chatType" in chat and "chatId" in chat: + send_ref = chat + else: + ref = util.chat_info_ref(chat) + if ref is None: + raise ValueError("api_send_messages: can't send messages to this chat") + send_ref = ref + r = await self.send_chat_cmd( + CC.APISendMessages_cmd_string( + { + "sendRef": send_ref, + "composedMessages": messages, + "liveMessage": live_message, + } + ) + ) + if r["type"] == "newChatItems": + return r["chatItems"] + raise ChatCommandError("unexpected response", r) + + async def api_send_text_message( + self, + chat: list | T.ChatRef | T.ChatInfo, + text: str, + in_reply_to: int | None = None, + ) -> list[T.AChatItem]: + msg: T.ComposedMessage = {"msgContent": {"type": "text", "text": text}, "mentions": {}} + if in_reply_to is not None: + msg["quotedItemId"] = in_reply_to + return await self.api_send_messages(chat, [msg]) + + async def api_send_text_reply(self, chat_item: T.AChatItem, text: str) -> list[T.AChatItem]: + return await self.api_send_text_message( + chat_item["chatInfo"], text, chat_item["chatItem"]["meta"]["itemId"] + ) + + async def api_update_chat_item( + self, + chat_type: T.ChatType, + chat_id: int, + chat_item_id: int, + msg_content: T.MsgContent, + live_message: bool = False, + ) -> T.ChatItem: + r = await self.send_chat_cmd( + CC.APIUpdateChatItem_cmd_string( + { + "chatRef": {"chatType": chat_type, "chatId": chat_id}, + "chatItemId": chat_item_id, + "liveMessage": live_message, + "updatedMessage": {"msgContent": msg_content, "mentions": {}}, + } + ) + ) + if r["type"] == "chatItemUpdated": + return r["chatItem"]["chatItem"] + raise ChatCommandError("error updating chat item", r) + + async def api_delete_chat_items( + self, + chat_type: T.ChatType, + chat_id: int, + chat_item_ids: list[int], + delete_mode: T.CIDeleteMode, + ) -> list[T.ChatItemDeletion]: + r = await self.send_chat_cmd( + CC.APIDeleteChatItem_cmd_string( + { + "chatRef": {"chatType": chat_type, "chatId": chat_id}, + "chatItemIds": chat_item_ids, + "deleteMode": delete_mode, + } + ) + ) + if r["type"] == "chatItemsDeleted": + return r["chatItemDeletions"] + raise ChatCommandError("error deleting chat item", r) + + async def api_delete_member_chat_item( + self, group_id: int, chat_item_ids: list[int] + ) -> list[T.ChatItemDeletion]: + r = await self.send_chat_cmd( + CC.APIDeleteMemberChatItem_cmd_string( + {"groupId": group_id, "chatItemIds": chat_item_ids} + ) + ) + if r["type"] == "chatItemsDeleted": + return r["chatItemDeletions"] + raise ChatCommandError("error deleting member chat item", r) + + async def api_chat_item_reaction( + self, + chat_type: T.ChatType, + chat_id: int, + chat_item_id: int, + add: bool, + reaction: T.MsgReaction, + ) -> T.ACIReaction: + r = await self.send_chat_cmd( + CC.APIChatItemReaction_cmd_string( + { + "chatRef": {"chatType": chat_type, "chatId": chat_id}, + "chatItemId": chat_item_id, + "add": add, + "reaction": reaction, + } + ) + ) + if r["type"] == "chatItemReaction": + return r["reaction"] + raise ChatCommandError("error setting item reaction", r) + + # ------------------------------------------------------------------ # + # File commands + # ------------------------------------------------------------------ # + + async def api_receive_file(self, file_id: int) -> T.AChatItem: + r = await self.send_chat_cmd( + CC.ReceiveFile_cmd_string({"fileId": file_id, "userApprovedRelays": True}) + ) + if r["type"] == "rcvFileAccepted": + return r["chatItem"] + raise ChatCommandError("error receiving file", r) + + async def api_cancel_file(self, file_id: int) -> None: + r = await self.send_chat_cmd(CC.CancelFile_cmd_string({"fileId": file_id})) + if r["type"] not in ("sndFileCancelled", "rcvFileCancelled"): + raise ChatCommandError("error canceling file", r) + + # ------------------------------------------------------------------ # + # Group commands + # ------------------------------------------------------------------ # + + async def api_add_member( + self, group_id: int, contact_id: int, member_role: T.GroupMemberRole + ) -> T.GroupMember: + r = await self.send_chat_cmd( + CC.APIAddMember_cmd_string( + {"groupId": group_id, "contactId": contact_id, "memberRole": member_role} + ) + ) + if r["type"] == "sentGroupInvitation": + return r["member"] + raise ChatCommandError("error adding member", r) + + async def api_join_group(self, group_id: int) -> T.GroupInfo: + r = await self.send_chat_cmd(CC.APIJoinGroup_cmd_string({"groupId": group_id})) + if r["type"] == "userAcceptedGroupSent": + return r["groupInfo"] + raise ChatCommandError("error joining group", r) + + async def api_accept_member( + self, group_id: int, group_member_id: int, member_role: T.GroupMemberRole + ) -> T.GroupMember: + r = await self.send_chat_cmd( + CC.APIAcceptMember_cmd_string( + {"groupId": group_id, "groupMemberId": group_member_id, "memberRole": member_role} + ) + ) + if r["type"] == "memberAccepted": + return r["member"] + raise ChatCommandError("error accepting member", r) + + async def api_set_members_role( + self, group_id: int, group_member_ids: list[int], member_role: T.GroupMemberRole + ) -> None: + r = await self.send_chat_cmd( + CC.APIMembersRole_cmd_string( + {"groupId": group_id, "groupMemberIds": group_member_ids, "memberRole": member_role} + ) + ) + if r["type"] != "membersRoleUser": + raise ChatCommandError("error setting members role", r) + + async def api_block_members_for_all( + self, group_id: int, group_member_ids: list[int], blocked: bool + ) -> None: + r = await self.send_chat_cmd( + CC.APIBlockMembersForAll_cmd_string( + {"groupId": group_id, "groupMemberIds": group_member_ids, "blocked": blocked} + ) + ) + if r["type"] != "membersBlockedForAllUser": + raise ChatCommandError("error blocking members", r) + + async def api_remove_members( + self, group_id: int, member_ids: list[int], with_messages: bool = False + ) -> list[T.GroupMember]: + r = await self.send_chat_cmd( + CC.APIRemoveMembers_cmd_string( + {"groupId": group_id, "groupMemberIds": member_ids, "withMessages": with_messages} + ) + ) + if r["type"] == "userDeletedMembers": + return r["members"] + raise ChatCommandError("error removing member", r) + + async def api_leave_group(self, group_id: int) -> T.GroupInfo: + r = await self.send_chat_cmd(CC.APILeaveGroup_cmd_string({"groupId": group_id})) + if r["type"] == "leftMemberUser": + return r["groupInfo"] + raise ChatCommandError("error leaving group", r) + + async def api_list_members(self, group_id: int) -> list[T.GroupMember]: + r = await self.send_chat_cmd(CC.APIListMembers_cmd_string({"groupId": group_id})) + if r["type"] == "groupMembers": + return r["group"]["members"] + raise ChatCommandError("error getting group members", r) + + async def api_new_group(self, user_id: int, group_profile: T.GroupProfile) -> T.GroupInfo: + r = await self.send_chat_cmd( + CC.APINewGroup_cmd_string( + {"userId": user_id, "groupProfile": group_profile, "incognito": False} + ) + ) + if r["type"] == "groupCreated": + return r["groupInfo"] + raise ChatCommandError("error creating group", r) + + async def api_update_group_profile( + self, group_id: int, group_profile: T.GroupProfile + ) -> T.GroupInfo: + r = await self.send_chat_cmd( + CC.APIUpdateGroupProfile_cmd_string( + {"groupId": group_id, "groupProfile": group_profile} + ) + ) + if r["type"] == "groupUpdated": + return r["toGroup"] + raise ChatCommandError("error updating group", r) + + # ------------------------------------------------------------------ # + # Group link commands + # ------------------------------------------------------------------ # + + async def api_create_group_link(self, group_id: int, member_role: T.GroupMemberRole) -> str: + r = await self.send_chat_cmd( + CC.APICreateGroupLink_cmd_string({"groupId": group_id, "memberRole": member_role}) + ) + if r["type"] == "groupLinkCreated": + link = r["groupLink"]["connLinkContact"] + return link.get("connShortLink") or link["connFullLink"] + raise ChatCommandError("error creating group link", r) + + async def api_set_group_link_member_role( + self, group_id: int, member_role: T.GroupMemberRole + ) -> None: + r = await self.send_chat_cmd( + CC.APIGroupLinkMemberRole_cmd_string({"groupId": group_id, "memberRole": member_role}) + ) + if r["type"] != "groupLink": + raise ChatCommandError("error setting group link member role", r) + + async def api_delete_group_link(self, group_id: int) -> None: + r = await self.send_chat_cmd(CC.APIDeleteGroupLink_cmd_string({"groupId": group_id})) + if r["type"] != "groupLinkDeleted": + raise ChatCommandError("error deleting group link", r) + + async def api_get_group_link(self, group_id: int) -> T.GroupLink: + r = await self.send_chat_cmd(CC.APIGetGroupLink_cmd_string({"groupId": group_id})) + if r["type"] == "groupLink": + return r["groupLink"] + raise ChatCommandError("error getting group link", r) + + async def api_get_group_link_str(self, group_id: int) -> str: + link = (await self.api_get_group_link(group_id))["connLinkContact"] + return link.get("connShortLink") or link["connFullLink"] + + # ------------------------------------------------------------------ # + # Connection commands + # ------------------------------------------------------------------ # + + async def api_create_link(self, user_id: int) -> str: + r = await self.send_chat_cmd( + CC.APIAddContact_cmd_string({"userId": user_id, "incognito": False}) + ) + if r["type"] == "invitation": + link = r["connLinkInvitation"] + return link.get("connShortLink") or link["connFullLink"] + raise ChatCommandError("error creating link", r) + + async def api_connect_plan( + self, user_id: int, connection_link: str + ) -> tuple[T.ConnectionPlan, T.CreatedConnLink]: + r = await self.send_chat_cmd( + CC.APIConnectPlan_cmd_string( + {"userId": user_id, "connectionLink": connection_link, "resolveKnown": False} + ) + ) + if r["type"] == "connectionPlan": + return (r["connectionPlan"], r["connLink"]) + raise ChatCommandError("error getting connect plan", r) + + async def api_connect( + self, + user_id: int, + incognito: bool, + prepared_link: T.CreatedConnLink | None = None, + ) -> ConnReqType: + args: CC.APIConnect = {"userId": user_id, "incognito": incognito} + if prepared_link is not None: + args["preparedLink_"] = prepared_link + r = await self.send_chat_cmd(CC.APIConnect_cmd_string(args)) + return self._handle_connect_result(r) + + async def api_connect_active_user(self, conn_link: str) -> ConnReqType: + r = await self.send_chat_cmd( + CC.Connect_cmd_string({"incognito": False, "connLink_": conn_link}) + ) + return self._handle_connect_result(r) + + def _handle_connect_result(self, r: CR.ChatResponse) -> ConnReqType: + if r["type"] == "sentConfirmation": + return "invitation" + if r["type"] == "sentInvitation": + return "contact" + if r["type"] == "contactAlreadyExists": + raise ChatCommandError("contact already exists", r) + raise ChatCommandError("connection error", r) + + async def api_accept_contact_request(self, contact_req_id: int) -> T.Contact: + r = await self.send_chat_cmd( + CC.APIAcceptContact_cmd_string({"contactReqId": contact_req_id}) + ) + if r["type"] == "acceptingContactRequest": + return r["contact"] + raise ChatCommandError("error accepting contact request", r) + + async def api_reject_contact_request(self, contact_req_id: int) -> None: + r = await self.send_chat_cmd( + CC.APIRejectContact_cmd_string({"contactReqId": contact_req_id}) + ) + if r["type"] != "contactRequestRejected": + raise ChatCommandError("error rejecting contact request", r) + + # ------------------------------------------------------------------ # + # Chat commands + # ------------------------------------------------------------------ # + + async def api_list_contacts(self, user_id: int) -> list[T.Contact]: + r = await self.send_chat_cmd(CC.APIListContacts_cmd_string({"userId": user_id})) + if r["type"] == "contactsList": + return r["contacts"] + raise ChatCommandError("error listing contacts", r) + + async def api_list_groups( + self, + user_id: int, + contact_id: int | None = None, + search: str | None = None, + ) -> list[T.GroupInfo]: + args: CC.APIListGroups = {"userId": user_id} + if contact_id is not None: + args["contactId_"] = contact_id + if search is not None: + args["search"] = search + r = await self.send_chat_cmd(CC.APIListGroups_cmd_string(args)) + if r["type"] == "groupsList": + return r["groups"] + raise ChatCommandError("error listing groups", r) + + async def api_get_chats( + self, + user_id: int, + pagination: T.PaginationByTime, + query: T.ChatListQuery | None = None, + pending_connections: bool = False, + ) -> list[T.AChat]: + if query is None: + query = {"type": "filters", "favorite": False, "unread": False} + r = await self.send_chat_cmd( + CC.APIGetChats_cmd_string( + { + "userId": user_id, + "pendingConnections": pending_connections, + "pagination": pagination, + "query": query, + } + ) + ) + if r["type"] == "apiChats": + return r["chats"] + raise ChatCommandError("error getting chats", r) + + async def api_delete_chat( + self, + chat_type: T.ChatType, + chat_id: int, + delete_mode: T.ChatDeleteMode | None = None, + ) -> None: + if delete_mode is None: + delete_mode = {"type": "full", "notify": True} + r = await self.send_chat_cmd( + CC.APIDeleteChat_cmd_string( + { + "chatRef": {"chatType": chat_type, "chatId": chat_id}, + "chatDeleteMode": delete_mode, + } + ) + ) + if chat_type == "direct" and r["type"] == "contactDeleted": + return + if chat_type == "group" and r["type"] == "groupDeletedUser": + return + raise ChatCommandError("error deleting chat", r) + + async def api_set_group_custom_data( + self, group_id: int, custom_data: dict[str, object] | None = None + ) -> None: + args: CC.APISetGroupCustomData = {"groupId": group_id} + if custom_data is not None: + args["customData"] = custom_data + r = await self.send_chat_cmd(CC.APISetGroupCustomData_cmd_string(args)) + if r["type"] != "cmdOk": + raise ChatCommandError("error setting group custom data", r) + + async def api_set_contact_custom_data( + self, contact_id: int, custom_data: dict[str, object] | None = None + ) -> None: + args: CC.APISetContactCustomData = {"contactId": contact_id} + if custom_data is not None: + args["customData"] = custom_data + r = await self.send_chat_cmd(CC.APISetContactCustomData_cmd_string(args)) + if r["type"] != "cmdOk": + raise ChatCommandError("error setting contact custom data", r) + + async def api_set_auto_accept_member_contacts(self, user_id: int, on_off: bool) -> None: + r = await self.send_chat_cmd( + CC.APISetUserAutoAcceptMemberContacts_cmd_string({"userId": user_id, "onOff": on_off}) + ) + if r["type"] != "cmdOk": + raise ChatCommandError("error setting auto-accept member contacts", r) + + async def api_get_chat(self, chat_type: T.ChatType, chat_id: int, count: int) -> dict[str, Any]: + ref = T.ChatType_cmd_string(chat_type) + str(chat_id) + r = await self.send_chat_cmd(f"/_get chat {ref} count={count}") + if r["type"] == "apiChat": + return r["chat"] + raise ChatCommandError("error getting chat", r) + + # ------------------------------------------------------------------ # + # User profile commands + # ------------------------------------------------------------------ # + + async def api_get_active_user(self) -> T.User | None: + try: + r = await self.send_chat_cmd(CC.ShowActiveUser_cmd_string({})) + if r["type"] == "activeUser": + return r["user"] + raise ChatCommandError("unexpected response", r) + except core.ChatAPIError as e: + ce = e.chat_error + if ( + ce is not None + and ce.get("type") == "error" + and ce.get("errorType", {}).get("type") == "noActiveUser" + ): + return None + raise + + async def api_create_active_user(self, profile: T.Profile | None = None) -> T.User: + new_user: T.NewUser = {"pastTimestamp": False, "userChatRelay": False} + if profile is not None: + new_user["profile"] = profile + r = await self.send_chat_cmd(CC.CreateActiveUser_cmd_string({"newUser": new_user})) + if r["type"] == "activeUser": + return r["user"] + raise ChatCommandError("unexpected response", r) + + async def api_list_users(self) -> list[T.UserInfo]: + r = await self.send_chat_cmd(CC.ListUsers_cmd_string({})) + if r["type"] == "usersList": + return r["users"] + raise ChatCommandError("error listing users", r) + + async def api_set_active_user(self, user_id: int, view_pwd: str | None = None) -> T.User: + args: CC.APISetActiveUser = {"userId": user_id} + if view_pwd is not None: + args["viewPwd"] = view_pwd + r = await self.send_chat_cmd(CC.APISetActiveUser_cmd_string(args)) + if r["type"] == "activeUser": + return r["user"] + raise ChatCommandError("error setting active user", r) + + async def api_delete_user( + self, user_id: int, del_smp_queues: bool, view_pwd: str | None = None + ) -> None: + args: CC.APIDeleteUser = {"userId": user_id, "delSMPQueues": del_smp_queues} + if view_pwd is not None: + args["viewPwd"] = view_pwd + r = await self.send_chat_cmd(CC.APIDeleteUser_cmd_string(args)) + if r["type"] != "cmdOk": + raise ChatCommandError("error deleting user", r) + + async def api_update_profile( + self, user_id: int, profile: T.Profile + ) -> T.UserProfileUpdateSummary | None: + r = await self.send_chat_cmd( + CC.APIUpdateProfile_cmd_string({"userId": user_id, "profile": profile}) + ) + if r["type"] == "userProfileNoChange": + return None + if r["type"] == "userProfileUpdated": + return r["updateSummary"] + raise ChatCommandError("error updating profile", r) + + async def api_set_contact_prefs(self, contact_id: int, preferences: T.Preferences) -> None: + r = await self.send_chat_cmd( + CC.APISetContactPrefs_cmd_string({"contactId": contact_id, "preferences": preferences}) + ) + if r["type"] != "contactPrefsUpdated": + raise ChatCommandError("error setting contact prefs", r) + + # ------------------------------------------------------------------ # + # Member contact commands + # ------------------------------------------------------------------ # + + async def api_create_member_contact(self, group_id: int, group_member_id: int) -> T.Contact: + r = await self.send_chat_cmd(f"/_create member contact #{group_id} {group_member_id}") + if r["type"] == "newMemberContact": + return r["contact"] + raise ChatCommandError("error creating member contact", r) + + async def api_send_member_contact_invitation( + self, + contact_id: int, + message: T.MsgContent | str | None = None, + ) -> T.Contact: + cmd = f"/_invite member contact @{contact_id}" + if message is not None: + if isinstance(message, str): + cmd += f" text {message}" + else: + cmd += f" json {json.dumps(message)}" + r = await self.send_chat_cmd(cmd) + if r["type"] == "newMemberContactSentInv": + return r["contact"] + raise ChatCommandError("error sending member contact invitation", r) diff --git a/packages/simplex-chat-python/src/simplex_chat/bot.py b/packages/simplex-chat-python/src/simplex_chat/bot.py new file mode 100644 index 0000000000..7414f28b87 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/bot.py @@ -0,0 +1,727 @@ +"""User-facing `Bot` API: decorators, filters, Message wrapper, lifecycle.""" + +from __future__ import annotations + +import asyncio +import logging +import os +import signal as _signal +from collections.abc import Awaitable, Callable +from dataclasses import dataclass +from typing import Any, Generic, Literal, TypeVar, overload + +from . import util +from .api import ChatApi, Db +from .core import ChatAPIError, MigrationConfirmation +from .filters import compile_message_filter +from .types import CEvt, T + +log = logging.getLogger("simplex_chat") + +C = TypeVar("C", bound="T.MsgContent") + + +@dataclass(slots=True) +class BotProfile: + display_name: str + full_name: str = "" + short_descr: str | None = None + image: str | None = None + + +@dataclass(slots=True) +class BotCommand: + keyword: str + label: str + + +@dataclass(slots=True, frozen=True) +class ParsedCommand: + keyword: str + args: str + + +@dataclass(slots=True, frozen=True) +class Message(Generic[C]): + chat_item: T.AChatItem + content: C + bot: "Bot" + + @property + def chat_info(self) -> T.ChatInfo: + return self.chat_item["chatInfo"] + + @property + def text(self) -> str | None: + c = self.content + if isinstance(c, dict): + return c.get("text") # type: ignore[return-value] + return None + + async def reply(self, text: str) -> "Message[T.MsgContent]": + items = await self.bot.api.api_send_text_reply(self.chat_item, text) + ci = items[0] + content = ci["chatItem"]["content"] + # content is CIContent — snd variant has msgContent; cast for type safety. + msg_content: T.MsgContent = content["msgContent"] # type: ignore[index] + return Message(chat_item=ci, content=msg_content, bot=self.bot) + + async def reply_content(self, content: T.MsgContent) -> "Message[T.MsgContent]": + items = await self.bot.api.api_send_messages( + self.chat_info, [{"msgContent": content, "mentions": {}}] + ) + ci = items[0] + ci_content = ci["chatItem"]["content"] + msg_content: T.MsgContent = ci_content["msgContent"] # type: ignore[index] + return Message(chat_item=ci, content=msg_content, bot=self.bot) + + +# Concrete narrowed aliases — one per MsgContent_ variant in _types.py. +TextMessage = Message[T.MsgContent_text] +LinkMessage = Message[T.MsgContent_link] +ImageMessage = Message[T.MsgContent_image] +VideoMessage = Message[T.MsgContent_video] +VoiceMessage = Message[T.MsgContent_voice] +FileMessage = Message[T.MsgContent_file] +ReportMessage = Message[T.MsgContent_report] +ChatMessage = Message[T.MsgContent_chat] +UnknownMessage = Message[T.MsgContent_unknown] + +MessageHandler = Callable[[Message[Any]], Awaitable[None]] +CommandHandler = Callable[[Message[Any], ParsedCommand], Awaitable[None]] +EventHandler = Callable[[CEvt.ChatEvent], Awaitable[None]] + + +class Middleware: + """Override `__call__` to wrap message handlers with cross-cutting logic. + + `handler` is the next stage in the chain — call it with `(message, data)` + to continue, or skip the call to short-circuit. `data` is a per-dispatch + dict that middleware can use to pass values down the chain. + """ + + async def __call__( + self, + handler: Callable[[Message[Any], dict[str, object]], Awaitable[None]], + message: Message[Any], + data: dict[str, object], + ) -> None: + await handler(message, data) + + +class Bot: + def __init__( + self, + *, + profile: BotProfile, + db: Db, + welcome: str | T.MsgContent | None = None, + commands: list[BotCommand] | None = None, + confirm_migrations: MigrationConfirmation = MigrationConfirmation.YES_UP, + create_address: bool = True, + update_address: bool = True, + update_profile: bool = True, + auto_accept: bool = True, + business_address: bool = False, + allow_files: bool = False, + use_bot_profile: bool = True, + log_contacts: bool = True, + log_network: bool = False, + ) -> None: + self._profile = profile + self._db = db + self._welcome = welcome + self._commands = commands or [] + self._confirm_migrations = confirm_migrations + self._opts = { + "create_address": create_address, + "update_address": update_address, + "update_profile": update_profile, + "auto_accept": auto_accept, + "business_address": business_address, + "allow_files": allow_files, + "use_bot_profile": use_bot_profile, + "log_contacts": log_contacts, + "log_network": log_network, + } + self._api: ChatApi | None = None + self._serving = False + self._stop_event = asyncio.Event() + self._message_handlers: list[tuple[Callable[[Message[Any]], bool], MessageHandler]] = [] + self._command_handlers: list[ + tuple[tuple[str, ...], Callable[[Message[Any]], bool], CommandHandler] + ] = [] + self._event_handlers: dict[str, list[EventHandler]] = {} + self._middleware: list[Middleware] = [] + # Track default-handler registration so __aenter__ on a re-used bot + # doesn't accumulate duplicate log/error handlers. + self._defaults_registered = False + + @property + def api(self) -> ChatApi: + if self._api is None: + raise RuntimeError("Bot not initialized — call bot.run() or use `async with bot:`") + return self._api + + # ------------------------------------------------------------------ # + # Decorators + # ------------------------------------------------------------------ # + + @overload + def on_message( + self, *, content_type: Literal["text"], **rest: Any + ) -> Callable[ + [Callable[[TextMessage], Awaitable[None]]], + Callable[[TextMessage], Awaitable[None]], + ]: ... + + @overload + def on_message( + self, *, content_type: Literal["link"], **rest: Any + ) -> Callable[ + [Callable[[LinkMessage], Awaitable[None]]], + Callable[[LinkMessage], Awaitable[None]], + ]: ... + + @overload + def on_message( + self, *, content_type: Literal["image"], **rest: Any + ) -> Callable[ + [Callable[[ImageMessage], Awaitable[None]]], + Callable[[ImageMessage], Awaitable[None]], + ]: ... + + @overload + def on_message( + self, *, content_type: Literal["video"], **rest: Any + ) -> Callable[ + [Callable[[VideoMessage], Awaitable[None]]], + Callable[[VideoMessage], Awaitable[None]], + ]: ... + + @overload + def on_message( + self, *, content_type: Literal["voice"], **rest: Any + ) -> Callable[ + [Callable[[VoiceMessage], Awaitable[None]]], + Callable[[VoiceMessage], Awaitable[None]], + ]: ... + + @overload + def on_message( + self, *, content_type: Literal["file"], **rest: Any + ) -> Callable[ + [Callable[[FileMessage], Awaitable[None]]], + Callable[[FileMessage], Awaitable[None]], + ]: ... + + @overload + def on_message( + self, *, content_type: Literal["report"], **rest: Any + ) -> Callable[ + [Callable[[ReportMessage], Awaitable[None]]], + Callable[[ReportMessage], Awaitable[None]], + ]: ... + + @overload + def on_message( + self, *, content_type: Literal["chat"], **rest: Any + ) -> Callable[ + [Callable[[ChatMessage], Awaitable[None]]], + Callable[[ChatMessage], Awaitable[None]], + ]: ... + + @overload + def on_message( + self, *, content_type: Literal["unknown"], **rest: Any + ) -> Callable[ + [Callable[[UnknownMessage], Awaitable[None]]], + Callable[[UnknownMessage], Awaitable[None]], + ]: ... + + @overload + def on_message(self, **rest: Any) -> Callable[[MessageHandler], MessageHandler]: ... + + def on_message(self, **filter_kw: Any) -> Callable[[MessageHandler], MessageHandler]: + predicate = compile_message_filter(filter_kw) + + def deco(fn: MessageHandler) -> MessageHandler: + self._message_handlers.append((predicate, fn)) + return fn + + return deco + + def on_command( + self, name: str | tuple[str, ...], **filter_kw: Any + ) -> Callable[[CommandHandler], CommandHandler]: + names = (name,) if isinstance(name, str) else tuple(name) + predicate = compile_message_filter(filter_kw) + + def deco(fn: CommandHandler) -> CommandHandler: + self._command_handlers.append((names, predicate, fn)) + return fn + + return deco + + def on_event(self, event: CEvt.ChatEvent_Tag, /) -> Callable[[EventHandler], EventHandler]: + def deco(fn: EventHandler) -> EventHandler: + self._event_handlers.setdefault(event, []).append(fn) + return fn + + return deco + + def use(self, middleware: Middleware) -> None: + self._middleware.append(middleware) + + # ------------------------------------------------------------------ # + # Lifecycle + # ------------------------------------------------------------------ # + + async def __aenter__(self) -> "Bot": + # Order matters: libsimplex `/_start` requires an active user, so + # ensure (or create) the user first, THEN start the chat, THEN + # do address + profile sync. Mirrors Node bot.ts:48-64. + self._api = await ChatApi.init(self._db, self._confirm_migrations) + user = await self._ensure_active_user() + await self._api.start_chat() + await self._sync_address_and_profile(user) + self._register_log_handlers() + return self + + async def __aexit__(self, *exc_info: object) -> None: + self.stop() + if self._api is not None: + try: + await self._api.stop_chat() + finally: + await self._api.close() + self._api = None + + def run(self) -> None: + """Blocking entry: runs serve_forever() with SIGINT/SIGTERM handlers installed. + + Configures `logging.basicConfig(level=INFO)` if the root logger has no + handlers yet, so the bot's startup messages and the announced address + are visible without callers having to set up logging. Embedders that + manage logging themselves are unaffected (basicConfig is a no-op when + handlers already exist). + """ + if not logging.getLogger().handlers: + logging.basicConfig( + level=logging.INFO, + format="%(asctime)s %(levelname)s %(name)s %(message)s", + ) + + async def _main() -> None: + async with self: + loop = asyncio.get_running_loop() + # First Ctrl+C → graceful stop (~500ms, bounded by the + # receive-loop poll interval). Second Ctrl+C → force-exit + # immediately (in case stop_chat / close hang on a wedged + # FFI call). Standard CLI UX (jupyter, ipython, …). + sigint_count = 0 + + def on_interrupt() -> None: + nonlocal sigint_count + sigint_count += 1 + if sigint_count == 1: + log.info("stopping bot... (press Ctrl+C again to force exit)") + self.stop() + else: + os._exit(130) # 128 + SIGINT + + if hasattr(_signal, "SIGINT"): + try: + loop.add_signal_handler(_signal.SIGINT, on_interrupt) + loop.add_signal_handler(_signal.SIGTERM, self.stop) + except NotImplementedError: # Windows + _signal.signal(_signal.SIGINT, lambda *_: on_interrupt()) + await self.serve_forever() + + asyncio.run(_main()) + + async def serve_forever(self) -> None: + if self._serving: + raise RuntimeError("already serving") + self._serving = True + self._stop_event.clear() + try: + await self._receive_loop() + finally: + self._serving = False + + def stop(self) -> None: + self._stop_event.set() + + async def _receive_loop(self) -> None: + # Catch broad Exception so a single malformed event or transient + # native error doesn't crash the whole bot. CancelledError must + # always re-raise so `bot.stop()` and asyncio cancellation work. + # `wait_us=500_000` (500ms) bounds the worst-case Ctrl+C latency: + # the C call blocks the worker thread until timeout, and the loop + # only checks `_stop_event` between polls. + while not self._stop_event.is_set(): + try: + event = await self.api.recv_chat_event(wait_us=500_000) + except asyncio.CancelledError: + raise + except ChatAPIError as e: + # Async chat errors emitted via the Haskell `eToView` path — + # routine soft errors (stale connections after a peer deletes + # a chat, file cleanup failures, etc.) intermixed with + # CRITICAL agent failures the operator must see. Mirror the + # desktop policy in SimpleXAPI.kt:3332-3340: escalate + # CRITICAL agent errors, keep everything else at debug. + chat_err: Any = e.chat_error or {} + agent_err: Any = ( + chat_err.get("agentError", {}) if chat_err.get("type") == "errorAgent" else {} + ) + if agent_err.get("type") == "CRITICAL": + log.error( + "chat agent CRITICAL: %s (offerRestart=%s)", + agent_err.get("criticalErr"), + agent_err.get("offerRestart"), + ) + else: + log.debug("chat event error: %s", chat_err.get("type")) + continue + except Exception: + log.exception("recv_chat_event failed") + # Bound the spin rate when the FFI is wedged on a persistent + # error (vs the timeout path, which already paces itself). + await asyncio.sleep(0.5) + continue + if event is None: + continue + try: + await self._dispatch_event(event) + except asyncio.CancelledError: + raise + except Exception: + log.exception("dispatch_event failed for tag=%s", event.get("type")) + + # ------------------------------------------------------------------ # + # Dispatch + # ------------------------------------------------------------------ # + + async def _dispatch_event(self, event: CEvt.ChatEvent) -> None: + tag = event["type"] + for h in self._event_handlers.get(tag, []): + try: + await h(event) + except Exception: + log.exception("on_event handler failed") + if tag == "newChatItems": + evt: CEvt.NewChatItems = event # type: ignore[assignment] + for ci in evt["chatItems"]: + content = ci["chatItem"]["content"] + if content["type"] != "rcvMsgContent": + continue + msg_content = content["msgContent"] # type: ignore[index] + msg: Message[T.MsgContent] = Message(chat_item=ci, content=msg_content, bot=self) + await self._dispatch_message(msg) + + async def _dispatch_message(self, msg: Message[Any]) -> None: + # First-match-wins. The squaring bot's `@on_message(text=NUMBER_RE)` + # and catch-all `@on_message(content_type="text")` both match a number + # like "1"; we want only the first to fire. Registration order is the + # priority order — register the most-specific filters first. + # + # Slash-commands are tried first against command handlers; if no + # command handler matches, fall through to message handlers (so + # `@on_message` can still catch unknown slash-commands). + cmd = self._parse_command(msg) + if cmd is not None: + for names, predicate, handler in self._command_handlers: + if cmd.keyword in names and predicate(msg): + await self._invoke_command_with_middleware(handler, msg, cmd) + return + for predicate, handler in self._message_handlers: + if predicate(msg): + await self._invoke_with_middleware(handler, msg) + return + + async def _invoke_with_middleware(self, handler: MessageHandler, message: Message[Any]) -> None: + # Fast path: most bots register no middleware. Skip the closure-chain + # construction and the empty-data dict on every dispatch. + if not self._middleware: + try: + await handler(message) + except Exception: + log.exception("message handler failed") + return + + async def call(m: Message[Any], _data: dict[str, object]) -> None: + await handler(m) + + chain: Callable[[Message[Any], dict[str, object]], Awaitable[None]] = call + for mw in reversed(self._middleware): + inner = chain + + async def _wrapped( + m: Message[Any], + d: dict[str, object], + mw: Middleware = mw, + inner: Callable[[Message[Any], dict[str, object]], Awaitable[None]] = inner, + ) -> None: + await mw(inner, m, d) + + chain = _wrapped + + try: + await chain(message, {}) + except Exception: + log.exception("message handler failed") + + async def _invoke_command_with_middleware( + self, handler: CommandHandler, message: Message[Any], cmd: ParsedCommand + ) -> None: + if not self._middleware: + try: + await handler(message, cmd) + except Exception: + log.exception("command handler failed") + return + + async def call(m: Message[Any], _data: dict[str, object]) -> None: + await handler(m, cmd) + + chain: Callable[[Message[Any], dict[str, object]], Awaitable[None]] = call + for mw in reversed(self._middleware): + inner = chain + + async def _wrapped( + m: Message[Any], + d: dict[str, object], + mw: Middleware = mw, + inner: Callable[[Message[Any], dict[str, object]], Awaitable[None]] = inner, + ) -> None: + await mw(inner, m, d) + + chain = _wrapped + + try: + await chain(message, {}) + except Exception: + log.exception("command handler failed") + + @staticmethod + def _parse_command(msg: Message[Any]) -> ParsedCommand | None: + parsed = util.ci_bot_command(msg.chat_item["chatItem"]) + if parsed is None: + return None + keyword, args = parsed + return ParsedCommand(keyword=keyword, args=args) + + # ------------------------------------------------------------------ # + # Profile + address sync + # ------------------------------------------------------------------ # + + async def _ensure_active_user(self) -> T.User: + """Get or create the active user. Must run before `start_chat`. + + Mirrors Node `createBotUser` (bot.ts:158-166). The chat controller + won't accept `/_start` without a user, so this phase has to land + before lifecycle proceeds. + """ + api = self.api + user = await api.api_get_active_user() + if user is None: + log.info("No active user in database, creating...") + user = await api.api_create_active_user(self._bot_profile_to_wire()) + log.info("Bot user: %s", user["profile"]["displayName"]) + return user + + async def _sync_address_and_profile(self, user: T.User) -> None: + """Address + profile sync. Runs after `start_chat` (mirrors bot.ts:57-63).""" + api = self.api + user_id = user["userId"] + + # 2. Address (numbered to match bot.ts comments — phase 1 was user creation). + address = await api.api_get_user_address(user_id) + if address is None: + if self._opts["create_address"]: + log.info("Bot has no address, creating...") + await api.api_create_user_address(user_id) + address = await api.api_get_user_address(user_id) + if address is None: + raise RuntimeError("Failed reading newly created user address") + else: + log.warning("Bot has no address") + + # Always announce the address — matches Node bot.ts:60. + link: str | None = None + if address is not None: + link = util.contact_address_str(address["connLinkContact"]) + log.info("Bot address: %s", link) + + # 3. Address settings (auto-accept + welcome message). Mirrors bot.ts:185-194. + # autoAccept present → accept; absent → no auto-accept (mirrors Node bot.ts). + if address is not None and self._opts["update_address"]: + desired: T.AddressSettings = {"businessAddress": self._opts["business_address"]} + if self._opts["auto_accept"]: + desired["autoAccept"] = {"acceptIncognito": False} + if self._welcome is not None: + desired["autoReply"] = ( + {"type": "text", "text": self._welcome} + if isinstance(self._welcome, str) + else self._welcome + ) + if address["addressSettings"] != desired: + log.info("Bot address settings changed, updating...") + await api.api_set_address_settings(user_id, desired) + + # 4. Profile update. Mirrors Node `updateBotUserProfile` (bot.ts:199-214). + # Field-by-field comparison: user["profile"] is LocalProfile (has extra + # fields profileId, localAlias, preferences, peerType) so a full-dict + # equality would always differ. + new_profile = self._bot_profile_to_wire() + if link is not None and self._opts["use_bot_profile"]: + # Mirrors bot.ts:62 — embed the connection link in the bot's profile + # so contacts that resolve the bot via stored profile data see the + # current address. + new_profile["contactLink"] = link + cur = user["profile"] + changed = ( + cur["displayName"] != new_profile["displayName"] + or cur.get("fullName", "") != new_profile.get("fullName", "") + or cur.get("shortDescr") != new_profile.get("shortDescr") + or cur.get("image") != new_profile.get("image") + or cur.get("preferences") != new_profile.get("preferences") + or cur.get("peerType") != new_profile.get("peerType") + or cur.get("contactLink") != new_profile.get("contactLink") + ) + if changed and self._opts["update_profile"]: + log.info("Bot profile changed, updating...") + await api.api_update_profile(user_id, new_profile) + + def _bot_profile_to_wire(self) -> T.Profile: + """Construct wire-format Profile, applying bot conventions when use_bot_profile=True. + + Mirrors Node mkBotProfile (bot.ts:88-102): bots get peerType="bot", + calls/voice prefs disabled, files gated on `allow_files`, and any + registered `commands` embedded in the profile preferences. + """ + p: T.Profile = { + "displayName": self._profile.display_name, + "fullName": self._profile.full_name, + } + if self._profile.short_descr is not None: + p["shortDescr"] = self._profile.short_descr + if self._profile.image is not None: + p["image"] = self._profile.image + if self._opts["use_bot_profile"]: + prefs: T.Preferences = { + "calls": {"allow": "no"}, + "voice": {"allow": "no"}, + "files": {"allow": "yes" if self._opts["allow_files"] else "no"}, + } + if self._commands: + prefs["commands"] = [ + {"type": "command", "keyword": c.keyword, "label": c.label} + for c in self._commands + ] + p["preferences"] = prefs + p["peerType"] = "bot" + elif self._commands: + raise ValueError( + "use_bot_profile=False but commands were passed; commands are " + "only sent when use_bot_profile=True (they're embedded in the " + "user profile preferences)." + ) + return p + + # ------------------------------------------------------------------ # + # Log subscriptions (mirror Node subscribeLogEvents bot.ts:142-156) + # ------------------------------------------------------------------ # + + def _register_log_handlers(self) -> None: + # Idempotent: a Bot reused across multiple `__aenter__` cycles must + # not stack duplicate log handlers. Always-on error handlers run + # regardless of log_contacts/log_network so messageError/chatError/ + # chatErrors don't disappear into the void. + if self._defaults_registered: + return + self._defaults_registered = True + self._event_handlers.setdefault("messageError", []).append(self._log_message_error) + self._event_handlers.setdefault("chatError", []).append(self._log_chat_error) + self._event_handlers.setdefault("chatErrors", []).append(self._log_chat_errors) + if self._opts["log_contacts"]: + self._event_handlers.setdefault("contactConnected", []).append( + self._log_contact_connected + ) + self._event_handlers.setdefault("contactDeletedByContact", []).append( + self._log_contact_deleted + ) + if self._opts["log_network"]: + self._event_handlers.setdefault("hostConnected", []).append(self._log_host_connected) + self._event_handlers.setdefault("hostDisconnected", []).append( + self._log_host_disconnected + ) + self._event_handlers.setdefault("subscriptionStatus", []).append( + self._log_subscription_status + ) + + @staticmethod + async def _log_contact_connected(evt: CEvt.ChatEvent) -> None: + log.info("%s connected", evt["contact"]["profile"]["displayName"]) # type: ignore[index] + + @staticmethod + async def _log_contact_deleted(evt: CEvt.ChatEvent) -> None: + log.info( + "%s deleted connection with bot", + evt["contact"]["profile"]["displayName"], # type: ignore[index] + ) + + @staticmethod + async def _log_host_connected(evt: CEvt.ChatEvent) -> None: + log.info("connected server %s", evt["transportHost"]) # type: ignore[index] + + @staticmethod + async def _log_host_disconnected(evt: CEvt.ChatEvent) -> None: + log.info("disconnected server %s", evt["transportHost"]) # type: ignore[index] + + @staticmethod + async def _log_subscription_status(evt: CEvt.ChatEvent) -> None: + log.info( + "%d subscription(s) %s", + len(evt["connections"]), # type: ignore[index] + evt["subscriptionStatus"]["type"], # type: ignore[index] + ) + + @staticmethod + async def _log_message_error(evt: CEvt.ChatEvent) -> None: + log.warning("messageError: %s", evt.get("severity", "?")) # type: ignore[union-attr] + + @staticmethod + async def _log_chat_error(evt: CEvt.ChatEvent) -> None: + err = evt.get("chatError") # type: ignore[union-attr] + log.error("chatError: %s", err.get("type") if isinstance(err, dict) else err) + + @staticmethod + async def _log_chat_errors(evt: CEvt.ChatEvent) -> None: + errs = evt.get("chatErrors") or [] # type: ignore[union-attr] + log.error("chatErrors: %d errors", len(errs)) + + +# Suppress unused-import warnings for re-exported names used only at type-check time. +__all__ = [ + "Bot", + "BotCommand", + "BotProfile", + "ChatMessage", + "FileMessage", + "ImageMessage", + "LinkMessage", + "Message", + "MessageHandler", + "CommandHandler", + "EventHandler", + "Middleware", + "ParsedCommand", + "ReportMessage", + "TextMessage", + "UnknownMessage", + "VideoMessage", + "VoiceMessage", +] diff --git a/packages/simplex-chat-python/src/simplex_chat/core.py b/packages/simplex-chat-python/src/simplex_chat/core.py new file mode 100644 index 0000000000..075db34b52 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/core.py @@ -0,0 +1,200 @@ +"""Internal typed async wrapper around libsimplex's 8 C ABI functions. + +Users interact with `Bot` / `ChatApi`. This module is exposed as +`simplex_chat.core` for tests and the api.ChatApi class only. +""" + +from __future__ import annotations + +import asyncio +import ctypes +import json +from enum import StrEnum +from typing import Any, TypedDict + +from . import _native +from .types import T, CR, CEvt + + +class ChatAPIError(Exception): + """Raised when chat_send_cmd / chat_recv_msg_wait returns a chat error.""" + + def __init__(self, message: str, chat_error: T.ChatError | None = None): + super().__init__(message) + self.chat_error = chat_error + + +class ChatInitError(Exception): + """Raised when chat_migrate_init returns a DBMigrationResult error.""" + + def __init__(self, message: str, db_migration_error: dict[str, Any]): + super().__init__(message) + self.db_migration_error = db_migration_error + + +class MigrationConfirmation(StrEnum): + YES_UP = "yesUp" + YES_UP_DOWN = "yesUpDown" + CONSOLE = "console" + ERROR = "error" + + +class CryptoArgs(TypedDict): # wire-format JSON; camelCase fields + fileKey: str + fileNonce: str + + +def _read_and_free(ptr: int | None) -> str: + """Copy a Haskell-allocated null-terminated UTF-8 string and free its buffer. + + Mirrors HandleCResult in packages/simplex-chat-nodejs/cpp/simplex.cc:157-165. + """ + if not ptr: + raise RuntimeError("null pointer returned from libsimplex") + try: + return ctypes.string_at(ptr).decode("utf-8") + finally: + _native.libc().free(ctypes.c_void_p(ptr)) + + +async def chat_send_cmd(ctrl: int, cmd: str) -> CR.ChatResponse: + def _call() -> str: + ptr = _native.lib().chat_send_cmd(ctrl, cmd.encode("utf-8")) + return _read_and_free(ptr) + + raw = await asyncio.to_thread(_call) + parsed = json.loads(raw) + if "result" in parsed and isinstance(parsed["result"], dict): + return parsed["result"] # type: ignore[return-value] + err = parsed.get("error") + if isinstance(err, dict): + raise ChatAPIError(f"chat command error: {err.get('type')}", err) # type: ignore[arg-type] + raise ChatAPIError(f"invalid chat command result: {raw[:200]}") + + +async def chat_recv_msg_wait(ctrl: int, wait_us: int = 500_000) -> CEvt.ChatEvent | None: + def _call() -> str: + # On timeout, the C side returns a non-NULL pointer to a single NUL byte + # (see Mobile.hs `fromMaybe ""`), so `_read_and_free` returns "" — no + # NULL-pointer guard is needed here. + ptr = _native.lib().chat_recv_msg_wait(ctrl, wait_us) + return _read_and_free(ptr) + + raw = await asyncio.to_thread(_call) + if not raw: + return None + parsed = json.loads(raw) + if "result" in parsed and isinstance(parsed["result"], dict): + return parsed["result"] # type: ignore[return-value] + err = parsed.get("error") + if isinstance(err, dict): + raise ChatAPIError(f"chat event error: {err.get('type')}", err) # type: ignore[arg-type] + raise ChatAPIError(f"invalid chat event: {raw[:200]}") + + +async def chat_migrate_init(db_path: str, db_key: str, confirm: MigrationConfirmation) -> int: + """Initialize chat controller. Returns opaque ctrl pointer as Python int.""" + + def _call() -> tuple[int, str]: + ctrl = ctypes.c_void_p() + ptr = _native.lib().chat_migrate_init( + db_path.encode("utf-8"), + db_key.encode("utf-8"), + confirm.encode("utf-8"), + ctypes.byref(ctrl), + ) + return (ctrl.value or 0, _read_and_free(ptr)) + + ctrl_val, raw = await asyncio.to_thread(_call) + parsed = json.loads(raw) + if parsed.get("type") == "ok": + if not ctrl_val: + # ABI invariant: type=="ok" → out-param written. Defensive guard so a + # broken libsimplex doesn't hand us a NULL controller that would only + # crash on first use much later. + raise RuntimeError("chat_migrate_init returned ok but did not set ctrl pointer") + return ctrl_val + raise ChatInitError( + "Database or migration error (see db_migration_error)", + parsed, + ) + + +async def chat_close_store(ctrl: int) -> None: + def _call() -> str: + ptr = _native.lib().chat_close_store(ctrl) + return _read_and_free(ptr) + + res = await asyncio.to_thread(_call) + if res: + raise RuntimeError(res) + + +async def chat_write_file(ctrl: int, path: str, data: bytes) -> CryptoArgs: + def _call() -> str: + ptr = _native.lib().chat_write_file(ctrl, path.encode("utf-8"), data, len(data)) + return _read_and_free(ptr) + + raw = await asyncio.to_thread(_call) + return _crypto_args_result(raw) + + +async def chat_read_file(path: str, args: CryptoArgs) -> bytes: + def _call() -> bytes: + ptr = _native.lib().chat_read_file( + path.encode("utf-8"), + args["fileKey"].encode("utf-8"), + args["fileNonce"].encode("utf-8"), + ) + if not ptr: + raise RuntimeError("chat_read_file returned null") + addr = ctypes.cast(ptr, ctypes.c_void_p).value + assert addr is not None # `if not ptr` above already filtered NULL + try: + status = ctypes.cast(addr, ctypes.POINTER(ctypes.c_uint8))[0] + if status == 1: + msg = ctypes.string_at(addr + 1).decode("utf-8") + raise RuntimeError(msg) + if status != 0: + raise RuntimeError(f"unexpected status {status} from chat_read_file") + # `addr + 1` is unaligned for a uint32 read. On the supported platforms + # (linux-x86_64, linux-aarch64, macos-aarch64, windows-x86_64) this is + # silently handled; matches the Node.js binding (cpp/simplex.cc:344). + length = ctypes.cast(addr + 1, ctypes.POINTER(ctypes.c_uint32))[0] + return ctypes.string_at(addr + 5, length) + finally: + _native.libc().free(ctypes.c_void_p(addr)) + + return await asyncio.to_thread(_call) + + +async def chat_encrypt_file(ctrl: int, src: str, dst: str) -> CryptoArgs: + def _call() -> str: + ptr = _native.lib().chat_encrypt_file(ctrl, src.encode("utf-8"), dst.encode("utf-8")) + return _read_and_free(ptr) + + return _crypto_args_result(await asyncio.to_thread(_call)) + + +async def chat_decrypt_file(src: str, args: CryptoArgs, dst: str) -> None: + def _call() -> str: + ptr = _native.lib().chat_decrypt_file( + src.encode("utf-8"), + args["fileKey"].encode("utf-8"), + args["fileNonce"].encode("utf-8"), + dst.encode("utf-8"), + ) + return _read_and_free(ptr) + + res = await asyncio.to_thread(_call) + if res: + raise RuntimeError(res) + + +def _crypto_args_result(raw: str) -> CryptoArgs: + parsed = json.loads(raw) + if parsed.get("type") == "result": + return parsed["cryptoArgs"] + if parsed.get("type") == "error": + raise RuntimeError(parsed.get("writeError", "unknown write error")) + raise RuntimeError(f"unexpected result: {raw[:200]}") diff --git a/packages/simplex-chat-python/src/simplex_chat/filters.py b/packages/simplex-chat-python/src/simplex_chat/filters.py new file mode 100644 index 0000000000..cdce5b7bb6 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/filters.py @@ -0,0 +1,45 @@ +"""Compile kwarg-based message filters into a single predicate.""" + +from __future__ import annotations + +import re +from typing import Any, Callable + + +def compile_message_filter(kw: dict[str, Any]) -> Callable[[Any], bool]: + """Compile filter kwargs into a single predicate function. + + Multiple kwargs combine with AND; tuples within a kwarg combine with OR. + `when` is the last predicate evaluated. + """ + predicates: list[Callable[[Any], bool]] = [] + + if (ct := kw.get("content_type")) is not None: + ct_set = (ct,) if isinstance(ct, str) else tuple(ct) + predicates.append(lambda m: m.content.get("type") in ct_set) + + if (t := kw.get("text")) is not None: + if isinstance(t, re.Pattern): + predicates.append(lambda m: bool(t.search(m.content.get("text", "") or ""))) + else: + predicates.append(lambda m: m.content.get("text") == t) + + if (cht := kw.get("chat_type")) is not None: + cht_set = (cht,) if isinstance(cht, str) else tuple(cht) + predicates.append(lambda m: m.chat_item["chatInfo"]["type"] in cht_set) + + if (gid := kw.get("group_id")) is not None: + gid_set: tuple[int, ...] = (gid,) if isinstance(gid, int) else tuple(gid) + + def gid_match(m: Any) -> bool: + ci = m.chat_item["chatInfo"] + return ci["type"] == "group" and ci["groupInfo"]["groupId"] in gid_set + + predicates.append(gid_match) + + if (when := kw.get("when")) is not None: + predicates.append(when) + + if not predicates: + return lambda _m: True + return lambda m: all(p(m) for p in predicates) diff --git a/packages/simplex-chat-python/src/simplex_chat/py.typed b/packages/simplex-chat-python/src/simplex_chat/py.typed new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/simplex-chat-python/src/simplex_chat/types/__init__.py b/packages/simplex-chat-python/src/simplex_chat/types/__init__.py new file mode 100644 index 0000000000..4d21965dd4 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/types/__init__.py @@ -0,0 +1,16 @@ +"""SimpleX Chat wire types — auto-generated from Haskell. + +Re-exports the four generated modules as namespaces: + +- ``T`` — :mod:`._types` (records, enums, discriminated unions) +- ``CC`` — :mod:`._commands` (command TypedDicts + ``_cmd_string`` helpers) +- ``CR`` — :mod:`._responses` (``ChatResponse`` and member TypedDicts) +- ``CEvt`` — :mod:`._events` (``ChatEvent`` and member TypedDicts) +""" + +from . import _commands as CC +from . import _events as CEvt +from . import _responses as CR +from . import _types as T + +__all__ = ["T", "CC", "CR", "CEvt"] diff --git a/packages/simplex-chat-python/src/simplex_chat/types/_commands.py b/packages/simplex-chat-python/src/simplex_chat/types/_commands.py new file mode 100644 index 0000000000..9806388835 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/types/_commands.py @@ -0,0 +1,705 @@ +# API Commands +# This file is generated automatically. +from __future__ import annotations +import json +from typing import NotRequired, TypedDict +from . import _types as T +from . import _responses as CR + +# Address commands +# Bots can use these commands to automatically check and create address when initialized + +# Create bot address. +# Network usage: interactive. +class APICreateMyAddress(TypedDict): + userId: int # int64 + + +def APICreateMyAddress_cmd_string(self: APICreateMyAddress) -> str: + return '/_address ' + str(self['userId']) + +APICreateMyAddress_Response = CR.UserContactLinkCreated | CR.ChatCmdError + + +# Delete bot address. +# Network usage: background. +class APIDeleteMyAddress(TypedDict): + userId: int # int64 + + +def APIDeleteMyAddress_cmd_string(self: APIDeleteMyAddress) -> str: + return '/_delete_address ' + str(self['userId']) + +APIDeleteMyAddress_Response = CR.UserContactLinkDeleted | CR.ChatCmdError + + +# Get bot address and settings. +# Network usage: no. +class APIShowMyAddress(TypedDict): + userId: int # int64 + + +def APIShowMyAddress_cmd_string(self: APIShowMyAddress) -> str: + return '/_show_address ' + str(self['userId']) + +APIShowMyAddress_Response = CR.UserContactLink | CR.ChatCmdError + + +# Add address to bot profile. +# Network usage: interactive. +class APISetProfileAddress(TypedDict): + userId: int # int64 + enable: bool + + +def APISetProfileAddress_cmd_string(self: APISetProfileAddress) -> str: + return '/_profile_address ' + str(self['userId']) + ' ' + ('on' if self['enable'] else 'off') + +APISetProfileAddress_Response = CR.UserProfileUpdated | CR.ChatCmdError + + +# Set bot address settings. +# Network usage: interactive. +class APISetAddressSettings(TypedDict): + userId: int # int64 + settings: "T.AddressSettings" + + +def APISetAddressSettings_cmd_string(self: APISetAddressSettings) -> str: + return '/_address_settings ' + str(self['userId']) + ' ' + json.dumps(self['settings']) + +APISetAddressSettings_Response = CR.UserContactLinkUpdated | CR.ChatCmdError + + +# Message commands +# Commands to send, update, delete, moderate messages and set message reactions + +# Send messages. +# Network usage: background. +class APISendMessages(TypedDict): + sendRef: "T.ChatRef" + liveMessage: bool + ttl: NotRequired[int] # int + composedMessages: list["T.ComposedMessage"] # non-empty + + +def APISendMessages_cmd_string(self: APISendMessages) -> str: + return '/_send ' + T.ChatRef_cmd_string(self['sendRef']) + (' live=on' if self['liveMessage'] else '') + ((' ttl=' + str(self.get('ttl'))) if self.get('ttl') is not None else '') + ' json ' + json.dumps(self['composedMessages']) + +APISendMessages_Response = CR.NewChatItems | CR.ChatCmdError + + +# Update message. +# Network usage: background. +class APIUpdateChatItem(TypedDict): + chatRef: "T.ChatRef" + chatItemId: int # int64 + liveMessage: bool + updatedMessage: "T.UpdatedMessage" + + +def APIUpdateChatItem_cmd_string(self: APIUpdateChatItem) -> str: + return '/_update item ' + T.ChatRef_cmd_string(self['chatRef']) + ' ' + str(self['chatItemId']) + (' live=on' if self['liveMessage'] else '') + ' json ' + json.dumps(self['updatedMessage']) + +APIUpdateChatItem_Response = CR.ChatItemUpdated | CR.ChatItemNotChanged | CR.ChatCmdError + + +# Delete message. +# Network usage: background. +class APIDeleteChatItem(TypedDict): + chatRef: "T.ChatRef" + chatItemIds: list[int] # int64, non-empty + deleteMode: "T.CIDeleteMode" + + +def APIDeleteChatItem_cmd_string(self: APIDeleteChatItem) -> str: + return '/_delete item ' + T.ChatRef_cmd_string(self['chatRef']) + ' ' + ','.join(map(str, self['chatItemIds'])) + ' ' + str(self['deleteMode']) + +APIDeleteChatItem_Response = CR.ChatItemsDeleted | CR.ChatCmdError + + +# Moderate message. Requires Moderator role (and higher than message author's). +# Network usage: background. +class APIDeleteMemberChatItem(TypedDict): + groupId: int # int64 + chatItemIds: list[int] # int64, non-empty + + +def APIDeleteMemberChatItem_cmd_string(self: APIDeleteMemberChatItem) -> str: + return '/_delete member item #' + str(self['groupId']) + ' ' + ','.join(map(str, self['chatItemIds'])) + +APIDeleteMemberChatItem_Response = CR.ChatItemsDeleted | CR.ChatCmdError + + +# Add/remove message reaction. +# Network usage: background. +class APIChatItemReaction(TypedDict): + chatRef: "T.ChatRef" + chatItemId: int # int64 + add: bool + reaction: "T.MsgReaction" + + +def APIChatItemReaction_cmd_string(self: APIChatItemReaction) -> str: + return '/_reaction ' + T.ChatRef_cmd_string(self['chatRef']) + ' ' + str(self['chatItemId']) + ' ' + ('on' if self['add'] else 'off') + ' ' + json.dumps(self['reaction']) + +APIChatItemReaction_Response = CR.ChatItemReaction | CR.ChatCmdError + + +# File commands +# Commands to receive and to cancel files. Files are sent as part of the message, there are no separate commands to send files. + +# Receive file. +# Network usage: no. +class ReceiveFile(TypedDict): + fileId: int # int64 + userApprovedRelays: bool + storeEncrypted: NotRequired[bool] + fileInline: NotRequired[bool] + filePath: NotRequired[str] + + +def ReceiveFile_cmd_string(self: ReceiveFile) -> str: + return '/freceive ' + str(self['fileId']) + (' approved_relays=on' if self['userApprovedRelays'] else '') + ((' encrypt=' + ('on' if self.get('storeEncrypted') else 'off')) if self.get('storeEncrypted') is not None else '') + ((' inline=' + ('on' if self.get('fileInline') else 'off')) if self.get('fileInline') is not None else '') + ((' ' + self.get('filePath')) if self.get('filePath') is not None else '') + +ReceiveFile_Response = CR.RcvFileAccepted | CR.RcvFileAcceptedSndCancelled | CR.ChatCmdError + + +# Cancel file. +# Network usage: background. +class CancelFile(TypedDict): + fileId: int # int64 + + +def CancelFile_cmd_string(self: CancelFile) -> str: + return '/fcancel ' + str(self['fileId']) + +CancelFile_Response = CR.SndFileCancelled | CR.RcvFileCancelled | CR.ChatCmdError + + +# Group commands +# Commands to manage and moderate groups. These commands can be used with business chats as well - they are groups. E.g., a common scenario would be to add human agents to business chat with the customer who connected via business address. + +# Add contact to group. Requires bot to have Admin role. +# Network usage: interactive. +class APIAddMember(TypedDict): + groupId: int # int64 + contactId: int # int64 + memberRole: "T.GroupMemberRole" + + +def APIAddMember_cmd_string(self: APIAddMember) -> str: + return '/_add #' + str(self['groupId']) + ' ' + str(self['contactId']) + ' ' + str(self['memberRole']) + +APIAddMember_Response = CR.SentGroupInvitation | CR.ChatCmdError + + +# Join group. +# Network usage: interactive. +class APIJoinGroup(TypedDict): + groupId: int # int64 + + +def APIJoinGroup_cmd_string(self: APIJoinGroup) -> str: + return '/_join #' + str(self['groupId']) + +APIJoinGroup_Response = CR.UserAcceptedGroupSent | CR.ChatCmdError + + +# Accept group member. Requires Admin role. +# Network usage: background. +class APIAcceptMember(TypedDict): + groupId: int # int64 + groupMemberId: int # int64 + memberRole: "T.GroupMemberRole" + + +def APIAcceptMember_cmd_string(self: APIAcceptMember) -> str: + return '/_accept member #' + str(self['groupId']) + ' ' + str(self['groupMemberId']) + ' ' + str(self['memberRole']) + +APIAcceptMember_Response = CR.MemberAccepted | CR.ChatCmdError + + +# Set members role. Requires Admin role. +# Network usage: background. +class APIMembersRole(TypedDict): + groupId: int # int64 + groupMemberIds: list[int] # int64, non-empty + memberRole: "T.GroupMemberRole" + + +def APIMembersRole_cmd_string(self: APIMembersRole) -> str: + return '/_member role #' + str(self['groupId']) + ' ' + ','.join(map(str, self['groupMemberIds'])) + ' ' + str(self['memberRole']) + +APIMembersRole_Response = CR.MembersRoleUser | CR.ChatCmdError + + +# Block members. Requires Moderator role. +# Network usage: background. +class APIBlockMembersForAll(TypedDict): + groupId: int # int64 + groupMemberIds: list[int] # int64, non-empty + blocked: bool + + +def APIBlockMembersForAll_cmd_string(self: APIBlockMembersForAll) -> str: + return '/_block #' + str(self['groupId']) + ' ' + ','.join(map(str, self['groupMemberIds'])) + ' blocked=' + ('on' if self['blocked'] else 'off') + +APIBlockMembersForAll_Response = CR.MembersBlockedForAllUser | CR.ChatCmdError + + +# Remove members. Requires Admin role. +# Network usage: background. +class APIRemoveMembers(TypedDict): + groupId: int # int64 + groupMemberIds: list[int] # int64, non-empty + withMessages: bool + + +def APIRemoveMembers_cmd_string(self: APIRemoveMembers) -> str: + return '/_remove #' + str(self['groupId']) + ' ' + ','.join(map(str, self['groupMemberIds'])) + (' messages=on' if self['withMessages'] else '') + +APIRemoveMembers_Response = CR.UserDeletedMembers | CR.ChatCmdError + + +# Leave group. +# Network usage: background. +class APILeaveGroup(TypedDict): + groupId: int # int64 + + +def APILeaveGroup_cmd_string(self: APILeaveGroup) -> str: + return '/_leave #' + str(self['groupId']) + +APILeaveGroup_Response = CR.LeftMemberUser | CR.ChatCmdError + + +# Get group members. +# Network usage: no. +class APIListMembers(TypedDict): + groupId: int # int64 + + +def APIListMembers_cmd_string(self: APIListMembers) -> str: + return '/_members #' + str(self['groupId']) + +APIListMembers_Response = CR.GroupMembers | CR.ChatCmdError + + +# Create group. +# Network usage: no. +class APINewGroup(TypedDict): + userId: int # int64 + incognito: bool + groupProfile: "T.GroupProfile" + + +def APINewGroup_cmd_string(self: APINewGroup) -> str: + return '/_group ' + str(self['userId']) + (' incognito=on' if self['incognito'] else '') + ' ' + json.dumps(self['groupProfile']) + +APINewGroup_Response = CR.GroupCreated | CR.ChatCmdError + + +# Create public group. +# Network usage: interactive. +class APINewPublicGroup(TypedDict): + userId: int # int64 + incognito: bool + relayIds: list[int] # int64, non-empty + groupProfile: "T.GroupProfile" + + +def APINewPublicGroup_cmd_string(self: APINewPublicGroup) -> str: + return '/_public group ' + str(self['userId']) + (' incognito=on' if self['incognito'] else '') + ' ' + ','.join(map(str, self['relayIds'])) + ' ' + json.dumps(self['groupProfile']) + +APINewPublicGroup_Response = CR.PublicGroupCreated | CR.PublicGroupCreationFailed | CR.ChatCmdError + + +# Get group relays. +# Network usage: no. +class APIGetGroupRelays(TypedDict): + groupId: int # int64 + + +def APIGetGroupRelays_cmd_string(self: APIGetGroupRelays) -> str: + return '/_get relays #' + str(self['groupId']) + +APIGetGroupRelays_Response = CR.GroupRelays | CR.ChatCmdError + + +# Add relays to group. +# Network usage: interactive. +class APIAddGroupRelays(TypedDict): + groupId: int # int64 + relayIds: list[int] # int64, non-empty + + +def APIAddGroupRelays_cmd_string(self: APIAddGroupRelays) -> str: + return '/_add relays #' + str(self['groupId']) + ' ' + ','.join(map(str, self['relayIds'])) + +APIAddGroupRelays_Response = CR.GroupRelaysAdded | CR.GroupRelaysAddFailed | CR.ChatCmdError + + +# Update group profile. +# Network usage: background. +class APIUpdateGroupProfile(TypedDict): + groupId: int # int64 + groupProfile: "T.GroupProfile" + + +def APIUpdateGroupProfile_cmd_string(self: APIUpdateGroupProfile) -> str: + return '/_group_profile #' + str(self['groupId']) + ' ' + json.dumps(self['groupProfile']) + +APIUpdateGroupProfile_Response = CR.GroupUpdated | CR.ChatCmdError + + +# Group link commands +# These commands can be used by bots that manage multiple public groups + +# Create group link. +# Network usage: interactive. +class APICreateGroupLink(TypedDict): + groupId: int # int64 + memberRole: "T.GroupMemberRole" + + +def APICreateGroupLink_cmd_string(self: APICreateGroupLink) -> str: + return '/_create link #' + str(self['groupId']) + ' ' + str(self['memberRole']) + +APICreateGroupLink_Response = CR.GroupLinkCreated | CR.ChatCmdError + + +# Set member role for group link. +# Network usage: no. +class APIGroupLinkMemberRole(TypedDict): + groupId: int # int64 + memberRole: "T.GroupMemberRole" + + +def APIGroupLinkMemberRole_cmd_string(self: APIGroupLinkMemberRole) -> str: + return '/_set link role #' + str(self['groupId']) + ' ' + str(self['memberRole']) + +APIGroupLinkMemberRole_Response = CR.GroupLink | CR.ChatCmdError + + +# Delete group link. +# Network usage: background. +class APIDeleteGroupLink(TypedDict): + groupId: int # int64 + + +def APIDeleteGroupLink_cmd_string(self: APIDeleteGroupLink) -> str: + return '/_delete link #' + str(self['groupId']) + +APIDeleteGroupLink_Response = CR.GroupLinkDeleted | CR.ChatCmdError + + +# Get group link. +# Network usage: no. +class APIGetGroupLink(TypedDict): + groupId: int # int64 + + +def APIGetGroupLink_cmd_string(self: APIGetGroupLink) -> str: + return '/_get link #' + str(self['groupId']) + +APIGetGroupLink_Response = CR.GroupLink | CR.ChatCmdError + + +# Connection commands +# These commands may be used to create connections. Most bots do not need to use them - bot users will connect via bot address with auto-accept enabled. + +# Create 1-time invitation link. +# Network usage: interactive. +class APIAddContact(TypedDict): + userId: int # int64 + incognito: bool + + +def APIAddContact_cmd_string(self: APIAddContact) -> str: + return '/_connect ' + str(self['userId']) + (' incognito=on' if self['incognito'] else '') + +APIAddContact_Response = CR.Invitation | CR.ChatCmdError + + +# Determine SimpleX link type and if the bot is already connected via this link. +# Network usage: interactive. +class APIConnectPlan(TypedDict): + userId: int # int64 + connectionLink: NotRequired[str] + resolveKnown: bool + linkOwnerSig: NotRequired["T.LinkOwnerSig"] + + +def APIConnectPlan_cmd_string(self: APIConnectPlan) -> str: + return '/_connect plan ' + str(self['userId']) + ' ' + self.get('connectionLink') + +APIConnectPlan_Response = CR.ConnectionPlan | CR.ChatCmdError + + +# Connect via prepared SimpleX link. The link can be 1-time invitation link, contact address or group link. +# Network usage: interactive. +class APIConnect(TypedDict): + userId: int # int64 + incognito: bool + preparedLink_: NotRequired["T.CreatedConnLink"] + + +def APIConnect_cmd_string(self: APIConnect) -> str: + return '/_connect ' + str(self['userId']) + ((' ' + T.CreatedConnLink_cmd_string(self.get('preparedLink_'))) if self.get('preparedLink_') is not None else '') + +APIConnect_Response = CR.SentConfirmation | CR.ContactAlreadyExists | CR.SentInvitation | CR.ChatCmdError + + +# Connect via SimpleX link as string in the active user profile. +# Network usage: interactive. +class Connect(TypedDict): + incognito: bool + connLink_: NotRequired[str] + + +def Connect_cmd_string(self: Connect) -> str: + return '/connect' + ((' ' + self.get('connLink_')) if self.get('connLink_') is not None else '') + +Connect_Response = CR.SentConfirmation | CR.ContactAlreadyExists | CR.SentInvitation | CR.ChatCmdError + + +# Accept contact request. +# Network usage: interactive. +class APIAcceptContact(TypedDict): + contactReqId: int # int64 + + +def APIAcceptContact_cmd_string(self: APIAcceptContact) -> str: + return '/_accept ' + str(self['contactReqId']) + +APIAcceptContact_Response = CR.AcceptingContactRequest | CR.ChatCmdError + + +# Reject contact request. The user who sent the request is **not notified**. +# Network usage: no. +class APIRejectContact(TypedDict): + contactReqId: int # int64 + + +def APIRejectContact_cmd_string(self: APIRejectContact) -> str: + return '/_reject ' + str(self['contactReqId']) + +APIRejectContact_Response = CR.ContactRequestRejected | CR.ChatCmdError + + +# Chat commands +# Commands to list and delete conversations. + +# Get contacts. +# Network usage: no. +class APIListContacts(TypedDict): + userId: int # int64 + + +def APIListContacts_cmd_string(self: APIListContacts) -> str: + return '/_contacts ' + str(self['userId']) + +APIListContacts_Response = CR.ContactsList | CR.ChatCmdError + + +# Get groups. +# Network usage: no. +class APIListGroups(TypedDict): + userId: int # int64 + contactId_: NotRequired[int] # int64 + search: NotRequired[str] + + +def APIListGroups_cmd_string(self: APIListGroups) -> str: + return '/_groups ' + str(self['userId']) + ((' @' + str(self.get('contactId_'))) if self.get('contactId_') is not None else '') + ((' ' + self.get('search')) if self.get('search') is not None else '') + +APIListGroups_Response = CR.GroupsList | CR.ChatCmdError + + +# Get chat previews. Supports time-based pagination — use this instead of APIListContacts / APIListGroups when scanning at scale (those load every record into memory and fail on large databases). +# Network usage: no. +class APIGetChats(TypedDict): + userId: int # int64 + pendingConnections: bool + pagination: "T.PaginationByTime" + query: "T.ChatListQuery" + + +def APIGetChats_cmd_string(self: APIGetChats) -> str: + return '/_get chats ' + str(self['userId']) + (' pcc=on' if self['pendingConnections'] else '') + ' ' + T.PaginationByTime_cmd_string(self['pagination']) + ' ' + json.dumps(self['query']) + +APIGetChats_Response = CR.ApiChats | CR.ChatCmdError + + +# Delete chat. +# Network usage: background. +class APIDeleteChat(TypedDict): + chatRef: "T.ChatRef" + chatDeleteMode: "T.ChatDeleteMode" + + +def APIDeleteChat_cmd_string(self: APIDeleteChat) -> str: + return '/_delete ' + T.ChatRef_cmd_string(self['chatRef']) + ' ' + T.ChatDeleteMode_cmd_string(self['chatDeleteMode']) + +APIDeleteChat_Response = CR.ContactDeleted | CR.ContactConnectionDeleted | CR.GroupDeletedUser | CR.ChatCmdError + + +# Set group custom data. +# Network usage: no. +class APISetGroupCustomData(TypedDict): + groupId: int # int64 + customData: NotRequired[dict[str, object]] + + +def APISetGroupCustomData_cmd_string(self: APISetGroupCustomData) -> str: + return '/_set custom #' + str(self['groupId']) + ((' ' + json.dumps(self.get('customData'))) if self.get('customData') is not None else '') + +APISetGroupCustomData_Response = CR.CmdOk | CR.ChatCmdError + + +# Set contact custom data. +# Network usage: no. +class APISetContactCustomData(TypedDict): + contactId: int # int64 + customData: NotRequired[dict[str, object]] + + +def APISetContactCustomData_cmd_string(self: APISetContactCustomData) -> str: + return '/_set custom @' + str(self['contactId']) + ((' ' + json.dumps(self.get('customData'))) if self.get('customData') is not None else '') + +APISetContactCustomData_Response = CR.CmdOk | CR.ChatCmdError + + +# Set auto-accept member contacts. +# Network usage: no. +class APISetUserAutoAcceptMemberContacts(TypedDict): + userId: int # int64 + onOff: bool + + +def APISetUserAutoAcceptMemberContacts_cmd_string(self: APISetUserAutoAcceptMemberContacts) -> str: + return '/_set accept member contacts ' + str(self['userId']) + ' ' + ('on' if self['onOff'] else 'off') + +APISetUserAutoAcceptMemberContacts_Response = CR.CmdOk | CR.ChatCmdError + + +# User profile commands +# Most bots don't need to use these commands, as bot profile can be configured manually via CLI or desktop client. These commands can be used by bots that need to manage multiple user profiles (e.g., the profiles of support agents). + +# Get active user profile. +# Network usage: no. +class ShowActiveUser(TypedDict): + pass + + +def ShowActiveUser_cmd_string(self: ShowActiveUser) -> str: + return '/user' + +ShowActiveUser_Response = CR.ActiveUser | CR.ChatCmdError + + +# Create new user profile. +# Network usage: no. +class CreateActiveUser(TypedDict): + newUser: "T.NewUser" + + +def CreateActiveUser_cmd_string(self: CreateActiveUser) -> str: + return '/_create user ' + json.dumps(self['newUser']) + +CreateActiveUser_Response = CR.ActiveUser | CR.ChatCmdError + + +# Get all user profiles. +# Network usage: no. +class ListUsers(TypedDict): + pass + + +def ListUsers_cmd_string(self: ListUsers) -> str: + return '/users' + +ListUsers_Response = CR.UsersList | CR.ChatCmdError + + +# Set active user profile. +# Network usage: no. +class APISetActiveUser(TypedDict): + userId: int # int64 + viewPwd: NotRequired[str] + + +def APISetActiveUser_cmd_string(self: APISetActiveUser) -> str: + return '/_user ' + str(self['userId']) + ((' ' + json.dumps(self.get('viewPwd'))) if self.get('viewPwd') is not None else '') + +APISetActiveUser_Response = CR.ActiveUser | CR.ChatCmdError + + +# Delete user profile. +# Network usage: background. +class APIDeleteUser(TypedDict): + userId: int # int64 + delSMPQueues: bool + viewPwd: NotRequired[str] + + +def APIDeleteUser_cmd_string(self: APIDeleteUser) -> str: + return '/_delete user ' + str(self['userId']) + ' del_smp=' + ('on' if self['delSMPQueues'] else 'off') + ((' ' + json.dumps(self.get('viewPwd'))) if self.get('viewPwd') is not None else '') + +APIDeleteUser_Response = CR.CmdOk | CR.ChatCmdError + + +# Update user profile. +# Network usage: background. +class APIUpdateProfile(TypedDict): + userId: int # int64 + profile: "T.Profile" + + +def APIUpdateProfile_cmd_string(self: APIUpdateProfile) -> str: + return '/_profile ' + str(self['userId']) + ' ' + json.dumps(self['profile']) + +APIUpdateProfile_Response = CR.UserProfileUpdated | CR.UserProfileNoChange | CR.ChatCmdError + + +# Configure chat preference overrides for the contact. +# Network usage: background. +class APISetContactPrefs(TypedDict): + contactId: int # int64 + preferences: "T.Preferences" + + +def APISetContactPrefs_cmd_string(self: APISetContactPrefs) -> str: + return '/_set prefs @' + str(self['contactId']) + ' ' + json.dumps(self['preferences']) + +APISetContactPrefs_Response = CR.ContactPrefsUpdated | CR.ChatCmdError + + +# Chat management +# These commands should not be used with CLI-based bots + +# Start chat controller. +# Network usage: no. +class StartChat(TypedDict): + mainApp: bool + enableSndFiles: bool + + +def StartChat_cmd_string(self: StartChat) -> str: + return '/_start' + +StartChat_Response = CR.ChatStarted | CR.ChatRunning + + +# Stop chat controller. +# Network usage: no. +class APIStopChat(TypedDict): + pass + + +def APIStopChat_cmd_string(self: APIStopChat) -> str: + return '/_stop' + +APIStopChat_Response = CR.ChatStopped + diff --git a/packages/simplex-chat-python/src/simplex_chat/types/_events.py b/packages/simplex-chat-python/src/simplex_chat/types/_events.py new file mode 100644 index 0000000000..77484fbf3f --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/types/_events.py @@ -0,0 +1,379 @@ +# API Events +# This file is generated automatically. +from __future__ import annotations +from typing import Literal, NotRequired, TypedDict +from . import _types as T + +class ContactConnected(TypedDict): + type: Literal["contactConnected"] + user: "T.User" + contact: "T.Contact" + userCustomProfile: NotRequired["T.Profile"] + +class ContactUpdated(TypedDict): + type: Literal["contactUpdated"] + user: "T.User" + fromContact: "T.Contact" + toContact: "T.Contact" + +class ContactDeletedByContact(TypedDict): + type: Literal["contactDeletedByContact"] + user: "T.User" + contact: "T.Contact" + +class ReceivedContactRequest(TypedDict): + type: Literal["receivedContactRequest"] + user: "T.User" + contactRequest: "T.UserContactRequest" + chat_: NotRequired["T.AChat"] + +class NewMemberContactReceivedInv(TypedDict): + type: Literal["newMemberContactReceivedInv"] + user: "T.User" + contact: "T.Contact" + groupInfo: "T.GroupInfo" + member: "T.GroupMember" + +class ContactSndReady(TypedDict): + type: Literal["contactSndReady"] + user: "T.User" + contact: "T.Contact" + +class NewChatItems(TypedDict): + type: Literal["newChatItems"] + user: "T.User" + chatItems: list["T.AChatItem"] + +class ChatItemReaction(TypedDict): + type: Literal["chatItemReaction"] + user: "T.User" + added: bool + reaction: "T.ACIReaction" + +class ChatItemsDeleted(TypedDict): + type: Literal["chatItemsDeleted"] + user: "T.User" + chatItemDeletions: list["T.ChatItemDeletion"] + byUser: bool + timed: bool + +class ChatItemUpdated(TypedDict): + type: Literal["chatItemUpdated"] + user: "T.User" + chatItem: "T.AChatItem" + +class GroupChatItemsDeleted(TypedDict): + type: Literal["groupChatItemsDeleted"] + user: "T.User" + groupInfo: "T.GroupInfo" + chatItemIDs: list[int] # int64 + byUser: bool + member_: NotRequired["T.GroupMember"] + +class ChatItemsStatusesUpdated(TypedDict): + type: Literal["chatItemsStatusesUpdated"] + user: "T.User" + chatItems: list["T.AChatItem"] + +class ReceivedGroupInvitation(TypedDict): + type: Literal["receivedGroupInvitation"] + user: "T.User" + groupInfo: "T.GroupInfo" + contact: "T.Contact" + fromMemberRole: "T.GroupMemberRole" + memberRole: "T.GroupMemberRole" + +class UserJoinedGroup(TypedDict): + type: Literal["userJoinedGroup"] + user: "T.User" + groupInfo: "T.GroupInfo" + hostMember: "T.GroupMember" + +class GroupUpdated(TypedDict): + type: Literal["groupUpdated"] + user: "T.User" + fromGroup: "T.GroupInfo" + toGroup: "T.GroupInfo" + member_: NotRequired["T.GroupMember"] + msgSigned: NotRequired["T.MsgSigStatus"] + +class JoinedGroupMember(TypedDict): + type: Literal["joinedGroupMember"] + user: "T.User" + groupInfo: "T.GroupInfo" + member: "T.GroupMember" + +class MemberRole(TypedDict): + type: Literal["memberRole"] + user: "T.User" + groupInfo: "T.GroupInfo" + byMember: "T.GroupMember" + member: "T.GroupMember" + fromRole: "T.GroupMemberRole" + toRole: "T.GroupMemberRole" + msgSigned: NotRequired["T.MsgSigStatus"] + +class DeletedMember(TypedDict): + type: Literal["deletedMember"] + user: "T.User" + groupInfo: "T.GroupInfo" + byMember: "T.GroupMember" + deletedMember: "T.GroupMember" + withMessages: bool + msgSigned: NotRequired["T.MsgSigStatus"] + +class LeftMember(TypedDict): + type: Literal["leftMember"] + user: "T.User" + groupInfo: "T.GroupInfo" + member: "T.GroupMember" + msgSigned: NotRequired["T.MsgSigStatus"] + +class DeletedMemberUser(TypedDict): + type: Literal["deletedMemberUser"] + user: "T.User" + groupInfo: "T.GroupInfo" + member: "T.GroupMember" + withMessages: bool + msgSigned: NotRequired["T.MsgSigStatus"] + +class GroupDeleted(TypedDict): + type: Literal["groupDeleted"] + user: "T.User" + groupInfo: "T.GroupInfo" + member: "T.GroupMember" + msgSigned: NotRequired["T.MsgSigStatus"] + +class ConnectedToGroupMember(TypedDict): + type: Literal["connectedToGroupMember"] + user: "T.User" + groupInfo: "T.GroupInfo" + member: "T.GroupMember" + memberContact: NotRequired["T.Contact"] + +class MemberAcceptedByOther(TypedDict): + type: Literal["memberAcceptedByOther"] + user: "T.User" + groupInfo: "T.GroupInfo" + acceptingMember: "T.GroupMember" + member: "T.GroupMember" + +class MemberBlockedForAll(TypedDict): + type: Literal["memberBlockedForAll"] + user: "T.User" + groupInfo: "T.GroupInfo" + byMember: "T.GroupMember" + member: "T.GroupMember" + blocked: bool + msgSigned: NotRequired["T.MsgSigStatus"] + +class GroupMemberUpdated(TypedDict): + type: Literal["groupMemberUpdated"] + user: "T.User" + groupInfo: "T.GroupInfo" + fromMember: "T.GroupMember" + toMember: "T.GroupMember" + +class GroupLinkDataUpdated(TypedDict): + type: Literal["groupLinkDataUpdated"] + user: "T.User" + groupInfo: "T.GroupInfo" + groupLink: "T.GroupLink" + groupRelays: list["T.GroupRelay"] + relaysChanged: bool + +class GroupRelayUpdated(TypedDict): + type: Literal["groupRelayUpdated"] + user: "T.User" + groupInfo: "T.GroupInfo" + member: "T.GroupMember" + groupRelay: "T.GroupRelay" + +class RcvFileDescrReady(TypedDict): + type: Literal["rcvFileDescrReady"] + user: "T.User" + chatItem: "T.AChatItem" + rcvFileTransfer: "T.RcvFileTransfer" + rcvFileDescr: "T.RcvFileDescr" + +class RcvFileComplete(TypedDict): + type: Literal["rcvFileComplete"] + user: "T.User" + chatItem: "T.AChatItem" + +class SndFileCompleteXFTP(TypedDict): + type: Literal["sndFileCompleteXFTP"] + user: "T.User" + chatItem: "T.AChatItem" + fileTransferMeta: "T.FileTransferMeta" + +class RcvFileStart(TypedDict): + type: Literal["rcvFileStart"] + user: "T.User" + chatItem: "T.AChatItem" + +class RcvFileSndCancelled(TypedDict): + type: Literal["rcvFileSndCancelled"] + user: "T.User" + chatItem: "T.AChatItem" + rcvFileTransfer: "T.RcvFileTransfer" + +class RcvFileAccepted(TypedDict): + type: Literal["rcvFileAccepted"] + user: "T.User" + chatItem: "T.AChatItem" + +class RcvFileError(TypedDict): + type: Literal["rcvFileError"] + user: "T.User" + chatItem_: NotRequired["T.AChatItem"] + agentError: "T.AgentErrorType" + rcvFileTransfer: "T.RcvFileTransfer" + +class RcvFileWarning(TypedDict): + type: Literal["rcvFileWarning"] + user: "T.User" + chatItem_: NotRequired["T.AChatItem"] + agentError: "T.AgentErrorType" + rcvFileTransfer: "T.RcvFileTransfer" + +class SndFileError(TypedDict): + type: Literal["sndFileError"] + user: "T.User" + chatItem_: NotRequired["T.AChatItem"] + fileTransferMeta: "T.FileTransferMeta" + errorMessage: str + +class SndFileWarning(TypedDict): + type: Literal["sndFileWarning"] + user: "T.User" + chatItem_: NotRequired["T.AChatItem"] + fileTransferMeta: "T.FileTransferMeta" + errorMessage: str + +class AcceptingContactRequest(TypedDict): + type: Literal["acceptingContactRequest"] + user: "T.User" + contact: "T.Contact" + +class AcceptingBusinessRequest(TypedDict): + type: Literal["acceptingBusinessRequest"] + user: "T.User" + groupInfo: "T.GroupInfo" + +class ContactConnecting(TypedDict): + type: Literal["contactConnecting"] + user: "T.User" + contact: "T.Contact" + +class BusinessLinkConnecting(TypedDict): + type: Literal["businessLinkConnecting"] + user: "T.User" + groupInfo: "T.GroupInfo" + hostMember: "T.GroupMember" + fromContact: "T.Contact" + +class JoinedGroupMemberConnecting(TypedDict): + type: Literal["joinedGroupMemberConnecting"] + user: "T.User" + groupInfo: "T.GroupInfo" + hostMember: "T.GroupMember" + member: "T.GroupMember" + +class SentGroupInvitation(TypedDict): + type: Literal["sentGroupInvitation"] + user: "T.User" + groupInfo: "T.GroupInfo" + contact: "T.Contact" + member: "T.GroupMember" + +class GroupLinkConnecting(TypedDict): + type: Literal["groupLinkConnecting"] + user: "T.User" + groupInfo: "T.GroupInfo" + hostMember: "T.GroupMember" + +class HostConnected(TypedDict): + type: Literal["hostConnected"] + protocol: str + transportHost: str + +class HostDisconnected(TypedDict): + type: Literal["hostDisconnected"] + protocol: str + transportHost: str + +class SubscriptionStatus(TypedDict): + type: Literal["subscriptionStatus"] + server: str + subscriptionStatus: "T.SubscriptionStatus" + connections: list[str] + +class MessageError(TypedDict): + type: Literal["messageError"] + user: "T.User" + severity: str + errorMessage: str + +class ChatError(TypedDict): + type: Literal["chatError"] + chatError: "T.ChatError" + +class ChatErrors(TypedDict): + type: Literal["chatErrors"] + chatErrors: list["T.ChatError"] + +ChatEvent = ( + ContactConnected + | ContactUpdated + | ContactDeletedByContact + | ReceivedContactRequest + | NewMemberContactReceivedInv + | ContactSndReady + | NewChatItems + | ChatItemReaction + | ChatItemsDeleted + | ChatItemUpdated + | GroupChatItemsDeleted + | ChatItemsStatusesUpdated + | ReceivedGroupInvitation + | UserJoinedGroup + | GroupUpdated + | JoinedGroupMember + | MemberRole + | DeletedMember + | LeftMember + | DeletedMemberUser + | GroupDeleted + | ConnectedToGroupMember + | MemberAcceptedByOther + | MemberBlockedForAll + | GroupMemberUpdated + | GroupLinkDataUpdated + | GroupRelayUpdated + | RcvFileDescrReady + | RcvFileComplete + | SndFileCompleteXFTP + | RcvFileStart + | RcvFileSndCancelled + | RcvFileAccepted + | RcvFileError + | RcvFileWarning + | SndFileError + | SndFileWarning + | AcceptingContactRequest + | AcceptingBusinessRequest + | ContactConnecting + | BusinessLinkConnecting + | JoinedGroupMemberConnecting + | SentGroupInvitation + | GroupLinkConnecting + | HostConnected + | HostDisconnected + | SubscriptionStatus + | MessageError + | ChatError + | ChatErrors +) + +ChatEvent_Tag = Literal["contactConnected", "contactUpdated", "contactDeletedByContact", "receivedContactRequest", "newMemberContactReceivedInv", "contactSndReady", "newChatItems", "chatItemReaction", "chatItemsDeleted", "chatItemUpdated", "groupChatItemsDeleted", "chatItemsStatusesUpdated", "receivedGroupInvitation", "userJoinedGroup", "groupUpdated", "joinedGroupMember", "memberRole", "deletedMember", "leftMember", "deletedMemberUser", "groupDeleted", "connectedToGroupMember", "memberAcceptedByOther", "memberBlockedForAll", "groupMemberUpdated", "groupLinkDataUpdated", "groupRelayUpdated", "rcvFileDescrReady", "rcvFileComplete", "sndFileCompleteXFTP", "rcvFileStart", "rcvFileSndCancelled", "rcvFileAccepted", "rcvFileError", "rcvFileWarning", "sndFileError", "sndFileWarning", "acceptingContactRequest", "acceptingBusinessRequest", "contactConnecting", "businessLinkConnecting", "joinedGroupMemberConnecting", "sentGroupInvitation", "groupLinkConnecting", "hostConnected", "hostDisconnected", "subscriptionStatus", "messageError", "chatError", "chatErrors"] diff --git a/packages/simplex-chat-python/src/simplex_chat/types/_responses.py b/packages/simplex-chat-python/src/simplex_chat/types/_responses.py new file mode 100644 index 0000000000..84d0f1c79f --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/types/_responses.py @@ -0,0 +1,360 @@ +# API Responses +# This file is generated automatically. +from __future__ import annotations +from typing import Literal, NotRequired, TypedDict +from . import _types as T + +class AcceptingContactRequest(TypedDict): + type: Literal["acceptingContactRequest"] + user: "T.User" + contact: "T.Contact" + +class ActiveUser(TypedDict): + type: Literal["activeUser"] + user: "T.User" + +class ChatItemNotChanged(TypedDict): + type: Literal["chatItemNotChanged"] + user: "T.User" + chatItem: "T.AChatItem" + +class ChatItemReaction(TypedDict): + type: Literal["chatItemReaction"] + user: "T.User" + added: bool + reaction: "T.ACIReaction" + +class ChatItemUpdated(TypedDict): + type: Literal["chatItemUpdated"] + user: "T.User" + chatItem: "T.AChatItem" + +class ChatItemsDeleted(TypedDict): + type: Literal["chatItemsDeleted"] + user: "T.User" + chatItemDeletions: list["T.ChatItemDeletion"] + byUser: bool + timed: bool + +class ChatRunning(TypedDict): + type: Literal["chatRunning"] + +class ChatStarted(TypedDict): + type: Literal["chatStarted"] + +class ChatStopped(TypedDict): + type: Literal["chatStopped"] + +class CmdOk(TypedDict): + type: Literal["cmdOk"] + user_: NotRequired["T.User"] + +class ChatCmdError(TypedDict): + type: Literal["chatCmdError"] + chatError: "T.ChatError" + +class ConnectionPlan(TypedDict): + type: Literal["connectionPlan"] + user: "T.User" + connLink: "T.CreatedConnLink" + connectionPlan: "T.ConnectionPlan" + +class ContactAlreadyExists(TypedDict): + type: Literal["contactAlreadyExists"] + user: "T.User" + contact: "T.Contact" + +class ContactConnectionDeleted(TypedDict): + type: Literal["contactConnectionDeleted"] + user: "T.User" + connection: "T.PendingContactConnection" + +class ContactDeleted(TypedDict): + type: Literal["contactDeleted"] + user: "T.User" + contact: "T.Contact" + +class ContactPrefsUpdated(TypedDict): + type: Literal["contactPrefsUpdated"] + user: "T.User" + fromContact: "T.Contact" + toContact: "T.Contact" + +class ContactRequestRejected(TypedDict): + type: Literal["contactRequestRejected"] + user: "T.User" + contactRequest: "T.UserContactRequest" + contact_: NotRequired["T.Contact"] + +class ContactsList(TypedDict): + type: Literal["contactsList"] + user: "T.User" + contacts: list["T.Contact"] + +class GroupDeletedUser(TypedDict): + type: Literal["groupDeletedUser"] + user: "T.User" + groupInfo: "T.GroupInfo" + msgSigned: bool + +class GroupLink(TypedDict): + type: Literal["groupLink"] + user: "T.User" + groupInfo: "T.GroupInfo" + groupLink: "T.GroupLink" + +class GroupLinkCreated(TypedDict): + type: Literal["groupLinkCreated"] + user: "T.User" + groupInfo: "T.GroupInfo" + groupLink: "T.GroupLink" + +class GroupLinkDeleted(TypedDict): + type: Literal["groupLinkDeleted"] + user: "T.User" + groupInfo: "T.GroupInfo" + +class GroupCreated(TypedDict): + type: Literal["groupCreated"] + user: "T.User" + groupInfo: "T.GroupInfo" + +class PublicGroupCreated(TypedDict): + type: Literal["publicGroupCreated"] + user: "T.User" + groupInfo: "T.GroupInfo" + groupLink: "T.GroupLink" + groupRelays: list["T.GroupRelay"] + +class PublicGroupCreationFailed(TypedDict): + type: Literal["publicGroupCreationFailed"] + user: "T.User" + addRelayResults: list["T.AddRelayResult"] + +class GroupRelays(TypedDict): + type: Literal["groupRelays"] + user: "T.User" + groupInfo: "T.GroupInfo" + groupRelays: list["T.GroupRelay"] + +class GroupRelaysAdded(TypedDict): + type: Literal["groupRelaysAdded"] + user: "T.User" + groupInfo: "T.GroupInfo" + groupLink: "T.GroupLink" + groupRelays: list["T.GroupRelay"] + +class GroupRelaysAddFailed(TypedDict): + type: Literal["groupRelaysAddFailed"] + user: "T.User" + addRelayResults: list["T.AddRelayResult"] + +class GroupMembers(TypedDict): + type: Literal["groupMembers"] + user: "T.User" + group: "T.Group" + +class GroupUpdated(TypedDict): + type: Literal["groupUpdated"] + user: "T.User" + fromGroup: "T.GroupInfo" + toGroup: "T.GroupInfo" + member_: NotRequired["T.GroupMember"] + msgSigned: bool + +class GroupsList(TypedDict): + type: Literal["groupsList"] + user: "T.User" + groups: list["T.GroupInfo"] + +class Invitation(TypedDict): + type: Literal["invitation"] + user: "T.User" + connLinkInvitation: "T.CreatedConnLink" + connection: "T.PendingContactConnection" + +class LeftMemberUser(TypedDict): + type: Literal["leftMemberUser"] + user: "T.User" + groupInfo: "T.GroupInfo" + +class MemberAccepted(TypedDict): + type: Literal["memberAccepted"] + user: "T.User" + groupInfo: "T.GroupInfo" + member: "T.GroupMember" + +class MembersBlockedForAllUser(TypedDict): + type: Literal["membersBlockedForAllUser"] + user: "T.User" + groupInfo: "T.GroupInfo" + members: list["T.GroupMember"] + blocked: bool + msgSigned: bool + +class MembersRoleUser(TypedDict): + type: Literal["membersRoleUser"] + user: "T.User" + groupInfo: "T.GroupInfo" + members: list["T.GroupMember"] + toRole: "T.GroupMemberRole" + msgSigned: bool + +class NewChatItems(TypedDict): + type: Literal["newChatItems"] + user: "T.User" + chatItems: list["T.AChatItem"] + +class RcvFileAccepted(TypedDict): + type: Literal["rcvFileAccepted"] + user: "T.User" + chatItem: "T.AChatItem" + +class RcvFileAcceptedSndCancelled(TypedDict): + type: Literal["rcvFileAcceptedSndCancelled"] + user: "T.User" + rcvFileTransfer: "T.RcvFileTransfer" + +class RcvFileCancelled(TypedDict): + type: Literal["rcvFileCancelled"] + user: "T.User" + chatItem_: NotRequired["T.AChatItem"] + rcvFileTransfer: "T.RcvFileTransfer" + +class SentConfirmation(TypedDict): + type: Literal["sentConfirmation"] + user: "T.User" + connection: "T.PendingContactConnection" + customUserProfile: NotRequired["T.Profile"] + +class SentGroupInvitation(TypedDict): + type: Literal["sentGroupInvitation"] + user: "T.User" + groupInfo: "T.GroupInfo" + contact: "T.Contact" + member: "T.GroupMember" + +class SentInvitation(TypedDict): + type: Literal["sentInvitation"] + user: "T.User" + connection: "T.PendingContactConnection" + customUserProfile: NotRequired["T.Profile"] + +class SndFileCancelled(TypedDict): + type: Literal["sndFileCancelled"] + user: "T.User" + chatItem_: NotRequired["T.AChatItem"] + fileTransferMeta: "T.FileTransferMeta" + sndFileTransfers: list["T.SndFileTransfer"] + +class UserAcceptedGroupSent(TypedDict): + type: Literal["userAcceptedGroupSent"] + user: "T.User" + groupInfo: "T.GroupInfo" + hostContact: NotRequired["T.Contact"] + +class UserContactLink(TypedDict): + type: Literal["userContactLink"] + user: "T.User" + contactLink: "T.UserContactLink" + +class UserContactLinkCreated(TypedDict): + type: Literal["userContactLinkCreated"] + user: "T.User" + connLinkContact: "T.CreatedConnLink" + +class UserContactLinkDeleted(TypedDict): + type: Literal["userContactLinkDeleted"] + user: "T.User" + +class UserContactLinkUpdated(TypedDict): + type: Literal["userContactLinkUpdated"] + user: "T.User" + contactLink: "T.UserContactLink" + +class UserDeletedMembers(TypedDict): + type: Literal["userDeletedMembers"] + user: "T.User" + groupInfo: "T.GroupInfo" + members: list["T.GroupMember"] + withMessages: bool + msgSigned: bool + +class UserProfileUpdated(TypedDict): + type: Literal["userProfileUpdated"] + user: "T.User" + fromProfile: "T.Profile" + toProfile: "T.Profile" + updateSummary: "T.UserProfileUpdateSummary" + +class UserProfileNoChange(TypedDict): + type: Literal["userProfileNoChange"] + user: "T.User" + +class UsersList(TypedDict): + type: Literal["usersList"] + users: list["T.UserInfo"] + +class ApiChats(TypedDict): + type: Literal["apiChats"] + user: "T.User" + chats: list["T.AChat"] + +ChatResponse = ( + AcceptingContactRequest + | ActiveUser + | ChatItemNotChanged + | ChatItemReaction + | ChatItemUpdated + | ChatItemsDeleted + | ChatRunning + | ChatStarted + | ChatStopped + | CmdOk + | ChatCmdError + | ConnectionPlan + | ContactAlreadyExists + | ContactConnectionDeleted + | ContactDeleted + | ContactPrefsUpdated + | ContactRequestRejected + | ContactsList + | GroupDeletedUser + | GroupLink + | GroupLinkCreated + | GroupLinkDeleted + | GroupCreated + | PublicGroupCreated + | PublicGroupCreationFailed + | GroupRelays + | GroupRelaysAdded + | GroupRelaysAddFailed + | GroupMembers + | GroupUpdated + | GroupsList + | Invitation + | LeftMemberUser + | MemberAccepted + | MembersBlockedForAllUser + | MembersRoleUser + | NewChatItems + | RcvFileAccepted + | RcvFileAcceptedSndCancelled + | RcvFileCancelled + | SentConfirmation + | SentGroupInvitation + | SentInvitation + | SndFileCancelled + | UserAcceptedGroupSent + | UserContactLink + | UserContactLinkCreated + | UserContactLinkDeleted + | UserContactLinkUpdated + | UserDeletedMembers + | UserProfileUpdated + | UserProfileNoChange + | UsersList + | ApiChats +) + +ChatResponse_Tag = Literal["acceptingContactRequest", "activeUser", "chatItemNotChanged", "chatItemReaction", "chatItemUpdated", "chatItemsDeleted", "chatRunning", "chatStarted", "chatStopped", "cmdOk", "chatCmdError", "connectionPlan", "contactAlreadyExists", "contactConnectionDeleted", "contactDeleted", "contactPrefsUpdated", "contactRequestRejected", "contactsList", "groupDeletedUser", "groupLink", "groupLinkCreated", "groupLinkDeleted", "groupCreated", "publicGroupCreated", "publicGroupCreationFailed", "groupRelays", "groupRelaysAdded", "groupRelaysAddFailed", "groupMembers", "groupUpdated", "groupsList", "invitation", "leftMemberUser", "memberAccepted", "membersBlockedForAllUser", "membersRoleUser", "newChatItems", "rcvFileAccepted", "rcvFileAcceptedSndCancelled", "rcvFileCancelled", "sentConfirmation", "sentGroupInvitation", "sentInvitation", "sndFileCancelled", "userAcceptedGroupSent", "userContactLink", "userContactLinkCreated", "userContactLinkDeleted", "userContactLinkUpdated", "userDeletedMembers", "userProfileUpdated", "userProfileNoChange", "usersList", "apiChats"] diff --git a/packages/simplex-chat-python/src/simplex_chat/types/_types.py b/packages/simplex-chat-python/src/simplex_chat/types/_types.py new file mode 100644 index 0000000000..8fd700a8a2 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/types/_types.py @@ -0,0 +1,3506 @@ +# API Types +# This file is generated automatically. +from __future__ import annotations +from typing import Literal, NotRequired, TypedDict + +class ACIReaction(TypedDict): + chatInfo: "ChatInfo" + chatReaction: "CIReaction" + +class AChat(TypedDict): + chatInfo: "ChatInfo" + chatItems: list["ChatItem"] + chatStats: "ChatStats" + +class AChatItem(TypedDict): + chatInfo: "ChatInfo" + chatItem: "ChatItem" + +class AddRelayResult(TypedDict): + relay: "UserChatRelay" + relayError: NotRequired["ChatError"] + +class AddressSettings(TypedDict): + businessAddress: bool + autoAccept: NotRequired["AutoAccept"] + autoReply: NotRequired["MsgContent"] + +class AgentCryptoError_DECRYPT_AES(TypedDict): + type: Literal["DECRYPT_AES"] + +class AgentCryptoError_DECRYPT_CB(TypedDict): + type: Literal["DECRYPT_CB"] + +class AgentCryptoError_RATCHET_HEADER(TypedDict): + type: Literal["RATCHET_HEADER"] + +class AgentCryptoError_RATCHET_SYNC(TypedDict): + type: Literal["RATCHET_SYNC"] + +AgentCryptoError = ( + AgentCryptoError_DECRYPT_AES + | AgentCryptoError_DECRYPT_CB + | AgentCryptoError_RATCHET_HEADER + | AgentCryptoError_RATCHET_SYNC +) + +AgentCryptoError_Tag = Literal["DECRYPT_AES", "DECRYPT_CB", "RATCHET_HEADER", "RATCHET_SYNC"] + +class AgentErrorType_CMD(TypedDict): + type: Literal["CMD"] + cmdErr: "CommandErrorType" + errContext: str + +class AgentErrorType_CONN(TypedDict): + type: Literal["CONN"] + connErr: "ConnectionErrorType" + errContext: str + +class AgentErrorType_NO_USER(TypedDict): + type: Literal["NO_USER"] + +class AgentErrorType_SMP(TypedDict): + type: Literal["SMP"] + serverAddress: str + smpErr: "ErrorType" + +class AgentErrorType_NTF(TypedDict): + type: Literal["NTF"] + serverAddress: str + ntfErr: "ErrorType" + +class AgentErrorType_XFTP(TypedDict): + type: Literal["XFTP"] + serverAddress: str + xftpErr: "XFTPErrorType" + +class AgentErrorType_FILE(TypedDict): + type: Literal["FILE"] + fileErr: "FileErrorType" + +class AgentErrorType_PROXY(TypedDict): + type: Literal["PROXY"] + proxyServer: str + relayServer: str + proxyErr: "ProxyClientError" + +class AgentErrorType_RCP(TypedDict): + type: Literal["RCP"] + rcpErr: "RCErrorType" + +class AgentErrorType_BROKER(TypedDict): + type: Literal["BROKER"] + brokerAddress: str + brokerErr: "BrokerErrorType" + +class AgentErrorType_AGENT(TypedDict): + type: Literal["AGENT"] + agentErr: "SMPAgentError" + +class AgentErrorType_NOTICE(TypedDict): + type: Literal["NOTICE"] + server: str + preset: bool + expiresAt: NotRequired[str] # ISO-8601 timestamp + +class AgentErrorType_INTERNAL(TypedDict): + type: Literal["INTERNAL"] + internalErr: str + +class AgentErrorType_CRITICAL(TypedDict): + type: Literal["CRITICAL"] + offerRestart: bool + criticalErr: str + +class AgentErrorType_INACTIVE(TypedDict): + type: Literal["INACTIVE"] + +AgentErrorType = ( + AgentErrorType_CMD + | AgentErrorType_CONN + | AgentErrorType_NO_USER + | AgentErrorType_SMP + | AgentErrorType_NTF + | AgentErrorType_XFTP + | AgentErrorType_FILE + | AgentErrorType_PROXY + | AgentErrorType_RCP + | AgentErrorType_BROKER + | AgentErrorType_AGENT + | AgentErrorType_NOTICE + | AgentErrorType_INTERNAL + | AgentErrorType_CRITICAL + | AgentErrorType_INACTIVE +) + +AgentErrorType_Tag = Literal["CMD", "CONN", "NO_USER", "SMP", "NTF", "XFTP", "FILE", "PROXY", "RCP", "BROKER", "AGENT", "NOTICE", "INTERNAL", "CRITICAL", "INACTIVE"] + +class AutoAccept(TypedDict): + acceptIncognito: bool + +class BlockingInfo(TypedDict): + reason: "BlockingReason" + notice: NotRequired["ClientNotice"] + +BlockingReason = Literal["spam", "content"] + +class BrokerErrorType_RESPONSE(TypedDict): + type: Literal["RESPONSE"] + respErr: str + +class BrokerErrorType_UNEXPECTED(TypedDict): + type: Literal["UNEXPECTED"] + respErr: str + +class BrokerErrorType_NETWORK(TypedDict): + type: Literal["NETWORK"] + networkError: "NetworkError" + +class BrokerErrorType_HOST(TypedDict): + type: Literal["HOST"] + +class BrokerErrorType_NO_SERVICE(TypedDict): + type: Literal["NO_SERVICE"] + +class BrokerErrorType_TRANSPORT(TypedDict): + type: Literal["TRANSPORT"] + transportErr: "TransportError" + +class BrokerErrorType_TIMEOUT(TypedDict): + type: Literal["TIMEOUT"] + +BrokerErrorType = ( + BrokerErrorType_RESPONSE + | BrokerErrorType_UNEXPECTED + | BrokerErrorType_NETWORK + | BrokerErrorType_HOST + | BrokerErrorType_NO_SERVICE + | BrokerErrorType_TRANSPORT + | BrokerErrorType_TIMEOUT +) + +BrokerErrorType_Tag = Literal["RESPONSE", "UNEXPECTED", "NETWORK", "HOST", "NO_SERVICE", "TRANSPORT", "TIMEOUT"] + +class BusinessChatInfo(TypedDict): + chatType: "BusinessChatType" + businessId: str + customerId: str + +BusinessChatType = Literal["business", "customer"] + +CICallStatus = Literal["pending", "missed", "rejected", "accepted", "negotiated", "progress", "ended", "error"] + +class CIContent_sndMsgContent(TypedDict): + type: Literal["sndMsgContent"] + msgContent: "MsgContent" + +class CIContent_rcvMsgContent(TypedDict): + type: Literal["rcvMsgContent"] + msgContent: "MsgContent" + +class CIContent_sndDeleted(TypedDict): + type: Literal["sndDeleted"] + deleteMode: "CIDeleteMode" + +class CIContent_rcvDeleted(TypedDict): + type: Literal["rcvDeleted"] + deleteMode: "CIDeleteMode" + +class CIContent_sndCall(TypedDict): + type: Literal["sndCall"] + status: "CICallStatus" + duration: int # int + +class CIContent_rcvCall(TypedDict): + type: Literal["rcvCall"] + status: "CICallStatus" + duration: int # int + +class CIContent_rcvIntegrityError(TypedDict): + type: Literal["rcvIntegrityError"] + msgError: "MsgErrorType" + +class CIContent_rcvDecryptionError(TypedDict): + type: Literal["rcvDecryptionError"] + msgDecryptError: "MsgDecryptError" + msgCount: int # word32 + +class CIContent_rcvMsgError(TypedDict): + type: Literal["rcvMsgError"] + rcvMsgError: "RcvMsgError" + +class CIContent_rcvGroupInvitation(TypedDict): + type: Literal["rcvGroupInvitation"] + groupInvitation: "CIGroupInvitation" + memberRole: "GroupMemberRole" + +class CIContent_sndGroupInvitation(TypedDict): + type: Literal["sndGroupInvitation"] + groupInvitation: "CIGroupInvitation" + memberRole: "GroupMemberRole" + +class CIContent_rcvDirectEvent(TypedDict): + type: Literal["rcvDirectEvent"] + rcvDirectEvent: "RcvDirectEvent" + +class CIContent_rcvGroupEvent(TypedDict): + type: Literal["rcvGroupEvent"] + rcvGroupEvent: "RcvGroupEvent" + +class CIContent_sndGroupEvent(TypedDict): + type: Literal["sndGroupEvent"] + sndGroupEvent: "SndGroupEvent" + +class CIContent_rcvConnEvent(TypedDict): + type: Literal["rcvConnEvent"] + rcvConnEvent: "RcvConnEvent" + +class CIContent_sndConnEvent(TypedDict): + type: Literal["sndConnEvent"] + sndConnEvent: "SndConnEvent" + +class CIContent_rcvChatFeature(TypedDict): + type: Literal["rcvChatFeature"] + feature: "ChatFeature" + enabled: "PrefEnabled" + param: NotRequired[int] # int + +class CIContent_sndChatFeature(TypedDict): + type: Literal["sndChatFeature"] + feature: "ChatFeature" + enabled: "PrefEnabled" + param: NotRequired[int] # int + +class CIContent_rcvChatPreference(TypedDict): + type: Literal["rcvChatPreference"] + feature: "ChatFeature" + allowed: "FeatureAllowed" + param: NotRequired[int] # int + +class CIContent_sndChatPreference(TypedDict): + type: Literal["sndChatPreference"] + feature: "ChatFeature" + allowed: "FeatureAllowed" + param: NotRequired[int] # int + +class CIContent_rcvGroupFeature(TypedDict): + type: Literal["rcvGroupFeature"] + groupFeature: "GroupFeature" + preference: "GroupPreference" + param: NotRequired[int] # int + memberRole_: NotRequired["GroupMemberRole"] + +class CIContent_sndGroupFeature(TypedDict): + type: Literal["sndGroupFeature"] + groupFeature: "GroupFeature" + preference: "GroupPreference" + param: NotRequired[int] # int + memberRole_: NotRequired["GroupMemberRole"] + +class CIContent_rcvChatFeatureRejected(TypedDict): + type: Literal["rcvChatFeatureRejected"] + feature: "ChatFeature" + +class CIContent_rcvGroupFeatureRejected(TypedDict): + type: Literal["rcvGroupFeatureRejected"] + groupFeature: "GroupFeature" + +class CIContent_sndModerated(TypedDict): + type: Literal["sndModerated"] + +class CIContent_rcvModerated(TypedDict): + type: Literal["rcvModerated"] + +class CIContent_rcvBlocked(TypedDict): + type: Literal["rcvBlocked"] + +class CIContent_sndDirectE2EEInfo(TypedDict): + type: Literal["sndDirectE2EEInfo"] + e2eeInfo: "E2EInfo" + +class CIContent_rcvDirectE2EEInfo(TypedDict): + type: Literal["rcvDirectE2EEInfo"] + e2eeInfo: "E2EInfo" + +class CIContent_sndGroupE2EEInfo(TypedDict): + type: Literal["sndGroupE2EEInfo"] + e2eeInfo: "E2EInfo" + +class CIContent_rcvGroupE2EEInfo(TypedDict): + type: Literal["rcvGroupE2EEInfo"] + e2eeInfo: "E2EInfo" + +class CIContent_chatBanner(TypedDict): + type: Literal["chatBanner"] + +CIContent = ( + CIContent_sndMsgContent + | CIContent_rcvMsgContent + | CIContent_sndDeleted + | CIContent_rcvDeleted + | CIContent_sndCall + | CIContent_rcvCall + | CIContent_rcvIntegrityError + | CIContent_rcvDecryptionError + | CIContent_rcvMsgError + | CIContent_rcvGroupInvitation + | CIContent_sndGroupInvitation + | CIContent_rcvDirectEvent + | CIContent_rcvGroupEvent + | CIContent_sndGroupEvent + | CIContent_rcvConnEvent + | CIContent_sndConnEvent + | CIContent_rcvChatFeature + | CIContent_sndChatFeature + | CIContent_rcvChatPreference + | CIContent_sndChatPreference + | CIContent_rcvGroupFeature + | CIContent_sndGroupFeature + | CIContent_rcvChatFeatureRejected + | CIContent_rcvGroupFeatureRejected + | CIContent_sndModerated + | CIContent_rcvModerated + | CIContent_rcvBlocked + | CIContent_sndDirectE2EEInfo + | CIContent_rcvDirectE2EEInfo + | CIContent_sndGroupE2EEInfo + | CIContent_rcvGroupE2EEInfo + | CIContent_chatBanner +) + +CIContent_Tag = Literal["sndMsgContent", "rcvMsgContent", "sndDeleted", "rcvDeleted", "sndCall", "rcvCall", "rcvIntegrityError", "rcvDecryptionError", "rcvMsgError", "rcvGroupInvitation", "sndGroupInvitation", "rcvDirectEvent", "rcvGroupEvent", "sndGroupEvent", "rcvConnEvent", "sndConnEvent", "rcvChatFeature", "sndChatFeature", "rcvChatPreference", "sndChatPreference", "rcvGroupFeature", "sndGroupFeature", "rcvChatFeatureRejected", "rcvGroupFeatureRejected", "sndModerated", "rcvModerated", "rcvBlocked", "sndDirectE2EEInfo", "rcvDirectE2EEInfo", "sndGroupE2EEInfo", "rcvGroupE2EEInfo", "chatBanner"] + +CIDeleteMode = Literal["broadcast", "internal", "internalMark", "history"] + +class CIDeleted_deleted(TypedDict): + type: Literal["deleted"] + deletedTs: NotRequired[str] # ISO-8601 timestamp + chatType: "ChatType" + +class CIDeleted_blocked(TypedDict): + type: Literal["blocked"] + deletedTs: NotRequired[str] # ISO-8601 timestamp + +class CIDeleted_blockedByAdmin(TypedDict): + type: Literal["blockedByAdmin"] + deletedTs: NotRequired[str] # ISO-8601 timestamp + +class CIDeleted_moderated(TypedDict): + type: Literal["moderated"] + deletedTs: NotRequired[str] # ISO-8601 timestamp + byGroupMember: "GroupMember" + +CIDeleted = CIDeleted_deleted | CIDeleted_blocked | CIDeleted_blockedByAdmin | CIDeleted_moderated + +CIDeleted_Tag = Literal["deleted", "blocked", "blockedByAdmin", "moderated"] + +class CIDirection_directSnd(TypedDict): + type: Literal["directSnd"] + +class CIDirection_directRcv(TypedDict): + type: Literal["directRcv"] + +class CIDirection_groupSnd(TypedDict): + type: Literal["groupSnd"] + +class CIDirection_groupRcv(TypedDict): + type: Literal["groupRcv"] + groupMember: "GroupMember" + +class CIDirection_channelRcv(TypedDict): + type: Literal["channelRcv"] + +class CIDirection_localSnd(TypedDict): + type: Literal["localSnd"] + +class CIDirection_localRcv(TypedDict): + type: Literal["localRcv"] + +CIDirection = ( + CIDirection_directSnd + | CIDirection_directRcv + | CIDirection_groupSnd + | CIDirection_groupRcv + | CIDirection_channelRcv + | CIDirection_localSnd + | CIDirection_localRcv +) + +CIDirection_Tag = Literal["directSnd", "directRcv", "groupSnd", "groupRcv", "channelRcv", "localSnd", "localRcv"] + +class CIFile(TypedDict): + fileId: int # int64 + fileName: str + fileSize: int # int64 + fileSource: NotRequired["CryptoFile"] + fileStatus: "CIFileStatus" + fileProtocol: "FileProtocol" + +class CIFileStatus_sndStored(TypedDict): + type: Literal["sndStored"] + +class CIFileStatus_sndTransfer(TypedDict): + type: Literal["sndTransfer"] + sndProgress: int # int64 + sndTotal: int # int64 + +class CIFileStatus_sndCancelled(TypedDict): + type: Literal["sndCancelled"] + +class CIFileStatus_sndComplete(TypedDict): + type: Literal["sndComplete"] + +class CIFileStatus_sndError(TypedDict): + type: Literal["sndError"] + sndFileError: "FileError" + +class CIFileStatus_sndWarning(TypedDict): + type: Literal["sndWarning"] + sndFileError: "FileError" + +class CIFileStatus_rcvInvitation(TypedDict): + type: Literal["rcvInvitation"] + +class CIFileStatus_rcvAccepted(TypedDict): + type: Literal["rcvAccepted"] + +class CIFileStatus_rcvTransfer(TypedDict): + type: Literal["rcvTransfer"] + rcvProgress: int # int64 + rcvTotal: int # int64 + +class CIFileStatus_rcvAborted(TypedDict): + type: Literal["rcvAborted"] + +class CIFileStatus_rcvComplete(TypedDict): + type: Literal["rcvComplete"] + +class CIFileStatus_rcvCancelled(TypedDict): + type: Literal["rcvCancelled"] + +class CIFileStatus_rcvError(TypedDict): + type: Literal["rcvError"] + rcvFileError: "FileError" + +class CIFileStatus_rcvWarning(TypedDict): + type: Literal["rcvWarning"] + rcvFileError: "FileError" + +class CIFileStatus_invalid(TypedDict): + type: Literal["invalid"] + text: str + +CIFileStatus = ( + CIFileStatus_sndStored + | CIFileStatus_sndTransfer + | CIFileStatus_sndCancelled + | CIFileStatus_sndComplete + | CIFileStatus_sndError + | CIFileStatus_sndWarning + | CIFileStatus_rcvInvitation + | CIFileStatus_rcvAccepted + | CIFileStatus_rcvTransfer + | CIFileStatus_rcvAborted + | CIFileStatus_rcvComplete + | CIFileStatus_rcvCancelled + | CIFileStatus_rcvError + | CIFileStatus_rcvWarning + | CIFileStatus_invalid +) + +CIFileStatus_Tag = Literal["sndStored", "sndTransfer", "sndCancelled", "sndComplete", "sndError", "sndWarning", "rcvInvitation", "rcvAccepted", "rcvTransfer", "rcvAborted", "rcvComplete", "rcvCancelled", "rcvError", "rcvWarning", "invalid"] + +class CIForwardedFrom_unknown(TypedDict): + type: Literal["unknown"] + +class CIForwardedFrom_contact(TypedDict): + type: Literal["contact"] + chatName: str + msgDir: "MsgDirection" + contactId: NotRequired[int] # int64 + chatItemId: NotRequired[int] # int64 + +class CIForwardedFrom_group(TypedDict): + type: Literal["group"] + chatName: str + msgDir: "MsgDirection" + groupId: NotRequired[int] # int64 + chatItemId: NotRequired[int] # int64 + +CIForwardedFrom = CIForwardedFrom_unknown | CIForwardedFrom_contact | CIForwardedFrom_group + +CIForwardedFrom_Tag = Literal["unknown", "contact", "group"] + +class CIGroupInvitation(TypedDict): + groupId: int # int64 + groupMemberId: int # int64 + localDisplayName: str + groupProfile: "GroupProfile" + status: "CIGroupInvitationStatus" + +CIGroupInvitationStatus = Literal["pending", "accepted", "rejected", "expired"] + +class CIMention(TypedDict): + memberId: str + memberRef: NotRequired["CIMentionMember"] + +class CIMentionMember(TypedDict): + groupMemberId: int # int64 + displayName: str + localAlias: NotRequired[str] + memberRole: "GroupMemberRole" + +class CIMeta(TypedDict): + itemId: int # int64 + itemTs: str # ISO-8601 timestamp + itemText: str + itemStatus: "CIStatus" + sentViaProxy: NotRequired[bool] + itemSharedMsgId: NotRequired[str] + itemForwarded: NotRequired["CIForwardedFrom"] + itemDeleted: NotRequired["CIDeleted"] + itemEdited: bool + itemTimed: NotRequired["CITimed"] + itemLive: NotRequired[bool] + userMention: bool + hasLink: bool + deletable: bool + editable: bool + forwardedByMember: NotRequired[int] # int64 + showGroupAsSender: bool + msgSigned: NotRequired["MsgSigStatus"] + createdAt: str # ISO-8601 timestamp + updatedAt: str # ISO-8601 timestamp + +class CIQuote(TypedDict): + chatDir: NotRequired["CIDirection"] + itemId: NotRequired[int] # int64 + sharedMsgId: NotRequired[str] + sentAt: str # ISO-8601 timestamp + content: "MsgContent" + formattedText: NotRequired[list["FormattedText"]] + +class CIReaction(TypedDict): + chatDir: "CIDirection" + chatItem: "ChatItem" + sentAt: str # ISO-8601 timestamp + reaction: "MsgReaction" + +class CIReactionCount(TypedDict): + reaction: "MsgReaction" + userReacted: bool + totalReacted: int # int + +class CIStatus_sndNew(TypedDict): + type: Literal["sndNew"] + +class CIStatus_sndSent(TypedDict): + type: Literal["sndSent"] + sndProgress: "SndCIStatusProgress" + +class CIStatus_sndRcvd(TypedDict): + type: Literal["sndRcvd"] + msgRcptStatus: "MsgReceiptStatus" + sndProgress: "SndCIStatusProgress" + +class CIStatus_sndErrorAuth(TypedDict): + type: Literal["sndErrorAuth"] + +class CIStatus_sndError(TypedDict): + type: Literal["sndError"] + agentError: "SndError" + +class CIStatus_sndWarning(TypedDict): + type: Literal["sndWarning"] + agentError: "SndError" + +class CIStatus_rcvNew(TypedDict): + type: Literal["rcvNew"] + +class CIStatus_rcvRead(TypedDict): + type: Literal["rcvRead"] + +class CIStatus_invalid(TypedDict): + type: Literal["invalid"] + text: str + +CIStatus = ( + CIStatus_sndNew + | CIStatus_sndSent + | CIStatus_sndRcvd + | CIStatus_sndErrorAuth + | CIStatus_sndError + | CIStatus_sndWarning + | CIStatus_rcvNew + | CIStatus_rcvRead + | CIStatus_invalid +) + +CIStatus_Tag = Literal["sndNew", "sndSent", "sndRcvd", "sndErrorAuth", "sndError", "sndWarning", "rcvNew", "rcvRead", "invalid"] + +class CITimed(TypedDict): + ttl: int # int + deleteAt: NotRequired[str] # ISO-8601 timestamp + +class ChatBotCommand_command(TypedDict): + type: Literal["command"] + keyword: str + label: str + params: NotRequired[str] + +class ChatBotCommand_menu(TypedDict): + type: Literal["menu"] + label: str + commands: list["ChatBotCommand"] + +ChatBotCommand = ChatBotCommand_command | ChatBotCommand_menu + +ChatBotCommand_Tag = Literal["command", "menu"] + +class ChatDeleteMode_full(TypedDict): + type: Literal["full"] + notify: bool + +class ChatDeleteMode_entity(TypedDict): + type: Literal["entity"] + notify: bool + +class ChatDeleteMode_messages(TypedDict): + type: Literal["messages"] + +ChatDeleteMode = ChatDeleteMode_full | ChatDeleteMode_entity | ChatDeleteMode_messages + +ChatDeleteMode_Tag = Literal["full", "entity", "messages"] + + +def ChatDeleteMode_cmd_string(self: ChatDeleteMode) -> str: + return str(self['type']) + ('' if str(self['type']) == 'messages' else (' notify=off' if not self['notify'] else '')) # type: ignore[typeddict-item] + +class ChatError_error(TypedDict): + type: Literal["error"] + errorType: "ChatErrorType" + +class ChatError_errorAgent(TypedDict): + type: Literal["errorAgent"] + agentError: "AgentErrorType" + agentConnId: str + connectionEntity_: NotRequired["ConnectionEntity"] + +class ChatError_errorStore(TypedDict): + type: Literal["errorStore"] + storeError: "StoreError" + +ChatError = ChatError_error | ChatError_errorAgent | ChatError_errorStore + +ChatError_Tag = Literal["error", "errorAgent", "errorStore"] + +class ChatErrorType_noActiveUser(TypedDict): + type: Literal["noActiveUser"] + +class ChatErrorType_noConnectionUser(TypedDict): + type: Literal["noConnectionUser"] + agentConnId: str + +class ChatErrorType_noSndFileUser(TypedDict): + type: Literal["noSndFileUser"] + agentSndFileId: str + +class ChatErrorType_noRcvFileUser(TypedDict): + type: Literal["noRcvFileUser"] + agentRcvFileId: str + +class ChatErrorType_userUnknown(TypedDict): + type: Literal["userUnknown"] + +class ChatErrorType_activeUserExists(TypedDict): + type: Literal["activeUserExists"] + +class ChatErrorType_userExists(TypedDict): + type: Literal["userExists"] + contactName: str + +class ChatErrorType_chatRelayExists(TypedDict): + type: Literal["chatRelayExists"] + +class ChatErrorType_differentActiveUser(TypedDict): + type: Literal["differentActiveUser"] + commandUserId: int # int64 + activeUserId: int # int64 + +class ChatErrorType_cantDeleteActiveUser(TypedDict): + type: Literal["cantDeleteActiveUser"] + userId: int # int64 + +class ChatErrorType_cantDeleteLastUser(TypedDict): + type: Literal["cantDeleteLastUser"] + userId: int # int64 + +class ChatErrorType_cantHideLastUser(TypedDict): + type: Literal["cantHideLastUser"] + userId: int # int64 + +class ChatErrorType_hiddenUserAlwaysMuted(TypedDict): + type: Literal["hiddenUserAlwaysMuted"] + userId: int # int64 + +class ChatErrorType_emptyUserPassword(TypedDict): + type: Literal["emptyUserPassword"] + userId: int # int64 + +class ChatErrorType_userAlreadyHidden(TypedDict): + type: Literal["userAlreadyHidden"] + userId: int # int64 + +class ChatErrorType_userNotHidden(TypedDict): + type: Literal["userNotHidden"] + userId: int # int64 + +class ChatErrorType_invalidDisplayName(TypedDict): + type: Literal["invalidDisplayName"] + displayName: str + validName: str + +class ChatErrorType_chatNotStarted(TypedDict): + type: Literal["chatNotStarted"] + +class ChatErrorType_chatNotStopped(TypedDict): + type: Literal["chatNotStopped"] + +class ChatErrorType_chatStoreChanged(TypedDict): + type: Literal["chatStoreChanged"] + +class ChatErrorType_invalidConnReq(TypedDict): + type: Literal["invalidConnReq"] + +class ChatErrorType_unsupportedConnReq(TypedDict): + type: Literal["unsupportedConnReq"] + +class ChatErrorType_connReqMessageProhibited(TypedDict): + type: Literal["connReqMessageProhibited"] + +class ChatErrorType_contactNotReady(TypedDict): + type: Literal["contactNotReady"] + contact: "Contact" + +class ChatErrorType_contactNotActive(TypedDict): + type: Literal["contactNotActive"] + contact: "Contact" + +class ChatErrorType_contactDisabled(TypedDict): + type: Literal["contactDisabled"] + contact: "Contact" + +class ChatErrorType_connectionDisabled(TypedDict): + type: Literal["connectionDisabled"] + connection: "Connection" + +class ChatErrorType_groupUserRole(TypedDict): + type: Literal["groupUserRole"] + groupInfo: "GroupInfo" + requiredRole: "GroupMemberRole" + +class ChatErrorType_groupMemberInitialRole(TypedDict): + type: Literal["groupMemberInitialRole"] + groupInfo: "GroupInfo" + initialRole: "GroupMemberRole" + +class ChatErrorType_contactIncognitoCantInvite(TypedDict): + type: Literal["contactIncognitoCantInvite"] + +class ChatErrorType_groupIncognitoCantInvite(TypedDict): + type: Literal["groupIncognitoCantInvite"] + +class ChatErrorType_groupContactRole(TypedDict): + type: Literal["groupContactRole"] + contactName: str + +class ChatErrorType_groupDuplicateMember(TypedDict): + type: Literal["groupDuplicateMember"] + contactName: str + +class ChatErrorType_groupDuplicateMemberId(TypedDict): + type: Literal["groupDuplicateMemberId"] + +class ChatErrorType_groupNotJoined(TypedDict): + type: Literal["groupNotJoined"] + groupInfo: "GroupInfo" + +class ChatErrorType_groupMemberNotActive(TypedDict): + type: Literal["groupMemberNotActive"] + +class ChatErrorType_cantBlockMemberForSelf(TypedDict): + type: Literal["cantBlockMemberForSelf"] + groupInfo: "GroupInfo" + member: "GroupMember" + setShowMessages: bool + +class ChatErrorType_groupMemberUserRemoved(TypedDict): + type: Literal["groupMemberUserRemoved"] + +class ChatErrorType_groupMemberNotFound(TypedDict): + type: Literal["groupMemberNotFound"] + +class ChatErrorType_groupCantResendInvitation(TypedDict): + type: Literal["groupCantResendInvitation"] + groupInfo: "GroupInfo" + contactName: str + +class ChatErrorType_groupInternal(TypedDict): + type: Literal["groupInternal"] + message: str + +class ChatErrorType_fileNotFound(TypedDict): + type: Literal["fileNotFound"] + message: str + +class ChatErrorType_fileSize(TypedDict): + type: Literal["fileSize"] + filePath: str + +class ChatErrorType_fileAlreadyReceiving(TypedDict): + type: Literal["fileAlreadyReceiving"] + message: str + +class ChatErrorType_fileCancelled(TypedDict): + type: Literal["fileCancelled"] + message: str + +class ChatErrorType_fileCancel(TypedDict): + type: Literal["fileCancel"] + fileId: int # int64 + message: str + +class ChatErrorType_fileAlreadyExists(TypedDict): + type: Literal["fileAlreadyExists"] + filePath: str + +class ChatErrorType_fileWrite(TypedDict): + type: Literal["fileWrite"] + filePath: str + message: str + +class ChatErrorType_fileSend(TypedDict): + type: Literal["fileSend"] + fileId: int # int64 + agentError: "AgentErrorType" + +class ChatErrorType_fileRcvChunk(TypedDict): + type: Literal["fileRcvChunk"] + message: str + +class ChatErrorType_fileInternal(TypedDict): + type: Literal["fileInternal"] + message: str + +class ChatErrorType_fileImageType(TypedDict): + type: Literal["fileImageType"] + filePath: str + +class ChatErrorType_fileImageSize(TypedDict): + type: Literal["fileImageSize"] + filePath: str + +class ChatErrorType_fileNotReceived(TypedDict): + type: Literal["fileNotReceived"] + fileId: int # int64 + +class ChatErrorType_fileNotApproved(TypedDict): + type: Literal["fileNotApproved"] + fileId: int # int64 + unknownServers: list[str] + +class ChatErrorType_fallbackToSMPProhibited(TypedDict): + type: Literal["fallbackToSMPProhibited"] + fileId: int # int64 + +class ChatErrorType_inlineFileProhibited(TypedDict): + type: Literal["inlineFileProhibited"] + fileId: int # int64 + +class ChatErrorType_invalidForward(TypedDict): + type: Literal["invalidForward"] + +class ChatErrorType_invalidChatItemUpdate(TypedDict): + type: Literal["invalidChatItemUpdate"] + +class ChatErrorType_invalidChatItemDelete(TypedDict): + type: Literal["invalidChatItemDelete"] + +class ChatErrorType_hasCurrentCall(TypedDict): + type: Literal["hasCurrentCall"] + +class ChatErrorType_noCurrentCall(TypedDict): + type: Literal["noCurrentCall"] + +class ChatErrorType_callContact(TypedDict): + type: Literal["callContact"] + contactId: int # int64 + +class ChatErrorType_directMessagesProhibited(TypedDict): + type: Literal["directMessagesProhibited"] + direction: "MsgDirection" + contact: "Contact" + +class ChatErrorType_agentVersion(TypedDict): + type: Literal["agentVersion"] + +class ChatErrorType_agentNoSubResult(TypedDict): + type: Literal["agentNoSubResult"] + agentConnId: str + +class ChatErrorType_commandError(TypedDict): + type: Literal["commandError"] + message: str + +class ChatErrorType_agentCommandError(TypedDict): + type: Literal["agentCommandError"] + message: str + +class ChatErrorType_invalidFileDescription(TypedDict): + type: Literal["invalidFileDescription"] + message: str + +class ChatErrorType_connectionIncognitoChangeProhibited(TypedDict): + type: Literal["connectionIncognitoChangeProhibited"] + +class ChatErrorType_connectionUserChangeProhibited(TypedDict): + type: Literal["connectionUserChangeProhibited"] + +class ChatErrorType_peerChatVRangeIncompatible(TypedDict): + type: Literal["peerChatVRangeIncompatible"] + +class ChatErrorType_relayTestError(TypedDict): + type: Literal["relayTestError"] + message: str + +class ChatErrorType_internalError(TypedDict): + type: Literal["internalError"] + message: str + +class ChatErrorType_exception(TypedDict): + type: Literal["exception"] + message: str + +ChatErrorType = ( + ChatErrorType_noActiveUser + | ChatErrorType_noConnectionUser + | ChatErrorType_noSndFileUser + | ChatErrorType_noRcvFileUser + | ChatErrorType_userUnknown + | ChatErrorType_activeUserExists + | ChatErrorType_userExists + | ChatErrorType_chatRelayExists + | ChatErrorType_differentActiveUser + | ChatErrorType_cantDeleteActiveUser + | ChatErrorType_cantDeleteLastUser + | ChatErrorType_cantHideLastUser + | ChatErrorType_hiddenUserAlwaysMuted + | ChatErrorType_emptyUserPassword + | ChatErrorType_userAlreadyHidden + | ChatErrorType_userNotHidden + | ChatErrorType_invalidDisplayName + | ChatErrorType_chatNotStarted + | ChatErrorType_chatNotStopped + | ChatErrorType_chatStoreChanged + | ChatErrorType_invalidConnReq + | ChatErrorType_unsupportedConnReq + | ChatErrorType_connReqMessageProhibited + | ChatErrorType_contactNotReady + | ChatErrorType_contactNotActive + | ChatErrorType_contactDisabled + | ChatErrorType_connectionDisabled + | ChatErrorType_groupUserRole + | ChatErrorType_groupMemberInitialRole + | ChatErrorType_contactIncognitoCantInvite + | ChatErrorType_groupIncognitoCantInvite + | ChatErrorType_groupContactRole + | ChatErrorType_groupDuplicateMember + | ChatErrorType_groupDuplicateMemberId + | ChatErrorType_groupNotJoined + | ChatErrorType_groupMemberNotActive + | ChatErrorType_cantBlockMemberForSelf + | ChatErrorType_groupMemberUserRemoved + | ChatErrorType_groupMemberNotFound + | ChatErrorType_groupCantResendInvitation + | ChatErrorType_groupInternal + | ChatErrorType_fileNotFound + | ChatErrorType_fileSize + | ChatErrorType_fileAlreadyReceiving + | ChatErrorType_fileCancelled + | ChatErrorType_fileCancel + | ChatErrorType_fileAlreadyExists + | ChatErrorType_fileWrite + | ChatErrorType_fileSend + | ChatErrorType_fileRcvChunk + | ChatErrorType_fileInternal + | ChatErrorType_fileImageType + | ChatErrorType_fileImageSize + | ChatErrorType_fileNotReceived + | ChatErrorType_fileNotApproved + | ChatErrorType_fallbackToSMPProhibited + | ChatErrorType_inlineFileProhibited + | ChatErrorType_invalidForward + | ChatErrorType_invalidChatItemUpdate + | ChatErrorType_invalidChatItemDelete + | ChatErrorType_hasCurrentCall + | ChatErrorType_noCurrentCall + | ChatErrorType_callContact + | ChatErrorType_directMessagesProhibited + | ChatErrorType_agentVersion + | ChatErrorType_agentNoSubResult + | ChatErrorType_commandError + | ChatErrorType_agentCommandError + | ChatErrorType_invalidFileDescription + | ChatErrorType_connectionIncognitoChangeProhibited + | ChatErrorType_connectionUserChangeProhibited + | ChatErrorType_peerChatVRangeIncompatible + | ChatErrorType_relayTestError + | ChatErrorType_internalError + | ChatErrorType_exception +) + +ChatErrorType_Tag = Literal["noActiveUser", "noConnectionUser", "noSndFileUser", "noRcvFileUser", "userUnknown", "activeUserExists", "userExists", "chatRelayExists", "differentActiveUser", "cantDeleteActiveUser", "cantDeleteLastUser", "cantHideLastUser", "hiddenUserAlwaysMuted", "emptyUserPassword", "userAlreadyHidden", "userNotHidden", "invalidDisplayName", "chatNotStarted", "chatNotStopped", "chatStoreChanged", "invalidConnReq", "unsupportedConnReq", "connReqMessageProhibited", "contactNotReady", "contactNotActive", "contactDisabled", "connectionDisabled", "groupUserRole", "groupMemberInitialRole", "contactIncognitoCantInvite", "groupIncognitoCantInvite", "groupContactRole", "groupDuplicateMember", "groupDuplicateMemberId", "groupNotJoined", "groupMemberNotActive", "cantBlockMemberForSelf", "groupMemberUserRemoved", "groupMemberNotFound", "groupCantResendInvitation", "groupInternal", "fileNotFound", "fileSize", "fileAlreadyReceiving", "fileCancelled", "fileCancel", "fileAlreadyExists", "fileWrite", "fileSend", "fileRcvChunk", "fileInternal", "fileImageType", "fileImageSize", "fileNotReceived", "fileNotApproved", "fallbackToSMPProhibited", "inlineFileProhibited", "invalidForward", "invalidChatItemUpdate", "invalidChatItemDelete", "hasCurrentCall", "noCurrentCall", "callContact", "directMessagesProhibited", "agentVersion", "agentNoSubResult", "commandError", "agentCommandError", "invalidFileDescription", "connectionIncognitoChangeProhibited", "connectionUserChangeProhibited", "peerChatVRangeIncompatible", "relayTestError", "internalError", "exception"] + +ChatFeature = Literal["timedMessages", "fullDelete", "reactions", "voice", "files", "calls", "sessions"] + +class ChatInfo_direct(TypedDict): + type: Literal["direct"] + contact: "Contact" + +class ChatInfo_group(TypedDict): + type: Literal["group"] + groupInfo: "GroupInfo" + groupChatScope: NotRequired["GroupChatScopeInfo"] + +class ChatInfo_local(TypedDict): + type: Literal["local"] + noteFolder: "NoteFolder" + +class ChatInfo_contactRequest(TypedDict): + type: Literal["contactRequest"] + contactRequest: "UserContactRequest" + +class ChatInfo_contactConnection(TypedDict): + type: Literal["contactConnection"] + contactConnection: "PendingContactConnection" + +ChatInfo = ( + ChatInfo_direct + | ChatInfo_group + | ChatInfo_local + | ChatInfo_contactRequest + | ChatInfo_contactConnection +) + +ChatInfo_Tag = Literal["direct", "group", "local", "contactRequest", "contactConnection"] + +class ChatItem(TypedDict): + chatDir: "CIDirection" + meta: "CIMeta" + content: "CIContent" + mentions: dict[str, "CIMention"] + formattedText: NotRequired[list["FormattedText"]] + quotedItem: NotRequired["CIQuote"] + reactions: list["CIReactionCount"] + file: NotRequired["CIFile"] + +# Message deletion result. + +class ChatItemDeletion(TypedDict): + deletedChatItem: "AChatItem" + toChatItem: NotRequired["AChatItem"] + +class ChatListQuery_filters(TypedDict): + type: Literal["filters"] + favorite: bool + unread: bool + +class ChatListQuery_search(TypedDict): + type: Literal["search"] + search: str + +ChatListQuery = ChatListQuery_filters | ChatListQuery_search + +ChatListQuery_Tag = Literal["filters", "search"] + +ChatPeerType = Literal["human", "bot"] + +# Used in API commands. Chat scope can only be passed with groups. + +class ChatRef(TypedDict): + chatType: "ChatType" + chatId: int # int64 + chatScope: NotRequired["GroupChatScope"] + + +def ChatRef_cmd_string(self: ChatRef) -> str: + return ChatType_cmd_string(self['chatType']) + str(self['chatId']) + ((GroupChatScope_cmd_string(self.get('chatScope'))) if self.get('chatScope') is not None else '') + +class ChatSettings(TypedDict): + enableNtfs: "MsgFilter" + sendRcpts: NotRequired[bool] + favorite: bool + +class ChatStats(TypedDict): + unreadCount: int # int + unreadMentions: int # int + reportsCount: int # int + minUnreadItemId: int # int64 + unreadChat: bool + +ChatType = Literal["direct", "group", "local"] + + +def ChatType_cmd_string(self: ChatType) -> str: + return '@' if str(self) == 'direct' else '#' if str(self) == 'group' else '*' if str(self) == 'local' else '' + +class ChatWallpaper(TypedDict): + preset: NotRequired[str] + imageFile: NotRequired[str] + background: NotRequired[str] + tint: NotRequired[str] + scaleType: NotRequired["ChatWallpaperScale"] + scale: NotRequired[float] # double + +ChatWallpaperScale = Literal["fill", "fit", "repeat"] + +class ClientNotice(TypedDict): + ttl: NotRequired[int] # int64 + +Color = Literal["black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"] + +class CommandError_UNKNOWN(TypedDict): + type: Literal["UNKNOWN"] + +class CommandError_SYNTAX(TypedDict): + type: Literal["SYNTAX"] + +class CommandError_PROHIBITED(TypedDict): + type: Literal["PROHIBITED"] + +class CommandError_NO_AUTH(TypedDict): + type: Literal["NO_AUTH"] + +class CommandError_HAS_AUTH(TypedDict): + type: Literal["HAS_AUTH"] + +class CommandError_NO_ENTITY(TypedDict): + type: Literal["NO_ENTITY"] + +CommandError = ( + CommandError_UNKNOWN + | CommandError_SYNTAX + | CommandError_PROHIBITED + | CommandError_NO_AUTH + | CommandError_HAS_AUTH + | CommandError_NO_ENTITY +) + +CommandError_Tag = Literal["UNKNOWN", "SYNTAX", "PROHIBITED", "NO_AUTH", "HAS_AUTH", "NO_ENTITY"] + +class CommandErrorType_PROHIBITED(TypedDict): + type: Literal["PROHIBITED"] + +class CommandErrorType_SYNTAX(TypedDict): + type: Literal["SYNTAX"] + +class CommandErrorType_NO_CONN(TypedDict): + type: Literal["NO_CONN"] + +class CommandErrorType_SIZE(TypedDict): + type: Literal["SIZE"] + +class CommandErrorType_LARGE(TypedDict): + type: Literal["LARGE"] + +CommandErrorType = ( + CommandErrorType_PROHIBITED + | CommandErrorType_SYNTAX + | CommandErrorType_NO_CONN + | CommandErrorType_SIZE + | CommandErrorType_LARGE +) + +CommandErrorType_Tag = Literal["PROHIBITED", "SYNTAX", "NO_CONN", "SIZE", "LARGE"] + +class CommentsGroupPreference(TypedDict): + enable: "GroupFeatureEnabled" + duration: NotRequired[int] # int + +class ComposedMessage(TypedDict): + fileSource: NotRequired["CryptoFile"] + quotedItemId: NotRequired[int] # int64 + msgContent: "MsgContent" + mentions: dict[str, int] # str : int64 + +class ConnStatus_new(TypedDict): + type: Literal["new"] + +class ConnStatus_prepared(TypedDict): + type: Literal["prepared"] + +class ConnStatus_joined(TypedDict): + type: Literal["joined"] + +class ConnStatus_requested(TypedDict): + type: Literal["requested"] + +class ConnStatus_accepted(TypedDict): + type: Literal["accepted"] + +class ConnStatus_sndReady(TypedDict): + type: Literal["sndReady"] + +class ConnStatus_ready(TypedDict): + type: Literal["ready"] + +class ConnStatus_deleted(TypedDict): + type: Literal["deleted"] + +class ConnStatus_failed(TypedDict): + type: Literal["failed"] + connError: str + +ConnStatus = ( + ConnStatus_new + | ConnStatus_prepared + | ConnStatus_joined + | ConnStatus_requested + | ConnStatus_accepted + | ConnStatus_sndReady + | ConnStatus_ready + | ConnStatus_deleted + | ConnStatus_failed +) + +ConnStatus_Tag = Literal["new", "prepared", "joined", "requested", "accepted", "sndReady", "ready", "deleted", "failed"] + +ConnType = Literal["contact", "member", "user_contact"] + +class Connection(TypedDict): + connId: int # int64 + agentConnId: str + connChatVersion: int # int + peerChatVRange: "VersionRange" + connLevel: int # int + viaContact: NotRequired[int] # int64 + viaUserContactLink: NotRequired[int] # int64 + viaGroupLink: bool + groupLinkId: NotRequired[str] + xContactId: NotRequired[str] + customUserProfileId: NotRequired[int] # int64 + connType: "ConnType" + connStatus: "ConnStatus" + contactConnInitiated: bool + localAlias: str + entityId: NotRequired[int] # int64 + connectionCode: NotRequired["SecurityCode"] + pqSupport: bool + pqEncryption: bool + pqSndEnabled: NotRequired[bool] + pqRcvEnabled: NotRequired[bool] + authErrCounter: int # int + quotaErrCounter: int # int + createdAt: str # ISO-8601 timestamp + +class ConnectionEntity_rcvDirectMsgConnection(TypedDict): + type: Literal["rcvDirectMsgConnection"] + entityConnection: "Connection" + contact: NotRequired["Contact"] + +class ConnectionEntity_rcvGroupMsgConnection(TypedDict): + type: Literal["rcvGroupMsgConnection"] + entityConnection: "Connection" + groupInfo: "GroupInfo" + groupMember: "GroupMember" + +class ConnectionEntity_userContactConnection(TypedDict): + type: Literal["userContactConnection"] + entityConnection: "Connection" + userContact: "UserContact" + +ConnectionEntity = ( + ConnectionEntity_rcvDirectMsgConnection + | ConnectionEntity_rcvGroupMsgConnection + | ConnectionEntity_userContactConnection +) + +ConnectionEntity_Tag = Literal["rcvDirectMsgConnection", "rcvGroupMsgConnection", "userContactConnection"] + +class ConnectionErrorType_NOT_FOUND(TypedDict): + type: Literal["NOT_FOUND"] + +class ConnectionErrorType_DUPLICATE(TypedDict): + type: Literal["DUPLICATE"] + +class ConnectionErrorType_SIMPLEX(TypedDict): + type: Literal["SIMPLEX"] + +class ConnectionErrorType_NOT_ACCEPTED(TypedDict): + type: Literal["NOT_ACCEPTED"] + +class ConnectionErrorType_NOT_AVAILABLE(TypedDict): + type: Literal["NOT_AVAILABLE"] + +ConnectionErrorType = ( + ConnectionErrorType_NOT_FOUND + | ConnectionErrorType_DUPLICATE + | ConnectionErrorType_SIMPLEX + | ConnectionErrorType_NOT_ACCEPTED + | ConnectionErrorType_NOT_AVAILABLE +) + +ConnectionErrorType_Tag = Literal["NOT_FOUND", "DUPLICATE", "SIMPLEX", "NOT_ACCEPTED", "NOT_AVAILABLE"] + +ConnectionMode = Literal["INV", "CON"] + +class ConnectionPlan_invitationLink(TypedDict): + type: Literal["invitationLink"] + invitationLinkPlan: "InvitationLinkPlan" + +class ConnectionPlan_contactAddress(TypedDict): + type: Literal["contactAddress"] + contactAddressPlan: "ContactAddressPlan" + +class ConnectionPlan_groupLink(TypedDict): + type: Literal["groupLink"] + groupLinkPlan: "GroupLinkPlan" + +class ConnectionPlan_error(TypedDict): + type: Literal["error"] + chatError: "ChatError" + +ConnectionPlan = ( + ConnectionPlan_invitationLink + | ConnectionPlan_contactAddress + | ConnectionPlan_groupLink + | ConnectionPlan_error +) + +ConnectionPlan_Tag = Literal["invitationLink", "contactAddress", "groupLink", "error"] + +class Contact(TypedDict): + contactId: int # int64 + localDisplayName: str + profile: "LocalProfile" + activeConn: NotRequired["Connection"] + contactUsed: bool + contactStatus: "ContactStatus" + chatSettings: "ChatSettings" + userPreferences: "Preferences" + mergedPreferences: "ContactUserPreferences" + createdAt: str # ISO-8601 timestamp + updatedAt: str # ISO-8601 timestamp + chatTs: NotRequired[str] # ISO-8601 timestamp + preparedContact: NotRequired["PreparedContact"] + contactRequestId: NotRequired[int] # int64 + contactGroupMemberId: NotRequired[int] # int64 + contactGrpInvSent: bool + groupDirectInv: NotRequired["GroupDirectInvitation"] + chatTags: list[int] # int64 + chatItemTTL: NotRequired[int] # int64 + uiThemes: NotRequired["UIThemeEntityOverrides"] + chatDeleted: bool + customData: NotRequired[dict[str, object]] + +class ContactAddressPlan_ok(TypedDict): + type: Literal["ok"] + contactSLinkData_: NotRequired["ContactShortLinkData"] + ownerVerification: NotRequired["OwnerVerification"] + +class ContactAddressPlan_ownLink(TypedDict): + type: Literal["ownLink"] + +class ContactAddressPlan_connectingConfirmReconnect(TypedDict): + type: Literal["connectingConfirmReconnect"] + +class ContactAddressPlan_connectingProhibit(TypedDict): + type: Literal["connectingProhibit"] + contact: "Contact" + +class ContactAddressPlan_known(TypedDict): + type: Literal["known"] + contact: "Contact" + +class ContactAddressPlan_contactViaAddress(TypedDict): + type: Literal["contactViaAddress"] + contact: "Contact" + +ContactAddressPlan = ( + ContactAddressPlan_ok + | ContactAddressPlan_ownLink + | ContactAddressPlan_connectingConfirmReconnect + | ContactAddressPlan_connectingProhibit + | ContactAddressPlan_known + | ContactAddressPlan_contactViaAddress +) + +ContactAddressPlan_Tag = Literal["ok", "ownLink", "connectingConfirmReconnect", "connectingProhibit", "known", "contactViaAddress"] + +class ContactShortLinkData(TypedDict): + profile: "Profile" + message: NotRequired["MsgContent"] + business: bool + +ContactStatus = Literal["active", "deleted", "deletedByUser"] + +class ContactUserPref_contact(TypedDict): + type: Literal["contact"] + preference: "SimplePreference" + +class ContactUserPref_user(TypedDict): + type: Literal["user"] + preference: "SimplePreference" + +ContactUserPref = ContactUserPref_contact | ContactUserPref_user + +ContactUserPref_Tag = Literal["contact", "user"] + +class ContactUserPreference(TypedDict): + enabled: "PrefEnabled" + userPreference: "ContactUserPref" + contactPreference: "SimplePreference" + +class ContactUserPreferences(TypedDict): + timedMessages: "ContactUserPreference" + fullDelete: "ContactUserPreference" + reactions: "ContactUserPreference" + voice: "ContactUserPreference" + files: "ContactUserPreference" + calls: "ContactUserPreference" + sessions: "ContactUserPreference" + commands: NotRequired[list["ChatBotCommand"]] + +class CreatedConnLink(TypedDict): + connFullLink: str + connShortLink: NotRequired[str] + + +def CreatedConnLink_cmd_string(self: CreatedConnLink) -> str: + return self['connFullLink'] + ((' ' + self.get('connShortLink')) if self.get('connShortLink') is not None else '') + +class CryptoFile(TypedDict): + filePath: str + cryptoArgs: NotRequired["CryptoFileArgs"] + +class CryptoFileArgs(TypedDict): + fileKey: str + fileNonce: str + +class DroppedMsg(TypedDict): + brokerTs: str # ISO-8601 timestamp + attempts: int # int + +class E2EInfo(TypedDict): + public: NotRequired[bool] + pqEnabled: NotRequired[bool] + +class ErrorType_BLOCK(TypedDict): + type: Literal["BLOCK"] + +class ErrorType_SESSION(TypedDict): + type: Literal["SESSION"] + +class ErrorType_CMD(TypedDict): + type: Literal["CMD"] + cmdErr: "CommandError" + +class ErrorType_PROXY(TypedDict): + type: Literal["PROXY"] + proxyErr: "ProxyError" + +class ErrorType_AUTH(TypedDict): + type: Literal["AUTH"] + +class ErrorType_BLOCKED(TypedDict): + type: Literal["BLOCKED"] + blockInfo: "BlockingInfo" + +class ErrorType_SERVICE(TypedDict): + type: Literal["SERVICE"] + +class ErrorType_CRYPTO(TypedDict): + type: Literal["CRYPTO"] + +class ErrorType_QUOTA(TypedDict): + type: Literal["QUOTA"] + +class ErrorType_STORE(TypedDict): + type: Literal["STORE"] + storeErr: str + +class ErrorType_NO_MSG(TypedDict): + type: Literal["NO_MSG"] + +class ErrorType_LARGE_MSG(TypedDict): + type: Literal["LARGE_MSG"] + +class ErrorType_EXPIRED(TypedDict): + type: Literal["EXPIRED"] + +class ErrorType_INTERNAL(TypedDict): + type: Literal["INTERNAL"] + +class ErrorType_DUPLICATE_(TypedDict): + type: Literal["DUPLICATE_"] + +ErrorType = ( + ErrorType_BLOCK + | ErrorType_SESSION + | ErrorType_CMD + | ErrorType_PROXY + | ErrorType_AUTH + | ErrorType_BLOCKED + | ErrorType_SERVICE + | ErrorType_CRYPTO + | ErrorType_QUOTA + | ErrorType_STORE + | ErrorType_NO_MSG + | ErrorType_LARGE_MSG + | ErrorType_EXPIRED + | ErrorType_INTERNAL + | ErrorType_DUPLICATE_ +) + +ErrorType_Tag = Literal["BLOCK", "SESSION", "CMD", "PROXY", "AUTH", "BLOCKED", "SERVICE", "CRYPTO", "QUOTA", "STORE", "NO_MSG", "LARGE_MSG", "EXPIRED", "INTERNAL", "DUPLICATE_"] + +FeatureAllowed = Literal["always", "yes", "no"] + +class FileDescr(TypedDict): + fileDescrText: str + fileDescrPartNo: int # int + fileDescrComplete: bool + +class FileError_auth(TypedDict): + type: Literal["auth"] + +class FileError_blocked(TypedDict): + type: Literal["blocked"] + server: str + blockInfo: "BlockingInfo" + +class FileError_noFile(TypedDict): + type: Literal["noFile"] + +class FileError_relay(TypedDict): + type: Literal["relay"] + srvError: "SrvError" + +class FileError_other(TypedDict): + type: Literal["other"] + fileError: str + +FileError = ( + FileError_auth + | FileError_blocked + | FileError_noFile + | FileError_relay + | FileError_other +) + +FileError_Tag = Literal["auth", "blocked", "noFile", "relay", "other"] + +class FileErrorType_NOT_APPROVED(TypedDict): + type: Literal["NOT_APPROVED"] + +class FileErrorType_SIZE(TypedDict): + type: Literal["SIZE"] + +class FileErrorType_REDIRECT(TypedDict): + type: Literal["REDIRECT"] + redirectError: str + +class FileErrorType_FILE_IO(TypedDict): + type: Literal["FILE_IO"] + fileIOError: str + +class FileErrorType_NO_FILE(TypedDict): + type: Literal["NO_FILE"] + +FileErrorType = ( + FileErrorType_NOT_APPROVED + | FileErrorType_SIZE + | FileErrorType_REDIRECT + | FileErrorType_FILE_IO + | FileErrorType_NO_FILE +) + +FileErrorType_Tag = Literal["NOT_APPROVED", "SIZE", "REDIRECT", "FILE_IO", "NO_FILE"] + +class FileInvitation(TypedDict): + fileName: str + fileSize: int # int64 + fileDigest: NotRequired[str] + fileConnReq: NotRequired[str] + fileInline: NotRequired["InlineFileMode"] + fileDescr: NotRequired["FileDescr"] + +FileProtocol = Literal["SMP", "XFTP", "LOCAL"] + +FileStatus = Literal["new", "accepted", "connected", "complete", "cancelled"] + +class FileTransferMeta(TypedDict): + fileId: int # int64 + xftpSndFile: NotRequired["XFTPSndFile"] + xftpRedirectFor: NotRequired[int] # int64 + fileName: str + filePath: str + fileSize: int # int64 + fileInline: NotRequired["InlineFileMode"] + chunkSize: int # int64 + cancelled: bool + +class Format_bold(TypedDict): + type: Literal["bold"] + +class Format_italic(TypedDict): + type: Literal["italic"] + +class Format_strikeThrough(TypedDict): + type: Literal["strikeThrough"] + +class Format_snippet(TypedDict): + type: Literal["snippet"] + +class Format_secret(TypedDict): + type: Literal["secret"] + +class Format_small(TypedDict): + type: Literal["small"] + +class Format_colored(TypedDict): + type: Literal["colored"] + color: "Color" + +class Format_uri(TypedDict): + type: Literal["uri"] + +class Format_hyperLink(TypedDict): + type: Literal["hyperLink"] + showText: NotRequired[str] + linkUri: str + +class Format_simplexLink(TypedDict): + type: Literal["simplexLink"] + showText: NotRequired[str] + linkType: "SimplexLinkType" + simplexUri: str + smpHosts: list[str] # non-empty + +class Format_command(TypedDict): + type: Literal["command"] + commandStr: str + +class Format_mention(TypedDict): + type: Literal["mention"] + memberName: str + +class Format_email(TypedDict): + type: Literal["email"] + +class Format_phone(TypedDict): + type: Literal["phone"] + +Format = ( + Format_bold + | Format_italic + | Format_strikeThrough + | Format_snippet + | Format_secret + | Format_small + | Format_colored + | Format_uri + | Format_hyperLink + | Format_simplexLink + | Format_command + | Format_mention + | Format_email + | Format_phone +) + +Format_Tag = Literal["bold", "italic", "strikeThrough", "snippet", "secret", "small", "colored", "uri", "hyperLink", "simplexLink", "command", "mention", "email", "phone"] + +class FormattedText(TypedDict): + format: NotRequired["Format"] + text: str + +class FullGroupPreferences(TypedDict): + timedMessages: "TimedMessagesGroupPreference" + directMessages: "RoleGroupPreference" + fullDelete: "GroupPreference" + reactions: "GroupPreference" + voice: "RoleGroupPreference" + files: "RoleGroupPreference" + simplexLinks: "RoleGroupPreference" + reports: "GroupPreference" + history: "GroupPreference" + support: "SupportGroupPreference" + sessions: "RoleGroupPreference" + comments: "CommentsGroupPreference" + commands: list["ChatBotCommand"] + +class FullPreferences(TypedDict): + timedMessages: "TimedMessagesPreference" + fullDelete: "SimplePreference" + reactions: "SimplePreference" + voice: "SimplePreference" + files: "SimplePreference" + calls: "SimplePreference" + sessions: "SimplePreference" + commands: list["ChatBotCommand"] + +class Group(TypedDict): + groupInfo: "GroupInfo" + members: list["GroupMember"] + +class GroupChatScope_memberSupport(TypedDict): + type: Literal["memberSupport"] + groupMemberId_: NotRequired[int] # int64 + +GroupChatScope = GroupChatScope_memberSupport + +GroupChatScope_Tag = Literal["memberSupport"] + + +def GroupChatScope_cmd_string(self: GroupChatScope) -> str: + return '(_support' + ((':' + str(self.get('groupMemberId_'))) if self.get('groupMemberId_') is not None else '') + ')' # type: ignore[typeddict-item] + +class GroupChatScopeInfo_memberSupport(TypedDict): + type: Literal["memberSupport"] + groupMember_: NotRequired["GroupMember"] + +GroupChatScopeInfo = GroupChatScopeInfo_memberSupport + +GroupChatScopeInfo_Tag = Literal["memberSupport"] + +class GroupDirectInvitation(TypedDict): + groupDirectInvLink: str + fromGroupId_: NotRequired[int] # int64 + fromGroupMemberId_: NotRequired[int] # int64 + fromGroupMemberConnId_: NotRequired[int] # int64 + groupDirectInvStartedConnection: bool + +GroupFeature = Literal["timedMessages", "directMessages", "fullDelete", "reactions", "voice", "files", "simplexLinks", "reports", "history", "support", "sessions", "comments"] + +GroupFeatureEnabled = Literal["on", "off"] + +class GroupInfo(TypedDict): + groupId: int # int64 + useRelays: bool + relayOwnStatus: NotRequired["RelayStatus"] + localDisplayName: str + groupProfile: "GroupProfile" + localAlias: str + businessChat: NotRequired["BusinessChatInfo"] + fullGroupPreferences: "FullGroupPreferences" + membership: "GroupMember" + chatSettings: "ChatSettings" + createdAt: str # ISO-8601 timestamp + updatedAt: str # ISO-8601 timestamp + chatTs: NotRequired[str] # ISO-8601 timestamp + userMemberProfileSentAt: NotRequired[str] # ISO-8601 timestamp + preparedGroup: NotRequired["PreparedGroup"] + chatTags: list[int] # int64 + chatItemTTL: NotRequired[int] # int64 + uiThemes: NotRequired["UIThemeEntityOverrides"] + customData: NotRequired[dict[str, object]] + groupSummary: "GroupSummary" + membersRequireAttention: int # int + viaGroupLinkUri: NotRequired[str] + groupKeys: NotRequired["GroupKeys"] + +class GroupKeys(TypedDict): + publicGroupId: str + groupRootKey: "GroupRootKey" + memberPrivKey: str + +class GroupLink(TypedDict): + userContactLinkId: int # int64 + connLinkContact: "CreatedConnLink" + shortLinkDataSet: bool + shortLinkLargeDataSet: bool + groupLinkId: str + acceptMemberRole: "GroupMemberRole" + +class GroupLinkOwner(TypedDict): + memberId: str + memberKey: str + +class GroupLinkPlan_ok(TypedDict): + type: Literal["ok"] + groupSLinkInfo_: NotRequired["GroupShortLinkInfo"] + groupSLinkData_: NotRequired["GroupShortLinkData"] + ownerVerification: NotRequired["OwnerVerification"] + +class GroupLinkPlan_ownLink(TypedDict): + type: Literal["ownLink"] + groupInfo: "GroupInfo" + +class GroupLinkPlan_connectingConfirmReconnect(TypedDict): + type: Literal["connectingConfirmReconnect"] + +class GroupLinkPlan_connectingProhibit(TypedDict): + type: Literal["connectingProhibit"] + groupInfo_: NotRequired["GroupInfo"] + +class GroupLinkPlan_known(TypedDict): + type: Literal["known"] + groupInfo: "GroupInfo" + groupUpdated: bool + ownerVerification: NotRequired["OwnerVerification"] + linkOwners: list["GroupLinkOwner"] + +class GroupLinkPlan_noRelays(TypedDict): + type: Literal["noRelays"] + groupSLinkData_: NotRequired["GroupShortLinkData"] + +GroupLinkPlan = ( + GroupLinkPlan_ok + | GroupLinkPlan_ownLink + | GroupLinkPlan_connectingConfirmReconnect + | GroupLinkPlan_connectingProhibit + | GroupLinkPlan_known + | GroupLinkPlan_noRelays +) + +GroupLinkPlan_Tag = Literal["ok", "ownLink", "connectingConfirmReconnect", "connectingProhibit", "known", "noRelays"] + +class GroupMember(TypedDict): + groupMemberId: int # int64 + groupId: int # int64 + indexInGroup: int # int64 + memberId: str + memberRole: "GroupMemberRole" + memberCategory: "GroupMemberCategory" + memberStatus: "GroupMemberStatus" + memberSettings: "GroupMemberSettings" + blockedByAdmin: bool + invitedBy: "InvitedBy" + invitedByGroupMemberId: NotRequired[int] # int64 + localDisplayName: str + memberProfile: "LocalProfile" + memberContactId: NotRequired[int] # int64 + memberContactProfileId: int # int64 + activeConn: NotRequired["Connection"] + memberChatVRange: "VersionRange" + createdAt: str # ISO-8601 timestamp + updatedAt: str # ISO-8601 timestamp + supportChat: NotRequired["GroupSupportChat"] + memberPubKey: NotRequired[str] + relayLink: NotRequired[str] + +class GroupMemberAdmission(TypedDict): + review: NotRequired["MemberCriteria"] + +GroupMemberCategory = Literal["user", "invitee", "host", "pre", "post"] + +class GroupMemberRef(TypedDict): + groupMemberId: int # int64 + profile: "Profile" + +GroupMemberRole = Literal["relay", "observer", "author", "member", "moderator", "admin", "owner"] + +class GroupMemberSettings(TypedDict): + showMessages: bool + +GroupMemberStatus = Literal["rejected", "removed", "left", "deleted", "unknown", "invited", "pending_approval", "pending_review", "introduced", "intro-inv", "accepted", "announced", "connected", "complete", "creator"] + +class GroupPreference(TypedDict): + enable: "GroupFeatureEnabled" + +class GroupPreferences(TypedDict): + timedMessages: NotRequired["TimedMessagesGroupPreference"] + directMessages: NotRequired["RoleGroupPreference"] + fullDelete: NotRequired["GroupPreference"] + reactions: NotRequired["GroupPreference"] + voice: NotRequired["RoleGroupPreference"] + files: NotRequired["RoleGroupPreference"] + simplexLinks: NotRequired["RoleGroupPreference"] + reports: NotRequired["GroupPreference"] + history: NotRequired["GroupPreference"] + support: NotRequired["SupportGroupPreference"] + sessions: NotRequired["RoleGroupPreference"] + comments: NotRequired["CommentsGroupPreference"] + commands: NotRequired[list["ChatBotCommand"]] + +class GroupProfile(TypedDict): + displayName: str + fullName: str + shortDescr: NotRequired[str] + description: NotRequired[str] + image: NotRequired[str] + publicGroup: NotRequired["PublicGroupProfile"] + groupPreferences: NotRequired["GroupPreferences"] + memberAdmission: NotRequired["GroupMemberAdmission"] + +class GroupRelay(TypedDict): + groupRelayId: int # int64 + groupMemberId: int # int64 + userChatRelay: "UserChatRelay" + relayStatus: "RelayStatus" + relayLink: NotRequired[str] + +class GroupRootKey_private(TypedDict): + type: Literal["private"] + rootPrivKey: str + +class GroupRootKey_public(TypedDict): + type: Literal["public"] + rootPubKey: str + +GroupRootKey = GroupRootKey_private | GroupRootKey_public + +GroupRootKey_Tag = Literal["private", "public"] + +class GroupShortLinkData(TypedDict): + groupProfile: "GroupProfile" + publicGroupData: NotRequired["PublicGroupData"] + +class GroupShortLinkInfo(TypedDict): + direct: bool + groupRelays: list[str] + publicGroupId: NotRequired[str] + +class GroupSummary(TypedDict): + currentMembers: int # int64 + publicMemberCount: NotRequired[int] # int64 + +class GroupSupportChat(TypedDict): + chatTs: str # ISO-8601 timestamp + unread: int # int64 + memberAttention: int # int64 + mentions: int # int64 + lastMsgFromMemberTs: NotRequired[str] # ISO-8601 timestamp + +GroupType = Literal["channel", "group"] + +HandshakeError = Literal["PARSE", "IDENTITY", "BAD_AUTH", "BAD_SERVICE"] + +InlineFileMode = Literal["offer", "sent"] + +class InvitationLinkPlan_ok(TypedDict): + type: Literal["ok"] + contactSLinkData_: NotRequired["ContactShortLinkData"] + ownerVerification: NotRequired["OwnerVerification"] + +class InvitationLinkPlan_ownLink(TypedDict): + type: Literal["ownLink"] + +class InvitationLinkPlan_connecting(TypedDict): + type: Literal["connecting"] + contact_: NotRequired["Contact"] + +class InvitationLinkPlan_known(TypedDict): + type: Literal["known"] + contact: "Contact" + +InvitationLinkPlan = ( + InvitationLinkPlan_ok + | InvitationLinkPlan_ownLink + | InvitationLinkPlan_connecting + | InvitationLinkPlan_known +) + +InvitationLinkPlan_Tag = Literal["ok", "ownLink", "connecting", "known"] + +class InvitedBy_contact(TypedDict): + type: Literal["contact"] + byContactId: int # int64 + +class InvitedBy_user(TypedDict): + type: Literal["user"] + +class InvitedBy_unknown(TypedDict): + type: Literal["unknown"] + +InvitedBy = InvitedBy_contact | InvitedBy_user | InvitedBy_unknown + +InvitedBy_Tag = Literal["contact", "user", "unknown"] + +class LinkContent_page(TypedDict): + type: Literal["page"] + +class LinkContent_image(TypedDict): + type: Literal["image"] + +class LinkContent_video(TypedDict): + type: Literal["video"] + duration: NotRequired[int] # int + +class LinkContent_unknown(TypedDict): + type: Literal["unknown"] + tag: str + json: dict[str, object] + +LinkContent = LinkContent_page | LinkContent_image | LinkContent_video | LinkContent_unknown + +LinkContent_Tag = Literal["page", "image", "video", "unknown"] + +class LinkOwnerSig(TypedDict): + ownerId: NotRequired[str] + chatBinding: str + ownerSig: str + +class LinkPreview(TypedDict): + uri: str + title: str + description: str + image: str + content: NotRequired["LinkContent"] + +class LocalProfile(TypedDict): + profileId: int # int64 + displayName: str + fullName: str + shortDescr: NotRequired[str] + image: NotRequired[str] + contactLink: NotRequired[str] + preferences: NotRequired["Preferences"] + peerType: NotRequired["ChatPeerType"] + localAlias: str + +MemberCriteria = Literal["all"] + +# Connection link sent in a message - only short links are allowed. + +class MsgChatLink_contact(TypedDict): + type: Literal["contact"] + connLink: str + profile: "Profile" + business: bool + +class MsgChatLink_invitation(TypedDict): + type: Literal["invitation"] + invLink: str + profile: "Profile" + +class MsgChatLink_group(TypedDict): + type: Literal["group"] + connLink: str + groupProfile: "GroupProfile" + +MsgChatLink = MsgChatLink_contact | MsgChatLink_invitation | MsgChatLink_group + +MsgChatLink_Tag = Literal["contact", "invitation", "group"] + +class MsgContent_text(TypedDict): + type: Literal["text"] + text: str + +class MsgContent_link(TypedDict): + type: Literal["link"] + text: str + preview: "LinkPreview" + +class MsgContent_image(TypedDict): + type: Literal["image"] + text: str + image: str + +class MsgContent_video(TypedDict): + type: Literal["video"] + text: str + image: str + duration: int # int + +class MsgContent_voice(TypedDict): + type: Literal["voice"] + text: str + duration: int # int + +class MsgContent_file(TypedDict): + type: Literal["file"] + text: str + +class MsgContent_report(TypedDict): + type: Literal["report"] + text: str + reason: "ReportReason" + +class MsgContent_chat(TypedDict): + type: Literal["chat"] + text: str + chatLink: "MsgChatLink" + ownerSig: NotRequired["LinkOwnerSig"] + +class MsgContent_unknown(TypedDict): + type: Literal["unknown"] + tag: str + text: str + json: dict[str, object] + +MsgContent = ( + MsgContent_text + | MsgContent_link + | MsgContent_image + | MsgContent_video + | MsgContent_voice + | MsgContent_file + | MsgContent_report + | MsgContent_chat + | MsgContent_unknown +) + +MsgContent_Tag = Literal["text", "link", "image", "video", "voice", "file", "report", "chat", "unknown"] + +MsgDecryptError = Literal["ratchetHeader", "tooManySkipped", "ratchetEarlier", "other", "ratchetSync"] + +MsgDirection = Literal["rcv", "snd"] + +class MsgErrorType_msgSkipped(TypedDict): + type: Literal["msgSkipped"] + fromMsgId: int # int64 + toMsgId: int # int64 + +class MsgErrorType_msgBadId(TypedDict): + type: Literal["msgBadId"] + msgId: int # int64 + +class MsgErrorType_msgBadHash(TypedDict): + type: Literal["msgBadHash"] + +class MsgErrorType_msgDuplicate(TypedDict): + type: Literal["msgDuplicate"] + +MsgErrorType = ( + MsgErrorType_msgSkipped + | MsgErrorType_msgBadId + | MsgErrorType_msgBadHash + | MsgErrorType_msgDuplicate +) + +MsgErrorType_Tag = Literal["msgSkipped", "msgBadId", "msgBadHash", "msgDuplicate"] + +MsgFilter = Literal["none", "all", "mentions"] + +class MsgReaction_emoji(TypedDict): + type: Literal["emoji"] + emoji: str + +class MsgReaction_unknown(TypedDict): + type: Literal["unknown"] + tag: str + json: dict[str, object] + +MsgReaction = MsgReaction_emoji | MsgReaction_unknown + +MsgReaction_Tag = Literal["emoji", "unknown"] + +MsgReceiptStatus = Literal["ok", "badMsgHash"] + +MsgSigStatus = Literal["verified", "signedNoKey"] + +class NetworkError_connectError(TypedDict): + type: Literal["connectError"] + connectError: str + +class NetworkError_tLSError(TypedDict): + type: Literal["tLSError"] + tlsError: str + +class NetworkError_unknownCAError(TypedDict): + type: Literal["unknownCAError"] + +class NetworkError_failedError(TypedDict): + type: Literal["failedError"] + +class NetworkError_timeoutError(TypedDict): + type: Literal["timeoutError"] + +class NetworkError_subscribeError(TypedDict): + type: Literal["subscribeError"] + subscribeError: str + +NetworkError = ( + NetworkError_connectError + | NetworkError_tLSError + | NetworkError_unknownCAError + | NetworkError_failedError + | NetworkError_timeoutError + | NetworkError_subscribeError +) + +NetworkError_Tag = Literal["connectError", "tLSError", "unknownCAError", "failedError", "timeoutError", "subscribeError"] + +class NewUser(TypedDict): + profile: NotRequired["Profile"] + pastTimestamp: bool + userChatRelay: bool + +class NoteFolder(TypedDict): + noteFolderId: int # int64 + userId: int # int64 + createdAt: str # ISO-8601 timestamp + updatedAt: str # ISO-8601 timestamp + chatTs: str # ISO-8601 timestamp + favorite: bool + unread: bool + +class OwnerVerification_verified(TypedDict): + type: Literal["verified"] + +class OwnerVerification_failed(TypedDict): + type: Literal["failed"] + reason: str + +OwnerVerification = OwnerVerification_verified | OwnerVerification_failed + +OwnerVerification_Tag = Literal["verified", "failed"] + +class PaginationByTime_last(TypedDict): + type: Literal["last"] + count: int # int + +PaginationByTime = PaginationByTime_last + +PaginationByTime_Tag = Literal["last"] + + +def PaginationByTime_cmd_string(self: PaginationByTime) -> str: + return 'count=' + str(self['count']) # type: ignore[typeddict-item] + +class PendingContactConnection(TypedDict): + pccConnId: int # int64 + pccAgentConnId: str + pccConnStatus: "ConnStatus" + viaContactUri: bool + viaUserContactLink: NotRequired[int] # int64 + groupLinkId: NotRequired[str] + customUserProfileId: NotRequired[int] # int64 + connLinkInv: NotRequired["CreatedConnLink"] + localAlias: str + createdAt: str # ISO-8601 timestamp + updatedAt: str # ISO-8601 timestamp + +class PrefEnabled(TypedDict): + forUser: bool + forContact: bool + +class Preferences(TypedDict): + timedMessages: NotRequired["TimedMessagesPreference"] + fullDelete: NotRequired["SimplePreference"] + reactions: NotRequired["SimplePreference"] + voice: NotRequired["SimplePreference"] + files: NotRequired["SimplePreference"] + calls: NotRequired["SimplePreference"] + sessions: NotRequired["SimplePreference"] + commands: NotRequired[list["ChatBotCommand"]] + +class PreparedContact(TypedDict): + connLinkToConnect: "CreatedConnLink" + uiConnLinkType: "ConnectionMode" + welcomeSharedMsgId: NotRequired[str] + requestSharedMsgId: NotRequired[str] + +class PreparedGroup(TypedDict): + connLinkToConnect: "CreatedConnLink" + connLinkPreparedConnection: bool + connLinkStartedConnection: bool + welcomeSharedMsgId: NotRequired[str] + requestSharedMsgId: NotRequired[str] + +class Profile(TypedDict): + displayName: str + fullName: str + shortDescr: NotRequired[str] + image: NotRequired[str] + contactLink: NotRequired[str] + preferences: NotRequired["Preferences"] + peerType: NotRequired["ChatPeerType"] + +class ProxyClientError_protocolError(TypedDict): + type: Literal["protocolError"] + protocolErr: "ErrorType" + +class ProxyClientError_unexpectedResponse(TypedDict): + type: Literal["unexpectedResponse"] + responseStr: str + +class ProxyClientError_responseError(TypedDict): + type: Literal["responseError"] + responseErr: "ErrorType" + +ProxyClientError = ( + ProxyClientError_protocolError + | ProxyClientError_unexpectedResponse + | ProxyClientError_responseError +) + +ProxyClientError_Tag = Literal["protocolError", "unexpectedResponse", "responseError"] + +class ProxyError_PROTOCOL(TypedDict): + type: Literal["PROTOCOL"] + protocolErr: "ErrorType" + +class ProxyError_BROKER(TypedDict): + type: Literal["BROKER"] + brokerErr: "BrokerErrorType" + +class ProxyError_BASIC_AUTH(TypedDict): + type: Literal["BASIC_AUTH"] + +class ProxyError_NO_SESSION(TypedDict): + type: Literal["NO_SESSION"] + +ProxyError = ProxyError_PROTOCOL | ProxyError_BROKER | ProxyError_BASIC_AUTH | ProxyError_NO_SESSION + +ProxyError_Tag = Literal["PROTOCOL", "BROKER", "BASIC_AUTH", "NO_SESSION"] + +class PublicGroupData(TypedDict): + publicMemberCount: int # int64 + +class PublicGroupProfile(TypedDict): + groupType: "GroupType" + groupLink: str + publicGroupId: str + +class RCErrorType_internal(TypedDict): + type: Literal["internal"] + internalErr: str + +class RCErrorType_identity(TypedDict): + type: Literal["identity"] + +class RCErrorType_noLocalAddress(TypedDict): + type: Literal["noLocalAddress"] + +class RCErrorType_newController(TypedDict): + type: Literal["newController"] + +class RCErrorType_notDiscovered(TypedDict): + type: Literal["notDiscovered"] + +class RCErrorType_tLSStartFailed(TypedDict): + type: Literal["tLSStartFailed"] + +class RCErrorType_exception(TypedDict): + type: Literal["exception"] + exception: str + +class RCErrorType_ctrlAuth(TypedDict): + type: Literal["ctrlAuth"] + +class RCErrorType_ctrlNotFound(TypedDict): + type: Literal["ctrlNotFound"] + +class RCErrorType_ctrlError(TypedDict): + type: Literal["ctrlError"] + ctrlErr: str + +class RCErrorType_invitation(TypedDict): + type: Literal["invitation"] + +class RCErrorType_version(TypedDict): + type: Literal["version"] + +class RCErrorType_encrypt(TypedDict): + type: Literal["encrypt"] + +class RCErrorType_decrypt(TypedDict): + type: Literal["decrypt"] + +class RCErrorType_blockSize(TypedDict): + type: Literal["blockSize"] + +class RCErrorType_syntax(TypedDict): + type: Literal["syntax"] + syntaxErr: str + +RCErrorType = ( + RCErrorType_internal + | RCErrorType_identity + | RCErrorType_noLocalAddress + | RCErrorType_newController + | RCErrorType_notDiscovered + | RCErrorType_tLSStartFailed + | RCErrorType_exception + | RCErrorType_ctrlAuth + | RCErrorType_ctrlNotFound + | RCErrorType_ctrlError + | RCErrorType_invitation + | RCErrorType_version + | RCErrorType_encrypt + | RCErrorType_decrypt + | RCErrorType_blockSize + | RCErrorType_syntax +) + +RCErrorType_Tag = Literal["internal", "identity", "noLocalAddress", "newController", "notDiscovered", "tLSStartFailed", "exception", "ctrlAuth", "ctrlNotFound", "ctrlError", "invitation", "version", "encrypt", "decrypt", "blockSize", "syntax"] + +RatchetSyncState = Literal["ok", "allowed", "required", "started", "agreed"] + +class RcvConnEvent_switchQueue(TypedDict): + type: Literal["switchQueue"] + phase: "SwitchPhase" + +class RcvConnEvent_ratchetSync(TypedDict): + type: Literal["ratchetSync"] + syncStatus: "RatchetSyncState" + +class RcvConnEvent_verificationCodeReset(TypedDict): + type: Literal["verificationCodeReset"] + +class RcvConnEvent_pqEnabled(TypedDict): + type: Literal["pqEnabled"] + enabled: bool + +RcvConnEvent = ( + RcvConnEvent_switchQueue + | RcvConnEvent_ratchetSync + | RcvConnEvent_verificationCodeReset + | RcvConnEvent_pqEnabled +) + +RcvConnEvent_Tag = Literal["switchQueue", "ratchetSync", "verificationCodeReset", "pqEnabled"] + +class RcvDirectEvent_contactDeleted(TypedDict): + type: Literal["contactDeleted"] + +class RcvDirectEvent_profileUpdated(TypedDict): + type: Literal["profileUpdated"] + fromProfile: "Profile" + toProfile: "Profile" + +class RcvDirectEvent_groupInvLinkReceived(TypedDict): + type: Literal["groupInvLinkReceived"] + groupProfile: "GroupProfile" + +RcvDirectEvent = ( + RcvDirectEvent_contactDeleted + | RcvDirectEvent_profileUpdated + | RcvDirectEvent_groupInvLinkReceived +) + +RcvDirectEvent_Tag = Literal["contactDeleted", "profileUpdated", "groupInvLinkReceived"] + +class RcvFileDescr(TypedDict): + fileDescrId: int # int64 + fileDescrText: str + fileDescrPartNo: int # int + fileDescrComplete: bool + +class RcvFileStatus_new(TypedDict): + type: Literal["new"] + +class RcvFileStatus_accepted(TypedDict): + type: Literal["accepted"] + filePath: str + +class RcvFileStatus_connected(TypedDict): + type: Literal["connected"] + filePath: str + +class RcvFileStatus_complete(TypedDict): + type: Literal["complete"] + filePath: str + +class RcvFileStatus_cancelled(TypedDict): + type: Literal["cancelled"] + filePath_: NotRequired[str] + +RcvFileStatus = ( + RcvFileStatus_new + | RcvFileStatus_accepted + | RcvFileStatus_connected + | RcvFileStatus_complete + | RcvFileStatus_cancelled +) + +RcvFileStatus_Tag = Literal["new", "accepted", "connected", "complete", "cancelled"] + +class RcvFileTransfer(TypedDict): + fileId: int # int64 + xftpRcvFile: NotRequired["XFTPRcvFile"] + fileInvitation: "FileInvitation" + fileStatus: "RcvFileStatus" + rcvFileInline: NotRequired["InlineFileMode"] + senderDisplayName: str + chunkSize: int # int64 + cancelled: bool + grpMemberId: NotRequired[int] # int64 + cryptoArgs: NotRequired["CryptoFileArgs"] + +class RcvGroupEvent_memberAdded(TypedDict): + type: Literal["memberAdded"] + groupMemberId: int # int64 + profile: "Profile" + +class RcvGroupEvent_memberConnected(TypedDict): + type: Literal["memberConnected"] + +class RcvGroupEvent_memberAccepted(TypedDict): + type: Literal["memberAccepted"] + groupMemberId: int # int64 + profile: "Profile" + +class RcvGroupEvent_userAccepted(TypedDict): + type: Literal["userAccepted"] + +class RcvGroupEvent_memberLeft(TypedDict): + type: Literal["memberLeft"] + +class RcvGroupEvent_memberRole(TypedDict): + type: Literal["memberRole"] + groupMemberId: int # int64 + profile: "Profile" + role: "GroupMemberRole" + +class RcvGroupEvent_memberBlocked(TypedDict): + type: Literal["memberBlocked"] + groupMemberId: int # int64 + profile: "Profile" + blocked: bool + +class RcvGroupEvent_userRole(TypedDict): + type: Literal["userRole"] + role: "GroupMemberRole" + +class RcvGroupEvent_memberDeleted(TypedDict): + type: Literal["memberDeleted"] + groupMemberId: int # int64 + profile: "Profile" + +class RcvGroupEvent_userDeleted(TypedDict): + type: Literal["userDeleted"] + +class RcvGroupEvent_groupDeleted(TypedDict): + type: Literal["groupDeleted"] + +class RcvGroupEvent_groupUpdated(TypedDict): + type: Literal["groupUpdated"] + groupProfile: "GroupProfile" + +class RcvGroupEvent_invitedViaGroupLink(TypedDict): + type: Literal["invitedViaGroupLink"] + +class RcvGroupEvent_memberCreatedContact(TypedDict): + type: Literal["memberCreatedContact"] + +class RcvGroupEvent_memberProfileUpdated(TypedDict): + type: Literal["memberProfileUpdated"] + fromProfile: "Profile" + toProfile: "Profile" + +class RcvGroupEvent_newMemberPendingReview(TypedDict): + type: Literal["newMemberPendingReview"] + +class RcvGroupEvent_msgBadSignature(TypedDict): + type: Literal["msgBadSignature"] + +RcvGroupEvent = ( + RcvGroupEvent_memberAdded + | RcvGroupEvent_memberConnected + | RcvGroupEvent_memberAccepted + | RcvGroupEvent_userAccepted + | RcvGroupEvent_memberLeft + | RcvGroupEvent_memberRole + | RcvGroupEvent_memberBlocked + | RcvGroupEvent_userRole + | RcvGroupEvent_memberDeleted + | RcvGroupEvent_userDeleted + | RcvGroupEvent_groupDeleted + | RcvGroupEvent_groupUpdated + | RcvGroupEvent_invitedViaGroupLink + | RcvGroupEvent_memberCreatedContact + | RcvGroupEvent_memberProfileUpdated + | RcvGroupEvent_newMemberPendingReview + | RcvGroupEvent_msgBadSignature +) + +RcvGroupEvent_Tag = Literal["memberAdded", "memberConnected", "memberAccepted", "userAccepted", "memberLeft", "memberRole", "memberBlocked", "userRole", "memberDeleted", "userDeleted", "groupDeleted", "groupUpdated", "invitedViaGroupLink", "memberCreatedContact", "memberProfileUpdated", "newMemberPendingReview", "msgBadSignature"] + +class RcvMsgError_dropped(TypedDict): + type: Literal["dropped"] + attempts: int # int + +class RcvMsgError_parseError(TypedDict): + type: Literal["parseError"] + parseError: str + +RcvMsgError = RcvMsgError_dropped | RcvMsgError_parseError + +RcvMsgError_Tag = Literal["dropped", "parseError"] + +class RelayProfile(TypedDict): + displayName: str + fullName: str + shortDescr: NotRequired[str] + image: NotRequired[str] + +RelayStatus = Literal["new", "invited", "accepted", "active", "inactive"] + +ReportReason = Literal["spam", "content", "community", "profile", "other"] + +class RoleGroupPreference(TypedDict): + enable: "GroupFeatureEnabled" + role: NotRequired["GroupMemberRole"] + +class SMPAgentError_A_MESSAGE(TypedDict): + type: Literal["A_MESSAGE"] + +class SMPAgentError_A_PROHIBITED(TypedDict): + type: Literal["A_PROHIBITED"] + prohibitedErr: str + +class SMPAgentError_A_VERSION(TypedDict): + type: Literal["A_VERSION"] + +class SMPAgentError_A_LINK(TypedDict): + type: Literal["A_LINK"] + linkErr: str + +class SMPAgentError_A_CRYPTO(TypedDict): + type: Literal["A_CRYPTO"] + cryptoErr: "AgentCryptoError" + +class SMPAgentError_A_DUPLICATE(TypedDict): + type: Literal["A_DUPLICATE"] + droppedMsg_: NotRequired["DroppedMsg"] + +class SMPAgentError_A_QUEUE(TypedDict): + type: Literal["A_QUEUE"] + queueErr: str + +SMPAgentError = ( + SMPAgentError_A_MESSAGE + | SMPAgentError_A_PROHIBITED + | SMPAgentError_A_VERSION + | SMPAgentError_A_LINK + | SMPAgentError_A_CRYPTO + | SMPAgentError_A_DUPLICATE + | SMPAgentError_A_QUEUE +) + +SMPAgentError_Tag = Literal["A_MESSAGE", "A_PROHIBITED", "A_VERSION", "A_LINK", "A_CRYPTO", "A_DUPLICATE", "A_QUEUE"] + +class SecurityCode(TypedDict): + securityCode: str + verifiedAt: str # ISO-8601 timestamp + +class SimplePreference(TypedDict): + allow: "FeatureAllowed" + +SimplexLinkType = Literal["contact", "invitation", "group", "channel", "relay"] + +SndCIStatusProgress = Literal["partial", "complete"] + +class SndConnEvent_switchQueue(TypedDict): + type: Literal["switchQueue"] + phase: "SwitchPhase" + member: NotRequired["GroupMemberRef"] + +class SndConnEvent_ratchetSync(TypedDict): + type: Literal["ratchetSync"] + syncStatus: "RatchetSyncState" + member: NotRequired["GroupMemberRef"] + +class SndConnEvent_pqEnabled(TypedDict): + type: Literal["pqEnabled"] + enabled: bool + +SndConnEvent = SndConnEvent_switchQueue | SndConnEvent_ratchetSync | SndConnEvent_pqEnabled + +SndConnEvent_Tag = Literal["switchQueue", "ratchetSync", "pqEnabled"] + +class SndError_auth(TypedDict): + type: Literal["auth"] + +class SndError_quota(TypedDict): + type: Literal["quota"] + +class SndError_expired(TypedDict): + type: Literal["expired"] + +class SndError_relay(TypedDict): + type: Literal["relay"] + srvError: "SrvError" + +class SndError_proxy(TypedDict): + type: Literal["proxy"] + proxyServer: str + srvError: "SrvError" + +class SndError_proxyRelay(TypedDict): + type: Literal["proxyRelay"] + proxyServer: str + srvError: "SrvError" + +class SndError_other(TypedDict): + type: Literal["other"] + sndError: str + +SndError = ( + SndError_auth + | SndError_quota + | SndError_expired + | SndError_relay + | SndError_proxy + | SndError_proxyRelay + | SndError_other +) + +SndError_Tag = Literal["auth", "quota", "expired", "relay", "proxy", "proxyRelay", "other"] + +class SndFileTransfer(TypedDict): + fileId: int # int64 + fileName: str + filePath: str + fileSize: int # int64 + chunkSize: int # int64 + recipientDisplayName: str + connId: int # int64 + agentConnId: str + groupMemberId: NotRequired[int] # int64 + fileStatus: "FileStatus" + fileDescrId: NotRequired[int] # int64 + fileInline: NotRequired["InlineFileMode"] + +class SndGroupEvent_memberRole(TypedDict): + type: Literal["memberRole"] + groupMemberId: int # int64 + profile: "Profile" + role: "GroupMemberRole" + +class SndGroupEvent_memberBlocked(TypedDict): + type: Literal["memberBlocked"] + groupMemberId: int # int64 + profile: "Profile" + blocked: bool + +class SndGroupEvent_userRole(TypedDict): + type: Literal["userRole"] + role: "GroupMemberRole" + +class SndGroupEvent_memberDeleted(TypedDict): + type: Literal["memberDeleted"] + groupMemberId: int # int64 + profile: "Profile" + +class SndGroupEvent_userLeft(TypedDict): + type: Literal["userLeft"] + +class SndGroupEvent_groupUpdated(TypedDict): + type: Literal["groupUpdated"] + groupProfile: "GroupProfile" + +class SndGroupEvent_memberAccepted(TypedDict): + type: Literal["memberAccepted"] + groupMemberId: int # int64 + profile: "Profile" + +class SndGroupEvent_userPendingReview(TypedDict): + type: Literal["userPendingReview"] + +SndGroupEvent = ( + SndGroupEvent_memberRole + | SndGroupEvent_memberBlocked + | SndGroupEvent_userRole + | SndGroupEvent_memberDeleted + | SndGroupEvent_userLeft + | SndGroupEvent_groupUpdated + | SndGroupEvent_memberAccepted + | SndGroupEvent_userPendingReview +) + +SndGroupEvent_Tag = Literal["memberRole", "memberBlocked", "userRole", "memberDeleted", "userLeft", "groupUpdated", "memberAccepted", "userPendingReview"] + +class SrvError_host(TypedDict): + type: Literal["host"] + +class SrvError_version(TypedDict): + type: Literal["version"] + +class SrvError_other(TypedDict): + type: Literal["other"] + srvError: str + +SrvError = SrvError_host | SrvError_version | SrvError_other + +SrvError_Tag = Literal["host", "version", "other"] + +class StoreError_duplicateName(TypedDict): + type: Literal["duplicateName"] + +class StoreError_userNotFound(TypedDict): + type: Literal["userNotFound"] + userId: int # int64 + +class StoreError_relayUserNotFound(TypedDict): + type: Literal["relayUserNotFound"] + +class StoreError_userNotFoundByName(TypedDict): + type: Literal["userNotFoundByName"] + contactName: str + +class StoreError_userNotFoundByContactId(TypedDict): + type: Literal["userNotFoundByContactId"] + contactId: int # int64 + +class StoreError_userNotFoundByGroupId(TypedDict): + type: Literal["userNotFoundByGroupId"] + groupId: int # int64 + +class StoreError_userNotFoundByFileId(TypedDict): + type: Literal["userNotFoundByFileId"] + fileId: int # int64 + +class StoreError_userNotFoundByContactRequestId(TypedDict): + type: Literal["userNotFoundByContactRequestId"] + contactRequestId: int # int64 + +class StoreError_contactNotFound(TypedDict): + type: Literal["contactNotFound"] + contactId: int # int64 + +class StoreError_contactNotFoundByName(TypedDict): + type: Literal["contactNotFoundByName"] + contactName: str + +class StoreError_contactNotFoundByMemberId(TypedDict): + type: Literal["contactNotFoundByMemberId"] + groupMemberId: int # int64 + +class StoreError_contactNotReady(TypedDict): + type: Literal["contactNotReady"] + contactName: str + +class StoreError_duplicateContactLink(TypedDict): + type: Literal["duplicateContactLink"] + +class StoreError_userContactLinkNotFound(TypedDict): + type: Literal["userContactLinkNotFound"] + +class StoreError_contactRequestNotFound(TypedDict): + type: Literal["contactRequestNotFound"] + contactRequestId: int # int64 + +class StoreError_contactRequestNotFoundByName(TypedDict): + type: Literal["contactRequestNotFoundByName"] + contactName: str + +class StoreError_invalidContactRequestEntity(TypedDict): + type: Literal["invalidContactRequestEntity"] + contactRequestId: int # int64 + +class StoreError_invalidBusinessChatContactRequest(TypedDict): + type: Literal["invalidBusinessChatContactRequest"] + +class StoreError_groupNotFound(TypedDict): + type: Literal["groupNotFound"] + groupId: int # int64 + +class StoreError_groupNotFoundByName(TypedDict): + type: Literal["groupNotFoundByName"] + groupName: str + +class StoreError_groupMemberNameNotFound(TypedDict): + type: Literal["groupMemberNameNotFound"] + groupId: int # int64 + groupMemberName: str + +class StoreError_groupMemberNotFound(TypedDict): + type: Literal["groupMemberNotFound"] + groupMemberId: int # int64 + +class StoreError_groupMemberNotFoundByIndex(TypedDict): + type: Literal["groupMemberNotFoundByIndex"] + groupMemberIndex: int # int64 + +class StoreError_memberRelationsVectorNotFound(TypedDict): + type: Literal["memberRelationsVectorNotFound"] + groupMemberId: int # int64 + +class StoreError_groupHostMemberNotFound(TypedDict): + type: Literal["groupHostMemberNotFound"] + groupId: int # int64 + +class StoreError_groupMemberNotFoundByMemberId(TypedDict): + type: Literal["groupMemberNotFoundByMemberId"] + memberId: str + +class StoreError_memberContactGroupMemberNotFound(TypedDict): + type: Literal["memberContactGroupMemberNotFound"] + contactId: int # int64 + +class StoreError_invalidMemberRelationUpdate(TypedDict): + type: Literal["invalidMemberRelationUpdate"] + +class StoreError_groupWithoutUser(TypedDict): + type: Literal["groupWithoutUser"] + +class StoreError_duplicateGroupMember(TypedDict): + type: Literal["duplicateGroupMember"] + +class StoreError_duplicateMemberId(TypedDict): + type: Literal["duplicateMemberId"] + +class StoreError_groupAlreadyJoined(TypedDict): + type: Literal["groupAlreadyJoined"] + +class StoreError_groupInvitationNotFound(TypedDict): + type: Literal["groupInvitationNotFound"] + +class StoreError_noteFolderAlreadyExists(TypedDict): + type: Literal["noteFolderAlreadyExists"] + noteFolderId: int # int64 + +class StoreError_noteFolderNotFound(TypedDict): + type: Literal["noteFolderNotFound"] + noteFolderId: int # int64 + +class StoreError_userNoteFolderNotFound(TypedDict): + type: Literal["userNoteFolderNotFound"] + +class StoreError_sndFileNotFound(TypedDict): + type: Literal["sndFileNotFound"] + fileId: int # int64 + +class StoreError_sndFileInvalid(TypedDict): + type: Literal["sndFileInvalid"] + fileId: int # int64 + +class StoreError_rcvFileNotFound(TypedDict): + type: Literal["rcvFileNotFound"] + fileId: int # int64 + +class StoreError_rcvFileDescrNotFound(TypedDict): + type: Literal["rcvFileDescrNotFound"] + fileId: int # int64 + +class StoreError_fileNotFound(TypedDict): + type: Literal["fileNotFound"] + fileId: int # int64 + +class StoreError_rcvFileInvalid(TypedDict): + type: Literal["rcvFileInvalid"] + fileId: int # int64 + +class StoreError_rcvFileInvalidDescrPart(TypedDict): + type: Literal["rcvFileInvalidDescrPart"] + +class StoreError_localFileNoTransfer(TypedDict): + type: Literal["localFileNoTransfer"] + fileId: int # int64 + +class StoreError_sharedMsgIdNotFoundByFileId(TypedDict): + type: Literal["sharedMsgIdNotFoundByFileId"] + fileId: int # int64 + +class StoreError_fileIdNotFoundBySharedMsgId(TypedDict): + type: Literal["fileIdNotFoundBySharedMsgId"] + sharedMsgId: str + +class StoreError_sndFileNotFoundXFTP(TypedDict): + type: Literal["sndFileNotFoundXFTP"] + agentSndFileId: str + +class StoreError_rcvFileNotFoundXFTP(TypedDict): + type: Literal["rcvFileNotFoundXFTP"] + agentRcvFileId: str + +class StoreError_connectionNotFound(TypedDict): + type: Literal["connectionNotFound"] + agentConnId: str + +class StoreError_connectionNotFoundById(TypedDict): + type: Literal["connectionNotFoundById"] + connId: int # int64 + +class StoreError_connectionNotFoundByMemberId(TypedDict): + type: Literal["connectionNotFoundByMemberId"] + groupMemberId: int # int64 + +class StoreError_pendingConnectionNotFound(TypedDict): + type: Literal["pendingConnectionNotFound"] + connId: int # int64 + +class StoreError_uniqueID(TypedDict): + type: Literal["uniqueID"] + +class StoreError_largeMsg(TypedDict): + type: Literal["largeMsg"] + +class StoreError_internalError(TypedDict): + type: Literal["internalError"] + message: str + +class StoreError_dBException(TypedDict): + type: Literal["dBException"] + message: str + +class StoreError_dBBusyError(TypedDict): + type: Literal["dBBusyError"] + message: str + +class StoreError_badChatItem(TypedDict): + type: Literal["badChatItem"] + itemId: int # int64 + itemTs: NotRequired[str] # ISO-8601 timestamp + +class StoreError_chatItemNotFound(TypedDict): + type: Literal["chatItemNotFound"] + itemId: int # int64 + +class StoreError_chatItemNotFoundByText(TypedDict): + type: Literal["chatItemNotFoundByText"] + text: str + +class StoreError_chatItemSharedMsgIdNotFound(TypedDict): + type: Literal["chatItemSharedMsgIdNotFound"] + sharedMsgId: str + +class StoreError_chatItemNotFoundByFileId(TypedDict): + type: Literal["chatItemNotFoundByFileId"] + fileId: int # int64 + +class StoreError_chatItemNotFoundByContactId(TypedDict): + type: Literal["chatItemNotFoundByContactId"] + contactId: int # int64 + +class StoreError_chatItemNotFoundByGroupId(TypedDict): + type: Literal["chatItemNotFoundByGroupId"] + groupId: int # int64 + +class StoreError_profileNotFound(TypedDict): + type: Literal["profileNotFound"] + profileId: int # int64 + +class StoreError_duplicateGroupLink(TypedDict): + type: Literal["duplicateGroupLink"] + groupInfo: "GroupInfo" + +class StoreError_groupLinkNotFound(TypedDict): + type: Literal["groupLinkNotFound"] + groupInfo: "GroupInfo" + +class StoreError_hostMemberIdNotFound(TypedDict): + type: Literal["hostMemberIdNotFound"] + groupId: int # int64 + +class StoreError_contactNotFoundByFileId(TypedDict): + type: Literal["contactNotFoundByFileId"] + fileId: int # int64 + +class StoreError_noGroupSndStatus(TypedDict): + type: Literal["noGroupSndStatus"] + itemId: int # int64 + groupMemberId: int # int64 + +class StoreError_duplicateGroupMessage(TypedDict): + type: Literal["duplicateGroupMessage"] + groupId: int # int64 + sharedMsgId: str + authorGroupMemberId: NotRequired[int] # int64 + forwardedByGroupMemberId: NotRequired[int] # int64 + +class StoreError_remoteHostNotFound(TypedDict): + type: Literal["remoteHostNotFound"] + remoteHostId: int # int64 + +class StoreError_remoteHostUnknown(TypedDict): + type: Literal["remoteHostUnknown"] + +class StoreError_remoteHostDuplicateCA(TypedDict): + type: Literal["remoteHostDuplicateCA"] + +class StoreError_remoteCtrlNotFound(TypedDict): + type: Literal["remoteCtrlNotFound"] + remoteCtrlId: int # int64 + +class StoreError_remoteCtrlDuplicateCA(TypedDict): + type: Literal["remoteCtrlDuplicateCA"] + +class StoreError_prohibitedDeleteUser(TypedDict): + type: Literal["prohibitedDeleteUser"] + userId: int # int64 + contactId: int # int64 + +class StoreError_operatorNotFound(TypedDict): + type: Literal["operatorNotFound"] + serverOperatorId: int # int64 + +class StoreError_usageConditionsNotFound(TypedDict): + type: Literal["usageConditionsNotFound"] + +class StoreError_userChatRelayNotFound(TypedDict): + type: Literal["userChatRelayNotFound"] + chatRelayId: int # int64 + +class StoreError_groupRelayNotFound(TypedDict): + type: Literal["groupRelayNotFound"] + groupRelayId: int # int64 + +class StoreError_groupRelayNotFoundByMemberId(TypedDict): + type: Literal["groupRelayNotFoundByMemberId"] + groupMemberId: int # int64 + +class StoreError_invalidQuote(TypedDict): + type: Literal["invalidQuote"] + +class StoreError_invalidMention(TypedDict): + type: Literal["invalidMention"] + +class StoreError_invalidDeliveryTask(TypedDict): + type: Literal["invalidDeliveryTask"] + taskId: int # int64 + +class StoreError_deliveryTaskNotFound(TypedDict): + type: Literal["deliveryTaskNotFound"] + taskId: int # int64 + +class StoreError_invalidDeliveryJob(TypedDict): + type: Literal["invalidDeliveryJob"] + jobId: int # int64 + +class StoreError_deliveryJobNotFound(TypedDict): + type: Literal["deliveryJobNotFound"] + jobId: int # int64 + +class StoreError_workItemError(TypedDict): + type: Literal["workItemError"] + errContext: str + +StoreError = ( + StoreError_duplicateName + | StoreError_userNotFound + | StoreError_relayUserNotFound + | StoreError_userNotFoundByName + | StoreError_userNotFoundByContactId + | StoreError_userNotFoundByGroupId + | StoreError_userNotFoundByFileId + | StoreError_userNotFoundByContactRequestId + | StoreError_contactNotFound + | StoreError_contactNotFoundByName + | StoreError_contactNotFoundByMemberId + | StoreError_contactNotReady + | StoreError_duplicateContactLink + | StoreError_userContactLinkNotFound + | StoreError_contactRequestNotFound + | StoreError_contactRequestNotFoundByName + | StoreError_invalidContactRequestEntity + | StoreError_invalidBusinessChatContactRequest + | StoreError_groupNotFound + | StoreError_groupNotFoundByName + | StoreError_groupMemberNameNotFound + | StoreError_groupMemberNotFound + | StoreError_groupMemberNotFoundByIndex + | StoreError_memberRelationsVectorNotFound + | StoreError_groupHostMemberNotFound + | StoreError_groupMemberNotFoundByMemberId + | StoreError_memberContactGroupMemberNotFound + | StoreError_invalidMemberRelationUpdate + | StoreError_groupWithoutUser + | StoreError_duplicateGroupMember + | StoreError_duplicateMemberId + | StoreError_groupAlreadyJoined + | StoreError_groupInvitationNotFound + | StoreError_noteFolderAlreadyExists + | StoreError_noteFolderNotFound + | StoreError_userNoteFolderNotFound + | StoreError_sndFileNotFound + | StoreError_sndFileInvalid + | StoreError_rcvFileNotFound + | StoreError_rcvFileDescrNotFound + | StoreError_fileNotFound + | StoreError_rcvFileInvalid + | StoreError_rcvFileInvalidDescrPart + | StoreError_localFileNoTransfer + | StoreError_sharedMsgIdNotFoundByFileId + | StoreError_fileIdNotFoundBySharedMsgId + | StoreError_sndFileNotFoundXFTP + | StoreError_rcvFileNotFoundXFTP + | StoreError_connectionNotFound + | StoreError_connectionNotFoundById + | StoreError_connectionNotFoundByMemberId + | StoreError_pendingConnectionNotFound + | StoreError_uniqueID + | StoreError_largeMsg + | StoreError_internalError + | StoreError_dBException + | StoreError_dBBusyError + | StoreError_badChatItem + | StoreError_chatItemNotFound + | StoreError_chatItemNotFoundByText + | StoreError_chatItemSharedMsgIdNotFound + | StoreError_chatItemNotFoundByFileId + | StoreError_chatItemNotFoundByContactId + | StoreError_chatItemNotFoundByGroupId + | StoreError_profileNotFound + | StoreError_duplicateGroupLink + | StoreError_groupLinkNotFound + | StoreError_hostMemberIdNotFound + | StoreError_contactNotFoundByFileId + | StoreError_noGroupSndStatus + | StoreError_duplicateGroupMessage + | StoreError_remoteHostNotFound + | StoreError_remoteHostUnknown + | StoreError_remoteHostDuplicateCA + | StoreError_remoteCtrlNotFound + | StoreError_remoteCtrlDuplicateCA + | StoreError_prohibitedDeleteUser + | StoreError_operatorNotFound + | StoreError_usageConditionsNotFound + | StoreError_userChatRelayNotFound + | StoreError_groupRelayNotFound + | StoreError_groupRelayNotFoundByMemberId + | StoreError_invalidQuote + | StoreError_invalidMention + | StoreError_invalidDeliveryTask + | StoreError_deliveryTaskNotFound + | StoreError_invalidDeliveryJob + | StoreError_deliveryJobNotFound + | StoreError_workItemError +) + +StoreError_Tag = Literal["duplicateName", "userNotFound", "relayUserNotFound", "userNotFoundByName", "userNotFoundByContactId", "userNotFoundByGroupId", "userNotFoundByFileId", "userNotFoundByContactRequestId", "contactNotFound", "contactNotFoundByName", "contactNotFoundByMemberId", "contactNotReady", "duplicateContactLink", "userContactLinkNotFound", "contactRequestNotFound", "contactRequestNotFoundByName", "invalidContactRequestEntity", "invalidBusinessChatContactRequest", "groupNotFound", "groupNotFoundByName", "groupMemberNameNotFound", "groupMemberNotFound", "groupMemberNotFoundByIndex", "memberRelationsVectorNotFound", "groupHostMemberNotFound", "groupMemberNotFoundByMemberId", "memberContactGroupMemberNotFound", "invalidMemberRelationUpdate", "groupWithoutUser", "duplicateGroupMember", "duplicateMemberId", "groupAlreadyJoined", "groupInvitationNotFound", "noteFolderAlreadyExists", "noteFolderNotFound", "userNoteFolderNotFound", "sndFileNotFound", "sndFileInvalid", "rcvFileNotFound", "rcvFileDescrNotFound", "fileNotFound", "rcvFileInvalid", "rcvFileInvalidDescrPart", "localFileNoTransfer", "sharedMsgIdNotFoundByFileId", "fileIdNotFoundBySharedMsgId", "sndFileNotFoundXFTP", "rcvFileNotFoundXFTP", "connectionNotFound", "connectionNotFoundById", "connectionNotFoundByMemberId", "pendingConnectionNotFound", "uniqueID", "largeMsg", "internalError", "dBException", "dBBusyError", "badChatItem", "chatItemNotFound", "chatItemNotFoundByText", "chatItemSharedMsgIdNotFound", "chatItemNotFoundByFileId", "chatItemNotFoundByContactId", "chatItemNotFoundByGroupId", "profileNotFound", "duplicateGroupLink", "groupLinkNotFound", "hostMemberIdNotFound", "contactNotFoundByFileId", "noGroupSndStatus", "duplicateGroupMessage", "remoteHostNotFound", "remoteHostUnknown", "remoteHostDuplicateCA", "remoteCtrlNotFound", "remoteCtrlDuplicateCA", "prohibitedDeleteUser", "operatorNotFound", "usageConditionsNotFound", "userChatRelayNotFound", "groupRelayNotFound", "groupRelayNotFoundByMemberId", "invalidQuote", "invalidMention", "invalidDeliveryTask", "deliveryTaskNotFound", "invalidDeliveryJob", "deliveryJobNotFound", "workItemError"] + +class SubscriptionStatus_active(TypedDict): + type: Literal["active"] + +class SubscriptionStatus_pending(TypedDict): + type: Literal["pending"] + +class SubscriptionStatus_removed(TypedDict): + type: Literal["removed"] + subError: str + +class SubscriptionStatus_noSub(TypedDict): + type: Literal["noSub"] + +SubscriptionStatus = ( + SubscriptionStatus_active + | SubscriptionStatus_pending + | SubscriptionStatus_removed + | SubscriptionStatus_noSub +) + +SubscriptionStatus_Tag = Literal["active", "pending", "removed", "noSub"] + +class SupportGroupPreference(TypedDict): + enable: "GroupFeatureEnabled" + +SwitchPhase = Literal["started", "confirmed", "secured", "completed"] + +class TimedMessagesGroupPreference(TypedDict): + enable: "GroupFeatureEnabled" + ttl: NotRequired[int] # int + +class TimedMessagesPreference(TypedDict): + allow: "FeatureAllowed" + ttl: NotRequired[int] # int + +class TransportError_badBlock(TypedDict): + type: Literal["badBlock"] + +class TransportError_version(TypedDict): + type: Literal["version"] + +class TransportError_largeMsg(TypedDict): + type: Literal["largeMsg"] + +class TransportError_badSession(TypedDict): + type: Literal["badSession"] + +class TransportError_noServerAuth(TypedDict): + type: Literal["noServerAuth"] + +class TransportError_handshake(TypedDict): + type: Literal["handshake"] + handshakeErr: "HandshakeError" + +TransportError = ( + TransportError_badBlock + | TransportError_version + | TransportError_largeMsg + | TransportError_badSession + | TransportError_noServerAuth + | TransportError_handshake +) + +TransportError_Tag = Literal["badBlock", "version", "largeMsg", "badSession", "noServerAuth", "handshake"] + +UIColorMode = Literal["light", "dark"] + +class UIColors(TypedDict): + accent: NotRequired[str] + accentVariant: NotRequired[str] + secondary: NotRequired[str] + secondaryVariant: NotRequired[str] + background: NotRequired[str] + menus: NotRequired[str] + title: NotRequired[str] + accentVariant2: NotRequired[str] + sentMessage: NotRequired[str] + sentReply: NotRequired[str] + receivedMessage: NotRequired[str] + receivedReply: NotRequired[str] + +class UIThemeEntityOverride(TypedDict): + mode: "UIColorMode" + wallpaper: NotRequired["ChatWallpaper"] + colors: "UIColors" + +class UIThemeEntityOverrides(TypedDict): + light: NotRequired["UIThemeEntityOverride"] + dark: NotRequired["UIThemeEntityOverride"] + +class UpdatedMessage(TypedDict): + msgContent: "MsgContent" + mentions: dict[str, int] # str : int64 + +class User(TypedDict): + userId: int # int64 + agentUserId: int # int64 + userContactId: int # int64 + localDisplayName: str + profile: "LocalProfile" + fullPreferences: "FullPreferences" + activeUser: bool + activeOrder: int # int64 + viewPwdHash: NotRequired["UserPwdHash"] + showNtfs: bool + sendRcptsContacts: bool + sendRcptsSmallGroups: bool + autoAcceptMemberContacts: bool + userMemberProfileUpdatedAt: NotRequired[str] # ISO-8601 timestamp + uiThemes: NotRequired["UIThemeEntityOverrides"] + userChatRelay: bool + +class UserChatRelay(TypedDict): + chatRelayId: int # int64 + address: str + relayProfile: "RelayProfile" + domains: list[str] + preset: bool + tested: NotRequired[bool] + enabled: bool + deleted: bool + +class UserContact(TypedDict): + userContactLinkId: int # int64 + connReqContact: str + groupId: NotRequired[int] # int64 + +class UserContactLink(TypedDict): + userContactLinkId: int # int64 + connLinkContact: "CreatedConnLink" + shortLinkDataSet: bool + shortLinkLargeDataSet: bool + addressSettings: "AddressSettings" + +class UserContactRequest(TypedDict): + contactRequestId: int # int64 + agentInvitationId: str + contactId_: NotRequired[int] # int64 + businessGroupId_: NotRequired[int] # int64 + userContactLinkId_: NotRequired[int] # int64 + cReqChatVRange: "VersionRange" + localDisplayName: str + profileId: int # int64 + profile: "Profile" + createdAt: str # ISO-8601 timestamp + updatedAt: str # ISO-8601 timestamp + xContactId: NotRequired[str] + pqSupport: bool + welcomeSharedMsgId: NotRequired[str] + requestSharedMsgId: NotRequired[str] + +class UserInfo(TypedDict): + user: "User" + unreadCount: int # int + +class UserProfileUpdateSummary(TypedDict): + updateSuccesses: int # int + updateFailures: int # int + changedContacts: list["Contact"] + +class UserPwdHash(TypedDict): + hash: str + salt: str + +class VersionRange(TypedDict): + minVersion: int # int + maxVersion: int # int + +class XFTPErrorType_BLOCK(TypedDict): + type: Literal["BLOCK"] + +class XFTPErrorType_SESSION(TypedDict): + type: Literal["SESSION"] + +class XFTPErrorType_HANDSHAKE(TypedDict): + type: Literal["HANDSHAKE"] + +class XFTPErrorType_CMD(TypedDict): + type: Literal["CMD"] + cmdErr: "CommandError" + +class XFTPErrorType_AUTH(TypedDict): + type: Literal["AUTH"] + +class XFTPErrorType_BLOCKED(TypedDict): + type: Literal["BLOCKED"] + blockInfo: "BlockingInfo" + +class XFTPErrorType_SIZE(TypedDict): + type: Literal["SIZE"] + +class XFTPErrorType_QUOTA(TypedDict): + type: Literal["QUOTA"] + +class XFTPErrorType_DIGEST(TypedDict): + type: Literal["DIGEST"] + +class XFTPErrorType_CRYPTO(TypedDict): + type: Literal["CRYPTO"] + +class XFTPErrorType_NO_FILE(TypedDict): + type: Literal["NO_FILE"] + +class XFTPErrorType_HAS_FILE(TypedDict): + type: Literal["HAS_FILE"] + +class XFTPErrorType_FILE_IO(TypedDict): + type: Literal["FILE_IO"] + +class XFTPErrorType_TIMEOUT(TypedDict): + type: Literal["TIMEOUT"] + +class XFTPErrorType_INTERNAL(TypedDict): + type: Literal["INTERNAL"] + +class XFTPErrorType_DUPLICATE_(TypedDict): + type: Literal["DUPLICATE_"] + +XFTPErrorType = ( + XFTPErrorType_BLOCK + | XFTPErrorType_SESSION + | XFTPErrorType_HANDSHAKE + | XFTPErrorType_CMD + | XFTPErrorType_AUTH + | XFTPErrorType_BLOCKED + | XFTPErrorType_SIZE + | XFTPErrorType_QUOTA + | XFTPErrorType_DIGEST + | XFTPErrorType_CRYPTO + | XFTPErrorType_NO_FILE + | XFTPErrorType_HAS_FILE + | XFTPErrorType_FILE_IO + | XFTPErrorType_TIMEOUT + | XFTPErrorType_INTERNAL + | XFTPErrorType_DUPLICATE_ +) + +XFTPErrorType_Tag = Literal["BLOCK", "SESSION", "HANDSHAKE", "CMD", "AUTH", "BLOCKED", "SIZE", "QUOTA", "DIGEST", "CRYPTO", "NO_FILE", "HAS_FILE", "FILE_IO", "TIMEOUT", "INTERNAL", "DUPLICATE_"] + +class XFTPRcvFile(TypedDict): + rcvFileDescription: "RcvFileDescr" + agentRcvFileId: NotRequired[str] + agentRcvFileDeleted: bool + userApprovedRelays: bool + +class XFTPSndFile(TypedDict): + agentSndFileId: str + privateSndFileDescr: NotRequired[str] + agentSndFileDeleted: bool + cryptoArgs: NotRequired["CryptoFileArgs"] diff --git a/packages/simplex-chat-python/src/simplex_chat/util.py b/packages/simplex-chat-python/src/simplex_chat/util.py new file mode 100644 index 0000000000..158bb72a79 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/util.py @@ -0,0 +1,128 @@ +"""Reusable helpers for working with chat events, types, and message content. + +Mirrors the Node `util.ts` exports — provides the same primitives bot +authors typically reach for: command parsing, sender display strings, +message-content extraction, profile field cleanup, and ChatRef extraction +from a ChatInfo (handy when echoing into a different chat). +""" + +from __future__ import annotations + +import re +from typing import Any + +from .types import T + + +def chat_info_ref(c_info: T.ChatInfo) -> T.ChatRef | None: + """Extract a wire-format `ChatRef` from a `ChatInfo`. + + Returns `None` for non-chat infos (contactRequest, contactConnection) + that can't be the target of `api_send_messages`. For groups, the + `memberSupport` scope is forwarded so messages land in the right + thread; other scopes are dropped (matches Node `util.chatInfoRef`). + """ + t = c_info["type"] + if t == "direct": + return {"chatType": "direct", "chatId": c_info["contact"]["contactId"]} # type: ignore[index] + if t == "group": + ref: T.ChatRef = {"chatType": "group", "chatId": c_info["groupInfo"]["groupId"]} # type: ignore[index] + scope = c_info.get("groupChatScope") # type: ignore[union-attr] + if scope and scope.get("type") == "memberSupport": + member = scope.get("groupMember_") + ms_scope: T.GroupChatScope_memberSupport = {"type": "memberSupport"} + if member is not None: + ms_scope["groupMemberId_"] = member["groupMemberId"] + ref["chatScope"] = ms_scope + return ref + return None + + +def chat_info_name(c_info: T.ChatInfo) -> str: + """Display string for a chat: `@Alice`, `#GroupName`, `private notes`, etc.""" + t = c_info["type"] + if t == "direct": + return f"@{c_info['contact']['profile']['displayName']}" # type: ignore[index] + if t == "group": + scope = c_info.get("groupChatScope") # type: ignore[union-attr] + if scope and scope.get("type") == "memberSupport": + member = scope.get("groupMember_") + scope_name = f" {member['memberProfile']['displayName']}" if member else "" + return f"#{c_info['groupInfo']['groupProfile']['displayName']}(support{scope_name})" # type: ignore[index] + return f"#{c_info['groupInfo']['groupProfile']['displayName']}" # type: ignore[index] + if t == "local": + return "private notes" + if t == "contactRequest": + return f"request from @{c_info['contactRequest']['profile']['displayName']}" # type: ignore[index] + if t == "contactConnection": + alias = c_info["contactConnection"].get("localAlias") # type: ignore[index] + return f"pending connection ({alias})" if alias else "pending connection" + return f"<{t}>" + + +def sender_name(c_info: T.ChatInfo, chat_dir: T.CIDirection) -> str: + """Sender display: chat name plus group sender suffix when applicable.""" + base = chat_info_name(c_info) + if chat_dir["type"] == "groupRcv": + sender = chat_dir["groupMember"]["memberProfile"]["displayName"] # type: ignore[index] + return f"{base} @{sender}" + return base + + +def contact_address_str(link: T.CreatedConnLink) -> str: + """Prefer the short link, fall back to the full link.""" + return link.get("connShortLink") or link["connFullLink"] + + +def from_local_profile(local: T.LocalProfile) -> T.Profile: + """Strip extra LocalProfile fields (profileId, localAlias) and undefined values.""" + p: dict[str, Any] = {} + for key in ( + "displayName", + "fullName", + "shortDescr", + "image", + "contactLink", + "preferences", + "peerType", + ): + v = local.get(key) # type: ignore[misc] + if v is not None: + p[key] = v + return p # type: ignore[return-value] + + +def ci_content_text(chat_item: T.ChatItem) -> str | None: + """Extract the message text from a sent or received message item, if any.""" + content = chat_item["content"] + if content["type"] in ("sndMsgContent", "rcvMsgContent"): + msg = content.get("msgContent", {}) # type: ignore[union-attr] + return msg.get("text") + return None + + +_BOT_COMMAND_RE = re.compile(r"^/([^\s]+)(.*)$") + + +def ci_bot_command(chat_item: T.ChatItem) -> tuple[str, str] | None: + """Parse a `/keyword args...` slash-command from a chat item. + + Returns `(keyword, trimmed_params)` or `None` if the message isn't a + slash command. Mirrors Node `util.ciBotCommand` semantics. + """ + text = ci_content_text(chat_item) + if not text: + return None + text = text.strip() + m = _BOT_COMMAND_RE.match(text) + if not m: + return None + return m.group(1), m.group(2).strip() + + +def reaction_text(reaction: T.ACIReaction) -> str: + """Format an `ACIReaction` as the emoji character or tag string.""" + r = reaction["chatReaction"]["reaction"] # type: ignore[index] + if r["type"] == "emoji": + return r["emoji"] # type: ignore[index] + return r.get("tag", "") # type: ignore[union-attr] diff --git a/packages/simplex-chat-python/tests/test_bot_registration.py b/packages/simplex-chat-python/tests/test_bot_registration.py new file mode 100644 index 0000000000..7401d2ef5d --- /dev/null +++ b/packages/simplex-chat-python/tests/test_bot_registration.py @@ -0,0 +1,357 @@ +import pytest + +from simplex_chat import Bot, BotCommand, BotProfile, Middleware, SqliteDb +from simplex_chat.api import ChatApi + + +def _bot() -> Bot: + return Bot(profile=BotProfile(display_name="x"), db=SqliteDb(file_prefix="/tmp/test")) + + +def test_decorator_registers_message_handler(): + bot = _bot() + + @bot.on_message(content_type="text") + async def h(msg): + pass + + assert len(bot._message_handlers) == 1 + + +def test_decorator_registers_command_handler(): + bot = _bot() + + @bot.on_command("ping") + async def h(msg, cmd): + pass + + assert len(bot._command_handlers) == 1 + assert bot._command_handlers[0][0] == ("ping",) + + +def test_decorator_registers_event_handler(): + bot = _bot() + + @bot.on_event("newChatItems") + async def h(evt): + pass + + assert "newChatItems" in bot._event_handlers + assert len(bot._event_handlers["newChatItems"]) == 1 + + +def test_api_property_raises_before_init(): + bot = _bot() + with pytest.raises(RuntimeError, match="not initialized"): + _ = bot.api + + +def test_command_keyword_tuple(): + bot = _bot() + + @bot.on_command(("p", "ping")) + async def h(msg, cmd): + pass + + assert bot._command_handlers[0][0] == ("p", "ping") + + +def test_bot_profile_to_wire_default(): + """use_bot_profile=True (default) sets peerType=bot and disables calls/voice.""" + bot = _bot() + p = bot._bot_profile_to_wire() + assert p["displayName"] == "x" + assert p.get("peerType") == "bot" + prefs = p.get("preferences") or {} + assert prefs.get("calls", {}).get("allow") == "no" + assert prefs.get("voice", {}).get("allow") == "no" + assert prefs.get("files", {}).get("allow") == "no" # allow_files defaults to False + + +def test_bot_profile_to_wire_allow_files(): + bot = Bot( + profile=BotProfile(display_name="x"), + db=SqliteDb(file_prefix="/tmp/test"), + allow_files=True, + ) + prefs = bot._bot_profile_to_wire().get("preferences") or {} + assert prefs.get("files", {}).get("allow") == "yes" + + +def test_bot_profile_to_wire_with_commands(): + bot = Bot( + profile=BotProfile(display_name="x"), + db=SqliteDb(file_prefix="/tmp/test"), + commands=[BotCommand(keyword="ping", label="Ping bot"), BotCommand("help", "Show help")], + ) + cmds = bot._bot_profile_to_wire().get("preferences", {}).get("commands") or [] + assert len(cmds) == 2 + assert cmds[0] == {"type": "command", "keyword": "ping", "label": "Ping bot"} + assert cmds[1] == {"type": "command", "keyword": "help", "label": "Show help"} + + +def test_bot_profile_to_wire_no_bot_profile(): + bot = Bot( + profile=BotProfile(display_name="x"), + db=SqliteDb(file_prefix="/tmp/test"), + use_bot_profile=False, + ) + p = bot._bot_profile_to_wire() + assert "peerType" not in p + assert "preferences" not in p + + +def test_commands_without_bot_profile_raises(): + bot = Bot( + profile=BotProfile(display_name="x"), + db=SqliteDb(file_prefix="/tmp/test"), + use_bot_profile=False, + commands=[BotCommand("ping", "Ping bot")], + ) + with pytest.raises(ValueError, match="use_bot_profile=False"): + bot._bot_profile_to_wire() + + +def test_dispatch_message_first_match_wins(): + """Two matching message handlers — only the first registered fires.""" + import asyncio + import re + + bot = _bot() + calls: list[str] = [] + + @bot.on_message(content_type="text", text=re.compile(r"^\d+$")) + async def number(_msg): + calls.append("number") + + @bot.on_message(content_type="text") + async def fallback(_msg): + calls.append("fallback") + + class M: + pass + + m = M() + m.content = {"type": "text", "text": "42"} + m.chat_item = { + "chatItem": { + "content": {"type": "rcvMsgContent", "msgContent": {"type": "text", "text": "42"}} + }, + "chatInfo": {"type": "direct"}, + } + m.text = "42" + + asyncio.run(bot._dispatch_message(m)) # type: ignore[arg-type] + assert calls == ["number"], f"expected only 'number' for '42', got {calls}" + + +def test_dispatch_message_falls_to_second_when_first_doesnt_match(): + """If the first handler's filter doesn't match, the second one fires.""" + import asyncio + import re + + bot = _bot() + calls: list[str] = [] + + @bot.on_message(content_type="text", text=re.compile(r"^\d+$")) + async def number(_msg): + calls.append("number") + + @bot.on_message(content_type="text") + async def fallback(_msg): + calls.append("fallback") + + class M: + pass + + m = M() + m.content = {"type": "text", "text": "hello"} + m.chat_item = { + "chatItem": { + "content": {"type": "rcvMsgContent", "msgContent": {"type": "text", "text": "hello"}} + }, + "chatInfo": {"type": "direct"}, + } + m.text = "hello" + + asyncio.run(bot._dispatch_message(m)) # type: ignore[arg-type] + assert calls == ["fallback"], f"expected 'fallback' for 'hello', got {calls}" + + +def test_register_log_handlers_idempotent(): + """Calling _register_log_handlers twice doesn't duplicate handlers.""" + bot = Bot( + profile=BotProfile(display_name="x"), + db=SqliteDb(file_prefix="/tmp/test"), + log_contacts=True, + log_network=True, + ) + bot._register_log_handlers() + counts1 = {tag: len(hs) for tag, hs in bot._event_handlers.items()} + bot._register_log_handlers() + counts2 = {tag: len(hs) for tag, hs in bot._event_handlers.items()} + assert counts1 == counts2, f"handler count changed across calls: {counts1} -> {counts2}" + + +def test_default_error_handlers_always_registered(): + """messageError/chatError/chatErrors get default loggers regardless of opts.""" + bot = Bot( + profile=BotProfile(display_name="x"), + db=SqliteDb(file_prefix="/tmp/test"), + log_contacts=False, + log_network=False, + ) + bot._register_log_handlers() + assert "messageError" in bot._event_handlers + assert "chatError" in bot._event_handlers + assert "chatErrors" in bot._event_handlers + + +def test_dispatch_command_suppresses_matching_message_handlers(): + """A `/help` message routed to a command handler must NOT also fire the + generic on_message text handler.""" + import asyncio + + bot = _bot() + calls: list[str] = [] + + @bot.on_message(content_type="text") + async def fallback(_msg): + calls.append("message") + + @bot.on_command("help") + async def help_cmd(_msg, _cmd): + calls.append("command") + + # Build a minimal Message-shaped object (handlers only inspect chat_item / text). + class M: + pass + + m = M() + m.content = {"type": "text", "text": "/help"} + m.chat_item = { + "chatItem": { + "content": { + "type": "rcvMsgContent", + "msgContent": {"type": "text", "text": "/help"}, + } + }, + "chatInfo": {"type": "direct"}, + } + m.text = "/help" + + asyncio.run(bot._dispatch_message(m)) # type: ignore[arg-type] + assert calls == ["command"], f"expected only 'command' to fire for /help, got {calls}" + + +def test_dispatch_unknown_command_falls_through_to_message_handlers(): + """A `/unknown` slash-command with no handler should still fire on_message.""" + import asyncio + + bot = _bot() + calls: list[str] = [] + + @bot.on_message(content_type="text") + async def fallback(_msg): + calls.append("message") + + @bot.on_command("help") + async def help_cmd(_msg, _cmd): + calls.append("command") + + class M: + pass + + m = M() + m.content = {"type": "text", "text": "/unknown"} + m.chat_item = { + "chatItem": { + "content": { + "type": "rcvMsgContent", + "msgContent": {"type": "text", "text": "/unknown"}, + } + }, + "chatInfo": {"type": "direct"}, + } + m.text = "/unknown" + + asyncio.run(bot._dispatch_message(m)) # type: ignore[arg-type] + assert calls == ["message"], f"expected message fallback to fire for /unknown, got {calls}" + + +def test_chat_api_status_properties(): + """`initialized` and `started` reflect lifecycle state without invoking the FFI.""" + api = ChatApi(ctrl=12345) + assert api.initialized is True + assert api.started is False + assert api.ctrl == 12345 + # Simulate close: ctrl wiped, both properties false. + api._ctrl = None + api._started = False + assert api.initialized is False + assert api.started is False + with pytest.raises(RuntimeError, match="not initialized"): + _ = api.ctrl + + +def test_log_contacts_registers_handlers(): + bot = Bot( + profile=BotProfile(display_name="x"), + db=SqliteDb(file_prefix="/tmp/test"), + log_contacts=True, + log_network=False, + ) + bot._register_log_handlers() + assert "contactConnected" in bot._event_handlers + assert "contactDeletedByContact" in bot._event_handlers + assert "hostConnected" not in bot._event_handlers + + +def test_log_network_registers_handlers(): + bot = Bot( + profile=BotProfile(display_name="x"), + db=SqliteDb(file_prefix="/tmp/test"), + log_contacts=False, + log_network=True, + ) + bot._register_log_handlers() + assert "hostConnected" in bot._event_handlers + assert "hostDisconnected" in bot._event_handlers + assert "subscriptionStatus" in bot._event_handlers + assert "contactConnected" not in bot._event_handlers + + +def test_middleware_registration_and_invocation_order(): + """Middleware registered first wraps middleware registered later (outer first).""" + bot = _bot() + calls: list[str] = [] + + class Outer(Middleware): + async def __call__(self, handler, message, data): + calls.append("outer-before") + await handler(message, data) + calls.append("outer-after") + + class Inner(Middleware): + async def __call__(self, handler, message, data): + calls.append("inner-before") + await handler(message, data) + calls.append("inner-after") + + bot.use(Outer()) + bot.use(Inner()) + assert len(bot._middleware) == 2 + + async def handler(msg): + calls.append("handler") + + import asyncio + + asyncio.run(bot._invoke_with_middleware(handler, message=object())) # type: ignore[arg-type] + assert calls == [ + "outer-before", + "inner-before", + "handler", + "inner-after", + "outer-after", + ] diff --git a/packages/simplex-chat-python/tests/test_codegen.py b/packages/simplex-chat-python/tests/test_codegen.py new file mode 100644 index 0000000000..509d919cfd --- /dev/null +++ b/packages/simplex-chat-python/tests/test_codegen.py @@ -0,0 +1,41 @@ +"""Sanity checks on auto-generated wire types — catches generator regressions.""" + +import typing + +from simplex_chat.types import CC, CEvt, CR, T + + +def test_types_module_imports(): + """Every generated module imports cleanly with no SyntaxError.""" + assert T is not None and CC is not None and CR is not None and CEvt is not None + + +def test_chat_type_is_literal_enum(): + """ChatType should be a Literal of expected member set.""" + args = typing.get_args(T.ChatType) + assert "direct" in args + assert "group" in args + assert "local" in args + + +def test_known_command_has_cmd_string(): + s = CC.APICreateMyAddress_cmd_string({"userId": 1}) + assert s == "/_address 1" + + +def test_chat_response_tag_alias_present(): + """ChatResponse_Tag union of literals exists.""" + assert hasattr(CR, "ChatResponse_Tag") + + +def test_chat_event_tag_alias_present(): + """ChatEvent_Tag exists; covers the on_event Literal annotation.""" + assert hasattr(CEvt, "ChatEvent_Tag") + args = typing.get_args(CEvt.ChatEvent_Tag) + assert "newChatItems" in args + + +def test_chat_ref_cmd_string_direct(): + """Sanity check the codegen fix for ChatRef-bearing commands.""" + assert T.ChatRef_cmd_string({"chatType": "direct", "chatId": 7}) == "@7" + assert T.ChatRef_cmd_string({"chatType": "group", "chatId": 42}) == "#42" diff --git a/packages/simplex-chat-python/tests/test_filters.py b/packages/simplex-chat-python/tests/test_filters.py new file mode 100644 index 0000000000..08fb66ed92 --- /dev/null +++ b/packages/simplex-chat-python/tests/test_filters.py @@ -0,0 +1,83 @@ +import re + +from simplex_chat.filters import compile_message_filter + + +def _msg(content_type="text", text=None, chat_type="direct", group_id=None): + """Build a minimal mock Message-like object for filter testing.""" + + class M: + pass + + m = M() + m.content = {"type": content_type, "text": text} if text is not None else {"type": content_type} + m.chat_item = { + "chatInfo": { + "type": chat_type, + **({"groupInfo": {"groupId": group_id}} if chat_type == "group" else {}), + } + } + return m + + +def test_no_filters_matches_all(): + f = compile_message_filter({}) + assert f(_msg(content_type="text")) + assert f(_msg(content_type="image")) + + +def test_content_type_singular(): + f = compile_message_filter({"content_type": "text"}) + assert f(_msg(content_type="text")) + assert not f(_msg(content_type="image")) + + +def test_content_type_tuple_or(): + f = compile_message_filter({"content_type": ("text", "image")}) + assert f(_msg(content_type="text")) + assert f(_msg(content_type="image")) + assert not f(_msg(content_type="voice")) + + +def test_text_exact(): + f = compile_message_filter({"text": "hello"}) + assert f(_msg(text="hello")) + assert not f(_msg(text="world")) + + +def test_text_regex(): + f = compile_message_filter({"text": re.compile(r"^\d+$")}) + assert f(_msg(text="123")) + assert not f(_msg(text="abc")) + + +def test_when_callable(): + f = compile_message_filter({"when": lambda m: m.content["type"] == "voice"}) + assert f(_msg(content_type="voice")) + assert not f(_msg(content_type="text")) + + +def test_combined_and(): + f = compile_message_filter({"content_type": "text", "text": re.compile(r"\d")}) + assert f(_msg(content_type="text", text="abc123")) + assert not f(_msg(content_type="text", text="abc")) + assert not f(_msg(content_type="image")) + + +def test_chat_type_filter(): + f = compile_message_filter({"chat_type": "group"}) + assert f(_msg(chat_type="group", group_id=1)) + assert not f(_msg(chat_type="direct")) + + +def test_group_id_filter(): + f = compile_message_filter({"group_id": 42}) + assert f(_msg(chat_type="group", group_id=42)) + assert not f(_msg(chat_type="group", group_id=99)) + assert not f(_msg(chat_type="direct")) + + +def test_group_id_tuple_or(): + f = compile_message_filter({"group_id": (1, 2, 3)}) + assert f(_msg(chat_type="group", group_id=2)) + assert not f(_msg(chat_type="group", group_id=99)) diff --git a/packages/simplex-chat-python/tests/test_native_cache.py b/packages/simplex-chat-python/tests/test_native_cache.py new file mode 100644 index 0000000000..bd3bc58da8 --- /dev/null +++ b/packages/simplex-chat-python/tests/test_native_cache.py @@ -0,0 +1,92 @@ +import zipfile +from pathlib import Path + +import pytest + +from simplex_chat._native import _cache_root, _resolve_libs_dir, _download + + +def test_cache_root_linux(tmp_path, monkeypatch): + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path)) + monkeypatch.setattr("sys.platform", "linux") + assert _cache_root() == tmp_path / "simplex-chat" + + +def test_cache_root_macos(tmp_path, monkeypatch): + monkeypatch.setattr("sys.platform", "darwin") + monkeypatch.setattr("pathlib.Path.home", lambda: tmp_path) + assert _cache_root() == tmp_path / "Library" / "Caches" / "simplex-chat" + + +def test_override_via_env(tmp_path, monkeypatch): + # _resolve_libs_dir intentionally does not validate the override directory — + # it returns it verbatim; the eventual ctypes.CDLL call surfaces any mistake. + monkeypatch.setenv("SIMPLEX_LIBS_DIR", str(tmp_path)) + monkeypatch.setattr("sys.platform", "linux") + assert _resolve_libs_dir("sqlite") == tmp_path + + +def test_resolve_downloads_when_missing(tmp_path, monkeypatch): + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path)) + monkeypatch.setattr("sys.platform", "linux") + monkeypatch.setattr("simplex_chat._native._platform_tag", lambda: "linux-x86_64") + + called = {} + + def fake_download(target_root: Path, backend: str) -> None: + called["target"] = target_root + called["backend"] = backend + target_root.mkdir(parents=True, exist_ok=True) + (target_root / "libsimplex.so").touch() + + monkeypatch.setattr("simplex_chat._native._download", fake_download) + libs_dir = _resolve_libs_dir("sqlite") + assert libs_dir == tmp_path / "simplex-chat" / "v6.5.1" / "sqlite" + assert called["backend"] == "sqlite" + assert (libs_dir / "libsimplex.so").exists() + + +def test_resolve_uses_cache_on_second_call(tmp_path, monkeypatch): + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path)) + monkeypatch.setattr("sys.platform", "linux") + cached = tmp_path / "simplex-chat" / "v6.5.1" / "sqlite" + cached.mkdir(parents=True) + (cached / "libsimplex.so").touch() + # Should NOT call _download — use the cached file. + monkeypatch.setattr( + "simplex_chat._native._download", lambda *a: pytest.fail("download should not be called") + ) + assert _resolve_libs_dir("sqlite") == cached + + +def test_postgres_on_macos_rejected(monkeypatch): + monkeypatch.setattr("sys.platform", "darwin") + monkeypatch.setattr("simplex_chat._native._platform_tag", lambda: "macos-aarch64") + with pytest.raises(RuntimeError, match="postgres.*linux-x86_64"): + _resolve_libs_dir("postgres") + + +def test_atomic_install(tmp_path, monkeypatch): + """Build a fake libs zip, mock _stream_to_file, verify extraction + atomic rename.""" + # Build zip: libs/libsimplex.so + libs/libHS-stub.so + src = tmp_path / "src" / "libs" + src.mkdir(parents=True) + (src / "libsimplex.so").write_text("fake-so") + (src / "libHS-stub.so").write_text("fake-hs") + zip_path = tmp_path / "fake-libs.zip" + with zipfile.ZipFile(zip_path, "w") as zf: + for f in src.iterdir(): + zf.write(f, f"libs/{f.name}") + + def fake_stream(url, dest, *, timeout=60.0): + import shutil + + shutil.copy(zip_path, dest) + + monkeypatch.setattr("simplex_chat._native._stream_to_file", fake_stream) + monkeypatch.setattr("simplex_chat._native._platform_tag", lambda: "linux-x86_64") + + target = tmp_path / "out" + _download(target, "sqlite") + assert (target / "libsimplex.so").read_text() == "fake-so" + assert (target / "libHS-stub.so").read_text() == "fake-hs" diff --git a/packages/simplex-chat-python/tests/test_native_url.py b/packages/simplex-chat-python/tests/test_native_url.py new file mode 100644 index 0000000000..7b53fa3ff7 --- /dev/null +++ b/packages/simplex-chat-python/tests/test_native_url.py @@ -0,0 +1,55 @@ +from unittest.mock import patch +import pytest +from simplex_chat._native import _platform_tag, _libs_url, _libname + + +@patch("sys.platform", "linux") +@patch("platform.machine", return_value="x86_64") +def test_platform_linux_x64(_): + assert _platform_tag() == "linux-x86_64" + + +@patch("sys.platform", "darwin") +@patch("platform.machine", return_value="arm64") +def test_platform_macos_arm64(_): + assert _platform_tag() == "macos-aarch64" + + +@patch("sys.platform", "win32") +@patch("platform.machine", return_value="AMD64") +def test_platform_windows_x64(_): + assert _platform_tag() == "windows-x86_64" + + +@patch("sys.platform", "freebsd") +@patch("platform.machine", return_value="x86_64") +def test_platform_unsupported(_): + with pytest.raises(RuntimeError, match="Unsupported"): + _platform_tag() + + +def test_libname_per_platform(): + with patch("sys.platform", "linux"): + assert _libname() == "libsimplex.so" + with patch("sys.platform", "darwin"): + assert _libname() == "libsimplex.dylib" + with patch("sys.platform", "win32"): + assert _libname() == "libsimplex.dll" + + +@patch("simplex_chat._native._platform_tag", return_value="linux-x86_64") +def test_url_sqlite(_): + assert ( + _libs_url("sqlite") + == "https://github.com/simplex-chat/simplex-chat-libs/releases/download/" + "v6.5.1/simplex-chat-libs-linux-x86_64.zip" + ) + + +@patch("simplex_chat._native._platform_tag", return_value="linux-x86_64") +def test_url_postgres(_): + assert ( + _libs_url("postgres") + == "https://github.com/simplex-chat/simplex-chat-libs/releases/download/" + "v6.5.1/simplex-chat-libs-linux-x86_64-postgres.zip" + ) diff --git a/packages/simplex-chat-python/tests/test_util.py b/packages/simplex-chat-python/tests/test_util.py new file mode 100644 index 0000000000..983b1c2a56 --- /dev/null +++ b/packages/simplex-chat-python/tests/test_util.py @@ -0,0 +1,175 @@ +from simplex_chat import util + + +def test_chat_info_ref_direct(): + ci = {"type": "direct", "contact": {"contactId": 7}} + assert util.chat_info_ref(ci) == {"chatType": "direct", "chatId": 7} + + +def test_chat_info_ref_group(): + ci = {"type": "group", "groupInfo": {"groupId": 42}} + assert util.chat_info_ref(ci) == {"chatType": "group", "chatId": 42} + + +def test_chat_info_ref_group_with_member_support_scope(): + ci = { + "type": "group", + "groupInfo": {"groupId": 42}, + "groupChatScope": {"type": "memberSupport", "groupMember_": {"groupMemberId": 99}}, + } + ref = util.chat_info_ref(ci) + assert ref == { + "chatType": "group", + "chatId": 42, + "chatScope": {"type": "memberSupport", "groupMemberId_": 99}, + } + + +def test_chat_info_ref_group_with_member_support_scope_no_member(): + ci = { + "type": "group", + "groupInfo": {"groupId": 42}, + "groupChatScope": {"type": "memberSupport"}, + } + ref = util.chat_info_ref(ci) + # No groupMember_ → no groupMemberId_ in the wire scope. + assert ref == { + "chatType": "group", + "chatId": 42, + "chatScope": {"type": "memberSupport"}, + } + + +def test_chat_info_ref_returns_none_for_non_targets(): + assert util.chat_info_ref({"type": "contactRequest"}) is None + assert util.chat_info_ref({"type": "contactConnection"}) is None + + +def test_chat_info_name_direct(): + ci = {"type": "direct", "contact": {"profile": {"displayName": "Alice"}}} + assert util.chat_info_name(ci) == "@Alice" + + +def test_chat_info_name_group(): + ci = {"type": "group", "groupInfo": {"groupProfile": {"displayName": "MyGroup"}}} + assert util.chat_info_name(ci) == "#MyGroup" + + +def test_chat_info_name_group_with_member_support(): + ci = { + "type": "group", + "groupInfo": {"groupProfile": {"displayName": "MyGroup"}}, + "groupChatScope": { + "type": "memberSupport", + "groupMember_": {"memberProfile": {"displayName": "Carol"}}, + }, + } + assert util.chat_info_name(ci) == "#MyGroup(support Carol)" + + +def test_chat_info_name_local(): + assert util.chat_info_name({"type": "local"}) == "private notes" + + +def test_chat_info_name_contact_request(): + ci = {"type": "contactRequest", "contactRequest": {"profile": {"displayName": "Eve"}}} + assert util.chat_info_name(ci) == "request from @Eve" + + +def test_chat_info_name_contact_connection(): + assert util.chat_info_name({"type": "contactConnection", "contactConnection": {}}) == ( + "pending connection" + ) + assert ( + util.chat_info_name({"type": "contactConnection", "contactConnection": {"localAlias": "X"}}) + == "pending connection (X)" + ) + + +def test_sender_name_direct_uses_chat_name(): + ci = {"type": "direct", "contact": {"profile": {"displayName": "Alice"}}} + chat_dir = {"type": "directRcv"} + assert util.sender_name(ci, chat_dir) == "@Alice" + + +def test_sender_name_group_appends_member(): + ci = {"type": "group", "groupInfo": {"groupProfile": {"displayName": "MyGroup"}}} + chat_dir = {"type": "groupRcv", "groupMember": {"memberProfile": {"displayName": "Bob"}}} + assert util.sender_name(ci, chat_dir) == "#MyGroup @Bob" + + +def test_contact_address_str_prefers_short(): + assert util.contact_address_str({"connFullLink": "full", "connShortLink": "short"}) == "short" + + +def test_contact_address_str_falls_back_to_full(): + assert util.contact_address_str({"connFullLink": "full"}) == "full" + + +def test_from_local_profile_strips_extras_and_undefined(): + local = { + "displayName": "x", + "fullName": "X Y", + "shortDescr": None, + "image": "data:image/png;base64,...", + "contactLink": None, + "preferences": {}, + "peerType": "bot", + "profileId": 99, # extra LocalProfile field + "localAlias": "alias", # extra LocalProfile field + } + p = util.from_local_profile(local) + assert p == { + "displayName": "x", + "fullName": "X Y", + "image": "data:image/png;base64,...", + "preferences": {}, + "peerType": "bot", + } + + +def test_ci_content_text_rcv(): + ci = {"content": {"type": "rcvMsgContent", "msgContent": {"type": "text", "text": "hello"}}} + assert util.ci_content_text(ci) == "hello" + + +def test_ci_content_text_snd(): + ci = {"content": {"type": "sndMsgContent", "msgContent": {"type": "text", "text": "world"}}} + assert util.ci_content_text(ci) == "world" + + +def test_ci_content_text_other(): + ci = {"content": {"type": "rcvGroupEvent"}} + assert util.ci_content_text(ci) is None + + +def test_ci_bot_command_match(): + ci = {"content": {"type": "rcvMsgContent", "msgContent": {"type": "text", "text": "/ping"}}} + assert util.ci_bot_command(ci) == ("ping", "") + + +def test_ci_bot_command_with_args(): + ci = { + "content": {"type": "rcvMsgContent", "msgContent": {"type": "text", "text": "/echo hi "}} + } + assert util.ci_bot_command(ci) == ("echo", "hi") + + +def test_ci_bot_command_not_a_command(): + ci = {"content": {"type": "rcvMsgContent", "msgContent": {"type": "text", "text": "hello"}}} + assert util.ci_bot_command(ci) is None + + +def test_ci_bot_command_no_text(): + ci = {"content": {"type": "rcvGroupEvent"}} + assert util.ci_bot_command(ci) is None + + +def test_reaction_text_emoji(): + r = {"chatReaction": {"reaction": {"type": "emoji", "emoji": "🎉"}}} + assert util.reaction_text(r) == "🎉" + + +def test_reaction_text_tag(): + r = {"chatReaction": {"reaction": {"type": "unknown", "tag": "thumbs_up"}}} + assert util.reaction_text(r) == "thumbs_up" diff --git a/plans/2026-04-29-member-profile-sending-channels.md b/plans/2026-04-29-member-profile-sending-channels.md new file mode 100644 index 0000000000..2ee36b676e --- /dev/null +++ b/plans/2026-04-29-member-profile-sending-channels.md @@ -0,0 +1,237 @@ +# Plan: Member Profile Sending in Channels + +## Context + +In channels (relayed groups), subscribers don't know profiles of other subscribers. When subscriber A sends a reaction/message that gets forwarded to subscriber B, B creates an "unknown member" record with a synthesized name. This degrades UX — subscribers see "unknown member" instead of real profiles. + +We can't eagerly send all subscriber profiles to all subscribers (doesn't scale to 100K+ channels). We need on-demand, deduplicated profile delivery: the relay tracks which subscribers have received which sender's profile, and prepends profile info when forwarding a message from a sender the recipient doesn't know. + +## Approach: Vector-tracked profile delivery + +### Core idea + +Each member record on the relay stores a `sent_profile_vector BLOB` — a byte vector where position `i` represents the recipient at `index_in_group = i`. Value 0 = profile not sent, non-zero = sent. + +When the relay forwards a batch (possibly from multiple senders): +1. Collect distinct senders in the batch. Load each sender's `sent_profile_vector`. +2. For each cursor-batch of recipients, partition into two groups: + - **Knows all**: recipient's index is marked as sent in every sender's vector → gets bare batch + - **Needs profiles**: recipient's index is unmarked in at least one sender's vector → gets batch with all sender profiles prepended as `XGrpMemNew` elements +3. Update all senders' vectors to mark recipients who were delivered to. + +When a sender updates their profile (relay receives `XInfo`): clear that sender's `sent_profile_vector`, so the updated profile is re-sent on next forwarded message. + +In steady state, most long-standing subscribers have received all active senders' profiles from previous deliveries. The "knows all" group dominates; the "needs profiles" group consists mainly of newcomers and is small. The partition converges quickly to near-zero redundancy. + +### Why this approach + +**Considered alternatives:** +- **Include profile in every FwdSender**: Wastes bandwidth sending profile on every message. +- **Subscriber requests profile from relay**: Adds latency (round-trip) and new request-response protocol complexity. +- **Separate delivery worker** (using commented-out `DWSMemberProfileUpdate` stubs): Harder to guarantee ordering (profile must arrive before message). +- **Bloom filters / epoch-based**: Same storage complexity as vectors, more complex to implement, probabilistic (false positives). + +**Advantages of prepend-to-batch approach:** +- Profile + forwarded message arrive in a single SMP message (no extra 16KB block overhead) +- SMP guarantees in-order processing within a batch +- No protocol changes — `XGrpMemNew` is already handled by subscribers +- No subscriber-side code changes for receiving + +### Design decisions to discuss + +**1. Bit-level vs byte-level vector** + +Byte-per-position is consistent with `member_relations_vector` but uses 8x more space. For 100K members: byte=100KB/sender, bit=12.5KB/sender. With 1000 active senders: byte=100MB, bit=12.5MB. Byte is simpler; bit is more space-efficient. **Recommend: byte-level for consistency, optimize to bit-level later if needed.** + +**2. Multi-sender batch profile strategy** + +Channels batch tasks from multiple senders into one job (`singleSenderGMId_ = Nothing`). Profile tracking requires knowing which senders' profiles each recipient has seen. Three approaches: + +**Option A — Per-sender precise targeting (rejected)**: For a batch with senders {A, B, C}, construct a separate batch variant for each combination of missing profiles: recipients missing only A get `profile(A) + batch`, those missing A and C get `profile(A) + profile(C) + batch`, etc. This produces up to 2^k batch variants for k senders — a combinatorial explosion that is fundamentally at odds with batching efficiency. Constructing nearly per-recipient blobs is worse than not batching at all. **Rejected.** + +**Option B — All-or-nothing profile sidecar (probably preferable)**: Partition recipients into two groups: those who know ALL senders (get bare batch) and those missing ANY sender profile (get all sender profiles prepended). Only 2 batch variants regardless of sender count. Preserves current multi-sender batching — no changes to `getNextDeliveryTasks`. Some recipients may receive profiles they already know, but XGrpMemNew is idempotent (~200-500 bytes per profile), and this redundancy only occurs at the rare intersection of a multi-sender batch AND a partially-informed recipient. In steady state, long-standing subscribers know all active senders, so the "needs profiles" group shrinks to just newcomers. +- Pros: preserves current batching, smaller diff (no `Store/Delivery.hs` changes), 2 variants only, fast convergence to zero-redundancy steady state +- Cons: slight redundancy for partially-informed recipients in multi-sender batches (rare and transient) + +**Option C — Force single-sender jobs**: Add `sender_group_member_id` filter to `getNextDeliveryTasks` for channels, same as fully connected groups. Each delivery job has exactly one sender, so profile sidecar is always one XGrpMemNew. Clean binary partition with zero redundancy. +- Pros: zero redundant profiles, simplest per-job logic +- Cons: changes delivery task query logic, slightly less batching efficiency (separate jobs per sender), though multi-sender batches are rare anyway + +--- + +## Detailed changes + +The code below assumes Option B (all-or-nothing sidecar). Option C would simplify section 4 (always one sender) and add a query change in `Store/Delivery.hs`. + +### 1. Database migration + +New migration file: `M{date}_sent_profile_vector.hs` + +```sql +ALTER TABLE group_members ADD COLUMN sent_profile_vector BLOB; +``` + +**Files:** +- `src/Simplex/Chat/Store/SQLite/Migrations/M{date}_sent_profile_vector.hs` (new) +- `src/Simplex/Chat/Store/SQLite/Migrations.hs` (register migration) +- `src/Simplex/Chat/Store/Postgres/Migrations/M{date}_sent_profile_vector.hs` (new) +- `src/Simplex/Chat/Store/Postgres/Migrations.hs` (register migration) +- `simplex-chat.cabal` (add module) + +### 2. Sent profile vector operations + +New functions in `src/Simplex/Chat/Store/Groups.hs`: + +```haskell +getSentProfileVector :: DB.Connection -> GroupMemberId -> IO ByteString + +-- Expands vector if needed (same expand-on-write pattern as setRelation in Types/MemberRelations.hs) +markProfilesSentToMembers :: DB.Connection -> GroupMemberId -> [Int64] -> IO () + +clearSentProfileVector :: DB.Connection -> GroupMemberId -> IO () +``` + +Pure helpers: +```haskell +isProfileSentTo :: ByteString -> Int64 -> Bool +isProfileSentTo vec idx + | idx < 0 || fromIntegral idx >= B.length vec = False + | otherwise = B.index vec (fromIntegral idx) /= 0 + +markSentPositions :: [Int64] -> ByteString -> ByteString +``` + +### 3. Profile batch element encoding + +New functions in `src/Simplex/Chat/Messages/Batch.hs`: + +```haskell +-- Prepend an element to an existing binary batch body +-- batchBody format: '=' ( )* +-- Increments count and inserts element at front without parsing/re-encoding existing elements +prependBatchElement :: ByteString -> ByteString -> ByteString + +-- Encode XGrpMemNew as a batch-ready element for a given member +-- Constructs ChatMessage with XGrpMemNew (memberToMemberInfo member) Nothing +encodeMemberProfileElement :: VersionRangeChat -> GroupMember -> ByteString +``` + +Check whether `memberInfo` or similar helper already exists for constructing `MemberInfo` from `GroupMember`. + +### 4. Delivery job worker changes + +**File:** `src/Simplex/Chat/Library/Subscriber.hs` — `processDeliveryJob` / `sendBodyToMembers` + +In the channel path (`useRelays' gInfo`, `DJSGroup {}`): + +**Before the cursor loop**, collect distinct senders from delivery tasks and load their profile data: +```haskell +senderProfiles <- forM (nub senderGMIds) $ \senderGMId -> do + sender <- withStore $ \db -> getGroupMemberById db vr user senderGMId + vec <- withStore' $ \db -> getSentProfileVector db senderGMId + pure (senderGMId, sender, vec) + +let profileElements = map (\(_, sender, _) -> encodeMemberProfileElement vr sender) senderProfiles + extBody = foldl' (flip prependBatchElement) body profileElements +``` + +**In the cursor loop**, partition recipients: +```haskell +sendLoop bucketSize cursorGMId_ = do + mems <- withStore' $ \db -> getGroupMembersByCursor ... + unless (null mems) $ do + if null senderProfiles + then deliver body mems + else do + let knowsAll m = all (\(_, _, vec) -> isProfileSentTo vec (indexInGroup' m)) senderProfiles + (hasAllProfiles, needsProfiles) = partition knowsAll mems + unless (null needsProfiles) $ deliver extBody needsProfiles + unless (null hasAllProfiles) $ deliver body hasAllProfiles + forM_ senderProfiles $ \(senderGMId, _, _) -> + withStore' $ \db -> markProfilesSentToMembers db senderGMId + (map indexInGroup' deliveredMems) + ... +``` + +Only mark vector bits for members who were actually delivered to (those with `readyMemberConn`), not all members in the cursor batch — otherwise members without ready connections get marked as "profile sent" without receiving it. + +### 5. Clear vector on profile update + +**File:** `src/Simplex/Chat/Library/Subscriber.hs` — `xInfoMember` + +After `processMemberProfileUpdate`, if the group uses relays and the user is the relay, clear the sender's vector: + +```haskell +xInfoMember gInfo m p' msg brokerTs = do + void $ processMemberProfileUpdate gInfo m p' (Just (msg, brokerTs)) + when (useRelays' gInfo && isRelay (membership gInfo)) $ + withStore' $ \db -> clearSentProfileVector db (groupMemberId' m) + pure $ memberEventDeliveryScope m +``` + +When the vector is cleared and XInfo is forwarded, the delivery prepends XGrpMemNew before the forwarded XInfo. Recipients process both — XGrpMemNew creates/updates the member record, then XInfo updates it again. Slightly redundant but correct and harmless. + +### 6. Set vector bits when relay announces members at join time + +When a new subscriber joins and the relay sends `XGrpMemNew` for owners/existing announced members, set the corresponding bits in those members' `sent_profile_vector` for the new subscriber's index. The exact location needs to be identified during implementation — look for where the relay processes new member joins and sends XGrpMemNew announcements. + +### 7. Update channel tests + +**File:** `tests/ChatTests/Groups.hs` + +Update `testChannels1RelayDeliver` and related tests: +- After cath sends a reaction, dan and eve should no longer see "forwarded a message from an unknown member, creating unknown member record cath" +- Instead, they receive cath's profile via XGrpMemNew (processed silently before the reaction) +- Test assertions for dan and eve should show the reaction with cath's name + +Add new tests: +- Profile update triggers re-announcement (clear vector → re-send on next message) +- New subscriber joining after a sender has been active gets the profile on first forwarded message +- Multiple senders: each sender's profile is independently tracked + +--- + +## Files to modify + +| File | Change | +|------|--------| +| `src/Simplex/Chat/Store/SQLite/Migrations/M{date}_sent_profile_vector.hs` | New migration | +| `src/Simplex/Chat/Store/SQLite/Migrations.hs` | Register migration | +| `src/Simplex/Chat/Store/Postgres/Migrations/M{date}_sent_profile_vector.hs` | New migration | +| `src/Simplex/Chat/Store/Postgres/Migrations.hs` | Register migration | +| `simplex-chat.cabal` | Add migration module | +| `src/Simplex/Chat/Store/Groups.hs` | Vector CRUD operations | +| `src/Simplex/Chat/Messages/Batch.hs` | `prependBatchElement`, `encodeMemberProfileElement` | +| `src/Simplex/Chat/Library/Subscriber.hs` | Delivery job worker profile logic, xInfoMember vector clear | +| `src/Simplex/Chat/Store/Delivery.hs` | Only if Option C chosen (single-sender jobs) | +| `tests/ChatTests/Groups.hs` | Update channel tests | + +## Subscriber-side impact + +**None required for receiving.** The subscriber already handles: +- `XGrpMemNew` from relay → creates member record with full profile +- `XGrpMsgForward` → finds existing member record +- Mixed batch elements (direct + forwarded) processed in order + +The only subscriber-side change is the test expectations. + +## Verification + +1. **Build**: `cabal build --ghc-options=-O0` +2. **Run channel tests**: `cabal test simplex-chat-test --test-options='-m "channels"'` +3. **Verification scenarios**: + - New subscriber sends reaction → other subscribers receive profile + reaction (no "unknown member") + - Subscriber updates profile → next message re-sends updated profile + - New subscriber joins after sender was active → first forwarded message from that sender includes profile + +## Known considerations + +1. **Vector expansion**: A member with `index_in_group = 100000` causes vector expansion to 100KB. `markSentPositions` handles this via the same expand-on-write pattern as `setRelation` in `Types/MemberRelations.hs`. + +2. **Delivery filtering**: Only mark vector bits for members who were actually delivered to (those with `readyMemberConn`). The `deliver` function filters for ready connections — if `markProfilesSentToMembers` marked all cursor members including those without connections, disconnected members would never receive the profile on reconnection. + +3. **Scope**: Profile tracking applies only to `DJSGroup` scope. Support scope (`DJSMemberSupport`) delivers to moderators who already know members — no profile tracking needed there. + +4. **Sender exclusion**: `getGroupMembersByCursor` already filters out the sender via `singleSenderGMId_` in the WHERE clause, so no self-profile issue arises. + +5. **Race: vector clear vs delivery**: If profile update and message delivery overlap, the delivery sees an empty vector and sends the profile. This is correct — the delivery uses the current (updated) profile, so recipients get the new profile. diff --git a/plans/2026-04-29-relay-management.md b/plans/2026-04-29-relay-management.md new file mode 100644 index 0000000000..a44a9f0b2c --- /dev/null +++ b/plans/2026-04-29-relay-management.md @@ -0,0 +1,415 @@ +# Relay Management Improvements + +## Problem Statement + +Channel owners currently can only add relays during channel creation (`APINewPublicGroup`). Once a channel is created, there is no way to: +1. Add a new relay to an existing channel. +2. Remove a relay from an existing channel. +3. Have relays and subscribers automatically detect and synchronize relay state changes. + +Several TODO markers in the codebase (`[relays]`) confirm these are planned but unimplemented. The `runRelayGroupLinkChecks` function (Commands.hs:4729) is a stub. The LINK event handler (Subscriber.hs:1308-1309) has a TODO for relay deletion detection. No `APIAddGroupRelays` command exists. + +## Solution Summary + +### Add relay to existing channel + +New `APIAddGroupRelays` command that reuses the existing `addRelays` function (Commands.hs:3887, in `processChatCommand`'s `where` block). The `addRelays` flow is asynchronous: after the invitation is sent (RSNew→RSInvited), the relay responds with its relay link (→RSAccepted), and the CON event handler (Subscriber.hs:861-864) calls `setGroupLinkDataAsync` to publish the new relay link. The LINK callback then promotes RSAccepted→RSActive. + +### Remove relay from existing channel + +Use the existing `APIRemoveMembers` command, extended with relay-specific handling. In channels, `APIRemoveMembers` already sends `XGrpMemDel` to all relay members via `sendGroupMessages` (the `memberSendAction` routing ensures the message goes to relays only, which forward it to subscribers). This is the correct approach: broadcasting the removal through *other* relays ensures all subscribers learn about the removal even if the removed relay is malicious and refuses to notify them. Link data synchronization serves as a backup mechanism. + +The extension needed: when removing a relay member, also update its `GroupRelay.relay_status` to `RSInactive`. Currently `APIRemoveMembers` updates `GroupMember` status (via `deleteOrUpdateMemberRecordIO`) and calls `updatePublicGroupData` (which updates link data), but does not touch the `GroupRelay` record. + +### State synchronization + +Three actors synchronize via the group link data on the SMP server: + +- **Owner**: publishes the authoritative relay list in link data via `setGroupLinkData`. The `getConnectedGroupRelays` function (which filters by `member_status = GSMemConnected AND relay_status IN (RSAccepted, RSActive)`) determines which relays appear in link data. +- **Relay**: `runRelayGroupLinkChecks` (implement the stub) periodically fetches group link data to confirm its own link is present. If absent → self-cleanup. +- **Subscriber**: when opening a channel, the UI already calls `APIGetUpdatedGroupLinkData` (Commands.hs:1777) which fetches link data from the SMP server. This handler will be extended to also synchronize relay state: connect to newly discovered relays, disconnect from removed relays. + +--- + +## Detailed Technical Design + +### 1. Relay Deactivation on Member Removal + +**File**: `src/Simplex/Chat/Library/Internal.hs` (lines 1804-1821) + +Two member-removal primitives exist: `deleteOrUpdateMemberRecordIO` (IO, line 1808) and `updateMemberRecordDeleted` (CM, line 1816). Both run in DB context. Relay deactivation belongs inside these functions so it runs in the same transaction as the member status change. + +**New helper** in Internal.hs: + +```haskell +deactivateRelayIfNeeded :: DB.Connection -> GroupMember -> IO () +deactivateRelayIfNeeded db m = + when (isRelay m) $ do + relay_ <- runExceptT $ getGroupRelayByGMId db (groupMemberId' m) + forM_ relay_ $ \relay -> void $ updateRelayStatus db relay RSInactive +``` + +**Extend `deleteOrUpdateMemberRecordIO`** (line 1808): + +```haskell +deleteOrUpdateMemberRecordIO db user@User {userId} gInfo m = do + (gInfo', m') <- deleteSupportChatIfExists db user gInfo m + checkGroupMemberHasItems db user m' >>= \case + Just _ -> updateGroupMemberStatus db userId m' GSMemRemoved + Nothing -> deleteGroupMember db user m' + deactivateRelayIfNeeded db m + pure gInfo' +``` + +**Extend `updateMemberRecordDeleted`** (line 1816): + +```haskell +updateMemberRecordDeleted user@User {userId} gInfo m newStatus = + withStore' $ \db -> do + (gInfo', m') <- deleteSupportChatIfExists db user gInfo m + updateGroupMemberStatus db userId m' newStatus + deactivateRelayIfNeeded db m + pure gInfo' +``` + +This covers all four call sites: +- `delMember` in `deleteMemsSend` (Commands.hs:2896) — owner removing relay via `APIRemoveMembers` +- `deleteOrUpdateMemberRecord` in `xGrpMemDel` (Subscriber.hs:3123) — receiving relay deletion notification +- `updateMemberRecordDeleted` in `xGrpMemDel` (Subscriber.hs:3121) — relay deletion with forwarding +- `updateMemberRecordDeleted` in `xGrpLeave` (Subscriber.hs:3168) — relay leaves voluntarily + +For subscribers who have no `GroupRelay` records, `getGroupRelayByGMId` returns `Left`, `forM_` on `Left` is a no-op — safe. + +**Cleanup**: remove the now-redundant separate relay deactivation in `xGrpLeave` (Subscriber.hs:3169-3172): + +```haskell +-- Before: +gInfo' <- updateMemberRecordDeleted user gInfo m GSMemLeft +when (isRelay m) $ + withStore' $ \db -> do + relay_ <- runExceptT $ getGroupRelayByGMId db (groupMemberId' m) + forM_ relay_ $ \relay -> void $ updateRelayStatus db relay RSInactive +gInfo'' <- updatePublicGroupData user gInfo' + +-- After: +gInfo' <- updateMemberRecordDeleted user gInfo m GSMemLeft +gInfo'' <- updatePublicGroupData user gInfo' +``` + +**`APIRemoveMembers` requires no changes** — `delMember` (line 2891) already calls `deleteOrUpdateMemberRecordIO` which now handles relay deactivation internally. The `getConnectedGroupRelays` query filters by both `member_status = GSMemConnected` and `relay_status IN (RSAccepted, RSActive)`, so the removed relay is excluded from link data when `updatePublicGroupData` runs (line 2828-2829). + +**iOS UI**: The remove button is currently hidden on the relay member info page by an explicit guard in `adminDestructiveSection` (GroupMemberInfoView.swift:646: `mem.memberRole != .relay`). Changes needed: + +1. **Remove the relay guard** — change the condition to allow relay members to be removed. The `canBeRemoved()` permission check (ChatTypes.swift:2868) already validates that the user has sufficient role. + +2. **Relay-specific button text** — the `removeMemberButton` (line 708) currently shows `"Remove subscriber"` for channels (`groupInfo.useRelays`). Add a relay branch: when `mem.memberRole == .relay`, show `"Remove relay"` instead. + +3. **Relay-specific alert text** — `showRemoveMemberAlert` (GroupChatInfoView.swift:926) currently shows `"Remove subscriber?"` / `"Subscriber will be removed from channel"` for channels. Add a relay branch: `"Remove relay?"` / `"Relay will be removed from channel"`. + +4. **Last active relay warning** — when removing a relay, check if it's the last active relay (count relay members with `memberCurrent` status in `chatModel.groupMembers`). If so, show a warning: `"This is the last active relay. Removing it will prevent message delivery to subscribers."` The count is available client-side from `chatModel.groupMembers.filter { $0.wrapped.memberRole == .relay && $0.wrapped.memberCurrent }`. + +No new API command needed for removal — the existing `apiRemoveMembers` is used. + +### 2. New `APIAddGroupRelays` Command + +**File**: `src/Simplex/Chat/Controller.hs` + +```haskell +-- New command +| APIAddGroupRelays GroupId (NonEmpty Int64) -- group ID, chat_relay_ids + +-- New responses +| CRGroupRelaysAdded { user :: User, groupInfo :: GroupInfo, groupLink :: GroupLink, groupRelays :: [GroupRelay] } +| CRGroupRelaysAddFailed { user :: User, addRelayResults :: [AddRelayResult] } +``` + +**File**: `src/Simplex/Chat/Library/Commands.hs` + +New handler: + +``` +APIAddGroupRelays groupId relayIds -> withUser $ \user -> withGroupLock "addGroupRelays" groupId $ do + -- 1. Validate: user is owner, group uses relays + gInfo <- withFastStore $ \db -> getGroupInfo db vr user groupId + assertUserGroupRole gInfo GROwner + unless (useRelays' gInfo) $ throwCmdError "group does not use relays" + + -- 2. Get group link (needed for relay invitation) + gLink <- withFastStore $ \db -> getGroupLink db user gInfo + sLnk <- case connShortLink' (connLinkContact gLink) of + Just sl -> pure sl + Nothing -> throwChatError $ CEException "group link has no short link" + + -- 3. Load requested relay configs + relays <- withFastStore $ \db -> mapM (getChatRelayById db user) (L.toList relayIds) + + -- 4. Reuse existing addRelays function (Commands.hs:3887) + results <- addRelays user gInfo sLnk relays + + -- 5. Check results + case partitionEithers (map snd results) of + ([], _) -> do + -- Relay connection is asynchronous: invitation sent (RSNew→RSInvited). + -- When relay responds (RSAccepted) and connects (CON at Subscriber.hs:861-864), + -- setGroupLinkDataAsync is called automatically to add the relay link. + -- The LINK callback then promotes RSAccepted→RSActive. + relays' <- withFastStore $ \db -> liftIO $ getGroupRelays db gInfo + pure $ CRGroupRelaysAdded user gInfo gLink relays' + _ -> do + let toRelayResult (r, Left e) = AddRelayResult r (Just e) + toRelayResult (r, Right _) = AddRelayResult r Nothing + pure $ CRGroupRelaysAddFailed user (map toRelayResult results) +``` + +Key points: +- Uses `withGroupLock` to prevent concurrent relay modifications. +- Reuses `addRelays` unchanged — it handles the full invitation flow (create relay member, create GroupRelay record, send `XGrpRelayInv`, update status RSNew→RSInvited). +- No synchronous `setGroupLinkData` call needed: the CON event handler calls `setGroupLinkDataAsync` when the relay connects. + +### 3. Extend `APIGetUpdatedGroupLinkData` for Subscriber Relay Sync + +**File**: `src/Simplex/Chat/Library/Commands.hs` (lines 1777-1787) + +Currently this handler fetches link data from the SMP server and updates group profile and member count. It is called by the iOS UI when a non-owner subscriber opens a channel (ChatView.swift:750). The `ConnLinkData` it receives already contains the relay list in `UserContactData.relays`. + +Extend the handler to also synchronize relay state: + +``` +APIGetUpdatedGroupLinkData groupId -> withUser $ \user -> do + gInfo@GroupInfo {groupProfile = p} <- withFastStore $ \db -> getGroupInfo db vr user groupId + case p of + GroupProfile {publicGroup = Just PublicGroupProfile {groupLink = sLnk}} | useRelays' gInfo -> do + (_, cData@(ContactLinkData _ UserContactData {relays = currentRelayLinks})) <- + getShortLinkConnReq nm user sLnk + groupSLinkData_ <- liftIO $ decodeLinkUserData cData + gInfo' <- case groupSLinkData_ of + Just sLinkData -> fst <$> updateGroupFromLinkData user gInfo sLinkData + _ -> pure gInfo + -- Sync relay state for non-owner subscribers + when (memberRole' (membership gInfo) /= GROwner) $ + syncSubscriberRelays nm user gInfo' currentRelayLinks + pure $ CRGroupInfo user gInfo' + _ -> throwCmdError "group link data not available" +``` + +**Parameterize `connectToRelay`** — move from `APIConnectPreparedGroup`'s `where` block to `processChatCommand`'s `where` block so both `APIConnectPreparedGroup` and subscriber sync can use it. The captured closure variables become explicit parameters or are derived internally: + +``` +-- In processChatCommand's where block (for connectViaContact access). +-- connectViaContact ignores incognito param for relay groups (Commands.hs:3545-3546), +-- using incognitoMembershipProfile gInfo instead. +connectToRelay :: User -> GroupInfo -> ShortLinkContact -> CM (ShortLinkContact, GroupMember, Either ChatError ()) +connectToRelay user gInfo relayLink = do + vr <- chatVersionRange + gVar <- asks random + relayMember <- withFastStore $ \db -> getCreateRelayForMember db vr gVar user gInfo relayLink + r <- tryAllErrors $ do + (fd@FixedLinkData {rootKey = relayKey, linkEntityId}, cData) <- + getShortLinkConnReq nm user relayLink + relayLinkData_ <- liftIO $ decodeLinkUserData cData + case (relayLinkData_, linkEntityId) of + (Just RelayShortLinkData {relayProfile = p}, Just entityId) -> + withFastStore $ \db -> updateRelayMemberData db user relayMember (MemberId entityId) (MemberKey relayKey) p + _ -> throwChatError $ CEException "relay link: no relay link data or entity id" + let cReq = linkConnReq fd + relayLinkToConnect = CCLink cReq (Just relayLink) + void $ connectViaContact user (Just $ PCEGroup gInfo relayMember) False relayLinkToConnect Nothing Nothing + relayMember' <- withFastStore $ \db -> getGroupMember db vr user (groupId' gInfo) (groupMemberId' relayMember) + pure (relayLink, relayMember', r) +``` + +`getCreateRelayForMember` stays outside `tryAllErrors` — the member must be available for re-read even on failure (for `RelayConnectionResult` reporting). `APIConnectPreparedGroup` calls `mapConcurrently (connectToRelay user gInfo') relays` as before. + +**New function** `syncSubscriberRelays` in `processChatCommand`'s scope (reuses `connectToRelay`): + +``` +syncSubscriberRelays :: NetworkRequestMode -> User -> GroupInfo -> [ShortLinkContact] -> CM () +syncSubscriberRelays nm user gInfo currentRelayLinks = tryAllErrors $ do + vr <- chatVersionRange + -- Get local relay members (all members with GRRelay role, regardless of status) + localRelayMembers <- withFastStore' $ \db -> getGroupRelayMembers db vr user gInfo + -- GroupMember.relayLink :: Maybe ShortLinkContact (Types.hs:1041) + -- Set by getCreateRelayForMember (Store/Groups.hs:1392) when subscriber connects to a relay. + let activeRelayMembers = filter memberCurrent localRelayMembers + localRelayLinks = mapMaybe relayLink activeRelayMembers + + -- Discover new relays (in link data but not among active local relay members) + let newRelayLinks = filter (`notElem` localRelayLinks) currentRelayLinks + forM_ newRelayLinks $ \rlnk -> tryAllErrors $ + void $ connectToRelay user gInfo rlnk + + -- Discover removed relays (active local relay member whose link is absent from link data) + forM_ activeRelayMembers $ \m -> + case relayLink m of + Just rlnk | rlnk `notElem` currentRelayLinks -> + tryAllErrors $ do + deleteMemberConnection m + void $ updateMemberRecordDeleted user gInfo m GSMemRemoved + _ -> pure () +``` + +**Note on `getCreateRelayForMember` idempotency**: This function queries `WHERE m.relay_link = ?` without filtering by member status (Store/Groups.hs:1379). If a relay was previously removed (GSMemRemoved) and is later re-added by the owner, `getCreateRelayForMember` will return the old removed member. During implementation, verify whether the member status needs to be reset before reconnecting, or whether `connectViaContact` handles this correctly. + +### 4. LINK Event Handler — Detect Relay Removal (Owner) + +**File**: `src/Simplex/Chat/Library/Subscriber.hs` (lines 1308-1317) + +Replace the TODO with relay removal detection. The LINK callback fires when this owner updates link data (via `setGroupLinkData` / `setConnShortLink`). Currently multi-owner channels are not supported, so this only fires after the same owner's own actions (add/remove relay, profile update). When multi-owner support is added, another owner's link data update on the SMP server would need a separate mechanism (e.g., periodic link data fetch or subscription) for this owner to learn about it — the LINK callback only fires in response to this client's own `setConnShortLink` calls. + +```haskell +updateRelay :: DB.Connection -> GroupRelay -> ([GroupRelay], Bool) -> IO ([GroupRelay], Bool) +updateRelay db relay@GroupRelay {relayLink, relayStatus} (acc, changed) = + case relayLink of + Just rLink + | rLink `elem` relayLinks && relayStatus == RSAccepted -> do + -- Relay link present in link data, promote to active + relay' <- updateRelayStatus db relay RSActive + pure (relay' : acc, True) + | rLink `elem` relayLinks -> pure (relay : acc, changed) + | relayStatus `elem` [RSAccepted, RSActive, RSInactive] -> do + -- Relay link ABSENT from link data — set to inactive. + -- TODO [multi-owner] When multi-owner channels are supported, another owner removing + -- a relay updates link data on the SMP server, but this owner won't receive a LINK + -- callback for it (LINK only fires in response to own setConnShortLink calls). + -- A separate mechanism will be needed for cross-owner relay state synchronization. + relay' <- updateRelayStatus db relay RSInactive + pure (relay' : acc, True) + _ -> pure (relay : acc, changed) +``` + +After the same owner's `APIRemoveMembers` call, the relay is already `RSInactive` before `updatePublicGroupData` triggers the LINK callback. The guard matches `RSInactive` but `updateRelayStatus` is idempotent (RSInactive→RSInactive is a no-op write). + +### 5. Relay Self-Check (`runRelayGroupLinkChecks`) + +**File**: `src/Simplex/Chat/Library/Commands.hs` (lines 4729-4735) + +Implement the stub. The existing `startRelayChecks` (Commands.hs:225-233) already launches `runRelayGroupLinkChecks` as an async task via `relayGroupLinkChecksAsync`. The stub currently does `pure ()` and exits immediately. Replace with a periodic loop following the `cleanupManager` pattern (Commands.hs:4643): + +``` +runRelayGroupLinkChecks :: User -> CM () +runRelayGroupLinkChecks user = do + initialDelay <- asks (initialCleanupManagerDelay . config) + liftIO $ threadDelay' initialDelay + interval <- asks (cleanupManagerInterval . config) -- or a dedicated config field + forever $ do + flip catchAllErrors eToView $ do + lift waitChatStartedAndActivated + checkRelayGroups + liftIO $ threadDelay' $ diffToMicroseconds interval + where + checkRelayGroups = do + vr <- chatVersionRange + -- Get all groups where this client is a relay (relay_own_status is set and not RSInactive) + relayGroups <- withFastStore' $ \db -> getRelayOwnGroups db vr user + forM_ relayGroups $ \gInfo -> tryAllErrors $ do + case publicGroup (groupProfile gInfo) of + Just PublicGroupProfile {groupLink = sLnk} -> do + -- getShortLinkConnReq' returns (FixedLinkData, ConnLinkData m). + -- ConnLinkData 'CMContact = ContactLinkData VersionRangeSMPA UserContactData + -- (NOT UserContactLinkData which is for the LINK event's auData) + (_, ContactLinkData _ UserContactData {relays = relayLinks}) <- + getShortLinkConnReq' NRMBackground user sLnk + -- Check if our own relay link is present + gLink_ <- withFastStore' $ \db -> runExceptT $ getGroupLink db user gInfo + case gLink_ of + Right GroupLink {connLinkContact = CCLink _ (Just ourLink)} -> + if ourLink `elem` relayLinks + then do + -- Our link is present — promote to RSActive if still RSAccepted + gInfo' <- withFastStore' $ \db -> updateRelayOwnStatusFromTo db gInfo RSAccepted RSActive + when (relayOwnStatus gInfo' /= relayOwnStatus gInfo) $ + toView $ CEvtGroupRelayUpdated user gInfo' (membership gInfo') + else do + -- Our link is ABSENT — we have been removed + withFastStore' $ \db -> updateRelayOwnStatus_ db gInfo RSInactive + -- Per RFC: relay should forward "relay is deleted" notification to + -- connected members, then clean up. The x.grp.mem.del from owner + -- may also arrive and trigger cleanup independently. + _ -> pure () + _ -> pure () +``` + +**New store function** in `Store/Groups.hs`: + +```haskell +getRelayOwnGroups :: DB.Connection -> VersionRangeChat -> User -> IO [GroupInfo] +-- SELECT groups WHERE relay_own_status IS NOT NULL AND relay_own_status != 'inactive' +``` + +--- + +## State Synchronization Summary + +``` + SMP Server (group link data) + ┌──────────────────────────────┐ + │ UserContactData { │ + │ relays: [relay1, relay2] │ + │ } │ + └──────────┬───────────────────┘ + │ + ┌───────────────┼───────────────┐ + │ writes │ reads │ reads + ▼ ▼ ▼ + Owner Relay (self) Subscriber + setGroupLinkData runRelayGroup syncSubscriber + (via updatePublic LinkChecks Relays (in + GroupData) APIGetUpdated + GroupLinkData) + Triggers: Triggers: Triggers: + - Add relay - Periodic check - Opening channel + - Remove member (existing UI flow) + - Profile update +``` + +**Owner writes** → SMP server is updated → **Relays and Subscribers read** → discover changes → adjust local state. + +**Key design principle**: The `XGrpMemDel` message broadcast through other relays is the primary notification mechanism for relay removal. Subscribers receive it promptly via their connected relays. Link data synchronization via `APIGetUpdatedGroupLinkData` is the backup mechanism — it catches cases where the `XGrpMemDel` was missed (subscriber offline, relay connection issues) and handles new relay discovery. + +--- + +## Edge Cases and Failure Recovery + +1. **Add relay fails (network)**: `addRelays` handles temporary errors. The relay remains in RSInvited; owner can retry or the relay will process the pending invitation when it comes online. + +2. **Removed relay is malicious / refuses to notify subscribers**: Not a problem. `APIRemoveMembers` sends `XGrpMemDel` to all relay members. Other (non-malicious) relays forward it to subscribers. Subscribers learn about the removal regardless of the removed relay's behavior. + +3. **Remove relay, all relays offline**: `XGrpMemDel` is queued for delivery. Link data is still updated. Subscribers will discover the change via `APIGetUpdatedGroupLinkData` next time they open the channel. + +4. **Owner removes last relay**: Subscribers lose message delivery. Owner must add a new relay. Subscribers will discover the new relay via `syncSubscriberRelays` when they next open the channel. + +5. **Relay goes offline permanently**: Owner removes it via `APIRemoveMembers`. New subscribers won't see it in link data. Existing subscribers with connections to this relay will experience connection failures. On next channel open, `syncSubscriberRelays` discovers the relay link is gone and marks it removed locally. + +6. **Subscriber discovers new relay via link data**: `syncSubscriberRelays` calls `connectToRelay` (same function used by `APIConnectPreparedGroup`). + +--- + +## Implementation Order + +1. **Relay deactivation in member-removal primitives** — add `deactivateRelayIfNeeded` helper to `deleteOrUpdateMemberRecordIO` and `updateMemberRecordDeleted` in Internal.hs; remove redundant code from `xGrpLeave`. +2. **LINK handler relay-removal detection** — implement the TODO in Subscriber.hs to detect absent relay links. +3. **`APIAddGroupRelays`** — new command, reuses `addRelays`. +4. **`runRelayGroupLinkChecks`** — relay self-check implementation. +5. **Extend `APIGetUpdatedGroupLinkData`** — add `syncSubscriberRelays` for subscriber relay synchronization. +6. **iOS UI** — ChannelRelaysView add/remove buttons, AddGroupRelayView sheet, API functions. + +## Files Changed (Backend) + +| File | Change | +|------|--------| +| `src/Simplex/Chat/Controller.hs` | Add `APIAddGroupRelays` command; add `CRGroupRelaysAdded`, `CRGroupRelaysAddFailed` responses | +| `src/Simplex/Chat/Library/Internal.hs` | Add `deactivateRelayIfNeeded` helper; extend `deleteOrUpdateMemberRecordIO` and `updateMemberRecordDeleted` to call it | +| `src/Simplex/Chat/Library/Commands.hs` | Parameterize and move `connectToRelay` to `processChatCommand` scope; implement `APIAddGroupRelays` handler; implement `runRelayGroupLinkChecks`; extend `APIGetUpdatedGroupLinkData`; add `syncSubscriberRelays` (all in `processChatCommand` scope for `connectViaContact` access) | +| `src/Simplex/Chat/Library/Subscriber.hs` | Fix LINK handler relay removal detection; remove redundant relay deactivation from `xGrpLeave` | +| `src/Simplex/Chat/Store/Groups.hs` | Add `getRelayOwnGroups` | + +## Files Changed (iOS) + +| File | Change | +|------|--------| +| `apps/ios/Shared/Model/AppAPITypes.swift` | Add `APIAddGroupRelays` command, `CRGroupRelaysAdded`/`CRGroupRelaysAddFailed` responses | +| `apps/ios/Shared/Model/SimpleXAPI.swift` | Add `apiAddGroupRelays` function | +| `apps/ios/Shared/Views/Chat/Group/GroupMemberInfoView.swift` | Remove `.relay` guard from `adminDestructiveSection` (line 646); add relay-specific button/alert text; add last-active-relay warning | +| `apps/ios/Shared/Views/Chat/Group/GroupChatInfoView.swift` | Add relay branch to `showRemoveMemberAlert` text | +| `apps/ios/Shared/Views/Chat/Group/ChannelRelaysView.swift` | Add relay button | +| `apps/ios/Shared/Views/Chat/Group/AddGroupRelayView.swift` | NEW: relay selection sheet | diff --git a/plans/2026-05-01-support-bot-list-api-pagination.md b/plans/2026-05-01-support-bot-list-api-pagination.md new file mode 100644 index 0000000000..44cfde7971 --- /dev/null +++ b/plans/2026-05-01-support-bot-list-api-pagination.md @@ -0,0 +1,377 @@ +# Plan: Fix support-bot crash on large databases — use pagination and direct lookup + +## Context + +The simplex-support-bot crashes during startup against large production +databases: + +``` +[2026-04-30T15:52:53.498Z] Grok contact from state: ID=142676 +[2026-04-30T15:52:53.498Z] Resolving team group... +:0 +[Error: Unknown failure] +``` + +The crash happens inside `chat.apiListGroups(mainUser.userId)` at +`apps/simplex-support-bot/src/index.ts:215`. The native binding marshals the +Haskell core's response to a JS string at +`packages/simplex-chat-nodejs/cpp/simplex.cc:255` (`chat_send_cmd`) → +`HandleCResult` (line 157) → `Napi::String::New` in `OnOK`. When the response +exceeds V8's max string length (~512 MB on 64-bit), N-API string allocation +fails. The literal string `"Unknown failure"` does **not** appear anywhere in +this repo — confirmed by full-tree search — so the message originates from V8 +or N-API internals rather than the binding's own error path (which would say +`chat_send_cmd failed`). Hypothesis: oversized string allocation throws a JS +exception that propagates up unannotated. + +Two distinct misuse patterns drive the payload size: + +**A. List-then-find by ID** (most call sites). The bot pulls every contact / +every group with `apiListContacts` / `apiListGroups`, then calls `find(...)` +to locate one record by a known ID. This is gratuitous — there is already +`apiGetChat(chatType, chatId, count=0)` (`packages/simplex-chat-nodejs/src/api.ts:819`) +that returns one `AChat` whose `chatInfo` carries the full `GroupInfo` / +`Contact` (with `customData`) and zero items. The Haskell parser accepts +`count=0` (`src/Simplex/Chat/Library/Commands.hs:5210`), and +`getDirectChatLast_` / `getGroupChatLast_` return empty `chatItems` with full +`chatInfo`. + +**B. Genuine multi-record scan** (one site). +`apps/simplex-support-bot/src/cards.ts:131` (`refreshAllCards`) enumerates +groups where `customData.cardItemId && !complete` to refresh in-flight cards +on restart. The Haskell side already supports paginated scans via +`APIGetChats` (`/_get chats {userId} pcc=on|off count=N`, +`src/Simplex/Chat/Library/Commands.hs:4868`). It is currently in +`undocumentedCommands` (`bots/src/API/Docs/Commands.hs:360`), so the codegen +does not emit it for the TypeScript bot library. Confirmed: the chat preview +returned by `getChatPreviews` carries `customData` on `GroupInfo` +(`src/Simplex/Chat/Store/Shared.hs:685`, `toGroupInfo`). + +Active card state is already tracked on each group via `customData.cardItemId` +and `customData.complete` (written through `apiSetGroupCustomData` at +`apps/simplex-support-bot/src/cards.ts:103,231`). No `state.json` schema +change is needed — phase 3 reads exactly the same `customData` it already +writes, just via paginated `APIGetChats` instead of a full `apiListGroups`. + +Per the constraint, `apiListContacts` / `apiListGroups` stay in the nodejs +library unchanged for other consumers. Audit confirmed no callers outside +support-bot use them today. + +## Phase 1 — Plumb `APIGetChats` through the bot library + +The codegen pipeline is test-driven: `tests/APIDocs.hs:41–44` invokes +`testGenerate` against the functions in +`bots/src/API/Docs/Generate/TypeScript.hs`, which writes to: + +- `packages/simplex-chat-client/types/typescript/src/commands.ts` +- `packages/simplex-chat-client/types/typescript/src/responses.ts` +- `packages/simplex-chat-client/types/typescript/src/types.ts` + +Run via `cabal test`. The published `@simplex-chat/types` npm package is +built from this TypeScript source; the copy under +`packages/simplex-chat-nodejs/node_modules/@simplex-chat/types/dist/` is a +downstream build artifact and is **not** edited directly. + +Currently missing from generated TS: +`T.PaginationByTime`, `T.ChatListQuery`, `CC.APIGetChats`, and the +`apiChats` response tag on `ChatResponse`. + +### 1.1 `bots/src/API/Docs/Commands.hs` + +- **Remove** `"APIGetChats",` from `undocumentedCommands` (line 360). +- **Add** an entry under "Chat commands" (next to `APIListContacts` / + `APIListGroups` at lines 145–146). Match the Haskell parser at + `src/Simplex/Chat/Library/Commands.hs:4868`: + + ```haskell + ( "APIGetChats", + [], + "Get chat previews. Supports time-based pagination — use this " <> + "instead of APIListContacts / APIListGroups when scanning at scale.", + ["CRApiChats", "CRChatCmdError"], + [], + Nothing, + "/_get chats " <> Param "userId" + <> OnOffParam "pcc" "pendingConnections" (Just False) + <> Optional "" (" " <> Param "$0") "pagination" + <> Optional "" (" " <> Json "$0") "query" + ) + ``` + + Note: the `query` segment uses `" " <> Json "$0"` (no `"json "` prefix) — + the parser accepts `A.space *> jsonP` directly. + +### 1.2 `bots/src/API/Docs/Types.hs` + +The type universe already references `PaginationByTime` and `ChatListQuery` +in commented form (lines 381, 390 and 592, 602). Uncomment all four lines. +Confirm the constructor-prefix encoding (`STRecord`/`STUnion`, prefix +`""`/`"CLQ"`) matches the existing definitions in +`src/Simplex/Chat/Controller.hs:992,998` and the JSON deriving at line 1661 +(`sumTypeJSON $ dropPrefix "CLQ"`). + +### 1.3 `bots/src/API/Docs/Responses.hs` + +- Uncomment `("CRApiChats", "...")` at line 100. +- Remove `"CRApiChats",` from `undocumentedResponses` at line 123. + +### 1.4 Regenerate TypeScript types + +Run `cabal test` (the `APIDocs` test suite drives generation). Inspect the +diffs in `packages/simplex-chat-client/types/typescript/src/{commands,responses,types}.ts`. +Verify: + +- `T.PaginationByTime` (sum type with `PTLast`/`PTAfter`/`PTBefore`) exists + with a generated `cmdString`. Compare wire format against the Haskell + `paginationByTimeP` at `src/Simplex/Chat/Library/Commands.hs:5216`: + `count=N` | `after=TS count=N` | `before=TS count=N`. +- `T.ChatListQuery` exists with `CLQFilters` / `CLQSearch` JSON-encoded + variants. +- `CC.APIGetChats.cmdString({userId, pendingConnections, pagination, query})` + exists and emits the expected wire format. +- `r.type === "apiChats"` with `r.chats: T.AChat[]` exists in the response + union (drops `CR` prefix per `sumTypeJSON`, + `src/Simplex/Chat/Controller.hs:1743`). + +Bump `@simplex-chat/types` version and re-link / re-build the +`simplex-chat-nodejs` package so the new symbols are available. + +### 1.5 `packages/simplex-chat-nodejs/src/api.ts` + +Add a single method next to `apiListGroups` (line 761): + +```ts +/** + * Get chat previews (paginated). + * Network usage: no. + * + * Prefer this over apiListContacts / apiListGroups for any scan: those + * methods load the entire history into memory and will fail on large DBs. + */ +async apiGetChats( + userId: number, + pagination: T.PaginationByTime, + query: T.ChatListQuery = {type: "filters", favorite: false, unread: false}, + pendingConnections = false, +): Promise { + const r = await this.sendChatCmd( + CC.APIGetChats.cmdString({userId, pendingConnections, pagination, query}) + ) + if (r.type === "apiChats") return r.chats + throw new ChatCommandError("error getting chats", r) +} +``` + +(Exact `T.PaginationByTime` / `T.ChatListQuery` shapes come from the codegen +output of phase 1.4 — verify the discriminator field names before locking +this signature.) + +## Phase 2 — Replace list-then-find with direct lookup + +For every site below, replace `apiList…().find(…)` with +`apiGetChat(ChatType.X, id, 0)`. Treat "not found" — the chat was deleted — +as a clean missing-record case (log + skip). The wire format +`/_get chat #{id} count=0` is already supported. + +### 2.1 Error matcher + +The Haskell `SEContactNotFound` / `SEGroupNotFound` (in +`src/Simplex/Chat/Store/Shared.hs:863` and elsewhere) surface to TS as: + +```ts +err.chatError?.type === "errorStore" + && err.chatError.storeError.type === "groupNotFound" // or "contactNotFound" +``` + +Both discriminators are already present in the generated types +(`types.d.ts:2825` and `:2788`). Add a small helper in +`apps/simplex-support-bot/src/util.ts`: + +```ts +export function isChatNotFound(err: unknown, kind: "group" | "contact"): boolean { + if (!(err instanceof core.ChatAPIError)) return false + if (err.chatError?.type !== "errorStore") return false + const seType = err.chatError.storeError.type + return kind === "group" ? seType === "groupNotFound" : seType === "contactNotFound" +} +``` + +(Strict — does not swallow other `errorStore` variants.) + +### 2.2 Ergonomic wrappers + +Add two thin helpers in `apps/simplex-support-bot/src/util.ts` (the constraint +forbids touching `apiListContacts` / `apiListGroups` in the nodejs library; +keeping these helpers in the support-bot util keeps the library surface +unchanged): + +```ts +export async function getGroupInfo(chat: api.ChatApi, groupId: number): Promise { + try { + const c = await chat.apiGetChat(T.ChatType.Group, groupId, 0) + return c.chatInfo.type === "group" ? c.chatInfo.groupInfo : null + } catch (err) { + if (isChatNotFound(err, "group")) return null + throw err + } +} + +export async function getContact(chat: api.ChatApi, contactId: number): Promise { + try { + const c = await chat.apiGetChat(T.ChatType.Direct, contactId, 0) + return c.chatInfo.type === "direct" ? c.chatInfo.contact : null + } catch (err) { + if (isChatNotFound(err, "contact")) return null + throw err + } +} +``` + +### 2.3 Call-site changes + +All sites must keep their existing `withMainProfile` / `profileMutex` +wrapping where present. + +- **`apps/simplex-support-bot/src/index.ts:165–180`** (Grok contact + resolution). Drop the `apiListContacts(mainUser.userId)` call entirely. If + `state.grokContactId` is set, call `getContact(chat, state.grokContactId)` + inside `profileMutex.runExclusive`. Preserve the existing log lines. +- **`apps/simplex-support-bot/src/index.ts:306–320`** (team member + validation). Loop and `getContact(chat, member.id)` per member. Compare + `displayName` as before. Team rosters are small; N round-trips are fine. +- **`apps/simplex-support-bot/src/index.ts:213–227`** (team group + resolution). Replace `apiListGroups` + `find` with + `getGroupInfo(chat, state.teamGroupId)`. Preserve the "create new group" + fallback when the lookup returns `null`. +- **`apps/simplex-support-bot/src/bot.ts:796–805`** (`handleJoinCommand`). + Replace with `getGroupInfo(chat, targetGroupId)`; same `businessChat` + validation. +- **`apps/simplex-support-bot/src/cards.ts:120`** (`flushOne`). Direct + `getGroupInfo(chat, groupId)` (still inside `withMainProfile`). +- **`apps/simplex-support-bot/src/cards.ts:213`** (`getRawCustomData`). + Direct lookup. **Hot path** — called on every `mergeCustomData` / + `clearCustomData`. Largest single win. +- **`apps/simplex-support-bot/src/cards.ts:251`** (`updateCard`). Direct + lookup. The "Read customData and groupInfo in one apiListGroups call" + comment goes away. + +After phase 2 the bot can boot and operate steadily on a large DB; phase 3 +is purely about startup reconciliation. + +## Phase 3 — Paginate `refreshAllCards` + +`apps/simplex-support-bot/src/cards.ts:131` is the only legitimate +multi-record scan. Convert it to a single bounded `apiGetChats` call: + +```ts +async refreshAllCards(): Promise { + // Scan the most recently active 1000 chats. Active cards live on + // recently-active customer chats by definition — a card stays open while + // the conversation is in flight. If the bot has been offline long enough + // that an active card has fallen outside the recent-1000 window, that + // card refreshes lazily on the next customer message (which moves the + // chat back into the recent window). + const chats = await this.withMainProfile(() => + this.chat.apiGetChats( + this.mainUserId, + {type: "last", count: 1000}, + ) + ) + const activeCards: {groupId: number; cardItemId: number}[] = [] + for (const c of chats) { + if (c.chatInfo.type !== "group") continue + const customData = c.chatInfo.groupInfo.customData as Record | undefined + if (customData + && typeof customData.cardItemId === "number" + && !customData.complete) { + activeCards.push({ + groupId: c.chatInfo.groupInfo.groupId, + cardItemId: customData.cardItemId, + }) + } + } + // (sort and refresh loop unchanged) +} +``` + +`count = 1000` per the constraint. No `state.json` schema change. Card +status remains entirely on the group's `customData` (`cardItemId`, +`complete`), which is what the bot already reads and writes. + +## Phase 4 — Verification + +### 4.1 Stress test + +Existing tests use `MockChatApi` (`apps/simplex-support-bot/bot.test.ts:24`), +which is in-memory and won't exercise the native binding. A meaningful +stress test needs a real `ChatApi.init` against Postgres. + +Add a new test file (e.g. +`apps/simplex-support-bot/test/stress.test.ts`) that: + +1. Starts an ephemeral Postgres or uses an existing test DB. +2. Calls `ChatApi.init` and seeds N synthetic groups + contacts via the + chat API. (No existing seeding helper — write one.) Reasonable N: 20k + each, large enough to expose the marshaling cliff but not so large that + the test takes minutes. +3. Boots the support-bot main flow against this DB and asserts: startup + completes within a wall-clock budget; resident memory stays bounded; + no native error. + +This is new infrastructure — keep scope tight. If standing up Postgres in +CI is too heavy, run as a manual stress harness rather than a CI test. + +### 4.2 Production replay + +Replay against (a copy of) the affected production DB. Confirm the bot +starts and the team group / Grok contact / team members all resolve. + +### 4.3 Smoke tests + +Existing functional flows via `bot.test.ts` continue to pass after the +phase-2 changes. Manually exercise: + +- Business-request acceptance. +- `/join` validation (the changed `bot.ts:799` path). +- Card create/update/complete cycle (`cards.ts` hot path). +- Restart-time card refresh (`refreshAllCards`). + +## Risks and footguns + +- **`/_get chats` parser default is `PTLast 5000`** + (`src/Simplex/Chat/Library/Commands.hs:4872`). Even 5000 previews can be + heavy. Support-bot now always passes an explicit `count=1000`, but the + default itself remains a footgun for other callers — flag for follow-up; + not changed here. +- **`apiListMembers` is per-group, not per-DB.** Used at `bot.ts:629,825` + and `cards.ts:165`. Bounded by group membership, not history size, so + out of scope for this fix. Flag if customer groups grow huge (>1000 + members) — would warrant a paginated members API at that point. +- **Codegen output sanity.** Phase 1.4 must be inspected by hand — the + generated `cmdString` for `T.PaginationByTime` and the `r.type` / + `r.chats` shape on the response side are the integration points the rest + of the plan depends on. Do not skip eyeballing the diff. +- **`apiGetChat(..., 0)` semantics on a non-existent chatId.** Verified: + the error tag is `chatError.type === "errorStore"` with + `storeError.type === "groupNotFound"` or `"contactNotFound"`. Both + discriminators already exist in the generated types + (`types.d.ts:2825,2788`). `isChatNotFound` matches them precisely; do + not loosen it. +- **Native binding crash hypothesis is unverified.** The literal "Unknown + failure" string is not in this tree. Most likely V8/N-API surfacing a + string-allocation or JSON-parse failure. The fix in this plan addresses + the proximate cause (oversized response payload) regardless of the exact + surfacing path; if the same error reappears after the fix, dig into the + binding's `OnOK` handler to add explicit size-check / better diagnostics. +- **`@simplex-chat/types` package version bump.** Phase 1.4 produces + TypeScript changes in `packages/simplex-chat-client/types/typescript/`. + Bumping the version and re-publishing (or rebuilding locally) is required + before phase 1.5 lands. Coordinate the release sequence. + +## Out of scope + +- Deprecating or paginating `apiListContacts` / `apiListGroups` in the + nodejs library. They stay as-is; only support-bot stops calling them. +- Lowering the `/_get chats` parser default from `PTLast 5000`. +- Adding a paginated members API. +- Native binding diagnostics for oversized responses. diff --git a/plans/2026-05-07-desktop-rtl-composer-fix.md b/plans/2026-05-07-desktop-rtl-composer-fix.md new file mode 100644 index 0000000000..b593be481d --- /dev/null +++ b/plans/2026-05-07-desktop-rtl-composer-fix.md @@ -0,0 +1,390 @@ +# Fix #4137 — desktop: RTL text rendering under send button + +Target file: +`apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/PlatformTextField.desktop.kt` + +--- + +## 1. Problem statement + +### 1.1 Symptom + +On desktop, when the user types right-to-left text (Arabic, Hebrew, +Persian) in the chat composer **while the global system locale is LTR**, +the first characters of the typed text are rendered **under the send +button** at the bottom-right corner and become invisible while typing. + +The same defect places the voice-preview / disabled-state +`ComposeOverlay` text on the wrong horizontal side in this configuration. + +### 1.2 Configurations affected + +Tested 4 combinations of (global locale × typed-text direction): + +| Global locale | Typed text | Behavior | +|---------------|------------|----------| +| LTR | LTR | OK | +| LTR | RTL | **broken** — text under send button | +| RTL | LTR | OK | +| RTL | RTL | OK | + +Only the LTR-locale + RTL-text combination is broken. This is the +configuration where the **inner text rendering direction** (forced RTL by +`decorationBox`) **disagrees** with the **outer layout direction** (LTR). + +### 1.3 Why it matters + +- Persian/Arabic/Hebrew users on a non-localized OS (very common: most + desktop installs default to English) cannot see the start of their own + message until it grows past the send button. +- The composer is the most-used input in the app; this is a daily + papercut for the affected user population. + +--- + +## 2. Root cause + +A direction-resolution decoupling introduced by an unrelated refactor. +Two commits matter: + +### 2.1 The original RTL fix — #4675 (`2ae5a8bff`, Aug 2024) + +Added padding logic *inside* a forced-RTL scope: + +```kotlin +CompositionLocalProvider(LocalLayoutDirection provides Rtl) { + Column(Modifier.weight(1f).padding(start = startPadding, end = endPadding)) { + TextFieldDecorationBox(...) + } +} +``` + +Inside that scope `start` resolves to the **right** edge. So setting +`startPadding = 50.dp` for the RTL-text + LTR-locale case correctly +reserved 50dp on the visual right — same side as the send button. + +**The padding side and button side were aligned by accident.** `start` +tracked the forced-RTL direction in the same way that `Alignment.BottomEnd` +in `SendMsgView.kt:120` tracked the global direction — and the two +happened to coincide *as long as those directions were the same.* The +pre-existing rule expressed in code was effectively "padding follows +typed-text direction," which was equivalent to "padding follows button +side" only when the inner forced direction and the outer global direction +agreed. + +### 2.2 The breaking refactor — #5051 edge-to-edge (`4162bccc4`, Nov 2024) + +The padding modifier was lifted **out** of the forced-RTL scope onto the +outer `BasicTextField` (the wrapping `Column` and `Row` were removed). +The outer modifier now resolves `start`/`end` against the **global** +layout direction, but `decorationBox` still forces +`LayoutDirection.Rtl` for RTL characters internally. + +In LTR-global + RTL-text: + +- `padding(start = 50.dp)` → 50dp reserved on visual **left** +- Text right-aligned by forced-RTL `decorationBox` → renders against + visual **right** +- 0dp on the right → text under the send button (which is at + `Alignment.BottomEnd` in LTR global = visual right) + +The compensation logic written for the inner-scope semantics silently +became wrong when the modifier moved outward. Code compiled, tests passed, +behavior diverged. + +### 2.3 The actual invariant the layout obeys + +Reading the layout call graph (`SendMsgView` → `PlatformTextField`): + +- `SendMsgView.kt:120` — `Box(Modifier.align(Alignment.BottomEnd)...)` + places the send button using the **global** layout direction. +- `PlatformTextField.desktop.kt` — `BasicTextField` modifier chain is + applied in the **global** layout direction. + +The constraint is therefore exactly one rule: + +> **The textfield must reserve space on the global layout direction's +> `end` — the same side `Alignment.BottomEnd` resolves to in the parent +> `Box`.** + +Pre-PR code expressed a different (wrong) rule — "padding follows +typed-text direction" — which agreed with the actual invariant only when +no RTL-text/LTR-locale mismatch existed. The 4 of 4 case failure → 1 of 4 +case failure shape is the signature of this kind of accidental alignment. + +### 2.4 Why this is structural, not a typo + +The defect is not a missing case — it is the **wrong rule**. Adding a +new branch (e.g. "if RTL-text + LTR-locale, swap padding sides +*again*") would silence the symptom while leaving the wrong rule in +place. The fix is to delete the wrong rule and write the actual +invariant. + +--- + +## 3. Solution summary + +Make the two conditional assignments that compute `startPadding` and +`endPadding` unconditional, taking the values they already produced in +the `else` branch: + +```kotlin +val startPadding = 0.dp +val endPadding = startEndPadding +``` + +The surrounding code is unchanged — `startEndPadding`'s computation, +the `PaddingValues(startPadding, 12.dp, endPadding, 0.dp)` construction, +the `.padding(start = startPadding, end = endPadding)` modifier call, +and the original two-line comment all stay verbatim. + +Master's `if (isRtlByCharacters && isLtrGlobally)` predicate split each +of `startPadding` and `endPadding` into two branches. In cases 1, 3, 4 +the predicate is `false` and master takes the `else` branch — exactly +the values the surgical version produces unconditionally. Only case 2 +(the bug) takes the `then` branch, and that branch reserves space on +the wrong horizontal side. Removing the predicate removes only case 2's +wrong values; cases 1/3/4 are byte-identical to master. + +The 95dp/50dp distinction is preserved verbatim through `startEndPadding`, +which is unchanged. + +`ComposeOverlay` (called twice at the bottom of `PlatformTextField`) +reuses the same `padding` value — its placement is corrected for the +same reason without an extra change. + +**Net effect**: 2 lines changed. + +--- + +## 4. Detailed technical design + +### 4.1 Behavior matrix (post-fix) + +| Case | Locale | Text | Master `(start, end)` | Surgical `(start, end)` | Button side | +|------|--------|------|-----------------------|-------------------------|-------------| +| 1 | LTR | LTR | `(0, 50)` | `(0, 50)` | right ✓ same | +| 2 | LTR | RTL | `(50, 0)` | `(0, 50)` | right ✓ **fix** | +| 2′ | LTR | RTL + empty + voice | `(95, 0)` | `(0, 95)` | right ✓ **fix** | +| 3 | RTL | LTR | `(0, 50)` | `(0, 50)` | left ✓ same | +| 4 | RTL | RTL | `(0, 50)` | `(0, 50)` | left ✓ same | + +Three of the four pre-PR cases are byte-identical to the new code. +Only the broken case (LTR locale + RTL text) flips from `(50, 0)` to +`(0, 50)`, which matches the side where the send button resolves. + +### 4.2 Why the 95dp condition stays exactly as-is + +The 95dp special case fires only in RTL-text + LTR-locale + empty + +voice-button. In every other configuration, the placeholder text +either left-aligns (no collision with the right-side voice button row) +or sits on the visual side opposite to the buttons (RTL global puts +buttons on the left while forced-RTL placeholder displays on visual +right). + +Only the RTL-text + LTR-global case puts a right-aligned placeholder +on the same side as the wider voice-button row. The condition is +intrinsic to the architecture (forced-RTL inside `decorationBox` while +the outer layout is global LTR), not a bug — it must be preserved. + +### 4.3 What is *not* changed + +Out of scope for #4137 — listed for clarity: + +- The `CompositionLocalProvider` inside `decorationBox` that forces + `LayoutDirection.Rtl` for RTL-by-characters input (the BiDi-detection + workaround from #4675 itself). +- `lastTimeWasRtlByCharacters` state and `isRtl` detection on the first + 50 characters of the message. +- The `ComposeOverlay` composable — it inherits the corrected + `padding`. +- `SendMsgView`, the `Alignment.BottomEnd` send button placement, and + the voice-button row layout. +- The Android implementation (`PlatformTextField.android.kt`) — uses + a native Android `EditText` with `setPaddingRelative`, which + resolves against the view's own layout direction; behavior is + unaffected and out of scope. + +### 4.4 Properties of the resulting code + +- The two adjacent conditional assignments dispatching on + `isRtlByCharacters && isLtrGlobally` (one for `startPadding`, one for + `endPadding`) become unconditional. The predicate is removed; the + `else` branch's values are lifted to the bare assignments. +- All four locals (`startEndPadding`, `startPadding`, `endPadding`, + `padding`) keep the same names and continue to exist. +- The `PaddingValues(startPadding, 12.dp, endPadding, 0.dp)` call and + the `.padding(start = startPadding, end = endPadding)` modifier are + unchanged. +- The original two-line comment is unchanged. "padding from right side + should be bigger" remains accurate — `endPadding` is still `95.dp` + vs `50.dp` under the same condition as before, just consistently on + the global end side. +- No behavior is removed: RTL detection, the `decorationBox` direction + override, overlay rendering, and the empty-text/voice-button 95dp + expansion are all retained verbatim. +- Diff size: 2 lines changed, one file. No reformatting of unrelated + code. + +### 4.5 Risk surface + +- **Compose 1.7.x BiDi engine** — unchanged; we still rely on + `decorationBox`'s forced direction for right-alignment of typed RTL + text. No new BiDi dependency. +- **Padding API** — `Modifier.padding(end = X.dp)` and + `PaddingValues(start, top, end, bottom)` are stable Compose APIs. +- **Direction resolution** — `Modifier.padding`'s start/end have + resolved against the enclosing `LocalLayoutDirection` since Compose + Foundation 1.0; no version-sensitive behavior. +- **Cross-platform** — Android implementation uses a native + `EditText`; no shared change required. + +--- + +## 5. Detailed implementation plan + +### 5.1 The exact edit + +File: +`apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/PlatformTextField.desktop.kt` + +**Lines 89–90 — replace 2 lines:** + +```kotlin +// remove + val startPadding = if (isRtlByCharacters && isLtrGlobally) startEndPadding else 0.dp + val endPadding = if (isRtlByCharacters && isLtrGlobally) 0.dp else startEndPadding + +// add + val startPadding = 0.dp + val endPadding = startEndPadding +``` + +No other lines change. No imports added or removed. The comment, the +`startEndPadding` computation, the `PaddingValues` construction, and +the `.padding(start = startPadding, end = endPadding)` modifier are +all preserved verbatim. + +### 5.2 Steps + +1. Edit `PlatformTextField.desktop.kt` at the site above (lines 89–90). +2. Build desktop module: + `cd apps/multiplatform && ./gradlew :common:desktopMainClasses` +3. Run desktop app on an LTR system locale; type + `متن راست به چپ` in the composer; verify all characters visible. +4. Type ASCII; verify no regression. +5. Switch system locale to Arabic/Persian/Hebrew; repeat both inputs; + verify send button and reservation flip together to the visual + left, with no overlap. +6. Trigger voice preview / disabled-state placeholder in each + configuration; verify the overlay text is on the side opposite + the send button. +7. Commit on a topic branch (`nd/fix-RTL`); PR title: + `desktop: fix RTL text rendering under the send button`; reference + `Fixes #4137`. + +### 5.3 Test matrix to verify manually + +| # | Locale | Typed text | Empty + voice? | Expectation | +|---|--------|-----------|----------------|-------------| +| 1 | LTR | ASCII | n/a | unchanged from current | +| 2 | LTR | RTL chars | no | chars visible, no overlap with right-side button | +| 3 | LTR | empty | yes | placeholder + voice-button row both visible | +| 4 | LTR | (was RTL) → empty | yes | placeholder clears 95dp on right (sticky `lastTimeWasRtlByCharacters`) | +| 5 | RTL | ASCII | no | unchanged | +| 6 | RTL | RTL chars | no | unchanged | +| 7 | RTL | empty | yes | unchanged | + +### 5.4 Rollback + +Revert is one commit and one file. Behavior reverts cleanly. + +--- + +## 6. Alternative approaches considered + +### 6.1 Chosen approach — drop the buggy `then` branch (§3) + +The 2-line surgical change. Removes the predicate from the +`startPadding` and `endPadding` assignments, keeping the (correct) +`else` branch values as the unconditional definition. Smallest +possible diff; preserves all variable names, the comment, the +`PaddingValues` call, and the `.padding(start, end)` modifier. +Fixes the overlay placement as a free byproduct. + +### 6.2 Re-couple padding to inner forced direction by wrapping `BasicTextField` + +Move the `CompositionLocalProvider(LocalLayoutDirection = Rtl)` *outside* +`BasicTextField` rather than inside `decorationBox`. The outer +`.padding(start, end)` would then resolve in the same direction as the +inner text, restoring the pre-#5051 invariant and letting the +historical `start = 50.dp / end = 0.dp` swap work again. + +**Pros**: padding-vs-text consistency at the source. + +**Cons**: also flips `fillMaxWidth`, `focusRequester`, `onPreviewKeyEvent`, +and the parent `Box`'s `Alignment.BottomEnd` resolution direction is +**still global** — so the textfield and the button align against +different directions, moving the mismatch instead of removing it. +Bigger refactor, broader test surface, no net gain. **Rejected.** + +### 6.3 Remove the forced-RTL override; rely on Compose BiDi + +Delete the `CompositionLocalProvider` inside `decorationBox`. Let +Compose's BiDi engine right-align RTL paragraphs without forcing a +paragraph direction. Then `start`/`end` resolve consistently against +the global direction everywhere; `isRtlByCharacters`, +`lastTimeWasRtlByCharacters`, and the 95dp special case can all be +deleted. + +**Pros**: largest simplification — eliminates the entire BiDi-detection +state machine and the 95dp branch. + +**Cons**: depends on Compose Desktop 1.7.x BiDi engine matching what +#4675 originally needed to enforce. If automatic BiDi is insufficient +(e.g. mixed Latin-RTL paragraphs, neutral characters at paragraph start, +numbers in RTL paragraphs), regressions reappear. Requires manual +verification across all the cases #4675 originally fixed. Out of scope +for #4137. **Reasonable follow-up; not part of this fix.** + +### 6.4 Derive padding from measured button-row width + +Refactor `SendMsgView` so the textfield's reservation comes from the +**measured** width of the button row (via `SubcomposeLayout` or shared +state), instead of hard-coded 50/95dp. The textfield would reserve +exactly as much as the buttons need, regardless of direction or button +configuration. + +**Pros**: removes the 50/95dp magic numbers and the +`showVoiceButton`-dependent branch. Self-correcting if the button row +ever changes. + +**Cons**: significantly larger refactor; `SubcomposeLayout` adds cost +to a frequently-recomposing view; doesn't fix the bug at hand any +better than §6.1. **Reasonable longer-term cleanup; not part of this +fix.** + +### 6.5 Add a third special case for the failing combination + +`if isRtlByCharacters && isLtrGlobally then padding(end=50) else +padding(start=startPadding, end=endPadding)`. + +**Pros**: one-line behavior fix. + +**Cons**: leaves the wrong rule in place plus a workaround on top. +Three branches where one suffices, and the underlying defect — padding +following typed-text direction instead of button side — is preserved +and now harder to spot. **Rejected as a workaround.** + +--- + +## 7. Recommendation + +Implement §3 (the chosen approach). It is the minimal structural +root-cause fix, also corrects the overlay placement as a free byproduct, +and removes the wrong-side `then` branch from both `startPadding` and +`endPadding`. + +Defer §6.3 and §6.4 to separate PRs if desired — both are reasonable +cleanups but are not necessary to fix #4137 and would expand the blast +radius beyond the bug. diff --git a/plans/2026-05-07-fullscreen-viewer-wrong-image.md b/plans/2026-05-07-fullscreen-viewer-wrong-image.md new file mode 100644 index 0000000000..068d2b07c7 --- /dev/null +++ b/plans/2026-05-07-fullscreen-viewer-wrong-image.md @@ -0,0 +1,135 @@ +# Fullscreen image viewer: opens the wrong image + +Design doc for the fix shipped in PR #6869. + +## Problem + +The fullscreen image viewer occasionally opened the chat's oldest media +instead of the image the user tapped. Reproductions were intermittent — +the gating condition turned out to be the runtime state of the +*immediately-older* sibling of the tapped item. + +## Background — pager state model + +`providerForGallery` (`apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt:3537`) +backs the fullscreen viewer with a virtual pager of 10000 pages. The +pager's state is two variables, captured in a closure: + +- `initialIndex` — pager page that maps to the anchor item; starts at 5000. +- `initialChatId` — id of the anchored chat item; starts at the tapped item. + +Invariant: page `initialIndex` always shows item `initialChatId`. Other +pages are computed by walking `chatItems` older / newer from the anchor +via the local `item()` helper. + +`scrollToStart()` is called by `ImageFullScreenView.kt` to lock the +pager's leftward boundary at the user's current item, in two situations: + +- **Init probe** (`ImageFullScreenView.kt:48-55`) — at viewer open, if + `getMedia(initialIndex - 1) == null` (no older sibling reachable), + reposition so the tapped item becomes page 0. +- **Runtime branch** (`ImageFullScreenView.kt:97-112`) — during scroll, + if `getMedia(index) == null` while the user is at `index + 1`, lock + the pager so the null page isn't reachable. + +Both callers want the same outcome: **page 0 = the user's current +anchor item**, leftward = unreachable. + +## Root cause + +Pre-fix body of `scrollToStart`: + +```kotlin +override fun scrollToStart() { + initialIndex = 0 + initialChatId = chatItems.firstOrNull { canShowMedia(it) }?.id ?: return +} +``` + +The second line rewrote `initialChatId` to the chat's *oldest showable +media* — not the user's current anchor. This mismatched what both +callers wanted. It happened to coincide with the correct behavior when +the anchor already was the chat's oldest showable, which is why the bug +masked itself for years. + +The bug surfaced when the init probe fired for a non-boundary reason: + +- The immediately-older sibling existed and passed `canShowMedia` (file + marked loaded; file path resolved or remote was connected). +- But `getLoadedImage` returned `null` at decode time (undecodable + bytes, missing file on disk, crypto error). +- `getMedia(initialIndex - 1)` therefore returned `null`. +- The probe misread that null as "no older sibling exists" and called + `scrollToStart()`. +- `scrollToStart` rewrote `initialChatId` to the chat's oldest showable. +- Page 0 of the pager rendered that oldest item — the wrong image. + +## Fix + +Delete the second line. `scrollToStart` becomes: + +```kotlin +override fun scrollToStart() { + initialIndex = 0 +} +``` + +`initialChatId` is preserved across the call. Page 0 now maps to the +current anchor — exactly what both callers wanted from the start. + +## Why this is correct for both callers + +- **Init probe.** Before the call, `initialChatId` is the tapped item. + After the call, page 0 = tapped item. ✓ +- **Runtime branch.** Before the call, `currentPageChanged` has already + updated `initialChatId` to the user's currently visible item. After + the call, page 0 = current item; the user's view is preserved with no + visible jump. (Pre-fix the user got teleported to the chat's oldest + media when a null sibling tripped this branch — a latent UX bug + resolved by the same one-line change.) + +## Why a wider structural change is not in scope here + +`getMedia` returns `null` for two distinct conditions: (a) navigation +found no showable item, (b) navigation found one but decode failed. A +deeper refactor would let consumers distinguish these. That refactor is +deliberately out of scope for this fix: + +- The user-visible bug (wrong image) is fully resolved by the one-line + change. No additional code is required to address the report. +- The remaining symptom — locking the user out of older loadable items + behind one that fails to decode — is mild, pre-existing, and not part + of the report. If it becomes user-visible, address it in a follow-up. +- A wider refactor would expand the diff, the review surface, and the + regression risk for a fix that needs to ship promptly. +- `good-code-v5.md`: *"Find the minimal change. The smallest structural + modification that achieves the goal."* The smallest modification that + resolves the reported bug is the deletion of one line. + +## Verification + +`apps/multiplatform/common/src/commonTest/kotlin/chat/simplex/app/ProviderForGalleryTest.kt`: + +- `testScrollToStartPreservesAnchor` — drives the public provider + interface: moves the anchor off `cItemId` via `currentPageChanged`, + calls `scrollToStart`, then reads the anchor back through `onDismiss`'s + `scrollTo` callback. Pre-fix would observe `scrollTo(2)` (the chat's + oldest); post-fix `scrollTo(1)` (anchor preserved). +- `testOnDismissOnActiveItemDoesNotScroll` — pins the `onDismiss` + early-return contract that the regression test reads through. + +Manual sanity (Android + desktop): tap newest / oldest / a middle image +in a chat with multiple media — fullscreen opens on the tapped image in +each case; swipe in both directions still works. + +## Alternatives considered and rejected + +- **Distinguish "no item" from "load failed" inside `getMedia`.** + Requires either a return-type redesign (sealed result type) or an + added query method on the interface. Both expand the diff well beyond + what the user-visible bug requires. Deferred to a possible follow-up + if the milder remaining symptom is reported. +- **Hoist the local `item()` helper to a top-level testable function.** + The regression test exercises the public provider interface and + reads the anchor back via `onDismiss`'s `scrollTo` callback, so no + internal extraction is needed for testability. diff --git a/plans/2026-05-07-simplex-chat-python-design.md b/plans/2026-05-07-simplex-chat-python-design.md new file mode 100644 index 0000000000..240f88714c --- /dev/null +++ b/plans/2026-05-07-simplex-chat-python-design.md @@ -0,0 +1,575 @@ +# SimpleX Chat Python library design + +## Table of contents + +- [What](#what) +- [Why](#why) +- [How](#how) +- [Architecture](#architecture) +- [Type generation](#type-generation) +- [Native lib loading](#native-lib-loading) +- [Public API](#public-api) +- [Distribution and CI](#distribution-and-ci) +- [Testing](#testing) +- [Open questions](#open-questions) + +## What + +A Python 3 client library `simplex-chat` on PyPI for SimpleX bots. Same capability as the Node.js library at `packages/simplex-chat-nodejs/`. + +The user writes a Python script with decorator-registered handlers; the library does the rest: + +```python +from simplex_chat import Bot, BotProfile, SqliteDb, TextMessage + +bot = Bot(profile=BotProfile(display_name="Squarer"), + db=SqliteDb(file_prefix="./bot"), + welcome="Send a number, I'll square it.") + +@bot.on_message(content_type="text") +async def square(msg: TextMessage) -> None: + try: + n = float(msg.text) + await msg.reply(f"{n} * {n} = {n * n}") + except ValueError: + await msg.reply("Not a number.") + +if __name__ == "__main__": + bot.run() +``` + +`pip install simplex-chat`, run the script, done. + +## Why + +SimpleX has a Node.js library (`simplex-chat`) and Haskell-built native lib (`libsimplex.{so,dylib,dll}`) but no Python equivalent. Python is the dominant language for bot scripting, automation, and data integration. Without a Python client, those users either can't use SimpleX or have to bridge through Node. + +The native `libsimplex` already exists as prebuilt artifacts (`simplex-chat/simplex-chat-libs` GitHub releases, one zip per platform/backend). The Haskell type metadata that drives the Node lib's TypeScript types is already in `bots/src/API/Docs/`. Both can be reused — adding Python bindings is mostly wiring, not a new system. + +## How + +Three pieces: + +1. **Extend the existing Haskell type generator** in `bots/src/API/Docs/Generate/` to emit a Python types module alongside the existing TypeScript one. The metadata is the same; only the rendering changes. Already includes `pySyntaxText` (used today in COMMANDS.md docs) — just needs a Python codegen module. + +2. **A new Python package** `packages/simplex-chat-python/` that wraps the prebuilt `libsimplex.*` via `ctypes`, downloading it on first use from the existing GitHub release. Async-only (`asyncio`), Python 3.11+. Single `Bot` class with decorator-registered handlers. + +3. **One small CI job** appended to `.github/workflows/build.yml`, after the existing `release-nodejs-libs` job, that publishes the Python package to PyPI on each release tag. ~15 lines of YAML. + +No new infrastructure: no separate libs build, no per-platform wheels, no PyPI size waiver, no second CI workflow. The libs zips that already exist for the Node lib are reused unchanged. + +## Architecture + +``` +┌────────────────────────────────────────────────────────────┐ +│ bots/src/API/Docs/ (Haskell, existing) │ +│ ├── Types/Commands/Events/Responses │ +│ ├── Syntax.hs (already has pySyntaxText) │ +│ ├── Generate/TypeScript.hs (existing) │ +│ └── Generate/Python.hs ← new │ +└────────────────────┬───────────────────────────────────────┘ + │ tests/APIDocs.hs runs both generators + ▼ writes auto-gen Python type files +┌────────────────────────────────────────────────────────────┐ +│ packages/simplex-chat-python/ (new) │ +│ │ +│ Bot ←── public API: decorators, lifecycle │ +│ └── ChatApi ← escape hatch: raw command access │ +│ └── core ← internal: typed FFI wrapper │ +│ └── _native ← internal: ctypes + lazy DL │ +│ ↓ │ +│ libsimplex.{so,dylib,dll} │ +│ downloaded from simplex-chat-libs releases │ +└────────────────────────────────────────────────────────────┘ +``` + +The split lets each layer be tested independently: `Bot`'s filter-routing without a real libsimplex (mock `api`), `core`'s JSON handling without ctypes (mock `_native`), `_native`'s download/ctypes work with a stub `.so`. Same shape as the Node lib (`bot.ts` → `api.ts` → `core.ts` → `simplex.cc`). + +## Type generation + +### New module: `bots/src/API/Docs/Generate/Python.hs` + +Mirrors `Generate/TypeScript.hs` line-for-line. Reuses the existing data sources (`chatCommandsDocs`, `chatResponsesDocs`, `chatEventsDocs`, `chatTypesDocs`) and `pySyntaxText` from `Syntax.hs`. Output goes to `packages/simplex-chat-python/src/simplex_chat/types/` as four files: `_types.py`, `_commands.py`, `_responses.py`, `_events.py`. + +### Type representation + +Wire types are `TypedDict` + `Literal` discriminators (matches Node lib semantics — just shapes, no runtime cost; pyright narrows tagged unions correctly). + +| Haskell | Python | +|---|---| +| `STRecord` | `class Foo(TypedDict)`; optional fields use `NotRequired[...]`. | +| `STUnion` / `STUnion1` | One `class Foo_(TypedDict)` per member with `type: Literal[""]` discriminator. Type alias `Foo = Foo_A \| Foo_B \| …`. Tag alias `Foo_Tag = Literal["", "", …]`. | +| `STEnum` / `STEnum1` / `STEnum'` | Type alias `Foo = Literal["a", "b", "c"]`. | +| `ATPrim TBool` | `bool` | +| `ATPrim TString` | `str` | +| `ATPrim TInt`/`TInt64`/`TWord32` | `int` | +| `ATPrim TDouble` | `float` | +| `ATPrim TJSONObject` | `dict[str, object]` | +| `ATPrim TUTCTime` | `str` (ISO-8601, comment-annotated) | +| `ATOptional t` | `NotRequired[]` in TypedDict fields; ` \| None` elsewhere | +| `ATArray {nonEmpty=False}` | `list[]` | +| `ATArray {nonEmpty=True}` | `list[]` with trailing `# non-empty` comment | +| `ATMap (PT k) v` | `dict[, ]` | +| `ATDef` / `ATRef` | type name as forward-string reference `""` | + +### Command serialization + +Each command becomes a `TypedDict` plus a `_cmd_string(self) -> str` function. The function body is produced by `pySyntaxText` (which already emits Python expressions for the existing Markdown docs): + +```python +class APICreateMyAddress(TypedDict): + userId: int + +def APICreateMyAddress_cmd_string(self: APICreateMyAddress) -> str: + return '/_address ' + str(self['userId']) + +APICreateMyAddress_Response = CR.UserContactLinkCreated | CR.ChatCmdError +``` + +### Field-naming convention + +| Where | Style | Why | +|---|---|---| +| Auto-generated `types/_*.py` | **camelCase** | Round-trips JSON to/from libsimplex; the keys are the wire format. | +| Hand-written user-facing types (`SqliteDb`, `BotProfile`, `Message`, …) | **snake_case** | These are Python-side configs and wrappers, never reach `chat_send_cmd` directly. | +| `CryptoArgs` (`fileKey`, `fileNonce`) | **camelCase** | Returned by `chat_write_file` as JSON; round-trips wire format. | +| Method names | **snake_case** | PEP 8. | +| Type names | **PascalCase** | PEP 8 + Haskell parity. | + +Generator must emit field names verbatim from `APIRecordField.fieldName'` — never transform. + +### Wiring + +Extend `tests/APIDocs.hs` with four `testGenerate` calls: + +```haskell +describe "Python" $ do + it "generate python commands" $ testGenerate Py.commandsCodeFile Py.commandsCodeText + it "generate python responses" $ testGenerate Py.responsesCodeFile Py.responsesCodeText + it "generate python events" $ testGenerate Py.eventsCodeFile Py.eventsCodeText + it "generate python types" $ testGenerate Py.typesCodeFile Py.typesCodeText +``` + +`cabal test` regenerates all eight files (4 TS + 4 PY) and fails on drift — same enforcement loop that already governs the TypeScript files. + +Add `API.Docs.Generate.Python` to `simplex-chat.cabal:572-580`. + +## Native lib loading + +### Approach: lazy download on first use + +Single pure-Python wheel on PyPI (`simplex-chat`, ~100 KB). On first `Bot(...)` / `ChatApi.init(...)`, the library downloads the matching `libsimplex` zip from `simplex-chat/simplex-chat-libs` releases into a user cache, then `ctypes.CDLL`s it. Subsequent runs read from cache. + +**Why not platform wheels?** Two reasons. First, simpler CI: one `python -m build` job vs a 5-platform matrix that has to download libs zips and rebuild wheels per platform. Second, the libs are already published as the source of truth (existing `release-nodejs-libs` job) — wheels would be a wrapper around those, adding nothing but build complexity. Tradeoff: first run requires internet; air-gap users set `SIMPLEX_LIBS_DIR=/path/to/libs`. + +### Cache layout + +``` +~/.cache/simplex-chat/ # XDG_CACHE_HOME on Linux +└── v6.5.1/ # = LIBS_VERSION + ├── sqlite/libsimplex.so + libHS*.so + └── postgres/libsimplex.so + libHS*.so +``` + +Platform-specific cache base: Linux `$XDG_CACHE_HOME`, macOS `~/Library/Caches/`, Windows `%LOCALAPPDATA%`. + +### Version pinning + +`src/simplex_chat/_version.py`: + +```python +__version__ = "6.5.1" # PEP 440 — bumped with each Python package release +LIBS_VERSION = "6.5.1" # simplex-chat-libs release tag (no 'v' prefix) +``` + +`__version__` is read by hatchling for wheel metadata. `LIBS_VERSION` is read by `_native.py` for the download URL. Wrapper-only patch releases use a post-suffix (`__version__ = "6.5.1.post1"`, `LIBS_VERSION` unchanged). Same pattern as Node lib's `RELEASE_TAG = 'v6.5.1'`. + +### `_native.py` responsibilities + +1. **Detect platform.** `sys.platform` × `platform.machine()` → `linux-x86_64`, `linux-aarch64`, `macos-x86_64`, `macos-aarch64`, `windows-x86_64`. Unsupported combos raise immediately. +2. **Resolve backend** from the `Db` instance (`isinstance(db, SqliteDb)` → sqlite, `PostgresDb` → postgres). Module-level `threading.Lock` guards selection — first call wins for the process; subsequent call with a different backend raises (one libsimplex variant per process — Haskell RTS constraint). +3. **Resolve libs path.** If `SIMPLEX_LIBS_DIR` env is set, use it directly. Otherwise: `~/.cache/simplex-chat/v{LIBS_VERSION}/{backend}/`, downloading if missing. +4. **Download URL** (`LIBS_VERSION` is stored without 'v' prefix; URL re-adds it): + + ``` + https://github.com/simplex-chat/simplex-chat-libs/releases/download/v{LIBS_VERSION}/simplex-chat-libs-{platform}-{arch}{-postgres?}.zip + ``` + +5. **Atomic install.** Download to sibling tempdir → `zipfile.extractall` → `os.replace` the `libs/` subdir into cache. The libs zip contains only regular files (no symlinks — `build.yml:751-781` builds it via `cp *.so` which resolves them), so plain `extractall` is sufficient. Two processes racing safely both extract; whichever rename lands first wins, contents identical. +6. **Load and init.** `ctypes.CDLL(libs_dir / libname)` once per process; declare `restype`/`argtypes` for the 8 FFI functions; call `hs_init_with_rtsopts` exactly once with `+RTS -A64m -H64m -xn --install-signal-handlers=no` (or without `-xn` on Windows — matches `cpp/simplex.cc:13-32`). +7. **Buffer ownership.** Haskell allocates result strings; caller must `free()` after copying. Declare `restype = c_void_p` (NOT `c_char_p`, which auto-converts to bytes and discards the pointer needed for free): + + ```python + ptr = lib.chat_send_cmd(ctrl, cmd_bytes) + if not ptr: raise RuntimeError("null result") + try: result = ctypes.string_at(ptr).decode("utf-8") + finally: libc.free(ptr) + ``` + + `libc` is `ctypes.CDLL(None)` on Linux/macOS, `ctypes.CDLL("msvcrt")` on Windows. Mirrors `HandleCResult` in `cpp/simplex.cc:157-165`. + +### Override / pre-fetch + +```bash +# Skip download — for Docker / air-gapped +SIMPLEX_LIBS_DIR=/opt/simplex/libs python my_bot.py + +# Pre-fetch in Dockerfile RUN step (avoids redundant download per container start) +python -m simplex_chat install --backend=sqlite +python -m simplex_chat install --backend=postgres +``` + +### Failure modes + +| Condition | Behavior | +|---|---| +| Unsupported platform/arch | Raise on first FFI call with explicit list of supported combinations. | +| Postgres on non-Linux-x86_64 | Raise — matches existing constraint in `download-libs.js:15-18`. | +| Download network/HTTP error | Propagate `urllib.error.URLError` with the URL. | +| Two processes downloading simultaneously | Both extract to sibling temp dirs; rename is atomic; identical contents → safe. | +| Two `Bot()` / `ChatApi.init()` calls in same process with different backends | Second raises — one libsimplex variant per process. | +| Two `Bot()` instances same backend, same process | Permitted — each has its own controller (`chat_ctrl`). | + +## Public API + +See the [Architecture](#architecture) section for the layering. This section specifies each layer's surface. + +### Construction + +User-facing config types are `@dataclass(slots=True)`, snake_case fields. + +```python +@dataclass(slots=True) +class SqliteDb: + file_prefix: str + encryption_key: str | None = None + +@dataclass(slots=True) +class PostgresDb: + connection_string: str + schema_prefix: str | None = None + +Db = SqliteDb | PostgresDb # discriminated by isinstance() + +@dataclass(slots=True) +class BotProfile: + display_name: str + full_name: str = "" + short_descr: str | None = None + image: str | None = None + +@dataclass(slots=True) +class BotCommand: + keyword: str + label: str + +class Bot: + def __init__( + self, *, + profile: BotProfile, + db: Db, + welcome: str | T.MsgContent | None = None, + commands: list[BotCommand] | None = None, # None → [] + confirm_migrations: MigrationConfirmation = MigrationConfirmation.YES_UP, + # behavioral toggles — mirror BotOptions in Node lib + create_address: bool = True, + update_address: bool = True, + update_profile: bool = True, + auto_accept: bool = True, + business_address: bool = False, + allow_files: bool = False, + use_bot_profile: bool = True, + log_contacts: bool = True, + log_network: bool = False, + ) -> None: ... + + @property + def api(self) -> ChatApi: ... +``` + +### Handler registration + +Three decorators. Filters are kwargs combined with **AND**; tuples within a kwarg are **OR**; arbitrary predicates use `when=`. + +```python +class Bot: + def on_message(self, *, + content_type: T.MsgContent_Tag | tuple[T.MsgContent_Tag, ...] | None = None, + text: str | re.Pattern | None = None, # exact match or regex.search() + chat_type: T.ChatType | tuple[T.ChatType, ...] | None = None, # direct/group/local + from_role: T.GroupMemberRole | tuple[T.GroupMemberRole, ...] | None = None, + from_contact_id: int | tuple[int, ...] | None = None, + from_member_id: int | tuple[int, ...] | None = None, + group_id: int | tuple[int, ...] | None = None, + when: Callable[[Message], bool] | None = None, + ) -> Callable[[MessageHandler], MessageHandler]: ... + + def on_command(self, name: str | tuple[str, ...], *, + args: str | re.Pattern | None = None, # match command argument string + chat_type: T.ChatType | tuple[T.ChatType, ...] | None = None, + from_role: T.GroupMemberRole | tuple[T.GroupMemberRole, ...] | None = None, + from_contact_id: int | tuple[int, ...] | None = None, + group_id: int | tuple[int, ...] | None = None, + when: Callable[[Message], bool] | None = None, + ) -> Callable[[CommandHandler], CommandHandler]: ... + + # Multiple handlers per tag dispatch in registration order. + def on_event(self, event: CEvt.Tag, /, + ) -> Callable[[EventHandler], EventHandler]: ... + + def use(self, middleware: Middleware) -> None: ... + +MessageHandler = Callable[[Message], Awaitable[None] | None] +CommandHandler = Callable[[Message, ParsedCommand], Awaitable[None] | None] +EventHandler = Callable[[CEvt.ChatEvent], Awaitable[None] | None] +``` + +`from_role` on direct chats: silent skip (not a runtime error). + +### Message wrapper and content-narrowed types + +`Message[C]` is generic in its content variant; concrete subclass aliases (`TextMessage`, `ImageMessage`, …) bind to the auto-generated `T.MsgContent_*` types. Decorator overloads narrow the handler parameter when `content_type` is a single `Literal`, so pyright sees the right concrete type. + +```python +C = TypeVar("C", bound=T.MsgContent) # bound covers the unparameterized case + +@dataclass(slots=True, frozen=True) +class Message(Generic[C]): + chat_item: T.AChatItem # raw wire object — fields below this point are camelCase + content: C # narrowed when filter pins content_type + bot: Bot + + @property + def chat_info(self) -> T.ChatInfo: ... # shortcut for chat_item["chatInfo"] + @property + def text(self) -> str | None: ... # shortcut; non-Optional for TextMessage + + async def reply(self, text: str) -> Message: ... + async def reply_content(self, content: T.MsgContent) -> Message: ... + async def react(self, emoji: str) -> None: ... + async def delete(self) -> None: ... + async def forward(self, to: T.ChatRef) -> Message: ... + +# Concrete narrowed aliases — exported from simplex_chat/__init__.py +TextMessage = Message[T.MsgContent_Text] +ImageMessage = Message[T.MsgContent_Image] +FileMessage = Message[T.MsgContent_File] +VoiceMessage = Message[T.MsgContent_Voice] +# … one per MsgContent variant + +@dataclass(slots=True, frozen=True) +class ParsedCommand: + keyword: str + args: str +``` + +Decorator overloads (one per `T.MsgContent_*` variant — ~15 lines, hand-written in `bot.py`): + +```python +class Bot: + @overload + def on_message(self, *, content_type: Literal["text"], **rest: Any + ) -> Callable[[Callable[[TextMessage], Awaitable[None] | None]], ...]: ... + @overload + def on_message(self, *, content_type: Literal["image"], **rest: Any + ) -> Callable[[Callable[[ImageMessage], Awaitable[None] | None]], ...]: ... + # … one overload per MsgContent variant … + @overload + def on_message(self, *, content_type: tuple[T.MsgContent_Tag, ...] | None = None, + **rest: Any) -> Callable[[Callable[[Message], Awaitable[None] | None]], ...]: ... +``` + +`@bot.on_message(content_type="text")` → handler typed as `TextMessage`, so `msg.text: str` (non-Optional). + +**Field-naming boundary in `Message`.** Wrapper properties (`msg.chat_info`, `msg.content`, `msg.text`) are snake_case. Descending into raw wire data via `msg.chat_item[...]` reverts to camelCase — same as accessing `T.AChatItem` returned by `bot.api`. Property shortcuts cover the common paths so most handlers never touch `chat_item` directly. + +### Lifecycle + +```python +class Bot: + # Blocking convenience — runs asyncio.run(self.serve_forever()), installs SIGINT + # via loop.add_signal_handler() (POSIX) or signal.signal() (Windows). Recommended for scripts. + def run(self) -> None: ... + + # Embedding form — caller owns the loop and signal handling. + async def __aenter__(self) -> Bot: ... + async def __aexit__(self, *exc_info: object) -> None: ... + + # Concurrent calls raise RuntimeError("already serving"). Re-callable after a clean stop(). + async def serve_forever(self) -> None: ... + + # Marks bot for shutdown. Safe from signal handler, another coroutine, or another thread. + def stop(self) -> None: ... +``` + +### Middleware + +aiogram pattern. A class with `async __call__(handler, message, data)` wraps every **handler invocation** (per-message-per-matching-handler). `data: dict[str, object]` is the cross-cutting injection channel. + +```python +class Middleware: + async def __call__(self, + handler: Callable[[Message, dict[str, object]], Awaitable[None]], + message: Message, + data: dict[str, object]) -> None: + await handler(message, data) +``` + +- **Invocation count.** A `newChatItems` event with N items × M matching handlers triggers N×M middleware calls. Per-event hooks should use `on_event`. +- **Exception propagation.** Handler exceptions propagate outward through the middleware stack. The outermost middleware can catch and swallow. Uncaught exceptions are logged via `logging.getLogger("simplex_chat")` and the chain moves to the next handler — the bot does not stop on individual handler errors. The receive loop only stops on a fatal `_native`/`core` error or explicit `bot.stop()`. +- **Order.** Registered via `bot.use(...)`. Called in registration order (first registered = outermost wrap). + +### Event-loop semantics + +`Bot` runs one event-receiver coroutine looping `chat_recv_msg_wait` (in `asyncio.to_thread`): + +1. All `on_event(tag)` handlers for the event's `type` field — registration order, sequentially. +2. If event is `newChatItems`: for each chat item, run **all matching message handlers** (each through the middleware stack, in registration order). For each command-parseable text item, also run matching command handlers. + +Handlers run **sequentially within an event**. Events are processed **sequentially**. Long-running work that shouldn't block the next event must `asyncio.create_task(...)` explicitly. + +`bot.api.api_xxx(...)` calls are safe during `serve_forever` — same controller, serialized through `chat_send_cmd`. Calling them from inside a handler is the normal pattern (`msg.reply()` does exactly this). + +### `ChatApi` (escape hatch) + +Reached via `bot.api`. ~40 async methods, one per Node `apiXxx` (api.ts:344-958). Full enumeration deferred to implementation; representative examples: + +```python +class ChatApi: + @classmethod + async def init(cls, db: Db, + confirm: MigrationConfirmation = MigrationConfirmation.YES_UP) -> ChatApi: ... + async def start_chat(self) -> None: ... + async def stop_chat(self) -> None: ... + async def close(self) -> None: ... + async def send_chat_cmd(self, cmd: str) -> CR.ChatResponse: ... + async def recv_chat_event(self, wait_us: int = 5_000_000) -> CEvt.ChatEvent | None: ... + + async def api_create_user_address(self, user_id: int) -> T.CreatedConnLink: ... + async def api_send_text_message(self, chat: T.ChatRef, text: str, + in_reply_to: int | None = None) -> list[T.AChatItem]: ... + async def api_get_chats(self, user_id: int, pagination: T.PaginationByTime, + query: T.ChatListQuery | None = None) -> list[T.AChat]: ... + # ... etc +``` + +TS `apiCreateUserAddress` → Python `api_create_user_address` (PEP 8). Wire-format type names (`T.AChatItem`, `T.UserContactLink`, …) keep their Haskell/TS spelling to match JSON keys. + +### Embedding example + +```python +import asyncio +from simplex_chat import Bot, BotProfile, SqliteDb + +async def main(): + async with Bot(profile=..., db=...) as bot: + @bot.on_message(content_type="text") + async def echo(msg): + await msg.reply(msg.text) + await asyncio.gather(bot.serve_forever(), other_task()) + +asyncio.run(main()) +``` + +## Distribution and CI + +### Project layout + +``` +packages/simplex-chat-python/ +├── pyproject.toml # hatchling, requires-python >= 3.11, no runtime deps +├── README.md +├── LICENSE # AGPL-3.0 +├── src/simplex_chat/ +│ ├── __init__.py # exports Bot, BotProfile, BotCommand, SqliteDb, PostgresDb, +│ │ # Message + TextMessage/ImageMessage/… aliases, ParsedCommand, +│ │ # ChatApi, MigrationConfirmation, Middleware, ChatAPIError +│ ├── _version.py # __version__ + LIBS_VERSION +│ ├── _native.py # ctypes + lazy lib download (internal) +│ ├── __main__.py # python -m simplex_chat install ... +│ ├── core.py # internal typed FFI wrapper +│ ├── api.py # ChatApi class — escape hatch +│ ├── bot.py # Bot class, decorators, Message wrapper, lifecycle +│ ├── filters.py # filter kwarg compilation; predicate combinators +│ ├── util.py # stateless helpers (chat_info_ref, ci_content_text, reaction_text, …) +│ ├── py.typed # PEP 561 marker +│ └── types/ +│ ├── __init__.py # re-exports T, CC, CR, CEvt +│ ├── _types.py # AUTOGEN +│ ├── _commands.py # AUTOGEN +│ ├── _responses.py # AUTOGEN +│ └── _events.py # AUTOGEN +├── examples/ +│ └── squaring_bot.py +└── tests/ +``` + +### `pyproject.toml` + +```toml +[build-system] +requires = ["hatchling>=1.24"] +build-backend = "hatchling.build" + +[project] +name = "simplex-chat" +description = "SimpleX Chat Python library for chat bots" +license = "AGPL-3.0" +authors = [{name = "SimpleX Chat"}] +requires-python = ">=3.11" +dynamic = ["version"] + +[tool.hatch.version] +path = "src/simplex_chat/_version.py" + +[tool.hatch.build.targets.wheel] +packages = ["src/simplex_chat"] +``` + +No runtime Python dependencies (ctypes, urllib, zipfile are stdlib). + +### CI publishing + +One job appended to `.github/workflows/build.yml`, after `release-nodejs-libs`: + +```yaml +publish-python: + needs: [release-nodejs-libs] + if: startsWith(github.ref, 'refs/tags/v') + runs-on: ubuntu-latest + permissions: { id-token: write } # OIDC, no API key + steps: + - uses: actions/checkout@v6 + - uses: actions/setup-python@v5 + with: { python-version: "3.11" } + - run: pip install build && python -m build --wheel + working-directory: packages/simplex-chat-python + - uses: pypa/gh-action-pypi-publish@release/v1 + with: { packages-dir: packages/simplex-chat-python/dist } +``` + +Triggered by the same `vX.Y.Z` tag that already drives the desktop and libs releases. + +### One-time setup + +1. Verify PyPI package name `simplex-chat` is available; register it. +2. On PyPI project page, configure trusted publisher → repo `simplex-chat/simplex-chat`, workflow `build.yml`, job `publish-python`. + +## Testing + +Three levels: + +1. **Codegen drift** — `tests/APIDocs.hs` adds Python generators alongside TypeScript. Same `testGenerate` mechanism enforces that committed `_types.py` etc. equal the generator output. + +2. **Python unit tests** — `pytest`, no real libsimplex needed: + - `test_native.py`: mock `urllib.request.urlretrieve` + `zipfile.ZipFile`; assert correct URL, atomic rename, cache hit on second call, override-env behavior, postgres-on-mac rejection. + - `test_codegen.py`: import every type from `simplex_chat.types`, sanity-check that `T.ChatType` is `Literal[...]` of expected size, etc. Catches generator regressions. + - `test_smoke.py`: build a fake `.so` (small C file with stub `chat_send_cmd` returning canned JSON, compiled per-test), point `SIMPLEX_LIBS_DIR` at it, run `Bot.__aenter__` → handler dispatch. Verifies FFI plumbing without real Haskell. + +3. **Integration** — `examples/squaring_bot.py` runs against real libsimplex. Not in CI (needs network + persistent state). + +## Open questions + +1. **Linux ARM64.** Existing `simplex-chat-libs` releases ship `linux-x86_64`, `macos-x86_64`, `macos-aarch64`, `windows-x86_64` — no `linux-aarch64`. Python lib will fail with a clear message there. Adding it requires changes to the existing `release-nodejs-libs` job in `build.yml` (out of scope for this spec). + +2. **`asyncio.to_thread` pool sizing.** Long-blocking `chat_recv_msg_wait` calls (default 5 s) pin executor threads. The default asyncio pool is unbounded but recycled. Bots running many concurrent chats may need a custom executor — first-pass uses `asyncio.to_thread`; document recommended pool sizing in README if it becomes a problem. diff --git a/plans/2026-05-07-simplex-chat-python-implementation.md b/plans/2026-05-07-simplex-chat-python-implementation.md new file mode 100644 index 0000000000..b1ff5b951c --- /dev/null +++ b/plans/2026-05-07-simplex-chat-python-implementation.md @@ -0,0 +1,2348 @@ +# SimpleX Chat Python library — implementation plan + +> **For agentic workers:** Use superpowers-extended-cc:subagent-driven-development (if subagents available) or superpowers-extended-cc:executing-plans to implement this plan. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Ship `simplex-chat` on PyPI — a Python 3 client library for SimpleX bots with the same capability as the existing Node.js library. + +**Architecture:** Two repositories of work in this monorepo: extend the Haskell type generator (`bots/src/API/Docs/`) to emit Python types alongside TypeScript, and add a new Python package (`packages/simplex-chat-python/`) that wraps the existing prebuilt `libsimplex.{so,dylib,dll}` via ctypes. Lazy download of libs from `simplex-chat/simplex-chat-libs` GitHub releases. Async-only public API with decorator-registered handlers. Single PyPI wheel. + +**Tech Stack:** Haskell (existing codegen), Python 3.11+ (ctypes, asyncio, hatchling), GitHub Actions (publish-python job in existing build.yml). + +**Spec:** [`plans/2026-05-07-simplex-chat-python-design.md`](./2026-05-07-simplex-chat-python-design.md) + +--- + +## Plan structure + +Eight phases, executed in order: + +1. Type generation (Haskell) — `Generate/Python.hs` + `tests/APIDocs.hs` wiring. +2. Python package scaffold — `pyproject.toml`, `_version.py`, layout, hatch config. +3. Native FFI layer — `_native.py` (lazy download, ctypes, hs_init, buffer ownership). +4. Core wrapper — `core.py` (typed async FFI). +5. ChatApi — escape-hatch class with raw + ~40 high-level methods. +6. Bot class — decorators, filters, Message wrapper, lifecycle, middleware. +7. Tests + CLI — pytest suite, `python -m simplex_chat install`. +8. CI publishing — append `publish-python` job to `.github/workflows/build.yml`. + +Each phase is a chunk. Phase boundaries are natural review points. + +--- + +## Chunk 1: Type generation (Haskell) + +Add `bots/src/API/Docs/Generate/Python.hs`, mirror `Generate/TypeScript.hs`, wire into the test suite. + +### Task 1.1: Create `Generate/Python.hs` skeleton + +**Files:** +- Create: `bots/src/API/Docs/Generate/Python.hs` + +- [ ] **Step 1: Copy `Generate/TypeScript.hs` as starting point** + +```bash +cp bots/src/API/Docs/Generate/TypeScript.hs bots/src/API/Docs/Generate/Python.hs +``` + +- [ ] **Step 2: Rename module and update output paths** + +Edit the new file: +- Module: `module API.Docs.Generate.Python where` +- File constants: + ```haskell + commandsCodeFile = "./packages/simplex-chat-python/src/simplex_chat/types/_commands.py" + responsesCodeFile = "./packages/simplex-chat-python/src/simplex_chat/types/_responses.py" + eventsCodeFile = "./packages/simplex-chat-python/src/simplex_chat/types/_events.py" + typesCodeFile = "./packages/simplex-chat-python/src/simplex_chat/types/_types.py" + ``` + +- [ ] **Step 3: Register module in cabal manifest** + +In `simplex-chat.cabal`, find the `other-modules:` list of the `simplex-chat-test` test-suite stanza (`type: exitcode-stdio-1.0`, `main-is: Test.hs`). Insert `API.Docs.Generate.Python` alphabetically between `API.Docs.Generate` and `API.Docs.Responses`. + +- [ ] **Step 4: Verify cabal compiles** + +``` +cabal build simplex-chat-test +``` +Expected: builds without error (the file is still TS-shaped but Haskell-valid). + +- [ ] **Step 5: Commit scaffolding** + +```bash +git add bots/src/API/Docs/Generate/Python.hs simplex-chat.cabal +git commit -m "feat(bots): add Python codegen module skeleton" +``` + +### Task 1.2: Implement Python type rendering + +Replace TypeScript-specific output with Python equivalents per the spec's [type-mapping rules](./2026-05-07-simplex-chat-python-design.md#type-representation). + +**Files:** +- Modify: `bots/src/API/Docs/Generate/Python.hs` + +- [ ] **Step 1: Rewrite `typesCodeText`** + +Output structure: +```python +# API Types +# This file is generated automatically. +from typing import Literal, NotRequired, TypedDict + +# … one class / type alias per chatTypesDocs entry … +``` + +Translation rules (mirror `TypeScript.hs` `typeCode`): +- `ATDRecord fields` → `class (TypedDict):` with translated field types. +- `ATDEnum cs` → ` = Literal["c1", "c2", …]`. +- `ATDUnion cs` → tagged TypedDicts + union alias + tag alias (see `unionTypeCode` in TS). + +Field-type translation (table in spec; mirrors `TypeScript.hs` `fieldsCode` `typeText`): +- `ATPrim` primitives → Python primitives (`bool`, `str`, `int`, `float`, `dict[str, object]`). +- `ATOptional t` inside TypedDict → `NotRequired[]`; elsewhere ` | None`. +- `ATArray {elemType, nonEmpty}` → `list[]`, append `# non-empty` comment when `nonEmpty=True`. +- `ATMap (PT k) v` → `dict[, ]`. +- `ATDef` / `ATRef` → forward-string reference `""`. + +- [ ] **Step 2: Rewrite `responsesCodeText` and `eventsCodeText`** + +Both produce union types — same shape as TS's `unionTypeCode`. Output structure: + +```python +# API Responses +# This file is generated automatically. +from . import _types as T + +ChatResponse_ = TypedDict(...) +ChatResponse_ = TypedDict(...) +… +ChatResponse = ChatResponse_ | ChatResponse_ | … +ChatResponse_Tag = Literal["", "", …] +``` + +- [ ] **Step 3: Rewrite `commandsCodeText`** + +Each command becomes a TypedDict + a `_cmd_string(self) -> str` function + a Response alias. Function body comes from `pySyntaxText` in `Syntax.hs:160` (no changes to that function — it's already correct and used today by Markdown docs). + +```python +# API Commands +# This file is generated automatically. +import json +from typing import TypedDict +from . import _types as T +from . import _responses as CR + +class APICreateMyAddress(TypedDict): + userId: int + +def APICreateMyAddress_cmd_string(self: APICreateMyAddress) -> str: + return '/_address ' + str(self['userId']) + +APICreateMyAddress_Response = CR.UserContactLinkCreated | CR.ChatCmdError +``` + +The `cmdString` body: invoke `pySyntaxText (constrName, params) syntax` analogously to TS's `funcCode`. + +- [ ] **Step 4: Build to verify Haskell compiles** + +``` +cabal build simplex-chat-test +``` +Expected: clean build. + +- [ ] **Step 5: Commit** + +```bash +git add bots/src/API/Docs/Generate/Python.hs +git commit -m "feat(bots): implement Python type generation" +``` + +### Task 1.3: Wire generators into `tests/APIDocs.hs` + +**Files:** +- Modify: `tests/APIDocs.hs` + +- [ ] **Step 1: Add import** + +Insert after line 11 (`import qualified API.Docs.Generate.TypeScript as TS`): + +```haskell +import qualified API.Docs.Generate.Python as Py +``` + +- [ ] **Step 2: Add four `testGenerate` calls** + +Inside `apiDocsTest`, after the existing `describe "TypeScript"` block (line 40-44), add: + +```haskell +describe "Python" $ do + it "generate python commands code" $ testGenerate Py.commandsCodeFile Py.commandsCodeText + it "generate python responses code" $ testGenerate Py.responsesCodeFile Py.responsesCodeText + it "generate python events code" $ testGenerate Py.eventsCodeFile Py.eventsCodeText + it "generate python types code" $ testGenerate Py.typesCodeFile Py.typesCodeText +``` + +- [ ] **Step 3: Create empty target directory** + +```bash +mkdir -p packages/simplex-chat-python/src/simplex_chat/types +``` + +- [ ] **Step 4: Run the API docs tests — they will write the four files** + +``` +cabal test simplex-chat-test --test-options="--match \"API\"" +``` + +First run: tests fail because the on-disk files are empty / missing. The `testGenerate` mechanism overwrites the file with generated content, so the second run passes. + +``` +cabal test simplex-chat-test --test-options="--match \"Python\"" +``` + +Expected: PASS on the second run. + +- [ ] **Step 5: Sanity-check generated output** + +Eyeball each of the four generated files: + +```bash +head -50 packages/simplex-chat-python/src/simplex_chat/types/_types.py +head -30 packages/simplex-chat-python/src/simplex_chat/types/_commands.py +head -30 packages/simplex-chat-python/src/simplex_chat/types/_responses.py +head -30 packages/simplex-chat-python/src/simplex_chat/types/_events.py +``` + +Verify: starts with `# This file is generated automatically.`, contains valid-looking Python, no obvious junk like `` markers. + +- [ ] **Step 6: Run them through Python parser to verify syntax** + +``` +python -c "import ast; [ast.parse(open(f).read()) for f in ['packages/simplex-chat-python/src/simplex_chat/types/_types.py', 'packages/simplex-chat-python/src/simplex_chat/types/_commands.py', 'packages/simplex-chat-python/src/simplex_chat/types/_responses.py', 'packages/simplex-chat-python/src/simplex_chat/types/_events.py']]" +``` + +Expected: no exception. Any `SyntaxError` indicates a generator bug — fix in `Generate/Python.hs` and re-run cabal test. + +- [ ] **Step 7: Commit generated artifacts** + +```bash +git add tests/APIDocs.hs packages/simplex-chat-python/src/simplex_chat/types/ +git commit -m "feat(bots): wire Python generators into APIDocs test suite" +``` + +### Task 1.4: Add `types/__init__.py` re-exporting namespaces + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/types/__init__.py` + +- [ ] **Step 1: Write namespace re-exports** + +```python +from . import _types as T +from . import _commands as CC +from . import _responses as CR +from . import _events as CEvt + +__all__ = ["T", "CC", "CR", "CEvt"] +``` + +- [ ] **Step 2: Verify import works** + +``` +python -c "from simplex_chat.types import T, CC, CR, CEvt; print(T, CC, CR, CEvt)" +``` + +(Run from `packages/simplex-chat-python/src/`.) + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/types/__init__.py +git commit -m "feat(python): add types namespace re-exports" +``` + +--- + +## Chunk 2: Python package scaffold + +Set up the package skeleton — `pyproject.toml`, version pinning, top-level `__init__.py`, AGPL license, README placeholder. + +### Task 2.1: Create `pyproject.toml` + +**Files:** +- Create: `packages/simplex-chat-python/pyproject.toml` + +- [ ] **Step 1: Write hatchling build config** + +```toml +[build-system] +requires = ["hatchling>=1.24"] +build-backend = "hatchling.build" + +[project] +name = "simplex-chat" +description = "SimpleX Chat Python library for chat bots" +readme = "README.md" +license = "AGPL-3.0-only" +authors = [{name = "SimpleX Chat"}] +requires-python = ">=3.11" +keywords = ["simplex", "messenger", "chat", "privacy", "security", "bots"] +classifiers = [ + "Development Status :: 4 - Beta", + "License :: OSI Approved :: GNU Affero General Public License v3", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Topic :: Communications :: Chat", +] +dynamic = ["version"] + +[project.urls] +Homepage = "https://github.com/simplex-chat/simplex-chat/tree/stable/packages/simplex-chat-python" +Issues = "https://github.com/simplex-chat/simplex-chat/issues" + +[project.optional-dependencies] +test = ["pytest>=8", "pytest-asyncio>=0.23"] +dev = ["pytest>=8", "pytest-asyncio>=0.23", "pyright>=1.1.380", "ruff>=0.6"] + +[tool.hatch.version] +path = "src/simplex_chat/_version.py" + +[tool.hatch.build.targets.wheel] +packages = ["src/simplex_chat"] + +[tool.pytest.ini_options] +asyncio_mode = "auto" +``` + +- [ ] **Step 2: Commit** + +```bash +git add packages/simplex-chat-python/pyproject.toml +git commit -m "feat(python): add pyproject.toml with hatchling backend" +``` + +### Task 2.2: Create `_version.py` + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/_version.py` + +- [ ] **Step 1: Write version constants** + +```python +"""Single source of truth for both the Python package version and the +simplex-chat-libs release tag we depend on. + +Bump both together for normal releases. For wrapper-only fixes use a PEP 440 +post-release: __version__ = "6.5.1.post1", LIBS_VERSION unchanged. +""" + +__version__ = "6.5.1" # PEP 440 — read by hatchling for wheel metadata +LIBS_VERSION = "6.5.1" # simplex-chat-libs release tag (no 'v' prefix) +``` + +- [ ] **Step 2: Verify hatchling can read the version** + +``` +cd packages/simplex-chat-python && python -m build --wheel +``` + +Expected: produces `dist/simplex_chat-6.5.1-py3-none-any.whl`. (Wheel will be incomplete — only the types module is in src/ at this point — but build should succeed.) + +Clean up: `rm -rf packages/simplex-chat-python/dist packages/simplex-chat-python/src/simplex_chat.egg-info` + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/_version.py +git commit -m "feat(python): add _version.py with package + libs version pinning" +``` + +### Task 2.3: Add `py.typed` marker, README placeholder, AGPL license + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/py.typed` +- Create: `packages/simplex-chat-python/README.md` +- Create: `packages/simplex-chat-python/LICENSE` + +- [ ] **Step 1: Touch the empty `py.typed` marker** + +```bash +touch packages/simplex-chat-python/src/simplex_chat/py.typed +``` + +- [ ] **Step 2: Copy AGPL license from a sibling package** + +```bash +cp packages/simplex-chat-nodejs/LICENSE packages/simplex-chat-python/LICENSE +``` + +- [ ] **Step 3: Write a minimal README** + +```markdown +# SimpleX Chat Python library + +Python 3 client library for [SimpleX Chat](https://simplex.chat) bots. + +Equivalent to the [Node.js library](https://www.npmjs.com/package/simplex-chat). + +## Installation + + pip install simplex-chat + +Requires Python 3.11+. + +## Quick start + +[example to be added] + +## License + +[AGPL-3.0](./LICENSE) +``` + +- [ ] **Step 4: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/py.typed \ + packages/simplex-chat-python/README.md \ + packages/simplex-chat-python/LICENSE +git commit -m "feat(python): add py.typed marker, README, AGPL license" +``` + +### Task 2.4: Top-level `__init__.py` with empty exports + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/__init__.py` + +- [ ] **Step 1: Write a placeholder that re-exports from submodules as they appear** + +```python +"""SimpleX Chat — Python client library for chat bots.""" + +from ._version import __version__ + +__all__ = ["__version__"] +``` + +(Will be expanded as `Bot`, `ChatApi`, etc. land in later phases.) + +- [ ] **Step 2: Verify import** + +``` +python -c "import simplex_chat; print(simplex_chat.__version__)" +``` + +Run from `packages/simplex-chat-python/src/`. + +Expected: prints `6.5.1`. + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/__init__.py +git commit -m "feat(python): add top-level package __init__.py" +``` + +--- + +## Chunk 3: Native FFI layer (`_native.py`) + +Lazy lib download, platform detection, ctypes signatures, `hs_init_with_rtsopts`, atomic install, buffer ownership. Single most-error-prone piece of the package — give it tests. + +### Task 3.1: `_native.py` — platform detection + URL building + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/_native.py` +- Create: `packages/simplex-chat-python/tests/test_native_url.py` + +- [ ] **Step 1: Write platform-detection tests** + +```python +# tests/test_native_url.py +from unittest.mock import patch +import pytest +from simplex_chat._native import _platform_tag, _libs_url, _libname + +@patch("sys.platform", "linux") +@patch("platform.machine", return_value="x86_64") +def test_platform_linux_x64(_): + assert _platform_tag() == "linux-x86_64" + +@patch("sys.platform", "darwin") +@patch("platform.machine", return_value="arm64") +def test_platform_macos_arm64(_): + assert _platform_tag() == "macos-aarch64" + +@patch("sys.platform", "win32") +@patch("platform.machine", return_value="AMD64") +def test_platform_windows_x64(_): + assert _platform_tag() == "windows-x86_64" + +@patch("sys.platform", "freebsd") +@patch("platform.machine", return_value="x86_64") +def test_platform_unsupported(_): + with pytest.raises(RuntimeError, match="Unsupported"): + _platform_tag() + +def test_libname_per_platform(): + with patch("sys.platform", "linux"): + assert _libname() == "libsimplex.so" + with patch("sys.platform", "darwin"): + assert _libname() == "libsimplex.dylib" + with patch("sys.platform", "win32"): + assert _libname() == "libsimplex.dll" + +@patch("simplex_chat._native._platform_tag", return_value="linux-x86_64") +def test_url_sqlite(_): + assert _libs_url("sqlite") == \ + "https://github.com/simplex-chat/simplex-chat-libs/releases/download/" \ + "v6.5.1/simplex-chat-libs-linux-x86_64.zip" + +@patch("simplex_chat._native._platform_tag", return_value="linux-x86_64") +def test_url_postgres(_): + assert _libs_url("postgres") == \ + "https://github.com/simplex-chat/simplex-chat-libs/releases/download/" \ + "v6.5.1/simplex-chat-libs-linux-x86_64-postgres.zip" +``` + +- [ ] **Step 2: Write `_native.py` skeleton with platform + URL helpers** + +```python +"""Native libsimplex loader: platform detection, lazy download, ctypes setup. + +Internal — users interact with `Bot` / `ChatApi`, never with this module. +""" +from __future__ import annotations + +import ctypes +import errno +import os +import platform +import sys +import tempfile +import threading +import urllib.request +import zipfile +from ctypes import POINTER, c_char_p, c_int, c_uint8, c_void_p +from pathlib import Path +from typing import Literal + +from ._version import LIBS_VERSION + +Backend = Literal["sqlite", "postgres"] + +_GITHUB_REPO = "simplex-chat/simplex-chat-libs" + +_PLATFORM_MAP = { + "linux": ("linux", {"x86_64": "x86_64", "aarch64": "aarch64"}), + "darwin": ("macos", {"x86_64": "x86_64", "arm64": "aarch64"}), + "win32": ("windows", {"AMD64": "x86_64", "x86_64": "x86_64"}), +} + +_LIBNAME = {"linux": "libsimplex.so", "darwin": "libsimplex.dylib", "win32": "libsimplex.dll"} + +SUPPORTED = ( + "linux-x86_64", "linux-aarch64", + "macos-x86_64", "macos-aarch64", + "windows-x86_64", +) + + +def _platform_tag() -> str: + info = _PLATFORM_MAP.get(sys.platform) + if not info: + raise RuntimeError(f"Unsupported platform: {sys.platform}") + sysname, archs = info + arch = archs.get(platform.machine()) + if not arch: + raise RuntimeError(f"Unsupported architecture: {sys.platform}/{platform.machine()}") + tag = f"{sysname}-{arch}" + if tag not in SUPPORTED: + raise RuntimeError(f"Unsupported combination: {tag}; supported: {SUPPORTED}") + return tag + + +def _libname() -> str: + return _LIBNAME[sys.platform] + + +def _libs_url(backend: Backend) -> str: + suffix = "-postgres" if backend == "postgres" else "" + return ( + f"https://github.com/{_GITHUB_REPO}/releases/download/" + f"v{LIBS_VERSION}/simplex-chat-libs-{_platform_tag()}{suffix}.zip" + ) +``` + +- [ ] **Step 3: Run tests** + +``` +cd packages/simplex-chat-python && pip install -e . && pip install pytest +PYTHONPATH=src pytest tests/test_native_url.py -v +``` + +Expected: all PASS. + +- [ ] **Step 4: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/_native.py \ + packages/simplex-chat-python/tests/test_native_url.py +git commit -m "feat(python): _native platform detection + URL building" +``` + +### Task 3.2: `_native.py` — cache resolution + lazy download + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/_native.py` +- Create: `packages/simplex-chat-python/tests/test_native_cache.py` + +- [ ] **Step 1: Write cache-resolution tests** + +```python +# tests/test_native_cache.py +import zipfile +from pathlib import Path + +import pytest + +from simplex_chat._native import _cache_root, _resolve_libs_dir, _download + + +def test_cache_root_linux(tmp_path, monkeypatch): + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path)) + monkeypatch.setattr("sys.platform", "linux") + assert _cache_root() == tmp_path / "simplex-chat" + +def test_cache_root_macos(tmp_path, monkeypatch): + monkeypatch.setattr("sys.platform", "darwin") + monkeypatch.setattr("pathlib.Path.home", lambda: tmp_path) + assert _cache_root() == tmp_path / "Library" / "Caches" / "simplex-chat" + +def test_override_via_env(tmp_path, monkeypatch): + # _resolve_libs_dir intentionally does not validate the override directory — + # it returns it verbatim; the eventual ctypes.CDLL call surfaces any mistake. + monkeypatch.setenv("SIMPLEX_LIBS_DIR", str(tmp_path)) + monkeypatch.setattr("sys.platform", "linux") + assert _resolve_libs_dir("sqlite") == tmp_path + +def test_resolve_downloads_when_missing(tmp_path, monkeypatch): + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path)) + monkeypatch.setattr("sys.platform", "linux") + monkeypatch.setattr("simplex_chat._native._platform_tag", lambda: "linux-x86_64") + + called = {} + def fake_download(target_root: Path, backend: str) -> None: + called["target"] = target_root + called["backend"] = backend + target_root.mkdir(parents=True, exist_ok=True) + (target_root / "libsimplex.so").touch() + + monkeypatch.setattr("simplex_chat._native._download", fake_download) + libs_dir = _resolve_libs_dir("sqlite") + assert libs_dir == tmp_path / "simplex-chat" / "v6.5.1" / "sqlite" + assert called["backend"] == "sqlite" + assert (libs_dir / "libsimplex.so").exists() + +def test_resolve_uses_cache_on_second_call(tmp_path, monkeypatch): + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path)) + monkeypatch.setattr("sys.platform", "linux") + cached = tmp_path / "simplex-chat" / "v6.5.1" / "sqlite" + cached.mkdir(parents=True) + (cached / "libsimplex.so").touch() + # Should NOT call _download — use the cached file. + monkeypatch.setattr("simplex_chat._native._download", + lambda *a: pytest.fail("download should not be called")) + assert _resolve_libs_dir("sqlite") == cached + +def test_postgres_on_macos_rejected(monkeypatch): + monkeypatch.setattr("sys.platform", "darwin") + monkeypatch.setattr("simplex_chat._native._platform_tag", lambda: "macos-aarch64") + with pytest.raises(RuntimeError, match="postgres.*linux-x86_64"): + _resolve_libs_dir("postgres") + +def test_atomic_install(tmp_path, monkeypatch): + """Build a fake libs zip, mock urlretrieve, verify extraction + atomic rename.""" + # Build zip: libs/libsimplex.so + libs/libHS-stub.so + src = tmp_path / "src" / "libs" + src.mkdir(parents=True) + (src / "libsimplex.so").write_text("fake-so") + (src / "libHS-stub.so").write_text("fake-hs") + zip_path = tmp_path / "fake-libs.zip" + with zipfile.ZipFile(zip_path, "w") as zf: + for f in src.iterdir(): + zf.write(f, f"libs/{f.name}") + + def fake_urlretrieve(url: str, dest: str) -> None: + import shutil + shutil.copy(zip_path, dest) + + monkeypatch.setattr("urllib.request.urlretrieve", fake_urlretrieve) + monkeypatch.setattr("simplex_chat._native._platform_tag", lambda: "linux-x86_64") + + target = tmp_path / "out" + _download(target, "sqlite") + assert (target / "libsimplex.so").read_text() == "fake-so" + assert (target / "libHS-stub.so").read_text() == "fake-hs" +``` + +- [ ] **Step 2: Implement `_cache_root`, `_resolve_libs_dir`, `_download`** + +Append to `_native.py`: + +```python +def _cache_root() -> Path: + if sys.platform == "darwin": + return Path.home() / "Library" / "Caches" / "simplex-chat" + if sys.platform == "win32": + return Path(os.environ["LOCALAPPDATA"]) / "simplex-chat" + base = os.environ.get("XDG_CACHE_HOME") or str(Path.home() / ".cache") + return Path(base) / "simplex-chat" + + +def _resolve_libs_dir(backend: Backend) -> Path: + if override := os.environ.get("SIMPLEX_LIBS_DIR"): + return Path(override) + if backend == "postgres" and _platform_tag() != "linux-x86_64": + raise RuntimeError( + "postgres backend is only supported on linux-x86_64; " + f"current platform is {_platform_tag()}" + ) + target = _cache_root() / f"v{LIBS_VERSION}" / backend + if not (target / _libname()).exists(): + _download(target, backend) + return target + + +def _download(target: Path, backend: Backend) -> None: + """Download libs zip → atomic rename into `target`. Concurrent processes safe. + + Atomicity strategy: each process extracts to its own sibling tempdir on the same + filesystem, then `os.rename` the `libs/` subdir to `target`. POSIX `os.rename` + onto a NON-EXISTENT path is atomic; if the target exists (another process won + the race), `os.rename` fails on most platforms — we then verify the winner has + what we need and proceed. NEVER rmtree the target: that creates a TOCTOU + window where another process is reading/loading the file we're deleting. + """ + target.parent.mkdir(parents=True, exist_ok=True) + print( + f"Downloading libsimplex ({_platform_tag()}, {backend}) " + f"v{LIBS_VERSION} ...", + file=sys.stderr, + flush=True, + ) + with tempfile.TemporaryDirectory(dir=target.parent) as tmp: + zip_path = Path(tmp) / "libs.zip" + urllib.request.urlretrieve(_libs_url(backend), zip_path) + with zipfile.ZipFile(zip_path) as zf: + zf.extractall(tmp) + # zip layout: /libs/libsimplex.* + libHS*.* + extracted_libs = Path(tmp) / "libs" + if not extracted_libs.is_dir(): + raise RuntimeError(f"libs/ missing from {_libs_url(backend)}") + try: + os.rename(extracted_libs, target) + except OSError as e: + # EEXIST / ENOTEMPTY mean another process won the race — fall through + # and check that the winner left a usable libsimplex behind. Anything + # else (ENOSPC, EACCES, EROFS, Windows codes mapped to None) is a real + # failure and must propagate. Same VERSION cached → same content → + # safe to proceed once we've confirmed the file is there. + if e.errno not in (errno.EEXIST, errno.ENOTEMPTY): + raise + if not (target / _libname()).exists(): + raise RuntimeError( + f"another process partially populated {target} but libsimplex " + f"is missing; remove the directory manually and retry" + ) from e +``` + +- [ ] **Step 3: Run tests** + +``` +PYTHONPATH=src pytest tests/test_native_cache.py -v +``` + +Expected: all PASS. + +- [ ] **Step 4: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/_native.py \ + packages/simplex-chat-python/tests/test_native_cache.py +git commit -m "feat(python): _native cache resolution and lazy download" +``` + +### Task 3.3: `_native.py` — ctypes signatures, `hs_init`, lib loader + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/_native.py` + +- [ ] **Step 1: Append the loader** + +(Imports for `ctypes`, `threading`, and the `from ctypes import …` line were already hoisted to the top of `_native.py` in Task 3.1 — do not re-add them here.) + +```python +_lock = threading.Lock() +_lib: ctypes.CDLL | None = None +_libc: ctypes.CDLL | None = None +_backend: Backend | None = None + + +def _load_libc() -> ctypes.CDLL: + if sys.platform == "win32": + return ctypes.CDLL("msvcrt") + return ctypes.CDLL(None) # libc on POSIX is the process's own symbol table + + +def _setup_signatures(lib: ctypes.CDLL) -> None: + """Declare argtypes/restype for the 8 chat_* functions exported by libsimplex. + + All result strings come back as raw c_void_p so the caller can free them + after copying — matches HandleCResult in cpp/simplex.cc:157-165. + """ + lib.chat_migrate_init.argtypes = [c_char_p, c_char_p, c_char_p, POINTER(c_void_p)] + lib.chat_migrate_init.restype = c_void_p + lib.chat_close_store.argtypes = [c_void_p] + lib.chat_close_store.restype = c_void_p + lib.chat_send_cmd.argtypes = [c_void_p, c_char_p] + lib.chat_send_cmd.restype = c_void_p + lib.chat_recv_msg_wait.argtypes = [c_void_p, c_int] + lib.chat_recv_msg_wait.restype = c_void_p + lib.chat_write_file.argtypes = [c_void_p, c_char_p, POINTER(c_uint8), c_int] + lib.chat_write_file.restype = c_void_p + lib.chat_read_file.argtypes = [c_char_p, c_char_p, c_char_p] + lib.chat_read_file.restype = POINTER(c_uint8) + lib.chat_encrypt_file.argtypes = [c_void_p, c_char_p, c_char_p] + lib.chat_encrypt_file.restype = c_void_p + lib.chat_decrypt_file.argtypes = [c_char_p, c_char_p, c_char_p, c_char_p] + lib.chat_decrypt_file.restype = c_void_p + + +def _hs_init(lib: ctypes.CDLL) -> None: + """Initialize the Haskell runtime exactly once. Mirrors cpp/simplex.cc:13-32.""" + if sys.platform == "win32": + argv_strs = [b"simplex", b"+RTS", b"-A64m", b"-H64m", b"--install-signal-handlers=no"] + else: + argv_strs = [b"simplex", b"+RTS", b"-A64m", b"-H64m", b"-xn", b"--install-signal-handlers=no"] + argc = c_int(len(argv_strs)) + arr = (c_char_p * (len(argv_strs) + 1))(*argv_strs, None) + arr_ptr = ctypes.byref(ctypes.cast(arr, POINTER(c_char_p))) + lib.hs_init_with_rtsopts.argtypes = [POINTER(c_int), POINTER(POINTER(c_char_p))] + lib.hs_init_with_rtsopts.restype = None + lib.hs_init_with_rtsopts(ctypes.byref(argc), arr_ptr) + + +def lib_for(backend: Backend) -> ctypes.CDLL: + """Resolve, load, and initialize libsimplex for the given backend. + + Idempotent for the same backend; raises if called with a different backend. + Concurrent calls serialize on the module-level lock. + """ + global _lib, _libc, _backend + with _lock: + if _lib is not None: + if _backend != backend: + raise RuntimeError( + f"libsimplex already loaded with backend={_backend!r}; " + f"cannot switch to {backend!r} in the same process" + ) + return _lib + libs_dir = _resolve_libs_dir(backend) + lib = ctypes.CDLL(str(libs_dir / _libname())) + _setup_signatures(lib) + _hs_init(lib) + _libc = _load_libc() + _lib = lib + _backend = backend + return lib + + +def libc() -> ctypes.CDLL: + """libc — needed by `core` to free Haskell-allocated result strings.""" + if _libc is None: + raise RuntimeError("lib_for() must be called before libc()") + return _libc +``` + +- [ ] **Step 2: Sanity-check imports** + +``` +PYTHONPATH=src python -c "import simplex_chat._native; print('ok')" +``` + +Expected: prints `ok`. + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/_native.py +git commit -m "feat(python): _native ctypes signatures, hs_init, lib loader" +``` + +--- + +## Chunk 4: Core wrapper (`core.py`) + +Typed async wrappers around the 8 FFI functions. Handles JSON parse, buffer free, error translation. No public API yet — that lands in `ChatApi`. + +### Task 4.1: `core.py` — exceptions, enums, `chat_send_cmd`, `chat_recv_msg_wait` + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/core.py` + +- [ ] **Step 1: Write the core module** + +```python +"""Internal typed async wrapper around libsimplex's 8 C ABI functions. + +Users interact with `Bot` / `ChatApi`. This module is exposed as +`simplex_chat.core` for tests and the api.ChatApi class only. +""" +from __future__ import annotations + +import asyncio +import ctypes +import json +from enum import StrEnum +from typing import TypedDict + +from . import _native +from .types import T, CR, CEvt + + +class ChatAPIError(Exception): + """Raised when chat_send_cmd / chat_recv_msg_wait returns a chat error.""" + def __init__(self, message: str, chat_error: T.ChatError | None = None): + super().__init__(message) + self.chat_error = chat_error + + +class ChatInitError(Exception): + """Raised when chat_migrate_init returns a DBMigrationResult error.""" + def __init__(self, message: str, db_migration_error): + super().__init__(message) + self.db_migration_error = db_migration_error + + +class MigrationConfirmation(StrEnum): + YES_UP = "yesUp" + YES_UP_DOWN = "yesUpDown" + CONSOLE = "console" + ERROR = "error" + + +class CryptoArgs(TypedDict): # wire-format JSON; camelCase fields + fileKey: str + fileNonce: str + + +def _read_and_free(ptr: int | None) -> str: + """Copy a Haskell-allocated null-terminated UTF-8 string and free its buffer. + + Mirrors HandleCResult in packages/simplex-chat-nodejs/cpp/simplex.cc:157-165. + """ + if not ptr: + raise RuntimeError("null pointer returned from libsimplex") + try: + return ctypes.string_at(ptr).decode("utf-8") + finally: + _native.libc().free(ctypes.c_void_p(ptr)) + + +async def chat_send_cmd(ctrl: int, cmd: str) -> CR.ChatResponse: + def _call() -> str: + ptr = _native._lib.chat_send_cmd(ctrl, cmd.encode("utf-8")) + return _read_and_free(ptr) + raw = await asyncio.to_thread(_call) + parsed = json.loads(raw) + if "result" in parsed and isinstance(parsed["result"], dict): + return parsed["result"] # type: ignore[return-value] + err = parsed.get("error") + if isinstance(err, dict): + raise ChatAPIError(f"chat command error: {err.get('type')}", err) + raise ChatAPIError(f"invalid chat command result: {raw[:200]}") + + +async def chat_recv_msg_wait(ctrl: int, wait_us: int = 5_000_000) -> CEvt.ChatEvent | None: + def _call() -> str: + ptr = _native._lib.chat_recv_msg_wait(ctrl, wait_us) + if not ptr: + return "" + return _read_and_free(ptr) + raw = await asyncio.to_thread(_call) + if not raw: + return None + parsed = json.loads(raw) + if "result" in parsed and isinstance(parsed["result"], dict): + return parsed["result"] # type: ignore[return-value] + err = parsed.get("error") + if isinstance(err, dict): + raise ChatAPIError(f"chat event error: {err.get('type')}", err) + raise ChatAPIError(f"invalid chat event: {raw[:200]}") +``` + +- [ ] **Step 2: Verify import** + +``` +PYTHONPATH=src python -c "from simplex_chat.core import chat_send_cmd, ChatAPIError, MigrationConfirmation; print('ok')" +``` + +Expected: `ok`. + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/core.py +git commit -m "feat(python): core typed wrappers for chat_send_cmd + chat_recv_msg_wait" +``` + +### Task 4.2: `core.py` — remaining FFI functions (`chat_migrate_init`, `chat_close_store`, file ops) + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/core.py` + +- [ ] **Step 1: Append the remaining functions** + +```python +async def chat_migrate_init( + db_path: str, db_key: str, confirm: MigrationConfirmation +) -> int: + """Initialize chat controller. Returns opaque ctrl pointer as Python int.""" + def _call() -> tuple[int, str]: + ctrl = ctypes.c_void_p() + ptr = _native._lib.chat_migrate_init( + db_path.encode("utf-8"), + db_key.encode("utf-8"), + confirm.encode("utf-8"), + ctypes.byref(ctrl), + ) + return (ctrl.value or 0, _read_and_free(ptr)) + ctrl_val, raw = await asyncio.to_thread(_call) + parsed = json.loads(raw) + if parsed.get("type") == "ok": + return ctrl_val + raise ChatInitError( + "Database or migration error (see db_migration_error)", + parsed, + ) + + +async def chat_close_store(ctrl: int) -> None: + def _call() -> str: + ptr = _native._lib.chat_close_store(ctrl) + return _read_and_free(ptr) + res = await asyncio.to_thread(_call) + if res: + raise RuntimeError(res) + + +async def chat_write_file(ctrl: int, path: str, data: bytes) -> CryptoArgs: + def _call() -> str: + buf = (ctypes.c_uint8 * len(data)).from_buffer_copy(data) + ptr = _native._lib.chat_write_file(ctrl, path.encode("utf-8"), buf, len(data)) + return _read_and_free(ptr) + raw = await asyncio.to_thread(_call) + return _crypto_args_result(raw) + + +async def chat_read_file(path: str, args: CryptoArgs) -> bytes: + def _call() -> bytes: + ptr = _native._lib.chat_read_file( + path.encode("utf-8"), + args["fileKey"].encode("utf-8"), + args["fileNonce"].encode("utf-8"), + ) + if not ptr: + raise RuntimeError("chat_read_file returned null") + addr = ctypes.cast(ptr, ctypes.c_void_p).value or 0 + try: + status = ctypes.cast(addr, ctypes.POINTER(ctypes.c_uint8))[0] + if status == 1: + msg = ctypes.string_at(addr + 1).decode("utf-8") + raise RuntimeError(msg) + if status != 0: + raise RuntimeError(f"unexpected status {status} from chat_read_file") + length = ctypes.cast(addr + 1, ctypes.POINTER(ctypes.c_uint32))[0] + return ctypes.string_at(addr + 5, length) + finally: + _native.libc().free(ctypes.c_void_p(addr)) + return await asyncio.to_thread(_call) + + +async def chat_encrypt_file(ctrl: int, src: str, dst: str) -> CryptoArgs: + def _call() -> str: + ptr = _native._lib.chat_encrypt_file( + ctrl, src.encode("utf-8"), dst.encode("utf-8") + ) + return _read_and_free(ptr) + return _crypto_args_result(await asyncio.to_thread(_call)) + + +async def chat_decrypt_file(src: str, args: CryptoArgs, dst: str) -> None: + def _call() -> str: + ptr = _native._lib.chat_decrypt_file( + src.encode("utf-8"), + args["fileKey"].encode("utf-8"), + args["fileNonce"].encode("utf-8"), + dst.encode("utf-8"), + ) + return _read_and_free(ptr) + res = await asyncio.to_thread(_call) + if res: + raise RuntimeError(res) + + +def _crypto_args_result(raw: str) -> CryptoArgs: + parsed = json.loads(raw) + if parsed.get("type") == "result": + return parsed["cryptoArgs"] + if parsed.get("type") == "error": + raise RuntimeError(parsed.get("writeError", "unknown write error")) + raise RuntimeError(f"unexpected result: {raw[:200]}") +``` + +- [ ] **Step 2: Re-verify import** + +``` +PYTHONPATH=src python -c "from simplex_chat import core; print(dir(core))" +``` + +Expected: prints a list including `chat_migrate_init`, `chat_close_store`, all eight functions. + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/core.py +git commit -m "feat(python): core wrappers for migrate, close, file ops" +``` + +--- + +## Chunk 5: ChatApi class + +Escape-hatch class with the 6 control methods plus ~40 `api_xxx` methods, one per Node `apiXxx`. Repetitive — done in batches grouped by domain. + +### Task 5.1: `api.py` — `ChatApi` class with control methods + `Db` config + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/api.py` + +- [ ] **Step 1: Write Db config dataclasses + ChatApi base** + +```python +"""Low-level escape-hatch API. Most users go through `Bot` instead.""" +from __future__ import annotations + +from dataclasses import dataclass +from typing import TYPE_CHECKING + +from . import core +from .core import ChatAPIError, ChatInitError, MigrationConfirmation +from .types import CC, CEvt, CR, T + + +@dataclass(slots=True) +class SqliteDb: + file_prefix: str + encryption_key: str | None = None + + +@dataclass(slots=True) +class PostgresDb: + connection_string: str + schema_prefix: str | None = None + + +Db = SqliteDb | PostgresDb + + +def _db_to_migrate_args(db: Db) -> tuple[str, str, str]: + """Returns (path-or-prefix, key-or-conn, backend).""" + if isinstance(db, SqliteDb): + return (db.file_prefix, db.encryption_key or "", "sqlite") + if isinstance(db, PostgresDb): + return (db.schema_prefix or "", db.connection_string, "postgres") + raise TypeError(f"Unknown db: {db!r}") + + +class ChatCommandError(Exception): + def __init__(self, message: str, response: CR.ChatResponse): + super().__init__(message) + self.response = response + + +class ChatApi: + def __init__(self, ctrl: int, backend: str): + self._ctrl = ctrl + self._backend = backend + + @classmethod + async def init( + cls, + db: Db, + confirm: MigrationConfirmation = MigrationConfirmation.YES_UP, + ) -> "ChatApi": + from . import _native + path_or_prefix, key_or_conn, backend = _db_to_migrate_args(db) + # Trigger lazy lib load with the right backend BEFORE chat_migrate_init. + _native.lib_for(backend) # type: ignore[arg-type] + ctrl = await core.chat_migrate_init(path_or_prefix, key_or_conn, confirm) + return cls(ctrl, backend) + + @property + def ctrl(self) -> int: + return self._ctrl + + async def start_chat(self) -> None: + r = await self.send_chat_cmd( + CC.StartChat_cmd_string({"mainApp": True, "enableSndFiles": True}) + ) + if r.get("type") not in ("chatStarted", "chatRunning"): + raise ChatCommandError("error starting chat", r) + + async def stop_chat(self) -> None: + r = await self.send_chat_cmd("/_stop") + if r.get("type") != "chatStopped": + raise ChatCommandError("error stopping chat", r) + + async def close(self) -> None: + await core.chat_close_store(self._ctrl) + + async def send_chat_cmd(self, cmd: str) -> CR.ChatResponse: + return await core.chat_send_cmd(self._ctrl, cmd) + + async def recv_chat_event(self, wait_us: int = 5_000_000) -> CEvt.ChatEvent | None: + return await core.chat_recv_msg_wait(self._ctrl, wait_us) +``` + +- [ ] **Step 2: Verify import** + +``` +PYTHONPATH=src python -c "from simplex_chat.api import ChatApi, SqliteDb, PostgresDb; print('ok')" +``` + +Expected: `ok`. + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/api.py +git commit -m "feat(python): ChatApi base class with control methods + Db config" +``` + +### Task 5.2: `api.py` — implement ~40 `api_xxx` methods + +Mirror methods from `packages/simplex-chat-nodejs/src/api.ts:344-958`. Each method is the same shape: + +```python +async def api_(self, ...args) -> : + r = await self.send_chat_cmd(CC._cmd_string({"...": args, ...})) + if r["type"] == "": + return r[""] + raise ChatCommandError("error ", r) +``` + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/api.py` + +- [ ] **Step 1: Reference the Node lib while implementing** + +```bash +# Open the Node source side-by-side +sed -n '344,958p' packages/simplex-chat-nodejs/src/api.ts | less +``` + +- [ ] **Step 2: Implement methods in groups, one commit per group** + +The Node file groups by domain — port them in the same order: + +| Group | Methods (Node names → Python snake_case) | Lines in api.ts | +|---|---|---| +| Address | apiCreateUserAddress, apiDeleteUserAddress, apiGetUserAddress, apiSetProfileAddress, apiSetAddressSettings | 344-409 | +| Messages | apiSendMessages, apiSendTextMessage, apiSendTextReply, apiUpdateChatItem, apiDeleteChatItems, apiDeleteMemberChatItem, apiChatItemReaction | 411-505 | +| Files | apiReceiveFile, apiCancelFile | 507-525 | +| Groups | apiAddMember, apiJoinGroup, apiAcceptMember, apiSetMembersRole, apiBlockMembersForAll, apiRemoveMembers, apiLeaveGroup, apiListMembers, apiNewGroup, apiUpdateGroupProfile | 527-625 | +| Group links | apiCreateGroupLink, apiSetGroupLinkMemberRole, apiDeleteGroupLink, apiGetGroupLink, apiGetGroupLinkStr | 627-672 | +| Connections | apiCreateLink, apiConnectPlan, apiConnect, apiConnectActiveUser, apiAcceptContactRequest, apiRejectContactRequest | 674-746 | +| Chats | apiListContacts, apiListGroups, apiGetChats, apiDeleteChat, apiSetGroupCustomData, apiSetContactCustomData, apiSetAutoAcceptMemberContacts, apiGetChat | 748-841 | +| Users | apiGetActiveUser, apiCreateActiveUser, apiListUsers, apiSetActiveUser, apiDeleteUser, apiUpdateProfile, apiSetContactPrefs | 843-928 | +| Member contacts | apiCreateMemberContact, apiSendMemberContactInvitation | 930-957 | + +For each method: +- TS `apiCreateUserAddress(userId)` → Python `api_create_user_address(self, user_id: int) -> T.CreatedConnLink` +- Use the autogenerated `CC._cmd_string({...})` to build the command string. Field names inside the dict are camelCase wire format. +- Use type narrowing on `r["type"]` to extract the expected response field. + +Example port: + +```python +async def api_create_user_address(self, user_id: int) -> T.CreatedConnLink: + r = await self.send_chat_cmd(CC.APICreateMyAddress_cmd_string({"userId": user_id})) + if r["type"] == "userContactLinkCreated": + return r["connLinkContact"] + raise ChatCommandError("error creating user address", r) +``` + +Commit pattern: one commit per group from the table above. Commit messages: + +```bash +git commit -m "feat(python): ChatApi address methods" +git commit -m "feat(python): ChatApi message methods" +git commit -m "feat(python): ChatApi file methods" +# … etc, one per group +``` + +- [ ] **Step 3: After all groups land, verify all methods are present** + +```bash +grep -c "async def api_" packages/simplex-chat-python/src/simplex_chat/api.py +``` + +Expected: ≥40 (matches the count of `apiXxx` methods in api.ts). + +- [ ] **Step 4: Verify import after all groups** + +``` +PYTHONPATH=src python -c "from simplex_chat.api import ChatApi; api = ChatApi.__init__; print('ok')" +``` + +Expected: `ok`. + +--- + +## Chunk 6: Bot class + +User-facing `Bot`: decorator-registered handlers, kwarg filters, `Message` wrapper with content-narrowed subclasses, dual lifecycle, middleware. + +### Task 6.1: `Message` wrapper class + content-narrowed aliases + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/bot.py` + +- [ ] **Step 1: Write `Message` and aliases** + +```python +"""User-facing `Bot` API: decorators, filters, Message wrapper, lifecycle.""" +from __future__ import annotations + +import asyncio +import logging +import re +import signal as _signal +from dataclasses import dataclass +from typing import ( + Any, Awaitable, Callable, Generic, Literal, TYPE_CHECKING, TypeVar, overload +) + +from . import _native +from .api import ChatApi, ChatCommandError, Db, PostgresDb, SqliteDb +from .core import ChatAPIError, MigrationConfirmation +from .types import CC, CEvt, CR, T + +if TYPE_CHECKING: + from typing_extensions import TypeAlias + +log = logging.getLogger("simplex_chat") + +C = TypeVar("C", bound=T.MsgContent) + + +@dataclass(slots=True) +class BotProfile: + display_name: str + full_name: str = "" + short_descr: str | None = None + image: str | None = None + + +@dataclass(slots=True) +class BotCommand: + keyword: str + label: str + + +@dataclass(slots=True, frozen=True) +class ParsedCommand: + keyword: str + args: str + + +@dataclass(slots=True, frozen=True) +class Message(Generic[C]): + chat_item: T.AChatItem + content: C + bot: "Bot" + + @property + def chat_info(self) -> T.ChatInfo: + return self.chat_item["chatInfo"] + + @property + def text(self) -> str | None: + c = self.content + if isinstance(c, dict): + return c.get("text") # type: ignore[return-value] + return None + + async def reply(self, text: str) -> "Message": + items = await self.bot.api.api_send_text_reply(self.chat_item, text) + return Message(chat_item=items[0], content=items[0]["chatItem"]["content"], bot=self.bot) + + async def reply_content(self, content: T.MsgContent) -> "Message": + items = await self.bot.api.api_send_messages( + self.chat_info, [{"msgContent": content, "mentions": {}}] + ) + return Message(chat_item=items[0], content=items[0]["chatItem"]["content"], bot=self.bot) + + async def react(self, emoji: str) -> None: + # Implementation defers to ChatApi.api_chat_item_reaction + ... + + async def delete(self) -> None: ... + async def forward(self, to: T.ChatRef) -> "Message": ... + + +# Concrete narrowed aliases — exported from package __init__.py +TextMessage = Message[T.MsgContent_Text] +ImageMessage = Message[T.MsgContent_Image] +FileMessage = Message[T.MsgContent_File] +VoiceMessage = Message[T.MsgContent_Voice] +VideoMessage = Message[T.MsgContent_Video] +LinkMessage = Message[T.MsgContent_Link] +# … one per T.MsgContent_* variant; full list mirrors what's emitted in _types.py +``` + +- [ ] **Step 2: Verify import** + +``` +PYTHONPATH=src python -c "from simplex_chat.bot import Message, TextMessage, BotProfile; print('ok')" +``` + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/bot.py +git commit -m "feat(python): Bot module skeleton — Message wrapper + aliases" +``` + +### Task 6.2: Filter compilation (`filters.py`) + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/filters.py` +- Create: `packages/simplex-chat-python/tests/test_filters.py` + +- [ ] **Step 1: Write filter tests** + +```python +# tests/test_filters.py +import re +import pytest +from simplex_chat.filters import compile_message_filter + +def _msg(content_type="text", text=None, chat_type="direct", + from_role=None, from_contact_id=None, group_id=None): + """Build a minimal mock Message-like object for filter testing.""" + class M: + pass + m = M() + m.content = {"type": content_type, "text": text} if text is not None else {"type": content_type} + m.chat_item = {"chatInfo": { + "type": chat_type, + **({"groupInfo": {"groupId": group_id}} if chat_type == "group" else {}), + }} + # Sender extraction is implementation-detail of the filter — keep test fixture pragmatic. + m._from_role = from_role + m._from_contact_id = from_contact_id + return m + +def test_no_filters_matches_all(): + f = compile_message_filter({}) + assert f(_msg(content_type="text")) + assert f(_msg(content_type="image")) + +def test_content_type_singular(): + f = compile_message_filter({"content_type": "text"}) + assert f(_msg(content_type="text")) + assert not f(_msg(content_type="image")) + +def test_content_type_tuple_or(): + f = compile_message_filter({"content_type": ("text", "image")}) + assert f(_msg(content_type="text")) + assert f(_msg(content_type="image")) + assert not f(_msg(content_type="voice")) + +def test_text_exact(): + f = compile_message_filter({"text": "hello"}) + assert f(_msg(text="hello")) + assert not f(_msg(text="world")) + +def test_text_regex(): + f = compile_message_filter({"text": re.compile(r"^\d+$")}) + assert f(_msg(text="123")) + assert not f(_msg(text="abc")) + +def test_when_callable(): + f = compile_message_filter({"when": lambda m: m.content["type"] == "voice"}) + assert f(_msg(content_type="voice")) + assert not f(_msg(content_type="text")) + +def test_combined_and(): + f = compile_message_filter({"content_type": "text", "text": re.compile(r"\d")}) + assert f(_msg(content_type="text", text="abc123")) + assert not f(_msg(content_type="text", text="abc")) + assert not f(_msg(content_type="image")) +``` + +- [ ] **Step 2: Implement `compile_message_filter`** + +```python +# filters.py +from __future__ import annotations +import re +from typing import Any, Callable + +def compile_message_filter(kw: dict[str, Any]) -> Callable[[Any], bool]: + """Compile filter kwargs into a single predicate function. + + Multiple kwargs combine with AND; tuples within a kwarg combine with OR. + `when` is the last predicate evaluated. + """ + predicates: list[Callable[[Any], bool]] = [] + + if (ct := kw.get("content_type")) is not None: + ct_set = (ct,) if isinstance(ct, str) else tuple(ct) + predicates.append(lambda m: m.content.get("type") in ct_set) + + if (t := kw.get("text")) is not None: + if isinstance(t, re.Pattern): + predicates.append(lambda m: bool(t.search(m.content.get("text", "") or ""))) + else: + predicates.append(lambda m: m.content.get("text") == t) + + if (ct := kw.get("chat_type")) is not None: + ct_set = (ct,) if isinstance(ct, str) else tuple(ct) + predicates.append(lambda m: m.chat_item["chatInfo"]["type"] in ct_set) + + if (gid := kw.get("group_id")) is not None: + gid_set = (gid,) if isinstance(gid, int) else tuple(gid) + def gid_match(m: Any) -> bool: + ci = m.chat_item["chatInfo"] + return ci["type"] == "group" and ci["groupInfo"]["groupId"] in gid_set + predicates.append(gid_match) + + # from_role, from_contact_id, from_member_id are looked up via helpers in filters.py + # — defer to integration with ChatInfo / GroupMember accessors implemented later. + + if (when := kw.get("when")) is not None: + predicates.append(when) + + if not predicates: + return lambda _m: True + return lambda m: all(p(m) for p in predicates) +``` + +- [ ] **Step 3: Run filter tests** + +``` +PYTHONPATH=src pytest tests/test_filters.py -v +``` + +Expected: all PASS. + +- [ ] **Step 4: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/filters.py \ + packages/simplex-chat-python/tests/test_filters.py +git commit -m "feat(python): filter kwarg compilation + tests" +``` + +### Task 6.3: `Bot` class — construction, decorators, registration + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/bot.py` + +- [ ] **Step 1: Add `Bot` class with `__init__`, decorator methods, registration storage** + +```python +# Append to bot.py + +MessageHandler = Callable[[Message], Awaitable[None]] +CommandHandler = Callable[[Message, ParsedCommand], Awaitable[None]] +EventHandler = Callable[[CEvt.ChatEvent], Awaitable[None]] +# Note: handlers are async-only (matches spec). Users must `async def handler(...)`. + + +class Middleware: + async def __call__( + self, + handler: Callable[[Message, dict[str, object]], Awaitable[None]], + message: Message, + data: dict[str, object], + ) -> None: + await handler(message, data) + + +class Bot: + def __init__( + self, + *, + profile: BotProfile, + db: Db, + welcome: str | T.MsgContent | None = None, + commands: list[BotCommand] | None = None, + confirm_migrations: MigrationConfirmation = MigrationConfirmation.YES_UP, + create_address: bool = True, + update_address: bool = True, + update_profile: bool = True, + auto_accept: bool = True, + business_address: bool = False, + allow_files: bool = False, + use_bot_profile: bool = True, + log_contacts: bool = True, + log_network: bool = False, + ) -> None: + self._profile = profile + self._db = db + self._welcome = welcome + self._commands = commands or [] + self._confirm_migrations = confirm_migrations + self._opts = { + "create_address": create_address, + "update_address": update_address, + "update_profile": update_profile, + "auto_accept": auto_accept, + "business_address": business_address, + "allow_files": allow_files, + "use_bot_profile": use_bot_profile, + "log_contacts": log_contacts, + "log_network": log_network, + } + self._api: ChatApi | None = None + self._serving = False + self._stop_event = asyncio.Event() + self._message_handlers: list[tuple[Callable[[Message], bool], MessageHandler]] = [] + self._command_handlers: list[tuple[tuple[str, ...], Callable[[Message], bool], CommandHandler]] = [] + self._event_handlers: dict[str, list[EventHandler]] = {} + self._middleware: list[Middleware] = [] + + @property + def api(self) -> ChatApi: + if self._api is None: + raise RuntimeError("Bot not initialized — call bot.run() or use `async with bot:`") + return self._api + + # --- decorator registration (overloads omitted for brevity; see spec for full list) --- + + def on_message(self, **filter_kw: Any) -> Callable[[MessageHandler], MessageHandler]: + from .filters import compile_message_filter + predicate = compile_message_filter(filter_kw) + def deco(fn: MessageHandler) -> MessageHandler: + self._message_handlers.append((predicate, fn)) + return fn + return deco + + def on_command( + self, name: str | tuple[str, ...], **filter_kw: Any + ) -> Callable[[CommandHandler], CommandHandler]: + names = (name,) if isinstance(name, str) else tuple(name) + from .filters import compile_message_filter + predicate = compile_message_filter(filter_kw) + def deco(fn: CommandHandler) -> CommandHandler: + self._command_handlers.append((names, predicate, fn)) + return fn + return deco + + def on_event(self, event: CEvt.Tag, /) -> Callable[[EventHandler], EventHandler]: + def deco(fn: EventHandler) -> EventHandler: + self._event_handlers.setdefault(event, []).append(fn) + return fn + return deco + + def use(self, middleware: Middleware) -> None: + self._middleware.append(middleware) +``` + +- [ ] **Step 2: Discover the full `MsgContent` variant list from generated types** + +After Chunk 1 lands, the generated file lists every variant. Get the canonical list: + +```bash +grep -E '^class MsgContent_' packages/simplex-chat-python/src/simplex_chat/types/_types.py | sed 's/class \([^(]*\).*/\1/' +``` + +Expected output (approximately — exact list comes from Haskell `MsgContent` defined at `bots/src/API/Docs/Types.hs:309` with prefix `"MC"`): + +``` +MsgContent_Text +MsgContent_Link +MsgContent_Image +MsgContent_Video +MsgContent_Voice +MsgContent_File +MsgContent_Report +MsgContent_Chat +MsgContent_Unknown +``` + +For each variant in that list, define both a `Message` alias (in Step 1 above — extend the alias block to cover every variant) and one decorator overload (this step). + +- [ ] **Step 3: Add the typed overloads — one per variant from Step 2** + +After the plain `on_message` definition, add a typed overload for each variant. The pattern below shows two; repeat for every variant the previous step found: + +```python + # Type-only overloads — compiler-visible, no runtime effect. + # MUST cover every variant from `grep '^class MsgContent_' _types.py`. + @overload + def on_message(self, *, content_type: Literal["text"], **rest: Any + ) -> Callable[[Callable[[TextMessage], Awaitable[None]]], + Callable[[TextMessage], Awaitable[None]]]: ... + @overload + def on_message(self, *, content_type: Literal["image"], **rest: Any + ) -> Callable[[Callable[[ImageMessage], Awaitable[None]]], + Callable[[ImageMessage], Awaitable[None]]]: ... + # … one per MsgContent variant; verify count matches Step 2's grep output … + @overload + def on_message(self, **rest: Any + ) -> Callable[[MessageHandler], MessageHandler]: ... +``` + +The per-variant tag string (`"text"`, `"image"`, …) is the lowercased suffix after `MsgContent_` — see the `Literal[...]` member on each generated TypedDict's `type` field for the canonical spelling. + +- [ ] **Step 3: Verify import + decorator binding** + +```python +# tests/test_bot_registration.py +from simplex_chat.bot import Bot, BotProfile +from simplex_chat.api import SqliteDb + +def test_decorator_registers_handler(): + bot = Bot(profile=BotProfile(display_name="x"), db=SqliteDb(file_prefix="/tmp/test")) + @bot.on_message(content_type="text") + async def h(msg): pass + assert len(bot._message_handlers) == 1 +``` + +``` +PYTHONPATH=src pytest tests/test_bot_registration.py -v +``` + +Expected: PASS. + +- [ ] **Step 4: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/bot.py \ + packages/simplex-chat-python/tests/test_bot_registration.py +git commit -m "feat(python): Bot class — construction + decorator registration" +``` + +### Task 6.4: Lifecycle: `run()`, `serve_forever()`, `__aenter__/__aexit__`, `stop()` + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/bot.py` + +- [ ] **Step 1: Append lifecycle methods** + +```python +class Bot: + # … existing methods … + + async def __aenter__(self) -> "Bot": + self._api = await ChatApi.init(self._db, self._confirm_migrations) + await self._api.start_chat() + # TODO Task 6.5: profile + address sync via mkBotProfile/createOrUpdateAddress + return self + + async def __aexit__(self, *exc_info: object) -> None: + self.stop() + if self._api is not None: + try: + await self._api.stop_chat() + finally: + await self._api.close() + self._api = None + + def run(self) -> None: + """Blocking entry: runs serve_forever() with a SIGINT handler installed.""" + async def _main() -> None: + async with self: + loop = asyncio.get_running_loop() + if hasattr(_signal, "SIGINT"): + try: + loop.add_signal_handler(_signal.SIGINT, self.stop) + loop.add_signal_handler(_signal.SIGTERM, self.stop) + except NotImplementedError: # Windows + _signal.signal(_signal.SIGINT, lambda *_: self.stop()) + await self.serve_forever() + asyncio.run(_main()) + + async def serve_forever(self) -> None: + if self._serving: + raise RuntimeError("already serving") + self._serving = True + self._stop_event.clear() + try: + await self._receive_loop() + finally: + self._serving = False + + def stop(self) -> None: + self._stop_event.set() + + async def _receive_loop(self) -> None: + while not self._stop_event.is_set(): + try: + event = await self.api.recv_chat_event(wait_us=5_000_000) + except ChatAPIError as e: + log.error("chat event error: %s", e) + continue + if event is None: + continue + await self._dispatch_event(event) +``` + +- [ ] **Step 2: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/bot.py +git commit -m "feat(python): Bot lifecycle (run, serve_forever, async-context, stop)" +``` + +### Task 6.5: Event dispatch + handler invocation through middleware + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/bot.py` + +- [ ] **Step 1: Append `_dispatch_event` and helpers** + +```python +class Bot: + async def _dispatch_event(self, event: CEvt.ChatEvent) -> None: + # 1. Tag-targeted on_event handlers (registration order) + tag = event["type"] + for h in self._event_handlers.get(tag, []): + try: + await h(event) + except Exception: + log.exception("on_event handler failed") + # 2. If newChatItems → message + command dispatch. + # Tag check narrows the union; pyright sees event as CEvt.ChatEvent_NewChatItems below. + if tag == "newChatItems": + evt: CEvt.ChatEvent_NewChatItems = event # type: ignore[assignment] + for ci in evt["chatItems"]: + content = ci["chatItem"]["content"] + if content["type"] != "rcvMsgContent": + continue + msg_content = content["msgContent"] + msg = Message(chat_item=ci, content=msg_content, bot=self) + await self._dispatch_message(msg) + + async def _dispatch_message(self, msg: Message) -> None: + # Run all matching message handlers + for predicate, handler in self._message_handlers: + if predicate(msg): + await self._invoke_with_middleware(handler, msg) + # Then any matching command handlers + cmd = self._parse_command(msg) + if cmd is not None: + for names, predicate, handler in self._command_handlers: + if cmd.keyword in names and predicate(msg): + await self._invoke_command_with_middleware(handler, msg, cmd) + + async def _invoke_with_middleware( + self, handler: MessageHandler, message: Message + ) -> None: + async def call(m: Message, _data: dict[str, object]) -> None: + await handler(m) + + chain: Callable[[Message, dict[str, object]], Awaitable[None]] = call + # mw=mw, inner=inner bind the loop variable (late-binding fix) + for mw in reversed(self._middleware): + inner = chain + async def _wrapped(m: Message, d: dict[str, object], mw=mw, inner=inner) -> None: + await mw(inner, m, d) + chain = _wrapped + + try: + await chain(message, {}) + except Exception: + log.exception("message handler failed") + + async def _invoke_command_with_middleware( + self, handler: CommandHandler, message: Message, cmd: ParsedCommand + ) -> None: + # Same shape as _invoke_with_middleware but the inner call gets cmd too. + async def call(m: Message, _data: dict[str, object]) -> None: + await handler(m, cmd) + chain: Callable[[Message, dict[str, object]], Awaitable[None]] = call + for mw in reversed(self._middleware): + inner = chain + async def _wrapped(m: Message, d: dict[str, object], mw=mw, inner=inner) -> None: + await mw(inner, m, d) + chain = _wrapped + try: + await chain(message, {}) + except Exception: + log.exception("command handler failed") + + @staticmethod + def _parse_command(msg: Message) -> ParsedCommand | None: + text = msg.text + if not text or not text.startswith("/"): + return None + body = text[1:].lstrip() + if not body: + return None + if " " in body: + kw, args = body.split(" ", 1) + return ParsedCommand(keyword=kw, args=args.strip()) + return ParsedCommand(keyword=body, args="") +``` + +- [ ] **Step 2: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/bot.py +git commit -m "feat(python): Bot event dispatch + middleware chaining" +``` + +### Task 6.6: Profile + address sync (mirror Node `bot.ts:158-214`) + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/bot.py` + +- [ ] **Step 1: Implement initial profile + address sync inside `__aenter__`** + +Mirror `bot.ts` `createBotUser`, `createOrUpdateAddress`, `updateBotUserProfile`, `mkBotProfile`. Each is straightforward — fetch via `self._api.api_get_active_user()`, compare with `self._profile`, update if `update_profile=True`, etc. Reference: `packages/simplex-chat-nodejs/src/bot.ts:158-214`. + +- [ ] **Step 2: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/bot.py +git commit -m "feat(python): Bot profile + address sync on init" +``` + +### Task 6.7: Update package `__init__.py` to export public API + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/__init__.py` + +- [ ] **Step 1: Add exports** + +```python +"""SimpleX Chat — Python client library for chat bots.""" +from ._version import __version__ +from .api import ChatApi, ChatCommandError, Db, PostgresDb, SqliteDb +from .bot import ( + Bot, + BotCommand, + BotProfile, + FileMessage, + ImageMessage, + Message, + Middleware, + ParsedCommand, + TextMessage, + VoiceMessage, + # … all aliases … +) +from .core import ChatAPIError, ChatInitError, MigrationConfirmation, CryptoArgs + +__all__ = [ + "__version__", + # … all the names above … +] +``` + +- [ ] **Step 2: Verify clean import** + +``` +PYTHONPATH=src python -c "from simplex_chat import Bot, TextMessage, SqliteDb, BotProfile; print('ok')" +``` + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/__init__.py +git commit -m "feat(python): export public API from package __init__" +``` + +--- + +## Chunk 7: Tests + CLI + +Pytest suite covering native, codegen, smoke; pre-fetch CLI; example bot. + +### Task 7.1: `__main__.py` — `python -m simplex_chat install` + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/__main__.py` + +- [ ] **Step 1: Write the CLI** + +```python +"""CLI: python -m simplex_chat install [--backend=sqlite|postgres]""" +from __future__ import annotations +import argparse +import sys +from . import _native + + +def main(argv: list[str] | None = None) -> int: + p = argparse.ArgumentParser(prog="simplex_chat") + sub = p.add_subparsers(dest="command", required=True) + install = sub.add_parser("install", help="Pre-fetch libsimplex into the user cache") + install.add_argument( + "--backend", choices=["sqlite", "postgres"], default="sqlite", + help="which libsimplex variant to download (default: sqlite)", + ) + args = p.parse_args(argv) + if args.command == "install": + try: + path = _native._resolve_libs_dir(args.backend) + print(f"libsimplex installed at: {path}") + return 0 + except Exception as e: + print(f"install failed: {e}", file=sys.stderr) + return 1 + return 1 + + +if __name__ == "__main__": + sys.exit(main()) +``` + +- [ ] **Step 2: Smoke-check the CLI exists** + +``` +PYTHONPATH=src python -m simplex_chat install --help +``` + +Expected: prints argparse help. + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/__main__.py +git commit -m "feat(python): python -m simplex_chat install CLI" +``` + +### Task 7.2: Codegen smoke test + +**Files:** +- Create: `packages/simplex-chat-python/tests/test_codegen.py` + +- [ ] **Step 1: Write the test** + +```python +"""Sanity checks on auto-generated wire types — catches generator regressions.""" +import typing +from simplex_chat.types import T, CC, CR, CEvt + + +def test_types_module_imports(): + """Every generated module imports cleanly with no SyntaxError.""" + assert T is not None and CC is not None and CR is not None and CEvt is not None + + +def test_chat_type_is_literal_enum(): + """ChatType should be a Literal of expected member set.""" + origin = typing.get_origin(T.ChatType) + args = typing.get_args(T.ChatType) + # Python ≥3.11 typing.Literal: origin is Literal, args is the tuple of values + assert "direct" in args + assert "group" in args + assert "local" in args + + +def test_known_command_has_cmd_string(): + s = CC.APICreateMyAddress_cmd_string({"userId": 1}) + assert s == "/_address 1" + + +def test_chat_response_tag_alias_present(): + """ChatResponse_Tag union of literals exists.""" + assert hasattr(CR, "ChatResponse_Tag") +``` + +- [ ] **Step 2: Run** + +``` +PYTHONPATH=src pytest tests/test_codegen.py -v +``` + +Expected: PASS. + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/tests/test_codegen.py +git commit -m "test(python): codegen sanity checks" +``` + +### Task 7.3: Smoke test with stub libsimplex + +**Files:** +- Create: `packages/simplex-chat-python/tests/test_smoke.py` +- Create: `packages/simplex-chat-python/tests/_stub_libsimplex.c` + +- [ ] **Step 1: Write a tiny C stub that returns canned JSON** + +```c +// _stub_libsimplex.c — compile to libsimplex.so for smoke testing +#include +#include +#include + +void hs_init_with_rtsopts(int *argc, char ***argv) { (void)argc; (void)argv; } + +static char *dup_str(const char *s) { return strdup(s); } + +char *chat_migrate_init(const char *path, const char *key, const char *confirm, void **ctrl) { + (void)path; (void)key; (void)confirm; + *ctrl = (void*)0x1; + return dup_str("{\"type\":\"ok\"}"); +} + +char *chat_close_store(void *ctrl) { (void)ctrl; return dup_str(""); } + +char *chat_send_cmd(void *ctrl, const char *cmd) { + (void)ctrl; (void)cmd; + return dup_str("{\"result\":{\"type\":\"chatStarted\"}}"); +} + +char *chat_recv_msg_wait(void *ctrl, int wait) { + (void)ctrl; (void)wait; + return NULL; +} +// stubs for the file functions: +char *chat_write_file() { return dup_str("{\"type\":\"result\",\"cryptoArgs\":{\"fileKey\":\"k\",\"fileNonce\":\"n\"}}"); } +char *chat_read_file() { return NULL; } +char *chat_encrypt_file() { return dup_str("{\"type\":\"result\",\"cryptoArgs\":{\"fileKey\":\"k\",\"fileNonce\":\"n\"}}"); } +char *chat_decrypt_file() { return dup_str(""); } +``` + +- [ ] **Step 2: Write the smoke test that compiles and uses the stub** + +```python +import asyncio +import os +import shutil +import subprocess +import sys +import tempfile +from pathlib import Path +import pytest + + +@pytest.fixture +def stub_libs_dir(tmp_path): + """Compile _stub_libsimplex.c into a libsimplex.so in tmp_path.""" + src = Path(__file__).parent / "_stub_libsimplex.c" + if sys.platform == "win32": + pytest.skip("stub compilation not implemented for Windows") + libname = "libsimplex.dylib" if sys.platform == "darwin" else "libsimplex.so" + out = tmp_path / libname + cc = shutil.which("cc") or shutil.which("gcc") or pytest.skip("no C compiler") + subprocess.run([cc, "-shared", "-fPIC", str(src), "-o", str(out)], check=True) + return tmp_path + + +@pytest.mark.asyncio +async def test_chat_api_init_and_start(stub_libs_dir, monkeypatch): + monkeypatch.setenv("SIMPLEX_LIBS_DIR", str(stub_libs_dir)) + from simplex_chat.api import ChatApi, SqliteDb + api = await ChatApi.init(SqliteDb(file_prefix=str(stub_libs_dir / "db"))) + await api.start_chat() + await api.close() +``` + +- [ ] **Step 3: Run** + +`test` extras are already declared in `pyproject.toml` (Task 2.1). + +``` +pip install -e ".[test]" +PYTHONPATH=src pytest tests/test_smoke.py -v +``` + +Expected: PASS on Linux/macOS; SKIPPED on Windows. + +- [ ] **Step 4: Commit** + +```bash +git add packages/simplex-chat-python/tests/_stub_libsimplex.c \ + packages/simplex-chat-python/tests/test_smoke.py +git commit -m "test(python): smoke test against stub libsimplex" +``` + +### Task 7.4: Squaring-bot example + +**Files:** +- Create: `packages/simplex-chat-python/examples/squaring_bot.py` + +- [ ] **Step 1: Write the example from the spec** + +```python +"""Squaring bot — receives numbers, replies with their squares. + +Run: python examples/squaring_bot.py +""" +import re +from simplex_chat import ( + Bot, BotProfile, BotCommand, SqliteDb, TextMessage, Message, ParsedCommand +) + +bot = Bot( + profile=BotProfile(display_name="Squaring bot"), + db=SqliteDb(file_prefix="./squaring_bot"), + welcome="Send me a number, I'll square it.", + commands=[BotCommand(keyword="help", label="Show help")], +) + +NUMBER_RE = re.compile(r"^-?\d+(\.\d+)?$") + +@bot.on_message(content_type="text", text=NUMBER_RE) +async def square(msg: TextMessage) -> None: + n = float(msg.text) + await msg.reply(f"{n} * {n} = {n * n}") + +@bot.on_message(content_type="text") # fallback +async def fallback(msg: Message) -> None: + await msg.reply("Send me a number, like 7 or 3.14.") + +@bot.on_command("help") +async def help_cmd(msg: Message, _cmd: ParsedCommand) -> None: + await msg.reply("Send a number, I'll square it.") + +if __name__ == "__main__": + bot.run() +``` + +- [ ] **Step 2: Document in README** + +Replace the placeholder in `packages/simplex-chat-python/README.md` with the example and `pip install simplex-chat` quick-start. + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/examples/squaring_bot.py \ + packages/simplex-chat-python/README.md +git commit -m "docs(python): squaring bot example + README quick-start" +``` + +--- + +## Chunk 8: CI publishing + +Append `publish-python` job to existing `.github/workflows/build.yml` after `release-nodejs-libs`. Configure PyPI trusted publisher. + +### Task 8.1: Add `publish-python` job to `build.yml` + +**Files:** +- Modify: `.github/workflows/build.yml` (append new job after `release-nodejs-libs:`) + +- [ ] **Step 1: Append the job** + +After line ~803 (end of `release-nodejs-libs`), add: + +```yaml +# ========================= +# Python package release +# ========================= + +# Publishes simplex-chat to PyPI on release tags. +# Depends on release-nodejs-libs because the Python package downloads +# its libsimplex from the simplex-chat-libs release at runtime, so the +# libs release must exist before the Python package is published. +# +# Trusted publishing is configured on PyPI: no API token, OIDC only. + + publish-python: + runs-on: ubuntu-latest + needs: [release-nodejs-libs] + if: startsWith(github.ref, 'refs/tags/v') + permissions: + id-token: write # OIDC for trusted publishing + steps: + - uses: actions/checkout@v6 + - uses: actions/setup-python@v5 + with: + python-version: "3.11" + - run: pip install build + - run: python -m build --wheel + working-directory: packages/simplex-chat-python + - uses: pypa/gh-action-pypi-publish@release/v1 + with: + packages-dir: packages/simplex-chat-python/dist +``` + +- [ ] **Step 2: Validate YAML locally** + +``` +python -c "import yaml; yaml.safe_load(open('.github/workflows/build.yml'))" +``` + +Expected: no exception. + +- [ ] **Step 3: Commit** + +```bash +git add .github/workflows/build.yml +git commit -m "ci: add publish-python job for PyPI release on tag" +``` + +### Task 8.2: One-time PyPI setup (manual, document in README) + +- [ ] **Step 1: Verify package name `simplex-chat` is available on PyPI** + +```bash +curl -s https://pypi.org/pypi/simplex-chat/json | head -c 200 +``` + +If it 404s, the name is free. If it returns metadata, the name is taken — coordinate with the team. + +- [ ] **Step 2: On PyPI, create a pending publisher** + +Navigate to https://pypi.org/manage/account/publishing/ and add: + +| Field | Value | +|---|---| +| PyPI Project Name | simplex-chat | +| Owner | simplex-chat | +| Repository name | simplex-chat | +| Workflow name | build.yml | +| Environment name | (leave blank) | + +- [ ] **Step 3: Add a section to `packages/simplex-chat-python/README.md` documenting the release process** + +Brief checklist for the maintainer: +1. Bump `_version.py` `__version__` (and `LIBS_VERSION` if libs changed). +2. Tag with `vX.Y.Z` matching `__version__`. +3. Push the tag → CI runs the existing build matrix, then `release-nodejs-libs`, then `publish-python`. +4. Verify the wheel appears at https://pypi.org/project/simplex-chat/. + +- [ ] **Step 4: Commit doc update** + +```bash +git add packages/simplex-chat-python/README.md +git commit -m "docs(python): release process + PyPI trusted publisher setup" +``` + +--- + +## Final acceptance + +After all phases: + +- [ ] **Type generation parity.** `cabal test simplex-chat-test` passes for all four `Python` test cases. +- [ ] **Python package builds.** `cd packages/simplex-chat-python && python -m build --wheel` produces a single `.whl` ≤ 200 KB. +- [ ] **All Python tests pass.** `pytest packages/simplex-chat-python/tests` — green on Linux + macOS. +- [ ] **Pyright clean.** `pyright packages/simplex-chat-python/src` — zero errors. +- [ ] **Squaring bot smoke.** Run `python examples/squaring_bot.py` against a fresh database; verify (a) lazy lib download succeeds, (b) `bot.run()` blocks, (c) Ctrl-C exits cleanly. +- [ ] **CI dry run.** Push a `v0.0.0-test` tag to a fork; verify the `publish-python` job runs after `release-nodejs-libs` and the wheel uploads to TestPyPI (configure a separate test publisher if doing this). diff --git a/plans/2026-05-08-desktop-text-selection-id-anchored.md b/plans/2026-05-08-desktop-text-selection-id-anchored.md new file mode 100644 index 0000000000..0d986f62ed --- /dev/null +++ b/plans/2026-05-08-desktop-text-selection-id-anchored.md @@ -0,0 +1,145 @@ +# Desktop Text Selection — Anchor by Item Id + +## 1. The bug + +`SelectionRange` stored two **positional** indices into the reversed merged-items list: + +```kotlin +data class SelectionRange( + val startIndex: Int, + val startOffset: Int, + val endIndex: Int, + val endOffset: Int +) +``` + +`reversedChatItems` grows from the front: a new message is prepended at index 0, every existing item shifts +1. Selection indices were never adjusted, so once the user had a selection on a message and another message arrived (or was sent), the indices kept pointing to the same numerical positions while the items at those positions had changed. The highlight (and the copy result) silently moved onto neighbouring messages. + +Same root cause for the deletion case: removing an item from the list left selection indices pointing into a different item. + +## 2. Root cause + +Selection is **about items**, not positions. Storing positions into a list whose front grows is structurally wrong. The data structure must encode the stable identity (`ChatItem.id`), not the volatile position. + +Two ingredients are mandatory for any correct fix: + +1. **Remember which items** are anchor and focus (their stable `ChatItem.id`s). +2. **Update the positional indices** when the list mutates, so that everything downstream that reads `range.startIndex` / `range.endIndex` (highlight rendering, copy iteration, snap, copy-button placement, anchor/focus detection in `setupItemSelection` / `setupEmojiSelection`, drag direction in `SelectionCopyButton`) stays correct. + +Anything beyond this is structural overreach. + +## 3. Approaches considered + +| # | Approach | Note | +|---|----------|------| +| A | Replace positional indices with ids in `SelectionRange`; cache items on the manager via `mutableStateOf`; expose indices via `derivedStateOf`; rename every reader from `range?.startIndex` to `manager.startIndex`; move top-level `selectedRange` into the manager as a method. | Structurally clean (single source of truth = ids), but renames every reader and moves a function for no behaviour reason. Ripples through `setupItemSelection`, `setupEmojiSelection`, `SelectionCopyButton`, `getSelectedCopiedText`, `snapSelection`, `copyButtonOffset`. | +| B | Same as A but replace the cached `var items` with `var mergedItemsState: State?` (mirrors the existing `listState` field; eliminates duplicated state and the items-sync line in `SideEffect`). | Marginal improvement; the cost is still the renames and the function move, neither of which the bug requires. | +| C | **Final** — keep positional indices in `SelectionRange`, **add** `startItemId, endItemId` alongside them; resync the indices to the items they were anchored to on every recomposition via a `SideEffect`. | Every existing reader of `range.startIndex` / `range.endIndex` keeps working unchanged. The fix is a pure addition. | + +Approach C accepts one piece of structural duplication that A and B do not have: anchor ids and positional indices coexist in `SelectionRange`, kept consistent by `resyncIndices`. For a bug-fix change, the trade-off favours diff minimality — migrating to a single source of truth (ids only, indices derived) is a separate refactor that should not be bundled with a fix. + +## 4. Final implementation + +### 4.1 `SelectionRange` — two new fields + +```kotlin +data class SelectionRange( + val startIndex: Int, + val startItemId: Long, // NEW — stable anchor for the selection start + val startOffset: Int, + val endIndex: Int, + val endItemId: Long, // NEW — stable anchor for the selection focus + val endOffset: Int, +) +``` + +Existing `r.copy(startOffset = …)`, `r.copy(endOffset = …)`, `r.copy(startOffset = …, endOffset = …)` calls in `setAnchorOffset` / `updateFocusOffset` / `snapSelection` automatically preserve the new fields (data-class `copy` semantics). No change to those methods. + +### 4.2 `SelectionManager` — one new field, two body additions, one new method + +```kotlin +var mergedItemsState: State? = null // mirrors existing listState +``` + +`startSelection` looks up the id once at click time: + +```kotlin +fun startSelection(startIndex: Int, anchorY: Float, anchorX: Float) { + val id = mergedItemsState?.value?.items?.getOrNull(startIndex)?.newest()?.item?.id ?: return + range = SelectionRange(startIndex, id, -1, startIndex, id, -1) + selectionState = SelectionState.Selecting + anchorWindowY = anchorY + anchorWindowX = anchorX +} +``` + +`updateFocusIndex` updates `endItemId` whenever it updates `endIndex` (called both from `updateDragFocus` and from the scroll snapshotFlow — both paths covered by this single method): + +```kotlin +fun updateFocusIndex(index: Int) { + val r = range ?: return + val id = mergedItemsState?.value?.items?.getOrNull(index)?.newest()?.item?.id ?: return + range = r.copy(endIndex = index, endItemId = id) +} +``` + +New method: + +```kotlin +fun resyncIndices() { + val r = range ?: return + val items = mergedItemsState?.value?.items ?: return + val newStartIndex = items.indexOfFirst { it.newest().item.id == r.startItemId } + val newEndIndex = items.indexOfFirst { it.newest().item.id == r.endItemId } + if (newStartIndex < 0 || newEndIndex < 0) clearSelection() + else range = r.copy(startIndex = newStartIndex, endIndex = newEndIndex) +} +``` + +### 4.3 `SelectionHandler` — three new lines + +```kotlin +manager.listState = listState +manager.mergedItemsState = mergedItems // NEW — wires items into the manager +manager.onCopySelection = { … } + +// Resync after the items list mutates (new message arrives, item deleted). +SideEffect { manager.resyncIndices() } // NEW — the trigger +``` + +### 4.4 What is *not* changed + +- `selectedRange(range, index)` — still a top-level function with its existing signature. +- `getSelectedCopiedText(items, revealedItems, linkMode)` — same signature, same body. +- `snapSelection(items, linkMode)` — same signature, same body. +- `copyButtonOffset(...)` — uses `r.endIndex` directly; no change. +- `setupItemSelection`, `setupEmojiSelection`, `SelectionCopyButton` — every `range?.startIndex` / `range?.endIndex` reference is preserved verbatim. +- `startDragSelection`, `updateDragFocus`, `startSelection` (signature), `updateFocusIndex` (signature) — unchanged. `mergedItemsState` is reached via the manager's own field, so callers don't thread items. + +This is the structural property that compresses the diff: callers see no API change, and the file's structure (top-level `selectedRange`, top-level `selectedItemCopiedText`, top-level `snapOffset`, top-level extension helpers) is untouched. + +## 5. Why this works in Compose + +`SideEffect { manager.resyncIndices() }` runs after every successful composition of `SelectionHandler`. `SelectionHandler` returns a `Modifier` (non-Unit return → non-skippable), so it re-runs whenever its caller (`ChatView`) re-runs, which `ChatView` does whenever `mergedItems.value` changes (it iterates the items list directly). Within the same Compose frame, the `SideEffect` mutation of `range` invalidates the children that read `range`, and Compose re-runs them to convergence before commit. Net visible result: the selection highlight stays on the originally selected items on the same frame the new message arrives — same fidelity as a `derivedStateOf`-based approach, no observable lag. + +`mergedItemsState` is a plain `var` (not `mutableStateOf`) — this is fine because (a) it is reassigned on every recomposition of `SelectionHandler` to the same `State` reference, and (b) the values inside it are read through `State.value`, which Compose tracks. The pattern is identical to the existing `var listState: State? = null` field on the manager. + +## 6. Behaviour changes — full inventory + +1. **Selection follows the original messages when the items list mutates.** This is the bug fix. +2. **Selection clears if either anchor item is removed from the list** (e.g. message deleted from another session). Previously, indices silently slid onto neighbouring messages. The new behaviour is `clearSelection()` when `indexOfFirst` returns -1. This is a side-effect of anchoring by id — once the anchor is gone, "the selection" is no longer well-defined. It is the same class of bug as #6.1 and is fixed by the same mechanism. +3. **Defensive `?: return` in `startSelection` and `updateFocusIndex`** when the id lookup fails. In practice this branch is unreachable: `mergedItemsState` is wired before any user input; the index passed in always comes from `resolveIndexAtY` (which only returns visible-item indices); `newest().item` is non-null for any merged item. No observable change, but worth flagging for completeness. + +Nothing else changes. Verified by reading the diff against master line-by-line. + +## 7. Verification + +1. **Linux desktop build** succeeded end-to-end, producing `SimpleX_Chat-x86_64.AppImage`. No compilation errors, no Compose runtime issues from the new field on the manager or the new fields on `SelectionRange`. +2. **Manual flow against the test plan**: selection persists across `new-message-arrives`, `new-message-sent`, multi-item span; deletion clears (see §6.2); drag-select & copy button behaviour preserved. + +## 8. Trade-offs and follow-ups + +The two pieces of structural debt this change knowingly leaves in place: + +1. **Anchor ids and positional indices coexist in `SelectionRange`.** Single source of truth would store only ids and derive indices on read. The cost of unifying is the rename and function-move churn, which is independent of this bug. A follow-up could collapse these into ids-only without behaviour change, scoped to its own commit. +2. **`resyncIndices` runs on every recomposition of `SelectionHandler`.** The two `indexOfFirst` calls are O(n) on the items list. If profiling ever shows this on a hot path, the cheap fix is to gate on the pointer identity of the items list (`if (lastResyncedItems !== items) { … }`) — one extra field, one branch. Not worth doing speculatively. diff --git a/plans/2026-05-08-fix-select-in-reports.md b/plans/2026-05-08-fix-select-in-reports.md new file mode 100644 index 0000000000..a1731c34d8 --- /dev/null +++ b/plans/2026-05-08-fix-select-in-reports.md @@ -0,0 +1,182 @@ +# Fix copying selected text in reports + +PR: [#6863](https://github.com/simplex-chat/simplex-chat/pull/6863) · branch `nd/fix-select-in-reports` · final commit `96d6f3222` + +## 1. Problem statement + +Report items in desktop render as a red italic *reason prefix* followed by the user's comment, e.g. `Spam: hi @alice`. The user reported that selecting `Spam: test` and pressing Ctrl-C / clicking the copy button placed only `test` on the clipboard — the `Spam: ` prefix was silently dropped. Selecting *only* the prefix produced an empty clipboard. + +A second symptom existed for any report whose comment contained a transformed segment (mention with `localAlias`, link with `showText`): dragging a selection boundary inside that segment snapped to the wrong character on release, then copy emitted the wrong text. + +Both symptoms have a single cause and the bug is desktop-only because the touch UI does not use this selection path. + +## 2. Solution summary + +`MarkdownText` builds the on-screen `AnnotatedString` as `[prefix][body]` (one composable, one layout). Compose's `layout.getOffsetForPosition(...)` therefore returns selection offsets in **display-text space**, which includes the prefix. Pre-PR, `selectedItemCopiedText` and `snapOffset` walked `ci.formattedText` from `displayOffset = 0` — i.e. they treated those offsets as **prefix-excluded body offsets**. Every offset for a report was off by `prefix.length`. + +The fix is one structural realisation: the prefix is the **leading display-space segment**, so the loop that walks `ci.formattedText` must start at `displayOffset = prefix.length`, and any portion of the selection that falls in `[0, prefix.length)` must be emitted by appending a slice of the prefix string before the loop runs. + +To prevent the same silent decoupling from re-emerging, the prefix string itself is extracted into a single source of truth — `itemPrefixText(ci)` — used by every call site that either renders or measures the prefix. + +## 3. Detailed tech design + +### 3.1 Where the offsets come from + +``` +Compose layout + └─> SelectableText / ClickableText (TextItemView.kt) + └─> getOffsetForPosition(localPos) // returns display-space offset + └─> SelectionManager.setAnchorOffset / updateFocusOffset + └─> selectedRange(...) → IntRange in display space + └─> selectedItemCopiedText(ci, sel, linkMode) // FIX site #1 + └─> snapOffset(ci, off, linkMode, expandRight) // FIX site #2 +``` + +`MarkdownText` (TextItemView.kt) builds the `AnnotatedString` in this order: + +``` +inlineContent — never present for report items +appendSender(...) — null for the CIMarkdownText path +prefix (AnnotatedString) — "${reason}: " for reports, null otherwise +text / formatted segments — the body +typingIndicator (live only) — past selectableEnd +reserve (timestamp space) — past selectableEnd +``` + +For non-report items the prefix is null and the existing identity `displayOffset = 0` holds. For reports, the body's first character lives at display offset `prefix.length`. + +### 3.2 The minimal structural change + +Pre-PR loop: + +```kotlin +var displayOffset = 0 +for (ft in formattedText) { + val segDisplay = itemSegmentDisplayText(ft, ci, linkMode) + val displayEnd = displayOffset + segDisplay.length + val overlapStart = maxOf(displayOffset, sel.first) + val overlapEnd = minOf(displayEnd, sel.last + 1) + if (overlapStart < overlapEnd) { /* emit */ } + displayOffset = displayEnd +} +``` + +Two changes only: + +1. **Seed with prefix length.** `var displayOffset = prefix.length` (or `itemPrefixText(ci).length` for `snapOffset`). Loop body is otherwise byte-for-byte identical to pre-PR. For non-reports `prefix.length == 0`, so the non-report path is unchanged. + +2. **Emit the prefix slice.** Before the loop, append the portion of `prefix` covered by the selection: + ```kotlin + if (sel.first < prefix.length) { + sb.append(prefix, sel.first, minOf(prefix.length, sel.last + 1)) + } + ``` + `selectedRange()` guarantees `sel.first ≥ 0`, so no clamping is needed at this site. + +3. **Handle the `formattedText == null` branch.** Reports with empty body have null `formattedText`, but the prefix selection still has to be returned. The early-return in the pre-PR null branch is replaced by the same `StringBuilder` path so prefix-only selections work: + ```kotlin + val formattedText = ci.formattedText ?: run { + val start = (sel.first - prefix.length).coerceAtLeast(0).coerceAtMost(ci.text.length) + val end = (sel.last + 1 - prefix.length).coerceAtMost(ci.text.length) + if (start < end) sb.append(ci.text, start, end) + return sb.toString() + } + ``` + `coerceAtLeast(0)` on `start` is required here because `sel.first - prefix.length` is negative when the selection lies entirely inside the prefix. + +### 3.3 Single source of truth + +Pre-PR, the prefix expression `if (mc.text.isEmpty()) mc.reason.text else "${mc.reason.text}: "` lived inline at two render sites: + +- `FramedItemView.kt:368` — the actual report row +- `ChatPreviewView.kt:262` — the chat list preview + +Re-introducing it inline in `TextSelection.kt` would have re-created exactly the silent coupling that produced the bug — a future change to the separator (e.g. localised colon) at the renderer would silently break copy/snap. The fix factors the expression into: + +```kotlin +// TextItemView.kt +fun itemPrefixText(ci: ChatItem): String = when (val mc = ci.content.msgContent) { + is MsgContent.MCReport -> if (mc.text.isEmpty()) mc.reason.text else "${mc.reason.text}: " + else -> "" +} +``` + +Both renderers and both selection-side functions now derive the string from this one definition. + +### 3.4 Edge cases verified + +| Case | Pre-PR | Post-PR | +|---|---|---| +| Non-report, fmt non-null (markdown) | works | byte-identical loop, works | +| Non-report, fmt null (plain text) | substring fast path | StringBuilder path, value-equivalent | +| Non-report, sel out of bounds | clamped to `[L, L]` → `""` | same | +| Report, full sel `Spam: test` | returns `test` (BUG) | returns `Spam: test` | +| Report, prefix-only sel | returns `""` (BUG) | returns prefix slice | +| Report, body-only sel | returns body (offset shift was hidden by `Int.MAX_VALUE` clamp at `sel.last+1`) | returns body | +| Report, sel.first == prefix.length | works coincidentally | works | +| Report, empty body, prefix-only sel | returns `""` | returns prefix | +| Report with mention having `localAlias` (transformed) | snap snapped to wrong char (BUG) | snaps correctly | +| Multi-item interior sel (`sel.last = MAX-1`) | works | no overflow on `+1 - prefix.length` | + +### 3.5 What was deliberately not done + +- **Performance restoration of the non-report null-fmt path.** Pre-PR returned `ci.text.substring(...)` directly (1 allocation). Post-PR uses `StringBuilder` (3 allocations). `selectedItemCopiedText` runs once per selected item per copy action — never on a hot path. Restoring the pre-PR fast path with an `if (prefix.isEmpty() && formattedText == null)` early return adds 4 lines of branching for negligible gain. Not worth it. + +- **Migrating `ChatPreviewView.kt` was kept** because it crossed the 3-site extraction threshold (FramedItemView + ChatPreviewView + 2× TextSelection) and the bug we are fixing is exactly the failure mode of duplicating this expression. ChatPreviewView is not a selection site, so no behaviour change — it shifts to the same single source of truth. + +## 4. Detailed implementation plan + +### 4.1 Files touched (final state) + +| File | Δ | Purpose | +|---|---|---| +| `apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/TextItemView.kt` | +7 / 0 | new `itemPrefixText(ci)` helper next to `itemDisplayText` | +| `apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/FramedItemView.kt` | +1 / −1 | report branch delegates to `itemPrefixText(ci)` | +| `apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatPreviewView.kt` | +2 / −2 | preview row delegates; drops unused `val mc =` | +| `apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/TextSelection.kt` | +16 / −8 | the actual fix in `selectedItemCopiedText` and `snapOffset`, plus import | + +Total: 4 files, +26 / −11. + +### 4.2 Step-by-step (final commit `96d6f3222`) + +1. **Add `itemPrefixText(ci)`** in `TextItemView.kt` next to `itemDisplayText` / `itemSegmentDisplayText`. Returns `""` for non-reports. + +2. **`FramedItemView.kt:365-372`** (`MCReport` branch): replace inline expression with `append(itemPrefixText(ci))`. The surrounding `withStyle(SpanStyle(color = Red, italic))` is preserved — visual rendering unchanged. + +3. **`ChatPreviewView.kt:258-264`**: replace inline expression with `append(itemPrefixText(ci))`. Drop the now-unused `val mc =` from `when (val mc = ci.content.msgContent)` (the discriminator becomes `when (ci.content.msgContent)`). + +4. **`TextSelection.kt`**: + - Add `import chat.simplex.common.views.chat.item.itemPrefixText`. + - In `selectedItemCopiedText`: + - Compute `val prefix = itemPrefixText(ci)` and `val sb = StringBuilder()` first. + - Emit prefix slice if `sel.first < prefix.length`. + - Modify the `formattedText ?: ...` early-return to a `?: run { … }` block that adds the body slice (offsets shifted by `-prefix.length`, clamped) to `sb` and returns `sb.toString()`. + - Seed the formattedText loop with `var displayOffset = prefix.length`. Loop body unchanged. + - In `snapOffset`: change `var displayOffset = 0` to `var displayOffset = itemPrefixText(ci).length`. Loop body unchanged. + - Update the docstring on `selectedItemCopiedText` to note that display-text space includes any leading `itemPrefixText`. + +### 4.3 Verification + +- `./gradlew :common:compileKotlinDesktop` — passes (warnings are pre-existing). +- `bash /home/user/build/linux.sh` — full Linux x86_64 AppImage produced (`SimpleX_Chat-x86_64-fix-select-in-reports.AppImage`). +- Manual test plan, all in desktop: + 1. Open a chat with a report whose rendered form is `Spam: test`. Select across the whole line + Ctrl-C → clipboard reads `Spam: test`. + 2. Select only the red prefix → clipboard reads the prefix. + 3. Select only the comment → clipboard reads the comment. + 4. Report comment containing `@alice (Bob)` (mention with localAlias). Drag a selection boundary into the mention → on release, highlight snaps to mention boundaries. + 5. Plain (non-report) messages: full-line, partial, mention, link selections — clipboard contents unchanged from pre-PR. + 6. Multi-item selection across non-report and report rows — prefixes appear inline at the correct positions. + +### 4.4 Risk and rollback + +- **Blast radius** is the desktop selection-copy code path. iOS / Android use separate selection mechanisms and are unaffected. +- The non-report selection path's inner loop body is byte-for-byte identical to pre-PR (the `displayOffset = 0` initialisation is unchanged when `prefix.length == 0`), so regressions on non-reports would require the prefix expression itself to fail — which is impossible because `itemPrefixText` returns `""` for any `msgContent` other than `MCReport`. +- Rollback is `git revert 96d6f3222 e97dd7bf4 6aacfa4d2` (three commits) and a force-push, restoring the pre-PR copy behaviour with the original bug. + +## 5. Why this specific shape + +- Recognising the prefix as the *first* display-space segment turns the bug into a one-line seed change. No special-cased report branch in copy/snap; the existing loop handles both. +- The inner loop of `selectedItemCopiedText` and `snapOffset` is byte-for-byte identical to pre-PR. Only the seed value of `displayOffset` and the pre-/post-amble change. +- Four sites need the prefix string (FramedItemView, ChatPreviewView, and two in TextSelection). `itemPrefixText` becomes their single point of change, closing the silent-coupling gap that produced the bug. +- `selectedRange()` guarantees `sel.first ≥ 0`, so no `coerceAtLeast(0)` is added at the prefix-slice append. The one `coerceAtLeast(0)` that survives (on the `formattedText == null` body branch) is reachable when the selection lies entirely inside the prefix and is needed. +- Final PR is 4 files, +26 / −11. The inner loop body changes by zero lines. diff --git a/plans/2026-05-08-relay-announce-impl.md b/plans/2026-05-08-relay-announce-impl.md new file mode 100644 index 0000000000..f617c78a31 --- /dev/null +++ b/plans/2026-05-08-relay-announce-impl.md @@ -0,0 +1,695 @@ +# Implementation plan: owner-pushed relay announcement (`XGrpRelayNew`) + +Companion to `/workspace/plans/2026-05-08-relay-announce.md` (overview). This file is the +file-and-symbol-level diff guide. Read the overview first. + +All file/line references are against the working tree at the start of the implementation; +they will drift slightly as edits land. Cite this plan when something looks unfamiliar. + +--- + +## 1. Step ordering and commit shape + +Compilation must hold after every step. The order below is the smallest reviewable +sequence; steps S1–S5 are intentionally split into two PRs: a wire-format-only PR and a +behaviour PR, so reviewers can evaluate the new event in isolation. + +PR 1 — wire format (compiles, no behaviour change) + +- S1 `Protocol.hs`: add `XGrpRelayNew` constructor, tag `x.grp.relay.new`, + `toCMEventTag`, JSON encode/parse, `isForwardedGroupMsg` row. +- S2 `Protocol.hs`: extend `requiresSignature` to include `XGrpRelayNew_`. +- S3 `docs/protocol/channels-protocol.md`: signing-table row + new "Relay addition" + subsection. + +PR 2 — receive + send + forward (one logical change) + +- S4 `Store/Groups.hs`: add active-status filter in place to the inner + `getGroupMemberByRelayLink` lookup inside `getCreateRelayForMember`. +- S5 `Library/Internal.hs`: introduce `connectToRelayAsync`. Move `syncSubscriberRelays` + from `Commands.hs` to `Internal.hs` and pivot its add-half to `connectToRelayAsync`. +- S6 `Library/Commands.hs`: drop the now-unused sync `connectToRelay`; `APIConnectPreparedGroup` + keeps the existing sync call (see §6 — left in place); update import of + `syncSubscriberRelays`. Keep `retryRelayConnectionAsync` as-is. +- S7 `Library/Subscriber.hs`: add forward-only case to `processEvent`, add + `XGrpRelayNew` case to `processForwardedMsg`, add owner send at end of LINK callback. +- S8 Tests in `tests/ChatTests/Channels.hs` (or split across files per §11). + +S1–S3 land in PR 1; S4–S8 in PR 2. PR 2 must not be split: the owner-side send and the +subscriber-side handler must ship together to avoid asymmetry where one direction is +emitted but not consumed. + +--- + +## 2. `Protocol.hs` — wire format (S1, S2) + +### 2.1 GADT constructor (Protocol.hs:443-445) + +Add at line 446 (immediately after `XGrpRelayTest`, before `XGrpMemNew`): + +``` +XGrpRelayNew :: ShortLinkContact -> ChatMsgEvent 'Json +``` + +Rationale: keeps the relay-related events grouped. Single `ShortLinkContact` field, no +record syntax, mirrors `XGrpRelayAcpt :: ShortLinkContact -> ChatMsgEvent 'Json` at +Protocol.hs:444. **Do not** introduce a record wrapper or `RelayInfo` envelope — the +overview locked the shape to a single field; the receiver looks the link up locally. + +### 2.2 Tag GADT and string encoding (Protocol.hs:986-988, 1043-1045, 1101-1103) + +- Insert `XGrpRelayNew_ :: CMEventTag 'Json` after `XGrpRelayTest_` (line 988). +- In `strEncode`, add `XGrpRelayNew_ -> "x.grp.relay.new"` after `XGrpRelayTest_` (line 1045). +- In `strDecode` map (line 1103), add `"x.grp.relay.new" -> XGrpRelayNew_` after + `"x.grp.relay.test" -> XGrpRelayTest_`. + +The `_` -> `XUnknown_` fallback at line 1129 already gives correct old-client behaviour; +no change there. + +### 2.3 `toCMEventTag` (Protocol.hs:1133-1184) + +Add `XGrpRelayNew _ -> XGrpRelayNew_` after the `XGrpRelayTest` line (1157). + +### 2.4 JSON parse / encode (Protocol.hs:1308-1314, 1378-1382) + +- `appJsonToCM`/`msg` parser (1271-1344): add + `XGrpRelayNew_ -> XGrpRelayNew <$> p "relayLink"` + immediately after `XGrpRelayAcpt_` (line 1309). Field name `"relayLink"` matches the + `XGrpRelayAcpt` precedent (1309) — do not invent a new key. +- `chatToAppMessage`/`params` encoder (1354-1410): add + `XGrpRelayNew relayLink -> o ["relayLink" .= relayLink]` + after the `XGrpRelayAcpt` clause (1379). Same key. + +### 2.5 `isForwardedGroupMsg` (Protocol.hs:484-503) + +Add a single case `XGrpRelayNew _ -> True` in the listed group of `True` cases (e.g. +between `XGrpMemNew {} -> True` (495) and `XGrpMemRole {} -> True` (496)). Rationale: +relays must forward this event to subscribers; it is the entire point. The comment +above the function (line 482) already says actual filtering happens in `processEvent`; +the listing here is for the send-side `memberSendAction` decisions about pre-member +forwarding (Internal.hs:2202), which we want to behave the same as `XGrpMemNew`. + +### 2.6 `requiresSignature` (Protocol.hs:1221-1231) + +Add `XGrpRelayNew_ -> True` to the list. Rationale: this is an administrative event; +must reuse the existing required-signature gate. Without this, `withVerifiedMsg` +(Subscriber.hs:3385-3407) would treat a missing signature as acceptable +(`signatureOptional` becomes `True`), breaking the threat model from +`channels-protocol.md` §"Message signing". + +### 2.7 What NOT to change + +- Do not touch `hasNotification` or `hasDeliveryReceipt` — relay-add is administrative, + not a notification surface for the user. The relay's delivery pipeline (delivery_task / + delivery_job) already handles forwarding without an entry in either table. +- Do not touch `unverifiedAllowed` (Protocol.hs:1240-1249). Owners always know their own + key; subscribers always have the owner key from link data. The "no key" branch is for + member-to-member events, not for owner-signed administrative events. + +--- + +## 3. `Store/Groups.hs` — active-status filter on relay-link lookup (S4) + +### 3.1 The current shape (Store/Groups.hs:1376-1407) + +`getCreateRelayForMember` runs `getGroupMemberByRelayLink` (an inner `let` at 1380-1385), +falls back to `createRelayMember`. The inner SQL filters on `group_id = ? AND relay_link += ?` only — no status filter. The schema permits multiple rows with the same +`(group_id, relay_link)` over time: when a relay is removed by the owner, its row is +preserved with `GSMemLeft` (this drives the "removed by operator" UI on the subscriber +side). For the existing subscriber-join flow (`APIConnectPreparedGroup → connectToRelay`, +Commands.hs:2141 / 3597-3613) the unfiltered lookup happens to work because rows in that +path are recent and active. For the new subscriber receive path we must filter to *active* +rows so that a re-add after a `GSMemLeft` creates a fresh row instead of resurrecting the +historical one. + +### 3.2 The change + +Add an active-status filter in place to the existing inner `let`. No extraction, no new +top-level function: + +``` +getGroupMemberByRelayLink = + maybeFirstRow (toContactMember vr user) $ + DB.query + db + (groupMemberQuery <> " WHERE m.group_id = ? AND m.relay_link = ? AND m.member_status IN (?,?,?,?,?,?,?)") + ( (groupId, relayLink) + :. (GSMemIntroduced, GSMemIntroInvited, GSMemAccepted, GSMemAnnounced) + :. (GSMemConnected, GSMemComplete, GSMemCreator) + ) +``` + +The seven statuses are the `memberCurrent'`-true set from Types.hs:1318-1334: +`GSMemIntroduced`, `GSMemIntroInvited`, `GSMemAccepted`, `GSMemAnnounced`, +`GSMemConnected`, `GSMemComplete`, `GSMemCreator`. Tuple shape is illustrative — match the +existing `:.` chaining convention used elsewhere in the module. + +Justification for SQL-level filter (vs. Haskell post-filter): `maybeFirstRow` returns +whatever row the engine yields first. With `GSMemLeft` history rows preserved alongside +active rows, an unfiltered query is non-deterministic without `ORDER BY`. Filtering in +SQL eliminates the ambiguity at the query level. The list of statuses is tiny and +stable. + +### 3.3 Existing call site unaffected + +`getCreateRelayForMember`'s lone existing caller is `connectToRelay` (Commands.hs:3597-3613), +invoked from `APIConnectPreparedGroup` (Commands.hs:2141). Rows it creates are inserted +with `GSMemAccepted` (line 1403), which is `memberCurrent`. The filtered lookup still +finds them on retry, so the subscriber-join flow's reuse-on-retry behaviour is preserved. +No signature or call-site change is needed in `Commands.hs`. + +### 3.4 What NOT to change + +- Do not extract `getGroupMemberByRelayLink` to a top-level function. The + filter-in-place shape is the minimal diff; both call sites (existing + `APIConnectPreparedGroup → connectToRelay` and new `connectToRelayAsync`) share one + definition by going through `getCreateRelayForMember`. +- Do not modify `getGroupMember`, `getGroupMembers`, or other lookups. The change is + scoped to the relay-link lookup inside `getCreateRelayForMember`. +- Do not delete the historical `GSMemLeft` row when re-adding a relay. The + delete-or-update logic in `syncSubscriberRelays` removes only when the link is no + longer in the channel's link data (Commands.hs:3623-3633); on re-add it remains in + link data, so the historical row stays untouched and is filtered out by the new + lookup. + +--- + +## 4. `Library/Internal.hs` — `connectToRelayAsync` and moved `syncSubscriberRelays` (S5) + +### 4.1 New helper + +Place near the existing relay/group plumbing (e.g. after `setGroupLinkDataAsync` at +Internal.hs:1316-1322) so that all relay-link async helpers cluster together. + +``` +connectToRelayAsync :: User -> GroupInfo -> ShortLinkContact -> CM () +``` + +Body — described, not coded: + +1. `vr <- chatVersionRange`. +2. `gVar <- asks random` — needed by `getCreateRelayForMember` via the create branch. +3. `relayMember <- withFastStore $ \db -> getCreateRelayForMember db vr gVar user gInfo relayLink`. + With the active-status filter from §3.2, this atomically returns the existing active + row (if any) or creates a fresh `GSMemAccepted` row. `GSMemLeft` history rows are + invisible to the lookup, so re-add after removal creates a new row beside the + historical one. +4. Idempotence check on `activeConn relayMember`: + + - `Just _` → `pure ()` (skip; an earlier path already bound a connection on this + row. The agent layer handles transient failures internally; permanent-failure + recovery is deferred to explicit retry paths and channel re-join.) + - `Nothing` → either a freshly created row or a leftover row from an attempt that + never bound a connection; proceed to step 5. +5. `subMode <- chatReadVar subscriptionMode`. +6. `newConnIds <- getAgentConnShortLinkAsync user CFGetRelayDataJoin Nothing relayLink` + (Commands.hs:2479 — already returns `(CommandId, ConnId)` for binding). +7. `withFastStore' $ \db -> createRelayMemberConnectionAsync db user gInfo relayMember relayLink newConnIds subMode` + (Direct.hs:225-244). +8. Return. Continuation is the existing `CFGetRelayDataJoin` LDATA callback at + Subscriber.hs:1131-1160 — unchanged. + +Store-call conventions: `getCreateRelayForMember` is `ExceptT StoreError IO`, so use +`withFastStore`. `createRelayMemberConnectionAsync` is `IO`, so `withFastStore'`. Both +match what `retryRelayConnectionAsync` (Commands.hs:2168-2174) and `connectToRelay` +(Commands.hs:3597-3613) already use. + +### 4.2 Locking argument + +`connectToRelayAsync` is called from two sites (after this PR): +- The forwarded `XGrpRelayNew` handler in `processForwardedMsg`. The entire receive path + is wrapped in `withEntityLock "processAgentMessage" lockEntity` (Subscriber.hs:117) and + the lock entity for any group connection is `CLGroup groupId` (Connections.hs:51-72). +- `syncSubscriberRelays`, called from `APIGetUpdatedGroupLinkData` inside + `withGroupLock "syncSubscriberRelays" groupId` (Commands.hs:1787) — also `CLGroup groupId`. + +Both paths therefore hold the same lock for the same group. The `getCreateRelayForMember` +call (lookup-or-create, atomic within its own transaction) and the `activeConn` check on +its result are performed under that lock, and any subsequent agent commands +(`getAgentConnShortLinkAsync`, `createRelayMemberConnectionAsync`) only persist state +that will be observed under the same lock by the next event's check. No additional lock +is needed. No `justCreated` flag, no per-link mutex. + +### 4.3 Move `syncSubscriberRelays` from `Commands.hs:3614-3633` to `Internal.hs` + +Place right below `connectToRelayAsync`. Body changes: + +- Replace the single `connectToRelay` call inside the `forM_ newRelayLinks` loop + (Commands.hs:3621-3622) with `connectToRelayAsync user gInfo rlnk`. Keep the + per-relay `void . tryAllErrors` wrapping verbatim — equivalent to the existing + pattern at Commands.hs:3621-3622 with only the connect helper substituted: + + ``` + forM_ newRelayLinks $ \rlnk -> void . tryAllErrors $ + connectToRelayAsync user gInfo rlnk + ``` + + `connectToRelayAsync` can fail at three local operations + (`getCreateRelayForMember` → store error if creating; `getAgentConnShortLinkAsync` + → agent error; `createRelayMemberConnectionAsync` → store error). Per-relay error + isolation costs nothing and ensures a failure on relay R1 does not short-circuit + attempts for R2, R3 in the same batch. The outer `void . tryAllErrors` (3615) is + preserved as well; it remains the catch-all for the whole sync operation. +- Remove half: keep verbatim — `deleteMemberConnection`, `deleteOrUpdateMemberRecord` + calls (3631-3632), the `null activeRelayMembers` guard (3629), and the + `localRelayMembers` filter (3617). + +Type signature after move (matches current except for module location): + +``` +syncSubscriberRelays :: User -> GroupInfo -> [ShortLinkContact] -> CM () +``` + +### 4.4 Imports / exports + +- `Internal.hs` likely already imports the relevant `Store.Groups`/`Store.Direct` + symbols; if `getCreateRelayForMember` or `createRelayMemberConnectionAsync` are not + imported, add them. +- Export `connectToRelayAsync` and `syncSubscriberRelays` from `Internal.hs` (it is a + module without an explicit export list — see "module Simplex.Chat.Library.Internal where" + near top — so any new top-level binding is automatically exported). + +### 4.5 What NOT to change + +- Do not change `connectToRelay` (sync, Commands.hs:3597-3613) signature. PR 2 keeps it + alive for the subscriber's initial channel-join — see §5.1. +- Do not touch `retryRelayConnectionAsync` (Commands.hs:2168-2174). Its retry semantics + are tied to the subscriber's initial channel-join (`APIConnectPreparedGroup`, + Commands.hs:2108-2161) and remain on that path. +- Do not introduce any new `withGroupLock` inside `connectToRelayAsync`. The caller's + lock is sufficient (see §4.2). + +--- + +## 5. `Library/Commands.hs` — drop unused sync helper, fix imports (S6) + +### 5.1 Decide what to delete + +Audit `connectToRelay` callers: only `APIConnectPreparedGroup` (Commands.hs:2108-2161) +uses it. That command is the **subscriber's** initial channel-join entry point +(not owner channel creation — owner-side relay invitation flows through +`APIAddGroupRelays` and `x.grp.relay.inv`/`x.grp.relay.acpt`, see channels-protocol.md +§"Relay acceptance"). At join time, the subscriber does +`mapConcurrently (connectToRelay user gInfo') relays` (Commands.hs:2141) to connect to +all relays in parallel during the join handshake. + +The sync flow is intentional there: +- the user is on a "joining channel" spinner; +- failures must surface immediately to UI so the user sees a meaningful error + instead of a stuck spinner; +- the existing flow already chains async retry via `retryRelayConnectionAsync` + (Commands.hs:2159) for the relays that fail with temporary errors — sync handles + the immediate-feedback path, async handles tail recovery. + +**Default; reviewer to confirm**: keep `connectToRelay` for the +`APIConnectPreparedGroup` path. The overview's "deletable once event-driven path is +wired" was conditional ("once no caller remains"). Subscriber join has different UX +semantics from event-driven relay sync; convergence onto async-only is a separate +concern and is out of scope for this PR. + +### 5.2 Move `syncSubscriberRelays` reference + +`APIGetUpdatedGroupLinkData` at Commands.hs:1787-1788 currently references +`syncSubscriberRelays` as a local where-binding inside `processChatCommand` (it is the +inner `where`-defined function at 3614). After moving it to `Internal.hs`, the call site +at 1788 unchanged but the local binding at 3614-3633 deleted. Imports auto-rerouted via +`Simplex.Chat.Library.Internal` (already imported at the top of Commands.hs). + +### 5.3 What NOT to change + +- Do not change `APIGetUpdatedGroupLinkData`'s `withGroupLock` wrapper or the `gInfo'` + it passes to the sync function. The lock and the link-data refresh are still required. +- Do not change `retryRelayConnectionAsync`. It is the right primitive for the + subscriber-join retry use case (`APIConnectPreparedGroup` tail recovery, + Commands.hs:2159); the new event-driven path is independent. + +--- + +## 6. `Library/Subscriber.hs` — owner send, relay forward, subscriber receive (S7) + +### 6.1 Owner — send site in LINK callback (Subscriber.hs:1300-1333) + +The relevant block: + +``` +LINK _link auData -> + withCompletedCommand conn agentMsg $ \CommandData {cmdFunction} -> + case cmdFunction of + CFSetShortLink -> + case (ucGroupId_, auData) of + (Just groupId, UserContactLinkData UserContactData {relays = relayLinks}) -> do + (gInfo, gLink, relays, relaysChanged) <- withStore $ \db -> do + gInfo <- getGroupInfo db vr user groupId + gLink <- getGroupLink db user gInfo + relays <- liftIO $ getGroupRelays db gInfo + (relays', changed) <- liftIO $ foldrM (updateRelay db) ([], False) relays + liftIO $ setGroupInProgressDone db gInfo + pure (gInfo, gLink, relays', changed) + toView $ CEvtGroupLinkDataUpdated user gInfo gLink relays relaysChanged + where + updateRelay db relay@GroupRelay {relayLink, relayStatus} (acc, changed) = + case relayLink of + Just rLink + | rLink `elem` relayLinks && relayStatus == RSAccepted -> do + relay' <- updateRelayStatus db relay RSActive + pure (relay' : acc, True) + ... +``` + +Plan: + +1. Extend the `updateRelay` accumulator from `([GroupRelay], Bool)` to + `([GroupRelay], Bool, [ShortLinkContact])`: keep the existing `Bool` for the + `CEvtGroupLinkDataUpdated`'s `relaysChanged` flag, and add a new + `[ShortLinkContact]` collecting the links of relays that just transitioned + `RSAccepted → RSActive`. In the `RSAccepted → RSActive` branch, replace + `pure (relay' : acc, True)` with `pure (relay' : acc, True, rLink : newlyActiveLinks)`. + In the `RSActive → RSInactive` branch (which also sets `changed = True` today, + line 1330), keep the `Bool` flip but pass `newlyActiveLinks` through unchanged + — removals are explicitly out of scope for the announce push (overview + §"Owner — send site"). Other branches pass both extra fields through unchanged. +2. Bind `(gInfo, gLink, relays, relaysChanged, newlyActiveLinks)` from the `withStore` + block; pass `relaysChanged` to the existing `CEvtGroupLinkDataUpdated` `toView` + call so its semantics are preserved exactly; pass `newlyActiveLinks` to the new + send block in step 3. + +3. After the `toView`, add (still inside `(Just groupId, UserContactLinkData ...)` + case). The send block fetches all relay members and filters inline (see §6.2): + + ``` + let newlyActiveLinks = ... -- collected from the fold accumulator + forM_ (L.nonEmpty newlyActiveLinks) $ \newlyActive -> do + allRelayMembers <- withFastStore' $ \db -> getGroupRelayMembers db vr user gInfo + let recipients = filter + (\m -> memberStatus m == GSMemConnected && relayLink m `notElem` newlyActiveLinks) + allRelayMembers + events = XGrpRelayNew <$> newlyActive + unless (null recipients) $ + void $ sendGroupMessages user gInfo Nothing False recipients events + ``` + + - `sendGroupMessages` signature (Internal.hs:2049): `User -> GroupInfo -> Maybe + GroupChatScope -> ShowGroupAsSender -> [GroupMember] -> NonEmpty (ChatMsgEvent e) -> CM (NonEmpty (Either ChatError SndMessage), GroupSndResult)`. + - `Nothing` for `Maybe GroupChatScope`: this is administrative, not scoped to a + support side-channel. Justified by `XGrpInfo` / `XGrpPrefs` send patterns elsewhere + where signed admin events use `Nothing`. + - `False` for `ShowGroupAsSender`: this is signed by the owner; relays must verify + the owner signature via `withVerifiedMsg` (Subscriber.hs:3385). `asGroup = True` + uses `CBChannel` binding (channels-protocol.md §"Channel-as-sender"), which has no + member ID and is not what we want — verification needs the owner's member ID. + - `void` discards the per-member result; logging is handled by the existing send + pipeline. + +### 6.2 Recipients query + +No new Store helper. Inline the filter in the LINK callback: + +- After the `withStore` block that runs the fold, call + `withFastStore' $ \db -> getGroupRelayMembers db vr user gInfo` to get + `[GroupMember]` (Store/Groups.hs:1185-1191). +- Filter in Haskell: + `filter (\m -> memberStatus m == GSMemConnected && relayLink m \`notElem\` newlyActiveLinks)`. +- `memberStatus == GSMemConnected` already implies `memberCurrent` (Types.hs:1318-1334); + do not add a redundant `memberCurrent` check. +- Pass the filtered list as the recipients argument to `sendGroupMessages`. + +Justification: one-shot use, low frequency (LINK callback only), no benefit +to introducing a new Store function. `vr` and `user` are already in scope at +the LINK callback (Subscriber.hs:1306, inside `processContactConnMessage`). + +### 6.3 Defensive batching + +Per overview, the receive-loop group lock serializes `XGrpRelayAcpt` handling (which +calls `setGroupLinkDataAsync`) so each LINK callback typically sees a single +`RSAccepted → RSActive` transition. Coding the send as `NonEmpty (XGrpRelayNew _)` keeps +the path correct if the agent ever consolidates `setConnShortLink` writes. The +`L.nonEmpty newlyActive` guard handles the empty case (no transition this callback). + +### 6.4 Relay — `processEvent` case (Subscriber.hs:990-1032) + +Insert this case before the catch-all `_ -> Nothing <$ messageError ...` at 1032: + +``` +XGrpRelayNew _ -> pure $ Just (DeliveryTaskContext (DJSGroup {jobSpec = DJDeliveryJob {includePending = False}}) False) +``` + +Justification by precedent: +- `XMsgNew` (991) → `newGroupContentMessage` returns `Just (ctx js)` where `ctx` is + `DeliveryTaskContext js False` (line 983). The `False` is the "don't include in + history" flag — relay forwards but doesn't snapshot. +- `XGrpMemNew` (1011) → `xGrpMemNew` returns `Just (ctx (DJSGroup {…}))`. We want + identical broadcast scope (all subscribers, no support-only channel). +- `XGrpDel` (1022) is the only event that uses `DJRelayRemoved`; that is for + relay-removal-by-owner, not relevant here. + +`DJDeliveryJob {includePending = False}` matches `XMsgNew`'s default (search +`Delivery.hs` for `DJDeliveryJob` constructor — `includePending = False` is the +non-administrative-state-change default; `XGrpInfo` uses `True` because it changes +group profile state and pending members must learn it on accept). The relay +**stores no member record for the announced relay** (overview §"Relay — forward +only"), so subscribers entering pending state later will instead learn via on-open +`syncSubscriberRelays`. `includePending = False` is correct. + +What NOT to do: +- Do not add an `xGrpRelayNew` handler on the relay side — the relay is forward-only. +- Do not create a `GroupMember` record for the announced relay on the relay. Departure + from `XGrpMemNew` semantics is intentional; relays don't connect to other relays of + the same channel. + +### 6.5 Subscriber — `processForwardedMsg` case (Subscriber.hs:3354-3383) + +Add to the inner `case event of` (just before the catch-all `_ -> messageError ...` at +3378): + +``` +XGrpRelayNew rl -> withAuthor XGrpRelayNew_ $ \_author -> connectToRelayAsync user gInfo rl +``` + +Notes: +- `withAuthor` (3380-3383) requires `author_ :: Maybe GroupMember` to be `Just` — + enforces the "must be attributable to a signing owner" invariant. `FwdChannel` (3351 + via `processForwardedMsg (VMUnsigned chatMsg) Nothing`) makes `author_ = Nothing`, + which `withAuthor` rejects with `messageError`. This is the desired behaviour: the + event must be owner-signed and attributed. +- Signature verification happens upstream in `withVerifiedMsg` (3385-3407) before + `processForwardedMsg` is invoked (3348-3349). With `requiresSignature` returning + `True` for `XGrpRelayNew_` (§2.6), an unsigned forwarded `XGrpRelayNew` triggers the + bad-signature path at 3389-3391. +- The `_author` is used only as an authorisation token here. The connect helper does + not need the author identity — the author is the owner whose link data already + carried the relay key, and the relay member's keys/profile are fetched from the + relay's own short link. + +### 6.6 `xGrpMsgForward` — no change needed + +Already validates the forwarder is a relay (`isMemberGrpFwdRelay`, 3340) and dispatches +to `processForwardedMsg`. Adding the new event tag inside that switch is the entirety +of the receive-side change. + +### 6.7 What NOT to change in `Subscriber.hs` + +- Do not touch the `CFGetRelayDataJoin` LDATA callback (1131-1160). Its end state + (subscriber-side) is exactly the continuation we want; the helper hands off to it. +- Do not touch the `CON` handler at 823-865 for relay members. The `firstConnectedHost` + branch (855-859) handles the first-connected-relay UI events; subsequent relays go + through 859. After `XGrpRelayNew`-driven connect, the new relay's `CON` will land in + this same handler and get `firstConnectedHost = False` (because at least one relay is + already connected), which is correct. +- Do not modify the `CONF`/`XGrpRelayAcpt` path at 768-772. That is owner-side. + +--- + +## 7. `docs/protocol/channels-protocol.md` updates (S3) + +### 7.1 Signing-required table + +Section: `## Protocol → ### Message signing → "Which messages require signatures:"` +table (lines 84-97). Add a row after `x.grp.mem.restrict`: + +``` +| `x.grp.relay.new` | Announce new relay to subscribers | Required | +``` + +Phrasing matches existing `Description` cells (verb + object). + +### 7.2 New subsection "Relay addition" + +Insert after the existing `### Relay acceptance` subsection (lines 42-58). Heading +level `###`, four short paragraphs: + +1. **Owner-side trigger.** When the owner has accepted a relay (existing flow, + `x.grp.relay.acpt` at line 36) and the agent confirms the link-data update by + delivering the LINK event, the owner promotes the relay locally to active and + sends `x.grp.relay.new` to every other currently-connected relay of the channel + (excluding the relay being announced). +2. **Wire format.** Single-field JSON object: `{"relayLink": ""}`. + Owner-signed via the same `CBGroup` binding prefix used for all administrative + events (see [Message signing](#message-signing)). +3. **Relay forwarding semantics.** Each relay forwards `x.grp.relay.new` verbatim to + all of its subscribers via the standard delivery pipeline (delivery_task / + delivery_job, see [Delivery pipeline](#delivery-pipeline)). The relay does **not** + create a member record for the announced relay — relays do not connect to other + relays of the same channel. +4. **Subscriber receive semantics.** The subscriber resolves the announced short link + asynchronously, creates a relay-member row (or reuses an existing active row), and + binds the resulting agent connection without blocking the receive loop. If the + subscriber's client doesn't recognise the event (older version), it is parsed as + `XUnknown` and ignored; the next `APIGetUpdatedGroupLinkData` (channel open) reaches + the same end state via `syncSubscriberRelays`. +5. **Idempotence.** The receive loop wraps each agent message in a per-group entity + lock (`CLGroup groupId`); the same lock is held by `APIGetUpdatedGroupLinkData`. + A duplicate `x.grp.relay.new` arriving from a second relay finds an active row + + active connection and is a no-op. + +### 7.3 What NOT to change + +- Do not renumber existing sections. +- Do not modify the `Binary batch format` section — `x.grp.relay.new` is a + `signedElement` like every other administrative event; no new ABNF. +- Do not touch the `Channel-as-sender messages` section — `XGrpRelayNew` is owner-bound, + `CBGroup`, never `CBChannel`. + +--- + +## 8. Test plan (S8) + +All tests live under `tests/ChatTests/Channels.hs` (or a dedicated +`tests/ChatTests/Channels/RelayAnnounce.hs` if the file is getting unwieldy — confirm +with reviewer). Each test maps to a row in the overview's "Test surface". + +| Overview test | Concrete test name | Harness | +|---|---|---| +| Owner adds relay → subscribers receive `XGrpRelayNew` and connect without channel open | `testRelayAnnounceOnlineSubscriber` | uses `testChat3` (owner + relay + subscriber); after channel is up, owner adds a second relay; assert subscriber's relay-member count for that group becomes 2 with both `GSMemConnected`, no `APIGetUpdatedGroupLinkData` invoked. | +| Two relays forward the announce; subscriber connects exactly once | `testRelayAnnounceDedupes` | `testChat4` (owner + 2 existing relays + subscriber); owner adds third relay; both existing relays forward; assert exactly one new relay-member row, exactly one connection. Inspect via `withCCStore (getGroupRelayMembers …)`. | +| Race vs. `APIGetUpdatedGroupLinkData` for same relay | `testRelayAnnounceRaceWithSync` | drive `APIGetUpdatedGroupLinkData` and `XGrpRelayNew` concurrently; assert no double row; rely on the existing `withGroupLock` to serialize. | +| `GSMemLeft` row preserved on re-add | `testRelayAnnounceReAddPreservesHistory` | owner adds relay, removes it, adds again with same link; assert two `GroupMember` rows for that link (one `GSMemLeft`, one current); the historical row is what drives the "removed by operator" UI. | +| Old subscriber ignores | `testRelayAnnounceOldSubscriber` | use `chatVersionRange` overrides to simulate an older subscriber; assert event is logged as unknown but produces no error item; `syncSubscriberRelays` invocation on next channel open creates the row. | +| Old relay drops | `testRelayAnnounceOldRelay` | inverse: relay's `chatVersionRange` does not include `XGrpRelayNew_` → `processEvent` default `messageError "unsupported"`. Subscribers fall back to on-open sync. | +| Bad signature | `testRelayAnnounceBadSignature` | inject an unsigned (or wrong-signed) `XGrpRelayNew` directly via the test SMP harness; assert `RGEMsgBadSignature` chat item is created on subscriber. | + +Helpers reused: `withSmpServer`, `testChat`, `testChat3`, `testChat4`, `awaitListChat`, +`withCCStore`, `getGroupRelayMembers`. Add a small helper in the test module +`assertRelayMemberCount :: TestCC -> GroupId -> Int -> IO ()` if not already present. + +For the dedup test specifically, assertion shape: + +``` +m <- getGroupRelayMembers db vr user gInfo +let relayRows = filter (\GM -> relayLink GM == Just newRl && memberCurrent GM) m +length relayRows `shouldBe` 1 +length (filter (isJust . activeConn) relayRows) `shouldBe` 1 +``` + +--- + +## 9. Risk register + +1. **Race: event arrives during channel open.** The receive loop and + `APIGetUpdatedGroupLinkData` share `CLGroup groupId`. Whichever path runs first + creates/uses the row; the second sees an active row + active conn (or creates one + if not yet) and is a no-op. Tested via `testRelayAnnounceRaceWithSync`. + +2. **Agent coalescing of `setConnShortLink` writes.** Today the receive-loop group lock + serializes `XGrpRelayAcpt` handling, so each LINK callback sees one transition. If + the agent ever batches multiple writes into one callback, the `NonEmpty + (XGrpRelayNew _)` send path stays correct: every newly-active relay gets announced. + No fix needed; defensive shape is already there. + +3. **Old relay between owner and subscriber.** Old relay's `processEvent` default branch + drops the event with `messageError "unsupported"`. Subscribers behind that relay + recover via on-open `syncSubscriberRelays`. Documented in the protocol doc and + covered by `testRelayAnnounceOldRelay`. + +4. **Malformed signature.** `requiresSignature XGrpRelayNew_ = True` causes + `withVerifiedMsg` to reject and produce `RGEMsgBadSignature`. Standard path; tested. + +5. **Agent error during `getAgentConnShortLinkAsync` (step 3 of + `connectToRelayAsync`).** If the failure happens before + `createRelayMemberConnectionAsync` runs, `activeConn` is `Nothing` + and the next trigger retries automatically. If the call succeeds + but a later async step (LDATA, CONF, CON) stalls, `activeConn` + exists in a non-`ConnReady` state; the chat layer does not retry + by design (Option A simple skip). The agent layer's internal + retries on subscription resume drive recovery for transient + network failures. Permanent stalls are recovered via explicit + retry paths (`retryRelayConnectionAsync`, channel re-join). + +6. **Link-data fetch failure after pre-created member row.** Two + sub-cases. (a) `createRelayMemberConnectionAsync` not yet run: + `activeConn = Nothing`, next trigger retries (`XGrpRelayNew` + arrival from another relay or channel re-open via + `syncSubscriberRelays`). (b) Connection record exists but LDATA + failed: `activeConn = Just _`, chat layer skips by Option A; + agent layer retries internally on subscription resume. + +7. **Active-status filter on lookup breaks other call sites.** The filter is added in + place on `getCreateRelayForMember`'s inner lookup. Its lone existing caller is the + subscriber-join path (`APIConnectPreparedGroup` → `connectToRelay`, Commands.hs:2141 + / 3597-3613); rows there are created with `GSMemAccepted`, which is `memberCurrent`, + so the filtered lookup still finds them on retry. Observable behaviour unchanged for + the existing caller. Audit done in §3.3; reviewer to confirm. + +8. **Multiple owners (future).** `LINK` callback only fires for the local owner's own + `setConnShortLink` calls (per existing TODO at Subscriber.hs:1327-1329). A second + owner adding a relay won't trigger the event from this owner — the second owner + would emit it themselves. Out of scope for current single-owner channels. + +--- + +## 10. Backward compatibility + +- **No schema migration.** The plan adds zero columns and zero tables. The new lookup + uses an existing column (`group_members.relay_link`) with an existing index path. +- **No protocol-version bump in the chat versioning.** The new tag is parsed as + `XUnknown` by clients that do not recognise `"x.grp.relay.new"` (Protocol.hs:1129 + default branch in `strDecode`). `XUnknown` is silently ignored when reached by + `processEvent` (Subscriber.hs:1032 catch-all `messageError`); since this is on the + receive side of an old client, the message is logged as unsupported and the channel + state is unaffected. +- **No serialization-compat shim.** The single-field JSON form means old clients fall + through to `XUnknown_` cleanly without any optional-field hand-rolling. + +--- + +## 11. Out of scope + +- **Owner authorization-chain pushes.** Adding/removing owners is governed by the + multi-owner roadmap (channels-overview.md §"Governance evolution"). `XGrpRelayNew` + does not carry owner-chain payload. +- **Profile pushes.** Subscriber profile changes are out of scope; relay profile + arrives via the relay's own link data in the existing `CFGetRelayDataJoin` LDATA + flow. +- **Content batching beyond the LINK callback.** The `NonEmpty` shape is defensive + for agent-side coalescing, not a general batching mechanism. +- **Retry-on-failure semantics for the new async path.** Existing + `retryRelayConnectionAsync` (Commands.hs:2168-2174) covers the subscriber-join + retry of failed initial connects (`APIConnectPreparedGroup` tail recovery). For + event-driven re-attempts, on-open `syncSubscriberRelays` is the recovery mechanism; + per-link retry timers are not added. +- **Deletion of `connectToRelay` (sync).** Default kept. The lone caller is + `APIConnectPreparedGroup` (subscriber's initial channel-join flow, + Commands.hs:2108-2161), not owner channel creation. Deletion is a reviewer-confirmed + follow-up if subscriber-join is converged onto async — see §5.1 for why the sync + flow is intentional there. + +--- + +## 12. Concerns with overview + +- **Overview §"Files touched" lists "remove sync `connectToRelay`".** After tracing + Commands.hs:2141 (`mapConcurrently (connectToRelay user gInfo') relays` inside + `APIConnectPreparedGroup`), the sync helper still has a real caller — the + **subscriber's initial channel-join** (not owner setup). Deleting it now would + either break that path or force `APIConnectPreparedGroup` onto the async helper, + which is a separate concern (different UX expectations: spinner-blocking immediate + feedback vs. fire-and-forget). Plan defers this to a follow-up. Reviewer to confirm. + +- **Overview §"Subscriber — receive" mentions `withAuthor XGrpRelayNew_`.** That + function name (the tag) does not currently exist in `Protocol.hs`; it lands in §2.2 + of this plan. Naming preserved verbatim. + +- **Overview §"Test surface" "Owner with old relay → relay drops the event".** The + current `processEvent` default branch is at Subscriber.hs:1032. Verified: the + default `_ -> Nothing <$ messageError ("unsupported message: " <> tshow event)` + drops the event after logging. This matches the overview's expectation. diff --git a/plans/2026-05-08-relay-announce.md b/plans/2026-05-08-relay-announce.md new file mode 100644 index 0000000000..37ffbd88ba --- /dev/null +++ b/plans/2026-05-08-relay-announce.md @@ -0,0 +1,119 @@ +# Plan: owner-pushed relay announcement (`XGrpRelayNew`) + +## Goal +Subscribers learn of newly added relays immediately via an owner-pushed event, +rather than only on next channel open via `syncSubscriberRelays`. + +## Wire-format +- New event: `XGrpRelayNew :: ShortLinkContact -> ChatMsgEvent 'Json`, tag `x.grp.relay.new`. +- Add to `isForwardedGroupMsg` in `Protocol.hs`. +- Add to required-signed-by-owner table in `docs/protocol/channels-protocol.md`. + Reuses existing `CBGroup`-prefixed signing infrastructure. + +## Owner — send site +- In LINK callback at `Subscriber.hs:1305-1322`, after the fold over relays + that drives `RSAccepted → RSActive` transitions. +- Collect `relayLink` for every relay that transitioned to Active in this callback. +- If non-empty, build `events = XGrpRelayNew rl1 :| [XGrpRelayNew rl2, ...]` + and call `sendGroupMessages user gInfo Nothing False otherRelays events`. +- Recipients: channel's currently-connected relays minus the newly-active ones + (the announced relays don't need self-announcement). +- Batched shape is defensive, not load-bearing. The receive-loop group lock + serializes `XGrpRelayAcpt` handling and the subsequent + `setGroupLinkDataAsync` → LINK chain, so each LINK callback typically + transitions at most one relay. Coding the send as a `NonEmpty` of + `XGrpRelayNew` events keeps the path correct if the agent ever consolidates + link-data writes. + +## Relay — forward only +- `processEvent` (Subscriber.hs:980-1032) gets a new case: + `XGrpRelayNew _ -> pure $ Just (DeliveryTaskContext (DJSGroup ...) False)`. +- No local handler — relay does NOT create a member record for the announced + relay (departure from `XGrpMemNew` semantics; relays don't connect to other + relays of the same channel). +- Forwarding is verbatim through binary-batch format, signature preserved. +- Old relay (no tag): `_ -> messageError "unsupported"` path drops the message. + Fallback: subscriber's on-open `syncSubscriberRelays` still works. + +## Subscriber — receive +- Add case in `processForwardedMsg` (Subscriber.hs:3357-3378): + `XGrpRelayNew rl -> withAuthor XGrpRelayNew_ $ \author -> connectToRelayAsync user gInfo rl`. +- Author resolution + signature verification via existing `withAuthor` / + `withVerifiedMsg` machinery — same boundary as `XGrpInfo` etc. today. +- `FwdChannel` (channel-as-sender) is NOT valid for this event — it is + administrative and must be attributed to a signing owner. + +## Subscriber — `connectToRelayAsync` helper +Place in `Internal.hs`. Both event handler and `syncSubscriberRelays` call it. +Body: + + 1. Look up active (`memberCurrent`) relay-member row by `relay_link`. + - If found AND has active connection → skip (already in flight or done). + - If found but no active connection → use it; proceed. + - If not found → create new relay-member row (with `relay_link`, role + `GRRelay`, status `GSMemAccepted`, no member-id/key/profile yet). + 2. `getAgentConnShortLinkAsync user CFGetRelayDataJoin Nothing relayLink` + → returns `(cmdId, agentConnId)`. + 3. `createRelayMemberConnectionAsync` binds those to the relay-member. + 4. Return. Continuation is the existing `CFGetRelayDataJoin` LDATA callback + at Subscriber.hs:1131-1160 (updates relay-member with member-id/key/profile, + calls `joinAgentConnectionAsync` → eventual `CON` flips status to + `GSMemConnected`). + +A `GSMemLeft` historical row for the same `relay_link` is left in place +(displays "removed by operator"). Lookup must filter to `memberCurrent`. + +## Idempotence and races +- Receive loop wraps each agent message in `withEntityLock` keyed by the + connection's lock entity (Subscriber.hs:115-117). Relay-member connections + resolve to `CLGroup groupId` (Store/Connections.hs:51-65). +- `APIGetUpdatedGroupLinkData` already uses `withGroupLock "syncSubscriberRelays" groupId`. +- Same key on both paths → event handler and open-channel command cannot + interleave for a given group_id. No additional lock needed. +- Inside the lock, "active row + active conn" check is sufficient. No + `justCreated` flag, no per-link mutex. + +## `syncSubscriberRelays` migration +- Move from `Commands.hs` to `Internal.hs`. +- "Add" half: replace synchronous `connectToRelay` with `connectToRelayAsync`. +- "Remove" half (Commands.hs:3623-3633): unchanged. +- `connectToRelay` (sync) deletable once event-driven path is wired and + no caller remains. + +## Old client compatibility +- Old subscriber: parses `XGrpRelayNew` as `XUnknown`, ignores. On-open + `syncSubscriberRelays` is the fallback path. +- Old relay: drops the message in `processEvent`'s default branch. Subscribers + on those relays fall back to on-open sync. Acceptable graceful degradation. + +## Test surface +- Owner adds relay → existing subscribers (online) receive `XGrpRelayNew` and + connect without channel open. +- Channel with two existing relays: owner adds a third relay; both existing + relays forward `XGrpRelayNew` for the new relay to subscribers in parallel + → shared-msg-id dedup leaves only one copy reaching the helper; subscriber + connects to the announced relay exactly once. +- `XGrpRelayNew` arrives while subscriber is mid-`APIGetUpdatedGroupLinkData` + for the same relay → group lock serializes; no double connection. +- Subscriber re-add scenario: previous `GSMemLeft` row for same `relay_link` + → new active row created, old row preserved for history. +- Old subscriber receives forwarded `XGrpRelayNew` → ignored, channel-open + sync still recovers. +- Owner with old relay → relay drops the event; subscribers learn on open. +- Bad signature on `XGrpRelayNew` → rejected with bad-signature event. + +## Files touched (anticipated) +- `src/Simplex/Chat/Protocol.hs` — event constructor, tag, JSON encode/parse, + `isForwardedGroupMsg`. +- `src/Simplex/Chat/Library/Internal.hs` — `connectToRelayAsync` helper, + `syncSubscriberRelays` moved here. +- `src/Simplex/Chat/Library/Subscriber.hs` — owner send (LINK callback), + relay forward-only `processEvent` case, subscriber forwarded + `processForwardedMsg` case. +- `src/Simplex/Chat/Library/Commands.hs` — remove sync `connectToRelay`, + `APIGetUpdatedGroupLinkData` calls async helper. +- `src/Simplex/Chat/Store/Groups.hs` — adjust relay-member lookup to filter + on `memberCurrent`. +- `docs/protocol/channels-protocol.md` — signing-required table row, + relay-addition subsection. +- `tests/ChatTests/...` — tests per "Test surface" above. diff --git a/plans/2026-05-09-desktop-tray-implementation.md b/plans/2026-05-09-desktop-tray-implementation.md new file mode 100644 index 0000000000..a3f047cd07 --- /dev/null +++ b/plans/2026-05-09-desktop-tray-implementation.md @@ -0,0 +1,422 @@ +# Desktop tray icon — implementation plan + +Companion to the design at `plans/2026-05-09-desktop-tray.md`. Read that first. + +## What + +Seven small commits that build the feature incrementally. After each commit the build is green and the app still runs; only the last commit makes the feature visible to the user end-to-end. + +## Why + +We split the work this way so each commit is reviewable on its own and revertable without unwinding others. The order keeps the build green throughout (no commit introduces a reference to something the next commit will define). + +## How + +### Pre-flight + +- Pull the branch `sh/tray` (current branch). It is at `stable`. +- Confirm dev environment can build desktop: `cd apps/multiplatform && ./gradlew :common:desktopMainClasses` — should succeed before any change. +- Read `plans/2026-05-09-desktop-tray.md` end to end. The implementation steps below assume that design is settled. + +--- + +### Task 1 — `CloseBehavior` enum + preference + +**Files** +- `apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt` + +**What to add.** The enum lives next to other small enums in this file (search for `enum class LAMode` for placement convention). The preference goes in `class AppPreferences` next to `notificationsMode`. + +Match the existing pattern (use `values().firstOrNull { it.name == this }`, not `entries`, to stay consistent with `LAMode` and others in this file): + +```kotlin +enum class CloseBehavior { + Ask, Quit, MinimizeToTray; + companion object { val default = Ask } +} + +// In AppPreferences: +val closeBehavior: SharedPreference = + mkSafeEnumPreference(SHARED_PREFS_DESKTOP_CLOSE_BEHAVIOR, CloseBehavior.default) +``` + +Add the constant at the bottom of `AppPreferences` next to other `SHARED_PREFS_*` constants: + +```kotlin +private const val SHARED_PREFS_DESKTOP_CLOSE_BEHAVIOR = "DesktopCloseBehavior" +``` + +**Verify.** Build: `./gradlew :common:desktopMainClasses` — succeeds. No behavior change yet. + +**Commit.** `desktop: add CloseBehavior preference` + +--- + +### Task 2 — Window-visibility state + branching close handler (no dialog, no tray yet) + +**(Note: a `Task 2 — Add ComposeNativeTray dependency` is removed. We now use Compose Multiplatform's built-in `androidx.compose.ui.window.Tray`, already on the classpath via the `org.jetbrains.compose` plugin. No new dep.)** + +**Files** +- `apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopApp.kt` + +**What to change.** + +1. Add `windowVisible` to `SimplexWindowState` (the class at line 227): + +```kotlin +class SimplexWindowState { + // ...existing fields... + val windowVisible = mutableStateOf(true) +} +``` + +2. In `AppWindow`, pass it to `Window`: + +```kotlin +Window( + state = windowState, + visible = simplexWindowState.windowVisible.value, + icon = painterResource(MR.images.ic_simplex), + onCloseRequest = { handleCloseRequest(closedByError) }, + // ...rest unchanged... +) +``` + +3. Add the handler at file scope (or near `showApp`). Temporarily make `Ask` fall through to `Quit` — the dialog comes in Task 3: + +```kotlin +private fun ApplicationScope.handleCloseRequest(closedByError: MutableState) { + if (closedByError.value) { closedByError.value = false; exitApplication(); return } + when (appPrefs.closeBehavior.get()) { + CloseBehavior.Quit, CloseBehavior.Ask -> { + closedByError.value = false + exitApplication() + } + CloseBehavior.MinimizeToTray -> { + simplexWindowState.windowVisible.value = false + } + } +} +``` + +The `MinimizeToTray` branch will get a tray-availability guard in Task 5 (defensive: a user could have set the pref on a different machine where tray works). + +(Imports: `chat.simplex.common.model.CloseBehavior`, `chat.simplex.common.model.ChatController.appPrefs`.) + +**Verify.** Build + run desktop: + +``` +./gradlew :desktop:run +``` + +Click X — app exits exactly as today. No dialog, no tray. (Internal preference is `Ask`, branch falls through to Quit.) + +**Commit.** `desktop: branch close handler on CloseBehavior preference` + +--- + +### Task 3 — First-close dialog + +**Files** +- `apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopTray.kt` *(new)* +- `apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopApp.kt` +- `apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml` + +**Strings.** Add to `strings.xml`: + +```xml +Minimize to tray? +If you choose Close, messages won\'t be received.\nYou can change it later in Appearance settings. +Close the app +Minimize to tray +``` + +**`DesktopTray.kt` — dialog only.** A `mutableStateOf?>` global, and a Composable that, when set, renders a non-dismissible `Dialog` with the two buttons. Skeleton: + +```kotlin +package chat.simplex.common + +import androidx.compose.foundation.layout.* +import androidx.compose.material.* +import androidx.compose.runtime.* +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import androidx.compose.ui.window.* +import chat.simplex.res.MR +import dev.icerock.moko.resources.compose.stringResource + +private val pendingCloseChoice = mutableStateOf(null) + +private data class CloseChoice(val onClose: () -> Unit, val onMinimize: () -> Unit) + +fun requestCloseBehavior(onClose: () -> Unit, onMinimize: () -> Unit) { + pendingCloseChoice.value = CloseChoice(onClose, onMinimize) +} + +@Composable +fun CloseBehaviorDialog() { + val choice = pendingCloseChoice.value ?: return + Dialog( + onCloseRequest = { /* swallow — non-dismissible */ }, + state = rememberDialogState(width = 420.dp, height = 220.dp), + title = stringResource(MR.strings.close_behavior_dialog_title), + resizable = false, + ) { + Column(Modifier.padding(24.dp)) { + Text(stringResource(MR.strings.close_behavior_dialog_text)) + Spacer(Modifier.height(24.dp)) + Row(horizontalArrangement = Arrangement.spacedBy(12.dp)) { + Button( + onClick = { pendingCloseChoice.value = null; choice.onClose() }, + colors = ButtonDefaults.buttonColors(backgroundColor = MaterialTheme.colors.error), + ) { Text(stringResource(MR.strings.close_behavior_dialog_close)) } + // Hide the Minimize button when tray isn't supported (stock GNOME). + // The dialog still asks once so the user gets a definitive Quit answer + // and doesn't see the dialog again. trayIsAvailable is defined in Task 5; + // until then, the button is always shown. + Button( + onClick = { pendingCloseChoice.value = null; choice.onMinimize() }, + colors = ButtonDefaults.buttonColors(backgroundColor = MaterialTheme.colors.primary), + ) { Text(stringResource(MR.strings.close_behavior_dialog_minimize)) } + } + } + } +} +``` + +**Wire it up in `DesktopApp.kt`.** Inside `application(exitProcessOnExit = false) { … }`, render `CloseBehaviorDialog()` alongside `AppWindow`. Update `handleCloseRequest`'s `Ask` branch: + +```kotlin +CloseBehavior.Ask -> requestCloseBehavior( + onClose = { + appPrefs.closeBehavior.set(CloseBehavior.Quit) + closedByError.value = false + exitApplication() + }, + onMinimize = { + appPrefs.closeBehavior.set(CloseBehavior.MinimizeToTray) + simplexWindowState.windowVisible.value = false + } +) +``` + +**Verify.** Run, click X — dialog appears with the exact text and button colors. Click "Close the app" → exits. Reopen, click X — exits without dialog (preference is `Quit`). + +To reset the preference for re-testing, delete the SimpleX Chat desktop preferences file: +- Linux: `~/.config/simplex/SimpleXChatDesktop.properties` +- macOS: `~/Library/Preferences/SimpleXChatDesktop.properties` +- Windows: `%AppData%\SimpleX\SimpleXChatDesktop.properties` + +Click "Minimize to tray" → window hides; the app process keeps running but is invisible (no tray icon yet — that's Task 6). Kill the JVM with Ctrl-C in the terminal to recover. + +**Commit.** `desktop: first-close dialog for tray choice` + +--- + +### Task 4 — Tray icon resources + +**Files** +- `apps/multiplatform/common/src/commonMain/resources/MR/images/ic_simplex_tray_dot.svg` +- `apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml` + +**Icons.** Reuse the existing `MR.images.ic_simplex` for the no-unread case and add a single new asset for the unread case: + +- `ic_simplex_tray_dot` — copy of `ic_simplex.svg` with a small red filled circle added in the bottom-right (~6px radius in the 40×40 viewBox). + +Drop the SVG into `MR/images/`. Moko picks it up; refer to as `MR.images.ic_simplex_tray_dot`. Run a build to check generation: `./gradlew :common:generateMRcommonMain`. + +**Strings.** Tray menu items + tooltip strings: + +```xml +Show SimpleX +Quit SimpleX +SimpleX +SimpleX — %d unread +``` + +**Verify.** Build succeeds; the generated `MR.images.ic_simplex_tray_dot` and `MR.strings.tray_*` symbols compile when referenced from a temporary scratch file (delete after). + +**Commit.** `desktop: tray icon assets and menu strings` + +--- + +### Task 5 — Tray composable (no unread indicator yet) + +**Files** +- `apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopTray.kt` +- `apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopApp.kt` + +**Add to `DesktopTray.kt`.** Tray-availability probe, functions to show window and quit, the Tray composable itself. + +```kotlin +import androidx.compose.ui.window.ApplicationScope +import androidx.compose.ui.window.Tray +import androidx.compose.ui.window.MenuBar +import dev.icerock.moko.resources.compose.painterResource +import dev.icerock.moko.resources.compose.stringResource +import java.awt.SystemTray + +// Probed once at startup. Performs a real add/remove of a transparent TrayIcon +// because SystemTray.isSupported() can return true while add() throws (JDK-8322750). +val trayIsAvailable: Boolean by lazy { + if (!SystemTray.isSupported()) return@lazy false + try { + val tray = SystemTray.getSystemTray() + val probe = TrayIcon(BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)) + tray.add(probe); tray.remove(probe); true + } catch (e: AWTException) { false } catch (e: SecurityException) { false } +} + +fun showWindow() { + simplexWindowState.windowVisible.value = true + simplexWindowState.window?.toFront() + simplexWindowState.window?.requestFocus() +} + +@Composable +fun ApplicationScope.SimplexTray(closedByError: MutableState) { + if (!trayIsAvailable) return + if (appPrefs.closeBehavior.state.value != CloseBehavior.MinimizeToTray) return + Tray( + icon = painterResource(MR.images.ic_simplex_tray), + tooltip = stringResource(MR.strings.tray_tooltip), + onAction = ::showWindow, + menu = { + Item(stringResource(MR.strings.tray_show), onClick = ::showWindow) + Separator() + Item(stringResource(MR.strings.tray_quit), onClick = { + closedByError.value = false + exitApplication() + }) + } + ) +} +``` + +(Note: this uses Compose Multiplatform's built-in `androidx.compose.ui.window.Tray`. The API is `icon: Painter`, `onAction` (not `primaryAction`), menu DSL uses `Separator()` (not `Divider()`).) + +**Update `DesktopApp.kt`'s close handler** to add the defensive tray-availability check from Task 2's TODO: + +```kotlin +CloseBehavior.MinimizeToTray -> { + if (trayIsAvailable) { + simplexWindowState.windowVisible.value = false + } else { + closedByError.value = false + exitApplication() + } +} +``` + +**Wire into `DesktopApp.kt`.** Inside `application(exitProcessOnExit = false) { … }`: + +```kotlin +SimplexTray(closedByError) +CloseBehaviorDialog() +AppWindow(closedByError) +``` + +The order doesn't affect rendering — the tray and dialog are top-level surfaces. + +**Verify.** Run; in the dialog pick "Minimize to tray". Window hides; tray icon appears. Left-click tray — window restores. Right-click tray — menu has "Show SimpleX" and "Quit SimpleX". Both work. Quit, restart — preference persists; clicking X hides directly without dialog. Tray icon appears at app startup (because the preference is now `MinimizeToTray`). + +**Commit.** `desktop: system tray icon with show/quit menu` + +--- + +### Task 6 — Unread indicator + tooltip count + +**Files** +- `apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopTray.kt` + +**Change `SimplexTray`.** Replace the static icon and tooltip with reactive ones: + +```kotlin +// UserInfo.unreadCount is incremented only when ntfsEnabled(item) — see SimpleXAPI.kt:2781-2783. +val unread by remember { + derivedStateOf { ChatModel.users.sumOf { it.unreadCount } } +} +val iconRes = if (unread > 0) MR.images.ic_simplex_tray_dot else MR.images.ic_simplex +val tooltip = + if (unread > 0) stringResource(MR.strings.tray_tooltip_unread, unread) + else stringResource(MR.strings.tray_tooltip) + +Tray( + icon = painterResource(iconRes), + tooltip = tooltip, + // onAction + menu unchanged +) +``` + +**Verify.** +1. With "Minimize to tray" enabled, hide the window. +2. Trigger a notification (have another account/contact send you a message; or open a direct chat with notifications enabled and post from another device). +3. Tray icon switches to the red-dot variant; tooltip shows "SimpleX — 1 unread" (or higher). +4. Click tray, view the message in the relevant chat. Icon reverts to the plain variant; tooltip becomes "SimpleX". + +**Commit.** `desktop: unread indicator on tray icon` + +--- + +### Task 7 — Appearance settings toggle + +**Files** +- `apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/views/usersettings/Appearance.desktop.kt` +- `apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml` + +**Strings.** + +```xml +Minimize to tray when closing window +Keep SimpleX running in the background to receive messages. +``` + +**UI row.** In `AppearanceLayout` (the Composable around line ~38), add a new section row using the existing `SectionItemView` / `SettingsActionItemWithContent` / similar patterns visible in this file. The entire row is gated on `trayIsAvailable` — if the OS has no tray host, the toggle is omitted. Read the surrounding rows for the exact convention; the snippet below is illustrative: + +```kotlin +if (trayIsAvailable) { + val pref = remember { appPrefs.closeBehavior.state } + val on = pref.value == CloseBehavior.MinimizeToTray + SectionItemView { + Row(verticalAlignment = Alignment.CenterVertically) { + Column(Modifier.weight(1f)) { + Text(stringResource(MR.strings.appearance_minimize_to_tray)) + Text( + stringResource(MR.strings.appearance_minimize_to_tray_desc), + style = MaterialTheme.typography.caption, + color = MaterialTheme.colors.onSurface.copy(alpha = 0.7f) + ) + } + Switch( + checked = on, + onCheckedChange = { checked -> + appPrefs.closeBehavior.set(if (checked) CloseBehavior.MinimizeToTray else CloseBehavior.Quit) + } + ) + } + } +} +``` + +Place the row in the existing `AppearanceLayout` Composable, after the theme/dark-mode rows and before the language selector — that grouping is for general window-and-display preferences and the new toggle fits there. Match the styling of nearby rows. If a clearer section emerges during implementation, add a new `SectionView` with a "Window" header instead. + +**Verify.** Open Appearance settings; toggle the row off — tray icon disappears; click X exits with no dialog. Toggle back on — tray icon reappears (Compose recomposes the gated `Tray` composable). Window-close behavior still depends on the toggle. + +**Commit.** `desktop: Appearance toggle for minimize-to-tray` + +--- + +### Final manual test pass + +Run the full test plan from the spec on each platform you can reach (Linux KDE, Windows 11, macOS): + +1. Fresh install (clear `~/.config/simplex/` or per-OS data dir). Click X → dialog with the right text and button colors. Esc / outside-tap do nothing. +2. Pick Close → exits. Reopen → click X → exits with no dialog. +3. Reset, pick Minimize to tray → window hides, tray icon shows. +4. Receive a message → red-dot variant + tooltip count. +5. Click tray → window restores and focuses (acceptable if focus is best-effort per spec). +6. Right-click tray → Show / Quit both work. +7. Appearance toggle off → tray vanishes, X exits without dialog. +8. Appearance toggle on → tray reappears. + +If anything fails, file follow-ups; the spec's "out of scope" list catches the expected omissions (autostart, number-on-icon, etc.). diff --git a/plans/2026-05-09-desktop-tray.md b/plans/2026-05-09-desktop-tray.md new file mode 100644 index 0000000000..ce67a83240 --- /dev/null +++ b/plans/2026-05-09-desktop-tray.md @@ -0,0 +1,197 @@ +# Desktop tray icon — minimize to tray on close + +## What + +Add a system tray icon (Windows notification area, Linux StatusNotifierItem, macOS menu bar) to the SimpleX desktop app, with a "minimize to tray" close behavior gated on first-time user choice. + +Three pieces: + +1. **First-close dialog** — the first time the user clicks the window's close (X) button, a modal asks whether to close the app or minimize it to the tray. The choice is remembered. +2. **Tray icon** — when the user has chosen "minimize to tray", the app installs a tray icon with a small right-click menu (Show / Quit) and an unread indicator. Clicking the icon restores the window. +3. **Appearance setting** — a "Minimize to tray when closing window" toggle in Appearance settings lets the user change their mind later. + +Scope: Linux + Windows + macOS. No autostart. No number-on-icon unread badge. No profile switcher in the tray menu. + +## Why + +Today, closing the SimpleX desktop window quits the process and the user stops receiving messages until they reopen the app. There is no way to keep the app running quietly in the background, which is the standard expectation for a chat client. + +We want this to be opt-in rather than a behavior change for existing users — hence the dialog on first close. Users who prefer the current quit-on-close behavior get exactly that with one click and never see the dialog again. Users who want background message delivery get it with one click and can manage it from settings. + +We are using Compose Multiplatform's built-in `androidx.compose.ui.window.Tray` rather than a third-party library. It works cleanly on Windows, macOS, and Linux desktops with a system tray host (KDE Plasma, XFCE, Cinnamon, MATE, GNOME with the AppIndicator extension). The trade-off is that on stock GNOME the JDK deliberately returns `false` from `SystemTray.isSupported()` (per JDK-8322750), so we **probe at startup and disable the feature entirely** when the OS reports no tray support — the dialog hides the "Minimize to tray" option and the Appearance toggle is hidden too. Users with a working tray get the feature; users without never see broken/invisible UI. + +All tray-specific code lives in `desktopMain` only. The Android target compiles none of it — there are no expect/actual surfaces calling into tray functionality from `commonMain`. + +Users upgrading from a prior version will see the dialog on their first window-close after the update — that is intentional. The dialog is the chosen mechanism for getting consent before keeping a process running in the background, and an existing user has no way to give that consent in advance. + +## How + +### Close behavior — preference and flow + +Add an enum preference: + +```kotlin +enum class CloseBehavior { Ask, Quit, MinimizeToTray } + +// in AppPreferences: +val closeBehavior: SharedPreference = + mkSafeEnumPreference(SHARED_PREFS_DESKTOP_CLOSE_BEHAVIOR, CloseBehavior.default) +``` + +`Ask` is the default for fresh installs and for users upgrading from a version that did not have this preference. + +Replace the inline close handler in `DesktopApp.kt` (currently `onCloseRequest = { closedByError.value = false; exitApplication() }`) with a function that branches on the preference: + +- **Crash recovery first.** If `closedByError.value == true`, exit immediately with no dialog, no minimize. The crash handler at `DesktopApp.kt:46-47` dispatches `WINDOW_CLOSING` and depends on the application loop ending so it can re-enter. Honouring `closedByError` is what keeps that path working. +- `Quit` → exit immediately, as today. +- `MinimizeToTray` → set `simplexWindowState.windowVisible.value = false` and return. +- `Ask` → show the first-close dialog. The dialog's button writes the preference and then performs the corresponding action. + +The same handler is invoked for the X button, Alt+F4 on Windows, and the macOS red traffic-light close — Compose routes all three through `onCloseRequest`. **macOS Cmd+Q is not routed through `onCloseRequest`**: it goes through the application menu's Quit and calls `exitApplication()` directly. We accept that as "always quit" — Cmd+Q is an explicit user intent to quit the application and should not be intercepted by the dialog. Programmatic `WindowEvent.WINDOW_CLOSING` (e.g. from the crash handler) reaches `onCloseRequest` and is handled by the `closedByError` branch above. + +The dialog is non-dismissible (no Esc, no outside-tap) so the user must choose. Wording verbatim: + +> **Minimize to tray?** +> +> If you choose Close, messages won't be received. +> You can change it later in Appearance settings. +> +> [ Close the app ] [ Minimize to tray ] + +The "Close the app" button uses `MaterialTheme.colors.error` (red); "Minimize to tray" uses `MaterialTheme.colors.primary` (blue). The dialog is implemented bespoke (not via the existing `AlertManager`), because `AlertManager` does not support the non-dismissible + custom-button-color combination needed here. + +The Compose application loop already runs with `exitProcessOnExit = false`, so hiding the window does not exit the process. No restructuring of `showApp()` is needed. + +### Tray icon + +No new dependency. We use `androidx.compose.ui.window.Tray` (built into Compose Multiplatform, already on the classpath). It wraps `java.awt.SystemTray` under the hood — works wherever AWT's tray works, returns silently when it doesn't. + +**Tray availability probe.** `java.awt.SystemTray.isSupported()` alone is not reliable — there is a JDK pattern where it returns `true` but `SystemTray.add()` then throws `AWTException` (and Compose-MP does not catch it). We expose a `desktopMain` value that runs a real add/remove of a transparent `TrayIcon` inside a `try/catch` and caches the result: + +```kotlin +val trayIsAvailable: Boolean by lazy { + if (!SystemTray.isSupported()) return@lazy false + try { + val tray = SystemTray.getSystemTray() + val probe = TrayIcon(BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)) + tray.add(probe) + tray.remove(probe) + true + } catch (e: AWTException) { false } + catch (e: SecurityException) { false } +} +``` + +The probe is force-evaluated at the top of `showApp()` (off the EDT) so the JDK-8322750 GNOME detection subprocess does not block composition. When `false`: the Appearance toggle is hidden, the first-close dialog is skipped (`Ask` migrates silently to `Quit`), and the close handler treats `MinimizeToTray` as `Quit` (in case the preference was carried over from a tray-capable machine). + +The tray composable lives next to `AppWindow` inside `application(exitProcessOnExit = false) { … }` in `showApp()`. It is gated by the preference AND by tray availability: + +```kotlin +if (trayIsAvailable && appPrefs.closeBehavior.state.value == CloseBehavior.MinimizeToTray) { + // UserInfo.unreadCount is the pre-aggregated, ntfs-filtered counter — see SimpleXAPI.kt:2781-2783. + val unread by remember { derivedStateOf { + ChatModel.users.sumOf { it.unreadCount } + } } + val iconRes = if (unread > 0) MR.images.ic_simplex_tray_dot else MR.images.ic_simplex + val tooltip = if (unread > 0) + stringResource(MR.strings.tray_tooltip_unread, unread) + else + stringResource(MR.strings.tray_tooltip) + Tray( + icon = painterResource(iconRes), + tooltip = tooltip, + onAction = ::showWindow, + menu = { + Item(stringResource(MR.strings.tray_show), onClick = ::showWindow) + Separator() + Item(stringResource(MR.strings.tray_quit), onClick = { exitApplication() }) + } + ) +} +``` + +Note: Compose's `Tray` takes `icon: Painter` (not `iconContent`), `onAction` (not `primaryAction`), and the menu DSL uses `Separator()` (not `Divider()`). These are the right names for the built-in API. + +`showWindow()` sets `windowVisible.value = true` and calls `window?.toFront()` + `window?.requestFocus()`. Quitting from the tray menu just calls `exitApplication()` — `closedByError` is already `false` in the non-crash path, so the outer loop in `showApp()` terminates cleanly. + +**Unread indicator.** Icon swap based on `hasUnread`: reuse `ic_simplex` when zero, `ic_simplex_tray_dot` (same icon with a red dot overlay in the bottom-right) otherwise. Compose passes the `Painter` into AWT via `Painter.toAwtImage(density, layoutDirection, size)` — a single bitmap per state. One new image resource is enough: +- `MR.images.ic_simplex_tray_dot` — base icon with the red-dot overlay. + +**Icon size.** Compose `Tray` rasterises the `Painter` once at a per-platform target size: Linux 22×22, Windows 16×16, macOS 22×22 (with retina 2×). It's a single bitmap, so we source the painter at a comfortable size (e.g. via a `painterResource(MR.images.ic_simplex)` from the 40×40 SVG already shipped) and let the conversion handle the scale. We accept the slight scaling cost on 16×16 Windows panels rather than ship multiple size variants. + +**Tooltip.** Plain "SimpleX" when unread is zero; "SimpleX — N unread" otherwise. + +**Window restore is best-effort.** Compose Multiplatform issue [#4231](https://github.com/JetBrains/compose-multiplatform/issues/4231) documents that `toFront()` does not always pull the restored window above other windows on Linux/Windows — the OS may flash the taskbar entry instead. Acceptable for v1; if it bites users we can add the `isAlwaysOnTop = true; toFront(); isAlwaysOnTop = false` workaround in a follow-up. + +**No collision with the existing notification path.** `NtfManager.desktop.kt:178-188` contains an `java.awt.SystemTray` hack inside a private helper that turns out to be unreachable — the live notification path is `displayNotificationViaLib` (TwoSlices). The hack will not fire and cannot conflict with our tray icon. Cleaning up that dead code is out of scope here. + +**Toggling at runtime.** The `Tray { … }` composable is gated on `closeBehavior.state.value == MinimizeToTray`; Compose's recomposition lifecycle handles install/uninstall when the user flips the setting. No `LaunchedEffect` is needed. + +**Android isolation.** All tray code (the `Tray` composable, the close-behavior dialog, `showWindow`, the `trayIsAvailable` probe) lives in `desktopMain` only. The Android target compiles none of it — there are no expect/actual surfaces from `commonMain` calling into tray functionality. The only shared piece is the `CloseBehavior` enum + `closeBehavior` preference in `SimpleXAPI.kt`, which is plain data and never references tray APIs. + +### Appearance settings row + +In `Appearance.desktop.kt`, add one row to the existing settings section — **only when `trayIsAvailable`**: + +> ☑ **Minimize to tray when closing window** +> *Keep SimpleX running in the background to receive messages.* + +The toggle maps to the preference: + +- `MinimizeToTray` → on. +- `Quit` or `Ask` → off. + +Flipping on writes `MinimizeToTray`. Flipping off writes `Quit`. Touching the toggle resolves the `Ask` state to a definitive value — so a fresh-install user who opens Appearance settings, flips the row off, and then closes the window will *not* see the dialog (their preference is now `Quit`). This matches the user's apparent intent (they made a choice in settings) and avoids the surprise of a dialog appearing for a setting they thought they had already configured. + +When `trayIsAvailable` is `false` (stock GNOME without AppIndicator extension), the entire row is omitted from Appearance settings, the first-close dialog is skipped (`Ask` migrates silently to `Quit`), and the close handler treats `MinimizeToTray` as `Quit` (in case the user previously enabled it on a different machine). + +The wording "Minimize to tray" is used uniformly across all platforms, including macOS where the more native term would be "menu bar". A consistent in-app term is more important here than per-platform purity. + +### Files changed + +| File | Change | +|---|---| +| `apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt` | Add `CloseBehavior` enum, `closeBehavior` preference, `SHARED_PREFS_DESKTOP_CLOSE_BEHAVIOR` constant. *(already in this branch as commit 1)* | +| `apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopApp.kt` | Replace inline `onCloseRequest`; add `windowVisible` to `SimplexWindowState`; wire `Window(visible = …)`; host the `Tray` composable conditionally on `trayIsAvailable && closeBehavior == MinimizeToTray`. | +| `apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopTray.kt` *(new)* | `trayIsAvailable` probe, `requestCloseBehavior` + `CloseBehaviorDialog`, `SimplexTray` composable, `showWindow` helper. | +| `apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/views/usersettings/Appearance.desktop.kt` | Add the toggle row (gated on `trayIsAvailable`). | +| `apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml` | Add 8 new strings (dialog title/body/buttons, settings row, tray menu). | +| `apps/multiplatform/common/src/commonMain/resources/MR/images/` | Add `ic_simplex_tray` + `ic_simplex_tray_dot`. | + +No `build.gradle.kts` change — Compose's `Tray` is already on the classpath via the existing `org.jetbrains.compose` plugin. + +### New strings + +```xml +Minimize to tray? +If you choose Close, messages won\'t be received.\nYou can change it later in Appearance settings. +Close the app +Minimize to tray +Minimize to tray when closing window +Keep SimpleX running in the background to receive messages. +Show SimpleX +Quit SimpleX +``` + +### Out of scope + +The following are deliberately not in this PR: + +- **Run on system startup / autostart entries.** Per-platform integration (Windows registry Run key, Linux `~/.config/autostart/*.desktop`, macOS LaunchAgents) is its own design. +- **Number-on-icon unread badges.** Cross-platform text rendering on tray icons is fragile across DPIs and macOS menu bar tinting. +- **Per-profile switcher / mute / mark-all-read** in the tray menu. Keep the menu to Show / Quit for now. +- **macOS template (auto-tinting) icon.** Compose `Tray` doesn't expose `NSImage.setTemplate:`; the tray icon will be a colored bitmap on macOS. Acceptable initial cost. +- **GNOME workaround documentation.** Users on stock GNOME won't see the option at all (probe returns false). We don't bundle or recommend the AppIndicator extension from the app itself; if we want to surface that guidance, it goes in the website/help docs, not in this PR. + +### Test plan + +Verified manually on at least one Linux (KDE Plasma), Windows 11, and macOS host: + +1. Fresh install. Click X on the window. Dialog appears with the exact text and button colors. Dialog cannot be dismissed by Esc or outside-click. +2. Click "Close the app". App exits. Reopen, click X — app exits with no dialog (preference is now `Quit`). +3. Reset preference (or fresh install). Click X, click "Minimize to tray". Window hides. Tray icon appears. +4. Send a message to yourself / receive one. Tray icon switches to the red-dot variant; tooltip updates with unread count. +5. Click tray icon (left-click). Window restores and gains focus. Unread is cleared on viewing the chat. +6. Right-click tray icon. Menu shows "Show SimpleX" and "Quit SimpleX". Both work. +7. Open Appearance settings, flip "Minimize to tray when closing window" off. Tray icon disappears. Click X — app exits with no dialog. +8. Flip the toggle back on. Tray icon appears immediately (the composable is gated on the preference, so installation/removal follows the toggle). diff --git a/plans/2026-05-09-fix-image-text-overlap.md b/plans/2026-05-09-fix-image-text-overlap.md new file mode 100644 index 0000000000..f759a454e0 --- /dev/null +++ b/plans/2026-05-09-fix-image-text-overlap.md @@ -0,0 +1,83 @@ +# Fix tall image preview overlapping caption text + +Branch: `nd/fix-image-text-overlap` · commit `0a3dcd249` · analogous to iOS PR [#6732](https://github.com/simplex-chat/simplex-chat/pull/6732). + +## 1. Problem statement + +For a tall image (height/width ≳ 2.33), the chat-bubble caption text was rendered on top of the bottom of the image instead of cleanly below it. The image looked like it had a semi-transparent text watermark across its lower section. Reproduced on Android and desktop with any sufficiently tall image; never reproduced on images with `height ≤ 2.33 × width`. + +iOS already had the analogous fix (PR #6732, commit `b24d003a8`); Android/desktop did not. + +## 2. Solution summary + +One line in `apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/CIImageView.kt`. The image preview Box's `Modifier.aspectRatio(width / height)` is wrapped with `.coerceAtLeast(1f / 2.33f)` so the aspect ratio cannot go below the floor at which the layout starts to break. + +```diff +- Modifier.width(w).aspectRatio(previewBitmap.width.toFloat() / previewBitmap.height.toFloat()) ++ Modifier.width(w).aspectRatio((previewBitmap.width.toFloat() / previewBitmap.height.toFloat()).coerceAtLeast(1f / 2.33f)) +``` + +Total diff: 1 file, +1 / −1. + +## 3. Root cause + +`PriorityLayout` (`FramedItemView.kt:480`, added in #6726) caps image-region height at `constraints.maxWidth × 2.33f` and passes that as `maxHeight` to its image child: + +```kotlin +val maxImageHeight = (constraints.maxWidth * 2.33f).toInt().coerceAtMost(constraints.maxHeight) +val imageConstraints = constraints.copy(maxHeight = maxImageHeight) +val imagePlaceable = … .measure(imageConstraints) +``` + +The image child is `CIImageView`'s outer Box, modified (line 185 pre-fix) by `Modifier.width(w).aspectRatio(previewBitmap.width / previewBitmap.height)`. Compose's `aspectRatio` modifier picks a layout size satisfying both the parent's constraints AND the requested ratio — by trying `tryMaxWidth`, `tryMaxHeight`, `tryMinWidth`, `tryMinHeight` in order and returning the first satisfying `IntSize`. + +When the natural ratio is below `1/2.33`, every candidate violates one bound: + +- `tryMaxWidth`: implied `height = W / ratio > W × 2.33 = parentMaxHeight` → fails the height cap. +- `tryMaxHeight`: implied `width = parentMaxHeight × ratio < W` → fails the fixed `.width(W)` lower bound. +- `tryMinWidth` / `tryMinHeight`: same failures. + +`findSize()` returns `IntSize.Zero`, and `aspectRatio` falls through to passing the parent's UNBOUNDED-height-shape constraints down (`wrappedConstraints = constraints` — see Compose Foundation's `AspectRatioNode`). The inner `Image` (`.width(W).contentScale=FillWidth`) then sizes itself by intrinsic aspect, with `height = W × imgH/imgW`, drawing past the layout box `clipToBounds` would have given it. The text below then renders on the same vertical strip as the painter's overflow — visible as overlap. + +`PriorityLayout`'s height cap (added in #6726) prevents the original crash but does not, on its own, prevent this visual overflow, because the cap propagates through `imageConstraints` to a modifier that silently drops it. + +## 4. The fix in detail + +`coerceAtLeast(1f / 2.33f)` raises the ratio's floor to exactly the value where Compose's `tryMaxWidth` succeeds: + +- At `ratio = 1/2.33`, implied `height = W / (1/2.33) = W × 2.33 ≤ parentMaxHeight` (since `W ≤ parentMaxWidth` and `parentMaxHeight = parentMaxWidth × 2.33`). +- `findSize` returns `(W, W × 2.33)`. `wrappedConstraints` becomes `Constraints.fixed(W, W × 2.33)` — both dimensions fixed. +- Inner `Image`'s `paint` modifier sees `hasFixedDimens=true` and returns the constraints unchanged. Image bounds = `(W, W × 2.33)`. +- `Image`'s built-in `clipToBounds` clips the painter's `FillWidth` overflow to the bounded layout. `PriorityLayout` reads `imagePlaceable.measuredHeight = W × 2.33` and places the caption text immediately below. + +For images at or above the floor (ratio ≥ 1/2.33), `coerceAtLeast` is a no-op — the ratio is unchanged, the layout is unchanged, no behavioural diff. The change only takes effect for images that triggered the bug. + +`coerceAtLeast` (not `maxOf`) matches the idiom already used at `FramedItemView.kt:480` (`.coerceAtMost(constraints.maxHeight)`) — both clamp at the bound where the layout starts to break. Reads as "ensure the ratio is at least 1/2.33". + +## 5. Why this specific shape + +- **Why preserve the existing expression untouched.** The pre-fix `previewBitmap.width.toFloat() / previewBitmap.height.toFloat()` is the aspect-ratio computation. Wrapping it `(...).coerceAtLeast(1f / 2.33f)` makes the diff purely additive — a reviewer reading the patch sees "the existing ratio is now floored", with zero risk that the inner expression silently changed. Diff is one line, character-minimal. + +- **Why no `MAX_IMAGE_HEIGHT_RATIO` constant.** Two use sites of `2.33f` after the fix (`PriorityLayout` line 480 and the new clamp). `good-code-v5.md`: *"Three similar lines are better than a premature abstraction."* If a third site appears (link preview, video preview, etc.) the constant earns its place. Until then, local duplication mirrors the convention already in `PriorityLayout`. + +- **Why no edit to `CIVideoView`.** The screenshot showed only the image-preview bug. iOS PR #6732 also touched `CIVideoView` and `CILinkView`, but on Android/desktop the video preview's outer Box has no `aspectRatio` modifier at all — it is sized by its inner `VideoPreviewImageView`'s `.width(width).FillWidth`, which is paint-clamped by `imageConstraints.maxHeight` and reports a correct layout size. If a tall-video overlap is actually reported in the wild, the fix is a separate commit; speculatively replicating the iOS scope would expand the diff and review surface beyond what the bug requires. + +- **Why no `fillMaxSize + ContentScale.Crop` rewrite of the inner `Image`.** A previous iteration of this fix did that to mirror iOS's `scaledToFill` change. It works, but is structurally redundant: once the outer Box's `aspectRatio` is bounded, the inner `Image`'s existing `paint` modifier (`hasFixedDimens=true → return constraints`) and `clipToBounds` already produce the same visual result. The rewrite was extra structure, not bug-fix; reverted. + +- **Why no `PriorityLayout` constant rename.** `2.33f` is already inline at line 480 and works as-is. Extracting it to `MAX_IMAGE_HEIGHT_RATIO` would be a rename bundled with a bugfix — `good-code-v5.md`: *"a rename in a diff signals a meaningful change to the reviewer — a gratuitous rename wastes reviewer attention and can mask real changes."* Out of scope. + +- **Why `coerceAtLeast(1f / 2.33f)` and not `coerceAtLeast(0.43f)`.** The form `1f / 2.33f` makes the relationship to `PriorityLayout`'s `2.33f` height multiplier visually explicit. `0.43f` would be opaque and would drift independently if either value changed. + +## 6. Verification + +Manual sanity (Android debug APK): + +- Send a tall screenshot (height ≫ width) with a caption → caption now sits below the cropped image preview, no overlap. +- Send an image where `height ≤ 2.33 × width` → preview unchanged from pre-fix (the clamp is a no-op for these). +- Tap the cropped preview → fullscreen viewer opens the full image at native aspect (the clamp only affects the inline preview, not the viewer). + +## 7. Risk and rollback + +- **Blast radius** is the single-Box modifier in `CIImageView` for non-`smallView` mode. `smallView` (used by `ChatPreviewView` thumbnails) takes the `else Modifier` branch and is untouched. +- The clamp is a no-op for images that did not trigger the bug, so regression risk on non-tall images is zero by construction. +- Rollback: `git revert 0a3dcd249` and force-push the branch (or just drop the commit before merge). diff --git a/plans/2026-05-11-channel-owner-unlimited-delete.md b/plans/2026-05-11-channel-owner-unlimited-delete.md new file mode 100644 index 0000000000..29461f3ebd --- /dev/null +++ b/plans/2026-05-11-channel-owner-unlimited-delete.md @@ -0,0 +1,59 @@ +# Channel editorial delete from history + +## Problem + +Channel owners cannot delete content older than 24 hours. The limit makes sense in p2p groups (no authority, each member holds independent copy). In channels, the owner is the authority - their content, their right to remove it. + +## Design + +Rather than bypassing the 24-hour time limit for broadcast deletes, we add a new delete mode: "delete from history." The relay removes the message from its store but does not forward the deletion to subscribers. Subscribers who already received the message keep it - the operation cleans the relay's history, not the subscriber's device. + +This is the right separation: within 24 hours, broadcast delete reaches subscribers and removes from relays. After 24 hours, history delete cleans relays only - no retroactive rewriting of subscriber devices. + +## Changes + +### Protocol.hs + +`XMsgDel` gains `onlyHistory :: Bool` field. Defaults to `False` (backward compatible - old clients don't send it, parser defaults missing field to `False`). Encoded only when `True` via `justTrue`. + +### CIContent.hs + +New `CIDMHistory` delete mode alongside `CIDMBroadcast`, `CIDMInternal`, `CIDMInternalMark`. + +### Commands.hs + +`APIDeleteChatItem` group path: +- `CIDMHistory` - validates `publicGroupEditor` (channel + role >= GRModerator), sends `XMsgDel` with `onlyHistory = True`, no time check. Rejected for non-channels and insufficient role. +- `CIDMInternal` - rejected for channel editorial roles (they should use `CIDMHistory` instead). +- `CIDMBroadcast` - unchanged (24-hour time check applies to everyone). + +### Subscriber.hs + +Relay `processEvent`: when `XMsgDel` has `onlyHistory = True`, processes the delete locally (marks item in relay's store) but returns `Nothing` for the delivery task - no forwarding to subscribers. + +Subscriber `processForwardedMsg`: ignores the `onlyHistory` flag (wildcard match) - if a message somehow arrives, process as normal delete. + +### Types.hs + +`publicGroupEditor :: GroupInfo -> GroupMember -> Bool` - shared predicate for channel editorial role check. + +### UI (iOS + Kotlin) + +Delete dialog for channel editorial roles (owner/admin/moderator): +- First button: "Delete from history" (iOS) / "From history" (Kotlin) - always available, sends `CIDMHistory` +- Second button: "For everyone" - only within 24-hour window, sends `CIDMBroadcast` +- No "Delete for me" option for editorial roles + +Batch selection follows the same logic - "From history" replaces "Delete for me" for editorial roles. + +API error handling: `apiDeleteChatItems` and `apiDeleteMemberChatItems` now show error alerts on failure (was silently swallowed on Android). + +## Backward compatibility + +- Old relays: don't recognize `onlyHistory`, process `XMsgDel` normally (forward to subscribers). Acceptable - the delete reaches subscribers, which is more than the owner intended but not harmful. +- Old subscribers: `onlyHistory` field is ignored (not in their parser, and the relay won't forward it anyway). +- Old owners: never send `onlyHistory = True`, behavior unchanged. + +## Test + +`testChannelMessageDeleteFromHistory` - owner sends message, deletes with `history` mode, verifies relay processes locally but subscribers don't receive deletion, verifies `internal` mode is rejected for channel editorial roles. diff --git a/plans/2026-05-11-fix-call-bind-port.md b/plans/2026-05-11-fix-call-bind-port.md new file mode 100644 index 0000000000..2c1ee016da --- /dev/null +++ b/plans/2026-05-11-fix-call-bind-port.md @@ -0,0 +1,168 @@ +# Desktop call server: pick a free port when `localhost:50395` is busy + +Branch: `nd/fix-call-bind-port` · code commit `587b79779` · PR [#6963](https://github.com/simplex-chat/simplex-chat/pull/6963). + +## 1. Problem statement + +On Desktop, a WebRTC call runs in the system browser, served by an embedded `NanoWSD` HTTP+WebSocket server. `startServer()` bound that server to a hard-coded port, `SERVER_PORT = 50395` (`apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/views/call/CallView.desktop.kt:23`). If port 50395 was already in use — another instance of the app, a leftover server thread, or any unrelated process — `NanoHTTPD.start()` propagated the bind failure and the call could not start: + +``` +java.net.BindException: Address already in use: bind + at java.base/sun.nio.ch.Net.bind0(Native Method) + at java.base/sun.nio.ch.Net.bind(Unknown Source) + at java.base/sun.nio.ch.Net.bind(Unknown Source) + at java.base/sun.nio.ch.NioSocketImpl.bind(Unknown Source) + at java.base/java.net.ServerSocket.bind(Unknown Source) + at java.base/java.net.ServerSocket.bind(Unknown Source) + at org.nanohttpd.protocols.http.ServerRunnable.run(ServerRunnable.java:63) + at java.base/java.lang.Thread.run(Unknown Source) +``` + +A call should not be a single point of contention on one fixed TCP port. When 50395 is taken, the call should bind a different port and proceed. + +Scope: Desktop only. Android renders the call in an in-process `WebView` via `WebViewAssetLoader` — no local server, no port — and is unaffected. + +## 2. Solution summary + +Three changes, all in `CallView.desktop.kt`, plus a one-line spec note. Total diff: 2 files, +24 / −15. + +1. **`startServer` retries on a free port.** It gains a `port: Int = SERVER_PORT` parameter (used only by the retry; the single existing call site is unchanged by the default). `server.start()` is wrapped: on `BindException`, log a warning, stop the half-initialised server, and recurse once with `port = 0` — which makes the OS assign any free port. The recursion terminates because `port == 0` rethrows (the kernel does not hand out a busy ephemeral port). +2. **`WebRTCController` opens the browser at the port actually bound.** Previously it opened `http://localhost:50395/simplex/call/` *before* calling `startServer`; now it starts the server first and uses `server.listeningPort` for the URL — which equals `50395` in the normal case, and equals the OS-assigned port after a fallback. +3. **Spec note** in `apps/multiplatform/spec/services/calls.md` describing the fallback. + +```diff ++import java.net.BindException + + val server = remember { +- try { +- uriHandler.openUri("http://${SERVER_HOST}:$SERVER_PORT/simplex/call/") +- } catch (e: Exception) { +- ... endCall() ... +- } +- startServer(onResponse) ++ startServer(onResponse).apply { ++ try { ++ uriHandler.openUri("http://${SERVER_HOST}:${listeningPort}/simplex/call/") ++ } catch (e: Exception) { ++ ... endCall() ... ++ } ++ } + } + +-fun startServer(onResponse: (WVAPIMessage) -> Unit): NanoWSD { +- val server = object: NanoWSD(SERVER_HOST, SERVER_PORT) { /* unchanged */ } +- server.start(60_000_000) ++fun startServer(onResponse: (WVAPIMessage) -> Unit, port: Int = SERVER_PORT): NanoWSD { ++ val server = object: NanoWSD(SERVER_HOST, port) { /* unchanged */ } ++ try { ++ server.start(60_000_000) ++ } catch (e: BindException) { ++ if (port == 0) throw e ++ Log.w(TAG, "Call server port $port is busy, using a random port: ${e.message}") ++ server.stop() ++ return startServer(onResponse, port = 0) ++ } + return server + } +``` + +The `NanoWSD` object body (request handling, resource serving) is untouched. + +## 3. Root cause / how NanoHTTPD binds + +`startServer()` builds an anonymous `NanoWSD(SERVER_HOST, SERVER_PORT)` and calls `server.start(60_000_000)`. Inside `NanoHTTPD.start(timeout)`: + +```java +this.myServerSocket = this.getServerSocketFactory().create(); +this.myServerSocket.setReuseAddress(true); +ServerRunnable serverRunnable = createServerRunnable(timeout); +this.myThread = new Thread(serverRunnable); +this.myThread.start(); +while (!serverRunnable.hasBinded() && serverRunnable.getBindException() == null) { Thread.sleep(10L); } +if (serverRunnable.getBindException() != null) throw serverRunnable.getBindException(); +``` + +`ServerRunnable.run()` does the actual `myServerSocket.bind(new InetSocketAddress(hostname, myPort))` on its own thread; if that throws (port in use → `java.net.BindException`, a subclass of `IOException`), it stores the exception, returns, and the accept loop is never entered. `start()` observes the stored exception and rethrows it — which is why the stack trace in the report shows `ServerRunnable.run` rather than `NanoHTTPD.start`: it is the same exception object, captured at the failed `bind`. + +`setReuseAddress(true)` already handles the benign case (a just-closed server in `TIME_WAIT`), so the only way `start()` fails this way is a genuine conflict: something else is listening on `50395`. Pre-fix that exception escaped `startServer` → escaped the `remember {}` initialiser in `WebRTCController` → the call view could not establish its control channel. + +A fixed port is also unnecessary on the wire. The browser page only ever connects back to *the origin it was served from*: `apps/multiplatform/common/src/commonMain/resources/assets/www/desktop/ui.js` opens `new WebSocket(`ws://${location.host}`)`, and `call.html` references its assets with root-relative paths (`/desktop/style.css`, `/call.js`, …). So the page follows whatever host:port the Kotlin side opened in the browser — there is no second place that hard-codes `50395`. + +## 4. The fix in detail + +### 4.1 Retry on `port = 0` + +`NanoHTTPD`/`NanoWSD` accept port `0`, the standard "let the OS pick a free ephemeral port" convention; after `start()`, `getListeningPort()` (Kotlin: `listeningPort`) returns the concrete port the kernel assigned. So the retry needs no port-scanning loop and no arbitrary range — one fallback attempt, guaranteed to find a free port if one exists at all. + +```kotlin +fun startServer(onResponse: (WVAPIMessage) -> Unit, port: Int = SERVER_PORT): NanoWSD { + val server = object: NanoWSD(SERVER_HOST, port) { /* unchanged */ } + try { + server.start(60_000_000) + } catch (e: BindException) { + if (port == 0) throw e + Log.w(TAG, "Call server port $port is busy, using a random port: ${e.message}") + server.stop() + return startServer(onResponse, port = 0) + } + return server +} +``` + +- `port: Int = SERVER_PORT` — the parameter exists for the recursive retry. `startServer` has exactly one caller in the tree (`WebRTCController`), and the default keeps that call site byte-identical. A default-valued parameter used for internal recursion is a routine Kotlin idiom (`fun f(x, acc = init)`). +- `catch (e: BindException)` — deliberately narrower than `IOException`. The reported failure mode is specifically "address already in use"; any *other* `start()` failure (e.g. an I/O error creating the socket) is not something a different port fixes, so it propagates exactly as before. Surgical: handle the bug, nothing else. +- `if (port == 0) throw e` — terminates the recursion. If even the OS-assigned port fails to bind, that is a pathological condition (no ephemeral ports at all); rethrow rather than loop, preserving the original "give up" behaviour on the second failure. +- `server.stop()` — `start()` assigns `myServerSocket` (an unbound `ServerSocket`) and `myThread` (which has already exited, having caught the bind error) *before* failing. `stop()` closes that orphaned socket and joins the dead thread. Pre-fix this leak was transient (the exception terminated the call attempt); now that the call *recovers* instead of failing, the half-initialised server must be released explicitly. `stop()` is the same call the existing `onDispose` already makes on the live server. + +### 4.2 Start the server before opening the browser + +The browser URL must carry the port the server actually bound, which is only known after `start()`. So the order in `WebRTCController`'s `remember {}` is inverted: `startServer` first, then `uriHandler.openUri`. + +```kotlin +val server = remember { + startServer(onResponse).apply { + try { + uriHandler.openUri("http://${SERVER_HOST}:${listeningPort}/simplex/call/") + } catch (e: Exception) { + Log.e(TAG, "Unable to open browser: ${e.stackTraceToString()}") + AlertManager.shared.showAlertMsg( + title = generalGetString(MR.strings.unable_to_open_browser_title), + text = generalGetString(MR.strings.unable_to_open_browser_desc) + ) + endCall() + } + } +} +``` + +- `.apply { … }` keeps the whole thing one memoized expression that yields the `NanoWSD` (as before), with no `val server = …; …; server` shadowing, and reads as "start the server, then (side effect) open the browser at its port". `listeningPort` resolves on the `apply` receiver. +- In the normal case `listeningPort == 50395`, so the opened URL is character-for-character what it was pre-fix — the browser keeps its per-origin permissions (camera/mic are granted to `localhost:50395`). Only a fallback changes the origin, and only for that call. +- Side benefit: pre-fix the browser was launched *before* the server's `start()` returned, so the page could (briefly) hit a not-yet-listening socket and rely on its own retry; now the server is provably listening before the browser is told about it. Strictly safer ordering. +- The error handling (alert + `endCall()`) is preserved verbatim; only its position moved. + +### 4.3 Spec note + +`apps/multiplatform/spec/services/calls.md` (the file the code links back to) gains one sentence on the NanoWSD bullet — "If that port is already in use it falls back to an OS-assigned free port (`port 0`); `WebRTCController` reads `server.listeningPort` for the browser URL" — and the WebRTCController bullet now reads "Starts the server, then opens `http://localhost:/simplex/call/` (normally `50395`)". + +## 5. Why this specific shape — alternatives considered + +- **Always bind `port = 0`, drop the fixed port.** Simplest possible code, no retry. Rejected: browser permissions (camera/mic, autoplay) are scoped per *origin* = `scheme://host:port`. A port that changes every call would re-prompt the user for camera/mic on every call. Keeping `50395` as the primary value preserves the granted permission; `0` is the *fallback*, used only on conflict. +- **Scan a fixed range (`50395, 50396, … 50404`).** More "predictable-ish" than an ephemeral port, but it can still be exhausted, needs a loop with an off-by-one boundary, and re-introduces the very problem (a finite set of fixed ports) in miniature. `port = 0` delegates the search to the kernel — one call, can't be exhausted while any port is free. Standard idiom; NanoHTTPD supports it directly. +- **Catch `IOException` instead of `BindException`.** Broader than the bug. A non-bind `start()` failure isn't fixed by retrying on another port; let it propagate. A narrow catch makes the diff describe exactly the failure it handles. +- **Extract the `NanoWSD` object into a local `fun newServer(port)` and `try { newServer(SERVER_PORT)… } catch { newServer(0)… }`.** Functionally equivalent, but it re-indents the ~25-line object body for no behavioural reason — a noisier diff. The default-parameter + tail-recursion form leaves the object body byte-identical and adds only the retry wrapper. +- **Move the browser-open into a `LaunchedEffect`.** Cleaner separation of "construct" vs "side effect", but it defers the launch past first composition (a behaviour change beyond the bug) and adds an effect to reason about. The pre-fix code already opened the browser inside `remember {}`; `.apply { }` keeps that timing while removing the only real wart (the open ran *before* the server existed). +- **Update every doc that mentions `localhost:50395`** (`product/flows/calling.md`, `product/glossary.md`, `product/rules.md`, `product/views/call.md`). Out of scope here: `50395` is still the primary port and those are higher-level narrative docs; only `spec/services/calls.md` (which the code references and which describes the exact mechanism) is updated. A follow-up can sweep the rest if desired. + +## 6. Verification + +- `./gradlew :common:compileKotlinDesktop` → `BUILD SUCCESSFUL` (only pre-existing deprecation warnings; nothing in the changed file). +- A full Linux x86_64 AppImage was built from this branch and launched (Compose software renderer in the test VM); the desktop app starts normally. +- Manual, normal path: starting a call opens the system browser at `http://localhost:50395/simplex/call/` exactly as before; the WebSocket connects and the call proceeds. +- Manual, fallback path: occupy the port first (e.g. `python3 -m http.server 50395`, or `nc -l 50395`) and then start a call → the log shows `Call server port 50395 is busy, using a random port: …`, the browser is opened at the OS-assigned port, the page's `ws://${location.host}` WebSocket connects to that same port, and the call proceeds. + +## 7. Risk and rollback + +- **Blast radius**: `startServer` and the `remember {}` initialiser in `WebRTCController`, Desktop only. Android (WebView, no server) is untouched; iOS is unrelated. +- The fallback branch executes only when `50395` is genuinely occupied — rare. The common path is unchanged except for the start-then-open ordering, which is strictly safer. +- Per-origin browser permissions are preserved on the common path (port unchanged); a fallback resets them for that one call — a clear improvement over the call failing outright. +- **Rollback**: `git revert 587b79779` (and drop the commit before merge if desired). No data, schema, or protocol surface is touched. diff --git a/plans/2026-05-11-link-tracking-whitelist.md b/plans/2026-05-11-link-tracking-whitelist.md new file mode 100644 index 0000000000..7bebe0ea03 --- /dev/null +++ b/plans/2026-05-11-link-tracking-whitelist.md @@ -0,0 +1,85 @@ +# "Remove link tracking" strips whitelisted query parameters (`?list=` in YouTube links, github `ref`) + +Design doc for the fix in PR #6965 (`nd/fix-list-in-link` → `master`). + +## Problem — what prompted this + +With **"remove link tracking"** enabled (Settings → Privacy & security), sending a message +with a YouTube link that has a `list` query parameter — `https://www.youtube.com/playlist?list=PL...` +or a video-in-playlist link `https://www.youtube.com/watch?v=...&list=PL...` — sent the URL +with `?list=...` removed, so the recipient got a plain (non-playlist) link instead of the +playlist. Fixing that `?list=` stripping is the immediate purpose of this change. + +iOS, Android and desktop are all affected — the URI sanitiser lives in the shared Haskell +core (`src/Simplex/Chat/Markdown.hs`). + +## Cause + +"Remove link tracking" on send uses *safe mode* of `sanitizeUri`: +`ComposeView.sanitizeMessage` → `parseSanitizeUri(_, safe = true)` → `chatParseUri 1` → +`sanitizeUri True`. + +`sanitizeUri` has three branches that pick which query parameters to keep; two of them +already consult `qsWhitelist` (the list of parameter names known *not* to be tracking — `q`, +`search`, `list`, `page`, youtube's `v`/`t`, github's `ref`, …): + +```haskell +let sanitizedQS + | safe = filter (not . isSafeBlacklisted . fst) originalQS -- ← whitelist NOT consulted + | isNamePath = case originalQS of + p@(n, _) : ps -> (if isWhitelisted n || not (isBlacklisted n) then (p :) else id) $ filter (isWhitelisted . fst) ps + [] -> [] + | otherwise = filter (isWhitelisted . fst) originalQS +... +isSafeBlacklisted p = any (`B.isPrefixOf` p) qsSafeBlacklist +qsSafeBlacklist = [ "ad", "af", ..., "li", ..., "ref", ... ] -- name *prefixes*; "li" → LinkedIn (li_fat_id, lipi, licu) +``` + +The safe-mode branch is the odd one out: it drops a parameter whenever its name *starts +with* a known tracking prefix, and never looks at `qsWhitelist`. So `list` was dropped +because `"li"` is a tracking prefix, and github's whitelisted `ref` was dropped because +`"ref"` is itself a tracking prefix — even though both are explicitly listed as non-tracking +and are kept by every other branch. + +## Fix + +Make the safe-mode branch apply the same "whitelisted *or* not blacklisted" rule the other +branches already use: + +```haskell +| safe = filter (\(n, _) -> isWhitelisted n || not (isSafeBlacklisted n)) originalQS +``` + +This *removes* a special case rather than adding one — `list` is no longer handled +differently from any other whitelisted parameter; `qsWhitelist` becomes authoritative in all +three branches. Effects relative to the previous behaviour: + +- `list` is kept everywhere (the reported `?list=` bug); +- github's `ref` is kept on `github.com` in safe mode too (it was already kept in eager + mode — it's in the whitelist for exactly that reason); +- nothing else changes: of all whitelist entries, only `list` (vs the `"li"` prefix) and + `ref` (vs the `"ref"` prefix) collide with a `qsSafeBlacklist` prefix today; +- every actual tracking parameter is still stripped — `qsWhitelist` does not contain + `li_fat_id`, `lipi`, `licu`, `utm*`, etc., and `ref` on any non-github host stays stripped. + +Regression tests added in `testSanitizeUri` (`tests/MarkdownTests.hs`): + +```haskell +it "should keep whitelisted parameters in safe mode even if they match a blacklist prefix" $ do + "https://example.com/playlist?list=abc" `sanitized` Nothing -- "list" is whitelisted, "li" is blacklisted + "https://example.com/playlist?list=abc&si=def" `sanitized` Just "https://example.com/playlist?list=abc" + "https://github.com/owner/repo?ref=main" `sanitized` Nothing -- "ref" is whitelisted for github.com +``` + +Verified: full library + test rebuild, then `cabal run simplex-chat-test -- --match /sanitizeUri/` +→ 4 examples, 0 failures (the new block plus the three pre-existing `sanitizeUri` cases). + +## Alternatives considered + +- **Special-case `list`** (`isSafeBlacklisted p = p /= "list" && …`). Smallest possible diff, + provably zero collateral, but it hard-codes one parameter name into a predicate and leaves + the structural inconsistency (safe mode ignoring the whitelist) in place — a fix by + exception rather than by rule. (This was the first version; replaced.) +- **Narrow the `"li"` blacklist entry to `"li_"`.** Fixes `list` but stops matching `lipi` + and `licu` (real LinkedIn email-link params), i.e. changes more than `list` while still + not addressing `ref` or the underlying inconsistency. diff --git a/scripts/flatpak/chat.simplex.simplex.metainfo.xml b/scripts/flatpak/chat.simplex.simplex.metainfo.xml index b8c329431c..a2bafe8a70 100644 --- a/scripts/flatpak/chat.simplex.simplex.metainfo.xml +++ b/scripts/flatpak/chat.simplex.simplex.metainfo.xml @@ -38,6 +38,33 @@ + + https://simplex.chat/blog/20260430-simplex-channels-v6-5-consortium-crowdfunding-freedom-of-speech.html + +

New in v6.5.1:

+
    +
  • additional preset chat relay.
  • +
  • fixed a rare bug when receiving files.
  • +
+

New in v6.5:

+

Public channels - speak freely!

+
    +
  • Reliability: many relays per channel.
  • +
  • Ownership: you can run your own relays.
  • +
  • Security: owners hold channel keys.
  • +
  • Privacy: for owners and subscribers.
  • +
+

Easier to invite your friends: we made connecting simpler for new users.

+

Safe web links:

+
    +
  • opt-in to send link previews.
  • +
  • use SOCKS proxy for previews (if enabled).
  • +
  • prevent hyperlink phishing.
  • +
  • remove link tracking.
  • +
+

Non-profit governance: to make SimpleX Network last.

+
+
https://simplex.chat/blog/20260430-simplex-channels-v6-5-consortium-crowdfunding-freedom-of-speech.html diff --git a/simplex-chat.cabal b/simplex-chat.cabal index 3da5cf1422..1e463dfe48 100644 --- a/simplex-chat.cabal +++ b/simplex-chat.cabal @@ -131,6 +131,7 @@ library Simplex.Chat.Store.Postgres.Migrations.M20260222_chat_relays Simplex.Chat.Store.Postgres.Migrations.M20260403_item_viewed Simplex.Chat.Store.Postgres.Migrations.M20260429_relay_request_retries + Simplex.Chat.Store.Postgres.Migrations.M20260507_relay_inactive_at else exposed-modules: Simplex.Chat.Archive @@ -284,6 +285,7 @@ library Simplex.Chat.Store.SQLite.Migrations.M20260222_chat_relays Simplex.Chat.Store.SQLite.Migrations.M20260403_item_viewed Simplex.Chat.Store.SQLite.Migrations.M20260429_relay_request_retries + Simplex.Chat.Store.SQLite.Migrations.M20260507_relay_inactive_at other-modules: Paths_simplex_chat hs-source-dirs: @@ -572,6 +574,7 @@ test-suite simplex-chat-test API.Docs.Commands API.Docs.Events API.Docs.Generate + API.Docs.Generate.Python API.Docs.Generate.TypeScript API.Docs.Responses API.Docs.Syntax diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index c5f17e5d69..c3658a1c94 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -117,6 +117,8 @@ defaultChatConfig = deliveryWorkerDelay = 0, deliveryBucketSize = 10000, channelSubscriberRole = GRObserver, + relayChecksInterval = 15 * 60, -- 15 minutes + relayInactiveTTL = nominalDay, relayRequestRetryInterval = RetryInterval {initialInterval = 5_000000, increaseAfter = 0, maxInterval = 600_000000}, relayRequestExpiry = (10, nominalDay), deviceNameForRemote = "", diff --git a/src/Simplex/Chat/Controller.hs b/src/Simplex/Chat/Controller.hs index cfb60c360a..84bebb3de6 100644 --- a/src/Simplex/Chat/Controller.hs +++ b/src/Simplex/Chat/Controller.hs @@ -159,6 +159,8 @@ data ChatConfig = ChatConfig deliveryWorkerDelay :: Int64, -- microseconds deliveryBucketSize :: Int, channelSubscriberRole :: GroupMemberRole, -- TODO [relays] starting role should be communicated in protocol from owner to relays + relayChecksInterval :: NominalDiffTime, + relayInactiveTTL :: NominalDiffTime, relayRequestRetryInterval :: RetryInterval, relayRequestExpiry :: (Int, NominalDiffTime), highlyAvailable :: Bool, @@ -521,6 +523,7 @@ data ChatCommand -- TODO [relays] starting role should be communicated in protocol from owner to relays (see channelSubscriberRole config) | APINewPublicGroup {userId :: UserId, incognito :: IncognitoEnabled, relayIds :: NonEmpty Int64, groupProfile :: GroupProfile} | APIGetGroupRelays {groupId :: GroupId} + | APIAddGroupRelays {groupId :: GroupId, relayIds :: NonEmpty Int64} | NewPublicGroup IncognitoEnabled (NonEmpty Int64) GroupProfile | AddMember GroupName ContactName GroupMemberRole | JoinGroup {groupName :: GroupName, enableNtfs :: MsgFilter} @@ -732,6 +735,8 @@ data ChatResponse | CRPublicGroupCreated {user :: User, groupInfo :: GroupInfo, groupLink :: GroupLink, groupRelays :: [GroupRelay]} | CRPublicGroupCreationFailed {user :: User, addRelayResults :: [AddRelayResult]} | CRGroupRelays {user :: User, groupInfo :: GroupInfo, groupRelays :: [GroupRelay]} + | CRGroupRelaysAdded {user :: User, groupInfo :: GroupInfo, groupLink :: GroupLink, groupRelays :: [GroupRelay]} + | CRGroupRelaysAddFailed {user :: User, addRelayResults :: [AddRelayResult]} | CRGroupMembers {user :: User, group :: Group} | CRMemberSupportChats {user :: User, groupInfo :: GroupInfo, members :: [GroupMember]} -- | CRGroupConversationsArchived {user :: User, groupInfo :: GroupInfo, archivedGroupConversations :: [GroupConversation]} @@ -993,7 +998,7 @@ data ChatPagination deriving (Show) data PaginationByTime - = PTLast Int + = PTLast {count :: Int} | PTAfter UTCTime Int | PTBefore UTCTime Int deriving (Show) diff --git a/src/Simplex/Chat/Library/Commands.hs b/src/Simplex/Chat/Library/Commands.hs index 31e6533ad3..3f66579969 100644 --- a/src/Simplex/Chat/Library/Commands.hs +++ b/src/Simplex/Chat/Library/Commands.hs @@ -748,11 +748,12 @@ processChatCommand vr nm = \case deletions <- case mode of CIDMInternal -> deleteDirectCIs user ct items CIDMInternalMark -> markDirectCIsDeleted user ct items =<< liftIO getCurrentTime + CIDMHistory -> throwChatError CEInvalidChatItemDelete CIDMBroadcast -> do assertDeletable items assertDirectAllowed user MDSnd ct XMsgDel_ let msgIds = itemsMsgIds items - events = map (\msgId -> XMsgDel msgId Nothing Nothing) msgIds + events = map (\msgId -> XMsgDel msgId Nothing Nothing False) msgIds forM_ (L.nonEmpty events) $ \events' -> sendDirectContactMessages user ct events' if featureAllowed SCFFullDelete forUser ct @@ -764,8 +765,9 @@ processChatCommand vr nm = \case -- TODO [knocking] check scope for all items? chatScopeInfo <- mapM (getChatScopeInfo vr user) scope deletions <- case mode of - CIDMInternal -> do - deleteGroupCIs user gInfo chatScopeInfo items Nothing =<< liftIO getCurrentTime + CIDMInternal + | publicGroupEditor gInfo (membership gInfo) -> throwChatError CEInvalidChatItemDelete + | otherwise -> deleteGroupCIs user gInfo chatScopeInfo items Nothing =<< liftIO getCurrentTime CIDMInternalMark -> do markGroupCIsDeleted user gInfo chatScopeInfo items Nothing =<< liftIO getCurrentTime CIDMBroadcast -> do @@ -773,9 +775,15 @@ processChatCommand vr nm = \case assertDeletable items assertUserGroupRole gInfo GRObserver -- can still delete messages sent earlier let msgIds = itemsMsgIds items - events = L.nonEmpty $ map (\msgId -> XMsgDel msgId Nothing $ toMsgScope gInfo <$> chatScopeInfo) msgIds + events = L.nonEmpty $ map (\msgId -> XMsgDel msgId Nothing (toMsgScope gInfo <$> chatScopeInfo) False) msgIds + mapM_ (sendGroupMessages user gInfo Nothing False recipients) events + delGroupChatItems user gInfo chatScopeInfo items False + CIDMHistory -> do + unless (publicGroupEditor gInfo (membership gInfo)) $ throwChatError CEInvalidChatItemDelete + recipients <- getGroupRecipients vr user gInfo chatScopeInfo groupKnockingVersion + let msgIds = itemsMsgIds items + events = L.nonEmpty $ map (\msgId -> XMsgDel msgId Nothing (toMsgScope gInfo <$> chatScopeInfo) True) msgIds mapM_ (sendGroupMessages user gInfo Nothing False recipients) events - -- TODO delGroupChatItems sends deletion events too. Are they needed? delGroupChatItems user gInfo chatScopeInfo items False pure $ CRChatItemsDeleted user deletions True False CTLocal -> do @@ -817,6 +825,7 @@ processChatCommand vr nm = \case deletions <- case mode of CIDMInternal -> deleteGroupCIs user gInfo Nothing items Nothing =<< liftIO getCurrentTime CIDMInternalMark -> markGroupCIsDeleted user gInfo Nothing items Nothing =<< liftIO getCurrentTime + CIDMHistory -> throwChatError CEInvalidChatItemDelete CIDMBroadcast -> do ms <- withFastStore' $ \db -> getGroupModerators db vr user gInfo let recipients = filter memberCurrent ms @@ -1778,11 +1787,14 @@ processChatCommand vr nm = \case gInfo@GroupInfo {groupProfile = p, groupSummary = GroupSummary {publicMemberCount = localCount}} <- withFastStore $ \db -> getGroupInfo db vr user groupId case p of GroupProfile {publicGroup = Just PublicGroupProfile {groupLink = sLnk}} | useRelays' gInfo -> do - (_, cData) <- getShortLinkConnReq nm user sLnk + (_, cData@(ContactLinkData _ UserContactData {relays = currentRelayLinks})) <- getShortLinkConnReq' nm user sLnk groupSLinkData_ <- liftIO $ decodeLinkUserData cData gInfo' <- case groupSLinkData_ of Just sLinkData -> fst <$> updateGroupFromLinkData user gInfo sLinkData _ -> pure gInfo + when (memberRole' (membership gInfo) /= GROwner && memberCurrent (membership gInfo)) $ + withGroupLock "syncSubscriberRelays" groupId $ + syncSubscriberRelays user gInfo' currentRelayLinks pure $ CRGroupInfo user gInfo' _ -> throwCmdError "group link data not available" APIGroupMemberInfo gId gMemberId -> withUser $ \user -> do @@ -2135,7 +2147,8 @@ processChatCommand vr nm = \case _ -> Nothing void $ createLinkOwnerMember db vr user gInfo' ctId_ (MemberId ownerId) ownerKey pure gInfo' - rs <- mapConcurrently (connectToRelay gInfo') relays + rs <- withGroupLock "connectPreparedGroup" groupId $ + mapConcurrently (connectToRelay user gInfo') relays let relayFailed = \case (_, _, Left _) -> True; _ -> False (failed, succeeded) = partition relayFailed rs if null succeeded @@ -2162,23 +2175,6 @@ processChatCommand vr nm = \case isTempErr = \case (_, _, Left ChatErrorAgent {agentError = e}) -> temporaryOrHostError e _ -> False - connectToRelay gInfo' relayLink = do - gVar <- asks random - -- Save relayLink to re-use relay member record on retry (check by relayLink) - relayMember <- withFastStore $ \db -> getCreateRelayForMember db vr gVar user gInfo' relayLink - r <- tryAllErrors $ do - (fd@FixedLinkData {rootKey = relayKey, linkEntityId}, cData) <- getShortLinkConnReq nm user relayLink - relayLinkData_ <- liftIO $ decodeLinkUserData cData - case (relayLinkData_, linkEntityId) of - (Just RelayShortLinkData {relayProfile = p}, Just entityId) -> - withFastStore $ \db -> updateRelayMemberData db user relayMember (MemberId entityId) (MemberKey relayKey) p - _ -> throwChatError $ CEException "relay link: no relay link data or entity id" - let cReq = linkConnReq fd - relayLinkToConnect = CCLink cReq (Just relayLink) - void $ connectViaContact user (Just $ PCEGroup gInfo' relayMember) incognito relayLinkToConnect Nothing Nothing - -- Re-read member to get updated activeConn and updated data (from updateRelayMemberData) - relayMember' <- withFastStore $ \db -> getGroupMember db vr user groupId (groupMemberId' relayMember) - pure (relayLink, relayMember', r) retryRelayConnectionAsync gInfo' relayLink relayMember@GroupMember {activeConn} = do forM_ activeConn $ \conn -> do deleteAgentConnectionAsync $ aConnId conn @@ -2547,6 +2543,37 @@ processChatCommand vr nm = \case relays <- liftIO $ getGroupRelays db gInfo pure (gInfo, relays) pure $ CRGroupRelays user gInfo relays + APIAddGroupRelays groupId relayIds -> withUser $ \user -> withGroupLock "addGroupRelays" groupId $ do + (gInfo, existingRelays) <- withFastStore $ \db -> do + gi <- getGroupInfo db vr user groupId + rs <- liftIO $ getGroupRelays db gi + pure (gi, rs) + assertUserGroupRole gInfo GROwner + unless (useRelays' gInfo) $ throwCmdError "group does not use relays" + let existingRelayIds = map (\GroupRelay {userChatRelay = UserChatRelay {chatRelayId = DBEntityId rId}} -> rId) existingRelays + when (any (`elem` existingRelayIds) relayIds) $ throwCmdError "some relays are already in the group" + gLink@GroupLink {connLinkContact = ccLink} <- withFastStore $ \db -> getGroupLink db user gInfo + sLnk <- case connShortLink' ccLink of + Just sl -> pure sl + Nothing -> throwChatError $ CEException "group link has no short link" + relays <- withFastStore $ \db -> mapM (getChatRelayById db user) (L.toList relayIds) + results <- addRelays user gInfo sLnk relays + case partitionEithers (map snd results) of + ([], _) -> do + relays' <- withFastStore $ \db -> liftIO $ getGroupRelays db gInfo + pure $ CRGroupRelaysAdded user gInfo gLink relays' + (errors@(e : _), _) -> do + if all isTempErr errors + then throwError e + else do + let toRelayResult (r, Left e') = AddRelayResult r (Just e') + toRelayResult (r, Right _) = AddRelayResult r Nothing + pure $ CRGroupRelaysAddFailed user (map toRelayResult results) + where + isTempErr :: ChatError -> Bool + isTempErr = \case + ChatErrorAgent {agentError = e} -> temporaryOrHostError e + _ -> False APIAddMember groupId contactId memRole -> withUser $ \user -> withGroupLock "addMember" groupId $ do -- TODO for large groups: no need to load all members to determine if contact is a member (group, contact) <- withFastStore $ \db -> (,) <$> getGroup db vr user groupId <*> getContact db vr user contactId @@ -3577,6 +3604,43 @@ processChatCommand vr nm = \case ct' <- withStore $ \db -> getContact db vr user contactId pure $ CRSentInvitationToContact user ct' incognitoProfile _ -> throwCmdError "contact already has connection" + connectToRelay :: User -> GroupInfo -> ShortLinkContact -> CM (ShortLinkContact, GroupMember, Either ChatError ()) + connectToRelay user gInfo relayLink = do + gVar <- asks random + -- Save relayLink to re-use relay member record on retry (check by relayLink) + relayMember <- withFastStore $ \db -> getCreateRelayForMember db vr gVar user gInfo relayLink + r <- tryAllErrors $ do + (fd@FixedLinkData {rootKey = relayKey, linkEntityId}, cData) <- getShortLinkConnReq nm user relayLink + relayLinkData_ <- liftIO $ decodeLinkUserData cData + case (relayLinkData_, linkEntityId) of + (Just RelayShortLinkData {relayProfile = p}, Just entityId) -> + withFastStore $ \db -> updateRelayMemberData db user relayMember (MemberId entityId) (MemberKey relayKey) p + _ -> throwChatError $ CEException "relay link: no relay link data or entity id" + let cReq = linkConnReq fd + relayLinkToConnect = CCLink cReq (Just relayLink) + void $ connectViaContact user (Just $ PCEGroup gInfo relayMember) (incognitoMembership gInfo) relayLinkToConnect Nothing Nothing + relayMember' <- withFastStore $ \db -> getGroupMember db vr user (groupId' gInfo) (groupMemberId' relayMember) + pure (relayLink, relayMember', r) + syncSubscriberRelays :: User -> GroupInfo -> [ShortLinkContact] -> CM () + syncSubscriberRelays user gInfo currentRelayLinks = void . tryAllErrors $ do + localRelayMembers <- withFastStore' $ \db -> getGroupRelayMembers db vr user gInfo + let activeRelayMembers = filter memberCurrent localRelayMembers + memberRelayLink GroupMember {relayLink = rl} = rl + localRelayLinks = mapMaybe memberRelayLink activeRelayMembers + newRelayLinks = filter (`notElem` localRelayLinks) currentRelayLinks + forM_ newRelayLinks $ \rlnk -> void . tryAllErrors $ + connectToRelayAsync user gInfo rlnk + forM_ localRelayMembers $ \m -> + case memberRelayLink m of + -- Remove relay if its link is no longer in the current link data. + -- Inactive relays (e.g. left) are only cleaned up when no active relays remain, + -- as that is the only case where the owner's relay removal can't be forwarded. + Just rlnk | rlnk `notElem` currentRelayLinks, + memberCurrent m || null activeRelayMembers -> + void . tryAllErrors $ do + deleteMemberConnection m + deleteOrUpdateMemberRecord user gInfo m + _ -> pure () prepareContact :: User -> ConnReqContact -> PQSupport -> CM (ConnId, VersionChat) prepareContact user cReq pqSup = do -- 0) toggle disabled - PQSupportOff @@ -3759,7 +3823,7 @@ processChatCommand vr nm = \case assertDeletable gInfo items assertUserGroupRole gInfo GRModerator let msgMemIds = itemsMsgMemIds gInfo items - events = L.nonEmpty $ map (\(msgId, memId) -> XMsgDel msgId memId $ toMsgScope gInfo <$> chatScopeInfo) msgMemIds + events = L.nonEmpty $ map (\(msgId, memId) -> XMsgDel msgId memId (toMsgScope gInfo <$> chatScopeInfo) False) msgMemIds mapM_ (sendGroupMessages_ user gInfo ms) events delGroupChatItems user gInfo chatScopeInfo items True where @@ -3889,38 +3953,36 @@ processChatCommand vr nm = \case mapConcurrently addRelay relays where addRelay :: UserChatRelay -> CM (UserChatRelay, Either ChatError GroupRelay) - addRelay relay@UserChatRelay {address} = do - r <- tryAllErrors $ do - (FixedLinkData {linkConnReq = cReq}, _cData) <- getShortLinkConnReq nm user address - lift (withAgent' $ \a -> connRequestPQSupport a PQSupportOff cReq) >>= \case - Nothing -> throwChatError CEInvalidConnReq - Just (agentV, _) -> do - let chatV = agentToChatVersion agentV - gVar <- asks random - subMode <- chatReadVar subscriptionMode - connId <- withAgent $ \a -> prepareConnectionToJoin a (aUserId user) True cReq PQSupportOff - (relayMember, conn, groupRelay) <- withFastStore $ \db -> do - relayMember <- createRelayForOwner db vr gVar user gInfo relay - groupRelay <- createGroupRelayRecord db gInfo relayMember relay - conn <- createRelayConnection db vr user (groupMemberId' relayMember) connId ConnPrepared chatV subMode - pure (relayMember, conn, groupRelay) - let GroupMember {memberRole = userRole, memberId = userMemberId} = membership - allowSimplexLinks = groupFeatureUserAllowed SGFSimplexLinks gInfo - membershipProfile = redactedMemberProfile allowSimplexLinks $ fromLocalProfile $ memberProfile membership - GroupMember {memberId = relayMemberId} = relayMember - relayInv = GroupRelayInvitation { - fromMember = MemberIdRole userMemberId userRole, - fromMemberProfile = membershipProfile, - relayMemberId, - groupLink = groupSLink - } - dm <- encodeConnInfo $ XGrpRelayInv relayInv - (sqSecured, _serviceId) <- withAgent $ \a -> joinConnection a nm (aUserId user) (aConnId conn) True cReq dm PQSupportOff subMode - let newConnStatus = if sqSecured then ConnSndReady else ConnJoined - withFastStore' $ \db -> do - void $ updateConnectionStatusFromTo db conn ConnPrepared newConnStatus - updateRelayStatusFromTo db groupRelay RSNew RSInvited - pure (relay, r) + addRelay relay@UserChatRelay {address} = fmap (relay,) . tryAllErrors $ do + (FixedLinkData {linkConnReq = cReq}, _cData) <- getShortLinkConnReq nm user address + lift (withAgent' $ \a -> connRequestPQSupport a PQSupportOff cReq) >>= \case + Nothing -> throwChatError CEInvalidConnReq + Just (agentV, _) -> do + let chatV = agentToChatVersion agentV + gVar <- asks random + subMode <- chatReadVar subscriptionMode + connId <- withAgent $ \a -> prepareConnectionToJoin a (aUserId user) True cReq PQSupportOff + (relayMember, conn, groupRelay) <- withFastStore $ \db -> do + relayMember <- createRelayForOwner db vr gVar user gInfo relay + groupRelay <- createGroupRelayRecord db gInfo relayMember relay + conn <- createRelayConnection db vr user (groupMemberId' relayMember) connId ConnPrepared chatV subMode + pure (relayMember, conn, groupRelay) + let GroupMember {memberRole = userRole, memberId = userMemberId} = membership + allowSimplexLinks = groupFeatureUserAllowed SGFSimplexLinks gInfo + membershipProfile = redactedMemberProfile allowSimplexLinks $ fromLocalProfile $ memberProfile membership + GroupMember {memberId = relayMemberId} = relayMember + relayInv = GroupRelayInvitation { + fromMember = MemberIdRole userMemberId userRole, + fromMemberProfile = membershipProfile, + relayMemberId, + groupLink = groupSLink + } + dm <- encodeConnInfo $ XGrpRelayInv relayInv + (sqSecured, _serviceId) <- withAgent $ \a -> joinConnection a nm (aUserId user) (aConnId conn) True cReq dm PQSupportOff subMode + let newConnStatus = if sqSecured then ConnSndReady else ConnJoined + withFastStore' $ \db -> do + void $ updateConnectionStatusFromTo db conn ConnPrepared newConnStatus + updateRelayStatusFromTo db groupRelay RSNew RSInvited privateGetUser :: UserId -> CM User privateGetUser userId = tryAllErrors (withStore (`getUser` userId)) >>= \case @@ -4727,12 +4789,41 @@ deleteInProgressGroup user gInfo = do withFastStore' $ \db -> deleteGroup db user gInfo runRelayGroupLinkChecks :: User -> CM () -runRelayGroupLinkChecks _user = do - -- TODO [relays] relay: periodically check presence of relay link in group links of served groups - -- TODO - retrieve group link data - -- TODO - if relay link is present, update relay status to RSActive - -- TODO - if relay link is absent and status was RSActive -> update to new "Removed" status? - pure () +runRelayGroupLinkChecks user = do + interval <- asks (relayChecksInterval . config) + liftIO $ threadDelay' $ diffToMicroseconds interval + forever $ do + flip catchAllErrors eToView $ do + lift waitChatStartedAndActivated + checkRelayServedGroups + checkRelayInactiveGroups + liftIO $ threadDelay' $ diffToMicroseconds interval + where + checkRelayServedGroups = do + vr <- chatVersionRange + relayGroups <- withStore' $ \db -> getRelayServedGroups db vr user + forM_ relayGroups $ \gInfo@GroupInfo {groupProfile = gp} -> flip catchAllErrors eToView $ do + case publicGroup gp of + Just PublicGroupProfile {groupLink = sLnk} -> do + (_, ContactLinkData _ UserContactData {relays = relayLinks}) <- + getShortLinkConnReq' NRMBackground user sLnk + gLink_ <- withStore' $ \db -> runExceptT $ getGroupLink db user gInfo + case gLink_ of + Right GroupLink {connLinkContact = CCLink _ (Just ourLink)} -> + if ourLink `elem` relayLinks + then do + -- TODO [relays] emit event to UI when relay own status promoted to RSActive + -- CEvtGroupRelayUpdated requires GroupRelay (owner-side), not available on relay side + void $ withStore' $ \db -> updateRelayOwnStatusFromTo db gInfo RSAccepted RSActive + else void $ withStore' $ \db -> updateRelayOwnStatusFromTo db gInfo RSActive RSInactive + _ -> pure () + _ -> pure () + checkRelayInactiveGroups = do + vr <- chatVersionRange + ttl <- asks (relayInactiveTTL . config) + inactiveGroups <- withStore' $ \db -> getRelayInactiveGroups db vr user ttl + forM_ inactiveGroups $ \gInfo -> flip catchAllErrors eToView $ + deleteGroupConnections user gInfo False expireChatItems :: User -> Int64 -> Bool -> CM () expireChatItems user@User {userId} globalTTL sync = do @@ -5026,6 +5117,7 @@ chatCommandP = ("/public group" <|> "/pg") *> (NewPublicGroup <$> incognitoP <* " relays=" <*> strP <* A.space <* char_ '#' <*> channelProfile), "/_public group " *> (APINewPublicGroup <$> A.decimal <*> incognitoOnOffP <*> _strP <* A.space <*> jsonP), "/_get relays #" *> (APIGetGroupRelays <$> A.decimal), + "/_add relays #" *> (APIAddGroupRelays <$> A.decimal <*> _strP), ("/add " <|> "/a ") *> char_ '#' *> (AddMember <$> displayNameP <* A.space <* char_ '@' <*> displayNameP <*> (memberRole <|> pure GRMember)), ("/join " <|> "/j ") *> char_ '#' *> (JoinGroup <$> displayNameP <*> (" mute" $> MFNone <|> pure MFAll)), "/accept member " *> char_ '#' *> (AcceptMember <$> displayNameP <* A.space <* char_ '@' <*> displayNameP <*> (memberRole <|> pure GRMember)), diff --git a/src/Simplex/Chat/Library/Internal.hs b/src/Simplex/Chat/Library/Internal.hs index d7de3a52ad..8324107a11 100644 --- a/src/Simplex/Chat/Library/Internal.hs +++ b/src/Simplex/Chat/Library/Internal.hs @@ -1321,6 +1321,18 @@ setGroupLinkDataAsync user gInfo gLink = do let (userLinkData, crClientData) = groupLinkData gInfo gLink groupRelays setAgentConnShortLinkAsync user conn userLinkData (Just crClientData) +connectToRelayAsync :: User -> GroupInfo -> ShortLinkContact -> CM () +connectToRelayAsync user gInfo relayLink = do + vr <- chatVersionRange + gVar <- asks random + relayMember@GroupMember {activeConn} <- withFastStore $ \db -> getCreateRelayForMember db vr gVar user gInfo relayLink + case activeConn of + Just _ -> pure () + Nothing -> do + subMode <- chatReadVar subscriptionMode + newConnIds <- getAgentConnShortLinkAsync user CFGetRelayDataJoin Nothing relayLink + withFastStore' $ \db -> createRelayMemberConnectionAsync db user gInfo relayMember relayLink newConnIds subMode + updatePublicGroupData :: User -> GroupInfo -> CM GroupInfo updatePublicGroupData user gInfo | useRelays' gInfo && memberRole' (membership gInfo) == GROwner = do @@ -1808,9 +1820,12 @@ deleteOrUpdateMemberRecord user gInfo m = deleteOrUpdateMemberRecordIO :: DB.Connection -> User -> GroupInfo -> GroupMember -> IO GroupInfo deleteOrUpdateMemberRecordIO db user@User {userId} gInfo m = do (gInfo', m') <- deleteSupportChatIfExists db user gInfo m - checkGroupMemberHasItems db user m' >>= \case - Just _ -> updateGroupMemberStatus db userId m' GSMemRemoved - Nothing -> deleteGroupMember db user m' + if isRelay m' + then deleteGroupMember db user m' + else + checkGroupMemberHasItems db user m' >>= \case + Just _ -> updateGroupMemberStatus db userId m' GSMemRemoved + Nothing -> deleteGroupMember db user m' pure gInfo' updateMemberRecordDeleted :: User -> GroupInfo -> GroupMember -> GroupMemberStatus -> CM GroupInfo @@ -1818,8 +1833,15 @@ updateMemberRecordDeleted user@User {userId} gInfo m newStatus = withStore' $ \db -> do (gInfo', m') <- deleteSupportChatIfExists db user gInfo m updateGroupMemberStatus db userId m' newStatus + deactivateRelay_ db m pure gInfo' +deactivateRelay_ :: DB.Connection -> GroupMember -> IO () +deactivateRelay_ db m = + when (isRelay m) $ do + relay_ <- runExceptT $ getGroupRelayByGMId db (groupMemberId' m) + forM_ relay_ $ \relay -> void $ updateRelayStatus db relay RSInactive + deleteSupportChatIfExists :: DB.Connection -> User -> GroupInfo -> GroupMember -> IO (GroupInfo, GroupMember) deleteSupportChatIfExists db user gInfo m = do gInfo' <- diff --git a/src/Simplex/Chat/Library/Subscriber.hs b/src/Simplex/Chat/Library/Subscriber.hs index 48dd63f6cf..6c960c3ce8 100644 --- a/src/Simplex/Chat/Library/Subscriber.hs +++ b/src/Simplex/Chat/Library/Subscriber.hs @@ -511,7 +511,7 @@ processAgentMessageConn vr user@User {userId} corrId agentConnId agentMessage = XMsgNew mc -> newContentMessage ct'' mc msg msgMeta XMsgFileDescr sharedMsgId fileDescr -> messageFileDescription ct'' sharedMsgId fileDescr XMsgUpdate sharedMsgId mContent _ ttl live _msgScope _ -> messageUpdate ct'' sharedMsgId mContent msg msgMeta ttl live - XMsgDel sharedMsgId _ _ -> messageDelete ct'' sharedMsgId msg msgMeta + XMsgDel sharedMsgId _ _ _ -> messageDelete ct'' sharedMsgId msg msgMeta XMsgReact sharedMsgId _ _ reaction add -> directMsgReaction ct'' sharedMsgId reaction add msg msgMeta -- TODO discontinue XFile XFile fInv -> processFileInvitation' ct'' fInv msg msgMeta @@ -931,7 +931,11 @@ processAgentMessageConn vr user@User {userId} corrId agentConnId agentMessage = newDeliveryTasks <- reverse <$> foldM (processAChatMsg gInfo' scopeInfo m' tags eInfo) [] aChatMsgs shouldDelConns <- if isUserGrpFwdRelay gInfo' && not (blockedByAdmin m) - then createDeliveryTasks gInfo' m' newDeliveryTasks + then + let tasks + | relayOwnStatus gInfo' == Just RSInactive = filter relayRemovedNewTask newDeliveryTasks + | otherwise = newDeliveryTasks + in createDeliveryTasks gInfo' m' tasks else pure False withRcpt <- checkSendRcpt $ rights aChatMsgs pure (withRcpt, shouldDelConns) @@ -995,7 +999,8 @@ processAgentMessageConn vr user@User {userId} corrId agentConnId agentMessage = checkSendAsGroup asGroup_ $ memberCanSend (Just m'') msgScope $ groupMessageUpdate gInfo' (Just m'') sharedMsgId mContent mentions msgScope msg brokerTs ttl live asGroup_ - XMsgDel sharedMsgId memberId_ scope_ -> groupMessageDelete gInfo' (Just m'') sharedMsgId memberId_ scope_ msg brokerTs + XMsgDel sharedMsgId memberId_ scope_ onlyHistory -> + groupMessageDelete gInfo' (Just m'') sharedMsgId memberId_ scope_ onlyHistory msg brokerTs XMsgReact sharedMsgId memberId scope_ reaction add -> groupMsgReaction gInfo' m'' sharedMsgId memberId scope_ reaction add msg brokerTs -- TODO discontinue XFile XFile fInv -> Nothing <$ processGroupFileInvitation' gInfo' m'' fInv msg brokerTs @@ -1004,6 +1009,7 @@ processAgentMessageConn vr user@User {userId} corrId agentConnId agentMessage = XInfo p -> fmap ctx <$> xInfoMember gInfo' m'' p msg brokerTs XGrpLinkMem p -> Nothing <$ xGrpLinkMem gInfo' m'' conn' p XGrpLinkAcpt acceptance role memberId -> Nothing <$ xGrpLinkAcpt gInfo' m'' acceptance role memberId msg brokerTs + XGrpRelayNew rl -> fmap ctx <$> xGrpRelayNew gInfo' m'' rl XGrpMemNew memInfo msgScope -> fmap ctx <$> xGrpMemNew gInfo' m'' memInfo msgScope msg brokerTs XGrpMemIntro memInfo memRestrictions_ -> Nothing <$ xGrpMemIntro gInfo' m'' memInfo memRestrictions_ XGrpMemInv memId introInv -> Nothing <$ xGrpMemInv gInfo' m'' memId introInv @@ -1039,6 +1045,8 @@ processAgentMessageConn vr user@User {userId} corrId agentConnId agentMessage = where aChatMsgHasReceipt (APMsg _ (ParsedMsg _ _ ChatMessage {chatMsgEvent})) = hasDeliveryReceipt (toCMEventTag chatMsgEvent) + relayRemovedNewTask :: NewMessageDeliveryTask -> Bool + relayRemovedNewTask NewMessageDeliveryTask {taskContext = DeliveryTaskContext {jobScope}} = isRelayRemoved jobScope createDeliveryTasks :: GroupInfo -> GroupMember -> [NewMessageDeliveryTask] -> CM ShouldDeleteGroupConns createDeliveryTasks gInfo'@GroupInfo {groupId = gId} m' newDeliveryTasks = do let relayRemovedTask_ = find (\NewMessageDeliveryTask {taskContext = DeliveryTaskContext {jobScope}} -> isRelayRemoved jobScope) newDeliveryTasks @@ -1297,25 +1305,48 @@ processAgentMessageConn vr user@User {userId} corrId agentConnId agentMessage = CFSetShortLink -> case (ucGroupId_, auData) of (Just groupId, UserContactLinkData UserContactData {relays = relayLinks}) -> do - (gInfo, gLink, relays, relaysChanged) <- withStore $ \db -> do + (gInfo, gLink, relays, relaysChanged, newlyActiveLinks) <- withStore $ \db -> do gInfo <- getGroupInfo db vr user groupId gLink <- getGroupLink db user gInfo relays <- liftIO $ getGroupRelays db gInfo - (relays', changed) <- liftIO $ foldrM (updateRelay db) ([], False) relays + (relays', changed, newlyActive) <- liftIO $ foldrM (updateRelay db) ([], False, []) relays liftIO $ setGroupInProgressDone db gInfo - pure (gInfo, gLink, relays', changed) + pure (gInfo, gLink, relays', changed, newlyActive) toView $ CEvtGroupLinkDataUpdated user gInfo gLink relays relaysChanged + let GroupSummary {publicMemberCount} = groupSummary gInfo + -- Owner is counted in publicMemberCount; > 1 means at least one subscriber. + -- TODO [relays] multi-owner: with N owners, threshold should be > N (or use a + -- dedicated subscriber count). + when (fromMaybe 0 publicMemberCount > 1) $ + forM_ (L.nonEmpty newlyActiveLinks) $ \newlyActive -> do + allRelayMembers <- withFastStore' $ \db -> getGroupRelayMembers db vr user gInfo + let recipients = + filter + (\GroupMember {memberStatus, relayLink} -> + memberStatus == GSMemConnected && relayLink `notElem` map Just newlyActiveLinks) + allRelayMembers + events = XGrpRelayNew <$> newlyActive + unless (null recipients) $ + void $ sendGroupMessages user gInfo Nothing False recipients events where - -- TODO [relays] owner: on relay deletion (link absent from relayLinks) - -- TODO move status RSActive to new "Removed" status / remove relay record - updateRelay :: DB.Connection -> GroupRelay -> ([GroupRelay], Bool) -> IO ([GroupRelay], Bool) - updateRelay db relay@GroupRelay {relayLink, relayStatus} (acc, changed) = + updateRelay :: DB.Connection -> GroupRelay -> ([GroupRelay], Bool, [ShortLinkContact]) -> IO ([GroupRelay], Bool, [ShortLinkContact]) + updateRelay db relay@GroupRelay {relayLink, relayStatus} (acc, changed, newlyActive) = case relayLink of Just rLink | rLink `elem` relayLinks && relayStatus == RSAccepted -> do relay' <- updateRelayStatus db relay RSActive - pure (relay' : acc, True) - _ -> pure (relay : acc, changed) + pure (relay' : acc, True, rLink : newlyActive) + | rLink `elem` relayLinks -> pure (relay : acc, changed, newlyActive) + | relayStatus == RSActive -> do + -- Relay link absent from link data — deactivate. + -- RSAccepted relays are not deactivated: their own link data update + -- may not have been processed yet (race with concurrent relay connections). + -- TODO [relays] multi-owner: Another owner removing a relay updates link data on + -- TODO the SMP server, but this owner won't receive a LINK callback for it + -- TODO (LINK only fires in response to own setConnShortLink calls). + relay' <- updateRelayStatus db relay RSInactive + pure (relay' : acc, True, newlyActive) + _ -> pure (relay : acc, changed, newlyActive) _ -> throwChatError $ CECommandError "LINK event expected for a group link only" _ -> throwChatError $ CECommandError "unexpected cmdFunction" MERR _ err -> do @@ -2142,26 +2173,30 @@ processAgentMessageConn vr user@User {userId} corrId agentConnId agentMessage = toView $ CEvtChatItemNotChanged user (AChatItem SCTGroup SMDRcv (GroupChat gInfo scopeInfo) ci) pure Nothing - groupMessageDelete :: GroupInfo -> Maybe GroupMember -> SharedMsgId -> Maybe MemberId -> Maybe MsgScope -> RcvMessage -> UTCTime -> CM (Maybe DeliveryTaskContext) - groupMessageDelete gInfo@GroupInfo {membership} m_ sharedMsgId sndMemberId_ scope_ rcvMsg brokerTs = + groupMessageDelete :: GroupInfo -> Maybe GroupMember -> SharedMsgId -> Maybe MemberId -> Maybe MsgScope -> Bool -> RcvMessage -> UTCTime -> CM (Maybe DeliveryTaskContext) + groupMessageDelete gInfo@GroupInfo {membership} m_ sharedMsgId sndMemberId_ scope_ onlyHistory rcvMsg brokerTs = findItem >>= \case Right cci@(CChatItem _ ci@ChatItem {chatDir}) -> case (chatDir, m_) of (CIGroupRcv mem, Just m@GroupMember {memberId}) -> let msgMemberId = fromMaybe memberId sndMemberId_ + isAuthor = sameMemberId memberId mem in case sndMemberId_ of -- regular deletion Nothing - | sameMemberId memberId mem && rcvItemDeletable ci brokerTs -> + | isAuthor && onlyHistory && publicGroupEditor gInfo m -> + delete cci False Nothing $> Nothing + | isAuthor && not onlyHistory && rcvItemDeletable ci brokerTs -> delete cci False Nothing | otherwise -> messageError "x.msg.del: member attempted invalid message delete" $> Nothing -- moderation (not limited by time) Just _ - | sameMemberId memberId mem && msgMemberId == memberId -> + | isAuthor && msgMemberId == memberId -> delete cci False (Just m) | otherwise -> moderate m mem cci (CIChannelRcv, _) - | isNothing sndMemberId_ && isOwner -> delete cci True Nothing + | isNothing sndMemberId_ && isOwner -> + (if onlyHistory then ($> Nothing) else id) $ delete cci True Nothing | otherwise -> messageError "x.msg.del: invalid channel message delete" $> Nothing (CIGroupSnd, Just m) -> moderate m membership cci _ -> messageError "x.msg.del: invalid message deletion" $> Nothing @@ -3096,10 +3131,12 @@ processAgentMessageConn vr user@User {userId} corrId agentConnId agentMessage = deleteGroupLinkIfExists user gInfo -- TODO [relays] possible improvement is to immediately delete rcv queues if isUserGrpFwdRelay unless (isUserGrpFwdRelay gInfo) $ deleteGroupConnections user gInfo False - withStore' $ \db -> updateGroupMemberStatus db userId membership GSMemRemoved + withStore' $ \db -> do + updateGroupMemberStatus db userId membership GSMemRemoved + when (isJust $ relayOwnStatus gInfo) $ updateRelayOwnStatus_ db gInfo RSInactive let membership' = membership {memberStatus = GSMemRemoved} when withMessages $ deleteMessages gInfo membership' SMDSnd - deleteMemberItem gInfo RGEUserDeleted + deleteMemberItem msg gInfo RGEUserDeleted toView $ CEvtDeletedMemberUser user gInfo {membership = membership'} m withMessages msgSigned pure $ Just DJSGroup {jobSpec = DJRelayRemoved} else @@ -3127,7 +3164,11 @@ processAgentMessageConn vr user@User {userId} corrId agentConnId agentMessage = let wasDeleted = memberStatus == GSMemRemoved || memberStatus == GSMemLeft deletedMember' = deletedMember {memberStatus = GSMemRemoved} when withMessages $ deleteMessages gInfo'' deletedMember' SMDRcv - unless wasDeleted $ deleteMemberItem gInfo'' $ RGEMemberDeleted groupMemberId (fromLocalProfile memberProfile) + -- Clear forwardedByMember if it references the deleted member, + -- as the member record was already deleted above. + let RcvMessage {forwardedByMember = fwdBy} = msg + msg' = if fwdBy == Just groupMemberId then (msg :: RcvMessage) {forwardedByMember = Nothing} else msg + unless wasDeleted $ deleteMemberItem msg' gInfo'' $ RGEMemberDeleted groupMemberId (fromLocalProfile memberProfile) toView $ CEvtDeletedMember user gInfo'' m deletedMember' withMessages msgSigned pure deliveryScope where @@ -3135,9 +3176,9 @@ processAgentMessageConn vr user@User {userId} corrId agentConnId agentMessage = | senderRole < GRAdmin || senderRole < memberRole = messageError "x.grp.mem.del with insufficient member permissions" $> Nothing | otherwise = a - deleteMemberItem gi gEvent = do + deleteMemberItem msg' gi gEvent = do (gi', m', scopeInfo) <- mkGroupChatScope gi m - (ci, cInfo) <- saveRcvChatItemNoParse user (CDGroupRcv gi' scopeInfo m') msg brokerTs (CIRcvGroupEvent gEvent) + (ci, cInfo) <- saveRcvChatItemNoParse user (CDGroupRcv gi' scopeInfo m') msg' brokerTs (CIRcvGroupEvent gEvent) groupMsgToView cInfo ci deleteMessages :: MsgDirectionI d => GroupInfo -> GroupMember -> SMsgDirection d -> CM () deleteMessages gInfo' delMem msgDir @@ -3168,10 +3209,6 @@ processAgentMessageConn vr user@User {userId} corrId agentConnId agentMessage = deleteMemberConnection m -- member record is not deleted to allow creation of "member left" chat item gInfo' <- updateMemberRecordDeleted user gInfo m GSMemLeft - when (isRelay m) $ - withStore' $ \db -> do - relay_ <- runExceptT $ getGroupRelayByGMId db (groupMemberId' m) - forM_ relay_ $ \relay -> void $ updateRelayStatus db relay RSInactive gInfo'' <- updatePublicGroupData user gInfo' unless (muteEventInChannel gInfo'' m) $ do (gInfo''', m', scopeInfo) <- mkGroupChatScope gInfo'' m @@ -3230,6 +3267,13 @@ processAgentMessageConn vr user@User {userId} corrId agentConnId agentMessage = let cd = CDGroupRcv g'' scopeInfo m' createGroupFeatureChangedItems user cd CIRcvGroupFeature g g'' + xGrpRelayNew :: GroupInfo -> GroupMember -> ShortLinkContact -> CM (Maybe DeliveryJobScope) + xGrpRelayNew gInfo GroupMember {memberRole} rl + | memberRole < GROwner = messageError "x.grp.relay.new with insufficient member permissions" $> Nothing + | otherwise = do + unless (isUserGrpFwdRelay gInfo) $ connectToRelayAsync user gInfo rl + pure $ Just DJSGroup {jobSpec = DJDeliveryJob {includePending = False}} + xGrpDirectInv :: GroupInfo -> GroupMember -> Connection -> ConnReqInvitation -> Maybe MsgContent -> RcvMessage -> UTCTime -> CM () xGrpDirectInv g@GroupInfo {groupId, groupProfile = gp} m mConn@Connection {connId = mConnId} connReq mContent_ msg brokerTs | not (groupFeatureMemberAllowed SGFDirectMessages m g) = messageError "x.grp.direct.inv: direct messages not allowed" @@ -3347,10 +3391,11 @@ processAgentMessageConn vr user@User {userId} corrId agentConnId agentMessage = XMsgFileDescr sharedMsgId fileDescr -> void $ groupMessageFileDescription gInfo author_ sharedMsgId fileDescr XMsgUpdate sharedMsgId mContent mentions ttl live msgScope asGroup_ -> void $ memberCanSend author_ msgScope $ groupMessageUpdate gInfo author_ sharedMsgId mContent mentions msgScope rcvMsg msgTs ttl live asGroup_ - XMsgDel sharedMsgId memId scope_ -> void $ groupMessageDelete gInfo author_ sharedMsgId memId scope_ rcvMsg msgTs + XMsgDel sharedMsgId memId scope_ _ -> void $ groupMessageDelete gInfo author_ sharedMsgId memId scope_ False rcvMsg msgTs XMsgReact sharedMsgId memId scope_ reaction add -> withAuthor XMsgReact_ $ \author -> void $ groupMsgReaction gInfo author sharedMsgId memId scope_ reaction add rcvMsg msgTs XFileCancel sharedMsgId -> void $ xFileCancelGroup gInfo author_ sharedMsgId XInfo p -> withAuthor XInfo_ $ \author -> void $ xInfoMember gInfo author p rcvMsg msgTs + XGrpRelayNew rl -> withAuthor XGrpRelayNew_ $ \author -> void $ xGrpRelayNew gInfo author rl XGrpMemNew memInfo msgScope -> withAuthor XGrpMemNew_ $ \author -> void $ xGrpMemNew gInfo author memInfo msgScope rcvMsg msgTs XGrpMemRole memId memRole -> withAuthor XGrpMemRole_ $ \author -> void $ xGrpMemRole gInfo author memId memRole rcvMsg msgTs XGrpMemRestrict memId memRestrictions -> withAuthor XGrpMemRestrict_ $ \author -> void $ xGrpMemRestrict gInfo author memId memRestrictions rcvMsg msgTs @@ -3526,19 +3571,24 @@ runDeliveryTaskWorker a deliveryKey Worker {doWork} = do processDeliveryTask :: MessageDeliveryTask -> CM () processDeliveryTask task@MessageDeliveryTask {jobScope} = case jobScopeImpliedSpec jobScope of - DJDeliveryJob _includePending -> - withWorkItems a doWork (withStore' $ \db -> getNextDeliveryTasks db gInfo task) $ \nextTasks -> do - let (body, taskIds, largeTaskIds) = batchDeliveryTasks1 vr maxEncodedMsgLength nextTasks - withStore' $ \db -> do - createMsgDeliveryJob db gInfo jobScope (singleSenderGMId_ nextTasks) body - forM_ taskIds $ \taskId -> updateDeliveryTaskStatus db taskId DTSProcessed - forM_ largeTaskIds $ \taskId -> setDeliveryTaskErrStatus db taskId "large" - lift . void $ getDeliveryJobWorker True deliveryKey + DJDeliveryJob _includePending + | relayOwnStatus gInfo == Just RSInactive -> do + logWarn "delivery task worker: relay inactive" + withStore' $ \db -> setDeliveryTaskErrStatus db (deliveryTaskId task) "relay inactive" + | otherwise -> + withWorkItems a doWork (withStore' $ \db -> getNextDeliveryTasks db gInfo task) $ \nextTasks -> do + let (body, taskIds, largeTaskIds) = batchDeliveryTasks1 vr maxEncodedMsgLength nextTasks + withStore' $ \db -> do + createMsgDeliveryJob db gInfo jobScope (singleSenderGMId_ nextTasks) body + forM_ taskIds $ \taskId -> updateDeliveryTaskStatus db taskId DTSProcessed + forM_ largeTaskIds $ \taskId -> setDeliveryTaskErrStatus db taskId "large" + lift . void $ getDeliveryJobWorker True deliveryKey where singleSenderGMId_ :: NonEmpty MessageDeliveryTask -> Maybe GroupMemberId singleSenderGMId_ (MessageDeliveryTask {senderGMId = senderGMId'} :| ts) | all (\MessageDeliveryTask {senderGMId} -> senderGMId == senderGMId') ts = Just senderGMId' | otherwise = Nothing + -- DJRelayRemoved is allowed when RSInactive - it forwards XGrpMemDel about relay's own deletion DJRelayRemoved | workerScope /= DWSGroup -> throwChatError $ CEInternalError "delivery task worker: relay removed task in wrong worker scope" @@ -3591,9 +3641,14 @@ runDeliveryJobWorker a deliveryKey Worker {doWork} = do processDeliveryJob :: MessageDeliveryJob -> CM () processDeliveryJob job = case jobScopeImpliedSpec jobScope of - DJDeliveryJob _includePending -> do - sendBodyToMembers - withStore' $ \db -> updateDeliveryJobStatus db jobId DJSComplete + DJDeliveryJob _includePending + | relayOwnStatus gInfo == Just RSInactive -> do + logWarn "delivery job worker: relay inactive" + withStore' $ \db -> setDeliveryJobErrStatus db (deliveryJobId job) "relay inactive" + | otherwise -> do + sendBodyToMembers + withStore' $ \db -> updateDeliveryJobStatus db jobId DJSComplete + -- DJRelayRemoved is allowed when RSInactive - it forwards XGrpMemDel about relay's own deletion DJRelayRemoved | workerScope /= DWSGroup -> throwChatError $ CEInternalError "delivery job worker: relay removed job in wrong worker scope" diff --git a/src/Simplex/Chat/Markdown.hs b/src/Simplex/Chat/Markdown.hs index 86ba441a57..39000fde84 100644 --- a/src/Simplex/Chat/Markdown.hs +++ b/src/Simplex/Chat/Markdown.hs @@ -360,7 +360,7 @@ parseUri s = case U.parseURI U.laxURIParserOptions s of sanitizeUri :: Bool -> U.URI -> Maybe U.URI sanitizeUri safe uri@U.URI {uriAuthority, uriPath, uriQuery = U.Query originalQS} = let sanitizedQS - | safe = filter (not . isSafeBlacklisted . fst) originalQS + | safe = filter (\(n, _) -> isWhitelisted n || not (isSafeBlacklisted n)) originalQS | isNamePath = case originalQS of p@(n, _) : ps -> (if isWhitelisted n || not (isBlacklisted n) then (p :) else id) $ filter (isWhitelisted . fst) ps [] -> [] diff --git a/src/Simplex/Chat/Messages/CIContent.hs b/src/Simplex/Chat/Messages/CIContent.hs index 2dc751d6bb..dcc34de6da 100644 --- a/src/Simplex/Chat/Messages/CIContent.hs +++ b/src/Simplex/Chat/Messages/CIContent.hs @@ -105,7 +105,7 @@ msgDirectionIntP = \case 1 -> Just MDSnd _ -> Nothing -data CIDeleteMode = CIDMBroadcast | CIDMInternal | CIDMInternalMark +data CIDeleteMode = CIDMBroadcast | CIDMInternal | CIDMInternalMark | CIDMHistory deriving (Show) instance StrEncoding CIDeleteMode where @@ -113,11 +113,13 @@ instance StrEncoding CIDeleteMode where CIDMBroadcast -> "broadcast" CIDMInternal -> "internal" CIDMInternalMark -> "internalMark" + CIDMHistory -> "history" strP = A.takeTill (== ' ') >>= \case "broadcast" -> pure CIDMBroadcast "internal" -> pure CIDMInternal "internalMark" -> pure CIDMInternalMark + "history" -> pure CIDMHistory _ -> fail "bad CIDeleteMode" instance ToJSON CIDeleteMode where @@ -132,6 +134,7 @@ ciDeleteModeToText = \case CIDMBroadcast -> "this item is deleted (broadcast)" CIDMInternal -> "this item is deleted (locally)" CIDMInternalMark -> "this item is deleted (locally)" + CIDMHistory -> "this item is deleted (from history)" -- This type is used both in API and in DB, so we use different JSON encodings for the database and for the API -- ! Nested sum types also have to use different encodings for database and API diff --git a/src/Simplex/Chat/Protocol.hs b/src/Simplex/Chat/Protocol.hs index b7e838c52b..3436a64132 100644 --- a/src/Simplex/Chat/Protocol.hs +++ b/src/Simplex/Chat/Protocol.hs @@ -423,7 +423,7 @@ data ChatMsgEvent (e :: MsgEncoding) where XMsgNew :: MsgContainer -> ChatMsgEvent 'Json XMsgFileDescr :: {msgId :: SharedMsgId, fileDescr :: FileDescr} -> ChatMsgEvent 'Json XMsgUpdate :: {msgId :: SharedMsgId, content :: MsgContent, mentions :: Map MemberName MsgMention, ttl :: Maybe Int, live :: Maybe Bool, scope :: Maybe MsgScope, asGroup :: Maybe Bool} -> ChatMsgEvent 'Json - XMsgDel :: {msgId :: SharedMsgId, memberId :: Maybe MemberId, scope :: Maybe MsgScope} -> ChatMsgEvent 'Json + XMsgDel :: {msgId :: SharedMsgId, memberId :: Maybe MemberId, scope :: Maybe MsgScope, onlyHistory :: Bool} -> ChatMsgEvent 'Json XMsgDeleted :: ChatMsgEvent 'Json XMsgReact :: {msgId :: SharedMsgId, memberId :: Maybe MemberId, scope :: Maybe MsgScope, reaction :: MsgReaction, add :: Bool} -> ChatMsgEvent 'Json XFile :: FileInvitation -> ChatMsgEvent 'Json -- TODO discontinue @@ -443,6 +443,7 @@ data ChatMsgEvent (e :: MsgEncoding) where XGrpRelayInv :: GroupRelayInvitation -> ChatMsgEvent 'Json XGrpRelayAcpt :: ShortLinkContact -> ChatMsgEvent 'Json XGrpRelayTest :: ByteString -> Maybe ByteString -> ChatMsgEvent 'Json + XGrpRelayNew :: ShortLinkContact -> ChatMsgEvent 'Json XGrpMemNew :: MemberInfo -> Maybe MsgScope -> ChatMsgEvent 'Json XGrpMemIntro :: MemberInfo -> Maybe MemberRestrictions -> ChatMsgEvent 'Json XGrpMemInv :: MemberId -> IntroInvitation -> ChatMsgEvent 'Json @@ -492,6 +493,7 @@ isForwardedGroupMsg ev = case ev of XMsgReact {} -> True XFileCancel _ -> True XInfo _ -> True + XGrpRelayNew _ -> True XGrpMemNew {} -> True XGrpMemRole {} -> True XGrpMemRestrict {} -> True @@ -986,6 +988,7 @@ data CMEventTag (e :: MsgEncoding) where XGrpRelayInv_ :: CMEventTag 'Json XGrpRelayAcpt_ :: CMEventTag 'Json XGrpRelayTest_ :: CMEventTag 'Json + XGrpRelayNew_ :: CMEventTag 'Json XGrpMemNew_ :: CMEventTag 'Json XGrpMemIntro_ :: CMEventTag 'Json XGrpMemInv_ :: CMEventTag 'Json @@ -1043,6 +1046,7 @@ instance MsgEncodingI e => StrEncoding (CMEventTag e) where XGrpRelayInv_ -> "x.grp.relay.inv" XGrpRelayAcpt_ -> "x.grp.relay.acpt" XGrpRelayTest_ -> "x.grp.relay.test" + XGrpRelayNew_ -> "x.grp.relay.new" XGrpMemNew_ -> "x.grp.mem.new" XGrpMemIntro_ -> "x.grp.mem.intro" XGrpMemInv_ -> "x.grp.mem.inv" @@ -1101,6 +1105,7 @@ instance StrEncoding ACMEventTag where "x.grp.relay.inv" -> XGrpRelayInv_ "x.grp.relay.acpt" -> XGrpRelayAcpt_ "x.grp.relay.test" -> XGrpRelayTest_ + "x.grp.relay.new" -> XGrpRelayNew_ "x.grp.mem.new" -> XGrpMemNew_ "x.grp.mem.intro" -> XGrpMemIntro_ "x.grp.mem.inv" -> XGrpMemInv_ @@ -1155,6 +1160,7 @@ toCMEventTag msg = case msg of XGrpRelayInv _ -> XGrpRelayInv_ XGrpRelayAcpt _ -> XGrpRelayAcpt_ XGrpRelayTest {} -> XGrpRelayTest_ + XGrpRelayNew _ -> XGrpRelayNew_ XGrpMemNew {} -> XGrpMemNew_ XGrpMemIntro _ _ -> XGrpMemIntro_ XGrpMemInv _ _ -> XGrpMemInv_ @@ -1227,6 +1233,7 @@ requiresSignature = \case XGrpMemRole_ -> True XGrpMemRestrict_ -> True XGrpLeave_ -> True + XGrpRelayNew_ -> True XInfo_ -> True _ -> False @@ -1281,7 +1288,7 @@ appJsonToCM AppMessageJson {v, msgId, event, params} = do scope <- opt "scope" asGroup <- opt "asGroup" pure XMsgUpdate {msgId = msgId', content, mentions, ttl, live, scope, asGroup} - XMsgDel_ -> XMsgDel <$> p "msgId" <*> opt "memberId" <*> opt "scope" + XMsgDel_ -> XMsgDel <$> p "msgId" <*> opt "memberId" <*> opt "scope" <*> (fromMaybe False <$> opt "onlyHistory") XMsgDeleted_ -> pure XMsgDeleted XMsgReact_ -> XMsgReact <$> p "msgId" <*> opt "memberId" <*> opt "scope" <*> p "reaction" <*> p "add" XFile_ -> XFile <$> p "file" @@ -1311,6 +1318,7 @@ appJsonToCM AppMessageJson {v, msgId, event, params} = do B64UrlByteString challenge <- p "challenge" sig_ <- fmap (\(B64UrlByteString s) -> s) <$> opt "signature" pure $ XGrpRelayTest challenge sig_ + XGrpRelayNew_ -> XGrpRelayNew <$> p "relayLink" XGrpMemNew_ -> XGrpMemNew <$> p "memberInfo" <*> opt "scope" XGrpMemIntro_ -> XGrpMemIntro <$> p "memberInfo" <*> opt "memberRestrictions" XGrpMemInv_ -> XGrpMemInv <$> p "memberId" <*> p "memberIntro" @@ -1358,7 +1366,7 @@ chatToAppMessage chatMsg@ChatMessage {chatVRange, msgId, chatMsgEvent} = case en _ -> JM.empty XMsgFileDescr msgId' fileDescr -> o ["msgId" .= msgId', "fileDescr" .= fileDescr] XMsgUpdate {msgId = msgId', content, mentions, ttl, live, scope, asGroup} -> o $ ("asGroup" .=? asGroup) $ ("ttl" .=? ttl) $ ("live" .=? live) $ ("scope" .=? scope) $ ("mentions" .=? nonEmptyMap mentions) ["msgId" .= msgId', "content" .= content] - XMsgDel msgId' memberId scope -> o $ ("memberId" .=? memberId) $ ("scope" .=? scope) ["msgId" .= msgId'] + XMsgDel msgId' memberId scope onlyHistory -> o $ ("memberId" .=? memberId) $ ("scope" .=? scope) $ ("onlyHistory" .=? justTrue onlyHistory) ["msgId" .= msgId'] XMsgDeleted -> JM.empty XMsgReact msgId' memberId scope reaction add -> o $ ("memberId" .=? memberId) $ ("scope" .=? scope) ["msgId" .= msgId', "reaction" .= reaction, "add" .= add] XFile fileInv -> o ["file" .= fileInv] @@ -1380,6 +1388,7 @@ chatToAppMessage chatMsg@ChatMessage {chatVRange, msgId, chatMsgEvent} = case en XGrpRelayTest challenge sig_ -> o $ ("signature" .=? (B64UrlByteString <$> sig_)) ["challenge" .= B64UrlByteString challenge] + XGrpRelayNew relayLink -> o ["relayLink" .= relayLink] XGrpMemNew memInfo scope -> o $ ("scope" .=? scope) ["memberInfo" .= memInfo] XGrpMemIntro memInfo memRestrictions -> o $ ("memberRestrictions" .=? memRestrictions) ["memberInfo" .= memInfo] XGrpMemInv memId memIntro -> o ["memberId" .= memId, "memberIntro" .= memIntro] diff --git a/src/Simplex/Chat/Store/Groups.hs b/src/Simplex/Chat/Store/Groups.hs index b375f77eb0..4bb94ba2a8 100644 --- a/src/Simplex/Chat/Store/Groups.hs +++ b/src/Simplex/Chat/Store/Groups.hs @@ -94,6 +94,9 @@ module Simplex.Chat.Store.Groups setGroupInProgressDone, createRelayRequestGroup, updateRelayOwnStatusFromTo, + updateRelayOwnStatus_, + getRelayServedGroups, + getRelayInactiveGroups, createNewContactMemberAsync, createJoiningMember, getMemberJoinRequest, @@ -188,7 +191,7 @@ import Data.Maybe (catMaybes, fromMaybe, isJust, isNothing) import Data.Ord (Down (..)) import Data.Text (Text) import qualified Data.Text as T -import Data.Time.Clock (UTCTime (..), getCurrentTime) +import Data.Time.Clock (NominalDiffTime, UTCTime (..), addUTCTime, getCurrentTime) import Data.Text.Encoding (encodeUtf8) import Simplex.Chat.Messages import Simplex.Chat.Operators @@ -1378,7 +1381,12 @@ getCreateRelayForMember db vr gVar user@User {userId, userContactId} GroupInfo { maybeFirstRow (toContactMember vr user) $ DB.query db - (groupMemberQuery <> " WHERE m.group_id = ? AND m.relay_link = ?") +#if defined(dbPostgres) + (groupMemberQuery <> " WHERE m.group_id = ? AND m.relay_link = ? AND is_current_member(m.member_status)") +#else + -- skips GSMemLeft historical rows so re-add allocates a fresh row instead of resurrecting + (groupMemberQuery <> " JOIN group_member_status_predicates sp ON m.member_status = sp.member_status WHERE m.group_id = ? AND m.relay_link = ? AND sp.current_member = 1") +#endif (groupId, relayLink) createRelayMember = do currentTs <- liftIO getCurrentTime @@ -1585,7 +1593,29 @@ updateRelayOwnStatusFromTo db gInfo@GroupInfo {groupId} fromStatus toStatus = do updateRelayOwnStatus_ :: DB.Connection -> GroupInfo -> RelayStatus -> IO () updateRelayOwnStatus_ db GroupInfo {groupId} relayStatus = do currentTs <- getCurrentTime - DB.execute db "UPDATE groups SET relay_own_status = ?, updated_at = ? WHERE group_id = ?" (relayStatus, currentTs, groupId) + let inactiveAt_ = if relayStatus == RSInactive then Just currentTs else Nothing + DB.execute db "UPDATE groups SET relay_own_status = ?, relay_inactive_at = ?, updated_at = ? WHERE group_id = ?" (relayStatus, inactiveAt_, currentTs, groupId) + +getRelayServedGroups :: DB.Connection -> VersionRangeChat -> User -> IO [GroupInfo] +getRelayServedGroups db vr User {userId, userContactId} = do + map (toGroupInfo vr userContactId []) + <$> DB.query + db + ( groupInfoQuery + <> " WHERE g.user_id = ? AND mu.contact_id = ? AND g.relay_own_status IN (?, ?)" + ) + (userId, userContactId, RSAccepted, RSActive) + +getRelayInactiveGroups :: DB.Connection -> VersionRangeChat -> User -> NominalDiffTime -> IO [GroupInfo] +getRelayInactiveGroups db vr User {userId, userContactId} ttl = do + cutoffTs <- addUTCTime (- ttl) <$> getCurrentTime + map (toGroupInfo vr userContactId []) + <$> DB.query + db + ( groupInfoQuery + <> " WHERE g.user_id = ? AND mu.contact_id = ? AND g.relay_own_status = ? AND g.relay_inactive_at IS NOT NULL AND g.relay_inactive_at <= ?" + ) + (userId, userContactId, RSInactive, cutoffTs) createNewContactMemberAsync :: DB.Connection -> TVar ChaChaDRG -> User -> GroupInfo -> Contact -> GroupMemberRole -> (CommandId, ConnId) -> VersionChat -> VersionRangeChat -> SubscriptionMode -> ExceptT StoreError IO () createNewContactMemberAsync db gVar user@User {userId, userContactId} GroupInfo {groupId, membership} Contact {contactId, localDisplayName, profile} memberRole (cmdId, agentConnId) chatV peerChatVRange subMode = @@ -1814,12 +1844,12 @@ updatePublicMemberCount db vr user GroupInfo {groupId} = do relayCount <- fromMaybe 0 <$> maybeFirstRow fromOnly (DB.query db - [sql| - SELECT COUNT(1) FROM group_members - WHERE group_id = ? AND member_role = ? - AND member_status IN (?,?,?,?,?,?,?) - |] - (groupId, GRRelay, GSMemIntroduced, GSMemIntroInvited, GSMemAccepted, GSMemAnnounced, GSMemConnected, GSMemComplete, GSMemCreator)) +#if defined(dbPostgres) + "SELECT COUNT(1) FROM group_members WHERE group_id = ? AND member_role = ? AND is_current_member(member_status)" +#else + "SELECT COUNT(1) FROM group_members m JOIN group_member_status_predicates sp ON m.member_status = sp.member_status WHERE m.group_id = ? AND m.member_role = ? AND sp.current_member = 1" +#endif + (groupId, GRRelay)) let publicCount = max 0 (totalCount - relayCount) :: Int64 currentTs <- getCurrentTime DB.execute db "UPDATE groups SET public_member_count = ?, updated_at = ? WHERE group_id = ?" (publicCount, currentTs, groupId) diff --git a/src/Simplex/Chat/Store/Postgres/Migrations.hs b/src/Simplex/Chat/Store/Postgres/Migrations.hs index cdb461ea70..822068a771 100644 --- a/src/Simplex/Chat/Store/Postgres/Migrations.hs +++ b/src/Simplex/Chat/Store/Postgres/Migrations.hs @@ -29,6 +29,7 @@ import Simplex.Chat.Store.Postgres.Migrations.M20260122_has_link import Simplex.Chat.Store.Postgres.Migrations.M20260222_chat_relays import Simplex.Chat.Store.Postgres.Migrations.M20260403_item_viewed import Simplex.Chat.Store.Postgres.Migrations.M20260429_relay_request_retries +import Simplex.Chat.Store.Postgres.Migrations.M20260507_relay_inactive_at import Simplex.Messaging.Agent.Store.Shared (Migration (..)) schemaMigrations :: [(String, Text, Maybe Text)] @@ -57,7 +58,8 @@ schemaMigrations = ("20260122_has_link", m20260122_has_link, Just down_m20260122_has_link), ("20260222_chat_relays", m20260222_chat_relays, Just down_m20260222_chat_relays), ("20260403_item_viewed", m20260403_item_viewed, Just down_m20260403_item_viewed), - ("20260429_relay_request_retries", m20260429_relay_request_retries, Just down_m20260429_relay_request_retries) + ("20260429_relay_request_retries", m20260429_relay_request_retries, Just down_m20260429_relay_request_retries), + ("20260507_relay_inactive_at", m20260507_relay_inactive_at, Just down_m20260507_relay_inactive_at) ] -- | The list of migrations in ascending order by date diff --git a/src/Simplex/Chat/Store/Postgres/Migrations/M20260507_relay_inactive_at.hs b/src/Simplex/Chat/Store/Postgres/Migrations/M20260507_relay_inactive_at.hs new file mode 100644 index 0000000000..f35927113c --- /dev/null +++ b/src/Simplex/Chat/Store/Postgres/Migrations/M20260507_relay_inactive_at.hs @@ -0,0 +1,19 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE QuasiQuotes #-} + +module Simplex.Chat.Store.Postgres.Migrations.M20260507_relay_inactive_at where + +import Data.Text (Text) +import Text.RawString.QQ (r) + +m20260507_relay_inactive_at :: Text +m20260507_relay_inactive_at = + [r| +ALTER TABLE groups ADD COLUMN relay_inactive_at TIMESTAMPTZ; +|] + +down_m20260507_relay_inactive_at :: Text +down_m20260507_relay_inactive_at = + [r| +ALTER TABLE groups DROP COLUMN relay_inactive_at; +|] diff --git a/src/Simplex/Chat/Store/Postgres/Migrations/chat_schema.sql b/src/Simplex/Chat/Store/Postgres/Migrations/chat_schema.sql index 354c41eaaf..495a6bb752 100644 --- a/src/Simplex/Chat/Store/Postgres/Migrations/chat_schema.sql +++ b/src/Simplex/Chat/Store/Postgres/Migrations/chat_schema.sql @@ -962,7 +962,8 @@ CREATE TABLE test_chat_schema.groups ( public_member_count bigint, relay_request_retries bigint DEFAULT 0 NOT NULL, relay_request_delay bigint DEFAULT 0 NOT NULL, - relay_request_execute_at timestamp with time zone DEFAULT '1970-01-01 01:00:00+01'::timestamp with time zone NOT NULL + relay_request_execute_at timestamp with time zone DEFAULT '1970-01-01 04:00:00+04'::timestamp with time zone NOT NULL, + relay_inactive_at timestamp with time zone ); diff --git a/src/Simplex/Chat/Store/SQLite/Migrations.hs b/src/Simplex/Chat/Store/SQLite/Migrations.hs index 0ab8911ffd..4ee3f44b07 100644 --- a/src/Simplex/Chat/Store/SQLite/Migrations.hs +++ b/src/Simplex/Chat/Store/SQLite/Migrations.hs @@ -152,6 +152,7 @@ import Simplex.Chat.Store.SQLite.Migrations.M20260122_has_link import Simplex.Chat.Store.SQLite.Migrations.M20260222_chat_relays import Simplex.Chat.Store.SQLite.Migrations.M20260403_item_viewed import Simplex.Chat.Store.SQLite.Migrations.M20260429_relay_request_retries +import Simplex.Chat.Store.SQLite.Migrations.M20260507_relay_inactive_at import Simplex.Messaging.Agent.Store.Shared (Migration (..)) schemaMigrations :: [(String, Query, Maybe Query)] @@ -303,7 +304,8 @@ schemaMigrations = ("20260122_has_link", m20260122_has_link, Just down_m20260122_has_link), ("20260222_chat_relays", m20260222_chat_relays, Just down_m20260222_chat_relays), ("20260403_item_viewed", m20260403_item_viewed, Just down_m20260403_item_viewed), - ("20260429_relay_request_retries", m20260429_relay_request_retries, Just down_m20260429_relay_request_retries) + ("20260429_relay_request_retries", m20260429_relay_request_retries, Just down_m20260429_relay_request_retries), + ("20260507_relay_inactive_at", m20260507_relay_inactive_at, Just down_m20260507_relay_inactive_at) ] -- | The list of migrations in ascending order by date diff --git a/src/Simplex/Chat/Store/SQLite/Migrations/M20260507_relay_inactive_at.hs b/src/Simplex/Chat/Store/SQLite/Migrations/M20260507_relay_inactive_at.hs new file mode 100644 index 0000000000..0596d4892a --- /dev/null +++ b/src/Simplex/Chat/Store/SQLite/Migrations/M20260507_relay_inactive_at.hs @@ -0,0 +1,18 @@ +{-# LANGUAGE QuasiQuotes #-} + +module Simplex.Chat.Store.SQLite.Migrations.M20260507_relay_inactive_at where + +import Database.SQLite.Simple (Query) +import Database.SQLite.Simple.QQ (sql) + +m20260507_relay_inactive_at :: Query +m20260507_relay_inactive_at = + [sql| +ALTER TABLE groups ADD COLUMN relay_inactive_at TEXT; +|] + +down_m20260507_relay_inactive_at :: Query +down_m20260507_relay_inactive_at = + [sql| +ALTER TABLE groups DROP COLUMN relay_inactive_at; +|] diff --git a/src/Simplex/Chat/Store/SQLite/Migrations/agent_query_plans.txt b/src/Simplex/Chat/Store/SQLite/Migrations/agent_query_plans.txt index 7a20f8e98f..de1e0a093d 100644 --- a/src/Simplex/Chat/Store/SQLite/Migrations/agent_query_plans.txt +++ b/src/Simplex/Chat/Store/SQLite/Migrations/agent_query_plans.txt @@ -1197,6 +1197,10 @@ Query: UPDATE connections SET smp_agent_version = ? WHERE conn_id = ? Plan: SEARCH connections USING PRIMARY KEY (conn_id=?) +Query: UPDATE connections SET smp_agent_version = ?, pq_support = ?, enable_ntfs = ? WHERE conn_id = ? +Plan: +SEARCH connections USING PRIMARY KEY (conn_id=?) + Query: UPDATE messages SET msg_body = x'' WHERE conn_id = ? AND internal_id = ? Plan: SEARCH messages USING PRIMARY KEY (conn_id=? AND internal_id=?) diff --git a/src/Simplex/Chat/Store/SQLite/Migrations/chat_query_plans.txt b/src/Simplex/Chat/Store/SQLite/Migrations/chat_query_plans.txt index 7a226bf78a..f4590f48c9 100644 --- a/src/Simplex/Chat/Store/SQLite/Migrations/chat_query_plans.txt +++ b/src/Simplex/Chat/Store/SQLite/Migrations/chat_query_plans.txt @@ -1405,14 +1405,6 @@ SEARCH users USING INTEGER PRIMARY KEY (rowid=?) INDEX 2 SEARCH users USING INDEX sqlite_autoindex_users_1 (contact_id=?) -Query: - SELECT COUNT(1) FROM group_members - WHERE group_id = ? AND member_role = ? - AND member_status IN (?,?,?,?,?,?,?) - -Plan: -SEARCH group_members USING INDEX idx_group_members_group_id_index_in_group (group_id=?) - Query: SELECT agent_conn_id FROM ( SELECT @@ -4642,6 +4634,15 @@ Query: Plan: +Query: + INSERT INTO connections ( + user_id, agent_conn_id, conn_status, conn_type, contact_conn_initiated, + group_member_id, via_short_link_contact, custom_user_profile_id, via_group_link, + created_at, updated_at, to_subscribe + ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?) + +Plan: + Query: INSERT INTO connections ( user_id, agent_conn_id, conn_status, conn_type, contact_conn_initiated, @@ -4927,6 +4928,16 @@ Query: Plan: SEARCH connections USING INTEGER PRIMARY KEY (rowid=?) +Query: + UPDATE connections + SET via_contact_uri = ?, via_contact_uri_hash = ?, group_link_id = ?, + conn_chat_version = ?, pq_support = ?, pq_encryption = ?, + updated_at = ? + WHERE user_id = ? AND connection_id = ? + +Plan: +SEARCH connections USING INTEGER PRIMARY KEY (rowid=?) + Query: UPDATE connections_sync SET should_sync = 0, last_sync_ts = ? @@ -5405,6 +5416,26 @@ SCAN chat_items USING COVERING INDEX idx_chat_items_group_member_id SEARCH p USING INTEGER PRIMARY KEY (rowid=?) SEARCH c USING INDEX idx_connections_group_member_id (group_member_id=?) LEFT-JOIN +Query: + SELECT + m.group_member_id, m.group_id, m.index_in_group, m.member_id, m.peer_chat_min_version, m.peer_chat_max_version, m.member_role, m.member_category, m.member_status, m.show_messages, m.member_restriction, + m.invited_by, m.invited_by_group_member_id, m.local_display_name, m.contact_id, m.contact_profile_id, p.contact_profile_id, p.display_name, p.full_name, p.short_descr, p.image, p.contact_link, p.chat_peer_type, p.local_alias, p.preferences, + m.created_at, m.updated_at, + m.support_chat_ts, m.support_chat_items_unread, m.support_chat_items_member_attention, m.support_chat_items_mentions, m.support_chat_last_msg_from_member_ts, m.member_pub_key, m.relay_link, + c.connection_id, c.agent_conn_id, c.conn_level, c.via_contact, c.via_user_contact_link, c.via_group_link, c.group_link_id, c.xcontact_id, c.custom_user_profile_id, + c.conn_status, c.conn_type, c.contact_conn_initiated, c.local_alias, c.contact_id, c.group_member_id, c.user_contact_link_id, + c.created_at, c.security_code, c.security_code_verified_at, c.pq_support, c.pq_encryption, c.pq_snd_enabled, c.pq_rcv_enabled, c.auth_err_counter, c.quota_err_counter, + c.conn_chat_version, c.peer_chat_min_version, c.peer_chat_max_version + FROM group_members m + JOIN contact_profiles p ON p.contact_profile_id = COALESCE(m.member_profile_id, m.contact_profile_id) + LEFT JOIN connections c ON c.group_member_id = m.group_member_id + JOIN group_member_status_predicates sp ON m.member_status = sp.member_status WHERE m.group_id = ? AND m.relay_link = ? AND sp.current_member = 1 +Plan: +SEARCH m USING INDEX idx_group_members_group_id_index_in_group (group_id=?) +SEARCH sp USING INDEX sqlite_autoindex_group_member_status_predicates_1 (member_status=?) +SEARCH p USING INTEGER PRIMARY KEY (rowid=?) +SEARCH c USING INDEX idx_connections_group_member_id (group_member_id=?) LEFT-JOIN + Query: SELECT m.group_member_id, m.group_id, m.index_in_group, m.member_id, m.peer_chat_min_version, m.peer_chat_max_version, m.member_role, m.member_category, m.member_status, m.show_messages, m.member_restriction, @@ -5500,25 +5531,6 @@ SEARCH m USING INDEX sqlite_autoindex_group_members_1 (group_id=? AND member_id= SEARCH p USING INTEGER PRIMARY KEY (rowid=?) SEARCH c USING INDEX idx_connections_group_member_id (group_member_id=?) LEFT-JOIN -Query: - SELECT - m.group_member_id, m.group_id, m.index_in_group, m.member_id, m.peer_chat_min_version, m.peer_chat_max_version, m.member_role, m.member_category, m.member_status, m.show_messages, m.member_restriction, - m.invited_by, m.invited_by_group_member_id, m.local_display_name, m.contact_id, m.contact_profile_id, p.contact_profile_id, p.display_name, p.full_name, p.short_descr, p.image, p.contact_link, p.chat_peer_type, p.local_alias, p.preferences, - m.created_at, m.updated_at, - m.support_chat_ts, m.support_chat_items_unread, m.support_chat_items_member_attention, m.support_chat_items_mentions, m.support_chat_last_msg_from_member_ts, m.member_pub_key, m.relay_link, - c.connection_id, c.agent_conn_id, c.conn_level, c.via_contact, c.via_user_contact_link, c.via_group_link, c.group_link_id, c.xcontact_id, c.custom_user_profile_id, - c.conn_status, c.conn_type, c.contact_conn_initiated, c.local_alias, c.contact_id, c.group_member_id, c.user_contact_link_id, - c.created_at, c.security_code, c.security_code_verified_at, c.pq_support, c.pq_encryption, c.pq_snd_enabled, c.pq_rcv_enabled, c.auth_err_counter, c.quota_err_counter, - c.conn_chat_version, c.peer_chat_min_version, c.peer_chat_max_version - FROM group_members m - JOIN contact_profiles p ON p.contact_profile_id = COALESCE(m.member_profile_id, m.contact_profile_id) - LEFT JOIN connections c ON c.group_member_id = m.group_member_id - WHERE m.group_id = ? AND m.relay_link = ? -Plan: -SEARCH m USING INDEX idx_group_members_group_id_index_in_group (group_id=?) -SEARCH p USING INTEGER PRIMARY KEY (rowid=?) -SEARCH c USING INDEX idx_connections_group_member_id (group_member_id=?) LEFT-JOIN - Query: SELECT m.group_member_id, m.group_id, m.index_in_group, m.member_id, m.peer_chat_min_version, m.peer_chat_max_version, m.member_role, m.member_category, m.member_status, m.show_messages, m.member_restriction, @@ -6590,6 +6602,11 @@ Query: SELECT COUNT(1) FROM group_members WHERE member_role = 'owner' AND member Plan: SCAN group_members +Query: SELECT COUNT(1) FROM group_members m JOIN group_member_status_predicates sp ON m.member_status = sp.member_status WHERE m.group_id = ? AND m.member_role = ? AND sp.current_member = 1 +Plan: +SEARCH m USING INDEX idx_group_members_group_id_index_in_group (group_id=?) +SEARCH sp USING INDEX sqlite_autoindex_group_member_status_predicates_1 (member_status=?) + Query: SELECT COUNT(1) FROM groups WHERE user_id = ? AND chat_item_ttl > 0 Plan: SEARCH groups USING INDEX sqlite_autoindex_groups_2 (user_id=?) @@ -6808,6 +6825,10 @@ Query: SELECT last_insert_rowid() Plan: SCAN CONSTANT ROW +Query: SELECT local_display_name FROM group_members +Plan: +SCAN group_members USING COVERING INDEX idx_group_members_user_id_local_display_name + Query: SELECT max(active_order) FROM users Plan: SEARCH users @@ -7148,7 +7169,7 @@ Query: UPDATE groups SET public_member_count = ?, updated_at = ? WHERE group_id Plan: SEARCH groups USING INTEGER PRIMARY KEY (rowid=?) -Query: UPDATE groups SET relay_own_status = ?, updated_at = ? WHERE group_id = ? +Query: UPDATE groups SET relay_own_status = ?, relay_inactive_at = ?, updated_at = ? WHERE group_id = ? Plan: SEARCH groups USING INTEGER PRIMARY KEY (rowid=?) diff --git a/src/Simplex/Chat/Store/SQLite/Migrations/chat_schema.sql b/src/Simplex/Chat/Store/SQLite/Migrations/chat_schema.sql index f081238908..b7a6db437b 100644 --- a/src/Simplex/Chat/Store/SQLite/Migrations/chat_schema.sql +++ b/src/Simplex/Chat/Store/SQLite/Migrations/chat_schema.sql @@ -176,7 +176,8 @@ CREATE TABLE groups( public_member_count INTEGER, relay_request_retries INTEGER NOT NULL DEFAULT 0, relay_request_delay INTEGER NOT NULL DEFAULT 0, - relay_request_execute_at TEXT NOT NULL DEFAULT '1970-01-01 00:00:00', -- received + relay_request_execute_at TEXT NOT NULL DEFAULT '1970-01-01 00:00:00', + relay_inactive_at TEXT, -- received FOREIGN KEY(user_id, local_display_name) REFERENCES display_names(user_id, local_display_name) ON DELETE CASCADE diff --git a/src/Simplex/Chat/Types.hs b/src/Simplex/Chat/Types.hs index b068e6b679..e2efcdf6d6 100644 --- a/src/Simplex/Chat/Types.hs +++ b/src/Simplex/Chat/Types.hs @@ -494,6 +494,9 @@ data GroupInfo = GroupInfo useRelays' :: GroupInfo -> Bool useRelays' GroupInfo {useRelays} = isTrue useRelays +publicGroupEditor :: GroupInfo -> GroupMember -> Bool +publicGroupEditor gInfo mem = useRelays' gInfo && memberRole' mem >= GRModerator + groupId' :: GroupInfo -> GroupId groupId' GroupInfo {groupId} = groupId diff --git a/src/Simplex/Chat/View.hs b/src/Simplex/Chat/View.hs index 98eb811f5a..1211dc55a9 100644 --- a/src/Simplex/Chat/View.hs +++ b/src/Simplex/Chat/View.hs @@ -182,6 +182,8 @@ chatResponseToView hu cfg@ChatConfig {logLevel, showReactions, testView} liveIte CRPublicGroupCreated u g _groupLink _relays -> ttyUser u $ viewGroupCreated g testView CRPublicGroupCreationFailed u results -> ttyUser u $ viewPublicGroupCreationFailed results CRGroupRelays u g relays -> ttyUser u $ viewGroupRelays g relays + CRGroupRelaysAdded u g _groupLink relays -> ttyUser u $ viewGroupRelays g relays + CRGroupRelaysAddFailed u results -> ttyUser u $ viewGroupRelaysAddFailed results CRGroupMembers u g -> ttyUser u $ viewGroupMembers g CRMemberSupportChats u g ms -> ttyUser u $ viewMemberSupportChats g ms -- CRGroupConversationsArchived u _g _conversations -> ttyUser u [] @@ -1239,14 +1241,18 @@ viewGroupCreated g testView = where relaysInstruction = "wait for selected relay(s) to join, then you can invite members via group link" -viewPublicGroupCreationFailed :: [AddRelayResult] -> [StyledString] -viewPublicGroupCreationFailed results = - ["channel not created, results:"] - <> map showRelayResult results +viewRelayResults :: StyledString -> [AddRelayResult] -> [StyledString] +viewRelayResults header results = [header] <> map showRelayResult results where showRelayResult (AddRelayResult UserChatRelay {chatRelayId = DBEntityId i} err_) = " relay " <> sShow i <> ": " <> maybe "ok" (plain . tshow) err_ +viewPublicGroupCreationFailed :: [AddRelayResult] -> [StyledString] +viewPublicGroupCreationFailed = viewRelayResults "channel not created, results:" + +viewGroupRelaysAddFailed :: [AddRelayResult] -> [StyledString] +viewGroupRelaysAddFailed = viewRelayResults "relays not added, results:" + viewCannotResendInvitation :: GroupInfo -> ContactName -> [StyledString] viewCannotResendInvitation g c = [ ttyContact c <> " is already invited to group " <> ttyGroup' g, diff --git a/tests/APIDocs.hs b/tests/APIDocs.hs index aa9cc8459f..e0c242dd00 100644 --- a/tests/APIDocs.hs +++ b/tests/APIDocs.hs @@ -8,6 +8,7 @@ module APIDocs where import API.Docs.Commands import API.Docs.Events import API.Docs.Generate +import qualified API.Docs.Generate.Python as Py import qualified API.Docs.Generate.TypeScript as TS import API.Docs.Responses import API.Docs.Types @@ -42,6 +43,11 @@ apiDocsTest = do it "generate typescript responses code" $ testGenerate TS.responsesCodeFile TS.responsesCodeText it "generate typescript events code" $ testGenerate TS.eventsCodeFile TS.eventsCodeText it "generate typescript types code" $ testGenerate TS.typesCodeFile TS.typesCodeText + describe "Python" $ do + it "generate python commands code" $ testGenerate Py.commandsCodeFile Py.commandsCodeText + it "generate python responses code" $ testGenerate Py.responsesCodeFile Py.responsesCodeText + it "generate python events code" $ testGenerate Py.eventsCodeFile Py.eventsCodeText + it "generate python types code" $ testGenerate Py.typesCodeFile Py.typesCodeText documentedCmds :: [String] documentedCmds = concatMap (map consName' . commands) chatCommandsDocs diff --git a/tests/Bots/DirectoryTests.hs b/tests/Bots/DirectoryTests.hs index 68acec0493..7fdd34061f 100644 --- a/tests/Bots/DirectoryTests.hs +++ b/tests/Bots/DirectoryTests.hs @@ -7,7 +7,6 @@ module Bots.DirectoryTests where import ChatClient -import ChatTests.ChatRelays (withRelay) import ChatTests.DBUtils import ChatTests.Groups (memberJoinChannel, prepareChannel1Relay) import ChatTests.Utils diff --git a/tests/ChatTests/ChatRelays.hs b/tests/ChatTests/ChatRelays.hs index 58fe1074ef..4b09347dcf 100644 --- a/tests/ChatTests/ChatRelays.hs +++ b/tests/ChatTests/ChatRelays.hs @@ -325,9 +325,6 @@ testShareChannelChannel ps = getTermLine2 :: TestCC -> IO (String, String) getTermLine2 c = (,) <$> getTermLine c <*> getTermLine c -withRelay :: HasCallStack => TestParams -> (TestCC -> IO ()) -> IO () -withRelay ps = withNewTestChatOpts ps relayTestOpts "relay" relayProfile - -- Create a public group with relay=1, wait for relay to join createChannelWithRelay :: HasCallStack => String -> TestCC -> TestCC -> IO () createChannelWithRelay gName owner relay = do diff --git a/tests/ChatTests/Groups.hs b/tests/ChatTests/Groups.hs index 21bf6cb65a..6ceb3c2cbe 100644 --- a/tests/ChatTests/Groups.hs +++ b/tests/ChatTests/Groups.hs @@ -269,9 +269,13 @@ chatGroupTests = do it "subscriber should update profile in channel (signed)" testChannelSubscriberProfileUpdate it "should report relay results when one relay deleted its address" testChannelCreateDeletedRelay it "should deliver support scope messages via relay" testChannelSupportScope + it "should add relay to existing channel" testChannelAddRelay + it "should remove relay from channel" testChannelRemoveRelay + it "should remove left relay from channel" testChannelRemoveLeftRelay describe "channel message operations" $ do it "should update channel message" testChannelMessageUpdate it "should delete channel message" testChannelMessageDelete + it "should delete channel message from history" testChannelMessageDeleteFromHistory it "should send and receive channel message file" testChannelMessageFile it "should cancel channel message file" testChannelMessageFileCancel it "should quote channel message" testChannelMessageQuote @@ -8454,7 +8458,7 @@ testSupportPreferenceGroup = testSupportPreferenceChannel :: HasCallStack => TestParams -> IO () testSupportPreferenceChannel ps = withNewTestChat ps "alice" aliceProfile $ \alice -> - withNewTestChatOpts ps relayTestOpts "relay" relayProfile $ \relay -> + withNewTestChatOpts ps relayTestOpts "relay" chatRelayProfile $ \relay -> withNewTestChat ps "bob" bobProfile $ \bob -> withNewTestChat ps "cath" cathProfile $ \cath -> do (shortLink, fullLink) <- prepareChannel1Relay "team" alice relay @@ -9686,6 +9690,237 @@ testChannelSubscriberProfileUpdate ps = dan `hasContactProfiles` ["alice", "bob", "kate", "dave"] eve `hasContactProfiles` ["alice", "bob", "kate", "dave", "eve"] +testChannelAddRelay :: HasCallStack => TestParams -> IO () +testChannelAddRelay ps = + withNewTestChat ps "alice" aliceProfile $ \alice -> + withNewTestChatOpts ps relayTestOpts "bob" bobProfile $ \bob -> + withNewTestChatOpts ps relayTestOpts "cath" cathProfile $ \cath -> + withNewTestChat ps "dan" danProfile $ \dan -> + withNewTestChat ps "eve" eveProfile $ \eve -> do + -- create channel with 1 relay (bob) + (shortLink, fullLink) <- prepareChannel1Relay "team" alice bob + + -- subscriber joins through bob (the only relay at this point) + memberJoinChannel "team" [bob] [alice] shortLink fullLink dan + + -- configure cath as a second relay + cath ##> "/ad" + (cathSLink, _cLink) <- getContactLinks cath True + alice ##> ("/relays name=cath " <> cathSLink) + alice <## "ok" + + -- can't add same relay twice + alice ##> "/_add relays #1 1" + alice <## "bad chat command: some relays are already in the group" + + -- add cath relay to existing channel + alice ##> "/_add relays #1 2" + alice <## "#team: group relays:" + alice <## " - relay id 1: active" + alice <## " - relay id 2: invited" + + -- wait for cath to join as relay (async) + concurrentlyN_ + [ do + alice <## "#team: group link relays updated, current relays:" + alice + <### [ " - relay id 1: active", + " - relay id 2: active" + ] + alice <## "group link:" + void $ getTermLine alice, + cath <## "#team: you joined the group as relay" + ] + + threadDelay 100000 + + -- existing subscriber discovers and connects to new relay + concurrentlyN_ + [ do + dan <## "#team: joining the group (connecting to relay cath)..." + dan <## "#team: you joined the group (connected to relay cath)", + do + cath <## "dan (Daniel): accepting request to join group #team..." + cath <## "#team: dan joined the group" + ] + + threadDelay 100000 + + -- new subscriber joins through both relays + memberJoinChannel "team" [bob, cath] [alice] shortLink fullLink eve + + -- verify delivery through both relays + alice #> "#team hello" + [bob, cath] *<# "#team> hello" + [dan, eve] *<# "#team> hello [>>]" + +testChannelRemoveRelay :: HasCallStack => TestParams -> IO () +testChannelRemoveRelay ps = + withNewTestChat ps "alice" aliceProfile $ \alice -> + withNewTestChatOpts ps relayTestOpts "bob" bobProfile $ \bob -> + withNewTestChatOpts ps relayTestOpts "cath" cathProfile $ \cath -> + withNewTestChat ps "dan" danProfile $ \dan -> do + (shortLink, fullLink) <- prepareChannel2Relays "team" alice bob cath + memberJoinChannel "team" [bob, cath] [alice] shortLink fullLink dan + + -- verify delivery works + alice #> "#team hello" + [bob, cath] *<# "#team> hello" + dan <# "#team> hello [>>]" + + -- remove relay bob + threadDelay 100000 + alice ##> "/rm #team bob" + alice <## "#team: you removed bob from the group (signed)" + concurrentlyN_ + [ do + bob <## "#team: alice removed you from the group (signed)" + bob <## "use /d #team to delete the group", + -- cath doesn't have bob in member list (relays aren't introduced to each other), + -- so x.grp.mem.del arrives with unknown member ID — cath still forwards it (Left branch in xGrpMemDel) + cath <## "error: x.grp.mem.del with unknown member ID", + dan <## "#team: alice removed bob from the group (signed)" + ] + + -- verify delivery still works via remaining relay (cath) + threadDelay 100000 + alice #> "#team still working" + cath <# "#team> still working" + dan <# "#team> still working [>>]" + + -- remove last relay cath + threadDelay 100000 + alice ##> "/rm #team cath" + alice <## "#team: you removed cath from the group (signed)" + concurrentlyN_ + [ do + cath <## "#team: alice removed you from the group (signed)" + cath <## "use /d #team to delete the group", + dan <## "#team: alice removed cath from the group (signed)" + ] + + -- verify delivery stops — no relays to forward + threadDelay 100000 + alice #> "#team no relays" + (dan + DB.query_ db "SELECT local_display_name FROM group_members" :: IO [Only T.Text] + aliceMembers `shouldMatchList` [Only "alice", Only "dan"] + danMembers <- withCCTransaction dan $ \db -> + DB.query_ db "SELECT local_display_name FROM group_members" :: IO [Only T.Text] + danMembers `shouldMatchList` [Only "dan", Only "alice"] + + -- re-add bob as relay + alice ##> "/_add relays #1 1" + alice <## "#team: group relays:" + alice .<##. (" - relay id", ": invited") + + -- wait for bob to rejoin as relay (bob gets LDN "team_1" since old group record exists) + concurrentlyN_ + [ do + alice <## "#team: group link relays updated, current relays:" + alice .<##. (" - relay id", ": active") + alice <## "group link:" + void $ getTermLine alice, + bob <## "#team_1: you joined the group as relay" + ] + + threadDelay 100000 + + -- subscriber discovers and connects to new relay + dan ##> "/_get group link data #1" + dan <## "group ID: 1" + void $ getTermLine dan -- subscribers: N + concurrentlyN_ + [ do + dan <## "#team: joining the group (connecting to relay bob)..." + dan <## "#team: you joined the group (connected to relay bob)", + do + bob <## "dan_1 (Daniel): accepting request to join group #team_1..." + bob <## "#team_1: dan_1 joined the group" + ] + + threadDelay 100000 + + -- verify delivery works again through re-added relay + alice #> "#team relays restored" + bob <# "#team_1> relays restored" + dan <# "#team> relays restored [>>]" + +testChannelRemoveLeftRelay :: HasCallStack => TestParams -> IO () +testChannelRemoveLeftRelay ps = + withNewTestChat ps "alice" aliceProfile $ \alice -> + withNewTestChatOpts ps relayTestOpts "bob" bobProfile $ \bob -> + withNewTestChatOpts ps relayTestOpts "cath" cathProfile $ \cath -> + withNewTestChat ps "dan" danProfile $ \dan -> do + (shortLink, fullLink) <- prepareChannel2Relays "team" alice bob cath + memberJoinChannel "team" [bob, cath] [alice] shortLink fullLink dan + + -- verify delivery works + alice #> "#team hello" + [bob, cath] *<# "#team> hello" + dan <# "#team> hello [>>]" + + -- bob leaves + threadDelay 100000 + bob ##> "/l team" + concurrentlyN_ + [ do + bob <## "#team: you left the group" + bob <## "use /d #team to delete the group", + alice <## "#team: bob left the group (signed)", + dan <## "#team: bob left the group (signed)" + ] + + -- alice removes left bob + threadDelay 100000 + alice ##> "/rm #team bob" + alice <## "#team: you removed bob from the group (signed)" + concurrentlyN_ + [ cath <## "error: x.grp.mem.del with unknown member ID", + dan <## "#team: alice removed bob from the group (signed)" + ] + + -- bob's member record should be deleted on alice's and dan's sides + threadDelay 100000 + aliceMembers <- withCCTransaction alice $ \db -> + DB.query_ db "SELECT local_display_name FROM group_members" :: IO [Only T.Text] + aliceMembers `shouldMatchList` [Only "alice", Only "cath", Only "dan"] + danMembers <- withCCTransaction dan $ \db -> + DB.query_ db "SELECT local_display_name FROM group_members" :: IO [Only T.Text] + danMembers `shouldMatchList` [Only "dan", Only "alice", Only "cath"] + + -- cath leaves + threadDelay 100000 + cath ##> "/l team" + concurrentlyN_ + [ do + cath <## "#team: you left the group" + cath <## "use /d #team to delete the group", + alice <## "#team: cath left the group (signed)", + dan <## "#team: cath left the group (signed)" + ] + + -- alice removes left cath - dan doesn't receive (no relay to forward) + threadDelay 100000 + alice ##> "/rm #team cath" + alice <## "#team: you removed cath from the group (signed)" + + -- dan syncs with link - should clean up cath's stale record + threadDelay 100000 + dan ##> "/_get group link data #1" + dan <## "group ID: 1" + void $ getTermLine dan -- subscribers: N + + -- cath's member record should be cleaned up on dan's side after sync + threadDelay 100000 + danMembers2 <- withCCTransaction dan $ \db -> + DB.query_ db "SELECT local_display_name FROM group_members" :: IO [Only T.Text] + danMembers2 `shouldMatchList` [Only "dan", Only "alice"] + testChannelCreateDeletedRelay :: HasCallStack => TestParams -> IO () testChannelCreateDeletedRelay ps = withNewTestChat ps "alice" aliceProfile $ \alice -> do @@ -9716,7 +9951,7 @@ testChannelCreateDeletedRelay ps = testChannelSupportScope :: HasCallStack => TestParams -> IO () testChannelSupportScope ps = withNewTestChat ps "alice" aliceProfile $ \alice -> - withNewTestChatOpts ps relayTestOpts "relay" relayProfile $ \relay -> + withNewTestChatOpts ps relayTestOpts "relay" chatRelayProfile $ \relay -> withNewTestChat ps "cath" cathProfile $ \cath -> withNewTestChat ps "dan" danProfile $ \dan -> do (shortLink, fullLink) <- prepareChannel1Relay "team" alice relay @@ -9793,6 +10028,37 @@ testChannelMessageDelete ps = bob <# "#team> [marked deleted] hello" [cath, dan, eve] *<# "#team> [marked deleted] hello" -- TODO show as forwarded +testChannelMessageDeleteFromHistory :: HasCallStack => TestParams -> IO () +testChannelMessageDeleteFromHistory ps = + testChat4 aliceProfile bobProfile cathProfile danProfile test ps + where + test alice bob cath dan = withRelay ps $ \relay -> do + (shortLink, fullLink) <- prepareChannel1Relay "team" alice relay + memberJoinChannel "team" [relay] [alice] shortLink fullLink bob + memberJoinChannel "team" [relay] [alice] shortLink fullLink cath + + alice #> "#team hello" + relay <# "#team> hello" + [bob, cath] *<# "#team> hello [>>]" + + -- owner deletes from history (relay processes locally but doesn't forward) + msgId <- lastItemId alice + alice #$> ("/_delete item #1 " <> msgId <> " history", id, "message marked deleted") + relay <# "#team> [marked deleted] hello" + + -- subscribers don't receive deletion - next message arrives cleanly + alice #> "#team still here" + relay <# "#team> still here" + [bob, cath] *<# "#team> still here [>>]" + + -- internal delete rejected for channel owner + msgId2 <- lastItemId alice + alice ##> ("/_delete item #1 " <> msgId2 <> " internal") + alice <## "cannot delete this item" + + memberJoinChannel "team" [relay] [alice] shortLink fullLink dan + dan <# "#team> still here [>>]" + testChannelMessageFile :: HasCallStack => TestParams -> IO () testChannelMessageFile ps = withNewTestChat ps "alice" aliceProfile $ \alice -> diff --git a/tests/ChatTests/Utils.hs b/tests/ChatTests/Utils.hs index d42e833c39..4b28229348 100644 --- a/tests/ChatTests/Utils.hs +++ b/tests/ChatTests/Utils.hs @@ -81,8 +81,8 @@ frankProfile = mkProfile "frank" "Frank" Nothing businessProfile :: Profile businessProfile = mkProfile "biz" "Biz Inc" Nothing -relayProfile :: Profile -relayProfile = mkProfile "relay" "Relay" Nothing +chatRelayProfile :: Profile +chatRelayProfile = mkProfile "relay" "Relay" Nothing mkProfile :: T.Text -> T.Text -> Maybe ImageData -> Profile mkProfile displayName descr image = Profile {displayName, fullName = "", shortDescr = Just descr, image, contactLink = Nothing, peerType = Nothing, preferences = defaultPrefs} @@ -149,6 +149,9 @@ runTestCfg3 aliceCfg bobCfg cathCfg runTest ps = withNewTestChatCfg ps cathCfg "cath" cathProfile $ \cath -> runTest alice bob cath +withRelay :: HasCallStack => TestParams -> (TestCC -> IO ()) -> IO () +withRelay ps = withNewTestChatOpts ps relayTestOpts "relay" chatRelayProfile + -- | test sending direct messages (<##>) :: HasCallStack => TestCC -> TestCC -> IO () cc1 <##> cc2 = do diff --git a/tests/MarkdownTests.hs b/tests/MarkdownTests.hs index 3683b536dd..54483babec 100644 --- a/tests/MarkdownTests.hs +++ b/tests/MarkdownTests.hs @@ -416,6 +416,10 @@ testSanitizeUri = describe "sanitizeUri" $ do "https://example.com/page/a123?source=abc" `safeSanitized` Nothing -- source is in unsafe blacklist "https://example.com/page/a123?name=abc" `eagerSanitized` Just "https://example.com/page/a123" "https://example.com/page/a123?name=abc" `safeSanitized` Nothing -- name is not in a whitelist + it "should keep whitelisted parameters in safe mode even if they match a blacklist prefix" $ do + "https://example.com/playlist?list=abc" `sanitized` Nothing -- "list" is whitelisted, "li" is blacklisted + "https://example.com/playlist?list=abc&si=def" `sanitized` Just "https://example.com/playlist?list=abc" + "https://github.com/owner/repo?ref=main" `sanitized` Nothing -- "ref" is whitelisted for github.com where s `eagerSanitized` res = sanitized_ False s res s `safeSanitized` res = sanitized_ True s res diff --git a/tests/ProtocolTests.hs b/tests/ProtocolTests.hs index 01399f6bbb..aef41e90d2 100644 --- a/tests/ProtocolTests.hs +++ b/tests/ProtocolTests.hs @@ -191,7 +191,7 @@ decodeChatMessageTest = describe "Chat message encoding/decoding" $ do #==# XMsgUpdate (SharedMsgId "\1\2\3\4") (MCText "hello") [] Nothing Nothing Nothing Nothing it "x.msg.del" $ "{\"v\":\"1\",\"event\":\"x.msg.del\",\"params\":{\"msgId\":\"AQIDBA==\"}}" - #==# XMsgDel (SharedMsgId "\1\2\3\4") Nothing Nothing + #==# XMsgDel (SharedMsgId "\1\2\3\4") Nothing Nothing False it "x.msg.deleted" $ "{\"v\":\"1\",\"event\":\"x.msg.deleted\",\"params\":{}}" #==# XMsgDeleted diff --git a/website/.eleventy.js b/website/.eleventy.js index cdbdc0520f..b02cc49e78 100644 --- a/website/.eleventy.js +++ b/website/.eleventy.js @@ -54,7 +54,7 @@ const globalConfig = { } const translationsDirectoryPath = './langs' -const supportedRoutes = ["blog", "contact", "invitation", "messaging", "docs", "fdroid", "why", ""] +const supportedRoutes = ["blog", "contact", "invitation", "messaging", "docs", "fdroid", "why", "file", ""] let supportedLangs = [] fs.readdir(translationsDirectoryPath, (err, files) => { if (err) { diff --git a/website/langs/ar.json b/website/langs/ar.json index 0124dfebb4..264bf9561a 100644 --- a/website/langs/ar.json +++ b/website/langs/ar.json @@ -260,7 +260,7 @@ "about-and-contact-us": "عن واتصل بنا", "index-hero-h1": "كن
حراً", "index-hero-h2": "في شبكتك", - "index-hero-p1": "مراسلة خاصة وآمنة.
الشبكة الأولى التي تمتلك فيها جهات اتصالك ومجموعاتك.", + "index-hero-p1": "أول شبكة بدون معرّفات مستخدمين.
أنت تملك جهات اتصالك ومجموعاتك وقنواتك.", "index-hero-download-desktop-btn-title": "نزّل تطبيق سطح مكتب SimpleX", "index-testflight-title": "إصدار SimpleX التجريبي ل iOS على TestFlight", "index-f-droid-title": "تطبيق SimpleX من خلال F-Droid", @@ -273,28 +273,28 @@ "index-publications-heise-title": "منشورات Heise Online", "index-publications-kuketz-title": "مراجعة بواسطة Mike Kuketz", "index-publications-optout-title": "مقابلة بودكاست OptOut", - "worlds-most-secure-messaging": "المُراسلة الأكثر أمانًا في العالم", - "index-messaging-p1": "تتميز رسائل SimpleX بتعمية متطورة بين الطرفين.", - "index-messaging-p2": "لأمانك وخصوصيتك، لا يمكن للخوادم رؤية رسائلك ومَن تتحدث إليه.", + "worlds-most-secure-messaging": "لا أحد يمكنه معرفة من تتحدث إليه", + "index-messaging-p1": "حتى الخوادم لا تستطيع ذلك – جميع الرسائل تبدو كضوضاء عشوائية.", + "index-messaging-p2": "عشرات الملايين من الرسائل تُسلَّم بخصوصية كل يوم.", "index-messaging-cta": "تعرّف على المزيد حول مراسلة SimpleX", - "index-nextweb-h2": "أنت تملك
الشبكة التالية", - "index-nextweb-p1": "تأسست SimpleX على الاعتقاد بأن عليك امتلاك هويتك وجهات اتصالك ومجتمعاتك.", - "index-nextweb-p2": "شبكة مفتوحة ولامركزية تتيح لك التواصل مع الأشخاص ومشاركة الأفكار: كن حرًا وآمنًا.", - "index-token-h2": "مجتمعات تدوم", - "index-token-p1": "ستدعم مجموعاتك المفضلة بقسائم المجتمع المستقبلية.", - "index-token-p2": "ستدفع القسائم ثمن الخوادم، لتمكين مجتمعاتك من البقاء حرة ومستقلة.", + "index-nextweb-h2": "أنت تملك
الشبكة", + "index-nextweb-p1": "كل جهة اتصال ومجموعة موجودة على جهازك، وليس في قاعدة بيانات خادم.", + "index-nextweb-p2": "لا يتحكم أي كيان واحد في الشبكة – يمكن لأي شخص تشغيل الخوادم.", + "index-token-h2": "يموّله مستخدموه", + "index-token-p1": "للحفاظ على الاستقلالية، ستدفع القنوات والمجتمعات الكبيرة مقابل خوادمها.", + "index-token-p2": "سيغطي ذلك البنية التحتية وتطوير البرمجيات وإدارة الشبكة.", "index-roadmap-h2": "خارطة طريق SimpleX للإنترنت المجاني", "index-roadmap-now": "الآن", "index-roadmap-1": "2026", "index-roadmap-1-title": "التوسع إلى مجتمعات كبيرة", "index-roadmap-1-desc": "الهروب من المنصات المركزية", "index-roadmap-2-title": "مجتمعات وخوادم مستدامة", - "index-roadmap-2-desc": "إطلاق قسائم المجتمع", + "index-roadmap-2-desc": "إطلاق أرصدة المجتمع", "index-roadmap-3-title": "اجعل مجتمعاتك تنمو", "index-roadmap-3-desc": "أدوات لتعزيز مجتمعاتك", "index-directory-h2": "انضم إلى مجتمعات SimpleX", - "index-directory-p1": "مئات الآلاف من الأشخاص يثقون بالفعل في مُراسلة SimpleX.", - "index-directory-p2": "ابحث عن مجتمعاتك في دليل SimpleX وأنشئ مجتمعك الخاص!", + "index-directory-p1": "أكثر من 2 مليون شخص قاموا بتنزيل تطبيقات SimpleX.", + "index-directory-p2": "ابحث عن قنواتك ومجتمعاتك في الدليل وأنشئ قنواتك الخاصة!", "index-directory-cta": "اعرض دليل SimpleX", "index-directory-users-group-title": "مجموعة مستخدمي SimpleX", "how-secure-comparison-title": "مقارنة أمان التعمية بين الطرفين في برامج المُراسلة المختلفة", @@ -313,7 +313,7 @@ "index-roadmap-2": "يونيو 2027", "index-roadmap-3": "ديسمبر 2027", "navbar-token": "رمز", - "index-token-cta": "تعرف على المزيد واحصل على NFT مجاني
للاختبار المبكر.", + "index-token-cta": "اعرف المزيد عن أرصدة المجتمع", "navbar-old-site": "الموقع القديم", "send-file": "إرسال ملف" } diff --git a/website/langs/cs.json b/website/langs/cs.json index 41b026fb58..0805173d37 100644 --- a/website/langs/cs.json +++ b/website/langs/cs.json @@ -261,7 +261,7 @@ "navbar-token": "Token", "index-hero-h1": "
Žijte
svobodně", "index-hero-h2": "Ve Své Síti", - "index-hero-p1": "Soukromé a bezpečné zasílání zpráv.
První síť, kde vaše kontakty a skupiny patří vám.", + "index-hero-p1": "První síť bez uživatelských ID.
Vaše kontakty, skupiny a kanály patří vám.", "index-hero-download-desktop-btn-title": "Stáhněte si desktopovou aplikaci SimpleX", "index-testflight-title": "Beta verze SimpleX pro iOS na TestFlight", "index-f-droid-title": "Stáhnout aplikaci SimpleX přes F-Droid", @@ -274,17 +274,17 @@ "index-publications-heise-title": "Publikace Heise Online", "index-publications-kuketz-title": "Rezence od Mike Kuketz", "index-publications-optout-title": "Rozhovor v podcastu OptOut", - "worlds-most-secure-messaging": "Nejbezpečnější komunikační platforma na světě", - "index-messaging-p1": "Zprávy v SimpleX jsou chráněny nejpokročilejším koncovým šifrováním (end-to-end).", - "index-messaging-p2": "Pro vaše soukromí servery nevidí vaše zprávy ani to, s kým si píšete.", + "worlds-most-secure-messaging": "Nikdo nevidí, s kým komunikujete", + "index-messaging-p1": "Ani servery – všechny zprávy vypadají jako náhodný šum.", + "index-messaging-p2": "Každý den je soukromě doručeno desítky milionů zpráv.", "index-messaging-cta": "Zjistit více o zprávách v SimpleX", - "index-nextweb-h2": "Váš internet
budoucnosti", - "index-nextweb-p1": "SimpleX je postaven na myšlence, že vy musíte vlastnit váš profil, kontakty a komunity.", - "index-nextweb-p2": "Nikým nevlastněná decentralizovaná síť, vám umožní spojit se s lidmi a sdílet nápady, být svobodný a bezpečný ve své síti.", - "index-token-h2": "Stabilní komunity", - "index-token-p1": "Své oblíbené skupiny budete moci podpořit pomocí připravovaných Skupinových Voucherů.", - "index-token-p2": "Vouchery budou sloužit k úhradě provozu serverů, aby skupiny zůstaly svobodné a nezávislé.", - "index-token-cta": "Zjistěte více a získejte bezplatnou vstupenku pro rané testování.", + "index-nextweb-h2": "Síť je
vaše", + "index-nextweb-p1": "Každý kontakt a skupina jsou na vašem zařízení, ne v databázi serveru.", + "index-nextweb-p2": "Síť nekontroluje žádný subjekt – servery může provozovat kdokoli.", + "index-token-h2": "Financováno uživateli", + "index-token-p1": "Pro zachování nezávislosti budou velké kanály a komunity platit za své servery.", + "index-token-p2": "To pokryje infrastrukturu, vývoj softwaru a správu sítě.", + "index-token-cta": "Zjistěte více o Community Credits", "index-roadmap-h2": "Plán SimpleX ke svobodnému internetu", "index-roadmap-now": "Nyní", "index-roadmap-1": "2026", @@ -292,13 +292,13 @@ "index-roadmap-1-desc": "Odchod od centralizovaných platforem", "index-roadmap-2": "Červen 2027", "index-roadmap-2-title": "Udržitelné komunity a servery", - "index-roadmap-2-desc": "Spuštění Skupinových Voucherů", + "index-roadmap-2-desc": "Spuštění Community Credits", "index-roadmap-3": "Prosinec 2027", "index-roadmap-3-title": "Pomozte své komunitě růst", "index-roadmap-3-desc": "Nástroje na podporu vašich komunit", "index-directory-h2": "Zapojte se do komunit SimpleX", - "index-directory-p1": "Statisíce lidí už důvěřují komunikaci přes SimpleX.", - "index-directory-p2": "Najděte své komunity v katalogu SimpleX a vytvořte si vlastní!", + "index-directory-p1": "Více než 2 miliony lidí si stáhly aplikace SimpleX.", + "index-directory-p2": "Najděte své kanály a komunity v katalogu a vytvořte si vlastní!", "index-directory-cta": "Zobrazit katalog SimpleX", "index-directory-users-group-title": "Uživatelské skupiny SimpleX", "how-secure-comparison-title": "Porovnání bezpečnosti koncového šifrování (end-to-end) v různých messengerech", diff --git a/website/langs/de.json b/website/langs/de.json index c35cd8fba6..7c52be6498 100644 --- a/website/langs/de.json +++ b/website/langs/de.json @@ -260,7 +260,7 @@ "directory": "Verzeichnis", "index-hero-h1": "Sei
frei", "index-hero-h2": "In Deinem Netzwerk", - "index-hero-p1": "Privater und sicherer Nachrichtenaustausch.
Das erste Netzwerk, in dem Ihnen
Ihre Kontakte und Gruppen gehören.", + "index-hero-p1": "Das erste Netzwerk ohne Benutzer-IDs.
Ihre Kontakte, Gruppen und Kanäle gehören Ihnen.", "index-hero-download-desktop-btn-title": "Download der SimpleX Desktop-App", "index-testflight-title": "Öffentlicher iOS-Preview auf TestFlight", "index-f-droid-title": "SimpleX-App über das F-Droid-Repository", @@ -273,17 +273,17 @@ "index-publications-heise-title": "Veröffentlichungen von Heise Online", "index-publications-kuketz-title": "Überprüfung von Mike Kuketz", "index-publications-optout-title": "Podcast-Interview von OptOut", - "worlds-most-secure-messaging": "Das sicherste Messaging-System der Welt", - "index-messaging-p1": "SimpleX-Messaging verfügt über modernste Ende-zu-Ende-Verschlüsselung.", - "index-messaging-p2": "Zu Ihrer Sicherheit und zum Schutz Ihrer Privatsphäre können Server weder Ihre Nachrichten sehen, noch mit wem Sie kommunizieren.", + "worlds-most-secure-messaging": "Niemand kann sehen, mit wem Sie kommunizieren", + "index-messaging-p1": "Nicht einmal Server – alle Nachrichten sehen aus wie zufälliges Rauschen.", + "index-messaging-p2": "Täglich werden Dutzende Millionen Nachrichten privat zugestellt.", "index-messaging-cta": "Lernen Sie mehr über SimpleX-Messaging", - "index-nextweb-h2": "Sie besitzen die
Zukunft des Webs", - "index-nextweb-p1": "SimpleX wurde aus der Überzeugung heraus entwickelt, dass Sie Eigentümer Ihrer Profile, Kontakte und Communitys bleiben müssen.", - "index-nextweb-p2": "Ein dezentrales Netzwerk, welches Niemanden gehört, ermöglicht es Ihnen, mit Menschen in Kontakt zu treten, Ideen auszutauschen und dabei frei und sicher in Ihrem Netzwerk zu sein.", - "index-token-h2": "Communitys, die Bestand haben", - "index-token-p1": "Sie werden Ihre Lieblingsgruppen in Zukunft mit Community-Gutscheinen unterstützen können.", - "index-token-p2": "Server werden mit Gutscheinen bezahlt, damit Ihre Communitys kostenlos und unabhängig bleiben können.", - "index-token-cta": "Erfahren Sie mehr und sichern Sie sich einen kostenlosen Zugangspass, um es frühzeitig auszuprobieren.", + "index-nextweb-h2": "Das Netzwerk
gehört Ihnen", + "index-nextweb-p1": "Jeder Kontakt und jede Gruppe liegt auf Ihrem Gerät, nicht in einer Server-Datenbank.", + "index-nextweb-p2": "Keine einzelne Instanz kontrolliert das Netzwerk – jeder kann Server betreiben.", + "index-token-h2": "Finanziert von seinen Nutzern", + "index-token-p1": "Um unabhängig zu bleiben, werden große Kanäle und Communitys für ihre Server bezahlen.", + "index-token-p2": "Dies deckt Infrastruktur, Softwareentwicklung und Netzwerkverwaltung ab.", + "index-token-cta": "Erfahren Sie mehr über Community-Credits", "index-roadmap-h2": "SimpleX - Der Weg zum freien Internet", "index-roadmap-now": "Jetzt", "index-roadmap-1": "2026", @@ -291,13 +291,13 @@ "index-roadmap-1-desc": "Ausstieg aus zentralisierten Plattformen", "index-roadmap-2": "Juni 2027", "index-roadmap-2-title": "Nachhaltige Communitys & Server", - "index-roadmap-2-desc": "Einführung von Community-Gutscheinen", + "index-roadmap-2-desc": "Einführung von Community-Credits", "index-roadmap-3": "Dez 2027", "index-roadmap-3-title": "Lassen Sie Ihre Communitys wachsen", "index-roadmap-3-desc": "Tools zur Förderung Ihrer Communitys", "index-directory-h2": "Treten Sie SimpleX-Communitys bei", - "index-directory-p1": "Hunderttausende Nutzer vertrauen bereits dem Messaging per SimpleX.", - "index-directory-p2": "Finden Sie Communitys im SimpleX-Verzeichnis oder erstellen Sie Ihre Eigenen!", + "index-directory-p1": "Mehr als 2 Millionen Menschen haben SimpleX-Apps heruntergeladen.", + "index-directory-p2": "Finden Sie Kanäle und Communitys im Verzeichnis oder erstellen Sie Ihre Eigenen!", "index-directory-cta": "SimpleX-Verzeichnis anzeigen", "index-directory-users-group-title": "SimpleX-Nutzergruppe", "how-secure-comparison-title": "Sicherheitsvergleich der Ende-zu-Ende-Verschlüsselung in verschiedenen Messengern", diff --git a/website/langs/en.json b/website/langs/en.json index 25e5f128ed..490e693f18 100644 --- a/website/langs/en.json +++ b/website/langs/en.json @@ -262,7 +262,7 @@ "please-use-link-in-mobile-app": "Please use the link in the mobile app", "index-hero-h1": "Be
Free", "index-hero-h2": "In Your Network", - "index-hero-p1": "Private and secure messaging.
The first network where you own
your contacts and groups.", + "index-hero-p1": "The first network without user IDs.
You own your contacts, groups and channels.", "index-hero-download-desktop-btn-title": "Download SimpleX Desktop App", "index-testflight-title": "SimpleX iOS beta-release on TestFlight", "index-f-droid-title": "SimpleX app via F-Droid", @@ -275,17 +275,17 @@ "index-publications-heise-title": "Heise Online publications", "index-publications-kuketz-title": "Review by Mike Kuketz", "index-publications-optout-title": "OptOut podcast interview", - "worlds-most-secure-messaging": "World's Most Secure Messaging", - "index-messaging-p1": "SimpleX messaging has cutting-edge end-to-end encryption.", - "index-messaging-p2": "For your security and privacy, servers can't see your messages and who you talk to.", + "worlds-most-secure-messaging": "Nobody Can See Who You Talk To", + "index-messaging-p1": "Not even servers – all messages look like random noise.", + "index-messaging-p2": "Tens of millions of messages delivered privately every day.", "index-messaging-cta": "Learn more about SimpleX messaging", - "index-nextweb-h2": "You Own
The Next Web", - "index-nextweb-p1": "SimpleX is created on the belief that you must own your profiles, contacts and communities.", - "index-nextweb-p2": "A decentralized network nobody owns lets you connect with people and share ideas, to be free and secure in your network.", - "index-token-h2": "Communities That Last", - "index-token-p1": "You will support your favorite groups with future Community Vouchers.", - "index-token-p2": "Vouchers will pay for servers, to let your communities stay free and independent.", - "index-token-cta": "Learn more and get a free access pass for early testing.", + "index-nextweb-h2": "You Own
The Network", + "index-nextweb-p1": "Every contact and group is on your device, not in a server's database.", + "index-nextweb-p2": "No single entity controls the network – anyone can run servers.", + "index-token-h2": "Funded by Its Users", + "index-token-p1": "To stay independent, large channels and communities will pay for their servers.", + "index-token-p2": "This will cover infrastructure, software development and network governance.", + "index-token-cta": "Learn more about Community Credits", "index-roadmap-h2": "SimpleX Roadmap to Free Internet", "index-roadmap-now": "Now", "index-roadmap-1": "2026", @@ -293,13 +293,13 @@ "index-roadmap-1-desc": "Escaping centralized platforms", "index-roadmap-2": "Jun 2027", "index-roadmap-2-title": "Sustainable Communities & Servers", - "index-roadmap-2-desc": "Launching Community Vouchers", + "index-roadmap-2-desc": "Launching Community Credits", "index-roadmap-3": "Dec 2027", "index-roadmap-3-title": "Make Your Communities Grow", "index-roadmap-3-desc": "Tools to promote your communities", "index-directory-h2": "Join SimpleX Communities", - "index-directory-p1": "Hundreds of thousands people already trust SimpleX messaging.", - "index-directory-p2": "Find your communities in SimpleX directory and create your own!", + "index-directory-p1": "More than 2 million people downloaded SimpleX apps.", + "index-directory-p2": "Find your channels and communities in directory and create your own!", "index-directory-cta": "View SimpleX Directory", "index-directory-users-group-title": "SimpleX users group", "how-secure-comparison-title": "Comparison of end-to-end encryption security in different messengers", diff --git a/website/langs/es.json b/website/langs/es.json index 1826885863..a294c683f6 100644 --- a/website/langs/es.json +++ b/website/langs/es.json @@ -260,7 +260,7 @@ "about-and-contact-us": "Quiénes somos & Contacto", "index-hero-h1": "Sé
Libre", "index-hero-h2": "En Tu Red", - "index-hero-p1": "Mensajería privada y segura.
La primera red donde
tus contactos y grupos te pertenecen.", + "index-hero-p1": "La primera red sin IDs de usuario.
Tus contactos, grupos y canales te pertenecen.", "index-hero-download-desktop-btn-title": "Descargar SimpleX Desktop App", "index-testflight-title": "Betas SimpleX para iOS en TestFlight", "index-f-droid-title": "SimpleX app vía F-Droid", @@ -273,17 +273,17 @@ "index-publications-heise-title": "Publicaciones Heise Online", "index-publications-kuketz-title": "Revisión por Mike Kuketz", "index-publications-optout-title": "Entrevista en podcast de OptOut", - "worlds-most-secure-messaging": "Mensajería Más Segura Del Mundo", - "index-messaging-p1": "La mensajería SimpleX posee un cifrado de extremo a extremo de vanguardia.", - "index-messaging-p2": "Para tu seguridad y privacidad los servidores no pueden ver tus mensajes ni con quién te comunicas.", + "worlds-most-secure-messaging": "Nadie Puede Ver Con Quién Hablas", + "index-messaging-p1": "Ni siquiera los servidores – todos los mensajes parecen ruido aleatorio.", + "index-messaging-p2": "Decenas de millones de mensajes entregados de forma privada cada día.", "index-messaging-cta": "Descubre más sobre la mensajería SimpleX", - "index-nextweb-h2": "La Web
Del Futuro
Te Pertenece", - "index-nextweb-p1": "SimpleX se origina en la creencia de que debes ser el propietario de tus perfiles, contactos y comunidades.", - "index-nextweb-p2": "Una red descentralizada que no pertenece a nadie, que te permite conectar con personas y compartir ideas de manera libre y segura en tu red.", - "index-token-h2": "Comunidades Duraderas", - "index-token-p1": "Podrás apoyar a tus grupos favoritos con los futuros Vales Comunitarios.", - "index-token-p2": "Los vales costearán los servidores para que tus comunidades sigan siendo libres e independientes.", - "index-token-cta": "Descubre más y obtén acceso gratuito para participar en las pruebas iniciales.", + "index-nextweb-h2": "La Red
Te Pertenece", + "index-nextweb-p1": "Cada contacto y grupo está en tu dispositivo, no en la base de datos de un servidor.", + "index-nextweb-p2": "Ninguna entidad controla la red – cualquiera puede ejecutar servidores.", + "index-token-h2": "Financiada por Sus Usuarios", + "index-token-p1": "Para mantenerse independientes, los canales y comunidades grandes pagarán por sus servidores.", + "index-token-p2": "Esto cubrirá infraestructura, desarrollo de software y gobernanza de la red.", + "index-token-cta": "Descubre más sobre los Créditos Comunitarios", "index-roadmap-h2": "Ruta SimpleX hacía el Internet Libre", "index-roadmap-now": "Ahora", "index-roadmap-1": "2026", @@ -291,13 +291,13 @@ "index-roadmap-1-desc": "Huir de plataformas centralizadas", "index-roadmap-2": "Jun 2027", "index-roadmap-2-title": "Comunidades y Servidores Sostenibles", - "index-roadmap-2-desc": "Lanzamiento de Vales Comunitarios", + "index-roadmap-2-desc": "Lanzamiento de Créditos Comunitarios", "index-roadmap-3": "Dic 2027", "index-roadmap-3-title": "Haz Crecer Tus Comunidades", "index-roadmap-3-desc": "Herramientas para la promoción", "index-directory-h2": "Únete a las Comunidades SimpleX", - "index-directory-p1": "Miles de personas confían ya en la mensajería SimpleX.", - "index-directory-p2": "¡Encuentra comunidades en el directorio SimpleX y crea la tuya!", + "index-directory-p1": "Más de 2 millones de personas descargaron las aplicaciones SimpleX.", + "index-directory-p2": "¡Encuentra tus canales y comunidades en el directorio y crea los tuyos!", "index-directory-cta": "Ir al Directorio SimpleX", "index-directory-users-group-title": "SimpleX users group", "how-secure-comparison-title": "Comparativa del cifrado de extremo a extremo en los distintos mensajeros", diff --git a/website/langs/fr.json b/website/langs/fr.json index 91ffa8e408..765e3feec8 100644 --- a/website/langs/fr.json +++ b/website/langs/fr.json @@ -262,7 +262,7 @@ "about-and-contact-us": "À propos & Contactez-nous", "index-hero-h1": "Soyez
Libre", "index-hero-h2": "Dans Votre Réseau", - "index-hero-p1": "Messagerie privée et sécurisée.
Le premier réseau où vous possédez vos contacts et vos groupes.", + "index-hero-p1": "Le premier réseau sans identifiants d'utilisateur.
Vos contacts, groupes et canaux vous appartiennent.", "index-hero-download-desktop-btn-title": "Téléchargez l’application de bureau SimpleX", "index-testflight-title": "Version bêta de SimpleX pour iOS sur TestFlight", "index-f-droid-title": "Application SimpleX via F-Droid", @@ -273,12 +273,13 @@ "index-publications-privacy-guides-title": "Messagerie recommandée par Privacy Guides", "index-publications-whonix-title": "Messagerie recommandée par Whonix", "index-publications-kuketz-title": "Analyse de Mike Kuketz", - "index-messaging-p1": "La messagerie SimpleX utilise un chiffrement de bout en bout à la pointe de la technologie.", + "worlds-most-secure-messaging": "Personne ne voit avec qui vous parlez", + "index-messaging-p1": "Même pas les serveurs – tous les messages ressemblent à du bruit aléatoire.", "index-messaging-cta": "En savoir plus sur la messagerie SimpleX", - "index-nextweb-h2": "Prenez le contrôle
du Web de demain", - "index-nextweb-p2": "Un réseau ouvert et décentralisé vous permet de communiquer avec les autres et de partager vos idées : soyez libre et en sécurité.", - "index-token-h2": "Des communautés qui durent", - "index-token-cta": "En savoir plus et obtenez votre NFT gratuit
pour les premiers tests.", + "index-nextweb-h2": "Le réseau
vous appartient", + "index-nextweb-p2": "Aucune entité ne contrôle le réseau – n'importe qui peut héberger des serveurs.", + "index-token-h2": "Financé par ses utilisateurs", + "index-token-cta": "En savoir plus sur les Crédits Communautaires", "index-roadmap-h2": "Feuille de route de SimpleX vers un Internet libre", "index-roadmap-now": "Maintenant", "index-roadmap-1": "2026", @@ -286,7 +287,7 @@ "index-roadmap-1-desc": "Quitter les plateformes centralisées", "index-roadmap-2": "Juin 2027", "index-roadmap-2-title": "Communautés et serveurs durables", - "index-roadmap-2-desc": "Lancement des bons communautaires", + "index-roadmap-2-desc": "Lancement des Crédits Communautaires", "index-roadmap-3": "Déc 2027", "index-roadmap-3-title": "Faites grandir vos communautés", "index-roadmap-3-desc": "Outils pour promouvoir vos communautés", diff --git a/website/langs/hu.json b/website/langs/hu.json index 1d0d51fc03..67133b8b80 100644 --- a/website/langs/hu.json +++ b/website/langs/hu.json @@ -259,7 +259,7 @@ "directory": "Csoportjegyzék", "about-and-contact-us": "Névjegy és kapcsolat", "index-hero-h1": "
Legyen
szabad
", - "index-hero-p1": "Privát és biztonságos üzenetküldés.
Az első olyan hálózat, ahol Ön a tulajdonosa saját partnereinek és csoportjainak.", + "index-hero-p1": "Az első hálózat felhasználói azonosítók nélkül.
Az Ön névjegyei, csoportjai és csatornái az Öné.", "index-hero-download-desktop-btn-title": "SimpleX számítógépes alkalmazásának letöltése", "index-security-assessment-title": "Biztonsági auditok", "index-security-review-2022-title": "Biztonsági audit 2022", @@ -273,17 +273,17 @@ "index-publications-whonix-title": "Whonix ajánlás", "index-publications-kuketz-title": "Áttekintette: Mike Kuketz", "index-publications-optout-title": "OptOut podcast interjú", - "worlds-most-secure-messaging": "A világ legbiztonságosabb üzenetváltó alkalmazása", - "index-messaging-p1": "Az üzenetváltás a SimpleXben élvonalbeli végpontok közötti titkosítással rendelkezik.", - "index-messaging-p2": "A biztonsága és magánszférájának védelme érdekében a kiszolgálók nem látják az üzeneteit, és azt sem, hogy kivel beszélget.", + "worlds-most-secure-messaging": "Senki sem láthatja, kivel beszélget", + "index-messaging-p1": "Még a kiszolgálók sem – az összes üzenet véletlenszerű zajnak tűnik.", + "index-messaging-p2": "Naponta több tízmillió üzenetet kézbesítünk bizalmasan.", "index-messaging-cta": "Tudjon meg többet a SimpleX üzenetváltó alkalmazásról", - "index-nextweb-h2": "Vegye birtokba
A jövő hálózatát", - "index-nextweb-p1": "A SimpleX abból a meggyőződésből jött létre, hogy a profilok, a kapcsolatok és a közösségek a felhasználók tulajdonát kell, hogy képezzék.", - "index-nextweb-p2": "Egy decentralizált hálózat, amelyet senki sem birtokol, lehetővé teszi a kapcsolatok létrehozását és az ötletek megosztását szabadon és biztonságosan a hálózaton.", - "index-token-h2": "Időtálló közösségek", - "index-token-p1": "A jövőben közösségi utalványokkal támogathatja a kedvenc csoportjait.", - "index-token-p2": "Az utalványokkal fizetni tudja a kiszolgálókat, hogy a közösségek szabadok és függetlenek maradhassanak.", - "index-token-cta": "Tudjon meg többet, és szerezzen ingyenes NFT-t az előzetes tesztelésért.", + "index-nextweb-h2": "A hálózat
az Öné", + "index-nextweb-p1": "Minden névjegy és csoport az Ön eszközén van, nem egy kiszolgáló adatbázisában.", + "index-nextweb-p2": "Egyetlen szervezet sem irányítja a hálózatot – bárki üzemeltethet kiszolgálókat.", + "index-token-h2": "A felhasználói finanszírozzák", + "index-token-p1": "A függetlenség megőrzéséhez a nagy csatornák és közösségek fizetni fognak a kiszolgálóikért.", + "index-token-p2": "Ez fedezi az infrastruktúrát, a szoftverfejlesztést és a hálózat irányítását.", + "index-token-cta": "Tudjon meg többet a Community Credits-ről", "index-roadmap-h2": "A SimpleX ütemterve a szabad internethez", "index-roadmap-now": "Most", "index-roadmap-1": "2026", @@ -291,13 +291,13 @@ "index-roadmap-1-desc": "Központosított platformok elhagyása", "index-roadmap-2": "2027. jún.", "index-roadmap-2-title": "Fenntartható közösségek és kiszolgálók", - "index-roadmap-2-desc": "Közösségi utalványok elindítása", + "index-roadmap-2-desc": "Community Credits elindítása", "index-roadmap-3": "2027. dec.", "index-roadmap-3-title": "Közösségek növelése", "index-roadmap-3-desc": "Eszközök biztosítása a közösségek népszerűsítéséhez", "index-directory-h2": "Csatlakozzon a SimpleX közösségekhez", - "index-directory-p1": "Már emberek százezrei bíznak a SimpleXen való üzenetváltásban.", - "index-directory-p2": "Találja meg a kedvenc közösségeit a SimpleX-csoportjegyzékben vagy hozza létre a saját csoportját!", + "index-directory-p1": "Több mint 2 millió ember töltötte le a SimpleX alkalmazásokat.", + "index-directory-p2": "Találja meg csatornáit és közösségeit a csoportjegyzékben, vagy hozza létre a sajátját!", "index-directory-cta": "SimpleX-csoportjegyzék megtekintése", "index-directory-users-group-title": "SimpleX felhasználók csoportja", "how-secure-comparison-title": "A végpontok közötti titkosítás összehasonlítása más üzenetváltó alkalmazásokkal", diff --git a/website/langs/id.json b/website/langs/id.json index 3768926f9d..c766929416 100644 --- a/website/langs/id.json +++ b/website/langs/id.json @@ -260,7 +260,7 @@ "get-simplex": "Dapatkan aplikasi desktop SimpleX", "index-hero-h1": "
Bebaslah
", "index-hero-h2": "Di Jaringan Anda", - "index-hero-p1": "Pesan yang privat dan aman.
Jaringan pertama tempat Anda memiliki kontak dan grup Anda.", + "index-hero-p1": "Jaringan pertama tanpa ID pengguna.
Anda pemilik kontak, grup, dan kanal Anda.", "index-hero-download-desktop-btn-title": "Unduh Aplikasi Desktop SimpleX", "index-testflight-title": "Pratinjau iOS publik di TestFlight", "index-f-droid-title": "Repositori SimpleX F-Droid", @@ -273,17 +273,17 @@ "index-publications-heise-title": "publikasi", "index-publications-kuketz-title": "tinjauan", "index-publications-optout-title": "wawancara podcast", - "worlds-most-secure-messaging": "Perpesanan Paling Aman di Dunia", - "index-messaging-p1": "Perpesanan SimpleX memiliki enkripsi end-to-end yang canggih.", - "index-messaging-p2": "Demi keamanan dan privasi Anda, server tidak dapat melihat pesan Anda atau dengan siapa Anda bicara.", + "worlds-most-secure-messaging": "Tidak Ada yang Bisa Melihat dengan Siapa Anda Bicara", + "index-messaging-p1": "Bahkan server pun tidak bisa – semua pesan terlihat seperti derau acak.", + "index-messaging-p2": "Puluhan juta pesan dikirim secara privat setiap hari.", "index-messaging-cta": "Pelajari lebih lanjut tentang perpesanan SimpleX", - "index-nextweb-h2": "Anda Pemilik
Web Berikutnya", - "index-nextweb-p1": "SimpleX didirikan atas keyakinan bahwa Anda harus memiliki identitas, kontak, dan komunitas Anda sendiri.", - "index-nextweb-p2": "Jaringan yang terbuka dan terdesentralisasi membuat Anda terhubung dengan orang lain dan berbagi ide: bebas dan aman.", - "index-token-h2": "Komunitas yang Bertahan Lama", - "index-token-p1": "Anda akan mendukung grup favorit Anda dengan voucher Komunitas di masa mendatang.", - "index-token-p2": "Voucher akan membayar server, agar komunitas Anda tetap bebas dan independen.", - "index-token-cta": "Pelajari lebih lanjut tentang Voucher Komunitas", + "index-nextweb-h2": "Anda Pemilik
Jaringannya", + "index-nextweb-p1": "Setiap kontak dan grup ada di perangkat Anda, bukan di basis data server.", + "index-nextweb-p2": "Tidak ada satu entitas pun yang mengendalikan jaringan – siapa saja bisa menjalankan server.", + "index-token-h2": "Didanai oleh Penggunanya", + "index-token-p1": "Untuk tetap independen, kanal dan komunitas besar akan membayar server mereka.", + "index-token-p2": "Ini akan mencakup infrastruktur, pengembangan perangkat lunak, dan tata kelola jaringan.", + "index-token-cta": "Pelajari lebih lanjut tentang Kredit Komunitas", "index-roadmap-h2": "SimpleX Roadmap Menuju Internet Bebas", "index-roadmap-now": "Sekarang", "index-roadmap-1": "2026", @@ -291,13 +291,13 @@ "index-roadmap-1-desc": "Menghindari platform terpusat", "index-roadmap-2": "Jun 2027", "index-roadmap-2-title": "Komunitas & Server Berkelanjutan", - "index-roadmap-2-desc": "Perilisan Voucher Komunitas", + "index-roadmap-2-desc": "Peluncuran Kredit Komunitas", "index-roadmap-3": "Des 2027", "index-roadmap-3-title": "Buat Komunitas Anda Meningkat", "index-roadmap-3-desc": "Alat untuk promosikan komunitas Anda", "index-directory-h2": "Gabung ke Komunitas SimpleX", - "index-directory-p1": "Ratusan ribu orang sudah memercayai perpesanan SimpleX.", - "index-directory-p2": "Temukan komunitas Anda di direktori SimpleX dan buat komunitas Anda sendiri!", + "index-directory-p1": "Lebih dari 2 juta orang telah mengunduh aplikasi SimpleX.", + "index-directory-p2": "Temukan kanal dan komunitas Anda di direktori dan buat milik Anda sendiri!", "index-directory-cta": "Lihat Direktori SimpleX", "index-directory-users-group-title": "Grup pengguna SimpleX", "how-secure-comparison-title": "Seberapa amankah enkripsi end-to-end di berbagai aplikasi perpesanan?", diff --git a/website/langs/it.json b/website/langs/it.json index 326afb82d4..a3ced52c8e 100644 --- a/website/langs/it.json +++ b/website/langs/it.json @@ -259,7 +259,7 @@ "about-and-contact-us": "Informazioni e contatti", "directory": "Directory", "index-hero-h2": "nella tua rete", - "index-hero-p1": "Messaggistica privata e sicura.
La prima rete in cui possiedi
i tuoi contatti e i tuoi gruppi.", + "index-hero-p1": "La prima rete senza ID utente.
I tuoi contatti, gruppi e canali sono tuoi.", "index-hero-download-desktop-btn-title": "Scarica l'app desktop di SimpleX", "index-testflight-title": "Anteprima pubblica per iOS su TestFlight", "index-f-droid-title": "SimpleX via F-Droid", @@ -273,17 +273,17 @@ "index-publications-heise-title": "Pubblicazioni di Heise Online", "index-publications-kuketz-title": "Recensione di Mike Kuketz", "index-publications-optout-title": "Intervista podcast di OptOut", - "worlds-most-secure-messaging": "La messaggistica più sicura del mondo", - "index-messaging-p1": "SimpleX usa una crittografia end-to-end all'avanguardia.", - "index-messaging-p2": "Per la tua sicurezza e privacy, i server non possono vedere i messaggi e con chi parli.", + "worlds-most-secure-messaging": "Nessuno può vedere con chi parli", + "index-messaging-p1": "Nemmeno i server – tutti i messaggi appaiono come rumore casuale.", + "index-messaging-p2": "Decine di milioni di messaggi recapitati privatamente ogni giorno.", "index-messaging-cta": "Scopri di più sui messaggi di SimpleX", - "index-nextweb-h2": "Il nuovo web
è tuo", - "index-nextweb-p1": "SimpleX è stato creato sulla convinzione che devi possedere i tuoi profili, contatti e comunità.", - "index-nextweb-p2": "Una rete decentralizzata che nessuno possiede consente di connetterti con persone e condividere idee, di restare libero e sicuro nella tua rete.", - "index-token-h2": "Comunità fatte per restare", - "index-token-p1": "Sosterrai i tuoi gruppi preferiti con futuri buoni comunitari.", - "index-token-p2": "I buoni pagheranno i server, per consentire alle tue comunità di rimanere libere e indipendenti.", - "index-token-cta": "Scopri di più e ricevi un pass di accesso gratuito per provarlo in anticipo.", + "index-nextweb-h2": "La rete
è tua", + "index-nextweb-p1": "Ogni contatto e gruppo è sul tuo dispositivo, non nel database di un server.", + "index-nextweb-p2": "Nessuna singola entità controlla la rete – chiunque può gestire server.", + "index-token-h2": "Finanziato dai suoi utenti", + "index-token-p1": "Per restare indipendenti, i grandi canali e le comunità pagheranno per i propri server.", + "index-token-p2": "Questo coprirà infrastruttura, sviluppo software e governance della rete.", + "index-token-cta": "Scopri di più sui Crediti Comunitari", "index-roadmap-h2": "Tabella di marcia per un internet libero", "index-roadmap-now": "Ora", "index-roadmap-1": "2026", @@ -291,13 +291,13 @@ "index-roadmap-1-desc": "Fuga da piattaforme centralizzate", "index-roadmap-2": "Giu 2027", "index-roadmap-2-title": "Comunità e server sostenibili", - "index-roadmap-2-desc": "Pubblicazione di buoni comunitari", + "index-roadmap-2-desc": "Lancio dei Crediti Comunitari", "index-roadmap-3": "Dic 2027", "index-roadmap-3-title": "Fare crescere le tue comunità", "index-roadmap-3-desc": "Strumenti per promuovere le tue comunità", "index-directory-h2": "Unisciti alle comunità di SimpleX", - "index-directory-p1": "Centinaia di migliaia di persone si fidano già di SimpleX.", - "index-directory-p2": "Trova le comunità nella directory di SimpleX e creane una tua!", + "index-directory-p1": "Più di 2 milioni di persone hanno scaricato le app SimpleX.", + "index-directory-p2": "Trova i tuoi canali e le comunità nella directory e creane di tuoi!", "index-directory-cta": "Vedi la directory di SimpleX", "index-directory-users-group-title": "Gruppo utenti SimpleX", "how-secure-comparison-title": "Confronto della sicurezza di crittografia end-to-end in diverse app di messaggistica", diff --git a/website/langs/ja.json b/website/langs/ja.json index fcec6944e3..8d8baee0c5 100644 --- a/website/langs/ja.json +++ b/website/langs/ja.json @@ -260,22 +260,22 @@ "about-and-contact-us": "概要・お問い合わせ", "index-hero-h1": "自由で
あれ", "index-hero-h2": "あなたのネットワークで", - "index-hero-p1": "プライベートで安全なメッセージング。
連絡先とグループをあなた自身が所有できる最初のネットワーク。", + "index-hero-p1": "ユーザーIDのない世界初のネットワーク。
連絡先、グループ、チャンネルはあなたのものです。", "index-hero-download-desktop-btn-title": "SimpleX デスクトップアプリをダウンロード", "index-security-assessment-title": "セキュリティ監査", "index-security-review-2022-title": "セキュリティ監査 2022", "index-security-review-2024-title": "セキュリティ監査 2024", "index-security-audits-label": "セキュリティ
監査", - "worlds-most-secure-messaging": "世界で最も安全なメッセージングサービス", - "index-messaging-p1": "SimpleXの通信は、最先端のエンドツーエンド暗号化によって保護されています。", - "index-messaging-p2": "安全とプライバシーのため、サーバーはメッセージの内容や、誰とやり取りしているかを知ることができません。", + "worlds-most-secure-messaging": "誰と話しているか、誰にも見えません", + "index-messaging-p1": "サーバーですら見ることはできません – すべてのメッセージはランダムなノイズに見えます。", + "index-messaging-p2": "毎日数千万件のメッセージがプライベートに配信されています。", "index-messaging-cta": "SimpleXのメッセージ機能について詳しく知る", - "index-nextweb-h2": "次のWebは
あなたのもの", - "index-nextweb-p1": "SimpleXは、 アイデンティティ・連絡先・コミュニティはあなたのものであるべきだという考えに基づいています。", - "index-nextweb-p2": "分散型のネットワークで、自由で安全に人とつながり、アイデアを共有できます。", - "index-token-h2": "続いていくコミュニティ", - "index-token-p1": "コミュニティバウチャーを通じて、お気に入りのグループをサポートできます。", - "index-token-p2": "バウチャーはサーバー費用に充てられ、コミュニティが自由で独立した状態を保ち続けられるようにします。", + "index-nextweb-h2": "ネットワークは
あなたのもの", + "index-nextweb-p1": "すべての連絡先とグループはあなたのデバイス上にあり、サーバーのデータベースにはありません。", + "index-nextweb-p2": "ネットワークを支配する単一の組織はありません – 誰でもサーバーを運用できます。", + "index-token-h2": "ユーザーの資金で運営", + "index-token-p1": "独立性を維持するため、大規模なチャンネルやコミュニティはサーバー費用を負担します。", + "index-token-p2": "これにより、インフラ、ソフトウェア開発、ネットワークガバナンスの費用が賄われます。", "index-roadmap-h2": "自由なインターネットを目指す SimpleX ロードマップ", "index-roadmap-now": "現在", "index-roadmap-1": "2026", @@ -283,13 +283,13 @@ "index-roadmap-1-desc": "中央集権型プラットフォームからの脱却", "index-roadmap-2": "2027年6月", "index-roadmap-2-title": "サステナブルなコミュニティ&サーバ", - "index-roadmap-2-desc": "コミュニティバウチャーの開始", + "index-roadmap-2-desc": "コミュニティクレジットの開始", "index-roadmap-3": "2027年12月", "index-roadmap-3-title": "コミュニティの成長", "index-roadmap-3-desc": "コミュニティを広げるツール", "index-directory-h2": "SimpleXコミュニティに参加する", - "index-directory-p1": "既に何十万人もの人々がSimpleXメッセージングを信頼しています。", - "index-directory-p2": "SimpleXディレクトリでコミュニティを見つけたり、あなた自身のコミュニティを作成しましょう!", + "index-directory-p1": "200万人以上がSimpleXアプリをダウンロードしました。", + "index-directory-p2": "ディレクトリでチャンネルやコミュニティを見つけて、あなた自身のものを作成しましょう!", "index-directory-cta": "SimpleXディレクトリを見る", "index-directory-users-group-title": "SimpleXユーザグループ", "index-publications-privacy-guides-title": "Privacy Guide 推奨", diff --git a/website/langs/pl.json b/website/langs/pl.json index 09062757b6..9a9fe79d16 100644 --- a/website/langs/pl.json +++ b/website/langs/pl.json @@ -261,7 +261,7 @@ "about-and-contact-us": "O nas i Kontakt", "index-hero-h1": "Bądź
Wolny", "index-hero-h2": "W Swojej Sieci", - "index-hero-p1": "Prywatne i bezpieczne wiadomości.
Pierwsza sieć, w której posiadasz na własność swoje kontakty i grupy.", + "index-hero-p1": "Pierwsza sieć bez identyfikatorów użytkowników.
Twoje kontakty, grupy i kanały należą do Ciebie.", "index-hero-download-desktop-btn-title": "Pobierz Aplikację SimpleX na Komputer", "index-testflight-title": "SimpleX iOS beta-wydanie na TestFlight", "index-f-droid-title": "SimpleX app z F-Droid", @@ -274,17 +274,17 @@ "index-publications-heise-title": "Publikacje Online Heise", "index-publications-kuketz-title": "Recenzja od Mike'a Kuketz'a", "index-publications-optout-title": "Wywiad w formie podcastu od OptOut", - "worlds-most-secure-messaging": "Najbardziej bezpieczne wiadomości na świecie", - "index-messaging-p1": "Komunikator SimpleX posiada najbardziej zaawansowane szyfrowanie end-to-end.", - "index-messaging-p2": "Dla Twojego bezpieczeństwa i prywatności, serwery nie mogą zobaczyć wiadomości i tego z kim rozmawiasz.", + "worlds-most-secure-messaging": "Nikt Nie Widzi Z Kim Rozmawiasz", + "index-messaging-p1": "Nawet serwery nie widzą – wszystkie wiadomości wyglądają jak losowy szum.", + "index-messaging-p2": "Dziesiątki milionów wiadomości dostarczanych prywatnie każdego dnia.", "index-messaging-cta": "Dowiedz się więcej o komunikatorze SimpleX", - "index-nextweb-h2": "Ty Posiadasz
Sieć Kolejnej Generacji", - "index-nextweb-p1": "SimpleX powstał w oparciu o przekonanie, że musisz być właścicielem swoich profili, kontaktów i społeczności.", - "index-nextweb-p2": "Zdecentralizowana sieć, której nikt nie jest właścicielem, pozwala łączyć się z ludźmi i dzielić się pomysłami, zapewniając swobodę i bezpieczeństwo w sieci.", - "index-token-h2": "Społeczności, Które Trwają", - "index-token-p1": "Będziesz mógł wspierać swoje ulubione grupy dzięki przyszłym Voucherom Społeczności.", - "index-token-p2": "Vouchery opłacą serwery, aby Twoje społeczności pozostały wolne i niezależne.", - "index-token-cta": "Dowiedz się więcej i uzyskaj bezpłatną przepustkę umożliwiającą wczesne testowanie.", + "index-nextweb-h2": "Sieć Należy
Do Ciebie", + "index-nextweb-p1": "Każdy kontakt i grupa znajduje się na Twoim urządzeniu, a nie w bazie danych serwera.", + "index-nextweb-p2": "Żaden podmiot nie kontroluje sieci – każdy może uruchomić serwer.", + "index-token-h2": "Finansowane Przez Użytkowników", + "index-token-p1": "Aby zachować niezależność, duże kanały i społeczności będą opłacać swoje serwery.", + "index-token-p2": "Pokryje to infrastrukturę, rozwój oprogramowania i zarządzanie siecią.", + "index-token-cta": "Dowiedz się więcej o Community Credits", "index-roadmap-h2": "Plan Działania SimpleX dla Wolnego Internetu", "index-roadmap-now": "Teraz", "index-roadmap-1": "2026", @@ -292,13 +292,13 @@ "index-roadmap-1-desc": "Wymyka się scentralizowanym platformom", "index-roadmap-2": "Cze 2027", "index-roadmap-2-title": "Zrównoważone Społeczności i Serwery", - "index-roadmap-2-desc": "Uruchomienie Voucherów Społeczności", + "index-roadmap-2-desc": "Uruchomienie Community Credits", "index-roadmap-3": "Gru 2027", "index-roadmap-3-title": "Spraw, aby Twoje społeczności Rosły", "index-roadmap-3-desc": "Narzędzia do promowania Twoich społeczności", "index-directory-h2": "Dołącz do Społeczności SimpleX", - "index-directory-p1": "Setki tysięcy ludzi już ufają wiadomościom SimpleX.", - "index-directory-p2": "Znajdź swoje społeczności w katalogu SimpleX i stwórz własne!", + "index-directory-p1": "Ponad 2 miliony osób pobrało aplikacje SimpleX.", + "index-directory-p2": "Znajdź swoje kanały i społeczności w katalogu i stwórz własne!", "index-directory-cta": "Zobacz katalog SimpleX", "index-directory-users-group-title": "Grupa użytkowników SimpleX", "how-secure-comparison-title": "Porównanie zabezpieczeń szyfrowania end-to-end w różnych komunikatorach", diff --git a/website/langs/pt_BR.json b/website/langs/pt_BR.json index 0fdf72adf0..32b2ac5e05 100644 --- a/website/langs/pt_BR.json +++ b/website/langs/pt_BR.json @@ -261,7 +261,7 @@ "navbar-token": "Token", "index-hero-h1": "Seja
Livre", "index-hero-h2": "Na Sua Rede", - "index-hero-p1": "Mensagens privadas e seguras.
A primeira rede onde você é dono dos seus contatos e grupos.", + "index-hero-p1": "A primeira rede sem IDs de usuário.
Seus contatos, grupos e canais pertencem a você.", "index-hero-download-desktop-btn-title": "Baixe o aplicativo SimpleX Desktop", "index-testflight-title": "SimpleX iOS - versão beta no TestFlight", "index-f-droid-title": "Aplicativo SimpleX no F-Droid", @@ -274,17 +274,17 @@ "index-publications-heise-title": "Publicações da Heise Online", "index-publications-kuketz-title": "Análise por Mike Kuketz", "index-publications-optout-title": "Entrevista no podcast OptOut", - "worlds-most-secure-messaging": "O sistema de mensagens mais seguro do mundo", - "index-messaging-p1": "O SimpleX possui criptografia de ponta a ponta de última geração.", - "index-messaging-p2": "Para sua segurança e privacidade, os servidores não podem ver suas mensagensnem com quem você conversa.", + "worlds-most-secure-messaging": "Ninguém Pode Ver Com Quem Você Conversa", + "index-messaging-p1": "Nem mesmo os servidores – todas as mensagens parecem ruído aleatório.", + "index-messaging-p2": "Dezenas de milhões de mensagens entregues de forma privada todos os dias.", "index-messaging-cta": "Saiba mais sobre SimpleX Messaging", - "index-nextweb-h2": "Você é Dono
da Próxima Web", - "index-nextweb-p1": "SimpleX é fundado na crença de que você deve ser dono da sua identidade, contatos e comunidades.", - "index-nextweb-p2": "Rede aberta e descentralizada permite que você se conecte com pessoas e compartilhe ideias: seja livre e seguro.", - "index-token-h2": "Comunidades Duradouras", - "index-token-p1": "Você apoiará seus grupos favoritos com futuros Vouchers da Comunidade.", - "index-token-p2": "Os vouchers pagarão pelos servidores, permitindo que suas comunidades continuem gratuitas e independentes.", - "index-token-cta": "Saiba mais e pegue sua NFT gratuita
para testes antecipados.", + "index-nextweb-h2": "A Rede
É Sua", + "index-nextweb-p1": "Cada contato e grupo está no seu dispositivo, não no banco de dados de um servidor.", + "index-nextweb-p2": "Nenhuma entidade controla a rede – qualquer pessoa pode operar servidores.", + "index-token-h2": "Financiado Pelos Seus Usuários", + "index-token-p1": "Para manter a independência, grandes canais e comunidades pagarão pelos seus servidores.", + "index-token-p2": "Isso cobrirá infraestrutura, desenvolvimento de software e governança da rede.", + "index-token-cta": "Saiba mais sobre os Créditos da Comunidade", "index-roadmap-h2": "Roteiro do SimpleX para uma Internet Livre", "index-roadmap-now": "Agora", "index-roadmap-1": "2026", @@ -292,13 +292,13 @@ "index-roadmap-1-desc": "Fugindo de plataformas centralizadas", "index-roadmap-2": "Jun 2027", "index-roadmap-2-title": "Comunidades e Servidores Sustentáveis", - "index-roadmap-2-desc": "Lançamento dos Vouchers da Comunidade", + "index-roadmap-2-desc": "Lançamento dos Créditos da Comunidade", "index-roadmap-3": "Dez 2027", "index-roadmap-3-title": "Faça Suas Comunidades Crescerem", "index-roadmap-3-desc": "Ferramentas para promover suas comunidades", "index-directory-h2": "Participe das Comunidades SimpleX", - "index-directory-p1": "Centenas de milhares de pessoas já confiam no SimpleX Messaging.", - "index-directory-p2": "Encontre suas comunidades no diretório SimpleX e crie a sua própria!", + "index-directory-p1": "Mais de 2 milhões de pessoas baixaram os aplicativos SimpleX.", + "index-directory-p2": "Encontre seus canais e comunidades no diretório e crie os seus próprios!", "index-directory-cta": "Ver diretório do SimpleX", "how-secure-comparison-title": "Comparação da segurança da criptografia de ponta a ponta em diferentes mensageiros", "how-secure-message-padding": "Preenchimento de mensagem", diff --git a/website/langs/ru.json b/website/langs/ru.json index 656c751636..ab968446af 100644 --- a/website/langs/ru.json +++ b/website/langs/ru.json @@ -260,7 +260,7 @@ "docs-dropdown-14": "SimpleX для бизнеса", "index-hero-h1": "
Будь
Свободен
", "index-hero-h2": "В Своей Сети", - "index-hero-p1": "Конфиденциальная и безопасная передача сообщений.
Первая сеть, где Вам принадлежат Ваши контакты и группы.", + "index-hero-p1": "Первая сеть без идентификаторов пользователей.
Ваши контакты, группы и каналы принадлежат Вам.", "index-hero-download-desktop-btn-title": "Загрузить приложение SimpleX для компьютера", "index-testflight-title": "Бета-релиз для iOS на TestFlight", "index-f-droid-title": "Загрузить через F-Droid", @@ -273,17 +273,17 @@ "index-publications-heise-title": "Публикации Heise Online", "index-publications-kuketz-title": "Обзор от Mike Kuketz", "index-publications-optout-title": "OptOut подкаст интервью", - "worlds-most-secure-messaging": "Самый Безопасный Мессенджер в Мире", - "index-messaging-p1": "Сообщения в SimpleX имеют самое передовое сквозное шифрование (end-to-end).", - "index-messaging-p2": "Для Вашей безопасности, серверы не могут видеть ваши сообщения и с кем Вы разговариваете.", + "worlds-most-secure-messaging": "Никто Не Знает С Кем Вы Общаетесь", + "index-messaging-p1": "Даже серверы – все сообщения выглядят как случайный шум.", + "index-messaging-p2": "Десятки миллионов сообщений доставляются конфиденциально каждый день.", "index-messaging-cta": "Узнать больше про сообщения в SimpleX", - "index-nextweb-h2": "Ваш Интернет Будущего", - "index-nextweb-p1": "SimpleX создан на убеждении, что Ваши профили, контакты и сообщества должны принадлежать Вам.", - "index-nextweb-p2": "Децентрализованная сеть, которой никто не владеет, позволяет Вам общаться и делиться идеями, оставаясь свободными и защищёнными в Вашей сети.", - "index-token-h2": "Стабильные Сообщества", - "index-token-p1": "Вы сможете поддерживать Ваши любимые группы с помощью будущих Ваучеров Групп.", - "index-token-p2": "Ваучеры будут использоваться для оплаты за серверы, чтобы группы оставались свободными и независимыми.", - "index-token-cta": "Узнайте больше и возьмите бесплатный пропуск, чтобы участвовать в тестировании.", + "index-nextweb-h2": "Сеть Принадлежит
Вам", + "index-nextweb-p1": "Все контакты и группы хранятся на Вашем устройстве, а не в базе данных сервера.", + "index-nextweb-p2": "Ни одна организация не контролирует сеть – каждый может запускать серверы.", + "index-token-h2": "Финансируется Пользователями", + "index-token-p1": "Для сохранения независимости крупные каналы и сообщества будут оплачивать свои серверы.", + "index-token-p2": "Это покроет расходы на инфраструктуру, разработку программного обеспечения и управление сетью.", + "index-token-cta": "Узнать больше про Community Credits", "index-roadmap-h2": "Путь Сети SimpleX к Свободному Интернету", "index-roadmap-now": "Сейчас", "index-roadmap-1": "2026", @@ -291,13 +291,13 @@ "index-roadmap-1-desc": "Чтобы Вы могли покинуть централизованные платформы", "index-roadmap-2": "Июнь 2027", "index-roadmap-2-title": "Самодостаточные группы и серверы", - "index-roadmap-2-desc": "Запуск Ваучеров Групп", + "index-roadmap-2-desc": "Запуск Community Credits", "index-roadmap-3": "Дек 2027", "index-roadmap-3-title": "Поддержка роста Ваших групп", "index-roadmap-3-desc": "Инструменты для продвижения групп", "index-directory-h2": "Вступайте в Группы SimpleX", - "index-directory-p1": "Сотни тысяч людей уже доверяют мессенджеру SimpleX.", - "index-directory-p2": "Найдите группы по душе в каталоге SimpleX и создайте свои!", + "index-directory-p1": "Более 2 миллионов человек скачали приложения SimpleX.", + "index-directory-p2": "Найдите каналы и сообщества в каталоге и создайте свои!", "index-directory-cta": "Открыть каталог SimpleX", "index-directory-users-group-title": "Группа пользователей SimpleX", "how-secure-comparison-title": "Сравнение безопасности сквозного шифрования в мессенджерах", diff --git a/website/langs/zh_Hans.json b/website/langs/zh_Hans.json index b5079ac5f4..70f87ca79e 100644 --- a/website/langs/zh_Hans.json +++ b/website/langs/zh_Hans.json @@ -261,7 +261,7 @@ "docs-dropdown-15": "验证并重现构建过程", "index-hero-h1": "变得
自由", "index-hero-h2": "在属于你的网络中", - "index-hero-p1": "私密安全的即时通讯。
首个由您掌控
联系人和群组的网络。", + "index-hero-p1": "首个无用户 ID 的网络。
您的联系人、群组和频道由您掌控。", "index-hero-download-desktop-btn-title": "下载 SimpleX 桌面应用程序", "index-testflight-title": "SimpleX iOS 测试版已在 TestFlight 上发布", "index-f-droid-title": "SimpleX 安卓应用(通过 F-Droid)", @@ -274,27 +274,27 @@ "index-publications-heise-title": "Heise Online出版物", "index-publications-kuketz-title": "Mike Kuketz 的评论", "index-publications-optout-title": "OptOut播客访谈", - "worlds-most-secure-messaging": "全球最安全的即时通讯", - "index-messaging-p1": "SimpleX 即时通讯采用最先进的端到端加密技术。", - "index-messaging-p2": "为了您的安全和隐私,服务器无法看到您的消息以及您与谁交谈。", + "worlds-most-secure-messaging": "没人能看到您与谁交谈", + "index-messaging-p1": "即使服务器也无法查看 – 所有消息看起来都像随机噪声。", + "index-messaging-p2": "每天数千万条消息被私密送达。", "index-messaging-cta": "了解更多关于 SimpleX 消息传递的知识", - "index-nextweb-h2": "属于你的
下一代互联网", - "index-nextweb-p1": "SimpleX 的创建理念是:您必须拥有自己的个人资料、联系人和社区。", - "index-nextweb-p2": "一个不归个人所有的去中心化网络,让你能够与他人联系并分享想法,在网络中自由安全地生活。", - "index-token-h2": "长久存在的社区", - "index-token-p1": "您将通过未来的社区代金券支持您喜爱的团体。", - "index-token-p2": "代金券将用于支付服务器费用,让您的社区保持自由和独立。", - "index-token-cta": "了解更多信息并获取免费抢先体验券,参与早期测试。", + "index-nextweb-h2": "网络
由您掌控", + "index-nextweb-p1": "每个联系人和群组都在您的设备上,而非服务器数据库中。", + "index-nextweb-p2": "没有任何单一实体控制网络 – 任何人都可以运行服务器。", + "index-token-h2": "由用户资助", + "index-token-p1": "为保持独立性,大型频道和社区将为其服务器付费。", + "index-token-p2": "这将用于支付基础设施、软件开发和网络治理费用。", + "index-token-cta": "了解更多关于 Community Credits", "index-roadmap-h2": "SimpleX 通往自由互联网的路线图", "index-roadmap-1-title": "扩展到大型社区", "index-roadmap-1-desc": "逃离中心化平台", "index-roadmap-2-title": "可持续社区与服务器", - "index-roadmap-2-desc": "推出社区代金券", + "index-roadmap-2-desc": "推出 Community Credits", "index-roadmap-3-title": "促进社区发展", "index-roadmap-3-desc": "用于推广社区的工具", "index-directory-h2": "加入 SimpleX 社区", - "index-directory-p1": "已有数十万人信赖 SimpleX 即时通讯服务。", - "index-directory-p2": "在 SimpleX 目录中找到您的社区并创建您自己的社区!", + "index-directory-p1": "超过 200 万人下载了 SimpleX 应用。", + "index-directory-p2": "在目录中找到您的频道和社区,并创建您自己的!", "index-directory-cta": "查看 SimpleX 目录", "index-directory-users-group-title": "SimpleX 用户群组", "how-secure-comparison-title": "不同即时通讯软件端到端加密安全性的比较", diff --git a/website/src/_includes/navbar.html b/website/src/_includes/navbar.html index 9e1913879f..37daa78d3c 100644 --- a/website/src/_includes/navbar.html +++ b/website/src/_includes/navbar.html @@ -142,7 +142,7 @@ - {% if ('blog' not in page.url) and ('about' not in page.url) and ('donate' not in page.url) and ('privacy' not in page.url) and ('directory' not in page.url) and ('vouchers' not in page.url) and ('file' not in page.url) %} + {% if ('blog' not in page.url) and ('about' not in page.url) and ('donate' not in page.url) and ('privacy' not in page.url) and ('directory' not in page.url) and ('credits' not in page.url) and ('file' not in page.url) %}
diff --git a/website/src/token.html b/website/src/token.html index 120e5c277d..0949e2f2ce 100644 --- a/website/src/token.html +++ b/website/src/token.html @@ -1,8 +1,8 @@ --- layout: layouts/redirect.html -title: "SimpleX Community Vouchers" +title: "SimpleX Community Credits" description: "" -destinationURI: "/vouchers/" -destinationText: Connect to the team +destinationURI: "/credits/" +destinationText: SimpleX Community Credits templateEngineOverride: njk --- diff --git a/website/src/token.md b/website/src/token.md index 802f341937..eab17edee6 100644 --- a/website/src/token.md +++ b/website/src/token.md @@ -1,24 +1,28 @@ --- layout: layouts/token.html -title: "SimpleX Community Vouchers" -permalink: "/vouchers/index.html" +title: "SimpleX Community Credits" +permalink: "/credits/index.html" --- -# SimpleX Community Vouchers: Strategy & Vision +# SimpleX Crowdfunding to Build Community Credits: Strategy & Vision -SimpleX is a private and secure messaging network where you own your identity, contacts, groups, and content — there are no ads, no tracking, and no central authority. It relies on open protocols and open-source code, enabling anyone to audit the code and to create alternative apps and servers. +SimpleX is a private and secure messaging network without user IDs where you own your identity, contacts, groups, and content — there are no ads, no tracking, and no central authority. It relies on open protocols and open-source code, enabling anyone to audit the code and to create alternative apps and servers. No single entity can control it. -To scale for large groups and channels, without relying on any single entity, SimpleX network needs a sustainable way to fund servers. +To scale for large groups and channels, without relying on any single entity, network needs a sustainable way to fund servers. -Community Vouchers offer the solution - they are prepaid infrastructure credits for servers used by groups and channels. +Take money from advertisers and you become a surveillance company. Take money from a single large investor and you hand them the kill switch. Community Credits offer the solution — they are prepaid infrastructure credits for servers used by groups and channels. -These vouchers are not tradable tokens or speculative assets — there will be no pre-sale or emission. It's a method to pay directly for the network infrastructure while maintaining privacy. +These credits are not tradable tokens or speculative assets — there will be no pre-sale or emission. It's a method to pay directly for the network infrastructure while maintaining privacy. -For early access to test vouchers, if you're familiar with cryptocurrencies, get a free access pass to the test version — a free non-transferable NFT on Ethereum mainnet, you only need to pay for gas. +## Crowdfunding to Build Community Credits -## Why Community Vouchers? +Building Community Credits requires more capital than we have. So we will raise capital in the same way we designed the network: decentralized, community-funded, with no single point of capture. If you use SimpleX, you can own part of what you already help build. Register your interest via [this form](https://simplexchat.typeform.com/crowdfunding) and join [SimpleX Crowdfunding News channel](https://smp10.simplex.im/c#q09nMBmWFGz1m2TvgfZFaEOG5D2a7Ma9mSkl6pHXEsg) for updates. + +_Disclaimer: SimpleX Chat is testing the waters for a possible Reg CF offering. We’re not asking for or accepting any money right now, and we won’t accept any if sent. We can’t accept any offers to buy securities or take any payments until the official filing is done and it’s live through a regulated platform. Our testing the waters and your possible indications of interest doesn’t create any obligation or commitment of any kind._ + +## Why Community Credits? To pay for network infrastructure securely and privately. @@ -37,13 +41,13 @@ In short: -- Buy Community Vouchers. Initially you would pay with a stablecoin (USDT/USDC). The goal is to allow using other popular cryptocurrencies (BTC/ETH/XMR) and also in-app payments - to make direct usage of blockchain optional for the end users. +- Buy Community Credits. Initially you would pay with a stablecoin (USDT/USDC). The goal is to allow using other popular cryptocurrencies (BTC/ETH/XMR) and also in-app payments - to make direct usage of blockchain optional for the end users. - Funds are locked in an autonomous smart contract not controlled by SimpleX Chat company or by anybody else. -- Assign the Community Voucher to a group or channel you want, using its public address. This assignment is private, and group owners or server operators won't be able to link it to the purchase, thanks to zero-knowledge proofs. +- Assign Community Credits to a group or channel you want, using its public address. This assignment is private, and group owners or server operators won't be able to link it to the purchase, thanks to zero-knowledge proofs. -- Group or channel owners redeem the Vouchers to the server operators they use. The redemption is also private, and not linkable to the assignment or purchase. +- Group or channel owners redeem the Credits to the server operators they use. The redemption is also private, and not linkable to the assignment or purchase. - Server operators receive up to 70% of the unlocked funds, with the rest being allocated to network development and governance. @@ -61,24 +65,23 @@ We are currently evaluating several popular blockchains that have strong support ## Timeline & How to Get Involved -**2025**: +**2025-26**: - evaluating blockchains, -- drafting Community Vouchers whitepaper about system and cryptography design for Community Vouchers. +- drafting Community Credits whitepaper about system and cryptography design for Community Credits. - development of large groups and communities. We welcome your feedback on this proposal and any in-progress design documents. -**2026**: +**2026-27**: - launch support for large groups and channels. -- test version of Community Vouchers. +- test version of Community Credits. - SimpleX network namespace v1. **Join in**: -- Get a free NFT for early testing. - Create a small group or channel using today's tech, and get it added to our experimental directory of groups. - Talk to us if you want to be a server operator to earn revenue and about any partnerships. -## Community Vouchers FAQ +## Community Credits FAQ **Will self-hosted servers still work?** @@ -95,13 +98,13 @@ Cryptocurrencies are: High level of privacy is achieved by new address per purchase, proxied access to blockchain, and zero-knowledge proofs that make payment and usage unlinkable. -**Can I sell or transfer vouchers?** +**Can I sell or transfer Credits?** -No, Community Vouchers cannot be sold or transferred. Once purchased and assigned to a group or channel, they can only be redeemed to server operators. They will expire in 12 months if not redeemed, with the funds released to network development and governance. +No, Community Credits cannot be sold or transferred. Once purchased and assigned to a group or channel, they can only be redeemed to server operators. They will expire in 12 months if not redeemed, with the funds released to network development and governance. **Free messaging limits?** -Private chats and small groups remain free within fair use (up to 128 undelivered messages per contact, with up to 21 days storage, up to 1GB files stored for 2 days). Community Vouchers will be used to pay for large groups infrastructure and for memorable public names. +Private chats and small groups remain free within fair use (up to 128 undelivered messages per contact, with up to 21 days storage, up to 1GB files stored for 2 days). Community Credits will be used to pay for large groups infrastructure and for memorable public names. **Who controls the smart contracts?** @@ -116,9 +119,9 @@ Server operators will receive up to 70% of the infrastructure payments. A higher **What is the technology design?** -[The conceptual design](https://github.com/simplex-chat/simplex-chat/blob/master/docs/rfcs/2025-12-10-vouchers-2.md) for Community Vouchers uses zero-knowledge proofs, making the purchase, assigning vouchers to groups and their redemptions unlinkable. +[The conceptual design](https://github.com/simplex-chat/simplex-chat/blob/master/docs/rfcs/2025-12-10-vouchers-2.md) for Community Credits uses zero-knowledge proofs, making the purchase, assigning credits to groups and their redemptions unlinkable. -A whitepaper will be published in February 2026. +A whitepaper will be published in June 2026. ## Disclaimer diff --git a/website/src/vouchers.html b/website/src/vouchers.html new file mode 100644 index 0000000000..0949e2f2ce --- /dev/null +++ b/website/src/vouchers.html @@ -0,0 +1,8 @@ +--- +layout: layouts/redirect.html +title: "SimpleX Community Credits" +description: "" +destinationURI: "/credits/" +destinationText: SimpleX Community Credits +templateEngineOverride: njk +--- diff --git a/website/web.sh b/website/web.sh index 7abaa4d12e..d276335358 100755 --- a/website/web.sh +++ b/website/web.sh @@ -48,6 +48,7 @@ for lang in "${langs[@]}"; do cp src/invitation.html src/$lang cp src/fdroid.html src/$lang cp src/why.html src/$lang + cp src/file.html src/$lang echo "{\"lang\":\"$lang\"}" > src/$lang/$lang.json echo "done $lang copying" done