Skip to content

Commit 942aa87

Browse files
PIG208gnprice
authored andcommitted
message [nfc]: Move sendMessage from PerAccountStore
MessageStore stands out as a better home for sendMessage, which was on PerAccountStore before we extracted all the separate stores. This will make it more natural to support outbox/local echoing.
1 parent 0494723 commit 942aa87

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

lib/model/message.dart

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import 'dart:convert';
22

3+
import '../api/core.dart';
34
import '../api/model/events.dart';
45
import '../api/model/model.dart';
6+
import '../api/route/messages.dart';
57
import '../log.dart';
68
import 'message_list.dart';
79

10+
const _apiSendMessage = sendMessage; // Bit ugly; for alternatives, see: https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/flutter.3A.20PerAccountStore.20methods/near/1545809
11+
812
/// The portion of [PerAccountStore] for messages and message lists.
913
mixin MessageStore {
1014
/// All known messages, indexed by [Message.id].
@@ -15,6 +19,11 @@ mixin MessageStore {
1519
void registerMessageList(MessageListView view);
1620
void unregisterMessageList(MessageListView view);
1721

22+
Future<void> sendMessage({
23+
required MessageDestination destination,
24+
required String content,
25+
});
26+
1827
/// Reconcile a batch of just-fetched messages with the store,
1928
/// mutating the list.
2029
///
@@ -29,11 +38,13 @@ mixin MessageStore {
2938
}
3039

3140
class MessageStoreImpl with MessageStore {
32-
MessageStoreImpl()
41+
MessageStoreImpl({required this.connection})
3342
// There are no messages in InitialSnapshot, so we don't have
3443
// a use case for initializing MessageStore with nonempty [messages].
3544
: messages = {};
3645

46+
final ApiConnection connection;
47+
3748
@override
3849
final Map<int, Message> messages;
3950

@@ -77,6 +88,17 @@ class MessageStoreImpl with MessageStore {
7788
// https://chat.zulip.org/#narrow/channel/243-mobile-team/topic/MessageListView.20lifecycle/near/2086893
7889
}
7990

91+
@override
92+
Future<void> sendMessage({required MessageDestination destination, required String content}) {
93+
// TODO implement outbox; see design at
94+
// https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/.23M3881.20Sending.20outbox.20messages.20is.20fraught.20with.20issues/near/1405739
95+
return _apiSendMessage(connection,
96+
destination: destination,
97+
content: content,
98+
readBySender: true,
99+
);
100+
}
101+
80102
@override
81103
void reconcileMessages(List<Message> messages) {
82104
// What to do when some of the just-fetched messages are already known?

lib/model/store.dart

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, UserStore, Channel
393393
typingStartedExpiryPeriod: Duration(milliseconds: initialSnapshot.serverTypingStartedExpiryPeriodMilliseconds),
394394
),
395395
channels: channels,
396-
messages: MessageStoreImpl(),
396+
messages: MessageStoreImpl(connection: connection),
397397
unreads: Unreads(
398398
initial: initialSnapshot.unreadMsgs,
399399
selfUserId: account.userId,
@@ -829,16 +829,10 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, UserStore, Channel
829829
}
830830
}
831831

832+
@override
832833
Future<void> sendMessage({required MessageDestination destination, required String content}) {
833834
assert(!_disposed);
834-
835-
// TODO implement outbox; see design at
836-
// https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/.23M3881.20Sending.20outbox.20messages.20is.20fraught.20with.20issues/near/1405739
837-
return _apiSendMessage(connection,
838-
destination: destination,
839-
content: content,
840-
readBySender: true,
841-
);
835+
return _messages.sendMessage(destination: destination, content: content);
842836
}
843837

844838
static List<CustomProfileField> _sortCustomProfileFields(List<CustomProfileField> initialCustomProfileFields) {
@@ -860,7 +854,6 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, UserStore, Channel
860854
String toString() => '${objectRuntimeType(this, 'PerAccountStore')}#${shortHash(this)}';
861855
}
862856

863-
const _apiSendMessage = sendMessage; // Bit ugly; for alternatives, see: https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/flutter.3A.20PerAccountStore.20methods/near/1545809
864857
const _tryResolveUrl = tryResolveUrl;
865858

866859
/// Like [Uri.resolve], but on failure return null instead of throwing.

0 commit comments

Comments
 (0)