try delete follower too
This commit is contained in:
parent
396bbb7969
commit
195072c237
3 changed files with 75 additions and 1 deletions
23
app/DB.hs
23
app/DB.hs
|
@ -17,6 +17,7 @@ data DB
|
||||||
, getNote :: DB.Int64 -> IO (Maybe Note)
|
, getNote :: DB.Int64 -> IO (Maybe Note)
|
||||||
, insertNote :: NoteEntry -> IO (DB.Int64, Note)
|
, insertNote :: NoteEntry -> IO (DB.Int64, Note)
|
||||||
, insertFollower :: FollowerEntry -> IO DB.Int64
|
, insertFollower :: FollowerEntry -> IO DB.Int64
|
||||||
|
, deleteFollower :: FollowerEntry -> IO DB.Int64
|
||||||
, getFollowers :: IO [Follower]
|
, getFollowers :: IO [Follower]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,6 +62,8 @@ mkDB connstr details = do
|
||||||
\note -> DB.withPool pool (insertNoteToDb (actorUrl details) note)
|
\note -> DB.withPool pool (insertNoteToDb (actorUrl details) note)
|
||||||
, insertFollower =
|
, insertFollower =
|
||||||
\follower -> DB.withPool pool (insertFollowerToDb follower)
|
\follower -> DB.withPool pool (insertFollowerToDb follower)
|
||||||
|
, deleteFollower =
|
||||||
|
\follower -> DB.withPool pool (deleteFollowerFromDb follower)
|
||||||
, getFollowers =
|
, getFollowers =
|
||||||
DB.withPool pool (getFollowersFromDb $ actorUrl details)
|
DB.withPool pool (getFollowersFromDb $ actorUrl details)
|
||||||
}
|
}
|
||||||
|
@ -139,6 +142,11 @@ insertFollowerToDb follower = do
|
||||||
[n] <- map decodeIntRow <$> uncurry DB.runWith (insertFollowerSQL follower)
|
[n] <- map decodeIntRow <$> uncurry DB.runWith (insertFollowerSQL follower)
|
||||||
pure n
|
pure n
|
||||||
|
|
||||||
|
deleteFollowerFromDb :: FollowerEntry -> DB.SQLite DB.Int64
|
||||||
|
deleteFollowerFromDb follower = do
|
||||||
|
[n] <- map decodeIntRow <$> uncurry DB.runWith (deleteFollowerSQL follower)
|
||||||
|
pure n
|
||||||
|
|
||||||
getFollowersFromDb :: Url -> DB.SQLite [Follower]
|
getFollowersFromDb :: Url -> DB.SQLite [Follower]
|
||||||
getFollowersFromDb url =
|
getFollowersFromDb url =
|
||||||
map decodeFollowerRow <$> uncurry DB.runWith (getFollowersSQL url)
|
map decodeFollowerRow <$> uncurry DB.runWith (getFollowersSQL url)
|
||||||
|
@ -211,7 +219,7 @@ insertNoteSQL actor note =
|
||||||
insertFollowerSQL :: FollowerEntry -> (DB.SQL, [DB.SQLData])
|
insertFollowerSQL :: FollowerEntry -> (DB.SQL, [DB.SQLData])
|
||||||
insertFollowerSQL follower =
|
insertFollowerSQL follower =
|
||||||
( [r|
|
( [r|
|
||||||
INSERT INTO note(follow_id, actor)
|
INSERT INTO follower(follow_id, actor)
|
||||||
VALUES (?, ?)
|
VALUES (?, ?)
|
||||||
RETURNING id
|
RETURNING id
|
||||||
|]
|
|]
|
||||||
|
@ -221,6 +229,19 @@ insertFollowerSQL follower =
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
deleteFollowerSQL :: FollowerEntry -> (DB.SQL, [DB.SQLData])
|
||||||
|
deleteFollowerSQL follower =
|
||||||
|
( [r|
|
||||||
|
DELETE FROM follower
|
||||||
|
WHERE followId = ? AND actor = ?
|
||||||
|
RETURNING followId
|
||||||
|
|]
|
||||||
|
,
|
||||||
|
[ DB.SQLText follower.followId
|
||||||
|
, DB.SQLText follower.actorId
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
getFollowersSQL :: Url -> (DB.SQL, [DB.SQLData])
|
getFollowersSQL :: Url -> (DB.SQL, [DB.SQLData])
|
||||||
getFollowersSQL url =
|
getFollowersSQL url =
|
||||||
( [r|
|
( [r|
|
||||||
|
|
|
@ -150,6 +150,32 @@ handleInbox db detailsFile activity = do
|
||||||
else Twain.next
|
else Twain.next
|
||||||
Nothing ->
|
Nothing ->
|
||||||
Twain.next
|
Twain.next
|
||||||
|
Fedi.ActivityUndo
|
||||||
|
( Fedi.Object
|
||||||
|
{ otype = Fedi.TypeActivity
|
||||||
|
{ atype = Fedi.TypeUndo
|
||||||
|
{ object = Fedi.ActivityFollow follow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}) -> do
|
||||||
|
let
|
||||||
|
id' = follow.id
|
||||||
|
actor = follow.otype.actor
|
||||||
|
object = follow.otype.atype.object
|
||||||
|
case id' of
|
||||||
|
Just id'' -> do
|
||||||
|
if object == Fedi.LLink (Fedi.Link $ Fedi.actorUrl details)
|
||||||
|
then do
|
||||||
|
liftIO do
|
||||||
|
deletedId <- db.deleteFollower FollowerEntry
|
||||||
|
{ actorId = fromString actor.unwrap
|
||||||
|
, followId = fromString id''.unwrap
|
||||||
|
}
|
||||||
|
print ("deleted follower: " <> show deletedId)
|
||||||
|
pure $ Fedi.jsonLD "{}"
|
||||||
|
else Twain.next
|
||||||
|
Nothing ->
|
||||||
|
Twain.next
|
||||||
_ -> do
|
_ -> do
|
||||||
liftIO (print activity)
|
liftIO (print activity)
|
||||||
Twain.next
|
Twain.next
|
||||||
|
|
|
@ -322,6 +322,30 @@ instance A.FromJSON TypeFollow where
|
||||||
object <- value A..: "object"
|
object <- value A..: "object"
|
||||||
pure TypeFollow {..}
|
pure TypeFollow {..}
|
||||||
|
|
||||||
|
-- | Undo
|
||||||
|
type Undo = Activity TypeUndo
|
||||||
|
|
||||||
|
data TypeUndo
|
||||||
|
= TypeUndo
|
||||||
|
{ object :: AnyActivity
|
||||||
|
}
|
||||||
|
deriving (Show, Eq)
|
||||||
|
|
||||||
|
instance ToObject TypeUndo where
|
||||||
|
toObject undo =
|
||||||
|
[ "type" A..= ("Undo" :: String)
|
||||||
|
, "object" A..= undo.object
|
||||||
|
]
|
||||||
|
|
||||||
|
instance A.FromJSON TypeUndo where
|
||||||
|
parseJSON =
|
||||||
|
A.withObject "TypeUndo" \value -> do
|
||||||
|
typ :: String <- value A..: "type"
|
||||||
|
guard (typ == "Undo")
|
||||||
|
object <- value A..: "object"
|
||||||
|
pure TypeUndo {..}
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
type Like = Object (TypeActivity TypeLike)
|
type Like = Object (TypeActivity TypeLike)
|
||||||
|
|
||||||
|
@ -342,6 +366,7 @@ instance A.FromJSON TypeLike where
|
||||||
data AnyActivity
|
data AnyActivity
|
||||||
= -- ActivityAnnounce Announce
|
= -- ActivityAnnounce Announce
|
||||||
ActivityCreate Create
|
ActivityCreate Create
|
||||||
|
| ActivityUndo Undo
|
||||||
| ActivityFollow Follow
|
| ActivityFollow Follow
|
||||||
| -- | ActivityLike Like
|
| -- | ActivityLike Like
|
||||||
ActivityAccept Accept
|
ActivityAccept Accept
|
||||||
|
@ -352,6 +377,7 @@ instance A.ToJSON AnyActivity where
|
||||||
toJSON = \case
|
toJSON = \case
|
||||||
-- ActivityAnnounce obj -> A.toJSON obj
|
-- ActivityAnnounce obj -> A.toJSON obj
|
||||||
ActivityCreate obj -> A.toJSON obj
|
ActivityCreate obj -> A.toJSON obj
|
||||||
|
ActivityUndo obj -> A.toJSON obj
|
||||||
ActivityFollow obj -> A.toJSON obj
|
ActivityFollow obj -> A.toJSON obj
|
||||||
-- ActivityLike obj -> A.toJSON obj
|
-- ActivityLike obj -> A.toJSON obj
|
||||||
ActivityAccept obj -> A.toJSON obj
|
ActivityAccept obj -> A.toJSON obj
|
||||||
|
@ -363,6 +389,7 @@ instance A.FromJSON AnyActivity where
|
||||||
typ :: String <- v A..: "type"
|
typ :: String <- v A..: "type"
|
||||||
case typ of
|
case typ of
|
||||||
"Create" -> ActivityCreate <$> A.parseJSON value
|
"Create" -> ActivityCreate <$> A.parseJSON value
|
||||||
|
"Undo" -> ActivityUndo <$> A.parseJSON value
|
||||||
"Follow" -> ActivityFollow <$> A.parseJSON value
|
"Follow" -> ActivityFollow <$> A.parseJSON value
|
||||||
"Accept" -> ActivityAccept <$> A.parseJSON value
|
"Accept" -> ActivityAccept <$> A.parseJSON value
|
||||||
"Reject" -> ActivityReject <$> A.parseJSON value
|
"Reject" -> ActivityReject <$> A.parseJSON value
|
||||||
|
|
Loading…
Add table
Reference in a new issue