notify.ex 1.89 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Webhook.Notify do
  alias Phoenix.View
  alias Pleroma.Activity
  alias Pleroma.User
  alias Pleroma.Web.AdminAPI.Report
  alias Pleroma.Webhook

  def trigger_webhooks(%Activity{} = activity, :"report.created" = type) do
    webhooks = Webhook.get_by_type(type)

    Enum.each(webhooks, fn webhook ->
      ConcurrentLimiter.limit(Webhook.Notify, fn ->
        Task.start(fn -> report_created(webhook, activity) end)
      end)
    end)
  end

  def trigger_webhooks(%User{} = user, :"account.created" = type) do
    webhooks = Webhook.get_by_type(type)

    Enum.each(webhooks, fn webhook ->
      ConcurrentLimiter.limit(Webhook.Notify, fn ->
        Task.start(fn -> account_created(webhook, user) end)
      end)
    end)
  end

  def report_created(%Webhook{} = webhook, %Activity{} = report) do
    object =
      View.render(
        Pleroma.Web.MastodonAPI.Admin.ReportView,
        "show.json",
        Report.extract_report_info(report)
      )

    deliver(webhook, object, :"report.created")
  end

  def account_created(%Webhook{} = webhook, %User{} = user) do
    object =
      View.render(
        Pleroma.Web.MastodonAPI.Admin.AccountView,
        "show.json",
        user: user
      )

    deliver(webhook, object, :"account.created")
  end

  defp deliver(%Webhook{url: url, secret: secret}, object, type) do
    body =
      View.render_to_string(Pleroma.Web.AdminAPI.WebhookView, "event.json",
        type: type,
        object: object
      )

    headers = [
      {"Content-Type", "application/json"},
      {"X-Hub-Signature", "sha256=#{signature(body, secret)}"}
    ]

    Pleroma.HTTP.post(url, body, headers)
  end

  defp signature(body, secret) do
    :crypto.mac(:hmac, :sha256, secret, body) |> Base.encode16()
  end
end