desktop, android: fix parser for reactions (#5629)

* desktop, android: fix parser for reactions

* core: restrict API to known reactions
This commit is contained in:
Evgeny
2025-02-14 23:37:06 +00:00
committed by GitHub
parent 56eaf12840
commit dfe5a4464b
4 changed files with 23 additions and 7 deletions
+6 -2
View File
@@ -3391,8 +3391,12 @@ extension MsgReaction: Decodable {
let type = try container.decode(String.self, forKey: CodingKeys.type)
switch type {
case "emoji":
let emoji = try container.decode(MREmojiChar.self, forKey: CodingKeys.emoji)
self = .emoji(emoji: emoji)
do {
let emoji = try container.decode(MREmojiChar.self, forKey: CodingKeys.emoji)
self = .emoji(emoji: emoji)
} catch {
self = .unknown(type: "emoji")
}
default:
self = .unknown(type: type)
}
@@ -2981,7 +2981,7 @@ sealed class MsgReaction {
MREmojiChar.Heart -> "❤️"
else -> emoji.value
}
is Unknown -> ""
is Unknown -> "?"
}
companion object {
@@ -3003,8 +3003,13 @@ object MsgReactionSerializer : KSerializer<MsgReaction> {
return if (json is JsonObject && "type" in json) {
when(val t = json["type"]?.jsonPrimitive?.content ?: "") {
"emoji" -> {
val emoji = Json.decodeFromString<MREmojiChar>(json["emoji"].toString())
if (emoji == null) MsgReaction.Unknown(t, json) else MsgReaction.Emoji(emoji)
val msgReaction = try {
val emoji = Json.decodeFromString<MREmojiChar>(json["emoji"].toString())
MsgReaction.Emoji(emoji)
} catch (e: Throwable) {
MsgReaction.Unknown(t, json)
}
msgReaction
}
else -> MsgReaction.Unknown(t, json)
}
+2 -2
View File
@@ -8463,8 +8463,8 @@ chatCommandP =
"/_update item " *> (APIUpdateChatItem <$> chatRefP <* A.space <*> A.decimal <*> liveMessageP <* A.space <*> msgContentP),
"/_delete item " *> (APIDeleteChatItem <$> chatRefP <*> _strP <*> _strP),
"/_delete member item #" *> (APIDeleteMemberChatItem <$> A.decimal <*> _strP),
"/_reaction " *> (APIChatItemReaction <$> chatRefP <* A.space <*> A.decimal <* A.space <*> onOffP <* A.space <*> jsonP),
"/_reaction members " *> (APIGetReactionMembers <$> A.decimal <* " #" <*> A.decimal <* A.space <*> A.decimal <* A.space <*> jsonP),
"/_reaction " *> (APIChatItemReaction <$> chatRefP <* A.space <*> A.decimal <* A.space <*> onOffP <* A.space <*> (knownReaction <$?> jsonP)),
"/_reaction members " *> (APIGetReactionMembers <$> A.decimal <* " #" <*> A.decimal <* A.space <*> A.decimal <* A.space <*> (knownReaction <$?> jsonP)),
"/_forward plan " *> (APIPlanForwardChatItems <$> chatRefP <*> _strP),
"/_forward " *> (APIForwardChatItems <$> chatRefP <* A.space <*> chatRefP <*> _strP <*> sendMessageTTLP),
"/_read user " *> (APIUserRead <$> A.decimal),
+7
View File
@@ -407,6 +407,13 @@ data MsgReaction = MREmoji {emoji :: MREmojiChar} | MRUnknown {tag :: Text, json
emojiTag :: IsString a => a
emojiTag = "emoji"
knownReaction :: MsgReaction -> Either String MsgReaction
knownReaction = \case
r@MREmoji {} -> Right r
MRUnknown {} -> Left "unknown MsgReaction"
-- parseJSON for MsgReaction parses unknown emoji reactions as MRUnknown with type "emoji",
-- allowing to add new emojis in a backwards compatible way - UI shows them as ?
instance FromJSON MsgReaction where
parseJSON (J.Object v) = do
tag <- v .: "type"