diff options
| author | adambrangenberg <adabran06@gmail.com> | 2025-12-24 03:40:10 +0100 |
|---|---|---|
| committer | adambrangenberg <adabran06@gmail.com> | 2025-12-24 03:40:10 +0100 |
| commit | a0886694f73fc382d78da79ab8bfb27475757bab (patch) | |
| tree | 652ba9b603a1acaf4dfca188f7bb2c29c6bccfd0 /src/Endpoints/ProfileEndpoint.hs | |
| parent | 2b48a574e8b9fed03a5c1969af4bb1e338f1be26 (diff) | |
Implemented basic auth, refactor
Diffstat (limited to 'src/Endpoints/ProfileEndpoint.hs')
| -rw-r--r-- | src/Endpoints/ProfileEndpoint.hs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/Endpoints/ProfileEndpoint.hs b/src/Endpoints/ProfileEndpoint.hs new file mode 100644 index 0000000..8aaaa52 --- /dev/null +++ b/src/Endpoints/ProfileEndpoint.hs @@ -0,0 +1,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"))
\ No newline at end of file |