47562306d136a76fc10917926025ac4c015fbee5
[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 get_by_id(id) do
40 Repo.get(Activity, id)
41 end
42 # TODO:
43 # Go through these and fix them everywhere.
44 # Wrong name, only returns create activities
45 def all_by_object_ap_id_q(ap_id) do
46 from(
47 activity in Activity,
48 where:
49 fragment(
50 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
51 activity.data,
52 activity.data,
53 ^to_string(ap_id)
54 ),
55 where: fragment("(?)->>'type' = 'Create'", activity.data)
56 )
57 end
58
59 # Wrong name, returns all.
60 def all_non_create_by_object_ap_id_q(ap_id) do
61 from(
62 activity in Activity,
63 where:
64 fragment(
65 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
66 activity.data,
67 activity.data,
68 ^to_string(ap_id)
69 )
70 )
71 end
72
73 # Wrong name plz fix thx
74 def all_by_object_ap_id(ap_id) do
75 Repo.all(all_by_object_ap_id_q(ap_id))
76 end
77
78 def create_activity_by_object_id_query(ap_ids) do
79 from(
80 activity in Activity,
81 where:
82 fragment(
83 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
84 activity.data,
85 activity.data,
86 ^ap_ids
87 ),
88 where: fragment("(?)->>'type' = 'Create'", activity.data)
89 )
90 end
91
92 def get_create_activity_by_object_ap_id(ap_id) when is_binary(ap_id) do
93 create_activity_by_object_id_query([ap_id])
94 |> Repo.one()
95 end
96
97 def get_create_activity_by_object_ap_id(_), do: nil
98
99 def normalize(obj) when is_map(obj), do: Activity.get_by_ap_id(obj["id"])
100 def normalize(ap_id) when is_binary(ap_id), do: Activity.get_by_ap_id(ap_id)
101 def normalize(_), do: nil
102
103 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
104 get_create_activity_by_object_ap_id(ap_id)
105 end
106
107 def get_in_reply_to_activity(_), do: nil
108
109 for {ap_type, type} <- @mastodon_notification_types do
110 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
111 do: unquote(type)
112 end
113
114 def mastodon_notification_type(%Activity{}), do: nil
115 end