"""
alias Pleroma.User
alias Pleroma.Object
+ alias Pleroma.Activity
alias Pleroma.Web.ActivityPub.ActivityPub
@doc """
object
|> Map.put("actor", object["attributedTo"])
|> fix_attachments
+ |> fix_context
+ end
+
+ def fix_context(object) do
+ object
+ |> Map.put("context", object["conversation"])
end
def fix_attachments(object) do
- attachments = object["attachment"] || []
+ attachments = (object["attachment"] || [])
|> Enum.map(fn (data) ->
- url = [%{"type" => "Link", "mediaType" => data["mediaType"], "url" => data["url"]}]
+ url = [%{"type" => "Link", "mediaType" => data["mediaType"], "href" => data["url"]}]
Map.put(data, "url", url)
end)
# - tags
# - emoji
def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
- with %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
+ with nil <- Activity.get_create_activity_by_object_ap_id(object["id"]),
+ %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
object = fix_object(data["object"])
params = %{
to: data["to"],
ActivityPub.create(params)
else
+ %Activity{} = activity -> {:ok, activity}
_e -> :error
end
end
def handle_incoming(%{"type" => "Announce", "object" => object_id, "actor" => actor, "id" => id} = data) do
with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
- {:ok, object} <- ActivityPub.fetch_object_from_id(object_id),
+ {:ok, object} <- get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
{:ok, activity, object} <- ActivityPub.announce(actor, object, id, false) do
{:ok, activity}
else
def handle_incoming(_), do: :error
+ def get_obj_helper(id) do
+ if object = Object.get_by_ap_id(id), do: {:ok, object}, else: nil
+ end
+
@doc
"""
internal -> Mastodon
"""
def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
object = object
+ |> set_sensitive
+ |> add_hashtags
|> add_mention_tags
|> add_attributed_to
|> prepare_attachments
+ |> set_conversation
data = data
|> Map.put("object", object)
{:ok, data}
end
- def prepare_outgoing(%{"type" => type} = data) when type in ["Follow", "Accept", "Like", "Announce"] do
+ def prepare_outgoing(%{"type" => type} = data) do
data = data
|> Map.put("@context", "https://www.w3.org/ns/activitystreams")
{:ok, data}
end
+ def add_hashtags(object) do
+ tags = (object["tag"] || [])
+ |> Enum.map fn (tag) -> %{"href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}", "name" => "##{tag}", "type" => "Hashtag"} end
+
+ object
+ |> Map.put("tag", tags)
+ end
+
def add_mention_tags(object) do
- mentions = object["to"]
+ recipients = object["to"] ++ (object["cc"] || [])
+ mentions = recipients
|> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
|> Enum.filter(&(&1))
|> Enum.map(fn(user) -> %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"} end)
|> Map.put("tag", tags ++ mentions)
end
+ def set_conversation(object) do
+ Map.put(object, "conversation", object["context"])
+ end
+
+ def set_sensitive(object) do
+ tags = object["tag"] || []
+ Map.put(object, "sensitive", "nsfw" in tags)
+ end
+
def add_attributed_to(object) do
attributedTo = object["attributedTo"] || object["actor"]