11/*
22 * Nextcloud Talk - Android Client
33 *
4- * SPDX-FileCopyrightText: 2022 Andy Scherzinger <info@andy-scherzinger.de>
5- * SPDX-FileCopyrightText: 2022 Marcel Hibbe <dev@mhibbe.de>
6- * SPDX-FileCopyrightText: 2017 Mario Danic <mario@lovelyhq.com>
4+ * SPDX-FileCopyrightText: 2017-2026 Nextcloud GmbH and Nextcloud contributors
75 * SPDX-License-Identifier: GPL-3.0-or-later
86 */
97package com.nextcloud.talk.jobs
@@ -39,21 +37,20 @@ import retrofit2.Retrofit
3937import javax.inject.Inject
4038
4139/* *
42- * Can be used for 4 different things:
40+ * Can be used for 5 different things:
4341 * - if inputData contains [USER_ID] and [ACTIVATION_TOKEN]: activate web push for user (on server) and unregister
4442 * for proxy push (on server) (received from [com.nextcloud.talk.services.UnifiedPushService])
4543 * - if inputData contains [USER_ID] and [UNIFIEDPUSH_ENDPOINT]: register for web push (on server)
4644 * (received from [com.nextcloud.talk.services.UnifiedPushService])
45+ * - if inputData contains [USER_ID] and [UNREGISTER_WEBPUSH]: unregister web push for user (on server)
4746 * - if inputData contains [USE_UNIFIEDPUSH] or if [AppPreferences.getUseUnifiedPush]: get the server VAPID key and
4847 * register for UnifiedPush to the distributor (on device)
4948 * - if [AppPreferences.getUseUnifiedPush] is false: unregister UnifiedPush (on device) and unregister for web push
5049 * (on server), then register for proxy push (on server)
5150 */
5251@AutoInjector(NextcloudTalkApplication ::class )
53- class PushRegistrationWorker (
54- context : Context ,
55- workerParams : WorkerParameters
56- ): Worker(context, workerParams) {
52+ @Suppress(" TooManyFunctions" )
53+ class PushRegistrationWorker (context : Context , workerParams : WorkerParameters ) : Worker(context, workerParams) {
5754 @Inject
5855 lateinit var retrofit: Retrofit
5956
@@ -96,7 +93,7 @@ class PushRegistrationWorker(
9693 if (userId != - 1L && activationToken != null ) {
9794 Log .d(TAG , " PushRegistrationWorker called via $origin (webPushActivationWork)" )
9895 webPushActivationWork(userId, activationToken)
99- } else if (userId != - 1L && pushEndpoint != null ) {
96+ } else if (userId != - 1L && pushEndpoint != null ) {
10097 Log .d(TAG , " PushRegistrationWorker called via $origin (webPushWork)" )
10198 webPushWork(userId, pushEndpoint)
10299 } else if (userId != - 1L && unregisterWebPush) {
@@ -228,25 +225,27 @@ class PushRegistrationWorker(
228225 }
229226 }
230227
231- private fun defaultUseUnifiedPush (): Boolean = preferences.useUnifiedPush &&
232- // If this is the first registration, we have never called [UnifiedPush.register]
233- // because it happens after this function
234- // => we can't be acked by the distributor yet, [UnifiedPush.getAckDistributor] == null
235- // So we check the SavedDistributor instead
236- UnifiedPush .getSavedDistributor(applicationContext).also {
237- // It is null if the distributor has unregistered all the accounts,
238- // or if it has been uninstalled from the system
239- if (it == null ) {
240- Log .d(TAG , " No saved distributor found: disabling UnifiedPush" )
241- preferences.useUnifiedPush = false
242- if (inputData.keyValueMap.any { (key, _) ->
243- RESTART_ON_DISTRIB_UNINSTALL .contains(key)
244- }) {
245- enqueueWorkerWithoutData(" defaultUseDistributor" )
246- enqueueNotifUnifiedPushDisabled()
228+ private fun defaultUseUnifiedPush (): Boolean =
229+ preferences.useUnifiedPush &&
230+ // If this is the first registration, we have never called [UnifiedPush.register]
231+ // because it happens after this function
232+ // => we can't be acked by the distributor yet, [UnifiedPush.getAckDistributor] == null
233+ // So we check the SavedDistributor instead
234+ UnifiedPush .getSavedDistributor(applicationContext).also {
235+ // It is null if the distributor has unregistered all the accounts,
236+ // or if it has been uninstalled from the system
237+ if (it == null ) {
238+ Log .d(TAG , " No saved distributor found: disabling UnifiedPush" )
239+ preferences.useUnifiedPush = false
240+ if (inputData.keyValueMap.any { (key, _) ->
241+ RESTART_ON_DISTRIB_UNINSTALL .contains(key)
242+ }
243+ ) {
244+ enqueueWorkerWithoutData(" defaultUseDistributor" )
245+ enqueueNotifUnifiedPushDisabled()
246+ }
247247 }
248- }
249- } != null
248+ } != null
250249
251250 /* *
252251 * Run the default worker, to use FCM if available
@@ -304,8 +303,8 @@ class PushRegistrationWorker(
304303 /* *
305304 * Unregister on NC server and NC proxy
306305 */
307- private fun unregisterProxyPush (user : User ): Observable <Void > {
308- return if (ClosedInterfaceImpl ().isGooglePlayServicesAvailable) {
306+ private fun unregisterProxyPush (user : User ): Observable <Void > =
307+ if (ClosedInterfaceImpl ().isGooglePlayServicesAvailable) {
309308 Log .d(TAG , " Unregistering proxy push for ${user.userId} " )
310309 ncApi.unregisterDeviceForNotificationsWithNextcloud(
311310 user.getCredentials(),
@@ -322,17 +321,14 @@ class PushRegistrationWorker(
322321 } else {
323322 Observable .empty()
324323 }
325- }
326324
327325 /* *
328326 * Register web push with the unifiedpush endpoint, if the server supports web push
329327 *
330328 * @return `Observable<Pair<User, Boolean>>`, true if registration succeed, false if server doesn't support web push
331329 */
332- private fun registerWebPushForAccount (
333- user : User ,
334- pushEndpoint : PushEndpoint
335- ): Observable <Pair <User , Boolean >> {
330+ @Suppress(" ReturnCount" )
331+ private fun registerWebPushForAccount (user : User , pushEndpoint : PushEndpoint ): Observable <Pair <User , Boolean >> {
336332 if (user.hasWebPushCapability) {
337333 Log .d(TAG , " Registering web push for ${user.userId} " )
338334 if (user.userId == null || user.baseUrl == null ) {
@@ -353,16 +349,19 @@ class PushRegistrationWorker(
353349 " talk"
354350 ).map { r ->
355351 return @map when (r.code()) {
356- 200 -> {
352+ HTTP_OK -> {
357353 Log .d(TAG , " Web push registration for ${user.userId} was already registered and activated\n " )
358354 user to true
359355 }
360- 201 -> {
356+ HTTP_CREATED -> {
361357 Log .d(TAG , " New web push registration for ${user.userId} " )
362358 user to true
363359 }
364360 else -> {
365- Log .d(TAG , " An error occurred while registering web push for ${user.userId} (status=${r.code()} )" )
361+ Log .d(
362+ TAG ,
363+ " An error occurred while registering web push for ${user.userId} (status=${r.code()} )"
364+ )
366365 user to false
367366 }
368367 }
@@ -373,10 +372,7 @@ class PushRegistrationWorker(
373372 }
374373 }
375374
376- private fun activateWebPushForAccount (
377- user : User ,
378- activationToken : String
379- ) : Observable <Boolean > {
375+ private fun activateWebPushForAccount (user : User , activationToken : String ): Observable <Boolean > {
380376 Log .d(TAG , " Activating web push for ${user.userId} " )
381377 if (user.userId == null || user.baseUrl == null ) {
382378 Log .w(TAG , " Null userId or baseUrl (userId=${user.userId} , baseUrl=${user.baseUrl} " )
@@ -388,11 +384,11 @@ class PushRegistrationWorker(
388384 activationToken
389385 ).map { r ->
390386 return @map when (r.code()) {
391- 200 -> {
387+ HTTP_OK -> {
392388 Log .d(TAG , " Web push registration for ${user.userId} was already activated\n " )
393389 true
394390 }
395- 202 -> {
391+ HTTP_CREATED -> {
396392 Log .d(TAG , " Web push registration for ${user.userId} activated" )
397393 true
398394 }
@@ -404,9 +400,7 @@ class PushRegistrationWorker(
404400 }
405401 }
406402
407- private fun unregisterWebPushForAccount (
408- user : User
409- ) : Observable <Boolean > {
403+ private fun unregisterWebPushForAccount (user : User ): Observable <Boolean > {
410404 Log .d(TAG , " Unregistering web push for ${user.userId} " )
411405 if (user.userId == null || user.baseUrl == null ) {
412406 Log .w(TAG , " Null userId or baseUrl (userId=${user.userId} , baseUrl=${user.baseUrl} " )
@@ -416,7 +410,6 @@ class PushRegistrationWorker(
416410 user.getCredentials(),
417411 ApiUtils .getUrlForWebPush(user.baseUrl!! )
418412 ).map { true }
419-
420413 }
421414
422415 /* *
@@ -426,16 +419,15 @@ class PushRegistrationWorker(
426419 *
427420 * @return `Observable<Pair<User, Boolean>>`, true if registration succeed, false if server doesn't support web push
428421 */
429- private fun registerUnifiedPushForAccount (
430- user : User
431- ): Observable <Pair <User , Boolean >> {
422+ @Suppress(" ReturnCount" )
423+ private fun registerUnifiedPushForAccount (user : User ): Observable <Pair <User , Boolean >> {
432424 if (user.hasWebPushCapability) {
433425 Log .d(TAG , " Registering UnifiedPush for ${user.userId} (${user.id} )" )
434426 if (user.userId == null || user.baseUrl == null ) {
435427 Log .w(TAG , " Null userId or baseUrl (userId=${user.userId} , baseUrl=${user.baseUrl} " )
436428 return Observable .empty()
437429 }
438- return ncApi.getVapidKey(user.getCredentials(),ApiUtils .getUrlForVapid(user.baseUrl!! ))
430+ return ncApi.getVapidKey(user.getCredentials(), ApiUtils .getUrlForVapid(user.baseUrl!! ))
439431 .flatMap { ocs ->
440432 ocs.ocs?.data?.vapid?.let { vapid ->
441433 UnifiedPush .register(
@@ -464,6 +456,8 @@ class PushRegistrationWorker(
464456 const val USE_UNIFIEDPUSH = " use_unifiedpush"
465457 const val UNIFIEDPUSH_ENDPOINT = " unifiedpush_endpoint"
466458 const val UNREGISTER_WEBPUSH = " unregister_webpush"
459+ private const val HTTP_OK : Int = 200
460+ private const val HTTP_CREATED : Int = 201
467461
468462 /* *
469463 * If any of these actions are present when we observe the distributor is uninstalled,
0 commit comments