Add markdown sending.

This commit is contained in:
Danila Fedorin 2018-12-20 22:01:09 -08:00
parent c483e6ac6c
commit eb9e82483b
5 changed files with 49 additions and 5 deletions

View File

@ -11,6 +11,7 @@ import Scylla.Route exposing (Route(..), RoomId)
import Scylla.UserData exposing (..) import Scylla.UserData exposing (..)
import Scylla.Notification exposing (..) import Scylla.Notification exposing (..)
import Scylla.Storage exposing (..) import Scylla.Storage exposing (..)
import Scylla.Markdown exposing (..)
import Url exposing (Url) import Url exposing (Url)
import Url.Parser exposing (parse) import Url.Parser exposing (parse)
import Url.Builder import Url.Builder
@ -90,6 +91,17 @@ update msg model = case msg of
FileUploadComplete rid mime ur -> updateFileUploadComplete model rid mime ur FileUploadComplete rid mime ur -> updateFileUploadComplete model rid mime ur
SendImageResponse _ -> (model, Cmd.none) SendImageResponse _ -> (model, Cmd.none)
SendFileResponse _ -> (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 : Model -> RoomId -> String -> (Result Http.Error String) -> (Model, Cmd Msg)
updateFileUploadComplete m rid mime ur = updateFileUploadComplete m rid mime ur =
@ -224,14 +236,11 @@ updateSendRoomText m r =
combinedCmd = case message of combinedCmd = case message of
Nothing -> Cmd.none Nothing -> Cmd.none
Just s -> Cmd.batch 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 , 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 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 : Model -> Browser.UrlRequest -> (Model, Cmd Msg)
updateTryUrl m ur = case ur of updateTryUrl m ur = case ur of
@ -315,6 +324,7 @@ subscriptions m =
[ onNotificationClickPort OpenRoom [ onNotificationClickPort OpenRoom
, receiveStoreValuePort ReceiveStoreData , receiveStoreValuePort ReceiveStoreData
, typingTimer , typingTimer
, receiveMarkdownPort ReceiveMarkdown
] ]
onUrlRequest : Browser.UrlRequest -> Msg onUrlRequest : Browser.UrlRequest -> Msg

View File

@ -76,6 +76,14 @@ sendMessage apiUrl token transactionId room msg contents = request
, tracker = Nothing , 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 -> ApiToken -> Int -> RoomId -> String -> Cmd Msg
sendTextMessage apiUrl token transactionId room message = sendMessage apiUrl token transactionId room SendRoomTextResponse sendTextMessage apiUrl token transactionId room message = sendMessage apiUrl token transactionId room SendRoomTextResponse
[ ("msgtype", string "m.text") [ ("msgtype", string "m.text")

15
src/Scylla/Markdown.elm Normal file
View File

@ -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

View File

@ -5,6 +5,7 @@ import Scylla.Login exposing (LoginResponse, Username, Password)
import Scylla.UserData exposing (UserData) import Scylla.UserData exposing (UserData)
import Scylla.Route exposing (Route(..), RoomId) import Scylla.Route exposing (Route(..), RoomId)
import Scylla.Storage exposing (..) import Scylla.Storage exposing (..)
import Scylla.Markdown exposing (..)
import Browser.Navigation as Nav import Browser.Navigation as Nav
import Browser.Dom exposing (Viewport) import Browser.Dom exposing (Viewport)
import Url.Builder import Url.Builder
@ -61,6 +62,7 @@ type Msg =
| FileUploadComplete RoomId String (Result Http.Error String) | FileUploadComplete RoomId String (Result Http.Error String)
| SendImageResponse (Result Http.Error ()) | SendImageResponse (Result Http.Error ())
| SendFileResponse (Result Http.Error ()) | SendFileResponse (Result Http.Error ())
| ReceiveMarkdown MarkdownResponse
displayName : Model -> Username -> String displayName : Model -> Username -> String
displayName m s = Maybe.withDefault (senderName s) <| Maybe.andThen .displayName <| Dict.get s m.userData displayName m s = Maybe.withDefault (senderName s) <| Maybe.andThen .displayName <| Dict.get s m.userData

9
static/js/markdown.js Normal file
View File

@ -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)
});
})
}