forked from zulip/zulip-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal_link.dart
281 lines (249 loc) · 9.74 KB
/
internal_link.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';
import '../api/model/narrow.dart';
import 'narrow.dart';
import 'store.dart';
import 'channel.dart';
part 'internal_link.g.dart';
const _hashReplacements = {
"%": ".",
"(": ".28",
")": ".29",
".": ".2E",
};
final _encodeHashComponentRegex = RegExp(r'[%().]');
// Corresponds to encodeHashComponent in Zulip web;
// see web/shared/src/internal_url.ts.
String _encodeHashComponent(String str) {
return Uri.encodeComponent(str)
.replaceAllMapped(_encodeHashComponentRegex, (Match m) => _hashReplacements[m[0]!]!);
}
/// Decode a dot-encoded string; return null if malformed.
// The Zulip webapp uses this encoding in narrow-links:
// https://github.com/zulip/zulip/blob/1577662a6/static/js/hash_util.js#L18-L25
@visibleForTesting
String? decodeHashComponent(String str) {
try {
return Uri.decodeComponent(str.replaceAll('.', '%'));
} on ArgumentError {
// as with '%1': Invalid argument(s): Truncated URI
return null;
} on FormatException {
// as with '%FF': FormatException: Invalid UTF-8 byte (at offset 0)
return null;
}
}
/// A URL to the given [Narrow], on `store`'s realm.
///
/// To include /near/{messageId} in the link, pass a non-null [nearMessageId].
// Why take [nearMessageId] in a param, instead of looking for it in [narrow]?
//
// A reasonable question: after all, the "near" part of a near link (e.g., for
// quote-and-reply) does take the same form as other operator/operand pairs
// that we represent with [ApiNarrowElement]s, like "/stream/48-mobile".
//
// But unlike those other elements, we choose not to give the "near" element
// an [ApiNarrowElement] representation, because it doesn't have quite that role:
// it says where to look in a list of messages, but it doesn't filter the list down.
// In fact, from a brief look at server code, it seems to be *ignored*
// if you include it in the `narrow` param in get-messages requests.
// When you want to point the server to a location in a message list, you
// you do so by passing the `anchor` param.
Uri narrowLink(PerAccountStore store, Narrow narrow, {int? nearMessageId}) {
// TODO(server-7)
final apiNarrow = resolveDmElements(
narrow.apiEncode(), store.connection.zulipFeatureLevel!);
final fragment = StringBuffer('narrow');
for (ApiNarrowElement element in apiNarrow) {
fragment.write('/');
if (element.negated) {
fragment.write('-');
}
fragment.write('${element.operator}/');
switch (element) {
case ApiNarrowStream():
final streamId = element.operand;
final name = store.streams[streamId]?.name ?? 'unknown';
final slugifiedName = _encodeHashComponent(name.replaceAll(' ', '-'));
fragment.write('$streamId-$slugifiedName');
case ApiNarrowTopic():
fragment.write(_encodeHashComponent(element.operand));
case ApiNarrowDmModern():
final suffix = element.operand.length >= 3 ? 'group' : 'dm';
fragment.write('${element.operand.join(',')}-$suffix');
case ApiNarrowPmWith():
final suffix = element.operand.length >= 3 ? 'group' : 'pm';
fragment.write('${element.operand.join(',')}-$suffix');
case ApiNarrowDm():
assert(false, 'ApiNarrowDm should have been resolved');
case ApiNarrowIsUnread():
fragment.write(element.operand.toString());
case ApiNarrowMessageId():
fragment.write(element.operand.toString());
}
}
if (nearMessageId != null) {
fragment.write('/near/$nearMessageId');
}
return store.realmUrl.replace(fragment: fragment.toString());
}
/// A [Narrow] from a given URL, on `store`'s realm.
///
/// `url` must already be a result from [PerAccountStore.tryResolveUrl]
/// on `store`.
///
/// Returns `null` if any of the operator/operand pairs are invalid.
///
/// Since narrow links can combine operators in ways our [Narrow] type can't
/// represent, this can also return null for valid narrow links.
///
/// This can also return null for some valid narrow links that our Narrow
/// type *could* accurately represent. We should try to understand these
/// better, but some kinds will be rare, even unheard-of:
/// #narrow/stream/1-announce/stream/1-announce (duplicated operator)
// TODO(#252): handle all valid narrow links, returning a search narrow
Narrow? parseInternalLink(Uri url, PerAccountStore store) {
if (!_isInternalLink(url, store.realmUrl)) return null;
final (category, segments) = _getCategoryAndSegmentsFromFragment(url.fragment);
switch (category) {
case 'narrow':
if (segments.isEmpty || !segments.length.isEven) return null;
return _interpretNarrowSegments(segments, store);
}
return null;
}
/// Check if `url` is an internal link on the given `realmUrl`.
bool _isInternalLink(Uri url, Uri realmUrl) {
try {
if (url.origin != realmUrl.origin) return false;
} on StateError {
return false;
}
return (url.hasEmptyPath || url.path == '/')
&& !url.hasQuery
&& url.hasFragment;
}
/// Split `fragment` of arbitrary segments and handle trailing slashes
(String, List<String>) _getCategoryAndSegmentsFromFragment(String fragment) {
final [category, ...segments] = fragment.split('/');
if (segments.length > 1 && segments.last == '') segments.removeLast();
return (category, segments);
}
Narrow? _interpretNarrowSegments(List<String> segments, PerAccountStore store) {
assert(segments.isNotEmpty);
assert(segments.length.isEven);
ApiNarrowStream? streamElement;
ApiNarrowTopic? topicElement;
ApiNarrowDm? dmElement;
for (var i = 0; i < segments.length; i += 2) {
final (operator, negated) = _parseOperator(segments[i]);
if (negated) return null;
final operand = segments[i + 1];
switch (operator) {
case _NarrowOperator.stream:
case _NarrowOperator.channel:
if (streamElement != null) return null;
final streamId = _parseStreamOperand(operand, store);
if (streamId == null) return null;
streamElement = ApiNarrowStream(streamId, negated: negated);
case _NarrowOperator.topic:
case _NarrowOperator.subject:
if (topicElement != null) return null;
final String? topic = decodeHashComponent(operand);
if (topic == null) return null;
topicElement = ApiNarrowTopic(topic, negated: negated);
case _NarrowOperator.dm:
case _NarrowOperator.pmWith:
if (dmElement != null) return null;
final dmIds = _parseDmOperand(operand);
if (dmIds == null) return null;
dmElement = ApiNarrowDm(dmIds, negated: negated);
case _NarrowOperator.near: // TODO(#82): support for near
case _NarrowOperator.with_: // TODO(#683): support for with
continue;
case _NarrowOperator.unknown:
return null;
}
}
if (dmElement != null) {
if (streamElement != null || topicElement != null) return null;
return DmNarrow.withUsers(dmElement.operand, selfUserId: store.selfUserId);
} else if (streamElement != null) {
final streamId = streamElement.operand;
if (topicElement != null) {
return TopicNarrow(streamId, topicElement.operand);
} else {
return StreamNarrow(streamId);
}
}
return null;
}
@JsonEnum(fieldRename: FieldRename.kebab, alwaysCreate: true)
enum _NarrowOperator {
// 'dm' is new in server-7.0; means the same as 'pm-with'
dm,
near,
// cannot use `with` as it is a reserved keyword in Dart
@JsonValue('with')
with_,
pmWith,
stream,
channel,
subject,
topic,
unknown;
static _NarrowOperator fromRawString(String raw) => _byRawString[raw] ?? unknown;
static final _byRawString = _$_NarrowOperatorEnumMap.map((key, value) => MapEntry(value, key));
}
(_NarrowOperator, bool) _parseOperator(String input) {
final String operator;
final bool negated;
if (input.startsWith('-')) {
operator = input.substring(1);
negated = true;
} else {
operator = input;
negated = false;
}
return (_NarrowOperator.fromRawString(operator), negated);
}
/// Parse the operand of a `stream` operator, returning a stream ID.
///
/// The ID might point to a stream that's hidden from our user (perhaps
/// doesn't exist). If so, most likely the user doesn't have permission to
/// see the stream's existence -- like with a guest user for any stream
/// they're not in, or any non-admin with a private stream they're not in.
/// Could be that whoever wrote the link just made something up.
///
/// Returns null if the operand has an unexpected shape, or has the old shape
/// (stream name but no ID) and we don't know of a stream by the given name.
int? _parseStreamOperand(String operand, ChannelStore store) {
// "New" (2018) format: ${stream_id}-${stream_name} .
final match = RegExp(r'^(\d+)(?:-.*)?$').firstMatch(operand);
final newFormatStreamId = (match != null) ? int.parse(match.group(1)!, radix: 10) : null;
if (newFormatStreamId != null && store.streams.containsKey(newFormatStreamId)) {
return newFormatStreamId;
}
// Old format: just stream name. This case is relevant indefinitely,
// so that links in old conversations continue to work.
final String? streamName = decodeHashComponent(operand);
if (streamName == null) return null;
final stream = store.streamsByName[streamName];
if (stream != null) return stream.streamId;
if (newFormatStreamId != null) {
// Neither format found a stream, so it's hidden or doesn't exist. But
// at least we have a stream ID; give that to the caller.
return newFormatStreamId;
}
// Unexpected shape, or the old shape and we don't know of a stream with
// the given name.
return null;
}
List<int>? _parseDmOperand(String operand) {
final rawIds = operand.split('-')[0].split(',');
try {
return rawIds.map((rawId) => int.parse(rawId, radix: 10)).toList();
} on FormatException {
return null;
}
}