1 defmodule Pleroma.Web.ActivityPub.ActivityPub do
2 alias Pleroma.{Activity, Repo, Object, Upload, User, Notification}
4 import Pleroma.Web.ActivityPub.Utils
7 @httpoison Application.get_env(:pleroma, :httpoison)
9 def get_recipients(data) do
10 (data["to"] || []) ++ (data["cc"] || [])
13 def insert(map, local \\ true) when is_map(map) do
14 with nil <- Activity.get_by_ap_id(map["id"]),
15 map <- lazy_put_activity_defaults(map),
16 :ok <- insert_full_object(map) do
17 {:ok, activity} = Repo.insert(%Activity{data: map, local: local, actor: map["actor"], recipients: get_recipients(map)})
18 Notification.create_notifications(activity)
22 %Activity{} = activity -> {:ok, activity}
23 error -> {:error, error}
27 def stream_out(activity) do
28 if activity.data["type"] in ["Create", "Announce"] do
29 Pleroma.Web.Streamer.stream("user", activity)
30 if Enum.member?(activity.data["to"], "https://www.w3.org/ns/activitystreams#Public") do
31 Pleroma.Web.Streamer.stream("public", activity)
33 Pleroma.Web.Streamer.stream("public:local", activity)
39 def create(%{to: to, actor: actor, context: context, object: object} = params) do
40 additional = params[:additional] || %{}
41 local = !(params[:local] == false) # only accept false as false value
42 published = params[:published]
44 with create_data <- make_create_data(%{to: to, actor: actor, published: published, context: context, object: object}, additional),
45 {:ok, activity} <- insert(create_data, local),
46 :ok <- maybe_federate(activity) do
51 # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
52 def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => _}} = object, activity_id \\ nil, local \\ true) do
53 with nil <- get_existing_like(ap_id, object),
54 like_data <- make_like_data(user, object, activity_id),
55 {:ok, activity} <- insert(like_data, local),
56 {:ok, object} <- add_like_to_object(activity, object),
57 :ok <- maybe_federate(activity) do
58 {:ok, activity, object}
60 %Activity{} = activity -> {:ok, activity, object}
61 error -> {:error, error}
65 def unlike(%User{} = actor, %Object{} = object) do
66 with %Activity{} = activity <- get_existing_like(actor.ap_id, object),
67 {:ok, _activity} <- Repo.delete(activity),
68 {:ok, object} <- remove_like_from_object(activity, object) do
70 else _e -> {:ok, object}
74 def announce(%User{ap_id: _} = user, %Object{data: %{"id" => _}} = object, activity_id \\ nil, local \\ true) do
75 with announce_data <- make_announce_data(user, object, activity_id),
76 {:ok, activity} <- insert(announce_data, local),
77 {:ok, object} <- add_announce_to_object(activity, object),
78 :ok <- maybe_federate(activity) do
79 {:ok, activity, object}
81 error -> {:error, error}
85 def follow(follower, followed, activity_id \\ nil, local \\ true) do
86 with data <- make_follow_data(follower, followed, activity_id),
87 {:ok, activity} <- insert(data, local),
88 :ok <- maybe_federate(activity) do
93 def unfollow(follower, followed, local \\ true) do
94 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
95 unfollow_data <- make_unfollow_data(follower, followed, follow_activity),
96 {:ok, activity} <- insert(unfollow_data, local),
97 :ok, maybe_federate(activity) do
102 def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
103 user = User.get_cached_by_ap_id(actor)
108 "to" => [user.follower_address, "https://www.w3.org/ns/activitystreams#Public"]
110 with Repo.delete(object),
111 Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
112 {:ok, activity} <- insert(data, local),
113 :ok <- maybe_federate(activity) do
118 def fetch_activities_for_context(context, opts \\ %{}) do
119 query = from activity in Activity,
120 where: fragment("?->>'type' = ? and ?->>'context' = ?", activity.data, "Create", activity.data, ^context),
121 order_by: [desc: :id]
122 query = restrict_blocked(query, opts)
126 def fetch_public_activities(opts \\ %{}) do
127 public = ["https://www.w3.org/ns/activitystreams#Public"]
128 fetch_activities(public, opts)
131 defp restrict_since(query, %{"since_id" => since_id}) do
132 from activity in query, where: activity.id > ^since_id
134 defp restrict_since(query, _), do: query
136 defp restrict_tag(query, %{"tag" => tag}) do
137 from activity in query,
138 where: fragment("? <@ (? #> '{\"object\",\"tag\"}')", ^tag, activity.data)
140 defp restrict_tag(query, _), do: query
142 defp restrict_recipients(query, []), do: query
143 defp restrict_recipients(query, recipients) do
144 from activity in query,
145 where: fragment("? && ?", ^recipients, activity.recipients)
148 defp restrict_local(query, %{"local_only" => true}) do
149 from activity in query, where: activity.local == true
151 defp restrict_local(query, _), do: query
153 defp restrict_max(query, %{"max_id" => max_id}) do
154 from activity in query, where: activity.id < ^max_id
156 defp restrict_max(query, _), do: query
158 defp restrict_actor(query, %{"actor_id" => actor_id}) do
159 from activity in query,
160 where: activity.actor == ^actor_id
162 defp restrict_actor(query, _), do: query
164 defp restrict_type(query, %{"type" => type}) when is_binary(type) do
165 restrict_type(query, %{"type" => [type]})
167 defp restrict_type(query, %{"type" => type}) do
168 from activity in query,
169 where: fragment("?->>'type' = ANY(?)", activity.data, ^type)
171 defp restrict_type(query, _), do: query
173 defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
174 from activity in query,
175 where: fragment("? <@ (? #> '{\"object\",\"likes\"}')", ^ap_id, activity.data)
177 defp restrict_favorited_by(query, _), do: query
179 defp restrict_media(query, %{"only_media" => val}) when val == "true" or val == "1" do
180 from activity in query,
181 where: fragment("not (? #> '{\"object\",\"attachment\"}' = ?)", activity.data, ^[])
183 defp restrict_media(query, _), do: query
185 # Only search through last 100_000 activities by default
186 defp restrict_recent(query, %{"whole_db" => true}), do: query
187 defp restrict_recent(query, _) do
188 since = (Repo.aggregate(Activity, :max, :id) || 0) - 100_000
190 from activity in query,
191 where: activity.id > ^since
194 defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
195 blocks = info["blocks"] || []
196 from activity in query,
197 where: fragment("not (? = ANY(?))", activity.actor, ^blocks)
199 defp restrict_blocked(query, _), do: query
201 def fetch_activities(recipients, opts \\ %{}) do
202 base_query = from activity in Activity,
204 order_by: [fragment("? desc nulls last", activity.id)]
207 |> restrict_recipients(recipients)
208 |> restrict_tag(opts)
209 |> restrict_since(opts)
210 |> restrict_local(opts)
211 |> restrict_max(opts)
212 |> restrict_actor(opts)
213 |> restrict_type(opts)
214 |> restrict_favorited_by(opts)
215 |> restrict_recent(opts)
216 |> restrict_blocked(opts)
217 |> restrict_media(opts)
223 data = Upload.store(file)
224 Repo.insert(%Object{data: data})
227 def make_user_from_ap_id(ap_id) do
228 with {:ok, %{status_code: 200, body: body}} <- @httpoison.get(ap_id, ["Accept": "application/activity+json"]),
229 {:ok, data} <- Poison.decode(body)
234 "ap_enabled" => true,
235 "source_data" => data
237 nickname: "#{data["preferredUsername"]}@#{URI.parse(ap_id).host}",
241 User.insert_or_update_user(user_data)
245 # TODO: Extract to own module, align as close to Mastodon format as possible.
246 def sanitize_outgoing_activity_data(data) do
248 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
251 def publish(actor, activity) do
252 remote_users = Pleroma.Web.Salmon.remote_users(activity)
253 data = sanitize_outgoing_activity_data(activity.data)
254 Enum.each remote_users, fn(user) ->
255 if user.info["ap_enabled"] do
256 inbox = user.info["source_data"]["inbox"]
257 Logger.info("Federating #{activity.data["id"]} to #{inbox}")
258 host = URI.parse(inbox).host
259 signature = Pleroma.Web.HTTPSignatures.sign(actor, %{host: host})
260 @httpoison.post(inbox, Poison.encode!(data), [{"Content-Type", "application/activity+json"}, {"signature", signature}])