From a0886694f73fc382d78da79ab8bfb27475757bab Mon Sep 17 00:00:00 2001 From: adambrangenberg Date: Wed, 24 Dec 2025 03:40:10 +0100 Subject: Implemented basic auth, refactor --- src/Endpoints/ProfileEndpoint.hs | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/Endpoints/ProfileEndpoint.hs (limited to 'src/Endpoints/ProfileEndpoint.hs') 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 -- cgit v1.2.3