2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter.table
|
|
|
|
|
|
|
|
import android.content.ContentValues
|
|
|
|
import android.database.sqlite.SQLiteDatabase
|
|
|
|
import android.support.v4.util.LruCache
|
|
|
|
import jp.juggler.subwaytooter.App1
|
2018-08-18 12:58:14 +02:00
|
|
|
import jp.juggler.subwaytooter.api.entity.EntityId
|
2018-08-20 02:07:55 +02:00
|
|
|
import jp.juggler.subwaytooter.api.entity.EntityIdString
|
2018-01-04 19:52:25 +01:00
|
|
|
import jp.juggler.subwaytooter.api.entity.TootAccount
|
|
|
|
import jp.juggler.subwaytooter.api.entity.TootRelationShip
|
2018-12-01 00:02:18 +01:00
|
|
|
import jp.juggler.util.LogCategory
|
|
|
|
import jp.juggler.util.getInt
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-08-28 14:25:45 +02:00
|
|
|
class UserRelation {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-08-20 02:07:55 +02:00
|
|
|
var following : Boolean = false // 認証ユーザからのフォロー状態にある
|
2018-01-04 19:52:25 +01:00
|
|
|
var followed_by : Boolean = false // 認証ユーザは被フォロー状態にある
|
2018-10-30 20:29:00 +01:00
|
|
|
var blocking : Boolean = false // 認証ユーザからブロックした
|
|
|
|
var blocked_by : Boolean = false // 認証ユーザからブロックされた(Misskeyのみ。Mastodonでは常にfalse)
|
2018-01-04 19:52:25 +01:00
|
|
|
var muting : Boolean = false
|
2018-08-20 02:07:55 +02:00
|
|
|
var requested : Boolean = false // 認証ユーザからのフォローは申請中である
|
2018-10-30 20:29:00 +01:00
|
|
|
var requested_by : Boolean = false // 相手から認証ユーザへのフォローリクエスト申請中(Misskeyのみ。Mastodonでは常にfalse)
|
2018-01-04 19:52:25 +01:00
|
|
|
var following_reblogs : Int = 0 // このユーザからのブーストをTLに表示する
|
2018-08-28 14:25:45 +02:00
|
|
|
var endorsed : Boolean = false // ユーザをプロフィールで紹介する
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
// 認証ユーザからのフォロー状態
|
|
|
|
fun getFollowing(who : TootAccount?) : Boolean {
|
|
|
|
return if(requested && ! following && who != null && ! who.locked) true else following
|
|
|
|
}
|
|
|
|
|
|
|
|
// 認証ユーザからのフォローリクエスト申請中状態
|
|
|
|
fun getRequested(who : TootAccount?) : Boolean {
|
|
|
|
return if(requested && ! following && who != null && ! who.locked) false else requested
|
|
|
|
}
|
|
|
|
|
2018-08-18 12:58:14 +02:00
|
|
|
companion object : TableCompanion {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
private val log = LogCategory("UserRelation")
|
|
|
|
|
|
|
|
private const val table = "user_relation"
|
|
|
|
private const val COL_TIME_SAVE = "time_save"
|
|
|
|
private const val COL_DB_ID = "db_id" // SavedAccount のDB_ID
|
|
|
|
private const val COL_WHO_ID = "who_id" // ターゲットアカウントのID
|
|
|
|
private const val COL_FOLLOWING = "following"
|
|
|
|
private const val COL_FOLLOWED_BY = "followed_by"
|
|
|
|
private const val COL_BLOCKING = "blocking"
|
|
|
|
private const val COL_MUTING = "muting"
|
|
|
|
private const val COL_REQUESTED = "requested"
|
2018-08-28 14:25:45 +02:00
|
|
|
private const val COL_ENDORSED = "endorsed"
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
// (mastodon 2.1 or later) per-following-user setting.
|
|
|
|
// Whether the boosts from target account will be shown on authorized user's home TL.
|
|
|
|
private const val COL_FOLLOWING_REBLOGS = "following_reblogs"
|
|
|
|
|
2018-08-18 12:58:14 +02:00
|
|
|
const val REBLOG_HIDE =
|
|
|
|
0 // don't show the boosts from target account will be shown on authorized user's home TL.
|
|
|
|
const val REBLOG_SHOW =
|
|
|
|
1 // show the boosts from target account will be shown on authorized user's home TL.
|
2018-01-04 19:52:25 +01:00
|
|
|
const val REBLOG_UNKNOWN = 2 // not following, or instance don't support hide reblog.
|
|
|
|
|
2018-08-20 02:07:55 +02:00
|
|
|
internal val mMemoryCache = LruCache<String, UserRelation>(2048)
|
|
|
|
|
|
|
|
private const val load_where = "$COL_DB_ID=? and $COL_WHO_ID=?"
|
|
|
|
|
|
|
|
private val load_where_arg = object : ThreadLocal<Array<String?>>() {
|
|
|
|
override fun initialValue() : Array<String?> {
|
|
|
|
return Array(2) { _ -> null }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 15:13:34 +01:00
|
|
|
override fun onDBCreate(db : SQLiteDatabase) {
|
2018-01-04 19:52:25 +01:00
|
|
|
log.d("onDBCreate!")
|
|
|
|
db.execSQL(
|
2018-08-28 14:25:45 +02:00
|
|
|
"""
|
|
|
|
create table if not exists $table
|
|
|
|
(_id INTEGER PRIMARY KEY
|
|
|
|
,$COL_TIME_SAVE integer not null
|
|
|
|
,$COL_DB_ID integer not null
|
|
|
|
,$COL_WHO_ID integer not null
|
|
|
|
,$COL_FOLLOWING integer not null
|
|
|
|
,$COL_FOLLOWED_BY integer not null
|
|
|
|
,$COL_BLOCKING integer not null
|
|
|
|
,$COL_MUTING integer not null
|
|
|
|
,$COL_REQUESTED integer not null
|
|
|
|
,$COL_FOLLOWING_REBLOGS integer not null
|
|
|
|
,$COL_ENDORSED integer default 0
|
|
|
|
)"""
|
2018-01-04 19:52:25 +01:00
|
|
|
)
|
|
|
|
db.execSQL(
|
2018-08-28 14:25:45 +02:00
|
|
|
"create unique index if not exists ${table}_id on $table ($COL_DB_ID,$COL_WHO_ID)"
|
2018-01-04 19:52:25 +01:00
|
|
|
)
|
|
|
|
db.execSQL(
|
2018-08-28 14:25:45 +02:00
|
|
|
"create index if not exists ${table}_time on $table ($COL_TIME_SAVE)"
|
2018-01-04 19:52:25 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-03-16 15:13:34 +01:00
|
|
|
override fun onDBUpgrade(db : SQLiteDatabase, oldVersion : Int, newVersion : Int) {
|
2018-01-04 19:52:25 +01:00
|
|
|
if(oldVersion < 6 && newVersion >= 6) {
|
|
|
|
onDBCreate(db)
|
|
|
|
}
|
|
|
|
if(oldVersion < 20 && newVersion >= 20) {
|
|
|
|
try {
|
|
|
|
db.execSQL("alter table $table add column $COL_FOLLOWING_REBLOGS integer default 1")
|
|
|
|
/*
|
|
|
|
(COL_FOLLOWING_REBLOGS カラムのデフォルト値について)
|
|
|
|
1.7.5でboolean値を保存していた関係でデフォルト値は1(REBLOG_SHOW)になってしまっている
|
|
|
|
1.7.6以降では3値論理にしたのでデフォルトは2(REBLOG_UNKNOWN)の方が適切だが、SQLiteにはカラムのデフォルト制約の変更を行う機能がない
|
|
|
|
データは適当に更新されるはずだから、今のままでも多分問題ないはず…
|
|
|
|
*/
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.trace(ex)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-08-28 14:25:45 +02:00
|
|
|
if(oldVersion < 32 && newVersion >= 32) {
|
|
|
|
try {
|
|
|
|
db.execSQL("alter table $table add column $COL_ENDORSED integer default 0")
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.trace(ex)
|
|
|
|
}
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun deleteOld(now : Long) {
|
|
|
|
try {
|
|
|
|
// 古いデータを掃除する
|
|
|
|
val expire = now - 86400000L * 365
|
2018-08-18 12:58:14 +02:00
|
|
|
App1.database.delete(table, "$COL_TIME_SAVE<?", arrayOf(expire.toString()))
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.e(ex, "deleteOld failed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-20 02:07:55 +02:00
|
|
|
// マストドン用
|
2018-01-04 19:52:25 +01:00
|
|
|
fun save1(now : Long, db_id : Long, src : TootRelationShip) : UserRelation {
|
|
|
|
try {
|
|
|
|
val cv = ContentValues()
|
|
|
|
cv.put(COL_TIME_SAVE, now)
|
|
|
|
cv.put(COL_DB_ID, db_id)
|
2018-08-20 02:07:55 +02:00
|
|
|
cv.put(COL_WHO_ID, src.id.toLong())
|
2018-08-28 14:25:45 +02:00
|
|
|
cv.put(COL_FOLLOWING, src.following.b2i())
|
|
|
|
cv.put(COL_FOLLOWED_BY, src.followed_by.b2i())
|
|
|
|
cv.put(COL_BLOCKING, src.blocking.b2i())
|
|
|
|
cv.put(COL_MUTING, src.muting.b2i())
|
|
|
|
cv.put(COL_REQUESTED, src.requested.b2i())
|
2018-01-04 19:52:25 +01:00
|
|
|
cv.put(COL_FOLLOWING_REBLOGS, src.showing_reblogs)
|
2018-08-28 14:25:45 +02:00
|
|
|
cv.put(COL_ENDORSED,src.endorsed.b2i() )
|
2018-01-04 19:52:25 +01:00
|
|
|
App1.database.replace(table, null, cv)
|
|
|
|
val key = String.format("%s:%s", db_id, src.id)
|
|
|
|
mMemoryCache.remove(key)
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.e(ex, "save failed.")
|
|
|
|
}
|
|
|
|
|
2018-08-28 14:25:45 +02:00
|
|
|
return load(db_id, src.id)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
2018-08-20 02:07:55 +02:00
|
|
|
// マストドン用
|
2018-01-10 16:47:35 +01:00
|
|
|
fun saveList(now : Long, db_id : Long, src_list : ArrayList<TootRelationShip>) {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
val cv = ContentValues()
|
|
|
|
cv.put(COL_TIME_SAVE, now)
|
|
|
|
cv.put(COL_DB_ID, db_id)
|
|
|
|
|
|
|
|
var bOK = false
|
|
|
|
val db = App1.database
|
|
|
|
db.execSQL("BEGIN TRANSACTION")
|
|
|
|
try {
|
|
|
|
for(src in src_list) {
|
2018-08-20 02:07:55 +02:00
|
|
|
cv.put(COL_WHO_ID, src.id.toLong())
|
2018-08-18 12:58:14 +02:00
|
|
|
cv.put(COL_FOLLOWING, src.following.b2i())
|
|
|
|
cv.put(COL_FOLLOWED_BY, src.followed_by.b2i())
|
|
|
|
cv.put(COL_BLOCKING, src.blocking.b2i())
|
|
|
|
cv.put(COL_MUTING, src.muting.b2i())
|
|
|
|
cv.put(COL_REQUESTED, src.requested.b2i())
|
2018-01-04 19:52:25 +01:00
|
|
|
cv.put(COL_FOLLOWING_REBLOGS, src.showing_reblogs)
|
2018-08-28 14:25:45 +02:00
|
|
|
cv.put(COL_ENDORSED,src.endorsed.b2i() )
|
2018-01-04 19:52:25 +01:00
|
|
|
db.replace(table, null, cv)
|
|
|
|
|
|
|
|
}
|
|
|
|
bOK = true
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.trace(ex)
|
|
|
|
log.e(ex, "saveList failed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bOK) {
|
|
|
|
db.execSQL("COMMIT TRANSACTION")
|
|
|
|
for(src in src_list) {
|
|
|
|
val key = String.format("%s:%s", db_id, src.id)
|
|
|
|
mMemoryCache.remove(key)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
db.execSQL("ROLLBACK TRANSACTION")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 14:25:45 +02:00
|
|
|
fun load(db_id : Long, whoId : EntityId) : UserRelation {
|
2018-08-20 02:07:55 +02:00
|
|
|
//
|
|
|
|
val key = String.format("%s:%s", db_id, whoId)
|
2018-01-04 19:52:25 +01:00
|
|
|
val cached : UserRelation? = mMemoryCache.get(key)
|
|
|
|
if(cached != null) return cached
|
2018-08-28 14:25:45 +02:00
|
|
|
|
|
|
|
val dst = if(whoId is EntityIdString) {
|
|
|
|
UserRelationMisskey.load(db_id, whoId.toString())
|
|
|
|
} else {
|
|
|
|
load(db_id, whoId.toLong())
|
2018-08-20 02:07:55 +02:00
|
|
|
} ?: UserRelation()
|
2018-08-28 14:25:45 +02:00
|
|
|
|
2018-08-20 02:07:55 +02:00
|
|
|
mMemoryCache.put(key, dst)
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun load(db_id : Long, who_id : Long) : UserRelation? {
|
2018-01-04 19:52:25 +01:00
|
|
|
try {
|
2018-09-27 16:21:37 +02:00
|
|
|
val where_arg = load_where_arg.get() ?: arrayOfNulls<String?>(2)
|
2018-01-04 19:52:25 +01:00
|
|
|
where_arg[0] = db_id.toString()
|
|
|
|
where_arg[1] = who_id.toString()
|
|
|
|
App1.database.query(table, null, load_where, where_arg, null, null, null)
|
|
|
|
.use { cursor ->
|
|
|
|
if(cursor.moveToNext()) {
|
|
|
|
val dst = UserRelation()
|
2018-10-30 20:29:00 +01:00
|
|
|
dst.following = cursor.getBoolean(COL_FOLLOWING)
|
|
|
|
dst.followed_by =cursor.getBoolean(COL_FOLLOWED_BY)
|
|
|
|
dst.blocking = cursor.getBoolean(COL_BLOCKING)
|
|
|
|
dst.muting = cursor.getBoolean(COL_MUTING)
|
|
|
|
dst.requested = cursor.getBoolean(COL_REQUESTED)
|
|
|
|
dst.following_reblogs = cursor.getInt(COL_FOLLOWING_REBLOGS)
|
|
|
|
dst.endorsed = cursor.getBoolean(COL_ENDORSED)
|
2018-01-04 19:52:25 +01:00
|
|
|
return dst
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.trace(ex)
|
|
|
|
log.e(ex, "load failed.")
|
|
|
|
}
|
2018-08-20 02:07:55 +02:00
|
|
|
return null
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
}
|