module Routes.Inbox.Like where

import Control.Logger.Simple qualified as Log
import DB
import Fedi qualified as Fedi
import Web.Twain qualified as Twain

handleInboxLike
  :: DB
  -> Fedi.Like
  -> Twain.ResponderM Twain.Response
handleInboxLike db like = do
  let
    id' = like.id
    actor = like.otype.actor
    note = like.otype.atype.object
  case id' of
    Just id'' -> do
      let
        likeEntry =
          ( LikeEntry
              { likeUrl = fromString id''.unwrap
              , likeActorUrl = actor
              , likeNoteUrl = note
              }
          )
        operation = do
          likeid <- db.insertLike likeEntry
          Log.logInfo ("New like: " <> Fedi.pShow (likeid, likeEntry))
      liftIO operation
      pure $ Twain.text ""
    Nothing ->
      Twain.next


handleInboxUnlike
  :: DB
  -> Fedi.Like
  -> Twain.ResponderM Twain.Response
handleInboxUnlike db like = do
  let
    id' = like.id
    actor = like.otype.actor
    note = like.otype.atype.object
  case id' of
    Just id'' -> do
      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