activity_pub.ex 7.83 KB
Newer Older
lain's avatar
lain committed
1
defmodule Pleroma.Web.ActivityPub.ActivityPub do
Thog's avatar
Thog committed
2
  alias Pleroma.{Activity, Repo, Object, Upload, User, Notification}
lain's avatar
lain committed
3
  alias Pleroma.Web.OStatus
lain's avatar
lain committed
4
  import Ecto.Query
lain's avatar
lain committed
5
  import Pleroma.Web.ActivityPub.Utils
lain's avatar
lain committed
6
  require Logger
lain's avatar
lain committed
7

lain's avatar
lain committed
8
9
10
11
  def get_recipients(data) do
    (data["to"] || []) ++ (data["cc"] || [])
  end

lain's avatar
lain committed
12
  def insert(map, local \\ true) when is_map(map) do
lain's avatar
lain committed
13
14
15
    with nil <- Activity.get_by_ap_id(map["id"]),
         map <- lazy_put_activity_defaults(map),
         :ok <- insert_full_object(map) do
lain's avatar
lain committed
16
      {:ok, activity} = Repo.insert(%Activity{data: map, local: local, actor: map["actor"], recipients: get_recipients(map)})
17
      Notification.create_notifications(activity)
lain's avatar
lain committed
18
      stream_out(activity)
19
      {:ok, activity}
lain's avatar
lain committed
20
21
22
    else
      %Activity{} = activity -> {:ok, activity}
      error -> {:error, error}
lain's avatar
lain committed
23
    end
lain's avatar
lain committed
24
  end
lain's avatar
lain committed
25

lain's avatar
lain committed
26
27
28
29
  def stream_out(activity) do
    if activity.data["type"] in ["Create", "Announce"] do
      Pleroma.Web.Streamer.stream("user", activity)
      if Enum.member?(activity.data["to"], "https://www.w3.org/ns/activitystreams#Public") do
lain's avatar
lain committed
30
        Pleroma.Web.Streamer.stream("public", activity)
lain's avatar
lain committed
31
        if activity.local do
Roger Braun's avatar
Roger Braun committed
32
33
          Pleroma.Web.Streamer.stream("public:local", activity)
        end
lain's avatar
lain committed
34
      end
lain's avatar
lain committed
35
36
37
38
39
40
41
    end
  end

  def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do
    with create_data <- make_create_data(%{to: to, actor: actor, published: published, context: context, object: object}, additional),
         {:ok, activity} <- insert(create_data, local),
         :ok <- maybe_federate(activity) do
42
43
      {:ok, activity}
    end
lain's avatar
lain committed
44
  end
lain's avatar
lain committed
45

lain's avatar
lain committed
46
  # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
Thog's avatar
Thog committed
47
  def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => _}} = object, activity_id \\ nil, local \\ true) do
lain's avatar
lain committed
48
49
50
51
52
53
54
55
56
    with nil <- get_existing_like(ap_id, object),
         like_data <- make_like_data(user, object, activity_id),
         {:ok, activity} <- insert(like_data, local),
         {:ok, object} <- add_like_to_object(activity, object),
         :ok <- maybe_federate(activity) do
      {:ok, activity, object}
    else
      %Activity{} = activity -> {:ok, activity, object}
      error -> {:error, error}
lain's avatar
lain committed
57
    end
lain's avatar
lain committed
58
59
  end

lain's avatar
lain committed
60
61
62
63
  def unlike(%User{} = actor, %Object{} = object) do
    with %Activity{} = activity <- get_existing_like(actor.ap_id, object),
         {:ok, _activity} <- Repo.delete(activity),
         {:ok, object} <- remove_like_from_object(activity, object) do
lain's avatar
lain committed
64
      {:ok, object}
lain's avatar
lain committed
65
      else _e -> {:ok, object}
lain's avatar
lain committed
66
67
68
    end
  end

Thog's avatar
Thog committed
69
  def announce(%User{ap_id: _} = user, %Object{data: %{"id" => _}} = object, activity_id \\ nil, local \\ true) do
lain's avatar
lain committed
70
71
72
73
74
75
76
77
    with announce_data <- make_announce_data(user, object, activity_id),
         {:ok, activity} <- insert(announce_data, local),
         {:ok, object} <- add_announce_to_object(activity, object),
         :ok <- maybe_federate(activity) do
      {:ok, activity, object}
    else
      error -> {:error, error}
    end
lain's avatar
lain committed
78
79
  end

lain's avatar
lain committed
80
81
82
83
84
85
  def follow(follower, followed, activity_id \\ nil, local \\ true) do
    with data <- make_follow_data(follower, followed, activity_id),
         {:ok, activity} <- insert(data, local),
         :ok <- maybe_federate(activity) do
      {:ok, activity}
    end
lain's avatar
lain committed
86
87
  end

lain's avatar
lain committed
88
89
90
91
92
93
94
  def unfollow(follower, followed, local \\ true) do
    with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
         unfollow_data <- make_unfollow_data(follower, followed, follow_activity),
         {:ok, activity} <- insert(unfollow_data, local),
         :ok, maybe_federate(activity) do
      {:ok, activity}
    end
lain's avatar
lain committed
95
96
  end

lain's avatar
lain committed
97
98
99
100
101
102
103
104
  def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
    user = User.get_cached_by_ap_id(actor)
    data = %{
      "type" => "Delete",
      "actor" => actor,
      "object" => id,
      "to" => [user.follower_address, "https://www.w3.org/ns/activitystreams#Public"]
    }
lain's avatar
lain committed
105
106
107
    with Repo.delete(object),
         Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
         {:ok, activity} <- insert(data, local),
lain's avatar
lain committed
108
109
110
111
112
         :ok <- maybe_federate(activity) do
      {:ok, activity}
    end
  end

113
  def fetch_activities_for_context(context, opts \\ %{}) do
lain's avatar
lain committed
114
    query = from activity in Activity,
lain's avatar
lain committed
115
      where: fragment("?->>'type' = ? and ?->>'context' = ?", activity.data, "Create", activity.data, ^context),
116
      order_by: [desc: :id]
117
    query = restrict_blocked(query, opts)
lain's avatar
lain committed
118
    Repo.all(query)
lain's avatar
lain committed
119
120
  end

lain's avatar
lain committed
121
  def fetch_public_activities(opts \\ %{}) do
lain's avatar
lain committed
122
123
124
125
    public = ["https://www.w3.org/ns/activitystreams#Public"]
    fetch_activities(public, opts)
  end

lain's avatar
lain committed
126
127
128
129
  defp restrict_since(query, %{"since_id" => since_id}) do
    from activity in query, where: activity.id > ^since_id
  end
  defp restrict_since(query, _), do: query
lain's avatar
lain committed
130

Roger Braun's avatar
Roger Braun committed
131
132
133
134
135
136
  defp restrict_tag(query, %{"tag" => tag}) do
    from activity in query,
      where: fragment("? <@ (? #> '{\"object\",\"tag\"}')", ^tag, activity.data)
  end
  defp restrict_tag(query, _), do: query

lain's avatar
lain committed
137
  defp restrict_recipients(query, recipients) do
Roger Braun's avatar
Roger Braun committed
138
139
140
141
142
    Enum.reduce(recipients, query, fn (recipient, q) ->
      map = %{ to: [recipient] }
      from activity in q,
      or_where: fragment(~s(? @> ?), activity.data, ^map)
    end)
lain's avatar
lain committed
143
  end
lain's avatar
lain committed
144

lain's avatar
lain committed
145
146
  defp restrict_local(query, %{"local_only" => true}) do
    from activity in query, where: activity.local == true
lain's avatar
lain committed
147
  end
lain's avatar
lain committed
148
  defp restrict_local(query, _), do: query
lain's avatar
lain committed
149

lain's avatar
lain committed
150
151
  defp restrict_max(query, %{"max_id" => max_id}) do
    from activity in query, where: activity.id < ^max_id
152
  end
lain's avatar
lain committed
153
  defp restrict_max(query, _), do: query
154

lain's avatar
lain committed
155
156
  defp restrict_actor(query, %{"actor_id" => actor_id}) do
    from activity in query,
lain's avatar
lain committed
157
      where: activity.actor == ^actor_id
158
  end
lain's avatar
lain committed
159
  defp restrict_actor(query, _), do: query
160

lain's avatar
lain committed
161
162
163
  defp restrict_type(query, %{"type" => type}) when is_binary(type) do
    restrict_type(query, %{"type" => [type]})
  end
164
165
  defp restrict_type(query, %{"type" => type}) do
    from activity in query,
lain's avatar
lain committed
166
      where: fragment("?->>'type' = ANY(?)", activity.data, ^type)
167
168
169
  end
  defp restrict_type(query, _), do: query

170
171
172
173
174
175
  defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
    from activity in query,
      where: fragment("? <@ (? #> '{\"object\",\"likes\"}')", ^ap_id, activity.data)
  end
  defp restrict_favorited_by(query, _), do: query

eal's avatar
eal committed
176
  defp restrict_media(query, %{"only_media" => val}) when val == "true" or val == "1" do
eal's avatar
eal committed
177
178
179
180
181
    from activity in query,
      where: fragment("not (? #> '{\"object\",\"attachment\"}' = ?)", activity.data, ^[])
  end
  defp restrict_media(query, _), do: query

182
  # Only search through last 100_000 activities by default
183
  defp restrict_recent(query, %{"whole_db" => true}), do: query
184
  defp restrict_recent(query, _) do
lain's avatar
lain committed
185
    since = (Repo.aggregate(Activity, :max, :id) || 0) - 100_000
186
187
188
189
190

    from activity in query,
      where: activity.id > ^since
  end

lain's avatar
lain committed
191
192
  defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
    blocks = info["blocks"] || []
193
    from activity in query,
lain's avatar
lain committed
194
      where: fragment("not (? = ANY(?))", activity.actor, ^blocks)
195
196
197
  end
  defp restrict_blocked(query, _), do: query

lain's avatar
lain committed
198
199
200
  def fetch_activities(recipients, opts \\ %{}) do
    base_query = from activity in Activity,
      limit: 20,
lain's avatar
lain committed
201
      order_by: [fragment("? desc nulls last", activity.id)]
lain's avatar
lain committed
202

lain's avatar
lain committed
203
204
    base_query
    |> restrict_recipients(recipients)
Roger Braun's avatar
Roger Braun committed
205
    |> restrict_tag(opts)
lain's avatar
lain committed
206
207
208
209
    |> restrict_since(opts)
    |> restrict_local(opts)
    |> restrict_max(opts)
    |> restrict_actor(opts)
210
    |> restrict_type(opts)
211
    |> restrict_favorited_by(opts)
212
    |> restrict_recent(opts)
213
    |> restrict_blocked(opts)
eal's avatar
eal committed
214
    |> restrict_media(opts)
lain's avatar
lain committed
215
216
    |> Repo.all
    |> Enum.reverse
dtluna's avatar
dtluna committed
217
218
  end

lain's avatar
lain committed
219
  def upload(file) do
lain's avatar
lain committed
220
221
222
    data = Upload.store(file)
    Repo.insert(%Object{data: data})
  end
lain's avatar
lain committed
223
224
225
226
227
228
229
230
231
232
233
234

  def prepare_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
    with {:ok, user} <- OStatus.find_or_make_user(data["actor"]) do
      data
    else
      _e -> :error
    end
  end

  def prepare_incoming(_) do
    :error
  end
lain's avatar
lain committed
235
end