Skip to content

Commit

Permalink
internal_links: Parse "/is/mentioned" internal links.
Browse files Browse the repository at this point in the history
As of now, we don't have generic for the "is" operator, but it will be a
easy refactor once we do.

Fixes #250.

Signed-off-by: Zixuan James Li <[email protected]>
  • Loading branch information
PIG208 committed Jul 29, 2024
1 parent 4fdc680 commit c769b23
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/model/internal_link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ Narrow? _interpretNarrowSegments(List<String> segments, PerAccountStore store) {
ApiNarrowStream? streamElement;
ApiNarrowTopic? topicElement;
ApiNarrowDm? dmElement;
ApiNarrowIsMentioned? isMentionedElement;

for (var i = 0; i < segments.length; i += 2) {
final (operator, negated) = _parseOperator(segments[i]);
Expand Down Expand Up @@ -181,6 +182,10 @@ Narrow? _interpretNarrowSegments(List<String> segments, PerAccountStore store) {
if (dmIds == null) return null;
dmElement = ApiNarrowDm(dmIds, negated: negated);

case _NarrowOperator.is_:
if (isMentionedElement != null) return null;
if (operand == 'mentioned') isMentionedElement = ApiNarrowIsMentioned();

case _NarrowOperator.near: // TODO(#82): support for near
case _NarrowOperator.with_: // TODO(#683): support for with
continue;
Expand All @@ -190,7 +195,10 @@ Narrow? _interpretNarrowSegments(List<String> segments, PerAccountStore store) {
}
}

if (dmElement != null) {
if (isMentionedElement != null) {
if (streamElement != null || topicElement != null || dmElement != null) return null;
return const MentionsNarrow();
} else if (dmElement != null) {
if (streamElement != null || topicElement != null) return null;
return DmNarrow.withUsers(dmElement.operand, selfUserId: store.selfUserId);
} else if (streamElement != null) {
Expand All @@ -212,6 +220,9 @@ enum _NarrowOperator {
// cannot use `with` as it is a reserved keyword in Dart
@JsonValue('with')
with_,
// cannot use `is` as it is a reserved keyword in Dart
@JsonValue('is')
is_,
pmWith,
stream,
channel,
Expand Down
1 change: 1 addition & 0 deletions lib/model/internal_link.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions test/model/internal_link_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,20 @@ void main() {
testExpectedNarrows(testCases, streams: streams);
});

group('"/#narrow/is/mentioned returns expected MentionsNarrow', () {
final testCases = [
('/#narrow/is/mentioned', const MentionsNarrow()),
('/#narrow/is/mentioned/near/1', const MentionsNarrow()),
('/#narrow/is/mentioned/with/2', const MentionsNarrow()),
('/#narrow/channel/7-test-here/is/mentioned', null),
('/#narrow/channel/check/topic/test/is/mentioned', null),
('/#narrow/topic/test/is/mentioned', null),
('/#narrow/dm/17327-Chris-Bobbe-(Test-Account)/is/mentioned', null),
('/#narrow/-is/mentioned', null),
];
testExpectedNarrows(testCases, streams: streams);
});

group('unexpected link shapes are rejected', () {
final testCases = [
('/#narrow/stream/name/topic/', null), // missing operand
Expand Down

0 comments on commit c769b23

Please sign in to comment.