Skip to content

Commit

Permalink
refactor(帖子内容相关): 帖子内容从字符串改为node tree形式
Browse files Browse the repository at this point in the history
  • Loading branch information
nullaqua committed Sep 25, 2024
1 parent 1ca6818 commit a68bbd2
Show file tree
Hide file tree
Showing 13 changed files with 404 additions and 121 deletions.
66 changes: 25 additions & 41 deletions src/main/kotlin/subit/dataClasses/Post.kt
Original file line number Diff line number Diff line change
@@ -1,57 +1,39 @@
package subit.dataClasses

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
import org.koin.core.component.KoinComponent
import subit.dataClasses.PostFullBasicInfo.Companion.SUB_CONTENT_LENGTH
import subit.plugin.contentNegotiationJson
import subit.utils.getContentText

@Serializable
data class PostVersionInfo(
val id: PostVersionId,
val post: PostId,
val title: String,
val content: String?,
val operation: Operation?,
val content: JsonElement,
val time: Long,
val draft: Boolean,
)
{

@Serializable
data class Interval(val start: Int, val end: Int)

@Serializable
data class Operation(
val insert: Map<Int, String> = mapOf(),
val del: List<Interval> = listOf(),
val newTitle: String,
val draft: Boolean,
val oldVersionId: PostVersionId,
)
{
companion object
{
val example = Operation(mapOf(0 to "a"), listOf(Interval(1, 2)), "new title", false, PostVersionId(0))
}
}

companion object
{
val example0 = PostVersionInfo(
val example = PostVersionInfo(
PostVersionId(1),
PostId(1),
"标题",
"内容",
null,
System.currentTimeMillis(),
false,
)

val example1 = PostVersionInfo(
PostVersionId(1),
PostId(1),
"标题",
null,
Operation.example,
contentNegotiationJson.parseToJsonElement("""
[
{
"id":"ff6br",
"children":[
{
"text":"1"
}
],
"type":"p"
}
]""".trimIndent()),
System.currentTimeMillis(),
false,
)
Expand All @@ -72,7 +54,7 @@ data class PostVersionBasicInfo(
{
companion object
{
val example = PostVersionInfo.example0.toPostVersionBasicInfo()
val example = PostVersionInfo.example.toPostVersionBasicInfo()
}
}

Expand Down Expand Up @@ -105,7 +87,7 @@ data class PostInfo(

fun toPostFull(
title: String,
content: String,
content: JsonElement,
create: Long,
lastModified: Long,
lastVersionId: PostVersionId,
Expand Down Expand Up @@ -143,7 +125,7 @@ data class PostInfo(
data class PostFull(
val id: PostId,
val title: String?,
val content: String?,
val content: JsonElement?,
val author: UserId,
val anonymous: Boolean,
val create: Long?,
Expand All @@ -168,7 +150,7 @@ data class PostFull(
PostFullBasicInfo(
id,
title,
content?.let { if (it.length > SUB_CONTENT_LENGTH) "${it.substring(0, SUB_CONTENT_LENGTH)}" else it },
content?.let { getContentText(it) },
author,
anonymous,
create,
Expand All @@ -191,7 +173,7 @@ data class PostFull(
val example = PostFull(
PostId(1),
"帖子标题",
"帖子内容",
PostVersionInfo.example.content,
UserId(1),
false,
System.currentTimeMillis(),
Expand Down Expand Up @@ -236,6 +218,8 @@ data class PostFullBasicInfo(
companion object
{
val example = PostFull.example.toPostFullBasicInfo()
const val SUB_CONTENT_LENGTH = 100
}

fun toPostInfo(): PostInfo =
PostInfo(id, author, anonymous, view, block, top, state, parent, root)
}
3 changes: 2 additions & 1 deletion src/main/kotlin/subit/database/PostVersions.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package subit.database

import kotlinx.serialization.json.JsonElement
import subit.dataClasses.*

interface PostVersions
Expand All @@ -14,7 +15,7 @@ interface PostVersions
suspend fun createPostVersion(
post: PostId,
title: String,
content: String,
content: JsonElement,
draft: Boolean,
): PostVersionId

Expand Down
9 changes: 4 additions & 5 deletions src/main/kotlin/subit/database/memoryImpl/PostVersionsImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package subit.database.memoryImpl
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.datetime.Instant
import kotlinx.serialization.json.JsonElement
import org.koin.core.component.KoinComponent
import subit.dataClasses.*
import subit.database.PostVersions
import subit.dataClasses.Slice.Companion.asSlice
import subit.plugin.contentNegotiationJson
import subit.database.PostVersions
import subit.utils.toInstant

class PostVersionsImpl: PostVersions, KoinComponent
Expand All @@ -20,16 +20,15 @@ class PostVersionsImpl: PostVersions, KoinComponent
override suspend fun createPostVersion(
post: PostId,
title: String,
content: String,
content: JsonElement,
draft: Boolean
): PostVersionId = lock.withLock()
{
val postVersionInfo = PostVersionInfo(
PostVersionId(list.size + 1L),
post,
title,
if (draft) null else content,
if (draft) contentNegotiationJson.decodeFromString(content) else null,
content,
System.currentTimeMillis(),
draft,
)
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/subit/database/memoryImpl/PostsImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import subit.dataClasses.PostId.Companion.toPostId
import subit.dataClasses.Slice.Companion.asSlice
import subit.database.*
import subit.router.utils.withPermission
import subit.utils.getContentText
import subit.utils.toInstant
import java.util.*
import kotlin.math.pow
Expand Down Expand Up @@ -138,7 +139,7 @@ class PostsImpl: Posts, KoinComponent

return post.toPostFull(
title = lastVersion.title,
content = lastVersion.content!!,
content = lastVersion.content,
lastModified = (postVersions as PostVersionsImpl).getLastModified(pid).toEpochMilliseconds(),
create = (postVersions as PostVersionsImpl).getCreate(pid).toEpochMilliseconds(),
like = likes.getLikesCount(pid),
Expand Down Expand Up @@ -196,7 +197,7 @@ class PostsImpl: Posts, KoinComponent
&& (createAfter == null || it.create!! >= createAfter.toEpochMilliseconds())
&& (lastModifiedBefore == null || it.lastModified!! <= lastModifiedBefore.toEpochMilliseconds())
&& (lastModifiedAfter == null || it.lastModified!! >= lastModifiedAfter.toEpochMilliseconds())
&& (containsKeyWord == null || (it.title?.contains(containsKeyWord) == true) || (it.content?.contains(containsKeyWord)) == true)
&& (containsKeyWord == null || (it.title?.contains(containsKeyWord) == true) || (it.content?.let(::getContentText)?.contains(containsKeyWord)) == true)

}
.sortedBy(sortBy(sortBy))
Expand Down
55 changes: 32 additions & 23 deletions src/main/kotlin/subit/database/sqlImpl/PostVersionsImpl.kt
Original file line number Diff line number Diff line change
@@ -1,66 +1,75 @@
package subit.database.sqlImpl

import kotlinx.serialization.json.JsonElement
import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.SortOrder
import org.jetbrains.exposed.sql.andWhere
import org.jetbrains.exposed.sql.insertAndGetId
import org.jetbrains.exposed.sql.json.jsonb
import org.jetbrains.exposed.sql.kotlin.datetime.CurrentTimestamp
import org.jetbrains.exposed.sql.kotlin.datetime.timestamp
import org.koin.core.component.KoinComponent
import subit.dataClasses.*
import subit.dataClasses.Slice
import subit.database.PostVersions
import subit.database.sqlImpl.utils.asSlice
import subit.database.sqlImpl.utils.singleOrNull
import subit.plugin.contentNegotiationJson
import subit.plugin.dataJson
import subit.utils.getContentText

class PostVersionsImpl: DaoSqlImpl<PostVersionsImpl.PostVersionsTable>(PostVersionsTable), PostVersions, KoinComponent
class PostVersionsImpl: DaoSqlImpl<PostVersionsImpl.PostVersionTable>(PostVersionTable), PostVersions, KoinComponent
{
object PostVersionsTable: IdTable<PostVersionId>("post_versions")
object PostVersionTable: IdTable<PostVersionId>("post_versions")
{
override val id = postVersionId("id").autoIncrement().entityId()
val post = reference("post", PostsImpl.PostsTable)
val title = varchar("title", 255)
val content = text("content")
val content = jsonb<JsonElement>("content", dataJson)
val textContent = varchar("text_content", 255)
val time = timestamp("time").defaultExpression(CurrentTimestamp).index()
val draft = bool("draft").default(false)
override val primaryKey = PrimaryKey(id)
}

private val postVersionColumns = PostVersionTable.columns - PostVersionTable.textContent
private val postVersionBasicColumns = postVersionColumns - PostVersionTable.content - PostVersionTable.textContent

private fun deserializePostVersion(row: ResultRow): PostVersionInfo = PostVersionInfo(
id = row[PostVersionsTable.id].value,
post = row[PostVersionsTable.post].value,
title = row[PostVersionsTable.title],
content = if (!row[PostVersionsTable.draft]) row[PostVersionsTable.content] else null,
operation = if (row[PostVersionsTable.draft]) contentNegotiationJson.decodeFromString(row[PostVersionsTable.content]) else null,
time = row[PostVersionsTable.time].toEpochMilliseconds(),
draft = row[PostVersionsTable.draft],
id = row[PostVersionTable.id].value,
post = row[PostVersionTable.post].value,
title = row[PostVersionTable.title],
content = row[PostVersionTable.content],
time = row[PostVersionTable.time].toEpochMilliseconds(),
draft = row[PostVersionTable.draft],
)

private fun deserializePostVersionBasicInfo(row: ResultRow): PostVersionBasicInfo = PostVersionBasicInfo(
id = row[PostVersionsTable.id].value,
post = row[PostVersionsTable.post].value,
title = row[PostVersionsTable.title],
time = row[PostVersionsTable.time].toEpochMilliseconds(),
draft = row[PostVersionsTable.draft],
id = row[PostVersionTable.id].value,
post = row[PostVersionTable.post].value,
title = row[PostVersionTable.title],
time = row[PostVersionTable.time].toEpochMilliseconds(),
draft = row[PostVersionTable.draft],
)

override suspend fun createPostVersion(
post: PostId,
title: String,
content: String,
content: JsonElement,
draft: Boolean
): PostVersionId = query()
{
insertAndGetId {
it[this.post] = post
it[this.title] = title
it[this.content] = content
it[this.textContent] = getContentText(content)
it[this.draft] = draft
}.value
}

override suspend fun getPostVersion(pid: PostVersionId): PostVersionInfo? = query()
{
selectAll().where { id eq pid }.singleOrNull()?.let(::deserializePostVersion)
select(postVersionColumns).where { id eq pid }.singleOrNull()?.let(::deserializePostVersion)
}

override suspend fun getPostVersions(
Expand All @@ -70,8 +79,8 @@ class PostVersionsImpl: DaoSqlImpl<PostVersionsImpl.PostVersionsTable>(PostVersi
count: Int
): Slice<PostVersionBasicInfo> = query()
{
select(id, table.post, title, time, draft)
.andWhere { PostVersionsTable.post eq post }
select(postVersionBasicColumns)
.andWhere { PostVersionTable.post eq post }
.apply { if (!containsDraft) andWhere { draft eq false } }
.orderBy(time, SortOrder.DESC)
.asSlice(begin, count)
Expand All @@ -81,7 +90,7 @@ class PostVersionsImpl: DaoSqlImpl<PostVersionsImpl.PostVersionsTable>(PostVersi
override suspend fun getLatestPostVersion(post: PostId, containsDraft: Boolean): PostVersionId? = query()
{
select(id)
.andWhere { PostVersionsTable.post eq post }
.andWhere { PostVersionTable.post eq post }
.apply { if (!containsDraft) andWhere { draft eq false } }
.orderBy(time, SortOrder.DESC)
.singleOrNull()
Expand Down
Loading

0 comments on commit a68bbd2

Please sign in to comment.