1 defmodule Pleroma.Web.ActivityPub.ActivityPub do
2 alias Pleroma.{Activity, Repo, Object, Upload, User, Notification}
3 alias Pleroma.Web.ActivityPub.Transmogrifier
4 alias Pleroma.Web.WebFinger
6 import Pleroma.Web.ActivityPub.Utils
9 @httpoison Application.get_env(:pleroma, :httpoison)
11 def get_recipients(data) do
12 (data["to"] || []) ++ (data["cc"] || [])
15 def insert(map, local \\ true) when is_map(map) do
16 with nil <- Activity.get_by_ap_id(map["id"]),
17 map <- lazy_put_activity_defaults(map),
18 :ok <- insert_full_object(map) do
19 {:ok, activity} = Repo.insert(%Activity{data: map, local: local, actor: map["actor"], recipients: get_recipients(map)})
20 Notification.create_notifications(activity)
24 %Activity{} = activity -> {:ok, activity}
25 error -> {:error, error}
29 def stream_out(activity) do
30 if activity.data["type"] in ["Create", "Announce"] do
31 Pleroma.Web.Streamer.stream("user", activity)
32 if Enum.member?(activity.data["to"], "https://www.w3.org/ns/activitystreams#Public") do
33 Pleroma.Web.Streamer.stream("public", activity)
35 Pleroma.Web.Streamer.stream("public:local", activity)
41 def create(%{to: to, actor: actor, context: context, object: object} = params) do
42 additional = params[:additional] || %{}
43 local = !(params[:local] == false) # only accept false as false value
44 published = params[:published]
46 with create_data <- make_create_data(%{to: to, actor: actor, published: published, context: context, object: object}, additional),
47 {:ok, activity} <- insert(create_data, local),
48 :ok <- maybe_federate(activity) do
53 def accept(%{to: to, actor: actor, object: object} = params) do
54 local = !(params[:local] == false) # only accept false as false value
56 with data <- %{"to" => to, "type" => "Accept", "actor" => actor, "object" => object},
57 {:ok, activity} <- insert(data, local),
58 :ok <- maybe_federate(activity) do
63 # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
64 def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => _}} = object, activity_id \\ nil, local \\ true) do
65 with nil <- get_existing_like(ap_id, object),
66 like_data <- make_like_data(user, object, activity_id),
67 {:ok, activity} <- insert(like_data, local),
68 {:ok, object} <- add_like_to_object(activity, object),
69 :ok <- maybe_federate(activity) do
70 {:ok, activity, object}
72 %Activity{} = activity -> {:ok, activity, object}
73 error -> {:error, error}
77 def unlike(%User{} = actor, %Object{} = object) do
78 with %Activity{} = activity <- get_existing_like(actor.ap_id, object),
79 {:ok, _activity} <- Repo.delete(activity),
80 {:ok, object} <- remove_like_from_object(activity, object) do
82 else _e -> {:ok, object}
86 def announce(%User{ap_id: _} = user, %Object{data: %{"id" => _}} = object, activity_id \\ nil, local \\ true) do
87 with true <- is_public?(object),
88 announce_data <- make_announce_data(user, object, activity_id),
89 {:ok, activity} <- insert(announce_data, local),
90 {:ok, object} <- add_announce_to_object(activity, object),
91 :ok <- maybe_federate(activity) do
92 {:ok, activity, object}
94 error -> {:error, error}
98 def follow(follower, followed, activity_id \\ nil, local \\ true) do
99 with data <- make_follow_data(follower, followed, activity_id),
100 {:ok, activity} <- insert(data, local),
101 :ok <- maybe_federate(activity) do
106 def unfollow(follower, followed, local \\ true) do
107 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
108 unfollow_data <- make_unfollow_data(follower, followed, follow_activity),
109 {:ok, activity} <- insert(unfollow_data, local),
110 :ok, maybe_federate(activity) do
115 def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
116 user = User.get_cached_by_ap_id(actor)
121 "to" => [user.follower_address, "https://www.w3.org/ns/activitystreams#Public"]
123 with Repo.delete(object),
124 Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
125 {:ok, activity} <- insert(data, local),
126 :ok <- maybe_federate(activity) do
131 def fetch_activities_for_context(context, opts \\ %{}) do
132 public = ["https://www.w3.org/ns/activitystreams#Public"]
133 recipients = if opts["user"], do: [opts["user"].ap_id | opts["user"].following] ++ public, else: public
135 query = from activity in Activity
137 |> restrict_blocked(opts)
138 |> restrict_recipients(recipients, opts["user"])
140 query = from activity in query,
141 where: fragment("?->>'type' = ? and ?->>'context' = ?", activity.data, "Create", activity.data, ^context),
142 order_by: [desc: :id]
146 def fetch_public_activities(opts \\ %{}) do
147 public = %{to: ["https://www.w3.org/ns/activitystreams#Public"]}
148 q = fetch_activities_query([], opts)
149 q = from activity in q,
150 where: fragment(~s(? @> ?), activity.data, ^public)
156 defp restrict_since(query, %{"since_id" => since_id}) do
157 from activity in query, where: activity.id > ^since_id
159 defp restrict_since(query, _), do: query
161 defp restrict_tag(query, %{"tag" => tag}) do
162 from activity in query,
163 where: fragment("? <@ (? #> '{\"object\",\"tag\"}')", ^tag, activity.data)
165 defp restrict_tag(query, _), do: query
167 defp restrict_recipients(query, [], user), do: query
168 defp restrict_recipients(query, recipients, nil) do
169 from activity in query,
170 where: fragment("? && ?", ^recipients, activity.recipients)
172 defp restrict_recipients(query, recipients, user) do
173 from activity in query,
174 where: fragment("? && ?", ^recipients, activity.recipients),
175 or_where: activity.actor == ^user.ap_id
178 defp restrict_local(query, %{"local_only" => true}) do
179 from activity in query, where: activity.local == true
181 defp restrict_local(query, _), do: query
183 defp restrict_max(query, %{"max_id" => max_id}) do
184 from activity in query, where: activity.id < ^max_id
186 defp restrict_max(query, _), do: query
188 defp restrict_actor(query, %{"actor_id" => actor_id}) do
189 from activity in query,
190 where: activity.actor == ^actor_id
192 defp restrict_actor(query, _), do: query
194 defp restrict_type(query, %{"type" => type}) when is_binary(type) do
195 restrict_type(query, %{"type" => [type]})
197 defp restrict_type(query, %{"type" => type}) do
198 from activity in query,
199 where: fragment("?->>'type' = ANY(?)", activity.data, ^type)
201 defp restrict_type(query, _), do: query
203 defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
204 from activity in query,
205 where: fragment("? <@ (? #> '{\"object\",\"likes\"}')", ^ap_id, activity.data)
207 defp restrict_favorited_by(query, _), do: query
209 defp restrict_media(query, %{"only_media" => val}) when val == "true" or val == "1" do
210 from activity in query,
211 where: fragment("not (? #> '{\"object\",\"attachment\"}' = ?)", activity.data, ^[])
213 defp restrict_media(query, _), do: query
215 # Only search through last 100_000 activities by default
216 defp restrict_recent(query, %{"whole_db" => true}), do: query
217 defp restrict_recent(query, _) do
218 since = (Repo.aggregate(Activity, :max, :id) || 0) - 100_000
220 from activity in query,
221 where: activity.id > ^since
224 defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
225 blocks = info["blocks"] || []
226 from activity in query,
227 where: fragment("not (? = ANY(?))", activity.actor, ^blocks)
229 defp restrict_blocked(query, _), do: query
231 def fetch_activities_query(recipients, opts \\ %{}) do
232 base_query = from activity in Activity,
234 order_by: [fragment("? desc nulls last", activity.id)]
237 |> restrict_recipients(recipients, opts["user"])
238 |> restrict_tag(opts)
239 |> restrict_since(opts)
240 |> restrict_local(opts)
241 |> restrict_max(opts)
242 |> restrict_actor(opts)
243 |> restrict_type(opts)
244 |> restrict_favorited_by(opts)
245 |> restrict_recent(opts)
246 |> restrict_blocked(opts)
247 |> restrict_media(opts)
250 def fetch_activities(recipients, opts \\ %{}) do
251 fetch_activities_query(recipients, opts)
257 data = Upload.store(file)
258 Repo.insert(%Object{data: data})
261 def make_user_from_ap_id(ap_id) do
262 with {:ok, %{status_code: 200, body: body}} <- @httpoison.get(ap_id, ["Accept": "application/activity+json"]),
263 {:ok, data} <- Poison.decode(body)
268 "ap_enabled" => true,
269 "source_data" => data
271 nickname: "#{data["preferredUsername"]}@#{URI.parse(ap_id).host}",
275 User.insert_or_update_user(user_data)
276 if user = User.get_by_ap_id(ap_id) do
277 User.info_changeset(user, user_data}
280 User.insert_or_update_user(user_data)
285 def make_user_from_nickname(nickname) do
286 with {:ok, %{"ap_id" => ap_id}} when not is_nil(ap_id) <- WebFinger.finger(nickname) do
287 make_user_from_ap_id(ap_id)
291 def publish(actor, activity) do
292 followers = if actor.follower_address in activity.recipients do
293 {:ok, followers} = User.get_followers(actor)
299 remote_inboxes = (Pleroma.Web.Salmon.remote_users(activity) ++ followers)
300 |> Enum.filter(fn (user) -> User.ap_enabled?(user) end)
301 |> Enum.map(fn (%{info: %{"source_data" => data}}) ->
302 (data["endpoints"] && data["endpoints"]["sharedInbox"]) || data["inbox"]
306 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
307 json = Poison.encode!(data)
308 Enum.each remote_inboxes, fn(inbox) ->
309 Logger.info("Federating #{activity.data["id"]} to #{inbox}")
310 host = URI.parse(inbox).host
311 signature = Pleroma.Web.HTTPSignatures.sign(actor, %{host: host, "content-length": byte_size(json)})
312 @httpoison.post(inbox, json, [{"Content-Type", "application/activity+json"}, {"signature", signature}])
317 # This will create a Create activity, which we need internally at the moment.
318 def fetch_object_from_id(id) do
319 if object = Object.get_cached_by_ap_id(id) do
322 with {:ok, %{body: body, status_code: code}} when code in 200..299 <- @httpoison.get(id, [Accept: "application/activity+json"], follow_redirect: true, timeout: 10000, recv_timeout: 20000),
323 {:ok, data} <- Poison.decode(body),
324 data <- Transmogrifier.fix_object(data),
325 nil <- Object.get_by_ap_id(data["id"]),
326 %User{} = user <- User.get_or_fetch_by_ap_id(data["attributedTo"]),
327 {:ok, activity} = create(%{to: data["to"], actor: user, context: data["context"], object: data, local: false, additional: %{"cc" => data["cc"]}}) do
328 {:ok, Object.get_by_ap_id(activity.data["object"]["id"])}
330 object = %Object{} -> {:ok, object}
336 def is_public?(activity) do
337 "https://www.w3.org/ns/activitystreams#Public" in (activity.data["to"] ++ (activity.data["cc"] || []))
340 def visible_for_user?(activity, nil) do
343 def visible_for_user?(activity, user) do
344 x = [user.ap_id | user.following]
345 y = (activity.data["to"] ++ (activity.data["cc"] || []))
346 visible_for_user?(activity, nil) || Enum.any?(x, &(&1 in y))