summaryrefslogtreecommitdiff
path: root/lib/pleroma/web/activity_pub/mrf/activity_expiration_policy.ex
blob: 7b4c78e0f86eb7e9afdec34563c78dcc25339611 (plain)
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
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy do
  @moduledoc "Adds expiration to all local Create activities"
  @behaviour Pleroma.Web.ActivityPub.MRF

  @impl true
  def filter(activity) do
    activity =
      if note?(activity) and local?(activity) do
        maybe_add_expiration(activity)
      else
        activity
      end

    {:ok, activity}
  end

  @impl true
  def describe, do: {:ok, %{}}

  defp local?(%{"actor" => actor}) do
    String.starts_with?(actor, Pleroma.Web.Endpoint.url())
  end

  defp note?(activity) do
    match?(%{"type" => "Create", "object" => %{"type" => "Note"}}, activity)
  end

  defp maybe_add_expiration(activity) do
    days = Pleroma.Config.get([:mrf_activity_expiration, :days], 365)
    expires_at = NaiveDateTime.utc_now() |> Timex.shift(days: days)

    with %{"expires_at" => existing_expires_at} <- activity,
         :lt <- NaiveDateTime.compare(existing_expires_at, expires_at) do
      activity
    else
      _ -> Map.put(activity, "expires_at", expires_at)
    end
  end
end