1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Activity do
9 alias Pleroma.Notification
15 @type t :: %__MODULE__{}
16 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
18 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
19 @mastodon_notification_types %{
20 "Create" => "mention",
22 "Announce" => "reblog",
26 @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
30 schema "activities" do
32 field(:local, :boolean, default: true)
33 field(:actor, :string)
34 field(:recipients, {:array, :string}, default: [])
35 has_many(:notifications, Notification, on_delete: :delete_all)
37 # Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
38 # The foreign key is embedded in a jsonb field.
40 # To use it, you probably want to do an inner join and a preload:
43 # |> join(:inner, [activity], o in Object,
44 # on: fragment("(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
45 # o.data, activity.data, activity.data))
46 # |> preload([activity, object], [object: object])
49 # As a convenience, Activity.with_preloaded_object() sets up an inner join and preload for the
51 has_one(:object, Object, on_delete: :nothing, foreign_key: :id)
56 def with_preloaded_object(query) do
64 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
70 |> preload([activity, object], object: object)
73 def get_by_ap_id(ap_id) do
77 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
82 def get_by_ap_id_with_object(ap_id) do
86 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
87 left_join: o in Object,
90 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
102 |> where([a], a.id == ^id)
103 |> restrict_disabled_users()
107 def get_by_id_with_object(id) do
108 from(activity in Activity,
109 where: activity.id == ^id,
110 inner_join: o in Object,
113 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
123 def by_object_ap_id(ap_id) do
125 activity in Activity,
128 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
136 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
138 activity in Activity,
141 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
146 where: fragment("(?)->>'type' = 'Create'", activity.data)
150 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
152 activity in Activity,
155 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
160 where: fragment("(?)->>'type' = 'Create'", activity.data)
164 def create_by_object_ap_id(_), do: nil
166 def get_all_create_by_object_ap_id(ap_id) do
167 Repo.all(create_by_object_ap_id(ap_id))
170 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
171 create_by_object_ap_id(ap_id)
172 |> restrict_disabled_users()
176 def get_create_by_object_ap_id(_), do: nil
178 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
180 activity in Activity,
183 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
188 where: fragment("(?)->>'type' = 'Create'", activity.data),
189 inner_join: o in Object,
192 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
201 def create_by_object_ap_id_with_object(_), do: nil
203 def get_create_by_object_ap_id_with_object(ap_id) do
205 |> create_by_object_ap_id_with_object()
209 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
210 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
211 def normalize(_), do: nil
213 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
214 get_create_by_object_ap_id(ap_id)
217 def get_in_reply_to_activity(_), do: nil
219 def delete_by_ap_id(id) when is_binary(id) do
225 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
230 def delete_by_ap_id(_), do: nil
232 for {ap_type, type} <- @mastodon_notification_types do
233 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
237 def mastodon_notification_type(%Activity{}), do: nil
239 def from_mastodon_notification_type(type) do
240 Map.get(@mastodon_to_ap_notification_types, type)
243 def all_by_actor_and_id(actor, status_ids \\ [])
244 def all_by_actor_and_id(_actor, []), do: []
246 def all_by_actor_and_id(actor, status_ids) do
248 |> where([s], s.id in ^status_ids)
249 |> where([s], s.actor == ^actor)
253 def increase_replies_count(id) do
261 jsonb_set(?, '{object, repliesCount}',
262 (coalesce((?->'object'->>'repliesCount')::int, 0) + 1)::varchar::jsonb, true)
269 |> Repo.update_all([])
271 {1, [activity]} -> activity
272 _ -> {:error, "Not found"}
276 def decrease_replies_count(id) do
284 jsonb_set(?, '{object, repliesCount}',
285 (greatest(0, (?->'object'->>'repliesCount')::int - 1))::varchar::jsonb, true)
292 |> Repo.update_all([])
294 {1, [activity]} -> activity
295 _ -> {:error, "Not found"}
299 def restrict_disabled_users(query) do
300 from(activity in query,
303 "? not in (SELECT ap_id FROM users WHERE info->'disabled' @> 'true')",