Add Undo of Follow Activity insertion

This commit is contained in:
dtluna 2017-04-21 19:54:21 +03:00
parent a9b3f99d48
commit 28b203d08f
6 changed files with 51 additions and 6 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

File diff suppressed because one or more lines are too long