1 defmodule Pleroma.Web.ActivityPub.ActivityPub do
2 alias Pleroma.{Activity, Repo, Object, Upload, User, Web, Notification}
3 alias Ecto.{Changeset, UUID}
5 import Pleroma.Web.ActivityPub.Utils
8 def insert(map, local \\ true) when is_map(map) do
9 with nil <- Activity.get_by_ap_id(map["id"]),
10 map <- lazy_put_activity_defaults(map),
11 :ok <- insert_full_object(map) do
12 {:ok, activity} = Repo.insert(%Activity{data: map, local: local})
13 Notification.create_notifications(activity)
16 %Activity{} = activity -> {:ok, activity}
17 error -> {:error, error}
21 def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do
22 with create_data <- make_create_data(%{to: to, actor: actor, published: published, context: context, object: object}, additional),
23 {:ok, activity} <- insert(create_data, local),
24 :ok <- maybe_federate(activity) do
29 # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
30 def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, activity_id \\ nil, local \\ true) do
31 with nil <- get_existing_like(ap_id, object),
32 like_data <- make_like_data(user, object, activity_id),
33 {:ok, activity} <- insert(like_data, local),
34 {:ok, object} <- add_like_to_object(activity, object),
35 :ok <- maybe_federate(activity) do
36 {:ok, activity, object}
38 %Activity{} = activity -> {:ok, activity, object}
39 error -> {:error, error}
43 def unlike(%User{} = actor, %Object{} = object) do
44 with %Activity{} = activity <- get_existing_like(actor.ap_id, object),
45 {:ok, _activity} <- Repo.delete(activity),
46 {:ok, object} <- remove_like_from_object(activity, object) do
48 else _e -> {:ok, object}
52 def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, activity_id \\ nil, local \\ true) do
53 with announce_data <- make_announce_data(user, object, activity_id),
54 {:ok, activity} <- insert(announce_data, local),
55 {:ok, object} <- add_announce_to_object(activity, object),
56 :ok <- maybe_federate(activity) do
57 {:ok, activity, object}
59 error -> {:error, error}
63 def follow(follower, followed, activity_id \\ nil, local \\ true) do
64 with data <- make_follow_data(follower, followed, activity_id),
65 {:ok, activity} <- insert(data, local),
66 :ok <- maybe_federate(activity) do
71 def unfollow(follower, followed, local \\ true) do
72 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
73 unfollow_data <- make_unfollow_data(follower, followed, follow_activity),
74 {:ok, activity} <- insert(unfollow_data, local),
75 :ok, maybe_federate(activity) do
80 def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
81 user = User.get_cached_by_ap_id(actor)
86 "to" => [user.follower_address, "https://www.w3.org/ns/activitystreams#Public"]
88 with Repo.delete(object),
89 Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
90 Repo.delete_all(Activity.all_by_object_ap_id_q(id)),
91 {:ok, activity} <- insert(data, local),
92 :ok <- maybe_federate(activity) do
97 def fetch_activities_for_context(context) do
98 query = from activity in Activity,
99 where: fragment("?->>'type' = ? and ?->>'context' = ?", activity.data, "Create", activity.data, ^context),
100 order_by: [desc: :inserted_at]
104 def fetch_public_activities(opts \\ %{}) do
105 public = ["https://www.w3.org/ns/activitystreams#Public"]
106 fetch_activities(public, opts)
109 defp restrict_since(query, %{"since_id" => since_id}) do
110 from activity in query, where: activity.id > ^since_id
112 defp restrict_since(query, _), do: query
114 defp restrict_recipients(query, recipients) do
115 Enum.reduce(recipients, query, fn (recipient, q) ->
116 map = %{ to: [recipient] }
118 or_where: fragment(~s(? @> ?), activity.data, ^map)
122 defp restrict_local(query, %{"local_only" => true}) do
123 from activity in query, where: activity.local == true
125 defp restrict_local(query, _), do: query
127 defp restrict_max(query, %{"max_id" => max_id}) do
128 from activity in query, where: activity.id < ^max_id
130 defp restrict_max(query, _), do: query
132 defp restrict_actor(query, %{"actor_id" => actor_id}) do
133 from activity in query,
134 where: fragment("?->>'actor' = ?", activity.data, ^actor_id)
136 defp restrict_actor(query, _), do: query
138 defp restrict_type(query, %{"type" => type}) do
139 from activity in query,
140 where: fragment("?->>'type' = ?", activity.data, ^type)
142 defp restrict_type(query, _), do: query
144 def fetch_activities(recipients, opts \\ %{}) do
145 base_query = from activity in Activity,
147 order_by: [desc: :id]
150 |> restrict_recipients(recipients)
151 |> restrict_since(opts)
152 |> restrict_local(opts)
153 |> restrict_max(opts)
154 |> restrict_actor(opts)
155 |> restrict_type(opts)
161 data = Upload.store(file)
162 Repo.insert(%Object{data: data})