From bbba33925ce499dfb8fa47c59dd7cce1cdfd6b29 Mon Sep 17 00:00:00 2001 From: me Date: Sat, 9 Nov 2024 15:09:34 +0200 Subject: [PATCH] undo like --- app/DB.hs | 24 ++++++++++++++++++++++++ app/Routes/Inbox.hs | 16 ++++++++++++++-- app/Routes/Inbox/Like.hs | 39 ++++++++++++++------------------------- 3 files changed, 52 insertions(+), 27 deletions(-) diff --git a/app/DB.hs b/app/DB.hs index 38b3a48..0cf66b9 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) -- , deleteLike :: LikeEntry -> IO (Maybe DB.Int64) , insertFollower :: forall a @@ -102,6 +103,8 @@ mkDB connstr details = do \note -> DB.withPool pool (insertNoteToDb (actorUrl details) note) , insertLike = \like -> DB.withPool pool (insertLikeToDb like) + , deleteLike = + \like -> DB.withPool pool (deleteLikeToDb like) , insertFollower = \follower handle -> DB.withPool pool $ DB.transaction do id' <- insertFollowerToDb follower @@ -204,6 +207,11 @@ insertLikeToDb like = do [n] <- map decodeIntRow <$> uncurry DB.runWith (insertLikeSQL like) pure n +deleteLikeToDb :: LikeEntry -> DB.SQLite (Maybe DB.Int64) +deleteLikeToDb like = do + ns <- map decodeIntRow <$> uncurry DB.runWith (deleteLikeSQL like) + pure $ listToMaybe ns + insertFollowerToDb :: FollowerEntry -> DB.SQLite DB.Int64 insertFollowerToDb follower = do [n] <- map decodeIntRow <$> uncurry DB.runWith (insertFollowerSQL follower) @@ -374,6 +382,22 @@ insertLikeSQL like = ] ) +deleteLikeSQL :: LikeEntry -> (DB.SQL, [DB.SQLData]) +deleteLikeSQL like = + ( [r| + DELETE FROM like + WHERE like_url = ? + AND actor_url = ? + AND note_url = ? + RETURNING + id as id + |] + , [ DB.SQLText (T.pack like.likeUrl) + , DB.SQLText (T.pack like.likeActorUrl.unwrap) + , DB.SQLText (T.pack like.likeNoteUrl.unwrap) + ] + ) + insertFollowerSQL :: FollowerEntry -> (DB.SQL, [DB.SQLData]) insertFollowerSQL follower = ( [r| diff --git a/app/Routes/Inbox.hs b/app/Routes/Inbox.hs index 18301fc..ffdf514 100644 --- a/app/Routes/Inbox.hs +++ b/app/Routes/Inbox.hs @@ -16,8 +16,6 @@ handleInbox db detailsFile activity = do case activity of Fedi.ActivityFollow follow -> handleInboxFollow details db activity follow - Fedi.ActivityLike like -> - handleInboxLike db like Fedi.ActivityUndo ( Fedi.Object { otype = @@ -30,6 +28,20 @@ handleInbox db detailsFile activity = do } ) -> handleInboxUnfollow details db activity follow + Fedi.ActivityLike like -> + handleInboxLike db like + Fedi.ActivityUndo + ( Fedi.Object + { otype = + Fedi.TypeActivity + { atype = + Fedi.TypeUndo + { object = Fedi.ActivityLike like + } + } + } + ) -> + handleInboxUnlike db like _ -> do Log.logError $ "Unsupported activity: " <> Fedi.pShow activity Twain.next diff --git a/app/Routes/Inbox/Like.hs b/app/Routes/Inbox/Like.hs index a23c8b7..3872ca2 100644 --- a/app/Routes/Inbox/Like.hs +++ b/app/Routes/Inbox/Like.hs @@ -32,7 +32,7 @@ handleInboxLike db like = do Nothing -> Twain.next - {- + handleInboxUnlike :: DB -> Fedi.Like @@ -44,29 +44,18 @@ handleInboxUnlike db like = do note = like.otype.atype.object case id' of Just id'' -> do - let - followerEntry = - ( LikeEntry - { likeUrl = fromString id''.unwrap - , likeActorUrl = actor - , likeNoteUrl = note - } - ) - operation sendAccept = do - deleteFollower - db - LikeEntry - ( \deletedId' -> do - let - deletedId = Fedi.fromMaybe 0 deletedId' - sendAccept deletedId - <* Log.logInfo ("Deleted follower: " <> Fedi.pShow deletedId) - ) - - liftIO $ acceptRequest details actor activity operation - - pure $ Twain.text "" + let + likeEntry = + ( LikeEntry + { likeUrl = fromString id''.unwrap + , likeActorUrl = actor + , likeNoteUrl = note + } + ) + operation = do + likeid <- db.deleteLike likeEntry + Log.logInfo ("Unlike: " <> Fedi.pShow (likeid, likeEntry)) + liftIO operation + pure $ Twain.text "" Nothing -> Twain.next - --}