This commit is contained in:
tateisu 2020-12-08 23:01:32 +09:00
parent 8818f25b6f
commit b0b28509a4
2 changed files with 40 additions and 38 deletions

View File

@ -604,7 +604,7 @@ class PollingWorker private constructor(contextArg: Context) {
} }
// JobService#onStartJob から呼ばれる // JobService#onStartJob から呼ばれる
suspend fun onStartJob(jobService: JobService, params: JobParameters): Boolean { fun onStartJob(jobService: JobService, params: JobParameters): Boolean {
val item = JobItem(jobService, params) val item = JobItem(jobService, params)
addJob(item, true) addJob(item, true)
return true return true
@ -623,11 +623,11 @@ class PollingWorker private constructor(contextArg: Context) {
} }
// FCMメッセージイベントから呼ばれる // FCMメッセージイベントから呼ばれる
private suspend fun addJobFCM() { private fun addJobFCM() {
addJob(JobItem(JOB_FCM), false) addJob(JobItem(JOB_FCM), false)
} }
private suspend fun addJob(item: JobItem, bRemoveOld: Boolean) { private fun addJob(item: JobItem, bRemoveOld: Boolean) {
val jobId = item.jobId val jobId = item.jobId
// 同じジョブ番号がジョブリストにあるか? // 同じジョブ番号がジョブリストにあるか?
@ -648,8 +648,9 @@ class PollingWorker private constructor(contextArg: Context) {
job_list.add(item) job_list.add(item)
} }
GlobalScope.launch(Dispatchers.Default){
workerNotifier.send(Unit) workerNotifier.send(Unit)
}
} }
// JobService#onStopJob から呼ばれる // JobService#onStopJob から呼ばれる

View File

@ -87,7 +87,7 @@ class PushSubscriptionHelper(
if (oldKey != serverKey) { if (oldKey != serverKey) {
// サーバキーをアプリサーバに登録 // サーバキーをアプリサーバに登録
val r = client.http( client.http(
JsonObject().apply { JsonObject().apply {
put("client_id", clientIdentifier) put("client_id", clientIdentifier)
put("server_key", serverKey) put("server_key", serverKey)
@ -96,12 +96,9 @@ class PushSubscriptionHelper(
.url("${PollingWorker.APP_SERVER}/webpushserverkey") .url("${PollingWorker.APP_SERVER}/webpushserverkey")
.build() .build()
) ).also{ result ->
val res = r?.response result.response?.let{ res ->
when (res?.code) { when (res.code.also{ res.close()}) {
null -> {
}
200 -> { 200 -> {
// 登録できたサーバーキーをアプリ内DBに保存 // 登録できたサーバーキーをアプリ内DBに保存
@ -115,6 +112,8 @@ class PushSubscriptionHelper(
} }
} }
} }
}
}
return TootApiResult() return TootApiResult()
} }
@ -123,7 +122,7 @@ class PushSubscriptionHelper(
client: TootApiClient, client: TootApiClient,
deviceId: String, deviceId: String,
endpoint: String endpoint: String
): TootApiResult? { ): TootApiResult {
if (account.last_push_endpoint == endpoint) return TootApiResult() if (account.last_push_endpoint == endpoint) return TootApiResult()
@ -136,19 +135,20 @@ class PushSubscriptionHelper(
.toPostRequestBuilder() .toPostRequestBuilder()
.url("${PollingWorker.APP_SERVER}/webpushendpoint") .url("${PollingWorker.APP_SERVER}/webpushendpoint")
.build() .build()
)?.also { result -> ).also { result ->
val res = result.response result.response?.let{ res->
if (res != null) { when (res.code.also{ res.close() }) {
val code = res.code in 200 until 300 -> {
if (code in 200 until 300) {
account.updateLastPushEndpoint(endpoint) account.updateLastPushEndpoint(endpoint)
} else { }
else -> {
result.caption = "(SubwayTooter App server)" result.caption = "(SubwayTooter App server)"
client.readBodyString(result) client.readBodyString(result)
} }
} }
} }
} }
}
private suspend fun updateSubscriptionMisskey(client: TootApiClient): TootApiResult? { private suspend fun updateSubscriptionMisskey(client: TootApiClient): TootApiResult? {
@ -170,7 +170,7 @@ class PushSubscriptionHelper(
// 購読が不要な場合 // 購読が不要な場合
// アプリサーバが410を返せるように状態を通知する // アプリサーバが410を返せるように状態を通知する
if (flags == 0) return registerEndpoint(client, device_id, "none")?.also { if (flags == 0) return registerEndpoint(client, device_id, "none").also {
if (it.error == null && verbose) addLog(context.getString(R.string.push_subscription_updated)) if (it.error == null && verbose) addLog(context.getString(R.string.push_subscription_updated))
} }
@ -191,9 +191,10 @@ class PushSubscriptionHelper(
// アプリサーバが過去のendpoint urlに410を返せるよう、状態を通知する // アプリサーバが過去のendpoint urlに410を返せるよう、状態を通知する
val r = registerEndpoint(client, device_id, endpoint.toUri().encodedPath!!) val r = registerEndpoint(client, device_id, endpoint.toUri().encodedPath!!)
if (r == null || r.error != null) return r if (r.error != null) return r
// 購読 // 購読
@Suppress("SpellCheckingInspection")
return client.request( return client.request(
"/api/sw/register", "/api/sw/register",
account.putMisskeyApiToken().apply { account.putMisskeyApiToken().apply {
@ -401,7 +402,7 @@ class PushSubscriptionHelper(
.toPostRequestBuilder() .toPostRequestBuilder()
.url("${PollingWorker.APP_SERVER}/webpushtokencheck") .url("${PollingWorker.APP_SERVER}/webpushtokencheck")
.build() .build()
) ?: return null )
res = r.response ?: return r res = r.response ?: return r
if (res.code == 200) { if (res.code == 200) {