feat: add encrypted push

This commit is contained in:
Sorunome 2021-10-28 11:14:47 +02:00
parent 81164316b5
commit ff19deb7fd
No known key found for this signature in database
GPG Key ID: B19471D07FC9BE9C
3 changed files with 32 additions and 80 deletions

View File

@ -131,84 +131,34 @@ class BackgroundPush {
await _fcmSharedIsolate?.requestPermission();
}
final clientName = PlatformInfos.clientName;
oldTokens ??= <String>{};
final pushers = await client.getPushers().catchError((e) {
Logs().w('[Push] Unable to request pushers', e);
return <Pusher>[];
});
var setNewPusher = false;
// Just the plain app id, we add the .data_message suffix later
var appId = AppConfig.pushNotificationsAppId;
// we need the deviceAppId to remove potential legacy UP pusher
var deviceAppId = '$appId.${client.deviceID}';
// appId may only be up to 64 chars as per spec
if (deviceAppId.length > 64) {
deviceAppId = deviceAppId.substring(0, 64);
final maxLength = 64 - '.data_message'.length;
if (deviceAppId.length > maxLength) {
deviceAppId = deviceAppId.substring(0, maxLength);
}
if (!useDeviceSpecificAppId && PlatformInfos.isAndroid) {
appId += '.data_message';
}
final thisAppId = useDeviceSpecificAppId ? deviceAppId : appId;
if (gatewayUrl != null && token != null && clientName != null) {
final currentPushers = pushers.where((pusher) => pusher.pushkey == token);
if (currentPushers.length == 1 &&
currentPushers.first.kind == 'http' &&
currentPushers.first.appId == thisAppId &&
currentPushers.first.appDisplayName == clientName &&
currentPushers.first.deviceDisplayName == client.deviceName &&
currentPushers.first.lang == 'en' &&
currentPushers.first.data.url.toString() == gatewayUrl &&
currentPushers.first.data.format ==
AppConfig.pushNotificationsPusherFormat) {
Logs().i('[Push] Pusher already set');
} else {
Logs().i('Need to set new pusher');
oldTokens.add(token);
if (client.isLogged()) {
setNewPusher = true;
}
}
} else {
Logs().w('[Push] Missing required push credentials');
}
for (final pusher in pushers) {
if ((token != null &&
pusher.pushkey != token &&
deviceAppId == pusher.appId) ||
oldTokens.contains(pusher.pushkey)) {
pusher.kind = null;
try {
await client.postPusher(
pusher,
append: true,
);
Logs().i('[Push] Removed legacy pusher for this device');
} catch (err) {
Logs().w('[Push] Failed to remove old pusher', err);
}
}
}
if (setNewPusher) {
try {
await client.postPusher(
Pusher(
pushkey: token,
appId: thisAppId,
appDisplayName: clientName,
deviceDisplayName: client.deviceName,
lang: 'en',
data: PusherData(
url: Uri.parse(gatewayUrl),
format: AppConfig.pushNotificationsPusherFormat,
),
kind: 'http',
),
append: false,
);
} catch (e, s) {
Logs().e('[Push] Unable to set pushers', e, s);
}
}
await client.setupPusher(
oldTokens: oldTokens,
pusher: Pusher(
pushkey: token,
appId: thisAppId,
appDisplayName: clientName,
deviceDisplayName: client.deviceName,
lang: 'en',
data: PusherData(
url: Uri.parse(gatewayUrl),
format: AppConfig.pushNotificationsPusherFormat,
),
kind: 'http',
),
);
}
bool _wentToRoomOnStartup = false;
@ -325,7 +275,7 @@ class BackgroundPush {
Logs().v('[Push] Foreground message received');
Map<String, dynamic> data;
try {
data = Map<String, dynamic>.from(message['data'] ?? message);
data = await client.processPushPayload(Map<String, dynamic>.from(message));
await _onMessage(data);
} catch (e, s) {
Logs().e('[Push] Error while processing notification', e, s);
@ -392,7 +342,7 @@ class BackgroundPush {
Future<void> _onUpMessage(String message) async {
Map<String, dynamic> data;
try {
data = Map<String, dynamic>.from(json.decode(message)['notification']);
data = await client.processPushPayload(json.decode(message));
await _onMessage(data);
} catch (e, s) {
Logs().e('[Push] Error while processing notification', e, s);

View File

@ -815,17 +815,17 @@ packages:
matrix:
dependency: "direct main"
description:
name: matrix
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.0"
path: "/home/sorunome/repos/famedly/famedlysdk"
relative: false
source: path
version: "0.6.2"
matrix_api_lite:
dependency: transitive
description:
name: matrix_api_lite
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.3"
path: "/home/sorunome/repos/famedly/frontend/libraries/matrix_api_lite"
relative: false
source: path
version: "0.4.4"
matrix_link_text:
dependency: "direct main"
description:

View File

@ -48,7 +48,9 @@ dependencies:
intl: any
localstorage: ^4.0.0+1
lottie: ^1.2.1
matrix: ^0.7.0
#matrix: ^0.7.0
matrix:
path: /home/sorunome/repos/famedly/famedlysdk
matrix_link_text: ^1.0.2
open_noti_settings: ^0.3.0
package_info_plus: ^1.2.1
@ -113,4 +115,4 @@ dependency_overrides:
hosted:
name: geolocator_android
url: https://hanntech-gmbh.gitlab.io/free2pass/flutter-geolocator-floss
provider: 5.0.0
provider: 5.0.0