diff --git a/lib/controllers/account_data.dart b/lib/controllers/account_data.dart index 8c68209..debad12 100644 --- a/lib/controllers/account_data.dart +++ b/lib/controllers/account_data.dart @@ -5,6 +5,7 @@ import "package:fast_immutable_collections/fast_immutable_collections.dart"; import "package:flutter/foundation.dart"; import "package:flutter_riverpod/flutter_riverpod.dart"; import "package:nexus/controllers/client.dart"; +import "package:nexus/controllers/pending_subprofiles.dart"; import "package:nexus/controllers/rooms.dart"; import "package:nexus/models/per_message_profiles.dart"; import "package:nexus/models/account_data.dart"; @@ -85,24 +86,33 @@ class AccountDataController extends Notifier { (data) => data.perMessageProfiles == profiles, ); - Future setRoomProfile(String roomId, String profileId) async { + Future setRoomProfile(String roomId, String profileId) { const type = AccountData.perMessageProfilesKey; - final rooms = ref.read(RoomsController.provider.notifier); - final previous = ref + const key = "default_profile_id"; + final current = ref .read(RoomsController.provider)[roomId] ?.accountData[type]; - final content = {...?previous, "default_profile_id": profileId}; - // Shown straight away, rather than once it comes down sync - rooms.setAccountData(roomId, type, content); - try { - await ref - .read(ClientController.provider.notifier) - .setAccountData(.new(type: type, roomId: roomId, content: content)); - } catch (_) { - rooms.setAccountData(roomId, type, previous); - rethrow; - } + return ref.read(PendingSubprofilesController.provider.notifier).track( + roomId, + profileId, + () async { + await ref + .read(ClientController.provider.notifier) + .setAccountData( + .new( + type: type, + roomId: roomId, + content: {...?current, key: profileId}, + ), + ); + await ref + .read(RoomsController.provider.notifier) + .awaitRooms( + (rooms) => rooms[roomId]?.accountData[type]?[key] == profileId, + ); + }, + ); } /// Saves [rule] of [kind], creating it if it's new, or otherwise changing only diff --git a/lib/controllers/pending_subprofiles.dart b/lib/controllers/pending_subprofiles.dart new file mode 100644 index 0000000..1e7b9b0 --- /dev/null +++ b/lib/controllers/pending_subprofiles.dart @@ -0,0 +1,45 @@ +import "package:fast_immutable_collections/fast_immutable_collections.dart"; +import "package:flutter_riverpod/flutter_riverpod.dart"; + +typedef PendingSubprofile = ({String id, Future synced}); + +/// Subprofiles just chosen for rooms, by room ID, until they come down sync. +/// gomuks only learns of them then, so messages sent before would use the +/// previous one. +class PendingSubprofilesController + extends Notifier> { + @override + IMap build() => const .empty(); + + Future track( + String roomId, + String id, + Future Function() save, + ) async { + final synced = save(); + state = state.add(roomId, (id: id, synced: synced)); + + try { + await synced; + } finally { + // Unless another subprofile was chosen meanwhile + if (state[roomId]?.synced == synced) state = state.remove(roomId); + } + } + + Future settled(String roomId) async { + try { + await state[roomId]?.synced; + } catch (_) { + throw Exception( + "The message wasn't sent, as the subprofile chosen for this room couldn't be saved", + ); + } + } + + static final provider = + NotifierProvider< + PendingSubprofilesController, + IMap + >(PendingSubprofilesController.new); +} diff --git a/lib/controllers/room_chat.dart b/lib/controllers/room_chat.dart index 2a53a0d..e0a6765 100644 --- a/lib/controllers/room_chat.dart +++ b/lib/controllers/room_chat.dart @@ -7,6 +7,8 @@ import "package:flutter_riverpod/flutter_riverpod.dart"; import "package:fluttertagger/fluttertagger.dart"; import "package:nexus/controllers/attachment.dart"; import "package:nexus/controllers/client.dart"; +import "package:nexus/controllers/pending_subprofiles.dart"; +import "package:nexus/controllers/subprofiles.dart"; import "package:nexus/controllers/event.dart"; import "package:nexus/controllers/rooms.dart"; import "package:nexus/controllers/thread_replies.dart"; @@ -399,6 +401,7 @@ class RoomChatController(final ChatParams info) required RelationType relationType, int? relatedEventRowId, }) async { + final subprofile = subprofileFor(text); final events = ref.read(RoomsController.provider)[info.roomId]?.events; final relatedEvent = relatedEventRowId == null ? null @@ -437,6 +440,7 @@ class RoomChatController(final ChatParams info) ); final client = ref.read(ClientController.provider.notifier); + await awaitSubprofile(text, subprofile); final event = await client.sendMessage( SendMessageRequest( roomId: info.roomId, @@ -458,6 +462,24 @@ class RoomChatController(final ChatParams info) addSent(event); } + String? subprofileFor(String text, {bool synced = false}) { + final subprofiles = ref.read(SubprofilesController.provider(info.roomId)); + return (subprofiles.triggeredBy(text) ?? + (synced ? subprofiles.synced : subprofiles.current)) + ?.id; + } + + Future awaitSubprofile(String text, String? shown) async { + await ref + .read(PendingSubprofilesController.provider.notifier) + .settled(info.roomId); + if (subprofileFor(text, synced: true) != shown) { + throw Exception( + "The message wasn't sent, as it would have been sent as a different subprofile than shown", + ); + } + } + /// Shows [event], which was just sent, until it comes down sync. void addSent(Event? event) { if (event case final event?) { @@ -614,6 +636,7 @@ class RoomChatController(final ChatParams info) .read(RoomsController.provider)[info.roomId] ?.events[repliedEventRowId]; + await awaitSubprofile("", subprofileFor("")); final event = await ref .read(ClientController.provider.notifier) .sendMessage( diff --git a/lib/controllers/rooms.dart b/lib/controllers/rooms.dart index bd162dd..91d8093 100644 --- a/lib/controllers/rooms.dart +++ b/lib/controllers/rooms.dart @@ -136,24 +136,6 @@ class RoomsController extends Notifier> { .add(event.eventId, event.rowId); }); - void setAccountData( - String roomId, - String type, - Map? content, - ) { - if (state[roomId] case final room?) { - state = state.add( - roomId, - room.copyWith( - accountData: switch (content) { - final content? => room.accountData.add(type, content), - null => room.accountData.remove(type), - }, - ), - ); - } - } - void update( IMap rooms, [ ISet leftRooms = const .empty(), diff --git a/lib/controllers/subprofiles.dart b/lib/controllers/subprofiles.dart index 98d5f3e..21b1a0d 100644 --- a/lib/controllers/subprofiles.dart +++ b/lib/controllers/subprofiles.dart @@ -3,6 +3,7 @@ import "package:fast_immutable_collections/fast_immutable_collections.dart"; import "package:flutter/foundation.dart"; import "package:flutter_riverpod/flutter_riverpod.dart"; import "package:nexus/controllers/account_data.dart"; +import "package:nexus/controllers/pending_subprofiles.dart"; import "package:nexus/controllers/rooms.dart"; import "package:nexus/models/account_data.dart"; import "package:nexus/models/per_message_profiles.dart"; @@ -39,14 +40,24 @@ class SubprofilesController(final String roomId) ), ); - final currentId = room?.defaultProfileId ?? global.defaultProfileId; + StoredProfile? byId(String? id) => id == null || id.isEmpty + ? null + : profiles.firstWhereOrNull((profile) => profile.id == id); + final syncedId = room?.defaultProfileId ?? global.defaultProfileId; + return .new( profiles: profiles, triggerOrder: (room?.profiles ?? const IList.empty()) .addAll(global.profiles), - current: currentId == null || currentId.isEmpty - ? null - : profiles.firstWhereOrNull((profile) => profile.id == currentId), + current: byId( + ref.watch( + PendingSubprofilesController.provider.select( + (pending) => pending[roomId]?.id, + ), + ) ?? + syncedId, + ), + synced: byId(syncedId), ); } diff --git a/lib/models/room_subprofiles.dart b/lib/models/room_subprofiles.dart index abf72ac..69ec889 100644 --- a/lib/models/room_subprofiles.dart +++ b/lib/models/room_subprofiles.dart @@ -11,6 +11,7 @@ class const RoomSubprofiles({ // Each trigger is tried in this order, as gomuks does when sending required final IList triggerOrder, final StoredProfile? current, + final StoredProfile? synced, }) with _$RoomSubprofiles { StoredProfile? triggeredBy(String text) => triggerOrder.firstWhereOrNull( (profile) =>