transmogrifier.ex 25.1 KB
Newer Older
1
# Pleroma: A lightweight social networking server
kaniini's avatar
kaniini committed
2
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3
4
# SPDX-License-Identifier: AGPL-3.0-only

5
6
7
8
9
defmodule Pleroma.Web.ActivityPub.Transmogrifier do
  @moduledoc """
  A module to handle coding from internal to wire ActivityPub and back.
  """
  alias Pleroma.User
lain's avatar
lain committed
10
  alias Pleroma.Object
11
  alias Pleroma.Activity
lain's avatar
lain committed
12
  alias Pleroma.Repo
13
  alias Pleroma.Web.ActivityPub.ActivityPub
14
  alias Pleroma.Web.ActivityPub.Utils
15

lain's avatar
lain committed
16
17
  import Ecto.Query

18
19
  require Logger

20
21
22
23
24
  def get_actor(%{"actor" => actor}) when is_binary(actor) do
    actor
  end

  def get_actor(%{"actor" => actor}) when is_list(actor) do
25
26
27
    if is_binary(Enum.at(actor, 0)) do
      Enum.at(actor, 0)
    else
28
      Enum.find(actor, fn %{"type" => type} -> type in ["Person", "Service", "Application"] end)
29
30
      |> Map.get("id")
    end
31
32
  end

33
34
  def get_actor(%{"actor" => %{"id" => id}}) when is_bitstring(id) do
    id
35
36
  end

37
38
39
40
  def get_actor(%{"actor" => nil, "attributedTo" => actor}) when not is_nil(actor) do
    get_actor(%{"actor" => actor})
  end

41
42
43
  @doc """
  Checks that an imported AP object's actor matches the domain it came from.
  """
Maksim's avatar
Maksim committed
44
  def contain_origin(_id, %{"actor" => nil}), do: :error
45

Maksim's avatar
Maksim committed
46
  def contain_origin(id, %{"actor" => _actor} = params) do
47
    id_uri = URI.parse(id)
48
    actor_uri = URI.parse(get_actor(params))
49
50
51
52
53
54
55
56

    if id_uri.host == actor_uri.host do
      :ok
    else
      :error
    end
  end

Maksim's avatar
Maksim committed
57
  def contain_origin_from_id(_id, %{"id" => nil}), do: :error
58

Maksim's avatar
Maksim committed
59
  def contain_origin_from_id(id, %{"id" => other_id} = _params) do
60
61
62
63
64
65
66
67
68
69
    id_uri = URI.parse(id)
    other_uri = URI.parse(other_id)

    if id_uri.host == other_uri.host do
      :ok
    else
      :error
    end
  end

70
71
72
73
74
  @doc """
  Modifies an incoming AP object (mastodon format) to our internal format.
  """
  def fix_object(object) do
    object
75
    |> fix_actor
76
    |> fix_url
77
    |> fix_attachments
lain's avatar
lain committed
78
    |> fix_context
lain's avatar
lain committed
79
    |> fix_in_reply_to
lain's avatar
lain committed
80
    |> fix_emoji
81
    |> fix_tag
82
    |> fix_content_map
83
    |> fix_likes
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    |> fix_addressing
  end

  def fix_addressing_list(map, field) do
    if is_binary(map[field]) do
      map
      |> Map.put(field, [map[field]])
    else
      map
    end
  end

  def fix_addressing(map) do
    map
    |> fix_addressing_list("to")
    |> fix_addressing_list("cc")
    |> fix_addressing_list("bto")
    |> fix_addressing_list("bcc")
lain's avatar
lain committed
102
103
  end

104
105
  def fix_actor(%{"attributedTo" => actor} = object) do
    object
106
    |> Map.put("actor", get_actor(%{"actor" => actor}))
107
108
  end

109
110
111
112
113
114
115
116
117
118
119
120
121
122
  def fix_likes(%{"likes" => likes} = object)
      when is_bitstring(likes) do
    # Check for standardisation
    # This is what Peertube does
    # curl -H 'Accept: application/activity+json' $likes | jq .totalItems
    object
    |> Map.put("likes", [])
    |> Map.put("like_count", 0)
  end

  def fix_likes(object) do
    object
  end

123
124
125
  def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object)
      when not is_nil(in_reply_to) do
    in_reply_to_id =
126
      cond do
127
128
129
130
131
132
133
134
135
        is_bitstring(in_reply_to) ->
          in_reply_to

        is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
          in_reply_to["id"]

        is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
          Enum.at(in_reply_to, 0)

136
        # Maybe I should output an error too?
137
138
        true ->
          ""
139
140
      end

141
    case fetch_obj_helper(in_reply_to_id) do
lain's avatar
lain committed
142
      {:ok, replied_object} ->
143
144
145
146
147
148
149
150
151
152
        with %Activity{} = activity <-
               Activity.get_create_activity_by_object_ap_id(replied_object.data["id"]) do
          object
          |> Map.put("inReplyTo", replied_object.data["id"])
          |> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
          |> Map.put("inReplyToStatusId", activity.id)
          |> Map.put("conversation", replied_object.data["context"] || object["conversation"])
          |> Map.put("context", replied_object.data["context"] || object["conversation"])
        else
          e ->
153
            Logger.error("Couldn't fetch \"#{inspect(in_reply_to_id)}\", error: #{inspect(e)}")
154
155
            object
        end
lain's avatar
lain committed
156

lain's avatar
lain committed
157
      e ->
158
        Logger.error("Couldn't fetch \"#{inspect(in_reply_to_id)}\", error: #{inspect(e)}")
lain's avatar
lain committed
159
160
161
        object
    end
  end
lain's avatar
lain committed
162

lain's avatar
lain committed
163
164
  def fix_in_reply_to(object), do: object

lain's avatar
lain committed
165
  def fix_context(object) do
Haelwenn's avatar
Haelwenn committed
166
167
    context = object["context"] || object["conversation"] || Utils.generate_context_id()

lain's avatar
lain committed
168
    object
Haelwenn's avatar
Haelwenn committed
169
170
    |> Map.put("context", context)
    |> Map.put("conversation", context)
lain's avatar
lain committed
171
172
  end

173
  def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
lain's avatar
lain committed
174
    attachments =
175
      attachment
lain's avatar
lain committed
176
      |> Enum.map(fn data ->
177
178
179
180
181
182
183
184
        media_type = data["mediaType"] || data["mimeType"]
        href = data["url"] || data["href"]

        url = [%{"type" => "Link", "mediaType" => media_type, "href" => href}]

        data
        |> Map.put("mediaType", media_type)
        |> Map.put("url", url)
lain's avatar
lain committed
185
      end)
lain's avatar
lain committed
186
187
188

    object
    |> Map.put("attachment", attachments)
189
190
  end

191
  def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
192
193
    Map.put(object, "attachment", [attachment])
    |> fix_attachments()
194
195
  end

196
  def fix_attachments(object), do: object
197

198
199
200
201
202
  def fix_url(%{"url" => url} = object) when is_map(url) do
    object
    |> Map.put("url", url["href"])
  end

203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  def fix_url(%{"type" => "Video", "url" => url} = object) when is_list(url) do
    first_element = Enum.at(url, 0)

    link_element =
      url
      |> Enum.filter(fn x -> is_map(x) end)
      |> Enum.filter(fn x -> x["mimeType"] == "text/html" end)
      |> Enum.at(0)

    object
    |> Map.put("attachment", [first_element])
    |> Map.put("url", link_element["href"])
  end

  def fix_url(%{"type" => object_type, "url" => url} = object)
      when object_type != "Video" and is_list(url) do
219
220
221
222
223
224
225
226
227
    first_element = Enum.at(url, 0)

    url_string =
      cond do
        is_bitstring(first_element) -> first_element
        is_map(first_element) -> first_element["href"] || ""
        true -> ""
      end

228
229
    object
    |> Map.put("url", url_string)
230
231
232
233
  end

  def fix_url(object), do: object

234
  def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
lain's avatar
lain committed
235
236
237
238
239
    emoji = tags |> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)

    emoji =
      emoji
      |> Enum.reduce(%{}, fn data, mapping ->
240
        name = String.trim(data["name"], ":")
lain's avatar
lain committed
241

lain's avatar
lain committed
242
243
        mapping |> Map.put(name, data["icon"]["url"])
      end)
lain's avatar
lain committed
244
245
246
247
248
249
250
251

    # we merge mastodon and pleroma emoji into a single mapping, to allow for both wire formats
    emoji = Map.merge(object["emoji"] || %{}, emoji)

    object
    |> Map.put("emoji", emoji)
  end

252
253
254
255
256
257
258
259
  def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
    name = String.trim(tag["name"], ":")
    emoji = %{name => tag["icon"]["url"]}

    object
    |> Map.put("emoji", emoji)
  end

260
  def fix_emoji(object), do: object
261

262
  def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
lain's avatar
lain committed
263
    tags =
264
      tag
lain's avatar
lain committed
265
266
      |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
      |> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
267

268
    combined = tag ++ tags
269
270
271
272
273

    object
    |> Map.put("tag", combined)
  end

274
275
  def fix_tag(%{"tag" => %{"type" => "Hashtag", "name" => hashtag} = tag} = object) do
    combined = [tag, String.slice(hashtag, 1..-1)]
276
277
278
279
280

    object
    |> Map.put("tag", combined)
  end

281
  def fix_tag(object), do: object
282

283
284
285
286
287
288
289
290
291
292
293
  # content map usually only has one language so this will do for now.
  def fix_content_map(%{"contentMap" => content_map} = object) do
    content_groups = Map.to_list(content_map)
    {_, content} = Enum.at(content_groups, 0)

    object
    |> Map.put("content", content)
  end

  def fix_content_map(object), do: object

Maksim's avatar
Maksim committed
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
  defp mastodon_follow_hack(%{"id" => id, "actor" => follower_id}, followed) do
    with true <- id =~ "follows",
         %User{local: true} = follower <- User.get_cached_by_ap_id(follower_id),
         %Activity{} = activity <- Utils.fetch_latest_follow(follower, followed) do
      {:ok, activity}
    else
      _ -> {:error, nil}
    end
  end

  defp mastodon_follow_hack(_, _), do: {:error, nil}

  defp get_follow_activity(follow_object, followed) do
    with object_id when not is_nil(object_id) <- Utils.get_ap_id(follow_object),
         {_, %Activity{} = activity} <- {:activity, Activity.get_by_ap_id(object_id)} do
      {:ok, activity}
    else
      # Can't find the activity. This might a Mastodon 2.3 "Accept"
      {:activity, nil} ->
        mastodon_follow_hack(follow_object, followed)

      _ ->
        {:error, nil}
    end
  end

320
321
322
323
324
325
  # disallow objects with bogus IDs
  def handle_incoming(%{"id" => nil}), do: :error
  def handle_incoming(%{"id" => ""}), do: :error
  # length of https:// = 8, should validate better, but good enough for now.
  def handle_incoming(%{"id" => id}) when not (is_binary(id) and length(id) > 8), do: :error

326
327
328
  # TODO: validate those with a Ecto scheme
  # - tags
  # - emoji
kaniini's avatar
kaniini committed
329
  def handle_incoming(%{"type" => "Create", "object" => %{"type" => objtype} = object} = data)
330
      when objtype in ["Article", "Note", "Video", "Page"] do
331
    actor = get_actor(data)
332
333
334
335

    data =
      Map.put(data, "actor", actor)
      |> fix_addressing
336

lain's avatar
lain committed
337
338
    with nil <- Activity.get_create_activity_by_object_ap_id(object["id"]),
         %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
339
      object = fix_object(data["object"])
340

341
342
343
344
      params = %{
        to: data["to"],
        object: object,
        actor: user,
lain's avatar
lain committed
345
        context: object["conversation"],
346
347
        local: false,
        published: data["published"],
lain's avatar
lain committed
348
349
350
351
352
        additional:
          Map.take(data, [
            "cc",
            "id"
          ])
353
354
355
356
      }

      ActivityPub.create(params)
    else
lain's avatar
lain committed
357
      %Activity{} = activity -> {:ok, activity}
358
359
360
361
      _e -> :error
    end
  end

lain's avatar
lain committed
362
363
364
  def handle_incoming(
        %{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data
      ) do
365
    with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
366
367
         %User{} = follower <- User.get_or_fetch_by_ap_id(follower),
         {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
368
      if not User.locked?(followed) do
kaniini's avatar
kaniini committed
369
370
371
372
373
374
375
        ActivityPub.accept(%{
          to: [follower.ap_id],
          actor: followed.ap_id,
          object: data,
          local: true
        })

376
377
        User.follow(follower, followed)
      end
lain's avatar
lain committed
378

379
380
381
382
383
384
      {:ok, activity}
    else
      _e -> :error
    end
  end

385
  def handle_incoming(
Maksim's avatar
Maksim committed
386
        %{"type" => "Accept", "object" => follow_object, "actor" => _actor, "id" => _id} = data
387
      ) do
388
389
    with actor <- get_actor(data),
         %User{} = followed <- User.get_or_fetch_by_ap_id(actor),
390
         {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
391
         {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
392
         %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
lain's avatar
lain committed
393
394
395
396
397
398
399
400
         {:ok, activity} <-
           ActivityPub.accept(%{
             to: follow_activity.data["to"],
             type: "Accept",
             actor: followed.ap_id,
             object: follow_activity.data["id"],
             local: false
           }) do
401
      if not User.following?(follower, followed) do
Maksim's avatar
Maksim committed
402
        {:ok, _follower} = User.follow(follower, followed)
403
      end
404

405
      {:ok, activity}
406
407
    else
      _e -> :error
408
409
410
411
    end
  end

  def handle_incoming(
Maksim's avatar
Maksim committed
412
        %{"type" => "Reject", "object" => follow_object, "actor" => _actor, "id" => _id} = data
413
      ) do
414
415
    with actor <- get_actor(data),
         %User{} = followed <- User.get_or_fetch_by_ap_id(actor),
416
         {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
417
         {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
418
         %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
lain's avatar
lain committed
419
420
421
422
423
424
425
426
         {:ok, activity} <-
           ActivityPub.accept(%{
             to: follow_activity.data["to"],
             type: "Accept",
             actor: followed.ap_id,
             object: follow_activity.data["id"],
             local: false
           }) do
427
428
      User.unfollow(follower, followed)

429
      {:ok, activity}
430
431
    else
      _e -> :error
432
433
434
    end
  end

lain's avatar
lain committed
435
  def handle_incoming(
Maksim's avatar
Maksim committed
436
        %{"type" => "Like", "object" => object_id, "actor" => _actor, "id" => id} = data
lain's avatar
lain committed
437
      ) do
438
439
440
    with actor <- get_actor(data),
         %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
         {:ok, object} <- get_obj_helper(object_id) || fetch_obj_helper(object_id),
feld's avatar
feld committed
441
         {:ok, activity, _object} <- ActivityPub.like(actor, object, id, false) do
lain's avatar
lain committed
442
443
444
445
446
447
      {:ok, activity}
    else
      _e -> :error
    end
  end

lain's avatar
lain committed
448
  def handle_incoming(
Maksim's avatar
Maksim committed
449
        %{"type" => "Announce", "object" => object_id, "actor" => _actor, "id" => id} = data
lain's avatar
lain committed
450
      ) do
451
452
453
    with actor <- get_actor(data),
         %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
         {:ok, object} <- get_obj_helper(object_id) || fetch_obj_helper(object_id),
feld's avatar
feld committed
454
         {:ok, activity, _object} <- ActivityPub.announce(actor, object, id, false) do
lain's avatar
lain committed
455
456
457
458
459
460
      {:ok, activity}
    else
      _e -> :error
    end
  end

lain's avatar
lain committed
461
  def handle_incoming(
462
        %{"type" => "Update", "object" => %{"type" => object_type} = object, "actor" => actor_id} =
lain's avatar
lain committed
463
          data
464
465
      )
      when object_type in ["Person", "Application", "Service", "Organization"] do
lain's avatar
lain committed
466
467
468
469
    with %User{ap_id: ^actor_id} = actor <- User.get_by_ap_id(object["id"]) do
      {:ok, new_user_data} = ActivityPub.user_data_from_user_object(object)

      banner = new_user_data[:info]["banner"]
470
      locked = new_user_data[:info]["locked"] || false
lain's avatar
lain committed
471
472
473
474

      update_data =
        new_user_data
        |> Map.take([:name, :bio, :avatar])
lain's avatar
lain committed
475
        |> Map.put(:info, %{"banner" => banner, "locked" => locked})
lain's avatar
lain committed
476
477
478

      actor
      |> User.upgrade_changeset(update_data)
lain's avatar
lain committed
479
      |> User.update_and_set_cache()
lain's avatar
lain committed
480

lain's avatar
lain committed
481
482
483
484
485
486
487
      ActivityPub.update(%{
        local: false,
        to: data["to"] || [],
        cc: data["cc"] || [],
        object: object,
        actor: actor_id
      })
lain's avatar
lain committed
488
489
490
491
492
493
494
    else
      e ->
        Logger.error(e)
        :error
    end
  end

495
496
497
498
499
  # TODO: We presently assume that any actor on the same origin domain as the object being
  # deleted has the rights to delete that object.  A better way to validate whether or not
  # the object should be deleted is to refetch the object URI, which should return either
  # an error or a tombstone.  This would allow us to verify that a deletion actually took
  # place.
lain's avatar
lain committed
500
  def handle_incoming(
501
        %{"type" => "Delete", "object" => object_id, "actor" => _actor, "id" => _id} = data
lain's avatar
lain committed
502
      ) do
lain's avatar
lain committed
503
    object_id = Utils.get_ap_id(object_id)
lain's avatar
lain committed
504

505
    with actor <- get_actor(data),
506
         %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
507
         {:ok, object} <- get_obj_helper(object_id) || fetch_obj_helper(object_id),
508
         :ok <- contain_origin(actor.ap_id, object.data),
lain's avatar
lain committed
509
510
511
         {:ok, activity} <- ActivityPub.delete(object, false) do
      {:ok, activity}
    else
feld's avatar
feld committed
512
      _e -> :error
lain's avatar
lain committed
513
514
515
    end
  end

516
  def handle_incoming(
517
518
        %{
          "type" => "Undo",
519
          "object" => %{"type" => "Announce", "object" => object_id},
Maksim's avatar
Maksim committed
520
          "actor" => _actor,
521
          "id" => id
522
        } = data
523
      ) do
524
525
526
    with actor <- get_actor(data),
         %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
         {:ok, object} <- get_obj_helper(object_id) || fetch_obj_helper(object_id),
527
         {:ok, activity, _} <- ActivityPub.unannounce(actor, object, id, false) do
528
529
      {:ok, activity}
    else
Thog's avatar
Thog committed
530
      _e -> :error
531
532
533
    end
  end

normandy's avatar
normandy committed
534
535
536
537
538
539
  def handle_incoming(
        %{
          "type" => "Undo",
          "object" => %{"type" => "Follow", "object" => followed},
          "actor" => follower,
          "id" => id
normandy's avatar
normandy committed
540
        } = _data
normandy's avatar
normandy committed
541
      ) do
normandy's avatar
normandy committed
542
543
    with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
         %User{} = follower <- User.get_or_fetch_by_ap_id(follower),
normandy's avatar
normandy committed
544
         {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
normandy's avatar
normandy committed
545
546
547
      User.unfollow(follower, followed)
      {:ok, activity}
    else
Maksim's avatar
Maksim committed
548
      _e -> :error
normandy's avatar
normandy committed
549
550
551
    end
  end

normandy's avatar
normandy committed
552
553
554
555
556
557
558
559
  def handle_incoming(
        %{
          "type" => "Undo",
          "object" => %{"type" => "Block", "object" => blocked},
          "actor" => blocker,
          "id" => id
        } = _data
      ) do
href's avatar
href committed
560
    with true <- Pleroma.Config.get([:activitypub, :accept_blocks]),
561
         %User{local: true} = blocked <- User.get_cached_by_ap_id(blocked),
normandy's avatar
normandy committed
562
563
         %User{} = blocker <- User.get_or_fetch_by_ap_id(blocker),
         {:ok, activity} <- ActivityPub.unblock(blocker, blocked, id, false) do
normandy's avatar
normandy committed
564
      User.unblock(blocker, blocked)
normandy's avatar
normandy committed
565
566
      {:ok, activity}
    else
Maksim's avatar
Maksim committed
567
      _e -> :error
normandy's avatar
normandy committed
568
569
570
    end
  end

571
  def handle_incoming(
Maksim's avatar
Maksim committed
572
        %{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = _data
573
      ) do
href's avatar
href committed
574
    with true <- Pleroma.Config.get([:activitypub, :accept_blocks]),
575
         %User{local: true} = blocked = User.get_cached_by_ap_id(blocked),
normandy's avatar
normandy committed
576
         %User{} = blocker = User.get_or_fetch_by_ap_id(blocker),
normandy's avatar
normandy committed
577
         {:ok, activity} <- ActivityPub.block(blocker, blocked, id, false) do
578
      User.unfollow(blocker, blocked)
579
      User.block(blocker, blocked)
normandy's avatar
normandy committed
580
581
      {:ok, activity}
    else
Maksim's avatar
Maksim committed
582
      _e -> :error
normandy's avatar
normandy committed
583
584
    end
  end
585

Thog's avatar
Thog committed
586
587
588
589
  def handle_incoming(
        %{
          "type" => "Undo",
          "object" => %{"type" => "Like", "object" => object_id},
Maksim's avatar
Maksim committed
590
          "actor" => _actor,
Thog's avatar
Thog committed
591
          "id" => id
592
        } = data
Thog's avatar
Thog committed
593
      ) do
594
595
596
    with actor <- get_actor(data),
         %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
         {:ok, object} <- get_obj_helper(object_id) || fetch_obj_helper(object_id),
Thog's avatar
Thog committed
597
598
599
         {:ok, activity, _, _} <- ActivityPub.unlike(actor, object, id, false) do
      {:ok, activity}
    else
Thog's avatar
Thog committed
600
      _e -> :error
Thog's avatar
Thog committed
601
602
603
    end
  end

604
605
  def handle_incoming(_), do: :error

606
607
608
  def fetch_obj_helper(id) when is_bitstring(id), do: ActivityPub.fetch_object_from_id(id)
  def fetch_obj_helper(obj) when is_map(obj), do: ActivityPub.fetch_object_from_id(obj["id"])

609
  def get_obj_helper(id) do
610
    if object = Object.normalize(id), do: {:ok, object}, else: nil
611
612
  end

613
614
615
616
617
618
619
620
  def set_reply_to_uri(%{"inReplyTo" => inReplyTo} = object) do
    with false <- String.starts_with?(inReplyTo, "http"),
         {:ok, %{data: replied_to_object}} <- get_obj_helper(inReplyTo) do
      Map.put(object, "inReplyTo", replied_to_object["external_url"] || inReplyTo)
    else
      _e -> object
    end
  end
lain's avatar
lain committed
621

622
623
624
  def set_reply_to_uri(obj), do: obj

  # Prepares the object of an outgoing create activity.
lain's avatar
lain committed
625
626
  def prepare_object(object) do
    object
lain's avatar
lain committed
627
    |> set_sensitive
lain's avatar
lain committed
628
    |> add_hashtags
lain's avatar
lain committed
629
    |> add_mention_tags
lain's avatar
lain committed
630
    |> add_emoji_tags
lain's avatar
lain committed
631
    |> add_attributed_to
lain's avatar
lain committed
632
    |> prepare_attachments
lain's avatar
lain committed
633
    |> set_conversation
634
    |> set_reply_to_uri
635
636
    |> strip_internal_fields
    |> strip_internal_tags
lain's avatar
lain committed
637
638
  end

feld's avatar
feld committed
639
640
641
642
  #  @doc
  #  """
  #  internal -> Mastodon
  #  """
lain's avatar
lain committed
643

644
  def prepare_outgoing(%{"type" => "Create", "object" => object} = data) do
lain's avatar
lain committed
645
646
647
648
649
650
651
    object =
      object
      |> prepare_object

    data =
      data
      |> Map.put("object", object)
lain's avatar
lain committed
652
      |> Map.merge(Utils.make_json_ld_header())
lain's avatar
lain committed
653
654
655
656

    {:ok, data}
  end

kaniini's avatar
kaniini committed
657
658
659
  # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
  # because of course it does.
  def prepare_outgoing(%{"type" => "Accept"} = data) do
660
    with follow_activity <- Activity.normalize(data["object"]) do
kaniini's avatar
kaniini committed
661
662
663
664
665
666
667
668
669
670
      object = %{
        "actor" => follow_activity.actor,
        "object" => follow_activity.data["object"],
        "id" => follow_activity.data["id"],
        "type" => "Follow"
      }

      data =
        data
        |> Map.put("object", object)
lain's avatar
lain committed
671
        |> Map.merge(Utils.make_json_ld_header())
kaniini's avatar
kaniini committed
672
673
674
675
676

      {:ok, data}
    end
  end

677
  def prepare_outgoing(%{"type" => "Reject"} = data) do
678
    with follow_activity <- Activity.normalize(data["object"]) do
679
680
681
682
683
684
685
686
687
688
      object = %{
        "actor" => follow_activity.actor,
        "object" => follow_activity.data["object"],
        "id" => follow_activity.data["id"],
        "type" => "Follow"
      }

      data =
        data
        |> Map.put("object", object)
lain's avatar
lain committed
689
        |> Map.merge(Utils.make_json_ld_header())
690
691
692
693
694

      {:ok, data}
    end
  end

feld's avatar
feld committed
695
  def prepare_outgoing(%{"type" => _type} = data) do
lain's avatar
lain committed
696
697
698
    data =
      data
      |> maybe_fix_object_url
lain's avatar
lain committed
699
      |> Map.merge(Utils.make_json_ld_header())
700
701
702
703

    {:ok, data}
  end

704
705
  def maybe_fix_object_url(data) do
    if is_binary(data["object"]) and not String.starts_with?(data["object"], "http") do
706
      case fetch_obj_helper(data["object"]) do
707
708
        {:ok, relative_object} ->
          if relative_object.data["external_url"] do
feld's avatar
feld committed
709
            _data =
lain's avatar
lain committed
710
711
              data
              |> Map.put("object", relative_object.data["external_url"])
712
713
714
          else
            data
          end
lain's avatar
lain committed
715

716
717
718
719
720
721
722
723
724
        e ->
          Logger.error("Couldn't fetch #{data["object"]} #{inspect(e)}")
          data
      end
    else
      data
    end
  end

lain's avatar
lain committed
725
  def add_hashtags(object) do
lain's avatar
lain committed
726
727
728
729
730
731
732
733
734
    tags =
      (object["tag"] || [])
      |> Enum.map(fn tag ->
        %{
          "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
          "name" => "##{tag}",
          "type" => "Hashtag"
        }
      end)
lain's avatar
lain committed
735
736
737
738
739

    object
    |> Map.put("tag", tags)
  end

lain's avatar
lain committed
740
  def add_mention_tags(object) do
lain's avatar
lain committed
741
    mentions =
742
743
      object
      |> Utils.get_notified_from_object()
lain's avatar
lain committed
744
745
746
      |> Enum.map(fn user ->
        %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"}
      end)
lain's avatar
lain committed
747

lain's avatar
lain committed
748
    tags = object["tag"] || []
lain's avatar
lain committed
749
750

    object
lain's avatar
lain committed
751
    |> Map.put("tag", tags ++ mentions)
lain's avatar
lain committed
752
753
  end

lain's avatar
lain committed
754
755
756
757
  # TODO: we should probably send mtime instead of unix epoch time for updated
  def add_emoji_tags(object) do
    tags = object["tag"] || []
    emoji = object["emoji"] || []
lain's avatar
lain committed
758
759
760
761
762
763
764
765
766
767
768
769

    out =
      emoji
      |> Enum.map(fn {name, url} ->
        %{
          "icon" => %{"url" => url, "type" => "Image"},
          "name" => ":" <> name <> ":",
          "type" => "Emoji",
          "updated" => "1970-01-01T00:00:00Z",
          "id" => url
        }
      end)
lain's avatar
lain committed
770
771
772
773
774

    object
    |> Map.put("tag", tags ++ out)
  end

lain's avatar
lain committed
775
776
777
778
  def set_conversation(object) do
    Map.put(object, "conversation", object["context"])
  end

lain's avatar
lain committed
779
780
781
782
783
  def set_sensitive(object) do
    tags = object["tag"] || []
    Map.put(object, "sensitive", "nsfw" in tags)
  end

lain's avatar
lain committed
784
785
786
787
788
  def add_attributed_to(object) do
    attributedTo = object["attributedTo"] || object["actor"]

    object
    |> Map.put("attributedTo", attributedTo)
789
  end
lain's avatar
lain committed
790
791

  def prepare_attachments(object) do
lain's avatar
lain committed
792
793
794
795
796
797
    attachments =
      (object["attachment"] || [])
      |> Enum.map(fn data ->
        [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
        %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
      end)
lain's avatar
lain committed
798
799
800
801

    object
    |> Map.put("attachment", attachments)
  end
lain's avatar
lain committed
802

803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
  defp strip_internal_fields(object) do
    object
    |> Map.drop([
      "likes",
      "like_count",
      "announcements",
      "announcement_count",
      "emoji",
      "context_id"
    ])
  end

  defp strip_internal_tags(%{"tag" => tags} = object) do
    tags =
      tags
      |> Enum.filter(fn x -> is_map(x) end)

    object
    |> Map.put("tag", tags)
  end

  defp strip_internal_tags(object), do: object

lain's avatar
lain committed
826
827
  defp user_upgrade_task(user) do
    old_follower_address = User.ap_followers(user)
lain's avatar
lain committed
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845

    q =
      from(
        u in User,
        where: ^old_follower_address in u.following,
        update: [
          set: [
            following:
              fragment(
                "array_replace(?,?,?)",
                u.following,
                ^old_follower_address,
                ^user.follower_address
              )
          ]
        ]
      )

lain's avatar
lain committed
846
847
    Repo.update_all(q, [])

848
849
    maybe_retire_websub(user.ap_id)

lain's avatar
lain committed
850
    # Only do this for recent activties, don't go through the whole db.
lain's avatar
lain committed
851
852
    # Only look at the last 1000 activities.
    since = (Repo.aggregate(Activity, :max, :id) || 0) - 1_000
lain's avatar
lain committed
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871

    q =
      from(
        a in Activity,
        where: ^old_follower_address in a.recipients,
        where: a.id > ^since,
        update: [
          set: [
            recipients:
              fragment(
                "array_replace(?,?,?)",
                a.recipients,
                ^old_follower_address,
                ^user.follower_address
              )
          ]
        ]
      )

lain's avatar
lain committed
872
873
874
875
    Repo.update_all(q, [])
  end

  def upgrade_user_from_ap_id(ap_id, async \\ true) do
876
    with %User{local: false} = user <- User.get_by_ap_id(ap_id),
lain's avatar
lain committed
877
         {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id) do
lain's avatar
lain committed
878
      already_ap = User.ap_enabled?(user)
lain's avatar
lain committed
879
880
881
882

      {:ok, user} =
        User.upgrade_changeset(user, data)
        |> Repo.update()
lain's avatar
lain committed
883

lain's avatar
lain committed
884
885
886
887
888
889
890
      if !already_ap do
        # This could potentially take a long time, do it in the background
        if async do
          Task.start(fn ->
            user_upgrade_task(user)
          end)
        else
lain's avatar
lain committed
891
          user_upgrade_task(user)
lain's avatar
lain committed
892
        end
lain's avatar
lain committed
893
      end
lain's avatar
lain committed
894
895
896
897
898
899

      {:ok, user}
    else
      e -> e
    end
  end
900
901
902

  def maybe_retire_websub(ap_id) do
    # some sanity checks
lain's avatar
lain committed
903
904
905
906
907
908
909
    if is_binary(ap_id) && String.length(ap_id) > 8 do
      q =
        from(
          ws in Pleroma.Web.Websub.WebsubClientSubscription,
          where: fragment("? like ?", ws.topic, ^"#{ap_id}%")
        )

910
911
912
      Repo.delete_all(q)
    end
  end
913
914
915

  def maybe_fix_user_url(data) do
    if is_map(data["url"]) do
Thog's avatar
Thog committed
916
917
918
      Map.put(data, "url", data["url"]["href"])
    else
      data
919
920
921
922
923
924
925
    end
  end

  def maybe_fix_user_object(data) do
    data
    |> maybe_fix_user_url
  end
926
end