Change relationship direction of subscriptions

This commit is contained in:
Sadposter 2019-04-05 16:51:45 +01:00 committed by Hannah Ward
parent d35f6551c1
commit 9ca91cbb87
12 changed files with 84 additions and 94 deletions

View file

@ -924,24 +924,28 @@ defmodule Pleroma.User do
end end
def subscribe(subscriber, %{ap_id: ap_id}) do def subscribe(subscriber, %{ap_id: ap_id}) do
with %User{} = user <- get_or_fetch_by_ap_id(ap_id) do
info_cng = info_cng =
subscriber.info user.info
|> User.Info.add_to_subscriptions(ap_id) |> User.Info.add_to_subscribers(subscriber.ap_id)
change(subscriber) change(user)
|> put_embed(:info, info_cng) |> put_embed(:info, info_cng)
|> update_and_set_cache() |> update_and_set_cache()
end end
end
def unsubscribe(unsubscriber, %{ap_id: ap_id}) do def unsubscribe(unsubscriber, %{ap_id: ap_id}) do
with %User{} = user <- get_or_fetch_by_ap_id(ap_id) do
info_cng = info_cng =
unsubscriber.info user.info
|> User.Info.remove_from_subscriptions(ap_id) |> User.Info.remove_from_subscribers(unsubscriber.ap_id)
change(unsubscriber) change(user)
|> put_embed(:info, info_cng) |> put_embed(:info, info_cng)
|> update_and_set_cache() |> update_and_set_cache()
end end
end
def block(blocker, %User{ap_id: ap_id} = blocked) do def block(blocker, %User{ap_id: ap_id} = blocked) do
# sever any follow relationships to prevent leaks per activitypub (Pleroma issue #213) # sever any follow relationships to prevent leaks per activitypub (Pleroma issue #213)
@ -1000,8 +1004,9 @@ defmodule Pleroma.User do
end end
def subscribed_to?(user, %{ap_id: ap_id}) do def subscribed_to?(user, %{ap_id: ap_id}) do
subs = user.info.subscriptions with %User{} = target <- User.get_by_ap_id(ap_id) do
Enum.member?(subs, ap_id) Enum.member?(target.info.subscribers, user.ap_id)
end
end end
def muted_users(user), def muted_users(user),
@ -1010,13 +1015,8 @@ defmodule Pleroma.User do
def blocked_users(user), def blocked_users(user),
do: Repo.all(from(u in User, where: u.ap_id in ^user.info.blocks)) do: Repo.all(from(u in User, where: u.ap_id in ^user.info.blocks))
def subscribed_users(user), def subscribers(user),
do: do: Repo.all(from(u in User, where: u.ap_id in ^user.info.subscribers))
Repo.all(
from(u in User,
where: fragment("?->'subscriptions' @> ?", u.info, ^user.ap_id)
)
)
def block_domain(user, domain) do def block_domain(user, domain) do
info_cng = info_cng =

View file

@ -22,7 +22,7 @@ defmodule Pleroma.User.Info do
field(:domain_blocks, {:array, :string}, default: []) field(:domain_blocks, {:array, :string}, default: [])
field(:mutes, {:array, :string}, default: []) field(:mutes, {:array, :string}, default: [])
field(:muted_reblogs, {:array, :string}, default: []) field(:muted_reblogs, {:array, :string}, default: [])
field(:subscriptions, {:array, :string}, default: []) field(:subscribers, {:array, :string}, default: [])
field(:deactivated, :boolean, default: false) field(:deactivated, :boolean, default: false)
field(:no_rich_text, :boolean, default: false) field(:no_rich_text, :boolean, default: false)
field(:ap_enabled, :boolean, default: false) field(:ap_enabled, :boolean, default: false)
@ -94,12 +94,12 @@ defmodule Pleroma.User.Info do
|> validate_required([:blocks]) |> validate_required([:blocks])
end end
def set_subscriptions(info, subscriptions) do def set_subscribers(info, subscribers) do
params = %{subscriptions: subscriptions} params = %{subscribers: subscribers}
info info
|> cast(params, [:subscriptions]) |> cast(params, [:subscribers])
|> validate_required([:subscriptions]) |> validate_required([:subscribers])
end end
def add_to_mutes(info, muted) do def add_to_mutes(info, muted) do
@ -118,12 +118,12 @@ defmodule Pleroma.User.Info do
set_blocks(info, List.delete(info.blocks, blocked)) set_blocks(info, List.delete(info.blocks, blocked))
end end
def add_to_subscriptions(info, subscribed) do def add_to_subscribers(info, subscribed) do
set_subscriptions(info, Enum.uniq([subscribed | info.subscriptions])) set_subscribers(info, Enum.uniq([subscribed | info.subscribers]))
end end
def remove_from_subscriptions(info, subscribed) do def remove_from_subscribers(info, subscribed) do
set_subscriptions(info, List.delete(info.subscriptions, subscribed)) set_subscribers(info, List.delete(info.subscribers, subscribed))
end end
def set_domain_blocks(info, domain_blocks) do def set_domain_blocks(info, domain_blocks) do

View file

@ -343,7 +343,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do
with %User{} = user <- User.get_by_ap_id(actor) do with %User{} = user <- User.get_by_ap_id(actor) do
subscriber_ids = subscriber_ids =
user user
|> User.subscribed_users() |> User.subscribers()
|> Enum.map(& &1.ap_id) |> Enum.map(& &1.ap_id)
recipients ++ subscriber_ids recipients ++ subscriber_ids

View file

@ -863,6 +863,26 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
json(conn, %{}) json(conn, %{})
end end
def subscribe(%{assigns: %{user: user}} = conn, %{"id" => id}) do
with %User{} = subscription_target <- User.get_by_id(id) do
{:ok, subscription_target} = User.subscribe(user, subscription_target)
conn
|> put_view(AccountView)
|> render("relationship.json", %{user: user, target: subscription_target})
end
end
def unsubscribe(%{assigns: %{user: user}} = conn, %{"id" => id}) do
with %User{} = subscription_target <- User.get_by_id(id) do
{:ok, subscription_target} = User.unsubscribe(user, subscription_target)
conn
|> put_view(AccountView)
|> render("relationship.json", %{user: user, target: subscription_target})
end
end
def status_search(user, query) do def status_search(user, query) do
fetched = fetched =
if Regex.match?(~r/https?:/, query) do if Regex.match?(~r/https?:/, query) do

View file

@ -53,6 +53,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
blocking: User.blocks?(user, target), blocking: User.blocks?(user, target),
muting: User.mutes?(user, target), muting: User.mutes?(user, target),
muting_notifications: false, muting_notifications: false,
subscribing: User.subscribed_to?(user, target),
requested: requested, requested: requested,
domain_blocking: false, domain_blocking: false,
showing_reblogs: User.showing_reblogs?(user, target), showing_reblogs: User.showing_reblogs?(user, target),

View file

@ -311,6 +311,9 @@ defmodule Pleroma.Web.Router do
post("/domain_blocks", MastodonAPIController, :block_domain) post("/domain_blocks", MastodonAPIController, :block_domain)
delete("/domain_blocks", MastodonAPIController, :unblock_domain) delete("/domain_blocks", MastodonAPIController, :unblock_domain)
post("/pleroma/accounts/:id/subscribe", MastodonAPIController, :subscribe)
post("/pleroma/accounts/:id/unsubscribe", MastodonAPIController, :unsubscribe)
end end
scope [] do scope [] do
@ -495,9 +498,6 @@ defmodule Pleroma.Web.Router do
post("/pleroma/friendships/approve", TwitterAPI.Controller, :approve_friend_request) post("/pleroma/friendships/approve", TwitterAPI.Controller, :approve_friend_request)
post("/pleroma/friendships/deny", TwitterAPI.Controller, :deny_friend_request) post("/pleroma/friendships/deny", TwitterAPI.Controller, :deny_friend_request)
post("/pleroma/subscriptions/create", TwitterAPI.Controller, :subscribe)
post("/pleroma/subscriptions/destroy", TwitterAPI.Controller, :unsubscribe)
post("/friendships/create", TwitterAPI.Controller, :follow) post("/friendships/create", TwitterAPI.Controller, :follow)
post("/friendships/destroy", TwitterAPI.Controller, :unfollow) post("/friendships/destroy", TwitterAPI.Controller, :unfollow)

View file

@ -59,20 +59,6 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
end end
end end
def subscribe(%User{} = subscriber, params) do
with {:ok, %User{} = subscribed} <- get_user(params),
{:ok, subscriber} <- User.subscribe(subscriber, subscribed) do
{:ok, subscriber, subscribed}
end
end
def unsubscribe(%User{} = unsubscriber, params) do
with {:ok, %User{} = unsubscribed} <- get_user(params),
{:ok, unsubscriber} <- User.unsubscribe(unsubscriber, unsubscribed) do
{:ok, unsubscriber, unsubscribed}
end
end
def repeat(%User{} = user, ap_id_or_id) do def repeat(%User{} = user, ap_id_or_id) do
with {:ok, _announce, %{data: %{"id" => id}}} <- CommonAPI.repeat(ap_id_or_id, user), with {:ok, _announce, %{data: %{"id" => id}}} <- CommonAPI.repeat(ap_id_or_id, user),
%Activity{} = activity <- Activity.get_create_by_object_ap_id(id) do %Activity{} = activity <- Activity.get_create_by_object_ap_id(id) do

View file

@ -269,30 +269,6 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
end end
end end
def subscribe(%{assigns: %{user: user}} = conn, params) do
case TwitterAPI.subscribe(user, params) do
{:ok, user, subscribed} ->
conn
|> put_view(UserView)
|> render("show.json", %{user: subscribed, for: user})
{:error, msg} ->
forbidden_json_reply(conn, msg)
end
end
def unsubscribe(%{assigns: %{user: user}} = conn, params) do
case TwitterAPI.unsubscribe(user, params) do
{:ok, user, unsubscribed} ->
conn
|> put_view(UserView)
|> render("show.json", %{user: unsubscribed, for: user})
{:error, msg} ->
forbidden_json_reply(conn, msg)
end
end
def fetch_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do def fetch_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
with %Activity{} = activity <- Activity.get_by_id(id), with %Activity{} = activity <- Activity.get_by_id(id),
true <- Visibility.visible_for_user?(activity, user) do true <- Visibility.visible_for_user?(activity, user) do

View file

@ -47,16 +47,15 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
for_user = assigns[:for] for_user = assigns[:for]
image = User.avatar_url(user) |> MediaProxy.url() image = User.avatar_url(user) |> MediaProxy.url()
{following, follows_you, statusnet_blocking, subscribed} = {following, follows_you, statusnet_blocking} =
if for_user do if for_user do
{ {
User.following?(for_user, user), User.following?(for_user, user),
User.following?(user, for_user), User.following?(user, for_user),
User.blocks?(for_user, user), User.blocks?(for_user, user)
User.subscribed_to?(for_user, user)
} }
else else
{false, false, false, false} {false, false, false}
end end
user_info = User.get_cached_user_info(user) user_info = User.get_cached_user_info(user)
@ -117,8 +116,7 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
"pleroma" => "pleroma" =>
%{ %{
"confirmation_pending" => user_info.confirmation_pending, "confirmation_pending" => user_info.confirmation_pending,
"tags" => user.tags, "tags" => user.tags
"subscribed" => subscribed
} }
|> maybe_with_activation_status(user, for_user) |> maybe_with_activation_status(user, for_user)
} }

View file

@ -142,6 +142,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
blocking: true, blocking: true,
muting: false, muting: false,
muting_notifications: false, muting_notifications: false,
subscribing: false,
requested: false, requested: false,
domain_blocking: false, domain_blocking: false,
showing_reblogs: true, showing_reblogs: true,
@ -198,6 +199,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
following: false, following: false,
followed_by: false, followed_by: false,
blocking: true, blocking: true,
subscribing: false,
muting: false, muting: false,
muting_notifications: false, muting_notifications: false,
requested: false, requested: false,

View file

@ -1555,6 +1555,25 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
assert %{"id" => _id, "muting" => false} = json_response(conn, 200) assert %{"id" => _id, "muting" => false} = json_response(conn, 200)
end end
test "subscribing / unsubscribing to a user", %{conn: conn} do
user = insert(:user)
subscription_target = insert(:user)
conn =
conn
|> assign(:user, user)
|> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
assert %{"id" => _id, "subscribing" => true} = json_response(conn, 200)
conn =
build_conn()
|> assign(:user, user)
|> post("/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
assert %{"id" => _id, "subscribing" => false} = json_response(conn, 200)
end
test "getting a list of mutes", %{conn: conn} do test "getting a list of mutes", %{conn: conn} do
user = insert(:user) user = insert(:user)
other_user = insert(:user) other_user = insert(:user)

View file

@ -105,8 +105,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
"fields" => [], "fields" => [],
"pleroma" => %{ "pleroma" => %{
"confirmation_pending" => false, "confirmation_pending" => false,
"tags" => [], "tags" => []
"subscribed" => false
} }
} }
@ -154,8 +153,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
"fields" => [], "fields" => [],
"pleroma" => %{ "pleroma" => %{
"confirmation_pending" => false, "confirmation_pending" => false,
"tags" => [], "tags" => []
"subscribed" => false
} }
} }
@ -204,22 +202,13 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
"fields" => [], "fields" => [],
"pleroma" => %{ "pleroma" => %{
"confirmation_pending" => false, "confirmation_pending" => false,
"tags" => [], "tags" => []
"subscribed" => false
} }
} }
assert represented == UserView.render("show.json", %{user: follower, for: user}) assert represented == UserView.render("show.json", %{user: follower, for: user})
end end
test "a user that you are subscribed to" do
user = insert(:user)
subscriber = insert(:user)
{:ok, subscriber} = User.subscribe(subscriber, user)
represented = UserView.render("show.json", %{user: user, for: subscriber})
assert represented["pleroma"]["subscribed"] == true
end
test "a user that is a moderator" do test "a user that is a moderator" do
user = insert(:user, %{info: %{is_moderator: true}}) user = insert(:user, %{info: %{is_moderator: true}})
represented = UserView.render("show.json", %{user: user, for: user}) represented = UserView.render("show.json", %{user: user, for: user})
@ -299,8 +288,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
"fields" => [], "fields" => [],
"pleroma" => %{ "pleroma" => %{
"confirmation_pending" => false, "confirmation_pending" => false,
"tags" => [], "tags" => []
"subscribed" => false
} }
} }