Skip to content

Commit 4621488

Browse files
authored
Merge pull request #7476 from vector-im/fix/mna/push-toggle-check-support
[Session manager] Hide push notification toggle when there is no server support (PSG-970)
2 parents f34758c + e9daef9 commit 4621488

24 files changed

+572
-65
lines changed

changelog.d/7457.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[Session manager] Hide push notification toggle when there is no server support

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/homeserver/HomeServerCapabilities.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ data class HomeServerCapabilities(
7070
* True if the home server supports threaded read receipts and unread notifications.
7171
*/
7272
val canUseThreadReadReceiptsAndNotifications: Boolean = false,
73+
74+
/**
75+
* True if the home server supports remote toggle of Pusher for a given device.
76+
*/
77+
val canRemotelyTogglePushNotificationsOfDevices: Boolean = false,
7378
) {
7479

7580
enum class RoomCapabilitySupport {

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/version/Versions.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.matrix.android.sdk.internal.auth.version
1818

1919
import com.squareup.moshi.Json
2020
import com.squareup.moshi.JsonClass
21+
import org.matrix.android.sdk.api.extensions.orFalse
2122

2223
/**
2324
* Model for https://matrix.org/docs/spec/client_server/latest#get-matrix-client-versions.
@@ -56,6 +57,7 @@ private const val FEATURE_THREADS_MSC3440_STABLE = "org.matrix.msc3440.stable"
5657
private const val FEATURE_QR_CODE_LOGIN = "org.matrix.msc3882"
5758
private const val FEATURE_THREADS_MSC3771 = "org.matrix.msc3771"
5859
private const val FEATURE_THREADS_MSC3773 = "org.matrix.msc3773"
60+
private const val FEATURE_REMOTE_TOGGLE_PUSH_NOTIFICATIONS_MSC3881 = "org.matrix.msc3881"
5961

6062
/**
6163
* Return true if the SDK supports this homeserver version.
@@ -142,3 +144,12 @@ private fun Versions.getMaxVersion(): HomeServerVersion {
142144
?.maxOrNull()
143145
?: HomeServerVersion.r0_0_0
144146
}
147+
148+
/**
149+
* Indicate if the server supports MSC3881: https://github.com/matrix-org/matrix-spec-proposals/pull/3881.
150+
*
151+
* @return true if remote toggle of push notifications is supported
152+
*/
153+
internal fun Versions.doesServerSupportRemoteToggleOfPushNotifications(): Boolean {
154+
return unstableFeatures?.get(FEATURE_REMOTE_TOGGLE_PUSH_NOTIFICATIONS_MSC3881).orFalse()
155+
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo038
5858
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo039
5959
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo040
6060
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo041
61+
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo042
6162
import org.matrix.android.sdk.internal.util.Normalizer
6263
import org.matrix.android.sdk.internal.util.database.MatrixRealmMigration
6364
import javax.inject.Inject
@@ -66,7 +67,7 @@ internal class RealmSessionStoreMigration @Inject constructor(
6667
private val normalizer: Normalizer
6768
) : MatrixRealmMigration(
6869
dbName = "Session",
69-
schemaVersion = 41L,
70+
schemaVersion = 42L,
7071
) {
7172
/**
7273
* Forces all RealmSessionStoreMigration instances to be equal.
@@ -117,5 +118,6 @@ internal class RealmSessionStoreMigration @Inject constructor(
117118
if (oldVersion < 39) MigrateSessionTo039(realm).perform()
118119
if (oldVersion < 40) MigrateSessionTo040(realm).perform()
119120
if (oldVersion < 41) MigrateSessionTo041(realm).perform()
121+
if (oldVersion < 42) MigrateSessionTo042(realm).perform()
120122
}
121123
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/mapper/HomeServerCapabilitiesMapper.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ internal object HomeServerCapabilitiesMapper {
4545
canUseThreading = entity.canUseThreading,
4646
canControlLogoutDevices = entity.canControlLogoutDevices,
4747
canLoginWithQrCode = entity.canLoginWithQrCode,
48-
canUseThreadReadReceiptsAndNotifications = entity.canUseThreadReadReceiptsAndNotifications
48+
canUseThreadReadReceiptsAndNotifications = entity.canUseThreadReadReceiptsAndNotifications,
49+
canRemotelyTogglePushNotificationsOfDevices = entity.canRemotelyTogglePushNotificationsOfDevices,
4950
)
5051
}
5152

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.matrix.android.sdk.internal.database.migration
18+
19+
import io.realm.DynamicRealm
20+
import org.matrix.android.sdk.internal.database.model.HomeServerCapabilitiesEntityFields
21+
import org.matrix.android.sdk.internal.extensions.forceRefreshOfHomeServerCapabilities
22+
import org.matrix.android.sdk.internal.util.database.RealmMigrator
23+
24+
internal class MigrateSessionTo042(realm: DynamicRealm) : RealmMigrator(realm, 42) {
25+
26+
override fun doMigrate(realm: DynamicRealm) {
27+
realm.schema.get("HomeServerCapabilitiesEntity")
28+
?.addField(HomeServerCapabilitiesEntityFields.CAN_REMOTELY_TOGGLE_PUSH_NOTIFICATIONS_OF_DEVICES, Boolean::class.java)
29+
?.forceRefreshOfHomeServerCapabilities()
30+
}
31+
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/HomeServerCapabilitiesEntity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ internal open class HomeServerCapabilitiesEntity(
3333
var canControlLogoutDevices: Boolean = false,
3434
var canLoginWithQrCode: Boolean = false,
3535
var canUseThreadReadReceiptsAndNotifications: Boolean = false,
36+
var canRemotelyTogglePushNotificationsOfDevices: Boolean = false,
3637
) : RealmObject() {
3738

3839
companion object

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/homeserver/GetHomeServerCapabilitiesTask.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilities
2525
import org.matrix.android.sdk.internal.auth.version.Versions
2626
import org.matrix.android.sdk.internal.auth.version.doesServerSupportLogoutDevices
2727
import org.matrix.android.sdk.internal.auth.version.doesServerSupportQrCodeLogin
28+
import org.matrix.android.sdk.internal.auth.version.doesServerSupportRemoteToggleOfPushNotifications
2829
import org.matrix.android.sdk.internal.auth.version.doesServerSupportThreadUnreadNotifications
2930
import org.matrix.android.sdk.internal.auth.version.doesServerSupportThreads
3031
import org.matrix.android.sdk.internal.auth.version.isLoginAndRegistrationSupportedBySdk
@@ -141,13 +142,18 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor(
141142
}
142143

143144
if (getVersionResult != null) {
144-
homeServerCapabilitiesEntity.lastVersionIdentityServerSupported = getVersionResult.isLoginAndRegistrationSupportedBySdk()
145-
homeServerCapabilitiesEntity.canControlLogoutDevices = getVersionResult.doesServerSupportLogoutDevices()
145+
homeServerCapabilitiesEntity.lastVersionIdentityServerSupported =
146+
getVersionResult.isLoginAndRegistrationSupportedBySdk()
147+
homeServerCapabilitiesEntity.canControlLogoutDevices =
148+
getVersionResult.doesServerSupportLogoutDevices()
146149
homeServerCapabilitiesEntity.canUseThreading = /* capabilities?.threads?.enabled.orFalse() || */
147150
getVersionResult.doesServerSupportThreads()
148151
homeServerCapabilitiesEntity.canUseThreadReadReceiptsAndNotifications =
149152
getVersionResult.doesServerSupportThreadUnreadNotifications()
150-
homeServerCapabilitiesEntity.canLoginWithQrCode = getVersionResult.doesServerSupportQrCodeLogin()
153+
homeServerCapabilitiesEntity.canLoginWithQrCode =
154+
getVersionResult.doesServerSupportQrCodeLogin()
155+
homeServerCapabilitiesEntity.canRemotelyTogglePushNotificationsOfDevices =
156+
getVersionResult.doesServerSupportRemoteToggleOfPushNotifications()
151157
}
152158

153159
if (getWellknownResult != null && getWellknownResult is WellknownResult.Prompt) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2022 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package im.vector.app.features.settings.devices.v2.notification
18+
19+
import im.vector.app.core.di.ActiveSessionHolder
20+
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
21+
import javax.inject.Inject
22+
23+
class CheckIfCanTogglePushNotificationsViaAccountDataUseCase @Inject constructor(
24+
private val activeSessionHolder: ActiveSessionHolder,
25+
) {
26+
27+
fun execute(deviceId: String): Boolean {
28+
return activeSessionHolder
29+
.getSafeActiveSession()
30+
?.accountDataService()
31+
?.getUserAccountDataEvent(UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + deviceId) != null
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2022 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package im.vector.app.features.settings.devices.v2.notification
18+
19+
import im.vector.app.core.di.ActiveSessionHolder
20+
import org.matrix.android.sdk.api.extensions.orFalse
21+
import javax.inject.Inject
22+
23+
class CheckIfCanTogglePushNotificationsViaPusherUseCase @Inject constructor(
24+
private val activeSessionHolder: ActiveSessionHolder,
25+
) {
26+
27+
fun execute(): Boolean {
28+
return activeSessionHolder
29+
.getSafeActiveSession()
30+
?.homeServerCapabilitiesService()
31+
?.getHomeServerCapabilities()
32+
?.canRemotelyTogglePushNotificationsOfDevices
33+
.orFalse()
34+
}
35+
}

0 commit comments

Comments
 (0)