transmogrifier.ex 29.9 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
defmodule Pleroma.Web.ActivityPub.Transmogrifier do
  @moduledoc """
  A module to handle coding from internal to wire ActivityPub and back.
  """
Haelwenn's avatar
Haelwenn committed
9
10
  alias Pleroma.Activity
  alias Pleroma.Object
rinpatch's avatar
rinpatch committed
11
  alias Pleroma.Object.Containment
Haelwenn's avatar
Haelwenn committed
12
  alias Pleroma.Repo
13
  alias Pleroma.User
Haelwenn's avatar
Haelwenn committed
14
15
  alias Pleroma.Web.ActivityPub.ActivityPub
  alias Pleroma.Web.ActivityPub.Utils
lain's avatar
lain committed
16
  alias Pleroma.Web.ActivityPub.Visibility
17
  alias Pleroma.Web.Federator
18

lain's avatar
lain committed
19
20
  import Ecto.Query

21
  require Logger
22
  require Pleroma.Constants
23

24
25
26
  @doc """
  Modifies an incoming AP object (mastodon format) to our internal format.
  """
27
  def fix_object(object, options \\ []) do
28
    object
29
    |> strip_internal_fields
30
    |> fix_actor
31
    |> fix_url
32
    |> fix_attachments
lain's avatar
lain committed
33
    |> fix_context
34
    |> fix_in_reply_to(options)
lain's avatar
lain committed
35
    |> fix_emoji
36
    |> fix_tag
37
    |> fix_content_map
38
    |> fix_addressing
39
    |> fix_summary
40
    |> fix_type(options)
41
42
43
  end

  def fix_summary(%{"summary" => nil} = object) do
44
    Map.put(object, "summary", "")
45
46
47
48
49
50
51
  end

  def fix_summary(%{"summary" => _} = object) do
    # summary is present, nothing to do
    object
  end

52
  def fix_summary(object), do: Map.put(object, "summary", "")
53
54

  def fix_addressing_list(map, field) do
55
56
57
58
59
60
61
62
63
    cond do
      is_binary(map[field]) ->
        Map.put(map, field, [map[field]])

      is_nil(map[field]) ->
        Map.put(map, field, [])

      true ->
        map
64
65
66
    end
  end

67
68
69
70
71
  def fix_explicit_addressing(
        %{"to" => to, "cc" => cc} = object,
        explicit_mentions,
        follower_collection
      ) do
72
    explicit_to = Enum.filter(to, fn x -> x in explicit_mentions end)
73

74
    explicit_cc = Enum.filter(to, fn x -> x not in explicit_mentions end)
75
76
77

    final_cc =
      (cc ++ explicit_cc)
78
      |> Enum.reject(fn x -> String.ends_with?(x, "/followers") and x != follower_collection end)
79
80
81
82
83
84
85
      |> Enum.uniq()

    object
    |> Map.put("to", explicit_to)
    |> Map.put("cc", final_cc)
  end

86
  def fix_explicit_addressing(object, _explicit_mentions, _followers_collection), do: object
87

88
89
  # if directMessage flag is set to true, leave the addressing alone
  def fix_explicit_addressing(%{"directMessage" => true} = object), do: object
90

91
  def fix_explicit_addressing(object) do
92
    explicit_mentions = Utils.determine_explicit_mentions(object)
93

94
95
96
97
    %User{follower_address: follower_collection} =
      object
      |> Containment.get_actor()
      |> User.get_cached_by_ap_id()
98

99
100
101
102
103
104
    explicit_mentions =
      explicit_mentions ++
        [
          Pleroma.Constants.as_public(),
          follower_collection
        ]
105

106
    fix_explicit_addressing(object, explicit_mentions, follower_collection)
lain's avatar
lain committed
107
108
  end

109
110
111
112
113
114
115
  # if as:Public is addressed, then make sure the followers collection is also addressed
  # so that the activities will be delivered to local users.
  def fix_implicit_addressing(%{"to" => to, "cc" => cc} = object, followers_collection) do
    recipients = to ++ cc

    if followers_collection not in recipients do
      cond do
116
        Pleroma.Constants.as_public() in cc ->
117
118
119
          to = to ++ [followers_collection]
          Map.put(object, "to", to)

120
        Pleroma.Constants.as_public() in to ->
121
122
123
124
125
126
          cc = cc ++ [followers_collection]
          Map.put(object, "cc", cc)

        true ->
          object
      end
127
    else
128
      object
129
130
131
    end
  end

132
133
  def fix_implicit_addressing(object, _), do: object

134
  def fix_addressing(object) do
Alexander Strizhakov's avatar
Alexander Strizhakov committed
135
    {:ok, %User{} = user} = User.get_or_fetch_by_ap_id(object["actor"])
136
137
    followers_collection = User.ap_followers(user)

138
    object
139
140
141
142
    |> fix_addressing_list("to")
    |> fix_addressing_list("cc")
    |> fix_addressing_list("bto")
    |> fix_addressing_list("bcc")
143
    |> fix_explicit_addressing()
144
    |> fix_implicit_addressing(followers_collection)
lain's avatar
lain committed
145
146
  end

147
  def fix_actor(%{"attributedTo" => actor} = object) do
148
    Map.put(object, "actor", Containment.get_actor(%{"actor" => actor}))
149
150
  end

151
152
153
  def fix_in_reply_to(object, options \\ [])

  def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object, options)
154
      when not is_nil(in_reply_to) do
155
    in_reply_to_id = prepare_in_reply_to(in_reply_to)
156
    object = Map.put(object, "inReplyToAtomUri", in_reply_to_id)
lain's avatar
lain committed
157

158
    if Federator.allowed_incoming_reply_depth?(options[:depth]) do
159
160
161
162
163
164
165
166
      with {:ok, replied_object} <- get_obj_helper(in_reply_to_id, options),
           %Activity{} = _ <- Activity.get_create_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("conversation", replied_object.data["context"] || object["conversation"])
        |> Map.put("context", replied_object.data["context"] || object["conversation"])
      else
167
168
169
170
171
172
        e ->
          Logger.error("Couldn't fetch \"#{inspect(in_reply_to_id)}\", error: #{inspect(e)}")
          object
      end
    else
      object
lain's avatar
lain committed
173
174
    end
  end
lain's avatar
lain committed
175

176
  def fix_in_reply_to(object, _options), do: object
lain's avatar
lain committed
177

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  defp prepare_in_reply_to(in_reply_to) do
    cond do
      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)

      true ->
        ""
    end
  end

lain's avatar
lain committed
194
  def fix_context(object) do
Haelwenn's avatar
Haelwenn committed
195
196
    context = object["context"] || object["conversation"] || Utils.generate_context_id()

lain's avatar
lain committed
197
    object
Haelwenn's avatar
Haelwenn committed
198
199
    |> Map.put("context", context)
    |> Map.put("conversation", context)
lain's avatar
lain committed
200
201
  end

202
  def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
lain's avatar
lain committed
203
    attachments =
204
      Enum.map(attachment, fn data ->
205
206
207
208
209
210
211
        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
212
      end)
lain's avatar
lain committed
213

214
    Map.put(object, "attachment", attachments)
215
216
  end

217
  def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
Maksim's avatar
Maksim committed
218
219
220
    object
    |> Map.put("attachment", [attachment])
    |> fix_attachments()
221
222
  end

223
  def fix_attachments(object), do: object
224

225
  def fix_url(%{"url" => url} = object) when is_map(url) do
226
    Map.put(object, "url", url["href"])
227
228
  end

229
230
231
  def fix_url(%{"type" => "Video", "url" => url} = object) when is_list(url) do
    first_element = Enum.at(url, 0)

232
    link_element = Enum.find(url, fn x -> is_map(x) and x["mimeType"] == "text/html" end)
233
234
235
236
237
238
239
240

    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
241
242
243
244
245
246
247
248
249
    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

250
    Map.put(object, "url", url_string)
251
252
253
254
  end

  def fix_url(object), do: object

255
  def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
lain's avatar
lain committed
256
    emoji =
257
258
      tags
      |> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)
lain's avatar
lain committed
259
      |> Enum.reduce(%{}, fn data, mapping ->
260
        name = String.trim(data["name"], ":")
lain's avatar
lain committed
261

262
        Map.put(mapping, name, data["icon"]["url"])
lain's avatar
lain committed
263
      end)
lain's avatar
lain committed
264
265
266
267

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

268
    Map.put(object, "emoji", emoji)
lain's avatar
lain committed
269
270
  end

271
272
273
274
  def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
    name = String.trim(tag["name"], ":")
    emoji = %{name => tag["icon"]["url"]}

275
    Map.put(object, "emoji", emoji)
276
277
  end

278
  def fix_emoji(object), do: object
279

280
  def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
lain's avatar
lain committed
281
    tags =
282
      tag
lain's avatar
lain committed
283
284
      |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
      |> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
285

286
    Map.put(object, "tag", tag ++ tags)
287
288
  end

289
290
  def fix_tag(%{"tag" => %{"type" => "Hashtag", "name" => hashtag} = tag} = object) do
    combined = [tag, String.slice(hashtag, 1..-1)]
291

292
    Map.put(object, "tag", combined)
293
294
  end

295
296
  def fix_tag(%{"tag" => %{} = tag} = object), do: Map.put(object, "tag", [tag])

297
  def fix_tag(object), do: object
298

299
300
301
302
303
  # 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)

304
    Map.put(object, "content", content)
305
306
307
308
  end

  def fix_content_map(object), do: object

309
310
  def fix_type(object, options \\ [])

311
312
  def fix_type(%{"inReplyTo" => reply_id, "name" => _} = object, options)
      when is_binary(reply_id) do
313
314
    with true <- Federator.allowed_incoming_reply_depth?(options[:depth]),
         {:ok, %{data: %{"type" => "Question"} = _} = _} <- get_obj_helper(reply_id, options) do
315
316
      Map.put(object, "type", "Answer")
    else
317
      _ -> object
318
319
320
    end
  end

321
  def fix_type(object, _), do: object
322

Maksim's avatar
Maksim committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
  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

349
350
351
352
353
354
355
356
357
358
359
  # Reduce the object list to find the reported user.
  defp get_reported(objects) do
    Enum.reduce_while(objects, nil, fn ap_id, _ ->
      with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
        {:halt, user}
      else
        _ -> {:cont, nil}
      end
    end)
  end

360
361
  def handle_incoming(data, options \\ [])

362
363
  # Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
  # with nil ID.
364
  def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = data, _options) do
365
366
367
368
    with context <- data["context"] || Utils.generate_context_id(),
         content <- data["content"] || "",
         %User{} = actor <- User.get_cached_by_ap_id(actor),
         # Reduce the object list to find the reported user.
369
         %User{} = account <- get_reported(objects),
370
371
         # Remove the reported user from the object list.
         statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
372
      %{
373
374
375
376
377
        actor: actor,
        context: context,
        account: account,
        statuses: statuses,
        content: content,
378
        additional: %{"cc" => [account.ap_id]}
379
      }
380
      |> ActivityPub.flag()
381
382
383
    end
  end

384
  # disallow objects with bogus IDs
385
386
  def handle_incoming(%{"id" => nil}, _options), do: :error
  def handle_incoming(%{"id" => ""}, _options), do: :error
387
  # length of https:// = 8, should validate better, but good enough for now.
388
389
  def handle_incoming(%{"id" => id}, _options) when not (is_binary(id) and length(id) > 8),
    do: :error
390

391
392
393
  # TODO: validate those with a Ecto scheme
  # - tags
  # - emoji
394
395
396
397
  def handle_incoming(
        %{"type" => "Create", "object" => %{"type" => objtype} = object} = data,
        options
      )
398
      when objtype in ["Article", "Note", "Video", "Page", "Question", "Answer"] do
399
    actor = Containment.get_actor(data)
400
401
402
403

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

405
    with nil <- Activity.get_create_by_object_ap_id(object["id"]),
406
         {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
407
408
      options = Keyword.put(options, :depth, (options[:depth] || 0) + 1)
      object = fix_object(data["object"], options)
409

410
411
412
413
      params = %{
        to: data["to"],
        object: object,
        actor: user,
lain's avatar
lain committed
414
        context: object["conversation"],
415
416
        local: false,
        published: data["published"],
lain's avatar
lain committed
417
418
419
        additional:
          Map.take(data, [
            "cc",
420
            "directMessage",
lain's avatar
lain committed
421
422
            "id"
          ])
423
424
425
426
      }

      ActivityPub.create(params)
    else
lain's avatar
lain committed
427
      %Activity{} = activity -> {:ok, activity}
428
429
430
431
      _e -> :error
    end
  end

lain's avatar
lain committed
432
  def handle_incoming(
433
434
        %{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data,
        _options
lain's avatar
lain committed
435
      ) do
436
437
438
439
    with %User{local: true} = followed <-
           User.get_cached_by_ap_id(Containment.get_actor(%{"actor" => followed})),
         {:ok, %User{} = follower} <-
           User.get_or_fetch_by_ap_id(Containment.get_actor(%{"actor" => follower})),
440
         {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
441
      with deny_follow_blocked <- Pleroma.Config.get([:user, :deny_follow_blocked]),
442
           {_, false} <- {:user_blocked, User.blocks?(followed, follower) && deny_follow_blocked},
443
444
           {_, false} <- {:user_locked, User.locked?(followed)},
           {_, {:ok, follower}} <- {:follow, User.follow(follower, followed)},
445
446
           {_, {:ok, _}} <-
             {:follow_state_update, Utils.update_follow_state_for_all(activity, "accept")} do
kaniini's avatar
kaniini committed
447
448
        ActivityPub.accept(%{
          to: [follower.ap_id],
449
          actor: followed,
kaniini's avatar
kaniini committed
450
451
452
          object: data,
          local: true
        })
453
454
      else
        {:user_blocked, true} ->
455
          {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
456
457
458
459
460
461
462
463
464

          ActivityPub.reject(%{
            to: [follower.ap_id],
            actor: followed,
            object: data,
            local: true
          })

        {:follow, {:error, _}} ->
465
          {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
466
467
468
469
470
471
472
473
474
475

          ActivityPub.reject(%{
            to: [follower.ap_id],
            actor: followed,
            object: data,
            local: true
          })

        {:user_locked, true} ->
          :noop
476
      end
lain's avatar
lain committed
477

478
479
      {:ok, activity}
    else
480
481
      _e ->
        :error
482
483
484
    end
  end

485
  def handle_incoming(
486
487
        %{"type" => "Accept", "object" => follow_object, "actor" => _actor, "id" => _id} = data,
        _options
488
      ) do
489
    with actor <- Containment.get_actor(data),
490
         {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
491
         {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
492
         {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "accept"),
493
         %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
494
495
496
497
498
499
500
501
         {:ok, _follower} = User.follow(follower, followed) do
      ActivityPub.accept(%{
        to: follow_activity.data["to"],
        type: "Accept",
        actor: followed,
        object: follow_activity.data["id"],
        local: false
      })
502
503
    else
      _e -> :error
504
505
506
507
    end
  end

  def handle_incoming(
508
509
        %{"type" => "Reject", "object" => follow_object, "actor" => _actor, "id" => _id} = data,
        _options
510
      ) do
511
    with actor <- Containment.get_actor(data),
512
         {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
513
         {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
514
         {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "reject"),
515
         %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
lain's avatar
lain committed
516
         {:ok, activity} <-
517
           ActivityPub.reject(%{
lain's avatar
lain committed
518
             to: follow_activity.data["to"],
519
             type: "Reject",
520
             actor: followed,
lain's avatar
lain committed
521
522
523
             object: follow_activity.data["id"],
             local: false
           }) do
524
525
      User.unfollow(follower, followed)

526
      {:ok, activity}
527
528
    else
      _e -> :error
529
530
531
    end
  end

lain's avatar
lain committed
532
  def handle_incoming(
533
534
        %{"type" => "Like", "object" => object_id, "actor" => _actor, "id" => id} = data,
        _options
lain's avatar
lain committed
535
      ) do
536
    with actor <- Containment.get_actor(data),
537
         {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
538
         {:ok, object} <- get_obj_helper(object_id),
feld's avatar
feld committed
539
         {:ok, activity, _object} <- ActivityPub.like(actor, object, id, false) do
lain's avatar
lain committed
540
541
542
543
544
545
      {:ok, activity}
    else
      _e -> :error
    end
  end

lain's avatar
lain committed
546
  def handle_incoming(
547
548
        %{"type" => "Announce", "object" => object_id, "actor" => _actor, "id" => id} = data,
        _options
lain's avatar
lain committed
549
      ) do
550
    with actor <- Containment.get_actor(data),
551
         {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
552
         {:ok, object} <- get_obj_helper(object_id),
lain's avatar
lain committed
553
         public <- Visibility.is_public?(data),
554
         {:ok, activity, _object} <- ActivityPub.announce(actor, object, id, false, public) do
lain's avatar
lain committed
555
556
557
558
559
560
      {:ok, activity}
    else
      _e -> :error
    end
  end

lain's avatar
lain committed
561
  def handle_incoming(
562
        %{"type" => "Update", "object" => %{"type" => object_type} = object, "actor" => actor_id} =
563
564
          data,
        _options
565
566
      )
      when object_type in ["Person", "Application", "Service", "Organization"] do
minibikini's avatar
minibikini committed
567
    with %User{ap_id: ^actor_id} = actor <- User.get_cached_by_ap_id(object["id"]) do
lain's avatar
lain committed
568
569
      {:ok, new_user_data} = ActivityPub.user_data_from_user_object(object)

rinpatch's avatar
rinpatch committed
570
571
      banner = new_user_data[:info][:banner]
      locked = new_user_data[:info][:locked] || false
572
573
574
575
576
577
      attachment = get_in(new_user_data, [:info, :source_data, "attachment"]) || []

      fields =
        attachment
        |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
        |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
lain's avatar
lain committed
578
579
580
581

      update_data =
        new_user_data
        |> Map.take([:name, :bio, :avatar])
582
        |> Map.put(:info, %{banner: banner, locked: locked, fields: fields})
lain's avatar
lain committed
583
584

      actor
585
      |> User.upgrade_changeset(update_data, true)
lain's avatar
lain committed
586
      |> User.update_and_set_cache()
lain's avatar
lain committed
587

lain's avatar
lain committed
588
589
590
591
592
593
594
      ActivityPub.update(%{
        local: false,
        to: data["to"] || [],
        cc: data["cc"] || [],
        object: object,
        actor: actor_id
      })
lain's avatar
lain committed
595
596
597
598
599
600
601
    else
      e ->
        Logger.error(e)
        :error
    end
  end

602
603
604
605
606
  # 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
607
  def handle_incoming(
608
        %{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => _id} = data,
609
        _options
lain's avatar
lain committed
610
      ) do
lain's avatar
lain committed
611
    object_id = Utils.get_ap_id(object_id)
lain's avatar
lain committed
612

613
    with actor <- Containment.get_actor(data),
614
         {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
615
         {:ok, object} <- get_obj_helper(object_id),
616
         :ok <- Containment.contain_origin(actor.ap_id, object.data),
lain's avatar
lain committed
617
618
619
         {:ok, activity} <- ActivityPub.delete(object, false) do
      {:ok, activity}
    else
620
621
622
      nil ->
        case User.get_cached_by_ap_id(object_id) do
          %User{ap_id: ^actor} = user ->
623
            User.delete(user)
624
625
626
627
628
629
630

          nil ->
            :error
        end

      _e ->
        :error
lain's avatar
lain committed
631
632
633
    end
  end

634
  def handle_incoming(
635
636
        %{
          "type" => "Undo",
637
          "object" => %{"type" => "Announce", "object" => object_id},
Maksim's avatar
Maksim committed
638
          "actor" => _actor,
639
          "id" => id
640
641
        } = data,
        _options
642
      ) do
643
    with actor <- Containment.get_actor(data),
644
         {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
645
         {:ok, object} <- get_obj_helper(object_id),
646
         {:ok, activity, _} <- ActivityPub.unannounce(actor, object, id, false) do
647
648
      {:ok, activity}
    else
Thog's avatar
Thog committed
649
      _e -> :error
650
651
652
    end
  end

normandy's avatar
normandy committed
653
654
655
656
657
658
  def handle_incoming(
        %{
          "type" => "Undo",
          "object" => %{"type" => "Follow", "object" => followed},
          "actor" => follower,
          "id" => id
659
660
        } = _data,
        _options
normandy's avatar
normandy committed
661
      ) do
normandy's avatar
normandy committed
662
    with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
663
         {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
normandy's avatar
normandy committed
664
         {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
normandy's avatar
normandy committed
665
666
667
      User.unfollow(follower, followed)
      {:ok, activity}
    else
Maksim's avatar
Maksim committed
668
      _e -> :error
normandy's avatar
normandy committed
669
670
671
    end
  end

normandy's avatar
normandy committed
672
673
674
675
676
677
  def handle_incoming(
        %{
          "type" => "Undo",
          "object" => %{"type" => "Block", "object" => blocked},
          "actor" => blocker,
          "id" => id
678
679
        } = _data,
        _options
normandy's avatar
normandy committed
680
      ) do
681
    with %User{local: true} = blocked <- User.get_cached_by_ap_id(blocked),
682
         {:ok, %User{} = blocker} <- User.get_or_fetch_by_ap_id(blocker),
normandy's avatar
normandy committed
683
         {:ok, activity} <- ActivityPub.unblock(blocker, blocked, id, false) do
normandy's avatar
normandy committed
684
      User.unblock(blocker, blocked)
normandy's avatar
normandy committed
685
686
      {:ok, activity}
    else
Maksim's avatar
Maksim committed
687
      _e -> :error
normandy's avatar
normandy committed
688
689
690
    end
  end

691
  def handle_incoming(
692
693
        %{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = _data,
        _options
694
      ) do
695
    with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked),
0x1C3B00DA's avatar
0x1C3B00DA committed
696
         {:ok, %User{} = blocker} = User.get_or_fetch_by_ap_id(blocker),
normandy's avatar
normandy committed
697
         {:ok, activity} <- ActivityPub.block(blocker, blocked, id, false) do
698
      User.unfollow(blocker, blocked)
699
      User.block(blocker, blocked)
normandy's avatar
normandy committed
700
701
      {:ok, activity}
    else
Maksim's avatar
Maksim committed
702
      _e -> :error
normandy's avatar
normandy committed
703
704
    end
  end
705

Thog's avatar
Thog committed
706
707
708
709
  def handle_incoming(
        %{
          "type" => "Undo",
          "object" => %{"type" => "Like", "object" => object_id},
Maksim's avatar
Maksim committed
710
          "actor" => _actor,
Thog's avatar
Thog committed
711
          "id" => id
712
713
        } = data,
        _options
Thog's avatar
Thog committed
714
      ) do
715
    with actor <- Containment.get_actor(data),
716
         {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
717
         {:ok, object} <- get_obj_helper(object_id),
Thog's avatar
Thog committed
718
719
720
         {:ok, activity, _, _} <- ActivityPub.unlike(actor, object, id, false) do
      {:ok, activity}
    else
Thog's avatar
Thog committed
721
      _e -> :error
Thog's avatar
Thog committed
722
723
724
    end
  end

725
  def handle_incoming(_, _), do: :error
726

727
  @spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
728
  def get_obj_helper(id, options \\ []) do
Maksim's avatar
Maksim committed
729
730
731
    case Object.normalize(id, true, options) do
      %Object{} = object -> {:ok, object}
      _ -> nil
732
    end
733
734
  end

735
736
737
738
  def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
    with false <- String.starts_with?(in_reply_to, "http"),
         {:ok, %{data: replied_to_object}} <- get_obj_helper(in_reply_to) do
      Map.put(object, "inReplyTo", replied_to_object["external_url"] || in_reply_to)
739
740
741
742
    else
      _e -> object
    end
  end
lain's avatar
lain committed
743

744
745
746
  def set_reply_to_uri(obj), do: obj

  # Prepares the object of an outgoing create activity.
lain's avatar
lain committed
747
748
  def prepare_object(object) do
    object
lain's avatar
lain committed
749
    |> set_sensitive
lain's avatar
lain committed
750
    |> add_hashtags
lain's avatar
lain committed
751
    |> add_mention_tags
lain's avatar
lain committed
752
    |> add_emoji_tags
lain's avatar
lain committed
753
    |> add_attributed_to
lain's avatar
lain committed
754
    |> prepare_attachments
lain's avatar
lain committed
755
    |> set_conversation
756
    |> set_reply_to_uri
757
758
    |> strip_internal_fields
    |> strip_internal_tags
759
    |> set_type
lain's avatar
lain committed
760
761
  end

feld's avatar
feld committed
762
763
764
765
  #  @doc
  #  """
  #  internal -> Mastodon
  #  """
lain's avatar
lain committed
766

767
  def prepare_outgoing(%{"type" => "Create", "object" => object_id} = data) do
lain's avatar
lain committed
768
    object =
minibikini's avatar
minibikini committed
769
770
771
      object_id
      |> Object.normalize()
      |> Map.get(:data)
lain's avatar
lain committed
772
773
774
775
776
      |> prepare_object

    data =
      data
      |> Map.put("object", object)
lain's avatar
lain committed
777
      |> Map.merge(Utils.make_json_ld_header())
minibikini's avatar
minibikini committed
778
      |> Map.delete("bcc")
lain's avatar
lain committed
779
780
781
782

    {:ok, data}
  end

kaniini's avatar
kaniini committed
783
784
785
  # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
  # because of course it does.
  def prepare_outgoing(%{"type" => "Accept"} = data) do
786
    with follow_activity <- Activity.normalize(data["object"]) do
kaniini's avatar
kaniini committed
787
788
789
790
791
792
793
794
795
796
      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
797
        |> Map.merge(Utils.make_json_ld_header())
kaniini's avatar
kaniini committed
798
799
800
801
802

      {:ok, data}
    end
  end

803
  def prepare_outgoing(%{"type" => "Reject"} = data) do
804
    with follow_activity <- Activity.normalize(data["object"]) do
805
806
807
808
809
810
811
812
813
814
      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
815
        |> Map.merge(Utils.make_json_ld_header())
816
817
818
819
820

      {:ok, data}
    end
  end

feld's avatar
feld committed
821
  def prepare_outgoing(%{"type" => _type} = data) do
lain's avatar
lain committed
822
823
    data =
      data
824
      |> strip_internal_fields
lain's avatar
lain committed
825
      |> maybe_fix_object_url
lain's avatar
lain committed
826
      |> Map.merge(Utils.make_json_ld_header())
827
828
829
830

    {:ok, data}
  end

831
832
833
834
835
836
  def maybe_fix_object_url(%{"object" => object} = data) when is_binary(object) do
    with false <- String.starts_with?(object, "http"),
         {:fetch, {:ok, relative_object}} <- {:fetch, get_obj_helper(object)},
         %{data: %{"external_url" => external_url}} when not is_nil(external_url) <-
           relative_object do
      Map.put(data, "object", external_url)
837
    else
838
839
840
841
842
843
      {:fetch, e} ->
        Logger.error("Couldn't fetch #{object} #{inspect(e)}")
        data

      _ ->
        data
844
845
846
    end
  end

847
848
  def maybe_fix_object_url(data), do: data

lain's avatar
lain committed
849
  def add_hashtags(object) do
lain's avatar
lain committed
850
851
    tags =
      (object["tag"] || [])
852
853
854
855
856
857
858
859
860
861
862
863
      |> Enum.map(fn
        # Expand internal representation tags into AS2 tags.
        tag when is_binary(tag) ->
          %{
            "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
            "name" => "##{tag}",
            "type" => "Hashtag"
          }

        # Do not process tags which are already AS2 tag objects.
        tag when is_map(tag) ->
          tag
lain's avatar
lain committed
864
      end)
lain's avatar
lain committed
865

866
    Map.put(object, "tag", tags)
lain's avatar
lain committed
867
868
  end

lain's avatar
lain committed
869
  def add_mention_tags(object) do
lain's avatar
lain committed
870
    mentions =
871
872
      object
      |> Utils.get_notified_from_object()
lain's avatar
lain committed
873
874
875
      |> Enum.map(fn user ->
        %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"}
      end)
lain's avatar
lain committed
876

lain's avatar
lain committed
877
    tags = object["tag"] || []
lain's avatar
lain committed
878

879
    Map.put(object, "tag", tags ++ mentions)
lain's avatar
lain committed
880
881
  end

Haelwenn's avatar
Haelwenn committed
882
883
884
  def add_emoji_tags(%User{info: %{"emoji" => _emoji} = user_info} = object) do
    user_info = add_emoji_tags(user_info)

885
    Map.put(object, :info, user_info)
Haelwenn's avatar
Haelwenn committed
886
887
  end

lain's avatar
lain committed
888
  # TODO: we should probably send mtime instead of unix epoch time for updated
Haelwenn's avatar
Haelwenn committed
889
  def add_emoji_tags(%{"emoji" => emoji} = object) do
lain's avatar
lain committed
890
    tags = object["tag"] || []
lain's avatar
lain committed
891
892

    out =
893
      Enum.map(emoji, fn {name, url} ->
lain's avatar
lain committed
894
895
896
897
898
899
900
901
        %{
          "icon" => %{"url" => url, "type" => "Image"},
          "name" => ":" <> name <> ":",
          "type" => "Emoji",
          "updated" => "1970-01-01T00:00:00Z",
          "id" => url
        }
      end)
lain's avatar
lain committed
902

903
    Map.put(object, "tag", tags ++ out)
lain's avatar
lain committed
904
905
  end

906
  def add_emoji_tags(object), do: object
Haelwenn's avatar
Haelwenn committed
907

lain's avatar
lain committed
908
909
910
911
  def set_conversation(object) do
    Map.put(object, "conversation", object["context"])
  end

lain's avatar
lain committed
912
913