Retrieve the associated sites for all profiles in one query when rendering the h-card
This commit is contained in:
parent
1fd650c8da
commit
4023b17e85
1 changed files with 13 additions and 2 deletions
|
@ -4,13 +4,24 @@
|
||||||
module Widget.Hcard (hcard) where
|
module Widget.Hcard (hcard) where
|
||||||
|
|
||||||
import Import
|
import Import
|
||||||
|
import Data.Maybe (fromJust)
|
||||||
|
import qualified Data.Map as M
|
||||||
import qualified Network.Libravatar as L
|
import qualified Network.Libravatar as L
|
||||||
|
|
||||||
|
entityToTuple :: Entity t -> (Key t, t)
|
||||||
|
entityToTuple (Entity key value) = (key, value)
|
||||||
|
|
||||||
|
arrangeProfiles :: M.Map (Key Site) Site -> [Profile] -> [(Site,Profile)]
|
||||||
|
arrangeProfiles sites profiles = sortBy icon $ zip profileSites profiles
|
||||||
|
where findSite = fromJust . flip M.lookup sites . profileSiteId
|
||||||
|
profileSites = findSite <$> profiles
|
||||||
|
icon = comparing $ siteIcon . fst
|
||||||
|
|
||||||
hcard :: Entity User -> Widget
|
hcard :: Entity User -> Widget
|
||||||
hcard (Entity userId user) = do
|
hcard (Entity userId user) = do
|
||||||
maybeAvatar <- liftIO $ L.avatarUrl (L.Email $ userEmail user) def { L.optSecure = True, L.optSize = L.Size 512 }
|
maybeAvatar <- liftIO $ L.avatarUrl (L.Email $ userEmail user) def { L.optSecure = True, L.optSize = L.Size 512 }
|
||||||
userProfiles <- handlerToWidget . runDB $ do
|
userProfiles <- handlerToWidget . runDB $ do
|
||||||
profiles <- map entityVal <$> selectList [ProfileUserId ==. userId] []
|
profiles <- map entityVal <$> selectList [ProfileUserId ==. userId] []
|
||||||
sites <- belongsToJust profileSiteId `mapM` profiles
|
sites <- M.fromList . map entityToTuple <$> selectList [SiteId <-. (profileSiteId <$> profiles)] []
|
||||||
return . sortBy (comparing $ siteIcon . fst) $ zip sites profiles
|
return $ arrangeProfiles sites profiles
|
||||||
$(widgetFile "hcard")
|
$(widgetFile "hcard")
|
||||||
|
|
Loading…
Reference in a new issue