Skip to content

Commit

Permalink
api: Add unregister{Apns,Fcm}Token; link to new docs for register-tok…
Browse files Browse the repository at this point in the history
…en fns

It's no longer true that these endpoints are undocumented, so, link
to them.
  • Loading branch information
chrisbobbe committed Oct 12, 2024
1 parent 1147845 commit e20fa5e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
30 changes: 20 additions & 10 deletions lib/api/route/notifications.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@

import '../core.dart';

// This endpoint is undocumented. Compare zulip-mobile:
// https://github.com/zulip/zulip-mobile/blob/86d94fa89/src/api/notifications/savePushToken.js
// and see the server implementation:
// https://github.com/zulip/zulip/blob/34ceafadd/zproject/urls.py#L383
// https://github.com/zulip/zulip/blob/34ceafadd/zerver/views/push_notifications.py#L47
/// https://zulip.com/api/add-fcm-token
Future<void> registerFcmToken(ApiConnection connection, {
required String token,
}) {
Expand All @@ -14,11 +10,16 @@ Future<void> registerFcmToken(ApiConnection connection, {
});
}

// This endpoint is undocumented. Compare zulip-mobile:
// https://github.com/zulip/zulip-mobile/blob/86d94fa89/src/api/notifications/savePushToken.js
// and see the server implementation:
// https://github.com/zulip/zulip/blob/34ceafadd/zproject/urls.py#L378-L381
// https://github.com/zulip/zulip/blob/34ceafadd/zerver/views/push_notifications.py#L34
/// https://zulip.com/api/remove-fcm-token
Future<void> unregisterFcmToken(ApiConnection connection, {
required String token,
}) {
return connection.delete('unregisterFcmToken', (_) {}, 'users/me/android_gcm_reg_id', {
'token': RawParameter(token),
});
}

/// https://zulip.com/api/add-apns-token
Future<void> registerApnsToken(ApiConnection connection, {
required String token,
String? appid,
Expand All @@ -28,3 +29,12 @@ Future<void> registerApnsToken(ApiConnection connection, {
if (appid != null) 'appid': RawParameter(appid),
});
}

/// https://zulip.com/api/remove-apns-token
Future<void> unregisterApnsToken(ApiConnection connection, {
required String token,
}) {
return connection.delete('unregisterApnsToken', (_) {}, 'users/me/apns_device_token', {
'token': RawParameter(token),
});
}
42 changes: 42 additions & 0 deletions test/api/route/notifications_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ void main() {
});
});

group('unregisterFcmToken', () {
Future<void> checkUnregisterFcmToken(FakeApiConnection connection, {
required String token,
}) async {
connection.prepare(json: {});
await unregisterFcmToken(connection, token: token);
check(connection.lastRequest).isA<http.Request>()
..method.equals('DELETE')
..url.path.equals('/api/v1/users/me/android_gcm_reg_id')
..bodyFields.deepEquals({
'token': token,
});
}

test('smoke', () {
return FakeApiConnection.with_((connection) async {
await checkUnregisterFcmToken(connection, token: 'asdf');
});
});
});

group('registerApnsToken', () {
Future<void> checkRegisterApnsToken(FakeApiConnection connection, {
required String token,
Expand Down Expand Up @@ -56,4 +77,25 @@ void main() {
});
});
});

group('unregisterApnsToken', () {
Future<void> checkUnregisterApnsToken(FakeApiConnection connection, {
required String token,
}) async {
connection.prepare(json: {});
await unregisterApnsToken(connection, token: token);
check(connection.lastRequest).isA<http.Request>()
..method.equals('DELETE')
..url.path.equals('/api/v1/users/me/apns_device_token')
..bodyFields.deepEquals({
'token': token,
});
}

test('smoke', () {
return FakeApiConnection.with_((connection) async {
await checkUnregisterApnsToken(connection, token: 'asdf');
});
});
});
}

0 comments on commit e20fa5e

Please sign in to comment.