Skip to content

Commit

Permalink
fix: ref aid
Browse files Browse the repository at this point in the history
  • Loading branch information
storytellerF committed Nov 7, 2024
1 parent 70d2819 commit fc529ec
Show file tree
Hide file tree
Showing 19 changed files with 415 additions and 119 deletions.
10 changes: 5 additions & 5 deletions backend/src/main/kotlin/com/storyteller_f/DatabaseFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,28 @@ object DatabaseFactory {
/**
* 带有transform
*/
suspend fun <T, R> query(transform: (T) -> R, block: suspend () -> T): R =
suspend fun <T, R> query(transform: T.() -> R, block: suspend () -> T): R =
dbQuery { transform(block()) }

/**
* 处理可能查询不到数据的问题
*/
suspend fun <T, R : Any?> queryNotNull(transform: T.() -> R?, block: suspend () -> T?): R? =
query({
it?.let { transform(it) }
this?.let { transform(it) }
}) { block() }

/**
* 带有transform
*/
suspend fun <T, R> mapQuery(transform: (T) -> R, block: suspend () -> SizedIterable<T>): List<R> =
suspend fun <T, R> mapQuery(transform: T.() -> R, block: suspend () -> SizedIterable<T>): List<R> =
dbQuery { block().map(transform) }

/**
* 带有transform
*/
suspend fun <T, R, R1> mapQuery(
transform: (R1) -> R,
transform: R1.() -> R,
typeTransform: (T) -> R1,
block: suspend () -> SizedIterable<T>
): List<R> =
Expand All @@ -99,7 +99,7 @@ object DatabaseFactory {
* @param typeTransform 主要用于将ResultRow 转换成普通数据
*/
suspend fun <T, R, R1> first(
transform: (R1) -> T,
transform: R1.() -> T,
typeTransform: (R) -> R1,
block: suspend () -> SizedIterable<R>
): T? = dbQuery {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import io.ktor.client.statement.*
import io.ktor.http.*

suspend fun HttpClient.requestRoomInfo(id: PrimaryKey) = get("room/$id").body<RoomInfo>()

suspend fun HttpClient.requestRoomInfoByAid(aid: String) = get("room") {
url {
parameters.append("aid", aid)
}
}.body<RoomInfo>()

suspend fun HttpClient.requestRoomKeys(id: PrimaryKey) =
get("room/$id/pub-keys").body<ServerResponse<Pair<PrimaryKey, String>>>()

Expand All @@ -34,6 +41,12 @@ suspend fun HttpClient.getRoomTopics(

suspend fun HttpClient.getCommunityInfo(id: PrimaryKey) = get("community/$id").body<CommunityInfo>()

suspend fun HttpClient.getCommunityInfoByAid(aid: String) = get("community", {
url {
parameters.append("aid", aid)
}
}).body<CommunityInfo>()

suspend fun HttpClient.getCommunityTopics(communityId: PrimaryKey, size: Int) =
get("community/$communityId/topics?size=$size").body<ServerResponse<TopicInfo>>()

Expand Down Expand Up @@ -64,6 +77,17 @@ suspend fun HttpClient.getWorldTopics(nextTopicId: PrimaryKey?, size: Int) = get

suspend fun HttpClient.getUserInfo(id: PrimaryKey) = get("user/$id").body<UserInfo>()

suspend fun HttpClient.updateUserInfo(newUserInfo: UserInfo) = post("user/update") {
contentType(ContentType.Application.Json)
setBody(newUserInfo)
}.body<Int>()

suspend fun HttpClient.getUserInfoByAid(aid: String) = get("user", {
url {
parameters.append("aid", aid)
}
}).body<UserInfo>()

suspend fun HttpClient.getTopicTopics(topicId: PrimaryKey, nextTopicId: PrimaryKey?, size: Int) =
get("topic/$topicId/topics") {
url {
Expand All @@ -73,6 +97,12 @@ suspend fun HttpClient.getTopicTopics(topicId: PrimaryKey, nextTopicId: PrimaryK

suspend fun HttpClient.getTopicInfo(id: PrimaryKey) = get("topic/$id").body<TopicInfo>()

suspend fun HttpClient.getTopicInfoByAid(aid: String) = get("topic") {
url {
parameters.append("aid", aid)
}
}.body<TopicInfo>()

suspend fun HttpClient.getJoinedRooms(size: Int, nextRoomId: PrimaryKey?) = get("room/joined") {
url {
appendPagingQueryParams(size, nextRoomId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,36 @@ import com.storyteller_f.a.app.compontents.*
import com.storyteller_f.a.app.room.RoomList
import com.storyteller_f.a.app.search.CustomSearchBar
import com.storyteller_f.a.app.world.TopicList
import com.storyteller_f.a.client_lib.getCommunityInfo
import com.storyteller_f.a.client_lib.getCommunityRooms
import com.storyteller_f.a.client_lib.getCommunityTopics
import com.storyteller_f.a.client_lib.joinCommunity
import com.storyteller_f.a.client_lib.*
import com.storyteller_f.shared.model.CommunityInfo
import com.storyteller_f.shared.model.RoomInfo
import com.storyteller_f.shared.model.TopicInfo
import com.storyteller_f.shared.type.ObjectType
import com.storyteller_f.shared.type.PrimaryKey
import io.ktor.client.*
import kotlinx.coroutines.launch
import moe.tlaster.precompose.viewmodel.viewModel
import moe.tlaster.precompose.viewmodel.viewModelScope
import org.jetbrains.compose.resources.stringResource

data class OnCommunityJoined(val communityId: PrimaryKey)

class CommunityViewModel(private val communityId: PrimaryKey) : SimpleViewModel<CommunityInfo>() {
class CommunityViewModel(private val requestInfo: suspend HttpClient.() -> CommunityInfo) :
SimpleViewModel<CommunityInfo>() {
constructor(communityId: PrimaryKey) : this({
getCommunityInfo(communityId)
})

constructor(communityAid: String) : this({
getCommunityInfoByAid(communityAid)
})

init {
load()
viewModelScope.launch {
for (i in bus) {
if (i is OnCommunityJoined) {
if (i.communityId == communityId) {
if (i.communityId == handler.data.value?.id) {
handler.refresh()
}
}
Expand All @@ -64,7 +71,7 @@ class CommunityViewModel(private val communityId: PrimaryKey) : SimpleViewModel<
override suspend fun loadInternal() {
handler.request {
serviceCatching {
client.getCommunityInfo(communityId)
requestInfo(client)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ fun CommunityRefCell(communityId: PrimaryKey, onClick: (PrimaryKey) -> Unit) {
CommunityCell(it, onClick)
}
}

@Composable
fun CommunityRefCell(communityAid: String, onClick: (PrimaryKey) -> Unit) {
val viewModel = viewModel(CommunityViewModel::class, keys = listOf("community", communityAid)) {
CommunityViewModel(communityAid)
}
StateView2(viewModel.handler) {
CommunityCell(it, onClick)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import com.storyteller_f.shared.obj.ServerResponse
import com.storyteller_f.shared.type.ObjectType
import com.storyteller_f.shared.type.PrimaryKey
import io.github.aakira.napier.Napier
import io.ktor.client.*
import io.ktor.client.plugins.websocket.*
import kotbase.Expression
import kotbase.MutableDocument
Expand Down Expand Up @@ -153,12 +154,19 @@ class RoomTopicsRemoteMediator(
}
}

class RoomViewModel(private val roomId: PrimaryKey) : SimpleViewModel<RoomInfo>() {
class RoomViewModel(private val requestInfo: suspend HttpClient.() -> RoomInfo) : SimpleViewModel<RoomInfo>() {
constructor(roomId: PrimaryKey) : this({
requestRoomInfo(roomId)
})
constructor(roomAid: String) : this({
requestRoomInfoByAid(roomAid)
})

init {
load()
viewModelScope.launch {
for (i in bus) {
if (i is OnRoomJoined && i.id == roomId) {
if (i is OnRoomJoined && i.id == handler.data.value?.id) {
handler.refresh()
}
}
Expand All @@ -168,7 +176,7 @@ class RoomViewModel(private val roomId: PrimaryKey) : SimpleViewModel<RoomInfo>(
override suspend fun loadInternal() {
handler.request {
serviceCatching {
client.requestRoomInfo(roomId)
requestInfo(client)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ fun RoomRefCell(roomId: PrimaryKey, onClick: (PrimaryKey) -> Unit) {
RoomCell(it, onClick)
}
}

@Composable
fun RoomRefCell(roomAid: String, onClick: (PrimaryKey) -> Unit) {
val viewModel = viewModel(RoomViewModel::class, keys = listOf("room", roomAid)) {
RoomViewModel(roomAid)
}
StateView2(viewModel.handler) {
RoomCell(it, onClick)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ import org.intellij.markdown.MarkdownTokenTypes
import org.intellij.markdown.ast.ASTNode
import org.intellij.markdown.ast.getTextInNode

class TopicRoute(val pattern: String, val builder: @Composable () -> Unit)

// private val ROUTE = mutableListOf(
// TopicRoute("/topic/{id}") {
// },
// TopicRoute("/root/{id}") {
// },
// TopicRoute("/community/{id}") {
// }
// )

@Composable
fun TopicCell(
topicInfo: TopicInfo?,
Expand Down Expand Up @@ -123,15 +134,25 @@ fun TopicRefCell(topicId: PrimaryKey, onClick: (PrimaryKey) -> Unit) {
}

StateView2(viewModel.handler) {
TopicRefCellContent(it, onClick, topicId)
TopicRefCellContent(it, onClick)
}
}

@Composable
fun TopicRefCell(topicAid: String, onClick: (PrimaryKey) -> Unit) {
val viewModel = viewModel(TopicViewModel::class, keys = listOf("topic", topicAid)) {
TopicViewModel(topicAid)
}

StateView2(viewModel.handler) {
TopicRefCellContent(it, onClick)
}
}

@Composable
private fun TopicRefCellContent(
it: TopicInfo,
onClick: (PrimaryKey) -> Unit,
topicId: PrimaryKey,
) {
val author = it.author
val authorViewModel = viewModel(UserViewModel::class, keys = listOf("user", author)) {
Expand All @@ -141,7 +162,7 @@ private fun TopicRefCellContent(
Row(
modifier = Modifier.fillMaxWidth()
.background(MaterialTheme.colorScheme.surfaceContainer, RoundedCornerShape(4.dp)).clickable {
onClick(topicId)
onClick(it.id)
}.padding(10.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically
Expand Down Expand Up @@ -187,22 +208,55 @@ private fun RefBlock(
val textInNode = readFenceContent(children, langOffset, content)
val uri = Uri.parse(textInNode)
return if (uri.pathSegments.size == 2) {
val id = uri.pathSegments[1].toULong()
val p1 = uri.pathSegments[0]
when (p1) {
"topic" -> TopicRefCell(id) {
onClick(it, ObjectType.TOPIC)
"topic" -> {
val id = uri.pathSegments[1].toULongOrNull()
if (id != null) {
TopicRefCell(id) {
onClick(it, ObjectType.TOPIC)
}
} else {
TopicRefCell(uri.pathSegments[1]) {
onClick(it, ObjectType.TOPIC)
}
}
}

"room" -> RoomRefCell(roomId = id) {
onClick(it, ObjectType.ROOM)
"room" -> {
val id = uri.pathSegments[1].toULongOrNull()
if (id != null) {
RoomRefCell(id) {
onClick(it, ObjectType.ROOM)
}
} else {
RoomRefCell(uri.pathSegments[1]) {
onClick(it, ObjectType.ROOM)
}
}
}

"community" -> CommunityRefCell(communityId = id) {
onClick(it, ObjectType.COMMUNITY)
"community" -> {
val id = uri.pathSegments[1].toULongOrNull()
if (id != null) {
CommunityRefCell(id) {
onClick(it, ObjectType.COMMUNITY)
}
} else {
CommunityRefCell(uri.pathSegments[1]) {
onClick(it, ObjectType.COMMUNITY)
}
}
}

"user" -> UserRefCell(userId = id)
"user" -> {
val id = uri.pathSegments[1].toULongOrNull()
if (id != null) {
UserRefCell(userId = id)
} else {
UserRefCell(userAid = uri.pathSegments[1])
}
}

else -> null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@ import com.storyteller_f.a.app.client
import com.storyteller_f.a.app.common.*
import com.storyteller_f.a.app.compontents.ReactionRow
import com.storyteller_f.a.app.search.CustomSearchBar
import com.storyteller_f.a.client_lib.ClientSession
import com.storyteller_f.a.client_lib.LoginViewModel
import com.storyteller_f.a.client_lib.getTopicInfo
import com.storyteller_f.a.client_lib.getTopicTopics
import com.storyteller_f.a.client_lib.*
import com.storyteller_f.shared.decrypt
import com.storyteller_f.shared.getDerPrivateKey
import com.storyteller_f.shared.model.TopicContent
import com.storyteller_f.shared.model.TopicInfo
import com.storyteller_f.shared.obj.ServerResponse
import com.storyteller_f.shared.type.ObjectType
import com.storyteller_f.shared.type.PrimaryKey
import io.ktor.client.*
import moe.tlaster.precompose.viewmodel.viewModel

@Composable
Expand Down Expand Up @@ -83,15 +81,23 @@ fun TopicPage(topicId: PrimaryKey, onClick: (PrimaryKey, ObjectType) -> Unit) {
}
}

class TopicViewModel(private val topicId: PrimaryKey) : SimpleViewModel<TopicInfo>() {
class TopicViewModel(private val requestInfo: suspend HttpClient.() -> TopicInfo) : SimpleViewModel<TopicInfo>() {
constructor(topicId: PrimaryKey) : this({
getTopicInfo(topicId)
})

constructor(topicAid: String) : this({
getTopicInfoByAid(topicAid)
})

init {
load()
}

override suspend fun loadInternal() {
handler.request {
serviceCatching {
client.getTopicInfo(topicId)
requestInfo(client)
}
}
}
Expand Down
Loading

0 comments on commit fc529ec

Please sign in to comment.