From eb9e82483b70fac25603ae5b584a2f2a9aec1c15 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 20 Dec 2018 22:01:09 -0800 Subject: [PATCH] Add markdown sending. --- src/Main.elm | 20 +++++++++++++++----- src/Scylla/Http.elm | 8 ++++++++ src/Scylla/Markdown.elm | 15 +++++++++++++++ src/Scylla/Model.elm | 2 ++ static/js/markdown.js | 9 +++++++++ 5 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 src/Scylla/Markdown.elm create mode 100644 static/js/markdown.js diff --git a/src/Main.elm b/src/Main.elm index 8ed3e50..a6dfe27 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -11,6 +11,7 @@ import Scylla.Route exposing (Route(..), RoomId) import Scylla.UserData exposing (..) import Scylla.Notification exposing (..) import Scylla.Storage exposing (..) +import Scylla.Markdown exposing (..) import Url exposing (Url) import Url.Parser exposing (parse) import Url.Builder @@ -90,6 +91,17 @@ update msg model = case msg of FileUploadComplete rid mime ur -> updateFileUploadComplete model rid mime ur SendImageResponse _ -> (model, Cmd.none) SendFileResponse _ -> (model, Cmd.none) + ReceiveMarkdown md -> updateMarkdown model md + +updateMarkdown : Model -> MarkdownResponse -> (Model, Cmd Msg) +updateMarkdown m { roomId, text, markdown } = + let + storeValueCmd = setStoreValuePort ("scylla.loginInfo", Json.Encode.string + <| encodeLoginInfo + <| LoginInfo (Maybe.withDefault "" m.token) m.apiUrl m.loginUsername (m.transactionId + 1)) + sendMessageCmd = sendMarkdownMessage m.apiUrl (Maybe.withDefault "" m.token) (m.transactionId + 1) roomId text markdown + in + ({ m | transactionId = m.transactionId + 1 }, Cmd.batch [ storeValueCmd, sendMessageCmd ]) updateFileUploadComplete : Model -> RoomId -> String -> (Result Http.Error String) -> (Model, Cmd Msg) updateFileUploadComplete m rid mime ur = @@ -224,14 +236,11 @@ updateSendRoomText m r = combinedCmd = case message of Nothing -> Cmd.none Just s -> Cmd.batch - [ sendTextMessage m.apiUrl token m.transactionId r s + [ requestMarkdownPort { roomId = r, text = s } , sendTypingIndicator m.apiUrl token r m.loginUsername False typingTimeout - , setStoreValuePort ("scylla.loginInfo", Json.Encode.string - <| encodeLoginInfo - <| LoginInfo (Maybe.withDefault "" m.token) m.apiUrl m.loginUsername (m.transactionId + 1)) ] in - ({ m | roomText = Dict.insert r "" m.roomText, transactionId = m.transactionId + 1 }, combinedCmd) + ({ m | roomText = Dict.insert r "" m.roomText }, combinedCmd) updateTryUrl : Model -> Browser.UrlRequest -> (Model, Cmd Msg) updateTryUrl m ur = case ur of @@ -315,6 +324,7 @@ subscriptions m = [ onNotificationClickPort OpenRoom , receiveStoreValuePort ReceiveStoreData , typingTimer + , receiveMarkdownPort ReceiveMarkdown ] onUrlRequest : Browser.UrlRequest -> Msg diff --git a/src/Scylla/Http.elm b/src/Scylla/Http.elm index e48b2bf..74faa93 100644 --- a/src/Scylla/Http.elm +++ b/src/Scylla/Http.elm @@ -76,6 +76,14 @@ sendMessage apiUrl token transactionId room msg contents = request , tracker = Nothing } +sendMarkdownMessage : ApiUrl -> ApiToken -> Int -> RoomId -> String -> String -> Cmd Msg +sendMarkdownMessage apiUrl token transactionId room message md = sendMessage apiUrl token transactionId room SendRoomTextResponse + [ ("msgtype", string "m.text") + , ("body", string message) + , ("formatted_body", string md) + , ("format", string "org.matrix.custom.html") + ] + sendTextMessage : ApiUrl -> ApiToken -> Int -> RoomId -> String -> Cmd Msg sendTextMessage apiUrl token transactionId room message = sendMessage apiUrl token transactionId room SendRoomTextResponse [ ("msgtype", string "m.text") diff --git a/src/Scylla/Markdown.elm b/src/Scylla/Markdown.elm new file mode 100644 index 0000000..9b56019 --- /dev/null +++ b/src/Scylla/Markdown.elm @@ -0,0 +1,15 @@ +port module Scylla.Markdown exposing (..) + +type alias MarkdownRequest = + { roomId : String + , text : String + } + +type alias MarkdownResponse = + { roomId : String + , text : String + , markdown : String + } + +port requestMarkdownPort : MarkdownRequest -> Cmd msg +port receiveMarkdownPort : (MarkdownResponse -> msg) -> Sub msg diff --git a/src/Scylla/Model.elm b/src/Scylla/Model.elm index 7ee5c30..459a956 100644 --- a/src/Scylla/Model.elm +++ b/src/Scylla/Model.elm @@ -5,6 +5,7 @@ import Scylla.Login exposing (LoginResponse, Username, Password) import Scylla.UserData exposing (UserData) import Scylla.Route exposing (Route(..), RoomId) import Scylla.Storage exposing (..) +import Scylla.Markdown exposing (..) import Browser.Navigation as Nav import Browser.Dom exposing (Viewport) import Url.Builder @@ -61,6 +62,7 @@ type Msg = | FileUploadComplete RoomId String (Result Http.Error String) | SendImageResponse (Result Http.Error ()) | SendFileResponse (Result Http.Error ()) + | ReceiveMarkdown MarkdownResponse displayName : Model -> Username -> String displayName m s = Maybe.withDefault (senderName s) <| Maybe.andThen .displayName <| Dict.get s m.userData diff --git a/static/js/markdown.js b/static/js/markdown.js new file mode 100644 index 0000000..2455317 --- /dev/null +++ b/static/js/markdown.js @@ -0,0 +1,9 @@ +function setupNotificationPorts(app) { + app.ports.requestMarkdownPort.subscribe(function(data) { + app.ports.receiveMarkdownPort.send({ + "roomId" : data.roomId, + "text" : data.text, + "markdown" : marked(data.text) + }); + }) +}