Test FlakeID old id compat & Ecto type
[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 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
12
13 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
14 @mastodon_notification_types %{
15 "Create" => "mention",
16 "Follow" => "follow",
17 "Announce" => "reblog",
18 "Like" => "favourite"
19 }
20
21 schema "activities" do
22 field(:data, :map)
23 field(:local, :boolean, default: true)
24 field(:actor, :string)
25 field(:recipients, {:array, :string})
26 has_many(:notifications, Notification, on_delete: :delete_all)
27
28 timestamps()
29 end
30
31 def get_by_ap_id(ap_id) do
32 Repo.one(
33 from(
34 activity in Activity,
35 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
36 )
37 )
38 end
39
40 def get_by_id(id) do
41 Repo.get(Activity, id)
42 end
43
44 # TODO:
45 # Go through these and fix them everywhere.
46 # Wrong name, only returns create activities
47 def all_by_object_ap_id_q(ap_id) do
48 from(
49 activity in Activity,
50 where:
51 fragment(
52 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
53 activity.data,
54 activity.data,
55 ^to_string(ap_id)
56 ),
57 where: fragment("(?)->>'type' = 'Create'", activity.data)
58 )
59 end
60
61 # Wrong name, returns all.
62 def all_non_create_by_object_ap_id_q(ap_id) do
63 from(
64 activity in Activity,
65 where:
66 fragment(
67 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
68 activity.data,
69 activity.data,
70 ^to_string(ap_id)
71 )
72 )
73 end
74
75 # Wrong name plz fix thx
76 def all_by_object_ap_id(ap_id) do
77 Repo.all(all_by_object_ap_id_q(ap_id))
78 end
79
80 def create_activity_by_object_id_query(ap_ids) do
81 from(
82 activity in Activity,
83 where:
84 fragment(
85 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
86 activity.data,
87 activity.data,
88 ^ap_ids
89 ),
90 where: fragment("(?)->>'type' = 'Create'", activity.data)
91 )
92 end
93
94 def get_create_activity_by_object_ap_id(ap_id) when is_binary(ap_id) do
95 create_activity_by_object_id_query([ap_id])
96 |> Repo.one()
97 end
98
99 def get_create_activity_by_object_ap_id(_), do: nil
100
101 def normalize(obj) when is_map(obj), do: Activity.get_by_ap_id(obj["id"])
102 def normalize(ap_id) when is_binary(ap_id), do: Activity.get_by_ap_id(ap_id)
103 def normalize(_), do: nil
104
105 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
106 get_create_activity_by_object_ap_id(ap_id)
107 end
108
109 def get_in_reply_to_activity(_), do: nil
110
111 for {ap_type, type} <- @mastodon_notification_types do
112 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
113 do: unquote(type)
114 end
115
116 def mastodon_notification_type(%Activity{}), do: nil
117 end