fix likes queries

This commit is contained in:
me 2024-12-17 10:47:00 +02:00
parent b3d784d490
commit 50431a97dd
4 changed files with 104 additions and 79 deletions

168
app/DB.hs
View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -194,3 +194,6 @@ typeActivityLike actor object =
, target = Nothing
, origin = Nothing
}
noteLikes :: Note -> [Like]
noteLikes note = note.otype.likes.otype.ctype.items