core: MCFile (#580)
This commit is contained in:
1 parent
a7554771a0
commit
c48a516586
2 files changed
+14
-3
No files matched your search
@@ -148,18 +148,20 @@ cmToQuotedMsg = \case
|
|||||||
XMsgNew (MCQuote quotedMsg _) -> Just quotedMsg
|
XMsgNew (MCQuote quotedMsg _) -> Just quotedMsg
|
||||||
_ -> Nothing
|
_ -> Nothing
|
||||||
|
|
||||||
data MsgContentTag = MCText_ | MCLink_ | MCImage_ | MCUnknown_ Text
|
data MsgContentTag = MCText_ | MCLink_ | MCImage_ | MCFile_ | MCUnknown_ Text
|
||||||
|
|
||||||
instance StrEncoding MsgContentTag where
|
instance StrEncoding MsgContentTag where
|
||||||
strEncode = \case
|
strEncode = \case
|
||||||
MCText_ -> "text"
|
MCText_ -> "text"
|
||||||
MCLink_ -> "link"
|
MCLink_ -> "link"
|
||||||
MCImage_ -> "image"
|
MCImage_ -> "image"
|
||||||
|
MCFile_ -> "file"
|
||||||
MCUnknown_ t -> encodeUtf8 t
|
MCUnknown_ t -> encodeUtf8 t
|
||||||
strDecode = \case
|
strDecode = \case
|
||||||
"text" -> Right MCText_
|
"text" -> Right MCText_
|
||||||
"link" -> Right MCLink_
|
"link" -> Right MCLink_
|
||||||
"image" -> Right MCImage_
|
"image" -> Right MCImage_
|
||||||
|
"file" -> Right MCFile_
|
||||||
t -> Right . MCUnknown_ $ safeDecodeUtf8 t
|
t -> Right . MCUnknown_ $ safeDecodeUtf8 t
|
||||||
strP = strDecode <$?> A.takeTill (== ' ')
|
strP = strDecode <$?> A.takeTill (== ' ')
|
||||||
|
|
||||||
@@ -196,6 +198,7 @@ data MsgContent
|
|||||||
= MCText Text
|
= MCText Text
|
||||||
| MCLink {text :: Text, preview :: LinkPreview}
|
| MCLink {text :: Text, preview :: LinkPreview}
|
||||||
| MCImage {text :: Text, image :: ImageData}
|
| MCImage {text :: Text, image :: ImageData}
|
||||||
|
| MCFile Text
|
||||||
| MCUnknown {tag :: Text, text :: Text, json :: J.Object}
|
| MCUnknown {tag :: Text, text :: Text, json :: J.Object}
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
@@ -204,6 +207,7 @@ msgContentText = \case
|
|||||||
MCText t -> t
|
MCText t -> t
|
||||||
MCLink {text} -> text
|
MCLink {text} -> text
|
||||||
MCImage {text} -> text
|
MCImage {text} -> text
|
||||||
|
MCFile t -> t
|
||||||
MCUnknown {text} -> text
|
MCUnknown {text} -> text
|
||||||
|
|
||||||
msgContentTag :: MsgContent -> MsgContentTag
|
msgContentTag :: MsgContent -> MsgContentTag
|
||||||
@@ -211,6 +215,7 @@ msgContentTag = \case
|
|||||||
MCText _ -> MCText_
|
MCText _ -> MCText_
|
||||||
MCLink {} -> MCLink_
|
MCLink {} -> MCLink_
|
||||||
MCImage {} -> MCImage_
|
MCImage {} -> MCImage_
|
||||||
|
MCFile {} -> MCFile_
|
||||||
MCUnknown {tag} -> MCUnknown_ tag
|
MCUnknown {tag} -> MCUnknown_ tag
|
||||||
|
|
||||||
data ExtMsgContent = ExtMsgContent MsgContent (Maybe FileInvitation)
|
data ExtMsgContent = ExtMsgContent MsgContent (Maybe FileInvitation)
|
||||||
@@ -236,6 +241,7 @@ instance FromJSON MsgContent where
|
|||||||
text <- v .: "text"
|
text <- v .: "text"
|
||||||
image <- v .: "image"
|
image <- v .: "image"
|
||||||
pure MCImage {image, text}
|
pure MCImage {image, text}
|
||||||
|
MCFile_ -> MCFile <$> v .: "text"
|
||||||
MCUnknown_ tag -> do
|
MCUnknown_ tag -> do
|
||||||
text <- fromMaybe unknownMsgType <$> v .:? "text"
|
text <- fromMaybe unknownMsgType <$> v .:? "text"
|
||||||
pure MCUnknown {tag, text, json = v}
|
pure MCUnknown {tag, text, json = v}
|
||||||
@@ -261,11 +267,13 @@ instance ToJSON MsgContent where
|
|||||||
MCText t -> J.object ["type" .= MCText_, "text" .= t]
|
MCText t -> J.object ["type" .= MCText_, "text" .= t]
|
||||||
MCLink {text, preview} -> J.object ["type" .= MCLink_, "text" .= text, "preview" .= preview]
|
MCLink {text, preview} -> J.object ["type" .= MCLink_, "text" .= text, "preview" .= preview]
|
||||||
MCImage {text, image} -> J.object ["type" .= MCImage_, "text" .= text, "image" .= image]
|
MCImage {text, image} -> J.object ["type" .= MCImage_, "text" .= text, "image" .= image]
|
||||||
|
MCFile t -> J.object ["type" .= MCFile_, "text" .= t]
|
||||||
toEncoding = \case
|
toEncoding = \case
|
||||||
MCUnknown {json} -> JE.value $ J.Object json
|
MCUnknown {json} -> JE.value $ J.Object json
|
||||||
MCText t -> J.pairs $ "type" .= MCText_ <> "text" .= t
|
MCText t -> J.pairs $ "type" .= MCText_ <> "text" .= t
|
||||||
MCLink {text, preview} -> J.pairs $ "type" .= MCLink_ <> "text" .= text <> "preview" .= preview
|
MCLink {text, preview} -> J.pairs $ "type" .= MCLink_ <> "text" .= text <> "preview" .= preview
|
||||||
MCImage {text, image} -> J.pairs $ "type" .= MCImage_ <> "text" .= text <> "image" .= image
|
MCImage {text, image} -> J.pairs $ "type" .= MCImage_ <> "text" .= text <> "image" .= image
|
||||||
|
MCFile t -> J.pairs $ "type" .= MCFile_ <> "text" .= t
|
||||||
|
|
||||||
instance ToField MsgContent where
|
instance ToField MsgContent where
|
||||||
toField = toField . safeDecodeUtf8 . LB.toStrict . J.encode
|
toField = toField . safeDecodeUtf8 . LB.toStrict . J.encode
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ decodeChatMessageTest = describe "Chat message encoding/decoding" $ do
|
|||||||
#==# XMsgNew (MCSimple (ExtMsgContent (MCImage "here's an image" $ ImageData "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII=") Nothing))
|
#==# XMsgNew (MCSimple (ExtMsgContent (MCImage "here's an image" $ ImageData "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII=") Nothing))
|
||||||
it "x.msg.new chat message " $
|
it "x.msg.new chat message " $
|
||||||
"{\"msgId\":\"AQIDBA==\",\"event\":\"x.msg.new\",\"params\":{\"content\":{\"text\":\"hello\",\"type\":\"text\"}}}"
|
"{\"msgId\":\"AQIDBA==\",\"event\":\"x.msg.new\",\"params\":{\"content\":{\"text\":\"hello\",\"type\":\"text\"}}}"
|
||||||
##==## (ChatMessage (Just $ SharedMsgId "\1\2\3\4") (XMsgNew (MCSimple (ExtMsgContent (MCText "hello") Nothing))))
|
##==## ChatMessage (Just $ SharedMsgId "\1\2\3\4") (XMsgNew (MCSimple (ExtMsgContent (MCText "hello") Nothing)))
|
||||||
it "x.msg.new quote" $
|
it "x.msg.new quote" $
|
||||||
"{\"msgId\":\"AQIDBA==\",\"event\":\"x.msg.new\",\"params\":{\"content\":{\"text\":\"hello to you too\",\"type\":\"text\"},\"quote\":{\"content\":{\"text\":\"hello there!\",\"type\":\"text\"},\"msgRef\":{\"msgId\":\"BQYHCA==\",\"sent\":true,\"sentAt\":\"1970-01-01T00:00:01.000000001Z\"}}}}"
|
"{\"msgId\":\"AQIDBA==\",\"event\":\"x.msg.new\",\"params\":{\"content\":{\"text\":\"hello to you too\",\"type\":\"text\"},\"quote\":{\"content\":{\"text\":\"hello there!\",\"type\":\"text\"},\"msgRef\":{\"msgId\":\"BQYHCA==\",\"sent\":true,\"sentAt\":\"1970-01-01T00:00:01.000000001Z\"}}}}"
|
||||||
##==## ChatMessage
|
##==## ChatMessage
|
||||||
@@ -123,9 +123,12 @@ decodeChatMessageTest = describe "Chat message encoding/decoding" $ do
|
|||||||
it "x.msg.new forward" $
|
it "x.msg.new forward" $
|
||||||
"{\"msgId\":\"AQIDBA==\",\"event\":\"x.msg.new\",\"params\":{\"content\":{\"text\":\"hello\",\"type\":\"text\"},\"forward\":true}}"
|
"{\"msgId\":\"AQIDBA==\",\"event\":\"x.msg.new\",\"params\":{\"content\":{\"text\":\"hello\",\"type\":\"text\"},\"forward\":true}}"
|
||||||
##==## ChatMessage (Just $ SharedMsgId "\1\2\3\4") (XMsgNew $ MCForward (ExtMsgContent (MCText "hello") Nothing))
|
##==## ChatMessage (Just $ SharedMsgId "\1\2\3\4") (XMsgNew $ MCForward (ExtMsgContent (MCText "hello") Nothing))
|
||||||
it "x.msg.new simple with file" $
|
it "x.msg.new simple text with file" $
|
||||||
"{\"event\":\"x.msg.new\",\"params\":{\"content\":{\"text\":\"hello\",\"type\":\"text\"},\"file\":{\"fileSize\":12345,\"fileName\":\"photo.jpg\"}}}"
|
"{\"event\":\"x.msg.new\",\"params\":{\"content\":{\"text\":\"hello\",\"type\":\"text\"},\"file\":{\"fileSize\":12345,\"fileName\":\"photo.jpg\"}}}"
|
||||||
#==# XMsgNew (MCSimple (ExtMsgContent (MCText "hello") (Just FileInvitation {fileName = "photo.jpg", fileSize = 12345, fileConnReq = Nothing})))
|
#==# XMsgNew (MCSimple (ExtMsgContent (MCText "hello") (Just FileInvitation {fileName = "photo.jpg", fileSize = 12345, fileConnReq = Nothing})))
|
||||||
|
it "x.msg.new simple file with file" $
|
||||||
|
"{\"event\":\"x.msg.new\",\"params\":{\"content\":{\"text\":\"\",\"type\":\"file\"},\"file\":{\"fileSize\":12345,\"fileName\":\"file.txt\"}}}"
|
||||||
|
#==# XMsgNew (MCSimple (ExtMsgContent (MCFile "") (Just FileInvitation {fileName = "file.txt", fileSize = 12345, fileConnReq = Nothing})))
|
||||||
it "x.msg.new quote with file" $
|
it "x.msg.new quote with file" $
|
||||||
"{\"msgId\":\"AQIDBA==\",\"event\":\"x.msg.new\",\"params\":{\"content\":{\"text\":\"hello to you too\",\"type\":\"text\"},\"quote\":{\"content\":{\"text\":\"hello there!\",\"type\":\"text\"},\"msgRef\":{\"msgId\":\"BQYHCA==\",\"sent\":true,\"sentAt\":\"1970-01-01T00:00:01.000000001Z\"}},\"file\":{\"fileSize\":12345,\"fileName\":\"photo.jpg\"}}}"
|
"{\"msgId\":\"AQIDBA==\",\"event\":\"x.msg.new\",\"params\":{\"content\":{\"text\":\"hello to you too\",\"type\":\"text\"},\"quote\":{\"content\":{\"text\":\"hello there!\",\"type\":\"text\"},\"msgRef\":{\"msgId\":\"BQYHCA==\",\"sent\":true,\"sentAt\":\"1970-01-01T00:00:01.000000001Z\"}},\"file\":{\"fileSize\":12345,\"fileName\":\"photo.jpg\"}}}"
|
||||||
##==## ChatMessage
|
##==## ChatMessage
|
||||||
|
|||||||
Reference in new issue
Block a user