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