Merge remote-tracking branch 'pleroma/develop' into feature/disable-account
[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
8 alias Pleroma.Repo
9 alias Pleroma.Activity
10 alias Pleroma.Notification
11
12 import Ecto.Query
13
14 @type t :: %__MODULE__{}
15 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
16
17 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
18 @mastodon_notification_types %{
19 "Create" => "mention",
20 "Follow" => "follow",
21 "Announce" => "reblog",
22 "Like" => "favourite"
23 }
24
25 schema "activities" do
26 field(:data, :map)
27 field(:local, :boolean, default: true)
28 field(:actor, :string)
29 field(:recipients, {:array, :string})
30 has_many(:notifications, Notification, on_delete: :delete_all)
31
32 timestamps()
33 end
34
35 def get_by_ap_id(ap_id) do
36 Repo.one(
37 from(
38 activity in Activity,
39 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
40 )
41 )
42 end
43
44 def get_by_id(id) do
45 Activity
46 |> where([a], a.id == ^id)
47 |> restrict_disabled_users()
48 |> Repo.one()
49 end
50
51 def by_object_ap_id(ap_id) do
52 from(
53 activity in Activity,
54 where:
55 fragment(
56 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
57 activity.data,
58 activity.data,
59 ^to_string(ap_id)
60 )
61 )
62 end
63
64 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
65 from(
66 activity in Activity,
67 where:
68 fragment(
69 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
70 activity.data,
71 activity.data,
72 ^ap_ids
73 ),
74 where: fragment("(?)->>'type' = 'Create'", activity.data)
75 )
76 end
77
78 def create_by_object_ap_id(ap_id) do
79 from(
80 activity in Activity,
81 where:
82 fragment(
83 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
84 activity.data,
85 activity.data,
86 ^to_string(ap_id)
87 ),
88 where: fragment("(?)->>'type' = 'Create'", activity.data)
89 )
90 end
91
92 def get_all_create_by_object_ap_id(ap_id) do
93 Repo.all(create_by_object_ap_id(ap_id))
94 end
95
96 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
97 create_by_object_ap_id(ap_id)
98 |> restrict_disabled_users()
99 |> Repo.one()
100 end
101
102 def get_create_by_object_ap_id(_), do: nil
103
104 def normalize(obj) when is_map(obj), do: Activity.get_by_ap_id(obj["id"])
105 def normalize(ap_id) when is_binary(ap_id), do: Activity.get_by_ap_id(ap_id)
106 def normalize(_), do: nil
107
108 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
109 get_create_by_object_ap_id(ap_id)
110 end
111
112 def get_in_reply_to_activity(_), do: nil
113
114 for {ap_type, type} <- @mastodon_notification_types do
115 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
116 do: unquote(type)
117 end
118
119 def mastodon_notification_type(%Activity{}), do: nil
120
121 def all_by_actor_and_id(actor, status_ids \\ [])
122 def all_by_actor_and_id(_actor, []), do: []
123
124 def all_by_actor_and_id(actor, status_ids) do
125 Activity
126 |> where([s], s.id in ^status_ids)
127 |> where([s], s.actor == ^actor)
128 |> Repo.all()
129 end
130
131 def restrict_disabled_users(query) do
132 from(activity in query,
133 where:
134 fragment(
135 "? not in (SELECT ap_id FROM users WHERE info->'disabled' @> 'true')",
136 activity.actor
137 )
138 )
139 end
140 end