blob: 8aaaa52c7d14112f8b1e0112b92336ad5fb8df6a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE OverloadedStrings #-}
module Endpoints.ProfileEndpoint (ProfileAPI, profileServer) where
import Servant
import Data.Aeson
import Data.Text (Text, pack)
import qualified Data.Text as T
import Database.Persist
import Database (runDb)
import Control.Monad.IO.Class (liftIO)
import Data.User (User(..), Unique(UniqueName))
import Model.Profile
import Model.MatrixErrorResponse
----------------------------------------------------------------------------------------------------
type ProfileAPI = GetProfile
profileServer :: Server ProfileAPI
profileServer = handleProfileGet
--- GET /_matrix/client/v3/profile/{userId} --------------------------------------------------------
type GetProfile = "_matrix" :> "client" :> "v3" :> "profile" :> Capture "userId" Text
:> Get '[JSON] ProfileResponse
handleProfileGet :: Text -> Handler ProfileResponse
handleProfileGet user_id = do
let username = T.takeWhile (/= ':') $ T.drop 1 user_id
maybe_user <- liftIO $ runDb $ getBy $ UniqueName username
case maybe_user of
Just (Entity _ db_user) ->
return $ ProfileResponse
{ display_name = (userDisplayName db_user) <> (Just $ userIdent $ db_user)
, avatar_url = userAvatarUrl db_user
, tz = Nothing
}
Nothing ->
throwError err404 { errBody = encode user_not_found_error }
where
user_not_found_error = (MatrixErrorResponse (pack "M_NOT_FOUND") (pack "Profile not found"))
|