Commit 28b203d0 authored by dtluna's avatar dtluna

Add Undo of Follow Activity insertion

parent a9b3f99d
......@@ -2,7 +2,8 @@ defmodule Pleroma.User do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
alias Pleroma.{Repo, User, Activity, Object}
alias Pleroma.{Repo, User, Object}
alias Pleroma.Web.ActivityPub.ActivityPub
schema "users" do
field :bio, :string
......@@ -91,9 +92,10 @@ def unfollow(%User{} = follower, %User{} = followed) do
following = follower.following
|> List.delete(ap_followers)
follower
{ :ok, follower } = follower
|> follow_changeset(%{following: following})
|> Repo.update
{ :ok, follower, ActivityPub.fetch_latest_follow(follower, followed)}
else
{ :error, "Not subscribed!" }
end
......
......@@ -174,6 +174,16 @@ def fetch_activities_for_context(context) do
Repo.all(query)
end
def fetch_latest_follow(%User{ap_id: follower_id},
%User{ap_id: followed_id}) do
query = from activity in Activity,
where: fragment("? @> ?", activity.data, ^%{type: "Follow", actor: follower_id,
object: followed_id}),
order_by: [desc: :inserted_at],
limit: 1
Repo.one(query)
end
def upload(file) do
data = Upload.store(file)
Repo.insert(%Object{data: data})
......
......@@ -129,11 +129,17 @@ def follow(%User{} = follower, params) do
end
end
def unfollow(%User{} = follower, params) do
def unfollow(%User{} = follower, params) do
with { :ok, %User{} = unfollowed } <- get_user(params),
{ :ok, follower } <- User.unfollow(follower, unfollowed)
{ :ok, follower, follow_activity } <- User.unfollow(follower, unfollowed),
{ :ok, _activity } <- ActivityPub.insert(%{
"type" => "Undo",
"actor" => follower.ap_id,
"object" => follow_activity, # get latest Follow for these users
"published" => make_date()
})
do
{ :ok, follower, unfollowed}
{ :ok, follower, unfollowed }
else
err -> err
end
......
......@@ -64,4 +64,21 @@ def like_activity_factory do
data: data
}
end
def follow_activity_factory do
follower = insert(:user)
followed = insert(:user)
data = %{
"id" => Pleroma.Web.ActivityPub.ActivityPub.generate_activity_id,
"actor" => follower.ap_id,
"type" => "Follow",
"object" => followed.ap_id,
"published_at" => DateTime.utc_now() |> DateTime.to_iso8601
}
%Pleroma.Activity{
data: data
}
end
end
......@@ -41,7 +41,7 @@ test "unfollow takes a user and another user" do
followed = insert(:user)
user = insert(:user, %{following: [User.ap_followers(followed)]})
{:ok, user } = User.unfollow(user, followed)
{:ok, user, _activity } = User.unfollow(user, followed)
user = Repo.get(User, user.id)
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment