Skip to content

Commit 44d8167

Browse files
committed
api: Add updatePresence
1 parent 66b648a commit 44d8167

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

lib/api/model/model.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:json_annotation/json_annotation.dart';
22

33
import '../../model/algorithms.dart';
4+
import '../route/users.dart';
45
import 'events.dart';
56
import 'initial_snapshot.dart';
67
import 'reaction.dart';
@@ -350,7 +351,7 @@ class PerUserPresence {
350351
Map<String, dynamic> toJson() => _$PerUserPresenceToJson(this);
351352
}
352353

353-
/// As in [PerClientPresence.status].
354+
/// As in [PerClientPresence.status] and [updatePresence].
354355
@JsonEnum(fieldRename: FieldRename.snake, alwaysCreate: true)
355356
enum PresenceStatus {
356357
active,

lib/api/route/users.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:json_annotation/json_annotation.dart';
22

33
import '../core.dart';
4+
import '../model/model.dart';
45

56
part 'users.g.dart';
67

@@ -32,3 +33,46 @@ class GetOwnUserResult {
3233

3334
Map<String, dynamic> toJson() => _$GetOwnUserResultToJson(this);
3435
}
36+
37+
/// https://zulip.com/api/update-presence
38+
///
39+
/// Passes true for `slim_presence` to avoid getting an ancient data format
40+
/// in the response.
41+
// TODO(#1611) Passing `slim_presence` is the old, deprecated way to avoid
42+
// getting an ancient data format. Pass `last_update_id` to new servers to get
43+
// that effect (make lastUpdateId required?) and update the dartdoc.
44+
Future<UpdatePresenceResult> updatePresence(ApiConnection connection, {
45+
int? lastUpdateId,
46+
int? historyLimitDays,
47+
bool? newUserInput,
48+
bool? pingOnly,
49+
required PresenceStatus status,
50+
}) {
51+
return connection.post('updatePresence', UpdatePresenceResult.fromJson, 'users/me/presence', {
52+
if (lastUpdateId != null) 'last_update_id': lastUpdateId,
53+
if (historyLimitDays != null) 'history_limit_days': historyLimitDays,
54+
if (newUserInput != null) 'new_user_input': newUserInput,
55+
if (pingOnly != null) 'ping_only': pingOnly,
56+
'status': RawParameter(status.toJson()),
57+
'slim_presence': true,
58+
});
59+
}
60+
61+
@JsonSerializable(fieldRename: FieldRename.snake)
62+
class UpdatePresenceResult {
63+
final int? presenceLastUpdateId; // TODO(server-9.0) new in FL 263
64+
final double? serverTimestamp; // 1656958539.6287155 in the example response
65+
final Map<int, PerUserPresence>? presences;
66+
// final bool zephyrMirrorActive; // deprecated, ignore
67+
68+
UpdatePresenceResult({
69+
required this.presenceLastUpdateId,
70+
required this.serverTimestamp,
71+
required this.presences,
72+
});
73+
74+
factory UpdatePresenceResult.fromJson(Map<String, dynamic> json) =>
75+
_$UpdatePresenceResultFromJson(json);
76+
77+
Map<String, dynamic> toJson() => _$UpdatePresenceResultToJson(this);
78+
}

lib/api/route/users.g.dart

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/api/route/users_test.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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/users.dart';
6+
7+
import '../../stdlib_checks.dart';
8+
import '../fake_api.dart';
9+
10+
void main() {
11+
test('smoke updatePresence', () {
12+
return FakeApiConnection.with_((connection) async {
13+
final response = UpdatePresenceResult(
14+
presenceLastUpdateId: -1,
15+
serverTimestamp: 1656958539.6287155,
16+
presences: {},
17+
);
18+
connection.prepare(json: response.toJson());
19+
await updatePresence(connection,
20+
lastUpdateId: -1,
21+
historyLimitDays: 21,
22+
newUserInput: false,
23+
pingOnly: false,
24+
status: PresenceStatus.active,
25+
);
26+
check(connection.takeRequests()).single.isA<http.Request>()
27+
..method.equals('POST')
28+
..url.path.equals('/api/v1/users/me/presence')
29+
..bodyFields.deepEquals({
30+
'last_update_id': '-1',
31+
'history_limit_days': '21',
32+
'new_user_input': 'false',
33+
'ping_only': 'false',
34+
'status': 'active',
35+
'slim_presence': 'true',
36+
});
37+
});
38+
});
39+
}

0 commit comments

Comments
 (0)