Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 49 additions & 10 deletions lib/api/model/narrow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,29 @@ typedef ApiNarrow = List<ApiNarrowElement>;
/// reasonably be omitted will be omitted.
ApiNarrow resolveApiNarrowForServer(ApiNarrow narrow, int zulipFeatureLevel) {
final supportsOperatorDm = zulipFeatureLevel >= 177; // TODO(server-7)
final supportsOperatorChannel = zulipFeatureLevel >= 250; // TODO(server-9)
final supportsOperatorWith = zulipFeatureLevel >= 271; // TODO(server-9)

bool hasDmElement = false;
bool hasChannelElement = false;
bool hasWithElement = false;
for (final element in narrow) {
switch (element) {
case ApiNarrowDm(): hasDmElement = true;
case ApiNarrowWith(): hasWithElement = true;
case ApiNarrowChannel(): hasChannelElement = true;
case ApiNarrowDm(): hasDmElement = true;
case ApiNarrowWith(): hasWithElement = true;
default:
}
}
if (!(hasDmElement || (hasWithElement && !supportsOperatorWith))) {
if (!(hasChannelElement || hasDmElement || (hasWithElement && !supportsOperatorWith))) {
return narrow;
}

final result = <ApiNarrowElement>[];
for (final element in narrow) {
switch (element) {
case ApiNarrowChannel():
result.add(element.resolve(legacy: !supportsOperatorChannel));
case ApiNarrowDm():
result.add(element.resolve(legacy: !supportsOperatorDm));
case ApiNarrowWith() when !supportsOperatorWith:
Expand Down Expand Up @@ -93,17 +98,51 @@ sealed class ApiNarrowElement {
};
}

class ApiNarrowStream extends ApiNarrowElement {
@override String get operator => 'stream';
class ApiNarrowChannel extends ApiNarrowElement {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

This change affects few places in the app, for example, generating
internal links and sending api request with the new "channel" operator.

s/few/a few/

Perhaps confusingly, the meaning of "few" and the meaning of "a few" point in opposite directions: "few" means the number is small, while "a few" means it's larger than one or two. So "affects few places" is a way of saying "don't worry, this doesn't affect much", which isn't the point you're aiming to make here.

@override String get operator {
assert(false,
"The [operator] getter was called on a plain [ApiNarrowChannel]. "
"Before passing to [jsonEncode] or otherwise getting [operator], "
"the [ApiNarrowChannel] must be replaced by the result of [ApiNarrowChannel.resolve]."
);
return "channel";
}

@override final int operand;

ApiNarrowStream(this.operand, {super.negated});
ApiNarrowChannel(this.operand, {super.negated});

factory ApiNarrowStream.fromJson(Map<String, dynamic> json) => ApiNarrowStream(
json['operand'] as int,
negated: json['negated'] as bool? ?? false,
);
factory ApiNarrowChannel.fromJson(Map<String, dynamic> json) {
var operand = (json['operand'] as int);
var negated = json['negated'] as bool? ?? false;
return json['operator'] == 'stream'
? ApiNarrowStream._(operand, negated: negated)
: ApiNarrowChannelModern._(operand, negated: negated);
}

/// This element resolved, as either an [ApiNarrowChannelModern] or an [ApiNarrowStream].
ApiNarrowChannel resolve({required bool legacy}) {
return legacy ? ApiNarrowStream._(operand, negated: negated)
: ApiNarrowChannelModern._(operand, negated: negated);
}
}

/// An [ApiNarrowElement] with the 'channel' operator (and not the legacy 'stream').
///
/// To construct one of these, use [ApiNarrowChannel.resolve].
class ApiNarrowChannelModern extends ApiNarrowChannel {
@override String get operator => 'channel';

ApiNarrowChannelModern._(super.operand, {super.negated});
}

/// An [ApiNarrowElement] with the legacy 'stream' operator.
///
/// To construct one of these, use [ApiNarrowChannel.resolve].
class ApiNarrowStream extends ApiNarrowChannel {
@override String get operator => 'stream';

ApiNarrowStream._(super.operand, {super.negated});
}

class ApiNarrowTopic extends ApiNarrowElement {
Expand Down
16 changes: 8 additions & 8 deletions lib/model/internal_link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Uri narrowLink(PerAccountStore store, Narrow narrow, {int? nearMessageId}) {
fragment.write('${element.operator}/');

switch (element) {
case ApiNarrowStream():
case ApiNarrowChannel():
final streamId = element.operand;
final name = store.streams[streamId]?.name ?? 'unknown';
final slugifiedName = _encodeHashComponent(name.replaceAll(' ', '-'));
Expand Down Expand Up @@ -182,7 +182,7 @@ NarrowLink? _interpretNarrowSegments(List<String> segments, PerAccountStore stor
assert(segments.isNotEmpty);
assert(segments.length.isEven);

ApiNarrowStream? streamElement;
ApiNarrowChannel? channelElement;
ApiNarrowTopic? topicElement;
ApiNarrowDm? dmElement;
ApiNarrowWith? withElement;
Expand All @@ -196,10 +196,10 @@ NarrowLink? _interpretNarrowSegments(List<String> segments, PerAccountStore stor
switch (operator) {
case _NarrowOperator.stream:
case _NarrowOperator.channel:
if (streamElement != null) return null;
if (channelElement != null) return null;
final streamId = _parseStreamOperand(operand, store);
if (streamId == null) return null;
streamElement = ApiNarrowStream(streamId, negated: negated);
channelElement = ApiNarrowChannel(streamId, negated: negated);

case _NarrowOperator.topic:
case _NarrowOperator.subject:
Expand Down Expand Up @@ -238,7 +238,7 @@ NarrowLink? _interpretNarrowSegments(List<String> segments, PerAccountStore stor

final Narrow? narrow;
if (isElementOperands.isNotEmpty) {
if (streamElement != null || topicElement != null || dmElement != null || withElement != null) {
if (channelElement != null || topicElement != null || dmElement != null || withElement != null) {
return null;
}
if (isElementOperands.length > 1) return null;
Expand All @@ -257,10 +257,10 @@ NarrowLink? _interpretNarrowSegments(List<String> segments, PerAccountStore stor
return null;
}
} else if (dmElement != null) {
if (streamElement != null || topicElement != null || withElement != null) return null;
if (channelElement != null || topicElement != null || withElement != null) return null;
narrow = DmNarrow.withUsers(dmElement.operand, selfUserId: store.selfUserId);
} else if (streamElement != null) {
final streamId = streamElement.operand;
} else if (channelElement != null) {
final streamId = channelElement.operand;
if (topicElement != null) {
narrow = TopicNarrow(streamId, topicElement.operand, with_: withElement?.operand);
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/model/narrow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ChannelNarrow extends Narrow {
}

@override
ApiNarrow apiEncode() => [ApiNarrowStream(streamId)];
ApiNarrow apiEncode() => [ApiNarrowChannel(streamId)];

@override
String toString() => 'ChannelNarrow($streamId)';
Expand Down Expand Up @@ -117,7 +117,7 @@ class TopicNarrow extends Narrow implements SendableNarrow {

@override
ApiNarrow apiEncode() => [
ApiNarrowStream(streamId),
ApiNarrowChannel(streamId),
ApiNarrowTopic(topic),
if (with_ != null) ApiNarrowWith(with_!),
];
Expand Down
21 changes: 17 additions & 4 deletions test/api/route/messages_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ void main() {

checkNarrow(const CombinedFeedNarrow().apiEncode(), jsonEncode([]));
checkNarrow(const ChannelNarrow(12).apiEncode(), jsonEncode([
{'operator': 'stream', 'operand': 12},
{'operator': 'channel', 'operand': 12},
]));
checkNarrow(eg.topicNarrow(12, 'stuff').apiEncode(), jsonEncode([
{'operator': 'stream', 'operand': 12},
{'operator': 'channel', 'operand': 12},
{'operator': 'topic', 'operand': 'stuff'},
]));
checkNarrow(eg.topicNarrow(12, 'stuff', with_: 1).apiEncode(), jsonEncode([
{'operator': 'stream', 'operand': 12},
{'operator': 'channel', 'operand': 12},
{'operator': 'topic', 'operand': 'stuff'},
{'operator': 'with', 'operand': 1},
]));
Expand All @@ -113,7 +113,7 @@ void main() {

connection.zulipFeatureLevel = 270;
checkNarrow(eg.topicNarrow(12, 'stuff', with_: 1).apiEncode(), jsonEncode([
{'operator': 'stream', 'operand': 12},
{'operator': 'channel', 'operand': 12},
Comment on lines 115 to +116
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test change shows that the effect of this commit is a bit broader than what the commit message says:

internal_link: Generate new narrow links with "channel", not "stream"

It's also changing the get-messages requests we make to the server.

I think it's fine for that to happen in the same commit; given the way ApiNarrow works, I think splitting those changes into separate commits would require complicating the logic. But the commit message should be adjusted to make that clear.

{'operator': 'topic', 'operand': 'stuff'},
]));
checkNarrow([ApiNarrowDm([123, 234])], jsonEncode([
Expand All @@ -123,6 +123,19 @@ void main() {
{'operator': 'dm', 'operand': [123, 234]},
]));

connection.zulipFeatureLevel = 249;
checkNarrow(const ChannelNarrow(12).apiEncode(), jsonEncode([
{'operator': 'stream', 'operand': 12},
]));
checkNarrow(eg.topicNarrow(12, 'stuff').apiEncode(), jsonEncode([
{'operator': 'stream', 'operand': 12},
{'operator': 'topic', 'operand': 'stuff'},
]));
checkNarrow(eg.topicNarrow(12, 'stuff', with_: 1).apiEncode(), jsonEncode([
{'operator': 'stream', 'operand': 12},
{'operator': 'topic', 'operand': 'stuff'},
]));

connection.zulipFeatureLevel = 176;
checkNarrow(eg.topicNarrow(12, 'stuff', with_: 1).apiEncode(), jsonEncode([
{'operator': 'stream', 'operand': 12},
Expand Down
13 changes: 13 additions & 0 deletions test/model/compose_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,19 @@ hello
await store.addStream(stream);
await store.addUser(sender);

check(quoteAndReplyPlaceholder(
GlobalLocalizations.zulipLocalizations, store, message: message)).equals('''
@_**Full Name|123** [said](${eg.selfAccount.realmUrl}#narrow/channel/1-test-here/topic/some.20topic/near/${message.id}): *(loading message ${message.id})*
''');

check(quoteAndReply(store, message: message, rawContent: 'Hello world!')).equals('''
@_**Full Name|123** [said](${eg.selfAccount.realmUrl}#narrow/channel/1-test-here/topic/some.20topic/near/${message.id}):
```quote
Hello world!
```
''');

store.connection.zulipFeatureLevel = 249;
check(quoteAndReplyPlaceholder(
GlobalLocalizations.zulipLocalizations, store, message: message)).equals('''
@_**Full Name|123** [said](${eg.selfAccount.realmUrl}#narrow/stream/1-test-here/topic/some.20topic/near/${message.id}): *(loading message ${message.id})*
Expand Down
47 changes: 29 additions & 18 deletions test/model/internal_link_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,16 @@ void main() {
.equals(store.realmUrl.resolve('#narrow/is/starred/near/1'));
});

test('ChannelNarrow / TopicNarrow', () {
group('ChannelNarrow / TopicNarrow', () {
void checkNarrow(String expectedFragment, {
required int streamId,
required String name,
String? topic,
int? nearMessageId,
int? zulipFeatureLevel = eg.futureZulipFeatureLevel,
}) async {
assert(expectedFragment.startsWith('#'), 'wrong-looking expectedFragment');
final store = eg.store();
final store = eg.store()..connection.zulipFeatureLevel = zulipFeatureLevel;
await store.addStream(eg.stream(streamId: streamId, name: name));
final narrow = topic == null
? ChannelNarrow(streamId)
Expand All @@ -77,22 +78,32 @@ void main() {
.equals(store.realmUrl.resolve(expectedFragment));
}

checkNarrow(streamId: 1, name: 'announce', '#narrow/stream/1-announce');
checkNarrow(streamId: 378, name: 'api design', '#narrow/stream/378-api-design');
checkNarrow(streamId: 391, name: 'Outreachy', '#narrow/stream/391-Outreachy');
checkNarrow(streamId: 415, name: 'chat.zulip.org', '#narrow/stream/415-chat.2Ezulip.2Eorg');
checkNarrow(streamId: 419, name: 'français', '#narrow/stream/419-fran.C3.A7ais');
checkNarrow(streamId: 403, name: 'Hshs[™~}(.', '#narrow/stream/403-Hshs.5B.E2.84.A2~.7D.28.2E');
checkNarrow(streamId: 60, name: 'twitter', nearMessageId: 1570686, '#narrow/stream/60-twitter/near/1570686');

checkNarrow(streamId: 48, name: 'mobile', topic: 'Welcome screen UI',
'#narrow/stream/48-mobile/topic/Welcome.20screen.20UI');
checkNarrow(streamId: 243, name: 'mobile-team', topic: 'Podfile.lock clash #F92',
'#narrow/stream/243-mobile-team/topic/Podfile.2Elock.20clash.20.23F92');
checkNarrow(streamId: 377, name: 'translation/zh_tw', topic: '翻譯 "stream"',
'#narrow/stream/377-translation.2Fzh_tw/topic/.E7.BF.BB.E8.AD.AF.20.22stream.22');
checkNarrow(streamId: 42, name: 'Outreachy 2016-2017', topic: '2017-18 Stream?', nearMessageId: 302690,
'#narrow/stream/42-Outreachy-2016-2017/topic/2017-18.20Stream.3F/near/302690');
test('modern including "channel" operator', () {
checkNarrow(streamId: 1, name: 'announce', '#narrow/channel/1-announce');
checkNarrow(streamId: 378, name: 'api design', '#narrow/channel/378-api-design');
checkNarrow(streamId: 391, name: 'Outreachy', '#narrow/channel/391-Outreachy');
checkNarrow(streamId: 415, name: 'chat.zulip.org', '#narrow/channel/415-chat.2Ezulip.2Eorg');
checkNarrow(streamId: 419, name: 'français', '#narrow/channel/419-fran.C3.A7ais');
checkNarrow(streamId: 403, name: 'Hshs[™~}(.', '#narrow/channel/403-Hshs.5B.E2.84.A2~.7D.28.2E');
checkNarrow(streamId: 60, name: 'twitter', nearMessageId: 1570686, '#narrow/channel/60-twitter/near/1570686');

checkNarrow(streamId: 48, name: 'mobile', topic: 'Welcome screen UI',
'#narrow/channel/48-mobile/topic/Welcome.20screen.20UI');
checkNarrow(streamId: 243, name: 'mobile-team', topic: 'Podfile.lock clash #F92',
'#narrow/channel/243-mobile-team/topic/Podfile.2Elock.20clash.20.23F92');
checkNarrow(streamId: 377, name: 'translation/zh_tw', topic: '翻譯 "stream"',
'#narrow/channel/377-translation.2Fzh_tw/topic/.E7.BF.BB.E8.AD.AF.20.22stream.22');
checkNarrow(streamId: 42, name: 'Outreachy 2016-2017', topic: '2017-18 Stream?', nearMessageId: 302690,
'#narrow/channel/42-Outreachy-2016-2017/topic/2017-18.20Stream.3F/near/302690');
});

test('legacy including "stream" operator', () {
checkNarrow(streamId: 1, name: 'announce', zulipFeatureLevel: 249,
'#narrow/stream/1-announce');
checkNarrow(streamId: 48, name: 'mobile-team', topic: 'Welcome screen UI',
zulipFeatureLevel: 249,
'#narrow/stream/48-mobile-team/topic/Welcome.20screen.20UI');
});
});

test('DmNarrow', () {
Expand Down
2 changes: 1 addition & 1 deletion test/model/message_list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void main() {
..method.equals('GET')
..url.path.equals('/api/v1/messages')
..url.queryParameters.deepEquals({
'narrow': jsonEncode(narrow),
'narrow': jsonEncode(resolveApiNarrowForServer(narrow, connection.zulipFeatureLevel!)),
'anchor': anchor,
if (includeAnchor != null) 'include_anchor': includeAnchor.toString(),
'num_before': numBefore.toString(),
Expand Down
14 changes: 10 additions & 4 deletions test/widgets/action_sheet_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ void main() {
'num_before': '0',
'num_after': '1000',
'narrow': jsonEncode([
{'operator': 'stream', 'operand': channelId},
{'operator': 'channel', 'operand': channelId},
{'operator': 'is', 'operand': 'unread'},
]),
'op': 'add',
Expand Down Expand Up @@ -1014,7 +1014,9 @@ void main() {
check(connection.lastRequest).isA<http.Request>()
..url.path.equals('/api/v1/messages/flags/narrow')
..bodyFields['narrow'].equals(jsonEncode([
...eg.topicNarrow(someChannel.streamId, someTopic).apiEncode(),
...resolveApiNarrowForServer(
eg.topicNarrow(someChannel.streamId, someTopic).apiEncode(),
connection.zulipFeatureLevel!),
ApiNarrowIs(IsOperand.unread),
]))
..bodyFields['op'].equals('add')
Expand Down Expand Up @@ -1656,7 +1658,9 @@ void main() {
'include_anchor': 'true',
'num_before': '0',
'num_after': '1000',
'narrow': jsonEncode(TopicNarrow.ofMessage(message).apiEncode()),
'narrow': jsonEncode(resolveApiNarrowForServer(
TopicNarrow.ofMessage(message).apiEncode(),
connection.zulipFeatureLevel!)),
'op': 'remove',
'flag': 'read',
});
Expand Down Expand Up @@ -1701,7 +1705,9 @@ void main() {
..method.equals('POST')
..url.path.equals('/api/v1/messages/flags/narrow')
..bodyFields['narrow'].equals(
jsonEncode(eg.topicNarrow(newStream.streamId, newTopic).apiEncode()));
jsonEncode(resolveApiNarrowForServer(
eg.topicNarrow(newStream.streamId, newTopic).apiEncode(),
connection.zulipFeatureLevel!)));
});

testWidgets('shows error when fails', (tester) async {
Expand Down
10 changes: 5 additions & 5 deletions test/widgets/actions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void main() {
'include_anchor': 'false',
'num_before': '0',
'num_after': '1000',
'narrow': jsonEncode(apiNarrow),
'narrow': jsonEncode(resolveApiNarrowForServer(apiNarrow, connection.zulipFeatureLevel!)),
'op': 'add',
'flag': 'read',
});
Expand Down Expand Up @@ -155,7 +155,7 @@ void main() {
'include_anchor': 'false',
'num_before': '0',
'num_after': '1000',
'narrow': jsonEncode(apiNarrow),
'narrow': jsonEncode(resolveApiNarrowForServer(apiNarrow, connection.zulipFeatureLevel!)),
'op': 'add',
'flag': 'read',
});
Expand All @@ -180,7 +180,7 @@ void main() {
'include_anchor': 'false',
'num_before': '0',
'num_after': '1000',
'narrow': jsonEncode(apiNarrow),
'narrow': jsonEncode(resolveApiNarrowForServer(apiNarrow, connection.zulipFeatureLevel!)),
'op': 'add',
'flag': 'read',
});
Expand All @@ -199,7 +199,7 @@ void main() {
'include_anchor': 'false',
'num_before': '0',
'num_after': '1000',
'narrow': jsonEncode(apiNarrow),
'narrow': jsonEncode(resolveApiNarrowForServer(apiNarrow, connection.zulipFeatureLevel!)),
'op': 'add',
'flag': 'read',
});
Expand All @@ -223,7 +223,7 @@ void main() {
'include_anchor': 'false',
'num_before': '0',
'num_after': '1000',
'narrow': jsonEncode(apiNarrow),
'narrow': jsonEncode(resolveApiNarrowForServer(apiNarrow, connection.zulipFeatureLevel!)),
'op': 'add',
'flag': 'read',
});
Expand Down
Loading