2018-12-24 13:34:45 +01:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
2018-12-27 11:24:04 +01:00
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/activity_pub/utils.ex
|
2018-12-24 13:34:45 +01:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Service.ActivityPub.Utils do
|
2018-06-14 18:15:27 +02:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
# Various ActivityPub related utils.
|
2018-06-14 18:15:27 +02:00
|
|
|
"""
|
|
|
|
|
2019-09-13 01:35:03 +02:00
|
|
|
alias Ecto.Changeset
|
|
|
|
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.{Actors, Addresses, Events, Reports, Users}
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Addresses.Address
|
2019-09-22 09:24:18 +02:00
|
|
|
alias Mobilizon.Events.{Comment, Event}
|
2019-09-13 01:35:03 +02:00
|
|
|
alias Mobilizon.Media.Picture
|
2019-07-23 13:49:22 +02:00
|
|
|
alias Mobilizon.Reports.Report
|
2019-09-22 18:29:13 +02:00
|
|
|
alias Mobilizon.Service.ActivityPub.{Activity, Converter}
|
|
|
|
alias Mobilizon.Service.Federator
|
2019-09-13 01:35:03 +02:00
|
|
|
alias Mobilizon.Storage.Repo
|
|
|
|
|
2019-09-17 02:45:32 +02:00
|
|
|
alias MobilizonWeb.{Email, Endpoint}
|
2019-04-25 19:05:05 +02:00
|
|
|
alias MobilizonWeb.Router.Helpers, as: Routes
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2019-09-13 01:35:03 +02:00
|
|
|
require Logger
|
|
|
|
|
2019-09-04 18:24:31 +02:00
|
|
|
@actor_types ["Group", "Person", "Application"]
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
# Some implementations send the actor URI as the actor field, others send the entire actor object,
|
|
|
|
# so figure out what the actor's URI is based on what we have.
|
2019-07-30 16:40:59 +02:00
|
|
|
def get_url(%{"id" => id}), do: id
|
|
|
|
def get_url(id) when is_bitstring(id), do: id
|
|
|
|
def get_url(_), do: nil
|
2018-11-12 09:05:31 +01:00
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
def make_json_ld_header do
|
|
|
|
%{
|
|
|
|
"@context" => [
|
|
|
|
"https://www.w3.org/ns/activitystreams",
|
2018-11-12 09:05:31 +01:00
|
|
|
"https://litepub.github.io/litepub/context.jsonld",
|
2018-05-17 11:32:23 +02:00
|
|
|
%{
|
2018-11-12 09:05:31 +01:00
|
|
|
"sc" => "http://schema.org#",
|
2018-05-17 11:32:23 +02:00
|
|
|
"Hashtag" => "as:Hashtag",
|
2018-11-12 09:05:31 +01:00
|
|
|
"category" => "sc:category",
|
|
|
|
"uuid" => "sc:identifier"
|
2018-05-17 11:32:23 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_date do
|
2019-02-14 14:19:55 +01:00
|
|
|
DateTime.utc_now() |> DateTime.truncate(:second) |> DateTime.to_iso8601()
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Enqueues an activity for federation if it's local
|
|
|
|
"""
|
|
|
|
def maybe_federate(%Activity{local: true} = activity) do
|
2018-11-12 09:05:31 +01:00
|
|
|
Logger.debug("Maybe federate an activity")
|
|
|
|
|
2019-09-27 16:18:29 +02:00
|
|
|
if Mobilizon.Config.get!([:instance, :federating]) do
|
|
|
|
priority =
|
|
|
|
case activity.data["type"] do
|
|
|
|
"Delete" -> 10
|
|
|
|
"Create" -> 1
|
|
|
|
_ -> 5
|
|
|
|
end
|
|
|
|
|
|
|
|
Federator.enqueue(:publish, activity, priority)
|
|
|
|
end
|
2019-09-22 18:29:13 +02:00
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def maybe_federate(_), do: :ok
|
|
|
|
|
2018-11-12 09:05:31 +01:00
|
|
|
def remote_actors(%{data: %{"to" => to} = data}) do
|
|
|
|
to = to ++ (data["cc"] || [])
|
|
|
|
|
|
|
|
to
|
2018-11-12 18:17:53 +01:00
|
|
|
|> Enum.map(fn url -> Actors.get_actor_by_url(url) end)
|
|
|
|
|> Enum.map(fn {status, actor} ->
|
|
|
|
case status do
|
|
|
|
:ok ->
|
|
|
|
actor
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|> Enum.map(& &1)
|
2018-11-12 09:05:31 +01:00
|
|
|
|> Enum.filter(fn actor -> actor && !is_nil(actor.domain) end)
|
|
|
|
end
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
@doc """
|
|
|
|
Adds an id and a published data if they aren't there,
|
|
|
|
also adds it to an included object
|
|
|
|
"""
|
|
|
|
def lazy_put_activity_defaults(map) do
|
|
|
|
if is_map(map["object"]) do
|
2018-11-12 23:30:47 +01:00
|
|
|
object = lazy_put_object_defaults(map["object"])
|
2018-05-17 11:32:23 +02:00
|
|
|
%{map | "object" => object}
|
|
|
|
else
|
|
|
|
map
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Adds an id and published date if they aren't there.
|
|
|
|
"""
|
2018-11-12 23:30:47 +01:00
|
|
|
def lazy_put_object_defaults(map) do
|
|
|
|
Map.put_new_lazy(map, "published", &make_date/0)
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Inserts a full object if it is contained in an activity.
|
|
|
|
"""
|
2018-12-14 17:41:55 +01:00
|
|
|
def insert_full_object(object_data)
|
2018-11-12 09:05:31 +01:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Inserts a full object if it is contained in an activity.
|
|
|
|
"""
|
2019-09-04 18:24:31 +02:00
|
|
|
def insert_full_object(%{"object" => %{"type" => "Event"} = object_data, "type" => "Create"})
|
2018-12-14 17:41:55 +01:00
|
|
|
when is_map(object_data) do
|
2019-07-30 10:35:29 +02:00
|
|
|
with {:ok, object_data} <-
|
2019-09-22 18:29:13 +02:00
|
|
|
Converter.Event.as_to_model_data(object_data),
|
2019-07-23 13:49:22 +02:00
|
|
|
{:ok, %Event{} = event} <- Events.create_event(object_data) do
|
|
|
|
{:ok, event}
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-04 18:24:31 +02:00
|
|
|
def insert_full_object(%{"object" => %{"type" => "Group"} = object_data, "type" => "Create"})
|
2018-12-14 17:41:55 +01:00
|
|
|
when is_map(object_data) do
|
|
|
|
with object_data <-
|
|
|
|
Map.put(object_data, "preferred_username", object_data["preferredUsername"]),
|
2019-07-23 13:49:22 +02:00
|
|
|
{:ok, %Actor{} = group} <- Actors.create_group(object_data) do
|
|
|
|
{:ok, group}
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Inserts a full object if it is contained in an activity.
|
|
|
|
"""
|
2019-09-04 18:24:31 +02:00
|
|
|
def insert_full_object(%{"object" => %{"type" => "Note"} = object_data, "type" => "Create"})
|
2018-12-14 17:41:55 +01:00
|
|
|
when is_map(object_data) do
|
2019-09-22 18:29:13 +02:00
|
|
|
with data <- Converter.Comment.as_to_model_data(object_data),
|
2019-07-23 13:49:22 +02:00
|
|
|
{:ok, %Comment{} = comment} <- Events.create_comment(data) do
|
|
|
|
{:ok, comment}
|
2019-05-22 14:12:11 +02:00
|
|
|
else
|
|
|
|
err ->
|
|
|
|
Logger.error("Error while inserting a remote comment inside database")
|
2019-07-30 16:40:59 +02:00
|
|
|
Logger.debug(inspect(err))
|
2019-05-22 14:12:11 +02:00
|
|
|
{:error, err}
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
@doc """
|
|
|
|
Inserts a full object if it is contained in an activity.
|
|
|
|
"""
|
|
|
|
def insert_full_object(%{"type" => "Flag"} = object_data)
|
|
|
|
when is_map(object_data) do
|
2019-09-22 18:29:13 +02:00
|
|
|
with data <- Converter.Flag.as_to_model_data(object_data),
|
2019-07-23 13:49:22 +02:00
|
|
|
{:ok, %Report{} = report} <- Reports.create_report(data) do
|
|
|
|
Enum.each(Users.list_moderators(), fn moderator ->
|
|
|
|
moderator
|
2019-09-21 23:59:07 +02:00
|
|
|
|> Email.Admin.report(report)
|
2019-09-17 02:45:32 +02:00
|
|
|
|> Email.Mailer.deliver_later()
|
2019-07-23 13:49:22 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
{:ok, report}
|
|
|
|
else
|
|
|
|
err ->
|
2019-09-09 09:31:08 +02:00
|
|
|
Logger.error("Error while inserting report inside database")
|
2019-07-30 16:40:59 +02:00
|
|
|
Logger.debug(inspect(err))
|
2019-07-23 13:49:22 +02:00
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def insert_full_object(_), do: {:ok, nil}
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2019-09-04 18:24:31 +02:00
|
|
|
@doc """
|
|
|
|
Update an object
|
|
|
|
"""
|
|
|
|
@spec update_object(struct(), map()) :: {:ok, struct()} | any()
|
|
|
|
def update_object(object, object_data)
|
|
|
|
|
|
|
|
def update_object(event_url, %{
|
|
|
|
"object" => %{"type" => "Event"} = object_data,
|
|
|
|
"type" => "Update"
|
|
|
|
})
|
|
|
|
when is_map(object_data) do
|
|
|
|
with {:event_not_found, %Event{} = event} <-
|
|
|
|
{:event_not_found, Events.get_event_by_url(event_url)},
|
2019-09-22 18:29:13 +02:00
|
|
|
{:ok, object_data} <- Converter.Event.as_to_model_data(object_data),
|
2019-09-04 18:24:31 +02:00
|
|
|
{:ok, %Event{} = event} <- Events.update_event(event, object_data) do
|
|
|
|
{:ok, event}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_object(actor_url, %{
|
|
|
|
"object" => %{"type" => type_actor} = object_data,
|
|
|
|
"type" => "Update"
|
|
|
|
})
|
|
|
|
when is_map(object_data) and type_actor in @actor_types do
|
|
|
|
with {:ok, %Actor{} = actor} <- Actors.get_actor_by_url(actor_url),
|
2019-09-22 18:29:13 +02:00
|
|
|
object_data <- Converter.Actor.as_to_model_data(object_data),
|
2019-09-04 18:24:31 +02:00
|
|
|
{:ok, %Actor{} = actor} <- Actors.update_actor(actor, object_data) do
|
|
|
|
{:ok, actor}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_object(_, _), do: {:ok, nil}
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
#### Like-related helpers
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
# @doc """
|
|
|
|
# Returns an existing like if a user already liked an object
|
|
|
|
# """
|
|
|
|
# def get_existing_like(actor, %{data: %{"id" => id}}) do
|
|
|
|
# query =
|
|
|
|
# from(
|
|
|
|
# activity in Activity,
|
|
|
|
# where: fragment("(?)->>'actor' = ?", activity.data, ^actor),
|
|
|
|
# # this is to use the index
|
|
|
|
# where:
|
|
|
|
# fragment(
|
|
|
|
# "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
|
|
|
|
# activity.data,
|
|
|
|
# activity.data,
|
|
|
|
# ^id
|
|
|
|
# ),
|
|
|
|
# where: fragment("(?)->>'type' = 'Like'", activity.data)
|
|
|
|
# )
|
|
|
|
#
|
|
|
|
# Repo.one(query)
|
|
|
|
# end
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2019-05-31 17:58:03 +02:00
|
|
|
@doc """
|
|
|
|
Save picture data from %Plug.Upload{} and return AS Link data.
|
|
|
|
"""
|
2019-05-22 14:12:11 +02:00
|
|
|
def make_picture_data(%Plug.Upload{} = picture) do
|
2019-07-23 18:06:22 +02:00
|
|
|
case MobilizonWeb.Upload.store(picture) do
|
|
|
|
{:ok, picture} ->
|
|
|
|
picture
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
nil
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-31 17:58:03 +02:00
|
|
|
@doc """
|
2019-09-22 18:29:13 +02:00
|
|
|
Convert a picture model into an AS Link representation.
|
2019-05-31 17:58:03 +02:00
|
|
|
"""
|
2019-09-22 18:29:13 +02:00
|
|
|
def make_picture_data(%Picture{} = picture) do
|
|
|
|
Converter.Picture.model_to_as(picture)
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
|
2019-05-31 17:58:03 +02:00
|
|
|
@doc """
|
|
|
|
Save picture data from raw data and return AS Link data.
|
|
|
|
"""
|
2019-07-04 17:41:06 +02:00
|
|
|
def make_picture_data(picture) when is_map(picture) do
|
2019-06-03 17:13:47 +02:00
|
|
|
with {:ok, %{"url" => [%{"href" => url, "mediaType" => content_type}], "size" => size}} <-
|
|
|
|
MobilizonWeb.Upload.store(picture.file),
|
2019-10-14 11:41:57 +02:00
|
|
|
{:picture_exists, nil} <- {:picture_exists, Mobilizon.Media.get_picture_by_url(url)},
|
2019-09-22 18:29:13 +02:00
|
|
|
{:ok, %Picture{file: _file} = picture} <-
|
2019-05-22 14:12:11 +02:00
|
|
|
Mobilizon.Media.create_picture(%{
|
|
|
|
"file" => %{
|
|
|
|
"url" => url,
|
2019-06-03 17:13:47 +02:00
|
|
|
"name" => picture.name,
|
|
|
|
"content_type" => content_type,
|
|
|
|
"size" => size
|
2019-05-31 17:58:03 +02:00
|
|
|
},
|
|
|
|
"actor_id" => picture.actor_id
|
2019-05-22 14:12:11 +02:00
|
|
|
}) do
|
2019-09-22 18:29:13 +02:00
|
|
|
Converter.Picture.model_to_as(picture)
|
2019-10-14 11:41:57 +02:00
|
|
|
else
|
|
|
|
{:picture_exists, %Picture{file: _file} = picture} ->
|
|
|
|
Converter.Picture.model_to_as(picture)
|
|
|
|
|
|
|
|
err ->
|
|
|
|
err
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_picture_data(nil), do: nil
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
|
|
|
Make an AP event object from an set of values
|
|
|
|
"""
|
2019-02-22 16:11:57 +01:00
|
|
|
@spec make_event_data(
|
|
|
|
String.t(),
|
2019-07-30 10:35:29 +02:00
|
|
|
map(),
|
2019-02-22 16:11:57 +01:00
|
|
|
String.t(),
|
|
|
|
String.t(),
|
2019-05-22 14:12:11 +02:00
|
|
|
map(),
|
2019-02-22 16:11:57 +01:00
|
|
|
list(),
|
2019-09-04 18:24:31 +02:00
|
|
|
map(),
|
|
|
|
String.t()
|
2019-02-22 16:11:57 +01:00
|
|
|
) :: map()
|
2018-12-14 17:41:55 +01:00
|
|
|
def make_event_data(
|
|
|
|
actor,
|
2019-07-30 10:35:29 +02:00
|
|
|
%{to: to, cc: cc} = _audience,
|
2018-12-14 17:41:55 +01:00
|
|
|
title,
|
|
|
|
content_html,
|
2019-05-22 14:12:11 +02:00
|
|
|
picture \\ nil,
|
2018-12-14 17:41:55 +01:00
|
|
|
tags \\ [],
|
2019-09-04 18:24:31 +02:00
|
|
|
metadata \\ %{},
|
|
|
|
uuid \\ nil,
|
|
|
|
url \\ nil
|
2018-12-14 17:41:55 +01:00
|
|
|
) do
|
|
|
|
Logger.debug("Making event data")
|
2019-09-04 18:24:31 +02:00
|
|
|
uuid = uuid || Ecto.UUID.generate()
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
res = %{
|
2018-12-14 17:41:55 +01:00
|
|
|
"type" => "Event",
|
|
|
|
"to" => to,
|
2019-07-30 10:35:29 +02:00
|
|
|
"cc" => cc || [],
|
2018-12-14 17:41:55 +01:00
|
|
|
"content" => content_html,
|
|
|
|
"name" => title,
|
2019-07-30 10:35:29 +02:00
|
|
|
"startTime" => metadata.begins_on,
|
2019-09-04 18:24:31 +02:00
|
|
|
"endTime" => metadata.ends_on,
|
2019-07-30 10:35:29 +02:00
|
|
|
"category" => metadata.category,
|
2018-12-14 17:41:55 +01:00
|
|
|
"actor" => actor,
|
2019-09-04 18:24:31 +02:00
|
|
|
"id" => url || Routes.page_url(Endpoint, :event, uuid),
|
2019-09-20 18:22:03 +02:00
|
|
|
"joinOptions" => metadata.join_options,
|
2019-09-23 10:26:23 +02:00
|
|
|
"status" => metadata.status,
|
|
|
|
"onlineAddress" => metadata.online_address,
|
|
|
|
"phoneAddress" => metadata.phone_address,
|
2019-10-02 17:59:07 +02:00
|
|
|
"draft" => metadata.draft,
|
2018-12-14 17:41:55 +01:00
|
|
|
"uuid" => uuid,
|
2019-07-30 10:35:29 +02:00
|
|
|
"tag" =>
|
|
|
|
tags |> Enum.uniq() |> Enum.map(fn tag -> %{"type" => "Hashtag", "name" => "##{tag}"} end)
|
2018-12-14 17:41:55 +01:00
|
|
|
}
|
|
|
|
|
2019-07-30 10:35:29 +02:00
|
|
|
res =
|
|
|
|
if is_nil(metadata.physical_address),
|
|
|
|
do: res,
|
|
|
|
else: Map.put(res, "location", make_address_data(metadata.physical_address))
|
|
|
|
|
2019-08-28 11:28:27 +02:00
|
|
|
res =
|
|
|
|
if is_nil(picture), do: res, else: Map.put(res, "attachment", [make_picture_data(picture)])
|
|
|
|
|
|
|
|
if is_nil(metadata.options) do
|
|
|
|
res
|
|
|
|
else
|
2019-09-22 13:41:24 +02:00
|
|
|
options = Events.EventOptions |> struct(metadata.options) |> Map.from_struct()
|
2019-08-28 11:28:27 +02:00
|
|
|
|
|
|
|
Enum.reduce(options, res, fn {key, value}, acc ->
|
2019-10-14 19:29:18 +02:00
|
|
|
(!is_nil(value) && Map.put(acc, camelize(key), value)) ||
|
2019-08-28 11:28:27 +02:00
|
|
|
acc
|
|
|
|
end)
|
|
|
|
end
|
2018-11-12 18:17:53 +01:00
|
|
|
end
|
|
|
|
|
2019-07-30 10:35:29 +02:00
|
|
|
def make_address_data(%Address{} = address) do
|
2019-08-22 15:57:44 +02:00
|
|
|
# res = %{
|
|
|
|
# "type" => "Place",
|
|
|
|
# "name" => address.description,
|
|
|
|
# "id" => address.url,
|
|
|
|
# "address" => %{
|
|
|
|
# "type" => "PostalAddress",
|
|
|
|
# "streetAddress" => address.street,
|
|
|
|
# "postalCode" => address.postal_code,
|
|
|
|
# "addressLocality" => address.locality,
|
|
|
|
# "addressRegion" => address.region,
|
|
|
|
# "addressCountry" => address.country
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# if is_nil(address.geom) do
|
|
|
|
# res
|
|
|
|
# else
|
|
|
|
# Map.put(res, "geo", %{
|
|
|
|
# "type" => "GeoCoordinates",
|
|
|
|
# "latitude" => address.geom.coordinates |> elem(0),
|
|
|
|
# "longitude" => address.geom.coordinates |> elem(1)
|
|
|
|
# })
|
|
|
|
# end
|
|
|
|
address.url
|
2019-07-30 10:35:29 +02:00
|
|
|
end
|
|
|
|
|
2019-08-22 15:57:44 +02:00
|
|
|
def make_address_data(address) when is_map(address) do
|
2019-07-30 10:35:29 +02:00
|
|
|
Address
|
|
|
|
|> struct(address)
|
|
|
|
|> make_address_data()
|
|
|
|
end
|
|
|
|
|
2019-08-22 15:57:44 +02:00
|
|
|
def make_address_data(address_url) when is_bitstring(address_url) do
|
|
|
|
with %Address{} = address <- Addresses.get_address_by_url(address_url) do
|
|
|
|
address.url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
|
|
|
Make an AP comment object from an set of values
|
|
|
|
"""
|
2018-11-07 19:14:59 +01:00
|
|
|
def make_comment_data(
|
|
|
|
actor,
|
|
|
|
to,
|
|
|
|
content_html,
|
2018-11-12 09:05:31 +01:00
|
|
|
# attachments,
|
|
|
|
inReplyTo \\ nil,
|
2018-12-14 17:41:55 +01:00
|
|
|
tags \\ [],
|
|
|
|
# _cw \\ nil,
|
2018-11-07 19:14:59 +01:00
|
|
|
cc \\ []
|
|
|
|
) do
|
2018-11-12 09:05:31 +01:00
|
|
|
Logger.debug("Making comment data")
|
|
|
|
uuid = Ecto.UUID.generate()
|
|
|
|
|
2018-11-07 19:14:59 +01:00
|
|
|
object = %{
|
|
|
|
"type" => "Note",
|
|
|
|
"to" => to,
|
2018-11-12 23:30:47 +01:00
|
|
|
"cc" => cc,
|
2018-11-07 19:14:59 +01:00
|
|
|
"content" => content_html,
|
2018-11-12 09:05:31 +01:00
|
|
|
# "summary" => cw,
|
|
|
|
# "attachment" => attachments,
|
2018-11-07 19:14:59 +01:00
|
|
|
"actor" => actor,
|
2019-04-25 19:05:05 +02:00
|
|
|
"id" => Routes.page_url(Endpoint, :comment, uuid),
|
2018-12-14 17:41:55 +01:00
|
|
|
"uuid" => uuid,
|
2019-07-26 11:30:28 +02:00
|
|
|
"tag" => tags |> Enum.uniq()
|
2018-11-07 19:14:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if inReplyTo do
|
|
|
|
object
|
2018-11-12 09:05:31 +01:00
|
|
|
|> Map.put("inReplyTo", inReplyTo)
|
2018-11-07 19:14:59 +01:00
|
|
|
else
|
|
|
|
object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
def make_group_data(
|
|
|
|
actor,
|
|
|
|
to,
|
|
|
|
preferred_username,
|
|
|
|
content_html,
|
|
|
|
# attachments,
|
|
|
|
tags \\ [],
|
|
|
|
# _cw \\ nil,
|
|
|
|
cc \\ []
|
|
|
|
) do
|
|
|
|
uuid = Ecto.UUID.generate()
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
%{
|
|
|
|
"type" => "Group",
|
|
|
|
"to" => to,
|
|
|
|
"cc" => cc,
|
|
|
|
"summary" => content_html,
|
|
|
|
"attributedTo" => actor,
|
|
|
|
"preferredUsername" => preferred_username,
|
2019-04-25 19:05:05 +02:00
|
|
|
"id" => Actor.build_url(preferred_username, :page),
|
2018-12-14 17:41:55 +01:00
|
|
|
"uuid" => uuid,
|
|
|
|
"tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
|
|
|
|
}
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
#### Like-related helpers
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns an existing like if a user already liked an object
|
|
|
|
"""
|
|
|
|
# @spec get_existing_like(Actor.t, map()) :: nil
|
|
|
|
# def get_existing_like(%Actor{url: url} = actor, %{data: %{"id" => id}}) do
|
|
|
|
# nil
|
|
|
|
# end
|
|
|
|
|
|
|
|
# def make_like_data(%Actor{url: url} = actor, %{data: %{"id" => id}} = object, activity_id) do
|
|
|
|
# data = %{
|
|
|
|
# "type" => "Like",
|
|
|
|
# "actor" => url,
|
|
|
|
# "object" => id,
|
|
|
|
# "to" => [actor.followers_url, object.data["actor"]],
|
|
|
|
# "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
# "context" => object.data["context"]
|
|
|
|
# }
|
|
|
|
|
|
|
|
# if activity_id, do: Map.put(data, "id", activity_id), else: data
|
|
|
|
# end
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
def update_element_in_object(property, element, object) do
|
|
|
|
with new_data <-
|
|
|
|
object.data
|
|
|
|
|> Map.put("#{property}_count", length(element))
|
|
|
|
|> Map.put("#{property}s", element),
|
|
|
|
changeset <- Changeset.change(object, data: new_data),
|
|
|
|
{:ok, object} <- Repo.update(changeset) do
|
|
|
|
{:ok, object}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
# def update_likes_in_object(likes, object) do
|
|
|
|
# update_element_in_object("like", likes, object)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def add_like_to_object(%Activity{data: %{"actor" => actor}}, object) do
|
|
|
|
# with likes <- [actor | object.data["likes"] || []] |> Enum.uniq() do
|
|
|
|
# update_likes_in_object(likes, object)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def remove_like_from_object(%Activity{data: %{"actor" => actor}}, object) do
|
|
|
|
# with likes <- (object.data["likes"] || []) |> List.delete(actor) do
|
|
|
|
# update_likes_in_object(likes, object)
|
|
|
|
# end
|
|
|
|
# end
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
#### Follow-related helpers
|
|
|
|
|
|
|
|
@doc """
|
2018-12-14 17:41:55 +01:00
|
|
|
Makes a follow activity data for the given followed and follower
|
2018-05-17 11:32:23 +02:00
|
|
|
"""
|
2018-12-14 17:41:55 +01:00
|
|
|
def make_follow_data(%Actor{url: followed_id}, %Actor{url: follower_id}, activity_id) do
|
2018-11-12 09:05:31 +01:00
|
|
|
Logger.debug("Make follow data")
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
data = %{
|
|
|
|
"type" => "Follow",
|
|
|
|
"actor" => follower_id,
|
|
|
|
"to" => [followed_id],
|
|
|
|
"cc" => ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"object" => followed_id
|
|
|
|
}
|
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
data =
|
|
|
|
if activity_id,
|
|
|
|
do: Map.put(data, "id", activity_id),
|
|
|
|
else: data
|
|
|
|
|
2018-11-12 09:05:31 +01:00
|
|
|
Logger.debug(inspect(data))
|
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
data
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
#### Announce-related helpers
|
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
require Logger
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
@doc """
|
|
|
|
Make announce activity data for the given actor and object
|
|
|
|
"""
|
2019-07-30 16:40:59 +02:00
|
|
|
def make_announce_data(actor, object, activity_id, public \\ true)
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
def make_announce_data(
|
2019-07-30 16:40:59 +02:00
|
|
|
%Actor{url: actor_url, followers_url: actor_followers_url} = _actor,
|
|
|
|
%{"id" => url, "type" => type} = _object,
|
|
|
|
activity_id,
|
|
|
|
public
|
|
|
|
)
|
2019-09-04 18:24:31 +02:00
|
|
|
when type in @actor_types do
|
2019-07-30 16:40:59 +02:00
|
|
|
do_make_announce_data(actor_url, actor_followers_url, url, url, activity_id, public)
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_announce_data(
|
|
|
|
%Actor{url: actor_url, followers_url: actor_followers_url} = _actor,
|
|
|
|
%{"id" => url, "type" => type, "actor" => object_actor_url} = _object,
|
|
|
|
activity_id,
|
|
|
|
public
|
|
|
|
)
|
|
|
|
when type in ["Note", "Event"] do
|
|
|
|
do_make_announce_data(
|
|
|
|
actor_url,
|
|
|
|
actor_followers_url,
|
|
|
|
object_actor_url,
|
|
|
|
url,
|
|
|
|
activity_id,
|
|
|
|
public
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_make_announce_data(
|
|
|
|
actor_url,
|
|
|
|
actor_followers_url,
|
|
|
|
object_actor_url,
|
|
|
|
object_url,
|
|
|
|
activity_id,
|
2019-08-14 17:45:11 +02:00
|
|
|
public
|
2019-07-30 16:40:59 +02:00
|
|
|
) do
|
|
|
|
{to, cc} =
|
|
|
|
if public do
|
|
|
|
{[actor_followers_url, object_actor_url],
|
|
|
|
["https://www.w3.org/ns/activitystreams#Public"]}
|
|
|
|
else
|
|
|
|
{[actor_followers_url], []}
|
|
|
|
end
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
data = %{
|
|
|
|
"type" => "Announce",
|
2018-12-14 17:41:55 +01:00
|
|
|
"actor" => actor_url,
|
2019-07-30 16:40:59 +02:00
|
|
|
"object" => object_url,
|
|
|
|
"to" => to,
|
|
|
|
"cc" => cc
|
2018-12-14 17:41:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if activity_id, do: Map.put(data, "id", activity_id), else: data
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-07-30 16:40:59 +02:00
|
|
|
Make unannounce activity data for the given actor and object
|
2018-12-14 17:41:55 +01:00
|
|
|
"""
|
2019-07-30 16:40:59 +02:00
|
|
|
def make_unannounce_data(
|
|
|
|
%Actor{url: url} = actor,
|
|
|
|
activity,
|
2018-12-14 17:41:55 +01:00
|
|
|
activity_id
|
|
|
|
) do
|
|
|
|
data = %{
|
2019-07-30 16:40:59 +02:00
|
|
|
"type" => "Undo",
|
|
|
|
"actor" => url,
|
|
|
|
"object" => activity,
|
|
|
|
"to" => [actor.followers_url, actor.url],
|
2018-12-14 17:41:55 +01:00
|
|
|
"cc" => ["https://www.w3.org/ns/activitystreams#Public"]
|
2018-05-17 11:32:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if activity_id, do: Map.put(data, "id", activity_id), else: data
|
|
|
|
end
|
|
|
|
|
|
|
|
#### Unfollow-related helpers
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@spec make_unfollow_data(Actor.t(), Actor.t(), map(), String.t()) :: map()
|
|
|
|
def make_unfollow_data(
|
|
|
|
%Actor{url: follower_url},
|
|
|
|
%Actor{url: followed_url},
|
|
|
|
follow_activity,
|
|
|
|
activity_id
|
|
|
|
) do
|
|
|
|
data = %{
|
2018-05-17 11:32:23 +02:00
|
|
|
"type" => "Undo",
|
2018-12-14 17:41:55 +01:00
|
|
|
"actor" => follower_url,
|
|
|
|
"to" => [followed_url],
|
|
|
|
"object" => follow_activity.data
|
2018-05-17 11:32:23 +02:00
|
|
|
}
|
2018-12-14 17:41:55 +01:00
|
|
|
|
|
|
|
if activity_id, do: Map.put(data, "id", activity_id), else: data
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
#### Create-related helpers
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
|
|
|
Make create activity data
|
|
|
|
"""
|
|
|
|
@spec make_create_data(map(), map()) :: map()
|
2018-11-12 18:17:53 +01:00
|
|
|
def make_create_data(params, additional \\ %{}) do
|
2018-12-14 17:41:55 +01:00
|
|
|
Logger.debug("Making create data")
|
|
|
|
Logger.debug(inspect(params))
|
2018-05-17 11:32:23 +02:00
|
|
|
published = params.published || make_date()
|
|
|
|
|
|
|
|
%{
|
|
|
|
"type" => "Create",
|
|
|
|
"to" => params.to |> Enum.uniq(),
|
|
|
|
"actor" => params.actor.url,
|
|
|
|
"object" => params.object,
|
2019-07-30 16:40:59 +02:00
|
|
|
"published" => published,
|
|
|
|
"id" => params.object["id"] <> "/activity"
|
2018-05-17 11:32:23 +02:00
|
|
|
}
|
|
|
|
|> Map.merge(additional)
|
|
|
|
end
|
2018-06-14 17:25:55 +02:00
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
#### Flag-related helpers
|
|
|
|
@spec make_flag_data(map(), map()) :: map()
|
|
|
|
def make_flag_data(params, additional) do
|
|
|
|
object = [params.reported_actor_url] ++ params.comments_url
|
|
|
|
|
|
|
|
object = if params[:event_url], do: object ++ [params.event_url], else: object
|
|
|
|
|
|
|
|
%{
|
|
|
|
"type" => "Flag",
|
|
|
|
"id" => "#{MobilizonWeb.Endpoint.url()}/report/#{Ecto.UUID.generate()}",
|
|
|
|
"actor" => params.reporter_url,
|
|
|
|
"content" => params.content,
|
|
|
|
"object" => object,
|
|
|
|
"state" => "open"
|
|
|
|
}
|
|
|
|
|> Map.merge(additional)
|
|
|
|
end
|
|
|
|
|
2019-08-14 17:45:11 +02:00
|
|
|
def make_join_data(%Event{} = event, %Actor{} = actor) do
|
|
|
|
%{
|
|
|
|
"type" => "Join",
|
|
|
|
"id" => "#{actor.url}/join/event/id",
|
|
|
|
"actor" => actor.url,
|
|
|
|
"object" => event.url
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_join_data(%Actor{type: :Group} = event, %Actor{} = actor) do
|
|
|
|
%{
|
|
|
|
"type" => "Join",
|
|
|
|
"id" => "#{actor.url}/join/group/id",
|
|
|
|
"actor" => actor.url,
|
|
|
|
"object" => event.url
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
@doc """
|
|
|
|
Converts PEM encoded keys to a public key representation
|
|
|
|
"""
|
|
|
|
def pem_to_public_key(pem) do
|
2018-08-03 10:16:22 +02:00
|
|
|
[key_code] = :public_key.pem_decode(pem)
|
|
|
|
key = :public_key.pem_entry_decode(key_code)
|
2018-08-03 10:19:28 +02:00
|
|
|
|
2018-08-03 10:16:22 +02:00
|
|
|
case key do
|
|
|
|
{:RSAPrivateKey, _, modulus, exponent, _, _, _, _, _, _, _} ->
|
|
|
|
{:RSAPublicKey, modulus, exponent}
|
2018-08-03 10:19:28 +02:00
|
|
|
|
|
|
|
{:RSAPublicKey, modulus, exponent} ->
|
|
|
|
{:RSAPublicKey, modulus, exponent}
|
2018-08-03 10:16:22 +02:00
|
|
|
end
|
2018-06-14 17:25:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Converts PEM encoded keys to a private key representation
|
|
|
|
"""
|
|
|
|
def pem_to_private_key(pem) do
|
|
|
|
[private_key_code] = :public_key.pem_decode(pem)
|
|
|
|
:public_key.pem_entry_decode(private_key_code)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Converts PEM encoded keys to a PEM public key representation
|
|
|
|
"""
|
|
|
|
def pem_to_public_key_pem(pem) do
|
|
|
|
public_key = pem_to_public_key(pem)
|
|
|
|
public_key = :public_key.pem_entry_encode(:RSAPublicKey, public_key)
|
|
|
|
:public_key.pem_encode([public_key])
|
|
|
|
end
|
2019-08-28 11:28:27 +02:00
|
|
|
|
|
|
|
def camelize(word) when is_atom(word) do
|
|
|
|
camelize(to_string(word))
|
|
|
|
end
|
|
|
|
|
|
|
|
def camelize(word) when is_bitstring(word) do
|
|
|
|
{first, rest} = String.split_at(Macro.camelize(word), 1)
|
|
|
|
String.downcase(first) <> rest
|
|
|
|
end
|
|
|
|
|
|
|
|
def underscore(word) when is_atom(word) do
|
|
|
|
underscore(to_string(word))
|
|
|
|
end
|
|
|
|
|
|
|
|
def underscore(word) when is_bitstring(word) do
|
|
|
|
Macro.underscore(word)
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|