ActivityPub: Partly handle incoming follows.
[akkoma] / lib / pleroma / web / activity_pub / activity_pub.ex
1 defmodule Pleroma.Web.ActivityPub.ActivityPub do
2 alias Pleroma.{Activity, Repo, Object, Upload, User, Notification}
3 alias Pleroma.Web.ActivityPub.Transmogrifier
4 import Ecto.Query
5 import Pleroma.Web.ActivityPub.Utils
6 require Logger
7
8 @httpoison Application.get_env(:pleroma, :httpoison)
9
10 def get_recipients(data) do
11 (data["to"] || []) ++ (data["cc"] || [])
12 end
13
14 def insert(map, local \\ true) when is_map(map) do
15 with nil <- Activity.get_by_ap_id(map["id"]),
16 map <- lazy_put_activity_defaults(map),
17 :ok <- insert_full_object(map) do
18 {:ok, activity} = Repo.insert(%Activity{data: map, local: local, actor: map["actor"], recipients: get_recipients(map)})
19 Notification.create_notifications(activity)
20 stream_out(activity)
21 {:ok, activity}
22 else
23 %Activity{} = activity -> {:ok, activity}
24 error -> {:error, error}
25 end
26 end
27
28 def stream_out(activity) do
29 if activity.data["type"] in ["Create", "Announce"] do
30 Pleroma.Web.Streamer.stream("user", activity)
31 if Enum.member?(activity.data["to"], "https://www.w3.org/ns/activitystreams#Public") do
32 Pleroma.Web.Streamer.stream("public", activity)
33 if activity.local do
34 Pleroma.Web.Streamer.stream("public:local", activity)
35 end
36 end
37 end
38 end
39
40 def create(%{to: to, actor: actor, context: context, object: object} = params) do
41 additional = params[:additional] || %{}
42 local = !(params[:local] == false) # only accept false as false value
43 published = params[:published]
44
45 with create_data <- make_create_data(%{to: to, actor: actor, published: published, context: context, object: object}, additional),
46 {:ok, activity} <- insert(create_data, local),
47 :ok <- maybe_federate(activity) do
48 {:ok, activity}
49 end
50 end
51
52 # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
53 def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => _}} = object, activity_id \\ nil, local \\ true) do
54 with nil <- get_existing_like(ap_id, object),
55 like_data <- make_like_data(user, object, activity_id),
56 {:ok, activity} <- insert(like_data, local),
57 {:ok, object} <- add_like_to_object(activity, object),
58 :ok <- maybe_federate(activity) do
59 {:ok, activity, object}
60 else
61 %Activity{} = activity -> {:ok, activity, object}
62 error -> {:error, error}
63 end
64 end
65
66 def unlike(%User{} = actor, %Object{} = object) do
67 with %Activity{} = activity <- get_existing_like(actor.ap_id, object),
68 {:ok, _activity} <- Repo.delete(activity),
69 {:ok, object} <- remove_like_from_object(activity, object) do
70 {:ok, object}
71 else _e -> {:ok, object}
72 end
73 end
74
75 def announce(%User{ap_id: _} = user, %Object{data: %{"id" => _}} = object, activity_id \\ nil, local \\ true) do
76 with announce_data <- make_announce_data(user, object, activity_id),
77 {:ok, activity} <- insert(announce_data, local),
78 {:ok, object} <- add_announce_to_object(activity, object),
79 :ok <- maybe_federate(activity) do
80 {:ok, activity, object}
81 else
82 error -> {:error, error}
83 end
84 end
85
86 def follow(follower, followed, activity_id \\ nil, local \\ true) do
87 with data <- make_follow_data(follower, followed, activity_id),
88 {:ok, activity} <- insert(data, local),
89 :ok <- maybe_federate(activity) do
90 {:ok, activity}
91 end
92 end
93
94 def unfollow(follower, followed, local \\ true) do
95 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
96 unfollow_data <- make_unfollow_data(follower, followed, follow_activity),
97 {:ok, activity} <- insert(unfollow_data, local),
98 :ok, maybe_federate(activity) do
99 {:ok, activity}
100 end
101 end
102
103 def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
104 user = User.get_cached_by_ap_id(actor)
105 data = %{
106 "type" => "Delete",
107 "actor" => actor,
108 "object" => id,
109 "to" => [user.follower_address, "https://www.w3.org/ns/activitystreams#Public"]
110 }
111 with Repo.delete(object),
112 Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
113 {:ok, activity} <- insert(data, local),
114 :ok <- maybe_federate(activity) do
115 {:ok, activity}
116 end
117 end
118
119 def fetch_activities_for_context(context, opts \\ %{}) do
120 query = from activity in Activity,
121 where: fragment("?->>'type' = ? and ?->>'context' = ?", activity.data, "Create", activity.data, ^context),
122 order_by: [desc: :id]
123 query = restrict_blocked(query, opts)
124 Repo.all(query)
125 end
126
127 def fetch_public_activities(opts \\ %{}) do
128 public = ["https://www.w3.org/ns/activitystreams#Public"]
129 fetch_activities(public, opts)
130 end
131
132 defp restrict_since(query, %{"since_id" => since_id}) do
133 from activity in query, where: activity.id > ^since_id
134 end
135 defp restrict_since(query, _), do: query
136
137 defp restrict_tag(query, %{"tag" => tag}) do
138 from activity in query,
139 where: fragment("? <@ (? #> '{\"object\",\"tag\"}')", ^tag, activity.data)
140 end
141 defp restrict_tag(query, _), do: query
142
143 defp restrict_recipients(query, []), do: query
144 defp restrict_recipients(query, recipients) do
145 from activity in query,
146 where: fragment("? && ?", ^recipients, activity.recipients)
147 end
148
149 defp restrict_local(query, %{"local_only" => true}) do
150 from activity in query, where: activity.local == true
151 end
152 defp restrict_local(query, _), do: query
153
154 defp restrict_max(query, %{"max_id" => max_id}) do
155 from activity in query, where: activity.id < ^max_id
156 end
157 defp restrict_max(query, _), do: query
158
159 defp restrict_actor(query, %{"actor_id" => actor_id}) do
160 from activity in query,
161 where: activity.actor == ^actor_id
162 end
163 defp restrict_actor(query, _), do: query
164
165 defp restrict_type(query, %{"type" => type}) when is_binary(type) do
166 restrict_type(query, %{"type" => [type]})
167 end
168 defp restrict_type(query, %{"type" => type}) do
169 from activity in query,
170 where: fragment("?->>'type' = ANY(?)", activity.data, ^type)
171 end
172 defp restrict_type(query, _), do: query
173
174 defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
175 from activity in query,
176 where: fragment("? <@ (? #> '{\"object\",\"likes\"}')", ^ap_id, activity.data)
177 end
178 defp restrict_favorited_by(query, _), do: query
179
180 defp restrict_media(query, %{"only_media" => val}) when val == "true" or val == "1" do
181 from activity in query,
182 where: fragment("not (? #> '{\"object\",\"attachment\"}' = ?)", activity.data, ^[])
183 end
184 defp restrict_media(query, _), do: query
185
186 # Only search through last 100_000 activities by default
187 defp restrict_recent(query, %{"whole_db" => true}), do: query
188 defp restrict_recent(query, _) do
189 since = (Repo.aggregate(Activity, :max, :id) || 0) - 100_000
190
191 from activity in query,
192 where: activity.id > ^since
193 end
194
195 defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
196 blocks = info["blocks"] || []
197 from activity in query,
198 where: fragment("not (? = ANY(?))", activity.actor, ^blocks)
199 end
200 defp restrict_blocked(query, _), do: query
201
202 def fetch_activities(recipients, opts \\ %{}) do
203 base_query = from activity in Activity,
204 limit: 20,
205 order_by: [fragment("? desc nulls last", activity.id)]
206
207 base_query
208 |> restrict_recipients(recipients)
209 |> restrict_tag(opts)
210 |> restrict_since(opts)
211 |> restrict_local(opts)
212 |> restrict_max(opts)
213 |> restrict_actor(opts)
214 |> restrict_type(opts)
215 |> restrict_favorited_by(opts)
216 |> restrict_recent(opts)
217 |> restrict_blocked(opts)
218 |> restrict_media(opts)
219 |> Repo.all
220 |> Enum.reverse
221 end
222
223 def upload(file) do
224 data = Upload.store(file)
225 Repo.insert(%Object{data: data})
226 end
227
228 def make_user_from_ap_id(ap_id) do
229 with {:ok, %{status_code: 200, body: body}} <- @httpoison.get(ap_id, ["Accept": "application/activity+json"]),
230 {:ok, data} <- Poison.decode(body)
231 do
232 user_data = %{
233 ap_id: data["id"],
234 info: %{
235 "ap_enabled" => true,
236 "source_data" => data
237 },
238 nickname: "#{data["preferredUsername"]}@#{URI.parse(ap_id).host}",
239 name: data["name"]
240 }
241
242 User.insert_or_update_user(user_data)
243 end
244 end
245
246 def publish(actor, activity) do
247 remote_users = Pleroma.Web.Salmon.remote_users(activity)
248 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
249 Enum.each remote_users, fn(user) ->
250 if user.info["ap_enabled"] do
251 inbox = user.info["source_data"]["inbox"]
252 Logger.info("Federating #{activity.data["id"]} to #{inbox}")
253 host = URI.parse(inbox).host
254 signature = Pleroma.Web.HTTPSignatures.sign(actor, %{host: host})
255 @httpoison.post(inbox, Poison.encode!(data), [{"Content-Type", "application/activity+json"}, {"signature", signature}])
256 end
257 end
258 end
259 end