81079ec3e64002f1b6eff8e3cd0638ca4f127208
[akkoma] / lib / pleroma / activity.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Activity do
6 use Ecto.Schema
7 alias Pleroma.{Repo, Activity, Notification}
8 import Ecto.Query
9
10 @type t :: %__MODULE__{}
11
12 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
13 @mastodon_notification_types %{
14 "Create" => "mention",
15 "Follow" => "follow",
16 "Announce" => "reblog",
17 "Like" => "favourite"
18 }
19
20 schema "activities" do
21 field(:data, :map)
22 field(:local, :boolean, default: true)
23 field(:actor, :string)
24 field(:recipients, {:array, :string})
25 has_many(:notifications, Notification, on_delete: :delete_all)
26
27 timestamps()
28 end
29
30 def get_by_ap_id(ap_id) do
31 Repo.one(
32 from(
33 activity in Activity,
34 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
35 )
36 )
37 end
38
39 def by_object_ap_id(ap_id) do
40 from(
41 activity in Activity,
42 where:
43 fragment(
44 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
45 activity.data,
46 activity.data,
47 ^to_string(ap_id)
48 )
49 )
50 end
51
52
53 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
54 from(
55 activity in Activity,
56 where:
57 fragment(
58 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
59 activity.data,
60 activity.data,
61 ^ap_ids
62 ),
63 where: fragment("(?)->>'type' = 'Create'", activity.data)
64 )
65 end
66
67 def create_by_object_ap_id(ap_id) do
68 from(
69 activity in Activity,
70 where:
71 fragment(
72 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
73 activity.data,
74 activity.data,
75 ^to_string(ap_id)
76 ),
77 where: fragment("(?)->>'type' = 'Create'", activity.data)
78 )
79 end
80
81 def get_all_create_by_object_ap_id(ap_id) do
82 Repo.all(create_by_object_ap_id(ap_id))
83 end
84
85 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
86 create_by_object_ap_id(ap_id)
87 |> Repo.one()
88 end
89
90 def get_create_by_object_ap_id(_), do: nil
91
92 def normalize(obj) when is_map(obj), do: Activity.get_by_ap_id(obj["id"])
93 def normalize(ap_id) when is_binary(ap_id), do: Activity.get_by_ap_id(ap_id)
94 def normalize(_), do: nil
95
96 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
97 get_create_by_object_ap_id(ap_id)
98 end
99
100 def get_in_reply_to_activity(_), do: nil
101
102 for {ap_type, type} <- @mastodon_notification_types do
103 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
104 do: unquote(type)
105 end
106
107 def mastodon_notification_type(%Activity{}), do: nil
108 end