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 use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
import Ecto.Query import Ecto.Query
alias Pleroma.{Repo, User, Activity, Object} alias Pleroma.{Repo, User, Object}
alias Pleroma.Web.ActivityPub.ActivityPub
schema "users" do schema "users" do
field :bio, :string field :bio, :string
@ -91,9 +92,10 @@ def unfollow(%User{} = follower, %User{} = followed) do
following = follower.following following = follower.following
|> List.delete(ap_followers) |> List.delete(ap_followers)
follower { :ok, follower } = follower
|> follow_changeset(%{following: following}) |> follow_changeset(%{following: following})
|> Repo.update |> Repo.update
{ :ok, follower, ActivityPub.fetch_latest_follow(follower, followed)}
else else
{ :error, "Not subscribed!" } { :error, "Not subscribed!" }
end end

View file

@ -174,6 +174,16 @@ def fetch_activities_for_context(context) do
Repo.all(query) Repo.all(query)
end 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 def upload(file) do
data = Upload.store(file) data = Upload.store(file)
Repo.insert(%Object{data: data}) Repo.insert(%Object{data: data})

View file

@ -131,7 +131,13 @@ def follow(%User{} = follower, params) do
def unfollow(%User{} = follower, params) do def unfollow(%User{} = follower, params) do
with { :ok, %User{} = unfollowed } <- get_user(params), 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 do
{ :ok, follower, unfollowed } { :ok, follower, unfollowed }
else else

View file

@ -64,4 +64,21 @@ def like_activity_factory do
data: data data: data
} }
end 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 end

View file

@ -41,7 +41,7 @@ test "unfollow takes a user and another user" do
followed = insert(:user) followed = insert(:user)
user = insert(:user, %{following: [User.ap_followers(followed)]}) 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) user = Repo.get(User, user.id)

File diff suppressed because one or more lines are too long