Pleroma/lib/pleroma/web/activity_pub/visibility.ex

117 lines
3.4 KiB
Elixir
Raw Normal View History

# Pleroma: A lightweight social networking server
2020-03-03 23:44:49 +01:00
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
2019-02-22 13:29:52 +01:00
defmodule Pleroma.Web.ActivityPub.Visibility do
alias Pleroma.Activity
alias Pleroma.Object
alias Pleroma.Repo
2019-02-22 13:29:52 +01:00
alias Pleroma.User
alias Pleroma.Web.ActivityPub.Utils
2019-02-22 13:29:52 +01:00
require Pleroma.Constants
@spec is_public?(Object.t() | Activity.t() | map()) :: boolean()
2019-02-22 13:29:52 +01:00
def is_public?(%Object{data: %{"type" => "Tombstone"}}), do: false
def is_public?(%Object{data: data}), do: is_public?(data)
2019-10-30 12:21:49 +01:00
def is_public?(%Activity{data: %{"type" => "Move"}}), do: true
2019-02-22 13:29:52 +01:00
def is_public?(%Activity{data: data}), do: is_public?(data)
def is_public?(%{"directMessage" => true}), do: false
def is_public?(data), do: Utils.label_in_message?(Pleroma.Constants.as_public(), data)
2019-02-22 13:29:52 +01:00
def is_private?(activity) do
2019-05-08 18:08:50 +02:00
with false <- is_public?(activity),
%User{follower_address: follower_address} <-
User.get_cached_by_ap_id(activity.data["actor"]) do
follower_address in activity.data["to"]
2019-02-22 13:29:52 +01:00
else
2019-05-08 18:08:50 +02:00
_ -> false
2019-02-22 13:29:52 +01:00
end
end
def is_announceable?(activity, user, public \\ true) do
is_public?(activity) ||
(!public && is_private?(activity) && activity.data["actor"] == user.ap_id)
end
2019-02-22 13:29:52 +01:00
def is_direct?(%Activity{data: %{"directMessage" => true}}), do: true
def is_direct?(%Object{data: %{"directMessage" => true}}), do: true
def is_direct?(activity) do
!is_public?(activity) && !is_private?(activity)
end
2019-07-11 14:29:24 +02:00
def is_list?(%{data: %{"listMessage" => _}}), do: true
def is_list?(_), do: false
2020-03-20 11:04:37 +01:00
@spec visible_for_user?(Activity.t(), User.t() | nil) :: boolean()
2019-07-11 14:29:24 +02:00
def visible_for_user?(%{actor: ap_id}, %User{ap_id: ap_id}), do: true
2020-06-24 13:29:08 +02:00
def visible_for_user?(nil, _), do: false
def visible_for_user?(%{data: %{"listMessage" => _}}, nil), do: false
def visible_for_user?(%{data: %{"listMessage" => list_ap_id}} = activity, %User{} = user) do
user.ap_id in activity.data["to"] ||
list_ap_id
|> Pleroma.List.get_by_ap_id()
|> Pleroma.List.member?(user)
2019-07-11 14:29:24 +02:00
end
2020-03-20 11:04:37 +01:00
def visible_for_user?(%{local: local} = activity, nil) do
cfg_key =
if local,
do: :local,
else: :remote
if Pleroma.Config.get([:restrict_unauthenticated, :activities, cfg_key]),
do: false,
else: is_public?(activity)
2019-02-22 13:29:52 +01:00
end
def visible_for_user?(activity, user) do
x = [user.ap_id | User.following(user)]
2019-02-22 13:29:52 +01:00
y = [activity.actor] ++ activity.data["to"] ++ (activity.data["cc"] || [])
2020-03-20 11:04:37 +01:00
is_public?(activity) || Enum.any?(x, &(&1 in y))
2019-02-22 13:29:52 +01:00
end
def entire_thread_visible_for_user?(%Activity{} = activity, %User{} = user) do
{:ok, %{rows: [[result]]}} =
Ecto.Adapters.SQL.query(Repo, "SELECT thread_visibility($1, $2)", [
user.ap_id,
activity.data["id"]
])
2019-02-22 13:29:52 +01:00
result
2019-02-22 13:29:52 +01:00
end
def get_visibility(object) do
to = object.data["to"] || []
cc = object.data["cc"] || []
cond do
Pleroma.Constants.as_public() in to ->
"public"
Pleroma.Constants.as_public() in cc ->
"unlisted"
# this should use the sql for the object's activity
Enum.any?(to, &String.contains?(&1, "/followers")) ->
"private"
object.data["directMessage"] == true ->
"direct"
2019-07-11 14:29:24 +02:00
is_binary(object.data["listMessage"]) ->
"list"
length(cc) > 0 ->
"private"
true ->
"direct"
end
end
2019-02-22 13:29:52 +01:00
end