2017-03-21 09:21:52 +01:00
|
|
|
defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
2017-04-27 15:18:50 +02:00
|
|
|
alias Pleroma.{Activity, Repo, Object, Upload, User, Web}
|
|
|
|
alias Ecto.{Changeset, UUID}
|
2017-03-21 17:53:20 +01:00
|
|
|
import Ecto.Query
|
2017-03-21 09:21:52 +01:00
|
|
|
|
2017-05-02 10:47:04 +02:00
|
|
|
def insert(map, local \\ true) when is_map(map) do
|
2017-04-13 15:50:05 +02:00
|
|
|
map = map
|
|
|
|
|> Map.put_new_lazy("id", &generate_activity_id/0)
|
|
|
|
|> Map.put_new_lazy("published", &make_date/0)
|
2017-03-23 17:56:49 +01:00
|
|
|
|
2017-04-04 02:30:07 +02:00
|
|
|
map = if is_map(map["object"]) do
|
2017-03-23 21:22:49 +01:00
|
|
|
object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0)
|
2017-03-30 18:07:01 +02:00
|
|
|
Repo.insert!(%Object{data: object})
|
2017-03-23 21:22:49 +01:00
|
|
|
Map.put(map, "object", object)
|
|
|
|
else
|
|
|
|
map
|
|
|
|
end
|
|
|
|
|
2017-05-02 10:47:04 +02:00
|
|
|
Repo.insert(%Activity{data: map, local: local})
|
2017-03-21 09:21:52 +01:00
|
|
|
end
|
2017-03-21 17:53:20 +01:00
|
|
|
|
2017-05-02 10:47:04 +02:00
|
|
|
def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do
|
2017-04-24 18:46:34 +02:00
|
|
|
published = published || make_date()
|
|
|
|
|
|
|
|
activity = %{
|
|
|
|
"type" => "Create",
|
2017-05-03 16:08:24 +02:00
|
|
|
"to" => to |> Enum.uniq,
|
2017-04-24 18:46:34 +02:00
|
|
|
"actor" => actor.ap_id,
|
|
|
|
"object" => object,
|
|
|
|
"published" => published,
|
|
|
|
"context" => context
|
|
|
|
}
|
|
|
|
|> Map.merge(additional)
|
|
|
|
|
2017-05-02 10:47:04 +02:00
|
|
|
with {:ok, activity} <- insert(activity, local) do
|
2017-04-24 18:46:34 +02:00
|
|
|
if actor.local do
|
2017-04-26 18:33:10 +02:00
|
|
|
Pleroma.Web.Federator.enqueue(:publish, activity)
|
2017-04-24 18:46:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
{:ok, activity}
|
|
|
|
end
|
2017-03-21 09:21:52 +01:00
|
|
|
end
|
2017-03-21 17:53:20 +01:00
|
|
|
|
2017-05-05 16:07:44 +02:00
|
|
|
def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, local \\ true) do
|
2017-04-14 15:07:24 +02:00
|
|
|
cond do
|
|
|
|
# There's already a like here, so return the original activity.
|
|
|
|
ap_id in (object.data["likes"] || []) ->
|
|
|
|
query = from activity in Activity,
|
2017-04-14 18:08:47 +02:00
|
|
|
where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id, type: "Like"})
|
2017-04-13 15:50:05 +02:00
|
|
|
|
2017-04-14 15:07:24 +02:00
|
|
|
activity = Repo.one(query)
|
|
|
|
{:ok, activity, object}
|
|
|
|
true ->
|
|
|
|
data = %{
|
|
|
|
"type" => "Like",
|
|
|
|
"actor" => ap_id,
|
|
|
|
"object" => id,
|
2017-05-02 15:54:14 +02:00
|
|
|
"to" => [User.ap_followers(user), object.data["actor"]],
|
|
|
|
"context" => object.data["context"]
|
2017-04-14 15:07:24 +02:00
|
|
|
}
|
2017-04-13 15:50:05 +02:00
|
|
|
|
2017-05-05 16:07:44 +02:00
|
|
|
{:ok, activity} = insert(data, local)
|
2017-04-13 15:50:05 +02:00
|
|
|
|
2017-04-14 15:07:24 +02:00
|
|
|
likes = [ap_id | (object.data["likes"] || [])] |> Enum.uniq
|
|
|
|
|
|
|
|
new_data = object.data
|
|
|
|
|> Map.put("like_count", length(likes))
|
|
|
|
|> Map.put("likes", likes)
|
|
|
|
|
2017-04-27 15:18:50 +02:00
|
|
|
changeset = Changeset.change(object, data: new_data)
|
2017-04-14 15:07:24 +02:00
|
|
|
{:ok, object} = Repo.update(changeset)
|
|
|
|
|
2017-04-14 18:08:47 +02:00
|
|
|
update_object_in_activities(object)
|
|
|
|
|
2017-05-02 16:45:54 +02:00
|
|
|
if user.local do
|
|
|
|
Pleroma.Web.Federator.enqueue(:publish, activity)
|
|
|
|
end
|
|
|
|
|
2017-04-14 15:07:24 +02:00
|
|
|
{:ok, activity, object}
|
|
|
|
end
|
2017-04-13 15:50:05 +02:00
|
|
|
end
|
|
|
|
|
2017-04-14 18:08:47 +02:00
|
|
|
defp update_object_in_activities(%{data: %{"id" => id}} = object) do
|
|
|
|
# Update activities that already had this. Could be done in a seperate process.
|
|
|
|
relevant_activities = Activity.all_by_object_ap_id(id)
|
|
|
|
Enum.map(relevant_activities, fn (activity) ->
|
|
|
|
new_activity_data = activity.data |> Map.put("object", object.data)
|
2017-04-27 15:18:50 +02:00
|
|
|
changeset = Changeset.change(activity, data: new_activity_data)
|
2017-04-14 18:08:47 +02:00
|
|
|
Repo.update(changeset)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2017-04-15 13:54:46 +02:00
|
|
|
def unlike(%User{ap_id: ap_id}, %Object{data: %{ "id" => id}} = object) do
|
2017-04-14 18:08:47 +02:00
|
|
|
query = from activity in Activity,
|
|
|
|
where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id, type: "Like"})
|
|
|
|
|
|
|
|
activity = Repo.one(query)
|
|
|
|
|
|
|
|
if activity do
|
|
|
|
# just delete for now...
|
|
|
|
{:ok, _activity} = Repo.delete(activity)
|
|
|
|
|
|
|
|
likes = (object.data["likes"] || []) |> List.delete(ap_id)
|
|
|
|
|
|
|
|
new_data = object.data
|
|
|
|
|> Map.put("like_count", length(likes))
|
|
|
|
|> Map.put("likes", likes)
|
|
|
|
|
2017-04-27 15:18:50 +02:00
|
|
|
changeset = Changeset.change(object, data: new_data)
|
2017-04-14 18:08:47 +02:00
|
|
|
{:ok, object} = Repo.update(changeset)
|
|
|
|
|
|
|
|
update_object_in_activities(object)
|
|
|
|
|
|
|
|
{:ok, object}
|
|
|
|
else
|
|
|
|
{:ok, object}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-23 17:56:49 +01:00
|
|
|
def generate_activity_id do
|
2017-03-23 23:34:10 +01:00
|
|
|
generate_id("activities")
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_context_id do
|
|
|
|
generate_id("contexts")
|
2017-03-23 17:56:49 +01:00
|
|
|
end
|
|
|
|
|
2017-03-23 21:22:49 +01:00
|
|
|
def generate_object_id do
|
2017-05-03 09:54:17 +02:00
|
|
|
Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :object, Ecto.UUID.generate)
|
2017-03-23 23:34:10 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate_id(type) do
|
2017-04-27 15:18:50 +02:00
|
|
|
"#{Web.base_url()}/#{type}/#{UUID.generate}"
|
2017-03-23 21:22:49 +01:00
|
|
|
end
|
|
|
|
|
2017-03-21 20:31:48 +01:00
|
|
|
def fetch_public_activities(opts \\ %{}) do
|
2017-03-22 14:45:17 +01:00
|
|
|
public = ["https://www.w3.org/ns/activitystreams#Public"]
|
|
|
|
fetch_activities(public, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_activities(recipients, opts \\ %{}) do
|
2017-03-21 21:09:20 +01:00
|
|
|
since_id = opts["since_id"] || 0
|
2017-03-21 20:31:48 +01:00
|
|
|
|
2017-03-21 17:53:20 +01:00
|
|
|
query = from activity in Activity,
|
2017-03-21 20:22:05 +01:00
|
|
|
limit: 20,
|
|
|
|
order_by: [desc: :inserted_at]
|
2017-03-21 17:53:20 +01:00
|
|
|
|
2017-03-22 14:45:17 +01:00
|
|
|
query = Enum.reduce(recipients, query, fn (recipient, q) ->
|
|
|
|
map = %{ to: [recipient] }
|
|
|
|
from activity in q,
|
|
|
|
or_where: fragment(~s(? @> ?), activity.data, ^map)
|
|
|
|
end)
|
|
|
|
|
|
|
|
query = from activity in query,
|
|
|
|
where: activity.id > ^since_id
|
|
|
|
|
2017-05-02 14:12:43 +02:00
|
|
|
query = if opts["local_only"] do
|
|
|
|
from activity in query, where: activity.local == true
|
|
|
|
else
|
|
|
|
query
|
|
|
|
end
|
|
|
|
|
2017-04-12 17:11:01 +02:00
|
|
|
query = if opts["max_id"] do
|
|
|
|
from activity in query, where: activity.id < ^opts["max_id"]
|
|
|
|
else
|
|
|
|
query
|
|
|
|
end
|
|
|
|
|
2017-04-14 15:09:13 +02:00
|
|
|
query = if opts["actor_id"] do
|
|
|
|
from activity in query,
|
|
|
|
where: fragment("? @> ?", activity.data, ^%{actor: opts["actor_id"]})
|
|
|
|
else
|
|
|
|
query
|
|
|
|
end
|
|
|
|
|
2017-04-27 15:18:50 +02:00
|
|
|
Enum.reverse(Repo.all(query))
|
2017-03-21 17:53:20 +01:00
|
|
|
end
|
2017-03-24 00:09:08 +01:00
|
|
|
|
2017-05-04 18:42:29 +02:00
|
|
|
def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, local \\ true) do
|
2017-04-15 12:11:20 +02:00
|
|
|
data = %{
|
|
|
|
"type" => "Announce",
|
|
|
|
"actor" => ap_id,
|
|
|
|
"object" => id,
|
2017-05-02 15:54:14 +02:00
|
|
|
"to" => [User.ap_followers(user), object.data["actor"]],
|
|
|
|
"context" => object.data["context"]
|
2017-04-15 12:11:20 +02:00
|
|
|
}
|
|
|
|
|
2017-05-04 18:42:29 +02:00
|
|
|
{:ok, activity} = insert(data, local)
|
2017-04-15 12:11:20 +02:00
|
|
|
|
|
|
|
announcements = [ap_id | (object.data["announcements"] || [])] |> Enum.uniq
|
|
|
|
|
|
|
|
new_data = object.data
|
|
|
|
|> Map.put("announcement_count", length(announcements))
|
|
|
|
|> Map.put("announcements", announcements)
|
|
|
|
|
2017-04-27 15:18:50 +02:00
|
|
|
changeset = Changeset.change(object, data: new_data)
|
2017-04-15 12:11:20 +02:00
|
|
|
{:ok, object} = Repo.update(changeset)
|
|
|
|
|
|
|
|
update_object_in_activities(object)
|
|
|
|
|
2017-05-03 17:41:55 +02:00
|
|
|
if user.local do
|
|
|
|
Pleroma.Web.Federator.enqueue(:publish, activity)
|
|
|
|
end
|
|
|
|
|
2017-04-15 12:11:20 +02:00
|
|
|
{:ok, activity, object}
|
|
|
|
end
|
|
|
|
|
2017-05-07 19:28:23 +02:00
|
|
|
def follow(%User{ap_id: follower_id, local: actor_local}, %User{ap_id: followed_id}, local \\ true) do
|
|
|
|
data = %{
|
|
|
|
"type" => "Follow",
|
|
|
|
"actor" => follower_id,
|
|
|
|
"to" => [followed_id],
|
|
|
|
"object" => followed_id,
|
|
|
|
"published" => make_date()
|
|
|
|
}
|
|
|
|
|
|
|
|
with {:ok, activity} <- insert(data, local) do
|
|
|
|
if actor_local do
|
|
|
|
Pleroma.Web.Federator.enqueue(:publish, activity)
|
|
|
|
end
|
|
|
|
|
|
|
|
{:ok, activity}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def unfollow(follower, followed, local \\ true) do
|
|
|
|
with follow_activity when not is_nil(follow_activity) <- fetch_latest_follow(follower, followed) do
|
|
|
|
data = %{
|
|
|
|
"type" => "Undo",
|
|
|
|
"actor" => follower.ap_id,
|
|
|
|
"to" => [followed.ap_id],
|
|
|
|
"object" => follow_activity.data["id"],
|
|
|
|
"published" => make_date()
|
|
|
|
}
|
|
|
|
|
|
|
|
with {:ok, activity} <- insert(data, local) do
|
|
|
|
if follower.local do
|
|
|
|
Pleroma.Web.Federator.enqueue(:publish, activity)
|
|
|
|
end
|
|
|
|
|
|
|
|
{:ok, activity}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-24 00:09:08 +01:00
|
|
|
def fetch_activities_for_context(context) do
|
|
|
|
query = from activity in Activity,
|
|
|
|
where: fragment("? @> ?", activity.data, ^%{ context: context })
|
|
|
|
Repo.all(query)
|
|
|
|
end
|
2017-03-29 02:05:51 +02:00
|
|
|
|
2017-04-21 18:54:21 +02:00
|
|
|
def fetch_latest_follow(%User{ap_id: follower_id},
|
|
|
|
%User{ap_id: followed_id}) do
|
|
|
|
query = from activity in Activity,
|
|
|
|
where: fragment("? @> ?", activity.data, ^%{type: "Follow", actor: follower_id,
|
|
|
|
object: followed_id}),
|
|
|
|
order_by: [desc: :inserted_at],
|
|
|
|
limit: 1
|
|
|
|
Repo.one(query)
|
|
|
|
end
|
|
|
|
|
2017-04-16 14:23:30 +02:00
|
|
|
def upload(file) do
|
2017-03-29 02:05:51 +02:00
|
|
|
data = Upload.store(file)
|
|
|
|
Repo.insert(%Object{data: data})
|
|
|
|
end
|
2017-04-13 15:50:05 +02:00
|
|
|
|
|
|
|
defp make_date do
|
|
|
|
DateTime.utc_now() |> DateTime.to_iso8601
|
|
|
|
end
|
2017-03-21 09:21:52 +01:00
|
|
|
end
|