|
1 | 1 | import 'dart:math';
|
2 | 2 |
|
3 | 3 | import '../api/model/model.dart';
|
4 |
| -import '../api/model/narrow.dart'; |
| 4 | +import 'internal_link.dart'; |
5 | 5 | import 'narrow.dart';
|
6 | 6 | import 'store.dart';
|
7 | 7 |
|
@@ -101,82 +101,6 @@ String wrapWithBacktickFence({required String content, String? infoString}) {
|
101 | 101 | return resultBuffer.toString();
|
102 | 102 | }
|
103 | 103 |
|
104 |
| -const _hashReplacements = { |
105 |
| - "%": ".", |
106 |
| - "(": ".28", |
107 |
| - ")": ".29", |
108 |
| - ".": ".2E", |
109 |
| -}; |
110 |
| - |
111 |
| -final _encodeHashComponentRegex = RegExp(r'[%().]'); |
112 |
| - |
113 |
| -// Corresponds to encodeHashComponent in Zulip web; |
114 |
| -// see web/shared/src/internal_url.ts. |
115 |
| -String _encodeHashComponent(String str) { |
116 |
| - return Uri.encodeComponent(str) |
117 |
| - .replaceAllMapped(_encodeHashComponentRegex, (Match m) => _hashReplacements[m[0]!]!); |
118 |
| -} |
119 |
| - |
120 |
| -/// A URL to the given [Narrow], on `store`'s realm. |
121 |
| -/// |
122 |
| -/// To include /near/{messageId} in the link, pass a non-null [nearMessageId]. |
123 |
| -// Why take [nearMessageId] in a param, instead of looking for it in [narrow]? |
124 |
| -// |
125 |
| -// A reasonable question: after all, the "near" part of a near link (e.g., for |
126 |
| -// quote-and-reply) does take the same form as other operator/operand pairs |
127 |
| -// that we represent with [ApiNarrowElement]s, like "/stream/48-mobile". |
128 |
| -// |
129 |
| -// But unlike those other elements, we choose not to give the "near" element |
130 |
| -// an [ApiNarrowElement] representation, because it doesn't have quite that role: |
131 |
| -// it says where to look in a list of messages, but it doesn't filter the list down. |
132 |
| -// In fact, from a brief look at server code, it seems to be *ignored* |
133 |
| -// if you include it in the `narrow` param in get-messages requests. |
134 |
| -// When you want to point the server to a location in a message list, you |
135 |
| -// you do so by passing the `anchor` param. |
136 |
| -Uri narrowLink(PerAccountStore store, Narrow narrow, {int? nearMessageId}) { |
137 |
| - final apiNarrow = narrow.apiEncode(); |
138 |
| - final fragment = StringBuffer('narrow'); |
139 |
| - for (ApiNarrowElement element in apiNarrow) { |
140 |
| - fragment.write('/'); |
141 |
| - if (element.negated) { |
142 |
| - fragment.write('-'); |
143 |
| - } |
144 |
| - |
145 |
| - if (element is ApiNarrowDm) { |
146 |
| - final supportsOperatorDm = store.connection.zulipFeatureLevel! >= 177; // TODO(server-7) |
147 |
| - element = element.resolve(legacy: !supportsOperatorDm); |
148 |
| - } |
149 |
| - |
150 |
| - fragment.write('${element.operator}/'); |
151 |
| - |
152 |
| - switch (element) { |
153 |
| - case ApiNarrowStream(): |
154 |
| - final streamId = element.operand; |
155 |
| - final name = store.streams[streamId]?.name ?? 'unknown'; |
156 |
| - final slugifiedName = _encodeHashComponent(name.replaceAll(' ', '-')); |
157 |
| - fragment.write('$streamId-$slugifiedName'); |
158 |
| - case ApiNarrowTopic(): |
159 |
| - fragment.write(_encodeHashComponent(element.operand)); |
160 |
| - case ApiNarrowDmModern(): |
161 |
| - final suffix = element.operand.length >= 3 ? 'group' : 'dm'; |
162 |
| - fragment.write('${element.operand.join(',')}-$suffix'); |
163 |
| - case ApiNarrowPmWith(): |
164 |
| - final suffix = element.operand.length >= 3 ? 'group' : 'pm'; |
165 |
| - fragment.write('${element.operand.join(',')}-$suffix'); |
166 |
| - case ApiNarrowDm(): |
167 |
| - assert(false, 'ApiNarrowDm should have been resolved'); |
168 |
| - case ApiNarrowMessageId(): |
169 |
| - fragment.write(element.operand.toString()); |
170 |
| - } |
171 |
| - } |
172 |
| - |
173 |
| - if (nearMessageId != null) { |
174 |
| - fragment.write('/near/$nearMessageId'); |
175 |
| - } |
176 |
| - |
177 |
| - return store.account.realmUrl.replace(fragment: fragment.toString()); |
178 |
| -} |
179 |
| - |
180 | 104 | /// An @-mention, like @**Chris Bobbe|13313**.
|
181 | 105 | ///
|
182 | 106 | /// To omit the user ID part ("|13313") whenever the name part is unambiguous,
|
|
0 commit comments