Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically fix broken registrations/webhooks #3875

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ class FirebaseCloudMessagingService : FirebaseMessagingService() {
launch {
try {
serverManager.integrationRepository(it.id).updateRegistration(
DeviceRegistration(
pushToken = token
)
deviceRegistration = DeviceRegistration(pushToken = token),
allowReregistration = false
)
} catch (e: Exception) {
Log.e(TAG, "Issue updating token", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.Flow
interface IntegrationRepository {

suspend fun registerDevice(deviceRegistration: DeviceRegistration)
suspend fun updateRegistration(deviceRegistration: DeviceRegistration)
suspend fun updateRegistration(deviceRegistration: DeviceRegistration, allowReregistration: Boolean = true)
suspend fun getRegistration(): DeviceRegistration
suspend fun deletePreferences()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class IntegrationRepositoryImpl @AssistedInject constructor(
}
}

override suspend fun updateRegistration(deviceRegistration: DeviceRegistration) {
override suspend fun updateRegistration(deviceRegistration: DeviceRegistration, allowReregistration: Boolean) {
val request =
IntegrationRequest(
"update_registration",
Expand All @@ -126,9 +126,20 @@ class IntegrationRepositoryImpl @AssistedInject constructor(
var causeException: Exception? = null
for (it in server.connection.getApiUrls()) {
try {
if (integrationService.callWebhook(it.toHttpUrlOrNull()!!, request).isSuccessful) {
persistDeviceRegistration(deviceRegistration)
return
val response = integrationService.callWebhook(it.toHttpUrlOrNull()!!, request)
// The server should return a body with the registration, but might return:
// 200 with empty body for broken direct webhook
// 404 for broken cloudhook
// 410 for missing config entry
if (response.isSuccessful) {
if (response.code() == 200 && (response.body()?.contentLength() ?: 0) == 0L) {
throw IllegalStateException("update_registration returned empty body")
} else {
persistDeviceRegistration(deviceRegistration)
return
}
} else if (response.code() == 404 || response.code() == 410) {
throw IllegalStateException("update_registration returned code ${response.code()}")
}
} catch (e: Exception) {
if (causeException == null) causeException = e
Expand All @@ -137,7 +148,16 @@ class IntegrationRepositoryImpl @AssistedInject constructor(
}

if (causeException != null) {
throw IntegrationException(causeException)
if (allowReregistration && (causeException is IllegalStateException)) {
Log.w(TAG, "Device registration broken, reregistering", causeException)
try {
registerDevice(deviceRegistration)
} catch (e: Exception) {
throw IntegrationException(e)
}
} else {
throw IntegrationException(causeException)
}
} else {
throw IntegrationException("Error calling integration request update_registration")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ class FirebaseCloudMessagingService : FirebaseMessagingService() {
launch {
try {
serverManager.integrationRepository(it.id).updateRegistration(
DeviceRegistration(
deviceRegistration = DeviceRegistration(
pushToken = token,
pushWebsocket = false
)
),
allowReregistration = false
)
} catch (e: Exception) {
Log.e(TAG, "Issue updating token", e)
Expand Down