2018-12-08 20:02:29 -08:00
|
|
|
import Browser exposing (application, UrlRequest(..))
|
2018-12-07 23:03:16 -08:00
|
|
|
import Browser.Navigation as Nav
|
2018-12-14 00:02:15 -08:00
|
|
|
import Browser.Dom exposing (Viewport, setViewportOf)
|
2018-12-08 13:49:30 -08:00
|
|
|
import Scylla.Sync exposing (..)
|
2019-02-25 19:54:54 -08:00
|
|
|
import Scylla.Room exposing (..)
|
|
|
|
import Scylla.Messages exposing (..)
|
2018-12-08 15:06:14 -08:00
|
|
|
import Scylla.Login exposing (..)
|
2018-12-17 02:40:39 -08:00
|
|
|
import Scylla.Api exposing (..)
|
2018-12-08 13:49:30 -08:00
|
|
|
import Scylla.Model exposing (..)
|
|
|
|
import Scylla.Http exposing (..)
|
2018-12-08 15:06:14 -08:00
|
|
|
import Scylla.Views exposing (viewFull)
|
2018-12-17 19:56:50 -08:00
|
|
|
import Scylla.Route exposing (Route(..), RoomId)
|
2018-12-13 12:45:30 -08:00
|
|
|
import Scylla.UserData exposing (..)
|
2018-12-13 13:42:23 -08:00
|
|
|
import Scylla.Notification exposing (..)
|
2018-12-17 02:34:46 -08:00
|
|
|
import Scylla.Storage exposing (..)
|
2018-12-20 22:01:09 -08:00
|
|
|
import Scylla.Markdown exposing (..)
|
2018-12-22 21:42:51 -08:00
|
|
|
import Scylla.AccountData exposing (..)
|
2018-12-07 23:03:16 -08:00
|
|
|
import Url exposing (Url)
|
2018-12-08 19:09:20 -08:00
|
|
|
import Url.Parser exposing (parse)
|
|
|
|
import Url.Builder
|
2018-12-17 02:34:46 -08:00
|
|
|
import Json.Encode
|
2018-12-17 16:32:39 -08:00
|
|
|
import Json.Decode
|
2018-12-17 19:56:50 -08:00
|
|
|
import Time exposing (every)
|
2018-12-08 13:49:30 -08:00
|
|
|
import Html exposing (div, text)
|
2018-12-20 19:22:51 -08:00
|
|
|
import File exposing (File)
|
|
|
|
import File.Select as Select
|
2018-12-08 15:06:14 -08:00
|
|
|
import Http
|
2018-12-09 23:38:43 -08:00
|
|
|
import Dict
|
2018-12-14 00:02:15 -08:00
|
|
|
import Task
|
2018-12-07 23:03:16 -08:00
|
|
|
|
2018-12-17 19:56:50 -08:00
|
|
|
syncTimeout = 10000
|
|
|
|
typingTimeout = 2000
|
|
|
|
|
2018-12-17 22:07:27 -08:00
|
|
|
init : () -> Url -> Nav.Key -> (Model, Cmd Msg)
|
|
|
|
init _ url key =
|
2018-12-07 23:03:16 -08:00
|
|
|
let
|
|
|
|
model =
|
|
|
|
{ key = key
|
2018-12-08 19:09:20 -08:00
|
|
|
, route = Maybe.withDefault Unknown <| parse Scylla.Route.route url
|
2018-12-17 22:07:27 -08:00
|
|
|
, token = Nothing
|
2018-12-08 15:06:14 -08:00
|
|
|
, loginUsername = ""
|
|
|
|
, loginPassword = ""
|
2018-12-08 13:49:30 -08:00
|
|
|
, apiUrl = "https://matrix.org"
|
2018-12-08 17:15:35 -08:00
|
|
|
, sync =
|
|
|
|
{ nextBatch = ""
|
|
|
|
, rooms = Nothing
|
|
|
|
, presence = Nothing
|
|
|
|
, accountData = Nothing
|
|
|
|
}
|
|
|
|
, errors = []
|
2018-12-09 23:38:43 -08:00
|
|
|
, roomText = Dict.empty
|
2019-02-25 20:04:15 -08:00
|
|
|
, sending = Dict.empty
|
2018-12-09 23:38:43 -08:00
|
|
|
, transactionId = 0
|
2018-12-13 02:38:25 -08:00
|
|
|
, userData = Dict.empty
|
2018-12-27 00:12:48 -08:00
|
|
|
, connected = True
|
2018-12-07 23:03:16 -08:00
|
|
|
}
|
2018-12-17 22:07:27 -08:00
|
|
|
cmd = getStoreValuePort "scylla.loginInfo"
|
2018-12-07 23:03:16 -08:00
|
|
|
in
|
|
|
|
(model, cmd)
|
|
|
|
|
|
|
|
view : Model -> Browser.Document Msg
|
|
|
|
view m =
|
2019-03-15 17:44:54 -07:00
|
|
|
let
|
|
|
|
notificationString = totalNotificationCountString m.sync
|
|
|
|
titleString = case notificationString of
|
|
|
|
Nothing -> "Scylla"
|
|
|
|
Just s -> s ++ " Scylla"
|
|
|
|
in
|
|
|
|
{ title = titleString
|
|
|
|
, body = viewFull m
|
|
|
|
}
|
2018-12-07 23:03:16 -08:00
|
|
|
|
|
|
|
update : Msg -> Model -> (Model, Cmd Msg)
|
2018-12-08 15:06:14 -08:00
|
|
|
update msg model = case msg of
|
|
|
|
ChangeApiUrl u -> ({ model | apiUrl = u }, Cmd.none)
|
|
|
|
ChangeLoginUsername u -> ({ model | loginUsername = u }, Cmd.none)
|
|
|
|
ChangeLoginPassword p -> ({ model | loginPassword = p }, Cmd.none)
|
|
|
|
AttemptLogin -> (model, Scylla.Http.login model.apiUrl model.loginUsername model.loginPassword) -- TODO
|
2018-12-08 20:02:29 -08:00
|
|
|
TryUrl urlRequest -> updateTryUrl model urlRequest
|
2018-12-13 14:06:15 -08:00
|
|
|
OpenRoom s -> (model, Nav.pushUrl model.key <| roomUrl s)
|
2018-12-15 20:56:17 -08:00
|
|
|
ChangeRoute r -> updateChangeRoute model r
|
2018-12-14 00:02:15 -08:00
|
|
|
ViewportAfterMessage v -> updateViewportAfterMessage model v
|
|
|
|
ViewportChangeComplete _ -> (model, Cmd.none)
|
2018-12-17 02:40:39 -08:00
|
|
|
ReceiveLoginResponse a r -> updateLoginResponse model a r
|
2018-12-13 01:46:57 -08:00
|
|
|
ReceiveFirstSyncResponse r -> updateSyncResponse model r False
|
|
|
|
ReceiveSyncResponse r -> updateSyncResponse model r True
|
2018-12-13 12:45:30 -08:00
|
|
|
ReceiveUserData s r -> updateUserData model s r
|
2018-12-17 19:56:50 -08:00
|
|
|
ChangeRoomText r t -> updateChangeRoomText model r t
|
2018-12-09 23:38:43 -08:00
|
|
|
SendRoomText r -> updateSendRoomText model r
|
2019-03-15 18:45:55 -07:00
|
|
|
SendRoomTextResponse t r -> updateSendRoomTextResponse model t r
|
2018-12-15 19:22:49 -08:00
|
|
|
ReceiveCompletedReadMarker r -> (model, Cmd.none)
|
2018-12-17 19:39:37 -08:00
|
|
|
ReceiveCompletedTypingIndicator r -> (model, Cmd.none)
|
2018-12-17 16:18:47 -08:00
|
|
|
ReceiveStoreData d -> updateStoreData model d
|
2018-12-17 19:56:50 -08:00
|
|
|
TypingTick _ -> updateTypingTick model
|
2018-12-19 21:52:07 -08:00
|
|
|
History r -> updateHistory model r
|
|
|
|
ReceiveHistoryResponse r hr -> updateHistoryResponse model r hr
|
2018-12-20 19:22:51 -08:00
|
|
|
SendImages rid -> (model, Select.files [ "image/png" ] <| ImagesSelected rid)
|
|
|
|
SendFiles rid -> (model, Select.files [ "application/*" ] <| FilesSelected rid)
|
|
|
|
ImagesSelected rid f fs -> updateUploadSelected model rid f fs (ImageUploadComplete rid)
|
|
|
|
FilesSelected rid f fs -> updateUploadSelected model rid f fs (FileUploadComplete rid)
|
2018-12-20 20:13:09 -08:00
|
|
|
ImageUploadComplete rid mime ur -> updateImageUploadComplete model rid mime ur
|
|
|
|
FileUploadComplete rid mime ur -> updateFileUploadComplete model rid mime ur
|
2018-12-20 19:22:51 -08:00
|
|
|
SendImageResponse _ -> (model, Cmd.none)
|
|
|
|
SendFileResponse _ -> (model, Cmd.none)
|
2018-12-20 22:01:09 -08:00
|
|
|
ReceiveMarkdown md -> updateMarkdown model md
|
2018-12-23 00:23:48 -08:00
|
|
|
DismissError i -> updateDismissError model i
|
2018-12-27 00:12:48 -08:00
|
|
|
AttemptReconnect -> ({ model | connected = True }, firstSync model.apiUrl (Maybe.withDefault "" model.token))
|
2018-12-23 00:23:48 -08:00
|
|
|
|
2019-02-25 20:16:06 -08:00
|
|
|
requestScrollCmd : Cmd Msg
|
|
|
|
requestScrollCmd = Task.attempt ViewportAfterMessage (Browser.Dom.getViewportOf "messages-wrapper")
|
|
|
|
|
2019-03-15 18:45:55 -07:00
|
|
|
updateSendRoomTextResponse : Model -> Int -> Result Http.Error String -> (Model, Cmd Msg)
|
|
|
|
updateSendRoomTextResponse m t r =
|
|
|
|
let
|
|
|
|
updateFunction newId msg = case msg of
|
|
|
|
Just (rid, { body, id }) -> Just (rid, { body = body, id = Just newId })
|
|
|
|
Nothing -> Nothing
|
|
|
|
in
|
|
|
|
case r of
|
|
|
|
Ok s -> ({ m | sending = Dict.update t (updateFunction s) m.sending }, Cmd.none)
|
|
|
|
Err e -> ({ m | sending = Dict.remove t m.sending }, Cmd.none)
|
|
|
|
|
2018-12-23 00:23:48 -08:00
|
|
|
updateDismissError : Model -> Int -> (Model, Cmd Msg)
|
|
|
|
updateDismissError m i = ({ m | errors = (List.take i m.errors) ++ (List.drop (i+1) m.errors)}, Cmd.none)
|
2018-12-20 22:01:09 -08:00
|
|
|
|
|
|
|
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
|
2019-02-25 19:54:54 -08:00
|
|
|
newModel =
|
|
|
|
{ m | transactionId = m.transactionId + 1
|
2019-03-15 18:01:07 -07:00
|
|
|
, sending = Dict.insert (m.transactionId + 1) (roomId, { body = TextMessage text, id = Nothing }) m.sending
|
2019-02-25 19:54:54 -08:00
|
|
|
}
|
2018-12-20 22:01:09 -08:00
|
|
|
in
|
2019-02-25 20:16:06 -08:00
|
|
|
(newModel, Cmd.batch [ storeValueCmd, sendMessageCmd, requestScrollCmd ])
|
2018-12-20 19:22:51 -08:00
|
|
|
|
2019-02-21 23:03:01 -08:00
|
|
|
updateFileUploadComplete : Model -> RoomId -> File -> (Result Http.Error String) -> (Model, Cmd Msg)
|
2018-12-20 20:13:09 -08:00
|
|
|
updateFileUploadComplete m rid mime ur =
|
2018-12-20 19:22:51 -08:00
|
|
|
let
|
|
|
|
command = case ur of
|
2019-01-24 21:14:02 -08:00
|
|
|
Ok u -> sendFileMessage m.apiUrl (Maybe.withDefault "" m.token) (m.transactionId + 1) rid mime u
|
2018-12-20 19:22:51 -08:00
|
|
|
_ -> Cmd.none
|
2018-12-23 00:39:20 -08:00
|
|
|
newErrors = case ur of
|
|
|
|
Err e -> [ "Error uploading file. Please check your internet connection and try again." ]
|
|
|
|
_ -> []
|
2018-12-20 19:22:51 -08:00
|
|
|
in
|
2018-12-23 00:39:20 -08:00
|
|
|
({ m | errors = newErrors ++ m.errors, transactionId = m.transactionId + 1}, command)
|
2018-12-20 19:22:51 -08:00
|
|
|
|
2019-02-21 23:03:01 -08:00
|
|
|
updateImageUploadComplete : Model -> RoomId -> File -> (Result Http.Error String) -> (Model, Cmd Msg)
|
2018-12-20 20:13:09 -08:00
|
|
|
updateImageUploadComplete m rid mime ur =
|
2018-12-20 19:22:51 -08:00
|
|
|
let
|
|
|
|
command = case ur of
|
2019-01-24 21:14:02 -08:00
|
|
|
Ok u -> sendImageMessage m.apiUrl (Maybe.withDefault "" m.token) (m.transactionId + 1) rid mime u
|
2018-12-20 19:22:51 -08:00
|
|
|
_ -> Cmd.none
|
2018-12-23 00:39:20 -08:00
|
|
|
newErrors = case ur of
|
|
|
|
Err e -> [ "Error uploading image. Please check your internet connection and try again." ]
|
|
|
|
_ -> []
|
2018-12-20 19:22:51 -08:00
|
|
|
in
|
|
|
|
({ m | transactionId = m.transactionId + 1}, command)
|
|
|
|
|
2019-02-21 23:03:01 -08:00
|
|
|
updateUploadSelected : Model -> RoomId -> File -> List File -> (File -> Result Http.Error String -> Msg) -> (Model, Cmd Msg)
|
2018-12-20 19:22:51 -08:00
|
|
|
updateUploadSelected m rid f fs msg =
|
|
|
|
let
|
|
|
|
uploadCmds = List.map (uploadMediaFile m.apiUrl (Maybe.withDefault "" m.token) msg) (f::fs)
|
|
|
|
in
|
|
|
|
(m, Cmd.batch uploadCmds)
|
2018-12-17 19:56:50 -08:00
|
|
|
|
2018-12-19 21:52:07 -08:00
|
|
|
updateHistoryResponse : Model -> RoomId -> Result Http.Error HistoryResponse -> (Model, Cmd Msg)
|
2018-12-20 16:39:10 -08:00
|
|
|
updateHistoryResponse m r hr =
|
|
|
|
let
|
|
|
|
newUsersCmd h = Cmd.batch
|
|
|
|
<| List.map (userData m.apiUrl (Maybe.withDefault "" m.token))
|
|
|
|
<| newUsers m
|
|
|
|
<| uniqueBy (\s -> s)
|
|
|
|
<| List.map .sender
|
|
|
|
<| h.chunk
|
|
|
|
in
|
|
|
|
case hr of
|
|
|
|
Ok h -> ({ m | sync = appendHistoryResponse m.sync r h }, newUsersCmd h)
|
2018-12-23 00:39:20 -08:00
|
|
|
Err _ -> ({ m | errors = "Unable to load older history from server"::m.errors }, Cmd.none)
|
2018-12-19 21:52:07 -08:00
|
|
|
|
|
|
|
updateHistory : Model -> RoomId -> (Model, Cmd Msg)
|
|
|
|
updateHistory m r =
|
|
|
|
let
|
|
|
|
prevBatch = Maybe.andThen .prevBatch
|
|
|
|
<| Maybe.andThen .timeline
|
|
|
|
<| Maybe.andThen (Dict.get r)
|
|
|
|
<| Maybe.andThen .join
|
|
|
|
<| m.sync.rooms
|
|
|
|
command = case prevBatch of
|
|
|
|
Just pv -> getHistory m.apiUrl (Maybe.withDefault "" m.token) r pv
|
|
|
|
Nothing -> Cmd.none
|
|
|
|
in
|
|
|
|
(m, command)
|
2018-12-17 19:56:50 -08:00
|
|
|
|
|
|
|
updateChangeRoomText : Model -> RoomId -> String -> (Model, Cmd Msg)
|
|
|
|
updateChangeRoomText m roomId text =
|
|
|
|
let
|
|
|
|
typingIndicator = case (text, Dict.get roomId m.roomText) of
|
|
|
|
("", _) -> Just False
|
|
|
|
(_, Just "") -> Just True
|
2018-12-17 20:30:11 -08:00
|
|
|
(_, Nothing) -> Just True
|
2018-12-17 19:56:50 -08:00
|
|
|
_ -> Nothing
|
|
|
|
command = case typingIndicator of
|
|
|
|
Just b -> sendTypingIndicator m.apiUrl (Maybe.withDefault "" m.token) roomId m.loginUsername b typingTimeout
|
|
|
|
_ -> Cmd.none
|
|
|
|
in
|
|
|
|
({ m | roomText = Dict.insert roomId text m.roomText}, command)
|
|
|
|
|
|
|
|
updateTypingTick : Model -> (Model, Cmd Msg)
|
|
|
|
updateTypingTick m =
|
|
|
|
let
|
|
|
|
command = case currentRoomId m of
|
|
|
|
Just rid -> sendTypingIndicator m.apiUrl (Maybe.withDefault "" m.token) rid m.loginUsername True typingTimeout
|
|
|
|
Nothing -> Cmd.none
|
|
|
|
in
|
|
|
|
(m, command)
|
|
|
|
|
2018-12-17 16:18:47 -08:00
|
|
|
|
2018-12-17 16:32:39 -08:00
|
|
|
updateStoreData : Model -> Json.Encode.Value -> (Model, Cmd Msg)
|
|
|
|
updateStoreData m d = case (Json.Decode.decodeValue storeDataDecoder d) of
|
|
|
|
Ok { key, value } -> case key of
|
|
|
|
"scylla.loginInfo" -> updateLoginInfo m value
|
|
|
|
_ -> (m, Cmd.none)
|
|
|
|
Err _ -> (m, Cmd.none)
|
2018-12-17 16:18:47 -08:00
|
|
|
|
2018-12-17 16:32:39 -08:00
|
|
|
updateLoginInfo : Model -> Json.Encode.Value -> (Model, Cmd Msg)
|
|
|
|
updateLoginInfo m s = case Json.Decode.decodeValue (Json.Decode.map decodeLoginInfo Json.Decode.string) s of
|
2018-12-17 20:30:11 -08:00
|
|
|
Ok (Just { token, apiUrl, username, transactionId }) ->
|
|
|
|
(
|
|
|
|
{ m | token = Just token
|
|
|
|
, apiUrl = apiUrl
|
|
|
|
, loginUsername = username
|
|
|
|
, transactionId = transactionId
|
|
|
|
}
|
|
|
|
, firstSync apiUrl token
|
|
|
|
)
|
2018-12-17 16:32:39 -08:00
|
|
|
_ -> (m, Nav.pushUrl m.key <| Url.Builder.absolute [ "login" ] [])
|
2018-12-09 23:38:43 -08:00
|
|
|
|
2018-12-15 20:56:17 -08:00
|
|
|
updateChangeRoute : Model -> Route -> (Model, Cmd Msg)
|
|
|
|
updateChangeRoute m r =
|
|
|
|
let
|
|
|
|
joinedRoom = case r of
|
|
|
|
Room rid -> Maybe.andThen (Dict.get rid) <| Maybe.andThen .join <| m.sync.rooms
|
|
|
|
_ -> Nothing
|
|
|
|
lastMessage = Maybe.andThen (findLastEvent (((==) "m.room.message") << .type_)) <| Maybe.andThen .events <| Maybe.andThen .timeline joinedRoom
|
|
|
|
readMarkerCmd = case (r, lastMessage) of
|
|
|
|
(Room rid, Just re) -> setReadMarkers m.apiUrl (Maybe.withDefault "" m.token) rid re.eventId <| Just re.eventId
|
|
|
|
_ -> Cmd.none
|
|
|
|
in
|
|
|
|
({ m | route = r }, readMarkerCmd)
|
|
|
|
|
2018-12-14 00:02:15 -08:00
|
|
|
updateViewportAfterMessage : Model -> Result Browser.Dom.Error Viewport -> (Model, Cmd Msg)
|
|
|
|
updateViewportAfterMessage m vr =
|
|
|
|
let
|
|
|
|
cmd vp = if vp.scene.height - (vp.viewport.y + vp.viewport.height ) < 100
|
2019-02-25 16:44:47 -08:00
|
|
|
then Task.attempt ViewportChangeComplete <| setViewportOf "messages-wrapper" vp.viewport.x vp.scene.height
|
2018-12-14 00:02:15 -08:00
|
|
|
else Cmd.none
|
|
|
|
in
|
|
|
|
(m, Result.withDefault Cmd.none <| Result.map cmd vr)
|
|
|
|
|
2018-12-13 12:45:30 -08:00
|
|
|
updateUserData : Model -> String -> Result Http.Error UserData -> (Model, Cmd Msg)
|
|
|
|
updateUserData m s r = case r of
|
|
|
|
Ok ud -> ({ m | userData = Dict.insert s ud m.userData }, Cmd.none)
|
2018-12-26 23:20:51 -08:00
|
|
|
Err e -> ({ m | errors = ("Failed to retrieve user data for user " ++ s)::m.errors }, Cmd.none)
|
2018-12-13 12:45:30 -08:00
|
|
|
|
2018-12-17 20:30:11 -08:00
|
|
|
updateSendRoomText : Model -> RoomId -> (Model, Cmd Msg)
|
2018-12-09 23:38:43 -08:00
|
|
|
updateSendRoomText m r =
|
|
|
|
let
|
|
|
|
token = Maybe.withDefault "" m.token
|
|
|
|
message = Maybe.andThen (\s -> if s == "" then Nothing else Just s)
|
|
|
|
<| Dict.get r m.roomText
|
2018-12-17 20:30:11 -08:00
|
|
|
combinedCmd = case message of
|
|
|
|
Nothing -> Cmd.none
|
|
|
|
Just s -> Cmd.batch
|
2018-12-20 22:01:09 -08:00
|
|
|
[ requestMarkdownPort { roomId = r, text = s }
|
2018-12-17 20:30:11 -08:00
|
|
|
, sendTypingIndicator m.apiUrl token r m.loginUsername False typingTimeout
|
|
|
|
]
|
2018-12-09 23:38:43 -08:00
|
|
|
in
|
2018-12-20 22:01:09 -08:00
|
|
|
({ m | roomText = Dict.insert r "" m.roomText }, combinedCmd)
|
2018-12-08 20:02:29 -08:00
|
|
|
|
|
|
|
updateTryUrl : Model -> Browser.UrlRequest -> (Model, Cmd Msg)
|
|
|
|
updateTryUrl m ur = case ur of
|
|
|
|
Internal u -> (m, Nav.pushUrl m.key (Url.toString u))
|
|
|
|
_ -> (m, Cmd.none)
|
2018-12-08 15:06:14 -08:00
|
|
|
|
2018-12-17 02:40:39 -08:00
|
|
|
updateLoginResponse : Model -> ApiUrl -> Result Http.Error LoginResponse -> (Model, Cmd Msg)
|
|
|
|
updateLoginResponse model a r = case r of
|
|
|
|
Ok lr -> ( { model | token = Just lr.accessToken, loginUsername = lr.userId, apiUrl = a }, Cmd.batch
|
2018-12-08 19:09:20 -08:00
|
|
|
[ firstSync model.apiUrl lr.accessToken
|
|
|
|
, Nav.pushUrl model.key <| Url.Builder.absolute [] []
|
2018-12-17 20:30:11 -08:00
|
|
|
, setStoreValuePort ("scylla.loginInfo", Json.Encode.string
|
|
|
|
<| encodeLoginInfo
|
|
|
|
<| LoginInfo lr.accessToken model.apiUrl lr.userId model.transactionId)
|
2018-12-08 19:09:20 -08:00
|
|
|
] )
|
2018-12-23 00:39:20 -08:00
|
|
|
Err e -> ({ model | errors = "Failed to log in. Are your username and password correct?"::model.errors }, Cmd.none)
|
2018-12-08 15:06:14 -08:00
|
|
|
|
2018-12-13 01:46:57 -08:00
|
|
|
updateSyncResponse : Model -> Result Http.Error SyncResponse -> Bool -> (Model, Cmd Msg)
|
|
|
|
updateSyncResponse model r notify =
|
2018-12-09 00:35:07 -08:00
|
|
|
let
|
2018-12-09 13:02:54 -08:00
|
|
|
token = Maybe.withDefault "" model.token
|
|
|
|
nextBatch = Result.withDefault model.sync.nextBatch
|
|
|
|
<| Result.map .nextBatch r
|
2018-12-19 21:52:07 -08:00
|
|
|
syncCmd = sync model.apiUrl token nextBatch
|
2018-12-14 00:04:41 -08:00
|
|
|
newUserCmd sr = Cmd.batch
|
2018-12-13 13:42:23 -08:00
|
|
|
<| List.map (userData model.apiUrl
|
|
|
|
<| Maybe.withDefault "" model.token)
|
2018-12-20 16:39:10 -08:00
|
|
|
<| newUsers model
|
|
|
|
<| allUsers sr
|
2018-12-13 16:01:54 -08:00
|
|
|
notification sr = findFirstBy
|
|
|
|
(\(s, e) -> e.originServerTs)
|
|
|
|
(\(s, e) -> e.sender /= model.loginUsername)
|
2018-12-23 21:17:03 -08:00
|
|
|
<| joinedRoomNotificationEvents sr
|
2018-12-14 00:04:41 -08:00
|
|
|
notificationCmd sr = if notify
|
|
|
|
then Maybe.withDefault Cmd.none
|
|
|
|
<| Maybe.map (\(s, e) -> sendNotificationPort
|
|
|
|
{ name = displayName model e.sender
|
|
|
|
, text = notificationText e
|
|
|
|
, room = s
|
|
|
|
}) <| notification sr
|
|
|
|
else Cmd.none
|
2018-12-15 19:22:49 -08:00
|
|
|
room = currentRoomId model
|
|
|
|
roomMessages sr = case room of
|
2018-12-14 00:02:15 -08:00
|
|
|
Just rid -> List.filter (((==) "m.room.message") << .type_)
|
|
|
|
<| Maybe.withDefault []
|
|
|
|
<| Maybe.andThen .events
|
|
|
|
<| Maybe.andThen .timeline
|
|
|
|
<| Maybe.andThen (Dict.get rid)
|
|
|
|
<| Maybe.andThen .join
|
|
|
|
<| sr.rooms
|
|
|
|
Nothing -> []
|
2018-12-14 00:04:41 -08:00
|
|
|
setScrollCmd sr = if List.isEmpty
|
|
|
|
<| roomMessages sr
|
|
|
|
then Cmd.none
|
2019-02-25 20:16:06 -08:00
|
|
|
else requestScrollCmd
|
2018-12-15 19:22:49 -08:00
|
|
|
setReadReceiptCmd sr = case (room, List.head <| List.reverse <| roomMessages sr) of
|
|
|
|
(Just rid, Just re) -> setReadMarkers model.apiUrl token rid re.eventId <| Just re.eventId
|
|
|
|
_ -> Cmd.none
|
2019-03-15 18:45:55 -07:00
|
|
|
receivedEvents sr = List.map Just <| allTimelineEventIds sr
|
|
|
|
sending sr = Dict.filter (\_ (rid, { body, id }) -> not <| List.member id <| receivedEvents sr) model.sending
|
2018-12-09 00:35:07 -08:00
|
|
|
in
|
|
|
|
case r of
|
2019-03-15 18:45:55 -07:00
|
|
|
Ok sr -> ({ model | sync = mergeSyncResponse model.sync sr, sending = sending (mergeSyncResponse model.sync sr) }, Cmd.batch
|
2018-12-13 13:42:23 -08:00
|
|
|
[ syncCmd
|
2018-12-14 00:04:41 -08:00
|
|
|
, newUserCmd sr
|
|
|
|
, notificationCmd sr
|
|
|
|
, setScrollCmd sr
|
2018-12-15 19:22:49 -08:00
|
|
|
, setReadReceiptCmd sr
|
2018-12-13 13:42:23 -08:00
|
|
|
])
|
2018-12-27 00:12:48 -08:00
|
|
|
_ -> ({ model | connected = False }, Cmd.none)
|
2018-12-07 23:03:16 -08:00
|
|
|
|
|
|
|
subscriptions : Model -> Sub Msg
|
2018-12-17 19:56:50 -08:00
|
|
|
subscriptions m =
|
|
|
|
let
|
2018-12-17 20:30:11 -08:00
|
|
|
currentText = Maybe.withDefault ""
|
|
|
|
<| Maybe.andThen (\rid -> Dict.get rid m.roomText)
|
|
|
|
<| currentRoomId m
|
|
|
|
typingTimer = case currentText of
|
2018-12-17 19:56:50 -08:00
|
|
|
"" -> Sub.none
|
|
|
|
_ -> every typingTimeout TypingTick
|
|
|
|
in
|
|
|
|
Sub.batch
|
|
|
|
[ onNotificationClickPort OpenRoom
|
|
|
|
, receiveStoreValuePort ReceiveStoreData
|
|
|
|
, typingTimer
|
2018-12-20 22:01:09 -08:00
|
|
|
, receiveMarkdownPort ReceiveMarkdown
|
2018-12-17 19:56:50 -08:00
|
|
|
]
|
2018-12-07 23:03:16 -08:00
|
|
|
|
|
|
|
onUrlRequest : Browser.UrlRequest -> Msg
|
|
|
|
onUrlRequest = TryUrl
|
|
|
|
|
|
|
|
onUrlChange : Url -> Msg
|
2018-12-08 19:09:20 -08:00
|
|
|
onUrlChange = ChangeRoute << Maybe.withDefault Unknown << parse Scylla.Route.route
|
2018-12-07 23:03:16 -08:00
|
|
|
|
|
|
|
main = application
|
|
|
|
{ init = init
|
|
|
|
, view = view
|
|
|
|
, update = update
|
|
|
|
, subscriptions = subscriptions
|
|
|
|
, onUrlRequest = onUrlRequest
|
|
|
|
, onUrlChange = onUrlChange
|
|
|
|
}
|