forked from zulip/zulip-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_data.dart
178 lines (161 loc) · 5.69 KB
/
example_data.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import 'package:zulip/api/model/initial_snapshot.dart';
import 'package:zulip/api/model/model.dart';
import 'package:zulip/model/store.dart';
import 'api/fake_api.dart';
final Uri realmUrl = Uri.parse('https://chat.example/');
const String recentZulipVersion = '8.0';
const int recentZulipFeatureLevel = 185;
const int futureZulipFeatureLevel = 9999;
User user({int? userId, String? email, String? fullName}) {
return User(
userId: userId ?? 123, // TODO generate example IDs
deliveryEmailStaleDoNotUse: '[email protected]',
email: email ?? '[email protected]', // TODO generate example emails
fullName: fullName ?? 'A user', // TODO generate example names
dateJoined: '2023-04-28',
isActive: true,
isOwner: false,
isAdmin: false,
isGuest: false,
isBillingAdmin: false,
isBot: false,
role: 400,
timezone: 'UTC',
avatarUrl: null,
avatarVersion: 0,
profileData: null,
);
}
final User selfUser = user(fullName: 'Self User', email: 'self@example', userId: 123);
final Account selfAccount = Account(
id: 1001,
realmUrl: realmUrl,
email: selfUser.email,
apiKey: 'asdfqwer',
userId: selfUser.userId,
zulipFeatureLevel: recentZulipFeatureLevel,
zulipVersion: recentZulipVersion,
zulipMergeBase: recentZulipVersion,
);
final User otherUser = user(fullName: 'Other User', email: 'other@example', userId: 234);
final Account otherAccount = Account(
id: 1002,
realmUrl: realmUrl,
email: otherUser.email,
apiKey: 'sdfgwert',
userId: otherUser.userId,
zulipFeatureLevel: recentZulipFeatureLevel,
zulipVersion: recentZulipVersion,
zulipMergeBase: recentZulipVersion,
);
final User thirdUser = user(fullName: 'Third User', email: 'third@example', userId: 345);
ZulipStream stream({
int? streamId,
String? name,
String? description,
String? renderedDescription,
int? dateCreated,
int? firstMessageId,
bool? inviteOnly,
bool? isWebPublic,
bool? historyPublicToSubscribers,
int? messageRetentionDays,
int? streamPostPolicy,
int? canRemoveSubscribersGroupId,
}) {
return ZulipStream(
streamId: streamId ?? 123, // TODO generate example IDs
name: name ?? 'A stream', // TODO generate example names
description: description ?? 'A description', // TODO generate example descriptions
renderedDescription: renderedDescription ?? '<p>A description</p>', // TODO generate random
dateCreated: dateCreated ?? 1686774898,
firstMessageId: firstMessageId,
inviteOnly: inviteOnly ?? false,
isWebPublic: isWebPublic ?? false,
historyPublicToSubscribers: historyPublicToSubscribers ?? true,
messageRetentionDays: messageRetentionDays,
streamPostPolicy: streamPostPolicy ?? 1,
canRemoveSubscribersGroupId: canRemoveSubscribersGroupId ?? 123,
);
}
final _messagePropertiesBase = {
'is_me_message': false,
'last_edit_timestamp': null,
'recipient_id': 32, // obsolescent in API, and ignored
};
Map<String, dynamic> _messagePropertiesFromSender(User? sender) {
return {
'client': 'ExampleClient',
'sender_email': sender?.email ?? 'a-person@example',
'sender_full_name': sender?.fullName ?? 'A Person',
'sender_id': sender?.userId ?? 12345, // TODO generate example IDs
'sender_realm_str': 'zulip',
};
}
// When we have a Stream object, this can take that as an argument.
// Also it can default explicitly to an example stream.
StreamMessage streamMessage(
{User? sender, String? streamName, int? streamId}) {
// The use of JSON here is convenient in order to delegate parts of the data
// to helper functions. The main downside is that it loses static typing
// of the properties as we're constructing the data. That's probably OK
// because (a) this is only for tests; (b) the types do get checked
// dynamically in the constructor, so any ill-typing won't propagate further.
return StreamMessage.fromJson({
..._messagePropertiesBase,
..._messagePropertiesFromSender(sender),
'display_recipient': streamName ?? 'a stream',
'stream_id': streamId ?? 123, // TODO generate example IDs
'content': '<p>This is an example stream message.</p>',
'content_type': 'text/html',
'flags': [],
'id': 1234567, // TODO generate example IDs
'subject': 'example topic',
'timestamp': 1678139636,
'type': 'stream',
});
}
/// Construct an example direct message.
///
/// See also:
/// * [streamMessage], to construct an example stream message.
DmMessage dmMessage({required User from, required List<User> to}) {
assert(!to.any((user) => user.userId == from.userId));
return DmMessage.fromJson({
..._messagePropertiesBase,
..._messagePropertiesFromSender(from),
'display_recipient': [from, ...to]
.map((u) => {'id': u.userId, 'email': u.email, 'full_name': u.fullName})
.toList(growable: false),
'content': '<p>This is an example DM.</p>',
'content_type': 'text/html',
'flags': [],
'id': 1234567, // TODO generate example IDs
'subject': '',
'timestamp': 1678139636,
'type': 'private',
});
}
// TODO example data for many more types
final InitialSnapshot initialSnapshot = InitialSnapshot(
queueId: '1:2345',
lastEventId: 1,
zulipFeatureLevel: recentZulipFeatureLevel,
zulipVersion: recentZulipVersion,
zulipMergeBase: recentZulipVersion,
alertWords: ['klaxon'],
customProfileFields: [],
subscriptions: [], // TODO add subscriptions to example initial snapshot
streams: [], // TODO add streams to example initial snapshot
maxFileUploadSizeMib: 25,
realmUsers: [],
realmNonActiveUsers: [],
crossRealmBots: [],
);
PerAccountStore store() {
return PerAccountStore.fromInitialSnapshot(
account: selfAccount,
connection: FakeApiConnection.fromAccount(selfAccount),
initialSnapshot: initialSnapshot,
);
}