Skip to content

Commit

Permalink
Merge pull request #1929 from pantasystem/feature/#1928/client-nyaize
Browse files Browse the repository at this point in the history
クライアント側でNyaize処理を実行するようにした
  • Loading branch information
pantasystem authored Oct 24, 2023
2 parents 29f457e + 7e2b3a0 commit a374a47
Show file tree
Hide file tree
Showing 14 changed files with 314 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ internal class NoteRecordTest {
),
isAcceptingOnlyLikeReaction = false,
isNotAcceptingSensitiveReaction = true,
isRequireNyaize = true,
)
)
record.applyModel(note)
Expand All @@ -315,6 +316,10 @@ internal class NoteRecordTest {
true,
record.misskeyIsNotAcceptingSensitiveReaction
)
Assertions.assertEquals(
true,
record.misskeyIsRequireNyaize,
)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class FuturePagingController<DTO, E>(

override suspend fun loadFuture(): Result<Int> {
if (locker.mutex.isLocked) {
return Result.failure(IllegalStateException())
return Result.failure(IllegalStateException("ローディング中にさらにローディング処理を実行することはできません"))
}
return locker.mutex.withLock {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package net.pantasystem.milktea.common_android.mfm

import android.util.Log
import jp.panta.misskeyandroidclient.mfm.*
import jp.panta.misskeyandroidclient.mfm.EmojiElement
import jp.panta.misskeyandroidclient.mfm.HashTag
import jp.panta.misskeyandroidclient.mfm.Mention
import net.pantasystem.milktea.common.runCancellableCatching
import net.pantasystem.milktea.common_android.emoji.V13EmojiUrlResolver
import net.pantasystem.milktea.common_android.nyaize.nyaize
import net.pantasystem.milktea.model.emoji.CustomEmoji
import net.pantasystem.milktea.model.instance.HostWithVersion
import java.net.URLDecoder
Expand Down Expand Up @@ -36,7 +39,8 @@ object MFMParser {
text: String?,
emojis: List<CustomEmoji>? = emptyList(),
userHost: String? = null,
accountHost: String? = null
accountHost: String? = null,
isRequireProcessNyaize: Boolean = false,
): Root? {
text ?: return null
//println("textSize:${text.length}")
Expand All @@ -50,6 +54,7 @@ object MFMParser {
emojisMap,
userHost = userHost,
accountHost = accountHost,
isRequireProcessNyaize = isRequireProcessNyaize,
).parse()
return root
}
Expand All @@ -59,7 +64,8 @@ object MFMParser {
emojis: Map<String, CustomEmoji>? = null,
instanceEmojis: Map<String, CustomEmoji>? = null,
userHost: String? = null,
accountHost: String? = null
accountHost: String? = null,
isRequireProcessNyaize: Boolean = false,
): Root? {
text ?: return null
//println("textSize:${text.length}")
Expand All @@ -70,6 +76,7 @@ object MFMParser {
instanceEmojiNameMap = instanceEmojis ?: emptyMap(),
userHost = userHost,
accountHost = accountHost,
isRequireProcessNyaize = isRequireProcessNyaize
).parse()
return root
}
Expand All @@ -92,6 +99,7 @@ object MFMParser {
val end: Int = parent.insideEnd,
val userHost: String?,
val accountHost: String?,
val isRequireProcessNyaize: Boolean,
) {
// タグ探索開始
// タグ探索中
Expand Down Expand Up @@ -139,7 +147,12 @@ object MFMParser {
// 文字数が0より多いとき
if ((tagStart - finallyDetected) > 0) {
val text = sourceText.substring(finallyDetected, tagStart)
parent.childElements.add(Text(text, finallyDetected))
val converted = if (isRequireProcessNyaize) {
nyaize(text)
} else {
text
}
parent.childElements.add(Text(converted, finallyDetected))
}
}

Expand Down Expand Up @@ -183,6 +196,7 @@ object MFMParser {
userHost = userHost,
accountHost = accountHost,
instanceEmojiNameMap = instanceEmojiNameMap,
isRequireProcessNyaize = isRequireProcessNyaize,
).parse()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.pantasystem.milktea.common_android.nyaize

fun nyaize(text: String): String {
// 日本語の変換
var result = text.replace("", "にゃ").replace("", "ニャ").replace("", "ニャ")

// 英語の変換
result = result.replace(Regex("(?<=n)a", RegexOption.IGNORE_CASE)) {
if (it.value == "A") "YA" else "ya"
}
result = result.replace(Regex("(?<=morn)ing", RegexOption.IGNORE_CASE)) {
if (it.value == "ING") "YAN" else "yan"
}
result = result.replace(Regex("(?<=every)one", RegexOption.IGNORE_CASE)) {
if (it.value == "ONE") "NYAN" else "nyan"
}

// 韓国語の変換
result = result.replace(Regex("[나-낳]")) {
val offset = ''.code - ''.code
it.value[0].plus(offset).toChar().toString()
}
result = result.replace(Regex("(다$)|(다(?=[.]))|(다(?= ))|(다(?=!))|(다(?=\\?))", RegexOption.MULTILINE)) {
"다냥"
}
result = result.replace(Regex("(야(?=\\?))|(야$)|(야(?= ))", RegexOption.MULTILINE)) {
""
}

return result
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sealed interface TextType {
) : TextType
}

fun getTextType(account: Account, note: NoteRelation, instanceEmojis: Map<String, CustomEmoji>?): TextType? {
fun getTextType(account: Account, note: NoteRelation, instanceEmojis: Map<String, CustomEmoji>?, isRequirePerformNyaize: Boolean = false): TextType? {
return when (account.instanceType) {
Account.InstanceType.MISSKEY, Account.InstanceType.FIREFISH -> {
val root = MFMParser.parse(
Expand All @@ -27,7 +27,8 @@ fun getTextType(account: Account, note: NoteRelation, instanceEmojis: Map<String
instanceEmojis,
userHost = note.user
.host,
accountHost = account.getHost()
accountHost = account.getHost(),
isRequireProcessNyaize = isRequirePerformNyaize,
)
note.note.text?.let {
TextType.Misskey(
Expand Down
7 changes: 6 additions & 1 deletion modules/data/objectbox-models/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"entities": [
{
"id": "1:4355718382021751829",
"lastPropertyId": "54:1696546822899785376",
"lastPropertyId": "55:68607803971520561",
"name": "NoteRecord",
"properties": [
{
Expand Down Expand Up @@ -286,6 +286,11 @@
"id": "54:1696546822899785376",
"name": "customEmojiUrlAndCachePathMap",
"type": 13
},
{
"id": "55:68607803971520561",
"name": "misskeyIsRequireNyaize",
"type": 1
}
],
"relations": []
Expand Down
79 changes: 18 additions & 61 deletions modules/data/objectbox-models/default.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -443,64 +443,6 @@
}
],
"relations": []
},
{
"id": "6:5418605338436881136",
"lastPropertyId": "9:4598656151033408118",
"name": "CustomEmojiRecord",
"properties": [
{
"id": "1:271785103207869909",
"name": "id",
"type": 6,
"flags": 1
},
{
"id": "2:6655539627519293459",
"name": "serverId",
"type": 9
},
{
"id": "3:211101382978291231",
"name": "name",
"indexId": "11:3813410665521821846",
"type": 9,
"flags": 2048
},
{
"id": "4:7927231724161011575",
"name": "emojiHost",
"indexId": "12:6383965324106330542",
"type": 9,
"flags": 2048
},
{
"id": "5:2060901050158269776",
"name": "url",
"type": 9
},
{
"id": "6:1468865394375722898",
"name": "uri",
"type": 9
},
{
"id": "7:3770123434781396120",
"name": "type",
"type": 9
},
{
"id": "8:8187355251103755613",
"name": "category",
"type": 9
},
{
"id": "9:4598656151033408118",
"name": "aliases",
"type": 30
}
],
"relations": []
}
],
"lastEntityId": "6:5418605338436881136",
Expand All @@ -509,9 +451,24 @@
"lastSequenceId": "0:0",
"modelVersion": 5,
"modelVersionParserMinimum": 5,
"retiredEntityUids": [],
"retiredIndexUids": [],
"retiredPropertyUids": [],
"retiredEntityUids": [
5418605338436881136
],
"retiredIndexUids": [
3813410665521821846,
6383965324106330542
],
"retiredPropertyUids": [
271785103207869909,
6655539627519293459,
211101382978291231,
7927231724161011575,
2060901050158269776,
1468865394375722898,
3770123434781396120,
8187355251103755613,
4598656151033408118
],
"retiredRelationUids": [],
"version": 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import net.pantasystem.milktea.model.channel.Channel
import net.pantasystem.milktea.model.drive.FileProperty
import net.pantasystem.milktea.model.emoji.CustomEmojiAspectRatioDataSource
import net.pantasystem.milktea.model.image.ImageCacheRepository
import net.pantasystem.milktea.model.instance.InstanceInfoService
import net.pantasystem.milktea.model.note.Note
import net.pantasystem.milktea.model.note.Visibility
import net.pantasystem.milktea.model.note.poll.Poll
Expand All @@ -21,10 +22,15 @@ import javax.inject.Singleton
class NoteDTOEntityConverter @Inject constructor(
private val customEmojiAspectRatioDataSource: CustomEmojiAspectRatioDataSource,
private val imageCacheRepository: ImageCacheRepository,
private val instanceInfoService: InstanceInfoService,
) {

suspend fun convert(noteDTO: NoteDTO, account: Account): Note {
val emojis = (noteDTO.emojiList + (noteDTO.reactionEmojiList))

val instanceInfo = instanceInfoService.find(account.normalizedInstanceUri).getOrNull()
val isRequireNyaize = (instanceInfo?.isRequirePerformNyaizeFrontend ?: false)
&& (noteDTO.user.isCat ?: false)
val aspects = customEmojiAspectRatioDataSource.findIn(emojis.mapNotNull {
it.url ?: it.uri
}).getOrElse {
Expand Down Expand Up @@ -98,6 +104,7 @@ class NoteDTOEntityConverter @Inject constructor(
ReactionAcceptanceType.NonSensitiveOnly4LocalOnly4Remote -> true
else -> false
},
isRequireNyaize = isRequireNyaize,
),
maxReactionsPerAccount = 1
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ data class NoteRecord(

var customEmojiAspectRatioMap: MutableMap<String, String>? = null,
var customEmojiUrlAndCachePathMap: MutableMap<String, String?>? = null,
var misskeyIsRequireNyaize: Boolean = false,
) {

companion object {
Expand Down Expand Up @@ -172,6 +173,7 @@ data class NoteRecord(
misskeyChannelName = t.channel?.name
misskeyIsAcceptingOnlyLikeReaction = t.isAcceptingOnlyLikeReaction
misskeyIsNotAcceptingSensitiveReaction = t.isNotAcceptingSensitiveReaction
misskeyIsRequireNyaize = t.isRequireNyaize
}
}
customEmojiAspectRatioMap = model.emojis?.mapNotNull { emoji ->
Expand Down Expand Up @@ -241,6 +243,7 @@ data class NoteRecord(
},
isAcceptingOnlyLikeReaction = misskeyIsAcceptingOnlyLikeReaction,
isNotAcceptingSensitiveReaction = misskeyIsNotAcceptingSensitiveReaction,
isRequireNyaize = misskeyIsRequireNyaize,
)
}
"mastodon" -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ class MediatorUserDataSource @Inject constructor(
isSimple: Boolean
): Result<List<User>> = runCancellableCatching {
withContext(ioDispatcher) {
val list = serverIds.distinct().chunked(100).map {
val list = serverIds.distinct().chunked(100).map { chunkedIds ->
if (isSimple) {
userDao.getSimplesInServerIds(accountId, serverIds)
userDao.getSimplesInServerIds(accountId, chunkedIds)
} else {
userDao.getInServerIds(accountId, serverIds)
userDao.getInServerIds(accountId, chunkedIds)
}.map {
it.toModel()
}
Expand Down Expand Up @@ -254,8 +254,8 @@ class MediatorUserDataSource @Inject constructor(
if (serverIds.isEmpty()) {
return flowOf(emptyList())
}
return serverIds.distinct().chunked(50).map {
userDao.observeInServerIds(accountId, serverIds).distinctUntilChanged().map { list ->
return serverIds.distinct().chunked(500).map { chunkedIds ->
userDao.observeInServerIds(accountId, chunkedIds).distinctUntilChanged().map { list ->
list.map {
it.toModel()
}
Expand Down
Loading

0 comments on commit a374a47

Please sign in to comment.