GET /api/v1/push/subscription を頻繁には呼び出さないようにする

This commit is contained in:
tateisu 2018-08-30 12:23:13 +09:00
parent 3431779356
commit bd51297fdb
5 changed files with 115 additions and 4 deletions

View File

@ -0,0 +1,55 @@
package jp.juggler.subwaytooter
import android.support.test.runner.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
import jp.juggler.subwaytooter.util.*
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Assert.assertEquals
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see [Testing documentation](http://d.android.com/tools/testing)
*/
@RunWith(AndroidJUnit4::class)
class JsonArrayForEach {
@Test
@Throws(Exception::class)
fun test(){
val array = JSONArray().apply{
put("a")
put("b")
put( null)
put( JSONObject.NULL)
}
var count = 0
array.forEach {
println("JSONArray.forEach $it")
++count
}
array.forEachIndexed { i,v->
println("JSONArray.forEachIndexed $i $v")
++count
}
array.downForEach {
println("JSONArray.downForEach $it")
++count
}
array.downForEachIndexed { i,v->
println("JSONArray.downForEachIndexed $i $v")
++count
}
assertEquals(count,16)
}
}

View File

@ -1078,9 +1078,10 @@ class TestTootApiClient {
val instance = "unit-test"
client.instance = instance
val clientName = "SubwayTooterUnitTest"
val scope_string = "read+write+follow+push"
// まずクライアント情報を作らないとcredentialのテストができない
var result = client.registerClient(clientName)
var result = client.registerClient(scope_string , clientName)
assertNotNull(result)
assertEquals(null, result?.error)
var jsonObject = result?.jsonObject
@ -1108,7 +1109,7 @@ class TestTootApiClient {
var url : String?
// ブラウザURLの作成
url = client.prepareBrowserUrl(clientInfo)
url = client.prepareBrowserUrl(scope_string,clientInfo)
assertNotNull(url)
println(url)

View File

@ -823,7 +823,7 @@ class TootApiClient(
// クライアントをタンスに登録
private fun registerClient(scope_string : String, clientName : String) : TootApiResult? {
fun registerClient(scope_string : String, clientName : String) : TootApiResult? {
val result = TootApiResult.makeWithCaption(this.instance)
if(result.error != null) return result
val instance = result.caption // same to instance

View File

@ -20,6 +20,22 @@ class PushSubscriptionHelper(
val verbose : Boolean = false
) {
companion object {
private val lastCheckedMap :HashMap<String,Long> = HashMap()
}
private fun preventRapid() :Boolean {
if(verbose ) return true
val now = System.currentTimeMillis()
synchronized(lastCheckedMap){
val lastChecked = lastCheckedMap[ account.acct ]
lastCheckedMap[ account.acct] = now
return ( lastChecked == null || now - lastChecked >= 600000L)
}
}
val flags : Int
var subscribed : Boolean = false
@ -350,6 +366,7 @@ class PushSubscriptionHelper(
}
fun updateSubscription(client : TootApiClient) : TootApiResult? {
if( !preventRapid() ) return TootApiResult()
val result = updateSubscription_sub(client)
val e = result?.error
if(e != null) addLog(e)

View File

@ -662,9 +662,47 @@ fun JSONObject.parseInt(key : String) : Int? {
}
}
fun JSONObject.toPostRequestBuilder()=
fun JSONObject.toPostRequestBuilder():Request.Builder=
Request.Builder().post(RequestBody.create(TootApiClient.MEDIA_TYPE_JSON,this.toString()))
fun removeJsonNull(o : Any?) = if(JSONObject.NULL === o) null else o
inline fun JSONArray.forEach(block : (v : Any?) -> Unit) {
val e = this.length()
var i = 0
while(i < e) {
block(removeJsonNull(this.opt(i)))
++ i
}
}
inline fun JSONArray.forEachIndexed(block : (i : Int, v : Any?) -> Unit) {
val e = this.length()
var i = 0
while(i < e) {
block(i, removeJsonNull(this.opt(i)))
++ i
}
}
inline fun JSONArray.downForEach(block : (v : Any?) -> Unit) {
var i = this.length() - 1
while(i >= 0) {
block(removeJsonNull(this.opt(i)))
-- i
}
}
inline fun JSONArray.downForEachIndexed(block : (i : Int, v : Any?) -> Unit) {
var i = this.length() - 1
while(i >= 0) {
block(i, removeJsonNull(this.opt(i)))
-- i
}
}
////////////////////////////////////////////////////////////////////
// Bundle