2019-07-09 23:13:23 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-02-22 05:29:52 -07:00
|
|
|
defmodule Pleroma.Web.ActivityPub.Visibility do
|
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.Object
|
2019-03-24 18:38:28 -06:00
|
|
|
alias Pleroma.Repo
|
2019-02-22 05:29:52 -07:00
|
|
|
alias Pleroma.User
|
2019-10-22 19:50:25 -06:00
|
|
|
alias Pleroma.Web.ActivityPub.Utils
|
2019-02-22 05:29:52 -07:00
|
|
|
|
2019-07-28 20:43:19 -06:00
|
|
|
require Pleroma.Constants
|
2019-07-23 13:15:48 -06:00
|
|
|
|
|
|
|
@spec is_public?(Object.t() | Activity.t() | map()) :: boolean()
|
2019-02-22 05:29:52 -07:00
|
|
|
def is_public?(%Object{data: %{"type" => "Tombstone"}}), do: false
|
|
|
|
def is_public?(%Object{data: data}), do: is_public?(data)
|
2019-10-30 05:21:49 -06:00
|
|
|
def is_public?(%Activity{data: %{"type" => "Move"}}), do: true
|
2019-02-22 05:29:52 -07:00
|
|
|
def is_public?(%Activity{data: data}), do: is_public?(data)
|
|
|
|
def is_public?(%{"directMessage" => true}), do: false
|
2019-10-22 19:50:25 -06:00
|
|
|
def is_public?(data), do: Utils.label_in_message?(Pleroma.Constants.as_public(), data)
|
2019-02-22 05:29:52 -07:00
|
|
|
|
|
|
|
def is_private?(activity) do
|
2019-05-08 10:08:50 -06: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 05:29:52 -07:00
|
|
|
else
|
2019-05-08 10:08:50 -06:00
|
|
|
_ -> false
|
2019-02-22 05:29:52 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-01 09:49:52 -06:00
|
|
|
def is_announceable?(activity, user, public \\ true) do
|
|
|
|
is_public?(activity) ||
|
|
|
|
(!public && is_private?(activity) && activity.data["actor"] == user.ap_id)
|
2019-02-24 10:45:29 -07:00
|
|
|
end
|
|
|
|
|
2019-02-22 05:29:52 -07: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 06:29:24 -06:00
|
|
|
def is_list?(%{data: %{"listMessage" => _}}), do: true
|
|
|
|
def is_list?(_), do: false
|
|
|
|
|
|
|
|
def visible_for_user?(%{actor: ap_id}, %User{ap_id: ap_id}), do: true
|
|
|
|
|
2019-07-15 01:54:40 -06:00
|
|
|
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 06:29:24 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def visible_for_user?(%{data: %{"listMessage" => _}}, nil), do: false
|
|
|
|
|
2019-02-22 05:29:52 -07:00
|
|
|
def visible_for_user?(activity, nil) do
|
|
|
|
is_public?(activity)
|
|
|
|
end
|
|
|
|
|
|
|
|
def visible_for_user?(activity, user) do
|
2019-10-10 13:35:32 -06:00
|
|
|
x = [user.ap_id | User.following(user)]
|
2019-02-22 05:29:52 -07:00
|
|
|
y = [activity.actor] ++ activity.data["to"] ++ (activity.data["cc"] || [])
|
|
|
|
visible_for_user?(activity, nil) || Enum.any?(x, &(&1 in y))
|
|
|
|
end
|
|
|
|
|
2019-03-24 18:38:28 -06:00
|
|
|
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 05:29:52 -07:00
|
|
|
|
2019-03-24 18:38:28 -06:00
|
|
|
result
|
2019-02-22 05:29:52 -07:00
|
|
|
end
|
2019-05-15 08:30:08 -06:00
|
|
|
|
|
|
|
def get_visibility(object) do
|
|
|
|
to = object.data["to"] || []
|
|
|
|
cc = object.data["cc"] || []
|
|
|
|
|
|
|
|
cond do
|
2019-07-28 20:43:19 -06:00
|
|
|
Pleroma.Constants.as_public() in to ->
|
2019-05-15 08:30:08 -06:00
|
|
|
"public"
|
|
|
|
|
2019-07-28 20:43:19 -06:00
|
|
|
Pleroma.Constants.as_public() in cc ->
|
2019-05-15 08:30:08 -06:00
|
|
|
"unlisted"
|
|
|
|
|
|
|
|
# this should use the sql for the object's activity
|
|
|
|
Enum.any?(to, &String.contains?(&1, "/followers")) ->
|
|
|
|
"private"
|
|
|
|
|
2019-05-31 21:23:21 -06:00
|
|
|
object.data["directMessage"] == true ->
|
|
|
|
"direct"
|
|
|
|
|
2019-07-11 06:29:24 -06:00
|
|
|
is_binary(object.data["listMessage"]) ->
|
|
|
|
"list"
|
|
|
|
|
2019-05-15 08:30:08 -06:00
|
|
|
length(cc) > 0 ->
|
|
|
|
"private"
|
|
|
|
|
|
|
|
true ->
|
|
|
|
"direct"
|
|
|
|
end
|
|
|
|
end
|
2019-02-22 05:29:52 -07:00
|
|
|
end
|