Add subscribed status to user view

Added in pleroma extensions, but can be moved whenever
This commit is contained in:
Sadposter 2019-04-05 15:21:33 +01:00 committed by Hannah Ward
parent 75c4ceb4a2
commit d56866c824
3 changed files with 25 additions and 7 deletions

View File

@ -999,6 +999,11 @@ def blocks?(user, %{ap_id: ap_id}) do
end) end)
end end
def subscribed_to?(user, %{ap_id: ap_id}) do
subs = user.info.subscriptions
Enum.member?(subs, ap_id)
end
def muted_users(user), def muted_users(user),
do: Repo.all(from(u in User, where: u.ap_id in ^user.info.mutes)) do: Repo.all(from(u in User, where: u.ap_id in ^user.info.mutes))

View File

@ -47,15 +47,16 @@ defp do_render("user.json", %{user: user = %User{}} = assigns) 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} = {following, follows_you, statusnet_blocking, subscribed} =
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)
@ -116,7 +117,8 @@ defp do_render("user.json", %{user: user = %User{}} = assigns) 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

@ -105,7 +105,8 @@ test "A user" do
"fields" => [], "fields" => [],
"pleroma" => %{ "pleroma" => %{
"confirmation_pending" => false, "confirmation_pending" => false,
"tags" => [] "tags" => [],
"subscribed" => false
} }
} }
@ -153,7 +154,8 @@ test "A user for a given other follower", %{user: user} do
"fields" => [], "fields" => [],
"pleroma" => %{ "pleroma" => %{
"confirmation_pending" => false, "confirmation_pending" => false,
"tags" => [] "tags" => [],
"subscribed" => false,
} }
} }
@ -202,13 +204,22 @@ test "A user that follows you", %{user: user} 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})