Skip to content

Commit

Permalink
now misskey is working (for basic feature)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlaster committed Aug 6, 2023
1 parent fa10175 commit dc578cb
Show file tree
Hide file tree
Showing 256 changed files with 2,165 additions and 1,256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import dev.dimension.flare.data.database.cache.model.DbUser
DbPagingTimeline::class,
DbEmoji::class
],
version = 3
version = 4
)
@TypeConverters(Converters::class)
abstract class CacheDatabase : RoomDatabase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ interface UserDao {

@Query("SELECT * FROM user WHERE userKey = :userKey")
fun getUser(userKey: MicroBlogKey): Flow<DbUser?>

@Query("SELECT * FROM user WHERE handle = :handle AND host = :host")
fun getUserByHandleAndHost(handle: String, host: String): Flow<DbUser?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ private fun Status.toDbStatusWithUser(
fun Account.toDbUser(
host: String
): DbUser {
val remoteHost = if (acct != null && acct.contains('@')) {
acct.substring(acct.indexOf('@') + 1)
} else {
host
}
return DbUser(
userKey = MicroBlogKey(
id = id ?: throw IllegalArgumentException("mastodon Account.id should not be null"),
Expand All @@ -236,7 +241,8 @@ fun Account.toDbUser(
?: throw IllegalArgumentException("mastodon Account.displayName should not be null"),
handle = username
?: throw IllegalArgumentException("mastodon Account.username should not be null"),
content = dev.dimension.flare.data.database.cache.model.UserContent.Mastodon(this)
content = dev.dimension.flare.data.database.cache.model.UserContent.Mastodon(this),
host = remoteHost
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package dev.dimension.flare.data.database.cache.mapper

import androidx.room.withTransaction
import dev.dimension.flare.data.database.cache.CacheDatabase
import dev.dimension.flare.data.database.cache.model.DbEmoji
import dev.dimension.flare.data.database.cache.model.DbPagingTimeline
import dev.dimension.flare.data.database.cache.model.DbPagingTimelineWithStatus
import dev.dimension.flare.data.database.cache.model.DbStatus
import dev.dimension.flare.data.database.cache.model.DbStatusWithReference
import dev.dimension.flare.data.database.cache.model.DbStatusWithUser
import dev.dimension.flare.data.database.cache.model.DbUser
import dev.dimension.flare.data.database.cache.model.EmojiContent
import dev.dimension.flare.data.database.cache.model.StatusContent
import dev.dimension.flare.data.database.cache.model.UserContent
import dev.dimension.flare.data.network.misskey.api.model.EmojiSimple
import dev.dimension.flare.data.network.misskey.api.model.Note
import dev.dimension.flare.data.network.misskey.api.model.Notification
import dev.dimension.flare.data.network.misskey.api.model.User
Expand Down Expand Up @@ -78,7 +81,7 @@ suspend fun saveDbPagingTimelineWithStatus() {
}
}

val result = (allUsers + exsitingUsers).distinctBy { it.userKey }
val result = (exsitingUsers + allUsers).distinctBy { it.userKey }
userDao().insertAll(result)
}
(
Expand Down Expand Up @@ -239,29 +242,48 @@ private fun Note.toDbStatusWithUser(
}

private fun UserLite.toDbUser(
host: String,
accountHost: String,
emojis: List<dev.dimension.flare.data.network.misskey.api.model.EmojiSimple>,
) = DbUser(
userKey = MicroBlogKey(
id = id,
host = host,
host = accountHost,
),
platformType = dev.dimension.flare.model.PlatformType.Misskey,
name = name ?: "",
handle = username,
content = UserContent.MisskeyLite(this, emojis)
content = UserContent.MisskeyLite(this, emojis),
host = if (host.isNullOrEmpty()) {
accountHost
} else {
host
}
)

fun User.toDbUser(
host: String,
accountHost: String,
emojis: List<dev.dimension.flare.data.network.misskey.api.model.EmojiSimple>,
) = DbUser(
userKey = MicroBlogKey(
id = id,
host = host,
host = accountHost,
),
platformType = dev.dimension.flare.model.PlatformType.Misskey,
name = name ?: "",
handle = username,
content = UserContent.Misskey(this, emojis)
)
content = UserContent.Misskey(this, emojis),
host = if (host.isNullOrEmpty()) {
accountHost
} else {
host
}
)

fun List<EmojiSimple>.toDb(
host: String
): DbEmoji {
return DbEmoji(
host = host,
content = EmojiContent.Misskey(this)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.dimension.flare.data.database.cache.model
import androidx.room.Entity
import androidx.room.PrimaryKey
import dev.dimension.flare.data.network.mastodon.api.model.Emoji
import dev.dimension.flare.data.network.misskey.api.model.EmojiSimple
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand All @@ -18,4 +19,8 @@ sealed interface EmojiContent {
@Serializable
@SerialName("Mastodon")
data class Mastodon(val data: List<Emoji>) : EmojiContent

@Serializable
@SerialName("Misskey")
data class Misskey(val data: List<EmojiSimple>) : EmojiContent
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ data class DbUser(
val platformType: PlatformType,
val name: String,
val handle: String,
val host: String,
val content: UserContent
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ internal class HomeTimelineRemoteMediator(
private val pagingKey: String,
private val emojiCache: MisskeyEmojiCache,
) : RemoteMediator<Int, DbPagingTimelineWithStatus>() {
override suspend fun initialize(): InitializeAction {
return InitializeAction.SKIP_INITIAL_REFRESH
}

override suspend fun load(
loadType: LoadType,
Expand Down Expand Up @@ -60,7 +57,7 @@ internal class HomeTimelineRemoteMediator(
until_id = lastItem.status.status.data.statusKey.id
)
}
}.body() ?: return MediatorResult.Success(
} ?: return MediatorResult.Success(
endOfPaginationReached = true
)
val emojis = emojiCache.getEmojis(account = account)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class MentionTimelineRemoteMediator(
until_id = lastItem.status.status.data.statusKey.id
)
}
}.body() ?: return MediatorResult.Success(
} ?: return MediatorResult.Success(
endOfPaginationReached = true
)
val emojis = emojiCache.getEmojis(account = account)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class NotificationRemoteMediator(
until_id = lastItem.status.status.data.statusKey.id
)
}
}.body() ?: return MediatorResult.Success(
} ?: return MediatorResult.Success(
endOfPaginationReached = true
)
val emojis = emojiCache.getEmojis(account = account)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ internal class StatusDetailRemoteMediator(
val result = if (statusOnly) {
val current = account.service.lookupStatus(
statusKey.id
).body()
)
listOf(current)
} else {
val current = if (loadType == LoadType.REFRESH) {
account.service.lookupStatus(
statusKey.id
).body()
)
} else {
null
}
Expand All @@ -82,7 +82,7 @@ internal class StatusDetailRemoteMediator(
statusKey.id,
count = state.config.pageSize,
until_id = lastItem.status.status.data.statusKey.id
).body().orEmpty()
).orEmpty()
listOfNotNull(current?.reply, current) + children
// context.ancestors.orEmpty() + listOf(current) + context.descendants.orEmpty()
}.filterNotNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ internal class UserTimelineRemoteMediator(
until_id = lastItem.status.status.data.statusKey.id
)
}
}.body() ?: return MediatorResult.Success(
} ?: return MediatorResult.Success(
endOfPaginationReached = true
)
val emojis = emojiCache.getEmojis(account = account)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ internal fun ktorfit(
baseUrl(baseUrl)
httpClient(
HttpClient(OkHttp) {
config.invoke(this)

install(ContentNegotiation) {
json(JSON)
Expand All @@ -36,6 +35,8 @@ internal fun ktorfit(
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
addInterceptor(loggingInterceptor)
}

config.invoke(this)
// install(Logging) {
// logger = Logger.ANDROID
// level = LogLevel.ALL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ class MastodonService(
return lookupResources.lookupUser(id)
}

suspend fun lookupUserByAcct(acct: String): Account? {
return lookupResources.lookupUserByAcct(acct)
}

suspend fun lookupStatus(id: String): Status {
return lookupResources.lookupStatus(id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.dimension.flare.data.network.mastodon.api

import de.jensklingenberg.ktorfit.http.GET
import de.jensklingenberg.ktorfit.http.Path
import de.jensklingenberg.ktorfit.http.Query
import dev.dimension.flare.data.network.mastodon.api.model.Account
import dev.dimension.flare.data.network.mastodon.api.model.Status

Expand All @@ -15,4 +16,9 @@ interface LookupResources {
suspend fun lookupStatus(
@Path("id") id: String
): Status

@GET("/api/v1/accounts/lookup")
suspend fun lookupUserByAcct(
@Query("acct") acct: String,
): Account?
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import dev.dimension.flare.data.network.misskey.api.model.NotesHybridTimelineReq
import dev.dimension.flare.data.network.misskey.api.model.NotesMentionsRequest
import dev.dimension.flare.data.network.misskey.api.model.UsersNotesRequest
import dev.dimension.flare.data.network.misskey.api.model.UsersShowRequest
import io.ktor.client.plugins.DefaultRequest
import io.ktor.client.request.header
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders

class MisskeyService(
private val baseUrl: String,
Expand All @@ -25,6 +29,9 @@ class MisskeyService(
install(MisskeyAuthorizationPlugin) {
token = this@MisskeyService.token
}
install(DefaultRequest) {
header(HttpHeaders.ContentType, ContentType.Application.Json)
}
}
).create()
}
Expand All @@ -35,6 +42,9 @@ class MisskeyService(
install(MisskeyAuthorizationPlugin) {
token = this@MisskeyService.token
}
install(DefaultRequest) {
header(HttpHeaders.ContentType, ContentType.Application.Json)
}
}
).create()
}
Expand All @@ -45,6 +55,9 @@ class MisskeyService(
install(MisskeyAuthorizationPlugin) {
token = this@MisskeyService.token
}
install(DefaultRequest) {
header(HttpHeaders.ContentType, ContentType.Application.Json)
}
}
).create()
}
Expand All @@ -56,6 +69,9 @@ class MisskeyService(
install(MisskeyAuthorizationPlugin) {
token = this@MisskeyService.token
}
install(DefaultRequest) {
header(HttpHeaders.ContentType, ContentType.Application.Json)
}
}
).create()
}
Expand All @@ -65,8 +81,9 @@ class MisskeyService(
) = usersApi.usersShow(UsersShowRequest(userId = userId)).body()

suspend fun emojis(): List<EmojiSimple> {
return metaApi.emojis(Any()).body()?.emojis ?: emptyList()
return metaApi.emojis().body()?.emojis ?: emptyList()
}

suspend fun homeTimeline(
count: Int,
since_id: String? = null,
Expand All @@ -77,7 +94,7 @@ class MisskeyService(
sinceId = since_id,
limit = count
)
)
).body()

suspend fun userTimeline(
userId: String,
Expand All @@ -91,7 +108,7 @@ class MisskeyService(
sinceId = since_id,
limit = count
)
)
).body()

suspend fun notifications(
count: Int,
Expand All @@ -103,7 +120,7 @@ class MisskeyService(
sinceId = since_id,
limit = count
)
)
).body()

suspend fun mentionTimeline(
count: Int,
Expand All @@ -115,11 +132,11 @@ class MisskeyService(
sinceId = since_id,
limit = count
)
)
).body()

suspend fun lookupStatus(
noteId: String,
) = notesApi.notesShow(IPinRequest(noteId = noteId))
) = notesApi.notesShow(IPinRequest(noteId = noteId)).body()

suspend fun childrenTimeline(
noteId: String,
Expand All @@ -133,7 +150,10 @@ class MisskeyService(
sinceId = since_id,
limit = count
)
)
).body()

suspend fun findUserByName(name: String, host: String) =
usersApi.usersShow(UsersShowRequest(username = name, host = host)).body()

}

Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ interface MetaApi {
* @param body * @return [Emojis200Response]
*/
@POST("emojis")
suspend fun emojis(@Body body: kotlin.Any): Response<Emojis200Response>
suspend fun emojis(): Response<Emojis200Response>

/**
* endpoint
Expand Down
Loading

0 comments on commit dc578cb

Please sign in to comment.