Skip to content

Commit 143c247

Browse files
committed
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 e524e6b commit 143c247

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

lib/model/message.dart

+23-1
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

+3-10
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, UserStore, Channel
366366
typingStartedExpiryPeriod: Duration(milliseconds: initialSnapshot.serverTypingStartedExpiryPeriodMilliseconds),
367367
),
368368
channels: channels,
369-
messages: MessageStoreImpl(),
369+
messages: MessageStoreImpl(connection: connection),
370370
unreads: Unreads(
371371
initial: initialSnapshot.unreadMsgs,
372372
selfUserId: account.userId,
@@ -802,16 +802,10 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, UserStore, Channel
802802
}
803803
}
804804

805+
@override
805806
Future<void> sendMessage({required MessageDestination destination, required String content}) {
806807
assert(!_disposed);
807-
808-
// TODO implement outbox; see design at
809-
// https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/.23M3881.20Sending.20outbox.20messages.20is.20fraught.20with.20issues/near/1405739
810-
return _apiSendMessage(connection,
811-
destination: destination,
812-
content: content,
813-
readBySender: true,
814-
);
808+
return _messages.sendMessage(destination: destination, content: content);
815809
}
816810

817811
static List<CustomProfileField> _sortCustomProfileFields(List<CustomProfileField> initialCustomProfileFields) {
@@ -833,7 +827,6 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, UserStore, Channel
833827
String toString() => '${objectRuntimeType(this, 'PerAccountStore')}#${shortHash(this)}';
834828
}
835829

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

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

0 commit comments

Comments
 (0)