mirror of
https://github.com/nextcloud/talk-android
synced 2025-06-19 19:49:33 +01:00
refactoring PushUtils
Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
parent
a7c298b224
commit
412af46ed5
@ -230,11 +230,11 @@ public class PushUtils {
|
||||
String pushTokenHash = generateSHA512Hash(token).toLowerCase();
|
||||
PublicKey devicePublicKey = (PublicKey) readKeyFromFile(true);
|
||||
if (devicePublicKey != null) {
|
||||
byte[] publicKeyBytes = Base64.encode(devicePublicKey.getEncoded(), Base64.NO_WRAP);
|
||||
String publicKey = new String(publicKeyBytes);
|
||||
publicKey = publicKey.replaceAll("(.{64})", "$1\n");
|
||||
byte[] devicePublicKeyBytes = Base64.encode(devicePublicKey.getEncoded(), Base64.NO_WRAP);
|
||||
String devicePublicKeyBase64 = new String(devicePublicKeyBytes);
|
||||
devicePublicKeyBase64 = devicePublicKeyBase64.replaceAll("(.{64})", "$1\n");
|
||||
|
||||
publicKey = "-----BEGIN PUBLIC KEY-----\n" + publicKey + "\n-----END PUBLIC KEY-----\n";
|
||||
devicePublicKeyBase64 = "-----BEGIN PUBLIC KEY-----\n" + devicePublicKeyBase64 + "\n-----END PUBLIC KEY-----\n";
|
||||
|
||||
if (userUtils.anyUserExists()) {
|
||||
String providerValue;
|
||||
@ -253,20 +253,32 @@ public class PushUtils {
|
||||
accountPushData = null;
|
||||
}
|
||||
|
||||
if (((TextUtils.isEmpty(providerValue) || accountPushData == null) && !userEntity.getScheduledForDeletion()) ||
|
||||
if (((TextUtils.isEmpty(providerValue) || accountPushData == null)
|
||||
&& !userEntity.getScheduledForDeletion()) ||
|
||||
(accountPushData != null && !accountPushData.getPushToken().equals(token) && !userEntity.getScheduledForDeletion())) {
|
||||
|
||||
Map<String, String> queryMap = new HashMap<>();
|
||||
queryMap.put("format", "json");
|
||||
queryMap.put("pushTokenHash", pushTokenHash);
|
||||
queryMap.put("devicePublicKey", publicKey);
|
||||
queryMap.put("proxyServer", proxyServer);
|
||||
Map<String, String> nextcloudRegisterPushMap = new HashMap<>();
|
||||
nextcloudRegisterPushMap.put("format", "json");
|
||||
nextcloudRegisterPushMap.put("pushTokenHash", pushTokenHash);
|
||||
nextcloudRegisterPushMap.put("devicePublicKey", devicePublicKeyBase64);
|
||||
nextcloudRegisterPushMap.put("proxyServer", proxyServer);
|
||||
|
||||
credentials = ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken());
|
||||
registerDeviceWithNextcloud(nextcloudRegisterPushMap, token, userEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Log.e(TAG, "push token was empty when trying to register at nextcloud server");
|
||||
}
|
||||
}
|
||||
|
||||
private void registerDeviceWithNextcloud(Map<String, String> nextcloudRegisterPushMap, String token, UserEntity userEntity) {
|
||||
String credentials = ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken());
|
||||
|
||||
ncApi.registerDeviceForNotificationsWithNextcloud(
|
||||
credentials,
|
||||
ApiUtils.getUrlNextcloudPush(userEntity.getBaseUrl()), queryMap)
|
||||
ApiUtils.getUrlNextcloudPush(userEntity.getBaseUrl()), nextcloudRegisterPushMap)
|
||||
.subscribe(new Observer<PushRegistrationOverall>() {
|
||||
@Override
|
||||
public void onSubscribe(@NonNull Disposable d) {
|
||||
@ -284,7 +296,7 @@ public class PushUtils {
|
||||
proxyMap.put("userPublicKey", pushRegistrationOverall.getOcs()
|
||||
.getData().getPublicKey());
|
||||
|
||||
registerDeviceWithPushProxy(pushRegistrationOverall, proxyMap, token, userEntity);
|
||||
registerDeviceWithPushProxy(proxyMap, userEntity);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -299,21 +311,9 @@ public class PushUtils {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Log.e(TAG, "push token was empty when trying to register at nextcloud server");
|
||||
}
|
||||
}
|
||||
|
||||
private void registerDeviceWithPushProxy(
|
||||
@androidx.annotation.NonNull PushRegistrationOverall pushRegistrationOverall,
|
||||
Map<String, String> proxyMap,
|
||||
String token,
|
||||
UserEntity userEntity) {
|
||||
ncApi.registerDeviceForNotificationsWithPushProxy(
|
||||
ApiUtils.getUrlPushProxy(), proxyMap)
|
||||
private void registerDeviceWithPushProxy(Map<String, String> proxyMap, UserEntity userEntity) {
|
||||
ncApi.registerDeviceForNotificationsWithPushProxy(ApiUtils.getUrlPushProxy(), proxyMap)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe(new Observer<Void>() {
|
||||
@Override
|
||||
@ -323,26 +323,45 @@ public class PushUtils {
|
||||
|
||||
@Override
|
||||
public void onNext(@NonNull Void aVoid) {
|
||||
PushConfigurationState pushConfigurationState =
|
||||
new PushConfigurationState();
|
||||
pushConfigurationState.setPushToken(token);
|
||||
pushConfigurationState.setDeviceIdentifier(
|
||||
pushRegistrationOverall.getOcs()
|
||||
.getData().getDeviceIdentifier());
|
||||
pushConfigurationState.setDeviceIdentifierSignature(
|
||||
pushRegistrationOverall
|
||||
.getOcs().getData().getSignature());
|
||||
pushConfigurationState.setUserPublicKey(
|
||||
pushRegistrationOverall.getOcs()
|
||||
.getData().getPublicKey());
|
||||
try {
|
||||
createOrUpdateUser(proxyMap, userEntity);
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "IOException while updating user", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(@NonNull Throwable e) {
|
||||
eventBus.post(new EventStatus(userEntity.getId(),
|
||||
EventStatus.EventType.PUSH_REGISTRATION, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
// unused atm
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void createOrUpdateUser(Map<String, String> proxyMap, UserEntity userEntity) throws IOException {
|
||||
PushConfigurationState pushConfigurationState = new PushConfigurationState();
|
||||
pushConfigurationState.setPushToken(proxyMap.get("pushToken"));
|
||||
pushConfigurationState.setDeviceIdentifier(proxyMap.get("deviceIdentifier"));
|
||||
pushConfigurationState.setDeviceIdentifierSignature(proxyMap.get("deviceIdentifierSignature"));
|
||||
pushConfigurationState.setUserPublicKey(proxyMap.get("userPublicKey"));
|
||||
pushConfigurationState.setUsesRegularPass(false);
|
||||
|
||||
try {
|
||||
userUtils.createOrUpdateUser(null,
|
||||
null, null,
|
||||
null,
|
||||
null,
|
||||
userEntity.getDisplayName(),
|
||||
LoganSquare.serialize(pushConfigurationState), null,
|
||||
null, userEntity.getId(), null, null, null)
|
||||
LoganSquare.serialize(pushConfigurationState),
|
||||
null,
|
||||
null,
|
||||
userEntity.getId(),
|
||||
null,
|
||||
null,
|
||||
null)
|
||||
.subscribe(new Observer<UserEntity>() {
|
||||
@Override
|
||||
public void onSubscribe(@NonNull Disposable d) {
|
||||
@ -356,26 +375,7 @@ public class PushUtils {
|
||||
|
||||
@Override
|
||||
public void onError(@NonNull Throwable e) {
|
||||
eventBus.post(new EventStatus
|
||||
(userEntity.getId(),
|
||||
EventStatus.EventType
|
||||
.PUSH_REGISTRATION, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
// unused atm
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "IOException while updating user", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(@NonNull Throwable e) {
|
||||
eventBus.post(new EventStatus(userEntity.getId(),
|
||||
EventStatus.EventType.PUSH_REGISTRATION, false));
|
||||
eventBus.post(new EventStatus(userEntity.getId(), EventStatus.EventType.PUSH_REGISTRATION, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user