Don't return activities from blocked users.
[akkoma] / lib / pleroma / web / activity_pub / activity_pub.ex
1 defmodule Pleroma.Web.ActivityPub.ActivityPub do
2 alias Pleroma.{Activity, Repo, Object, Upload, User, Web, Notification}
3 alias Ecto.{Changeset, UUID}
4 import Ecto.Query
5 import Pleroma.Web.ActivityPub.Utils
6 require Logger
7
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)
14 {:ok, activity}
15 else
16 %Activity{} = activity -> {:ok, activity}
17 error -> {:error, error}
18 end
19 end
20
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
25 {:ok, activity}
26 end
27 end
28
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}
37 else
38 %Activity{} = activity -> {:ok, activity, object}
39 error -> {:error, error}
40 end
41 end
42
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
47 {:ok, object}
48 else _e -> {:ok, object}
49 end
50 end
51
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}
58 else
59 error -> {:error, error}
60 end
61 end
62
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
67 {:ok, activity}
68 end
69 end
70
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
76 {:ok, activity}
77 end
78 end
79
80 def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
81 user = User.get_cached_by_ap_id(actor)
82 data = %{
83 "type" => "Delete",
84 "actor" => actor,
85 "object" => id,
86 "to" => [user.follower_address, "https://www.w3.org/ns/activitystreams#Public"]
87 }
88 with Repo.delete(object),
89 Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
90 {:ok, activity} <- insert(data, local),
91 :ok <- maybe_federate(activity) do
92 {:ok, activity}
93 end
94 end
95
96 def fetch_activities_for_context(context) do
97 query = from activity in Activity,
98 where: fragment("?->>'type' = ? and ?->>'context' = ?", activity.data, "Create", activity.data, ^context),
99 order_by: [desc: :id]
100 Repo.all(query)
101 end
102
103 def fetch_public_activities(opts \\ %{}) do
104 public = ["https://www.w3.org/ns/activitystreams#Public"]
105 fetch_activities(public, opts)
106 end
107
108 defp restrict_since(query, %{"since_id" => since_id}) do
109 from activity in query, where: activity.id > ^since_id
110 end
111 defp restrict_since(query, _), do: query
112
113 defp restrict_tag(query, %{"tag" => tag}) do
114 from activity in query,
115 where: fragment("? <@ (? #> '{\"object\",\"tag\"}')", ^tag, activity.data)
116 end
117 defp restrict_tag(query, _), do: query
118
119 defp restrict_recipients(query, recipients) do
120 Enum.reduce(recipients, query, fn (recipient, q) ->
121 map = %{ to: [recipient] }
122 from activity in q,
123 or_where: fragment(~s(? @> ?), activity.data, ^map)
124 end)
125 end
126
127 defp restrict_local(query, %{"local_only" => true}) do
128 from activity in query, where: activity.local == true
129 end
130 defp restrict_local(query, _), do: query
131
132 defp restrict_max(query, %{"max_id" => max_id}) do
133 from activity in query, where: activity.id < ^max_id
134 end
135 defp restrict_max(query, _), do: query
136
137 defp restrict_actor(query, %{"actor_id" => actor_id}) do
138 from activity in query,
139 where: fragment("?->>'actor' = ?", activity.data, ^actor_id)
140 end
141 defp restrict_actor(query, _), do: query
142
143 defp restrict_type(query, %{"type" => type}) when is_binary(type) do
144 restrict_type(query, %{"type" => [type]})
145 end
146 defp restrict_type(query, %{"type" => type}) do
147 from activity in query,
148 where: fragment("?->>'type' = ANY(?)", activity.data, ^type)
149 end
150 defp restrict_type(query, _), do: query
151
152 defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
153 from activity in query,
154 where: fragment("? <@ (? #> '{\"object\",\"likes\"}')", ^ap_id, activity.data)
155 end
156 defp restrict_favorited_by(query, _), do: query
157
158 # Only search through last 100_000 activities by default
159 defp restrict_recent(query, _) do
160 since = Repo.aggregate(Activity, :max, :id) - 100_000
161
162 from activity in query,
163 where: activity.id > ^since
164 end
165
166 defp restrict_blocked(query, %{"blocking_user" => user}) do
167 blocks = user.info["blocks"] || []
168 from activity in query,
169 where: fragment("not (?->>'actor' = ANY(?))", activity.data, ^blocks)
170 end
171 defp restrict_blocked(query, _), do: query
172
173 def fetch_activities(recipients, opts \\ %{}) do
174 base_query = from activity in Activity,
175 limit: 20,
176 order_by: [desc: :id]
177
178 base_query
179 |> restrict_recipients(recipients)
180 |> restrict_tag(opts)
181 |> restrict_since(opts)
182 |> restrict_local(opts)
183 |> restrict_max(opts)
184 |> restrict_actor(opts)
185 |> restrict_type(opts)
186 |> restrict_favorited_by(opts)
187 |> restrict_recent(opts)
188 |> restrict_blocked(opts)
189 |> Repo.all
190 |> Enum.reverse
191 end
192
193 def upload(file) do
194 data = Upload.store(file)
195 Repo.insert(%Object{data: data})
196 end
197 end