2018-12-08 13:49:30 -08:00
|
|
|
module Scylla.Http exposing (..)
|
|
|
|
import Scylla.Model exposing (..)
|
|
|
|
import Scylla.Api exposing (..)
|
2018-12-15 19:22:49 -08:00
|
|
|
import Scylla.Route exposing (RoomId)
|
2018-12-08 13:49:30 -08:00
|
|
|
import Scylla.Sync exposing (syncResponseDecoder)
|
|
|
|
import Scylla.Login exposing (loginResponseDecoder, Username, Password)
|
2018-12-13 02:15:14 -08:00
|
|
|
import Scylla.UserData exposing (userDataDecoder, UserData)
|
2018-12-17 19:39:37 -08:00
|
|
|
import Json.Encode exposing (object, string, int, bool)
|
2018-12-09 23:38:43 -08:00
|
|
|
import Http exposing (request, emptyBody, jsonBody, expectJson, expectWhatever)
|
2018-12-08 13:49:30 -08:00
|
|
|
|
2018-12-13 19:45:55 -08:00
|
|
|
fullClientUrl : ApiUrl -> ApiUrl
|
|
|
|
fullClientUrl s = s ++ "/_matrix/client/r0"
|
|
|
|
|
|
|
|
fullMediaUrl : ApiUrl -> ApiUrl
|
|
|
|
fullMediaUrl s = s ++ "/_matrix/media/r0"
|
2018-12-08 15:06:14 -08:00
|
|
|
|
2018-12-08 13:49:30 -08:00
|
|
|
-- Http Requests
|
|
|
|
firstSync : ApiUrl -> ApiToken -> Cmd Msg
|
|
|
|
firstSync apiUrl token = request
|
|
|
|
{ method = "GET"
|
|
|
|
, headers = authenticatedHeaders token
|
2018-12-13 19:45:55 -08:00
|
|
|
, url = (fullClientUrl apiUrl) ++ "/sync"
|
2018-12-09 00:35:07 -08:00
|
|
|
, body = emptyBody
|
2018-12-13 01:46:57 -08:00
|
|
|
, expect = expectJson ReceiveFirstSyncResponse syncResponseDecoder
|
2018-12-09 00:35:07 -08:00
|
|
|
, timeout = Nothing
|
|
|
|
, tracker = Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
sync : String -> ApiUrl -> ApiToken -> Cmd Msg
|
|
|
|
sync nextBatch apiUrl token = request
|
|
|
|
{ method = "GET"
|
|
|
|
, headers = authenticatedHeaders token
|
2018-12-13 19:45:55 -08:00
|
|
|
, url = (fullClientUrl apiUrl) ++ "/sync" ++ "?since=" ++ (nextBatch) ++ "&timeout=10000"
|
2018-12-09 00:35:07 -08:00
|
|
|
, body = emptyBody
|
2018-12-08 13:49:30 -08:00
|
|
|
, expect = expectJson ReceiveSyncResponse syncResponseDecoder
|
|
|
|
, timeout = Nothing
|
|
|
|
, tracker = Nothing
|
|
|
|
}
|
|
|
|
|
2018-12-09 23:38:43 -08:00
|
|
|
sendTextMessage : ApiUrl -> ApiToken -> Int -> String -> String -> Cmd Msg
|
|
|
|
sendTextMessage apiUrl token transactionId room message = request
|
|
|
|
{ method = "PUT"
|
|
|
|
, headers = authenticatedHeaders token
|
2018-12-13 19:45:55 -08:00
|
|
|
, url = (fullClientUrl apiUrl)
|
2018-12-09 23:38:43 -08:00
|
|
|
++ "/rooms/" ++ room
|
|
|
|
++ "/send/" ++ "m.room.message"
|
|
|
|
++ "/" ++ (String.fromInt transactionId)
|
|
|
|
, body = jsonBody <| object
|
|
|
|
[ ("msgtype", string "m.text")
|
|
|
|
, ("body", string message)
|
|
|
|
]
|
|
|
|
, expect = expectWhatever SendRoomTextResponse
|
|
|
|
, timeout = Nothing
|
|
|
|
, tracker = Nothing
|
|
|
|
}
|
|
|
|
|
2018-12-08 13:49:30 -08:00
|
|
|
login : ApiUrl -> Username -> Password -> Cmd Msg
|
|
|
|
login apiUrl username password = request
|
|
|
|
{ method = "POST"
|
|
|
|
, headers = basicHeaders
|
2018-12-13 19:45:55 -08:00
|
|
|
, url = (fullClientUrl apiUrl) ++ "/login"
|
2018-12-08 13:49:30 -08:00
|
|
|
, body = jsonBody <| object
|
|
|
|
[ ("type", string "m.login.password")
|
|
|
|
, ("identifier", object
|
|
|
|
[ ("type", string "m.id.user")
|
|
|
|
, ("user", string username)
|
|
|
|
] )
|
|
|
|
, ("password", string password)
|
|
|
|
]
|
2018-12-17 02:40:39 -08:00
|
|
|
, expect = expectJson (ReceiveLoginResponse apiUrl) loginResponseDecoder
|
2018-12-08 13:49:30 -08:00
|
|
|
, timeout = Nothing
|
|
|
|
, tracker = Nothing
|
|
|
|
}
|
2018-12-13 02:15:14 -08:00
|
|
|
|
|
|
|
userData : ApiUrl -> ApiToken -> Username -> Cmd Msg
|
|
|
|
userData apiUrl token username = request
|
|
|
|
{ method = "GET"
|
|
|
|
, headers = authenticatedHeaders token
|
2018-12-13 19:45:55 -08:00
|
|
|
, url = (fullClientUrl apiUrl) ++ "/profile/" ++ username
|
2018-12-13 02:15:14 -08:00
|
|
|
, body = emptyBody
|
|
|
|
, expect = expectJson (ReceiveUserData username) userDataDecoder
|
|
|
|
, timeout = Nothing
|
|
|
|
, tracker = Nothing
|
|
|
|
}
|
2018-12-15 19:22:49 -08:00
|
|
|
|
|
|
|
setReadMarkers : ApiUrl -> ApiToken -> String -> RoomId -> Maybe String -> Cmd Msg
|
|
|
|
setReadMarkers apiUrl token roomId fullyRead readReceipt =
|
|
|
|
let
|
|
|
|
readReciptFields = case readReceipt of
|
|
|
|
Just s -> [ ("m.read", string s) ]
|
|
|
|
_ -> []
|
|
|
|
in
|
|
|
|
request
|
|
|
|
{ method = "POST"
|
|
|
|
, headers = authenticatedHeaders token
|
|
|
|
, url = (fullClientUrl apiUrl) ++ "/rooms/" ++ roomId ++ "/read_markers"
|
|
|
|
, body = jsonBody <| object <| [ ("m.fully_read", string fullyRead) ] ++ readReciptFields
|
|
|
|
, expect = expectWhatever ReceiveCompletedReadMarker
|
|
|
|
, timeout = Nothing
|
|
|
|
, tracker = Nothing
|
|
|
|
}
|
2018-12-17 19:39:37 -08:00
|
|
|
|
|
|
|
sendTypingIndicator : ApiUrl -> ApiToken -> RoomId -> Username -> Bool -> Int -> Cmd Msg
|
|
|
|
sendTypingIndicator apiUrl token room user isTyping timeout = request
|
|
|
|
{ method = "PUT"
|
|
|
|
, headers = authenticatedHeaders token
|
|
|
|
, url = (fullClientUrl apiUrl) ++ "/rooms/" ++ room ++ "/typing/" ++ user
|
|
|
|
, body = jsonBody <| object [ ("timeout", int timeout), ("typing", bool isTyping) ]
|
|
|
|
, expect = expectWhatever ReceiveCompletedTypingIndicator
|
|
|
|
, timeout = Nothing
|
|
|
|
, tracker = Nothing
|
|
|
|
}
|