From 50431a97dd478a3ee69295afda96765040ddbf6f Mon Sep 17 00:00:00 2001 From: me Date: Tue, 17 Dec 2024 10:47:00 +0200 Subject: [PATCH] fix likes queries --- app/DB.hs | 168 ++++++++++++++++++++----------------- app/Html.hs | 8 +- app/Routes/Inbox/Accept.hs | 4 +- src/Fedi/Types/Helpers.hs | 3 + 4 files changed, 104 insertions(+), 79 deletions(-) diff --git a/app/DB.hs b/app/DB.hs index 2c5f8f2..38b3a48 100644 --- a/app/DB.hs +++ b/app/DB.hs @@ -29,6 +29,7 @@ data DB , getNote :: DB.Int64 -> IO (Maybe Note) , insertNote :: NoteEntry -> IO (DB.Int64, Note) , insertLike :: LikeEntry -> IO DB.Int64 + -- , deleteLike :: LikeEntry -> IO (Maybe DB.Int64) , insertFollower :: forall a . (Typeable a) => FollowerEntry -> (DB.Int64 -> IO a) -> IO a @@ -184,13 +185,14 @@ migrateDown = \case -- * Database actions getNotesFromDb :: DB.SQLite [Note] -getNotesFromDb = - map (snd . decodeNoteRow) <$> uncurry DB.runWith getNotesSQL +getNotesFromDb = do + result <- uncurry DB.runWith getNotesSQL + pure $ map (snd . decodeNoteRow) result getNoteFromDb :: DB.Int64 -> DB.SQLite (Maybe Note) getNoteFromDb noteid = do - n <- map (snd . decodeNoteRow) <$> uncurry DB.runWith (getNoteSQL noteid) - pure (listToMaybe n) + result <- uncurry DB.runWith (getNoteSQL noteid) + pure $ listToMaybe $ map (snd . decodeNoteRow) result insertNoteToDb :: Url -> NoteEntry -> DB.SQLite (DB.Int64, Note) insertNoteToDb actor note = do @@ -222,48 +224,55 @@ getNotesSQL :: (DB.SQL, [DB.SQLData]) getNotesSQL = ( [r| SELECT - id as nid, - actor || '/notes/' || id, + note_id, + note_url_id, published, - actor, - content, - name, - inReplyTo, - url, - json_group_array(like) FILTER (WHERE like IS NOT NULL) as likes + note_actor, + note_content, + note_name, + note_inReplyTo, + note_url, + + json_group_array( + json_object( + 'likeId', + like_id, + 'likeUrl', + like_url, + 'likeActorUrl', + like_actor_url, + 'likeNoteUrl', + like_note_url + ) + ) FILTER (WHERE like_id IS NOT NULL) as likes + FROM ( SELECT - note.*, - CASE - WHEN like.id IS NOT NULL - THEN json_object( - 'likeId', - like.id, - 'likeUrl', - like.like_url, - 'likeActorUrl', - like.actor_url, - 'likeNoteUrl', - like.note_url - ) - ELSE NULL - END AS like + note.id as note_id, + note.url_id as note_url_id, + note.published as published, + note.actor as note_actor, + note.content as note_content, + note.name as note_name, + note.inReplyTo as note_inReplyTo, + note.url as note_url, + + like.id as like_id, + like.like_url as like_url, + like.actor_url as like_actor_url, + like.note_url as like_note_url + FROM - ( SELECT * FROM note + ( SELECT + *, + actor || '/notes/' || id as url_id + FROM note WHERE inReplyTo IS NULL ) as note LEFT JOIN like - ON note.url = like.note_url + ON note.url_id = like.note_url ) - GROUP BY - id, - actor, - published, - actor, - content, - name, - inReplyTo, - url + GROUP BY note_id ORDER BY published DESC |] , [] @@ -273,45 +282,55 @@ getNoteSQL :: DB.Int64 -> (DB.SQL, [DB.SQLData]) getNoteSQL noteid = ( [r| SELECT - id as nid, - actor || '/notes/' || id, + note_id, + note_url_id, published, - actor, - content, - name, - inReplyTo, - url, - json_group_array(like) FILTER (WHERE like IS NOT NULL) as likes + note_actor, + note_content, + note_name, + note_inReplyTo, + note_url, + + json_group_array( + json_object( + 'likeId', + like_id, + 'likeUrl', + like_url, + 'likeActorUrl', + like_actor_url, + 'likeNoteUrl', + like_note_url + ) + ) FILTER (WHERE like_id IS NOT NULL) as likes FROM ( SELECT - note.*, - CASE - WHEN like.id IS NOT NULL - THEN json_object( - 'likeId', - like.id, - 'likeUrl', - like.like_url, - 'likeActorUrl', - like.actor_url, - 'likeNoteUrl', - like.note_url - ) - ELSE NULL - END AS like - FROM (SELECT * FROM note WHERE id = ?) as note + note.id as note_id, + note.url_id as note_url_id, + note.published as published, + note.actor as note_actor, + note.content as note_content, + note.name as note_name, + note.inReplyTo as note_inReplyTo, + note.url as note_url, + + like.id as like_id, + like.like_url as like_url, + like.actor_url as like_actor_url, + like.note_url as like_note_url + + FROM + ( SELECT + *, + actor || '/notes/' || id as url_id + FROM note WHERE id = ? + ) as note LEFT JOIN like - ON note.url = like.note_url + ON note.url_id = like.note_url ) GROUP BY - id, - actor, - published, - actor, - content, - name, - inReplyTo, - url + note_id + |] , [DB.SQLInteger noteid] ) @@ -344,13 +363,10 @@ insertNoteSQL actor note = insertLikeSQL :: LikeEntry -> (DB.SQL, [DB.SQLData]) insertLikeSQL like = ( [r| - INSERT INTO outer_like(like_url, actor_url, note_url) + INSERT INTO like(like_url, actor_url, note_url) VALUES (?, ?, ?) RETURNING - id as id, - like_url, - actor_url, - note_url + id as id |] , [ DB.SQLText (T.pack like.likeUrl) , DB.SQLText (T.pack like.likeActorUrl.unwrap) diff --git a/app/Html.hs b/app/Html.hs index 50906c8..4136e8c 100644 --- a/app/Html.hs +++ b/app/Html.hs @@ -72,13 +72,19 @@ noteHtml note = do H.p_ $ H.a_ [H.href_ (T.pack url)] $ fromString url Nothing -> pure () - Fedi.for_ note.published \published -> + Fedi.for_ note.published \published -> do H.a_ [ H.href_ noteid , H.class_ "note-time" , H.title_ "See note page" ] (H.span_ [H.class_ $ "note-date-published"] $ H.toHtml (show published)) + let numberOfLikes = length $ Fedi.noteLikes note + Fedi.when (numberOfLikes > 0) do + H.div_ do + H.toHtml (show numberOfLikes) + " " + "⭐" H.div_ [H.class_ $ "note-content " <> checkDirection (maybe "" id note.content)] $ do H.toHtmlRaw (maybe "" id note.content) diff --git a/app/Routes/Inbox/Accept.hs b/app/Routes/Inbox/Accept.hs index 58568d3..f81c808 100644 --- a/app/Routes/Inbox/Accept.hs +++ b/app/Routes/Inbox/Accept.hs @@ -14,8 +14,8 @@ acceptRequest -> IO () acceptRequest details actor activity operation = do _ <- liftIO $ Async.async do - Log.logDebug "Waiting 10 seconds before accepting follow..." - threadDelay 10000000 -- 10 seconds + Log.logDebug "Waiting 1 second before accepting follow..." + threadDelay 1000000 -- 1 second let callback = ( \(opid :: DB.Int64) -> do diff --git a/src/Fedi/Types/Helpers.hs b/src/Fedi/Types/Helpers.hs index aaabf4a..d0c3476 100644 --- a/src/Fedi/Types/Helpers.hs +++ b/src/Fedi/Types/Helpers.hs @@ -194,3 +194,6 @@ typeActivityLike actor object = , target = Nothing , origin = Nothing } + +noteLikes :: Note -> [Like] +noteLikes note = note.otype.likes.otype.ctype.items