Add API endpoints and user column for subscription

This commit is contained in:
Sadposter 2019-04-05 13:49:33 +01:00 committed by Hannah Ward
parent 9c9eec62c2
commit 79910ce5cc
5 changed files with 76 additions and 0 deletions

View File

@ -923,6 +923,26 @@ defmodule Pleroma.User do
update_and_set_cache(cng) update_and_set_cache(cng)
end end
def subscribe(subscriber, %{ap_id: ap_id}) do
info_cng =
subscriber.info
|> User.Info.add_to_subscriptions(ap_id)
change(subscriber)
|> put_embed(:info, info_cng)
|> update_and_set_cache()
end
def unsubscribe(unsubscriber, %{ap_id: ap_id}) do
info_cng =
subscriber.info
|> User.Info.remove_from_subscriptions(ap_id)
change(subscriber)
|> put_embed(:info, info_cng)
|> update_and_set_cache()
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)
blocker = blocker =

View File

@ -22,6 +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(:subscribed_to, {: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)
@ -93,6 +94,14 @@ defmodule Pleroma.User.Info do
|> validate_required([:blocks]) |> validate_required([:blocks])
end end
def set_subscriptions(info, subscriptions) do
params = %{subscriptions: subscriptions}
info
|> cast(params, [:subscribed_to])
|> validate_required([:subscribed_to])
end
def add_to_mutes(info, muted) do def add_to_mutes(info, muted) do
set_mutes(info, Enum.uniq([muted | info.mutes])) set_mutes(info, Enum.uniq([muted | info.mutes]))
end end
@ -109,6 +118,14 @@ 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
set_subscriptions(info, Enum.uniq([subscribed | info.subscribed_to]))
end
def remove_from_subscriptions(info, subscribed) do
set_subscriptions(info, List.delete(info.subscribed_to, subscribed))
end
def set_domain_blocks(info, domain_blocks) do def set_domain_blocks(info, domain_blocks) do
params = %{domain_blocks: domain_blocks} params = %{domain_blocks: domain_blocks}

View File

@ -500,6 +500,9 @@ defmodule Pleroma.Web.Router do
post("/blocks/create", TwitterAPI.Controller, :block) post("/blocks/create", TwitterAPI.Controller, :block)
post("/blocks/destroy", TwitterAPI.Controller, :unblock) post("/blocks/destroy", TwitterAPI.Controller, :unblock)
post("/subscriptions/create", TwitterAPI.Controller, :subscribe)
post("/subscriptions/destroy", TwitterAPI.Controller, :unsubscribe)
end end
end end

View File

@ -59,6 +59,18 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
end end
end end
def subscribe(%User{} = subscriber, params) do
with {:ok, %User{} = subscribed} <- get_user(params) do
User.subscribe(subscriber, subscribed)
end
end
def unsubscribe(%User{} = unsubscriber, params) do
with {:ok, %User{} = unsubscribed} <- get_user(params) do
User.unsubscribe(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,6 +269,30 @@ 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