Skip to content

Commit

Permalink
api: Indicate support for handling empty topics
Browse files Browse the repository at this point in the history
Signed-off-by: Zixuan James Li <[email protected]>
  • Loading branch information
PIG208 committed Jan 29, 2025
1 parent 0b2d6c8 commit b38c9bc
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/api/route/channels.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ part 'channels.g.dart';
Future<GetStreamTopicsResult> getStreamTopics(ApiConnection connection, {
required int streamId,
}) {
return connection.get('getStreamTopics', GetStreamTopicsResult.fromJson, 'users/me/$streamId/topics', {});
final supportsEmptyTopics = connection.zulipFeatureLevel! >= 334; // TODO(server-10)
return connection.get('getStreamTopics', GetStreamTopicsResult.fromJson, 'users/me/$streamId/topics', {
if (supportsEmptyTopics) 'allow_empty_topic_name': true,
});
}

@JsonSerializable(fieldRename: FieldRename.snake)
Expand Down
1 change: 1 addition & 0 deletions lib/api/route/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Future<InitialSnapshot> registerQueue(ApiConnection connection) {
'user_avatar_url_field_optional': false, // TODO(#254): turn on
'stream_typing_notifications': true,
'user_settings_object': true,
'empty_topic_name': true,
},
});
}
Expand Down
4 changes: 4 additions & 0 deletions lib/api/route/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ Future<GetMessageResult> getMessage(ApiConnection connection, {
bool? applyMarkdown,
}) {
assert(connection.zulipFeatureLevel! >= 120);
final supportsEmptyTopics = connection.zulipFeatureLevel! >= 334; // TODO(server-10)
return connection.get('getMessage', GetMessageResult.fromJson, 'messages/$messageId', {
if (applyMarkdown != null) 'apply_markdown': applyMarkdown,
if (supportsEmptyTopics) 'allow_empty_topic_name': true,
});
}

Expand Down Expand Up @@ -90,6 +92,7 @@ Future<GetMessagesResult> getMessages(ApiConnection connection, {
bool? applyMarkdown,
// bool? useFirstUnreadAnchor // omitted because deprecated
}) {
final supportsEmptyTopics = connection.zulipFeatureLevel! >= 334; // TODO(server-10)
return connection.get('getMessages', GetMessagesResult.fromJson, 'messages', {
'narrow': resolveDmElements(narrow, connection.zulipFeatureLevel!),
'anchor': RawParameter(anchor.toJson()),
Expand All @@ -98,6 +101,7 @@ Future<GetMessagesResult> getMessages(ApiConnection connection, {
'num_after': numAfter,
if (clientGravatar != null) 'client_gravatar': clientGravatar,
if (applyMarkdown != null) 'apply_markdown': applyMarkdown,
if (supportsEmptyTopics) 'allow_empty_topic_name': true,
});
}

Expand Down
24 changes: 24 additions & 0 deletions test/api/route/channels_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@ import '../../stdlib_checks.dart';
import '../fake_api.dart';

void main() {
test('smoke getStreamTopics', () {
return FakeApiConnection.with_((connection) async {
connection.prepare(json: GetStreamTopicsResult(topics: []).toJson());
await getStreamTopics(connection, streamId: 1);
check(connection.takeRequests()).single.isA<http.Request>()
..method.equals('GET')
..url.path.equals('/api/v1/users/me/1/topics')
..url.queryParameters.deepEquals({
'allow_empty_topic_name': 'true',
});
});
});

test('legacy: getStreamTopics when FL < 334', () {
return FakeApiConnection.with_(zulipFeatureLevel: 333, (connection) async {
connection.prepare(json: GetStreamTopicsResult(topics: []).toJson());
await getStreamTopics(connection, streamId: 1);
check(connection.takeRequests()).single.isA<http.Request>()
..method.equals('GET')
..url.path.equals('/api/v1/users/me/1/topics')
..url.queryParameters.deepEquals({});
});
});

test('smoke updateUserTopic', () {
return FakeApiConnection.with_((connection) async {
connection.prepare(json: {});
Expand Down
37 changes: 35 additions & 2 deletions test/api/route/messages_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void main() {
..url.path.equals('/api/v1/messages/$messageId')
..url.queryParameters.deepEquals({
if (applyMarkdown != null) 'apply_markdown': applyMarkdown.toString(),
'allow_empty_topic_name': 'true',
});
}
return result;
Expand Down Expand Up @@ -149,7 +150,10 @@ void main() {
await checkGetMessage(connection,
messageId: 1,
applyMarkdown: true,
expected: {'apply_markdown': 'true'});
expected: {
'apply_markdown': 'true',
'allow_empty_topic_name': 'true',
});
});
});

Expand All @@ -159,7 +163,19 @@ void main() {
await checkGetMessage(connection,
messageId: 1,
applyMarkdown: false,
expected: {'apply_markdown': 'false'});
expected: {
'apply_markdown': 'false',
'allow_empty_topic_name': 'true',
});
});
});

test('legacy: empty topic name not supported', () {
return FakeApiConnection.with_(zulipFeatureLevel: 333, (connection) async {
connection.prepare(json: fakeResult.toJson());
await checkGetMessage(connection,
messageId: 1,
expected: {});
});
});

Expand Down Expand Up @@ -259,6 +275,7 @@ void main() {
'anchor': 'newest',
'num_before': '10',
'num_after': '20',
'allow_empty_topic_name': 'true',
});
});
});
Expand Down Expand Up @@ -292,6 +309,22 @@ void main() {
'anchor': '42',
'num_before': '10',
'num_after': '20',
'allow_empty_topic_name': 'true',
});
});
});

test('legacy: empty topic name not supported', () {
return FakeApiConnection.with_(zulipFeatureLevel: 333, (connection) async {
connection.prepare(json: fakeResult.toJson());
await checkGetMessages(connection,
narrow: const CombinedFeedNarrow().apiEncode(),
anchor: AnchorCode.newest, numBefore: 10, numAfter: 20,
expected: {
'narrow': jsonEncode([]),
'anchor': 'newest',
'num_before': '10',
'num_after': '20',
});
});
});
Expand Down

0 comments on commit b38c9bc

Please sign in to comment.