Fix Mastodon API when actor's nickname is null
[akkoma] / lib / pleroma / activity.ex
1 defmodule Pleroma.Activity do
2 use Ecto.Schema
3 alias Pleroma.{Repo, Activity, Notification}
4 import Ecto.Query
5
6 schema "activities" do
7 field(:data, :map)
8 field(:local, :boolean, default: true)
9 field(:actor, :string)
10 field(:recipients, {:array, :string})
11 field(:recipients_to, {:array, :string})
12 field(:recipients_cc, {:array, :string})
13 has_many(:notifications, Notification, on_delete: :delete_all)
14
15 timestamps()
16 end
17
18 def get_by_ap_id(ap_id) do
19 Repo.one(
20 from(
21 activity in Activity,
22 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
23 )
24 )
25 end
26
27 # TODO:
28 # Go through these and fix them everywhere.
29 # Wrong name, only returns create activities
30 def all_by_object_ap_id_q(ap_id) do
31 from(
32 activity in Activity,
33 where:
34 fragment(
35 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
36 activity.data,
37 activity.data,
38 ^to_string(ap_id)
39 ),
40 where: fragment("(?)->>'type' = 'Create'", activity.data)
41 )
42 end
43
44 # Wrong name, returns all.
45 def all_non_create_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 )
56 end
57
58 # Wrong name plz fix thx
59 def all_by_object_ap_id(ap_id) do
60 Repo.all(all_by_object_ap_id_q(ap_id))
61 end
62
63 def create_activity_by_object_id_query(ap_ids) do
64 from(
65 activity in Activity,
66 where:
67 fragment(
68 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
69 activity.data,
70 activity.data,
71 ^ap_ids
72 ),
73 where: fragment("(?)->>'type' = 'Create'", activity.data)
74 )
75 end
76
77 def get_create_activity_by_object_ap_id(ap_id) when is_binary(ap_id) do
78 create_activity_by_object_id_query([ap_id])
79 |> Repo.one()
80 end
81
82 def get_create_activity_by_object_ap_id(_), do: nil
83
84 def normalize(obj) when is_map(obj), do: Activity.get_by_ap_id(obj["id"])
85 def normalize(ap_id) when is_binary(ap_id), do: Activity.get_by_ap_id(ap_id)
86 def normalize(_), do: nil
87 end