Skip to content

Commit 8108762

Browse files
committed
api: Add updateSettings route
1 parent af95d11 commit 8108762

File tree

6 files changed

+70
-2
lines changed

6 files changed

+70
-2
lines changed

lib/api/model/events.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/api/model/initial_snapshot.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ class UserSettings {
257257
// (1) add it to the [UserSettingName] enum
258258
// (2) then re-run the command to refresh the .g.dart files
259259
// (3) handle the event that signals an update to the setting
260+
// (4) add the setting to the [updateSettings] route binding
260261

261262
UserSettings({
262263
required this.twentyFourHourTime,

lib/api/model/initial_snapshot.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/api/model/model.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ enum UserSettingName {
180180
// _$…EnumMap is thanks to `alwaysCreate: true` and `fieldRename: FieldRename.snake`
181181
static final _byRawString = _$UserSettingNameEnumMap
182182
.map((key, value) => MapEntry(value, key));
183+
184+
String toJson() => _$UserSettingNameEnumMap[this]!;
183185
}
184186

185187
/// As in [UserSettings.emojiset].
@@ -199,6 +201,8 @@ enum Emojiset {
199201
// _$…EnumMap is thanks to `alwaysCreate: true` and `fieldRename: FieldRename.kebab`
200202
static final _byRawString = _$EmojisetEnumMap
201203
.map((key, value) => MapEntry(value, key));
204+
205+
String toJson() => _$EmojisetEnumMap[this]!;
202206
}
203207

204208
/// As in [InitialSnapshot.realmUserGroups] or [UserGroupAddEvent].

lib/api/route/settings.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import '../core.dart';
2+
import '../model/model.dart';
3+
4+
/// https://zulip.com/api/update-settings
5+
Future<void> updateSettings(ApiConnection connection, {
6+
required Map<UserSettingName, Object?> newSettings,
7+
}) {
8+
final params = <String, Object?>{};
9+
for (final entry in newSettings.entries) {
10+
final name = entry.key;
11+
final valueRaw = entry.value;
12+
final value = switch (name) {
13+
UserSettingName.twentyFourHourTime => valueRaw as bool,
14+
UserSettingName.displayEmojiReactionUsers => valueRaw as bool,
15+
UserSettingName.emojiset => RawParameter((valueRaw as Emojiset).toJson()),
16+
UserSettingName.presenceEnabled => valueRaw as bool,
17+
};
18+
params[name.toJson()] = value;
19+
}
20+
21+
return connection.patch('updateSettings', (_) {}, 'settings', params);
22+
}

test/api/route/settings_test.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'package:checks/checks.dart';
2+
import 'package:http/http.dart' as http;
3+
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:zulip/api/model/model.dart';
5+
import 'package:zulip/api/route/settings.dart';
6+
7+
import '../../stdlib_checks.dart';
8+
import '../fake_api.dart';
9+
10+
void main() {
11+
test('smoke updateSettings', () {
12+
return FakeApiConnection.with_((connection) async {
13+
connection.prepare(json: {});
14+
15+
final newSettings = <UserSettingName, Object?>{};
16+
final expectedBodyFields = <String, String>{};
17+
for (final name in UserSettingName.values) {
18+
switch (name) {
19+
case UserSettingName.twentyFourHourTime:
20+
newSettings[name] = true;
21+
expectedBodyFields['twenty_four_hour_time'] = 'true';
22+
case UserSettingName.displayEmojiReactionUsers:
23+
newSettings[name] = false;
24+
expectedBodyFields['display_emoji_reaction_users'] = 'false';
25+
case UserSettingName.emojiset:
26+
newSettings[name] = Emojiset.googleBlob;
27+
expectedBodyFields['emojiset'] = 'google-blob';
28+
case UserSettingName.presenceEnabled:
29+
newSettings[name] = true;
30+
expectedBodyFields['presence_enabled'] = 'true';
31+
}
32+
}
33+
34+
await updateSettings(connection, newSettings: newSettings);
35+
check(connection.takeRequests()).single.isA<http.Request>()
36+
..method.equals('PATCH')
37+
..url.path.equals('/api/v1/settings')
38+
..bodyFields.deepEquals(expectedBodyFields);
39+
});
40+
});
41+
}

0 commit comments

Comments
 (0)