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})
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')",
101 Repo.get(Activity, id)
104 def get_by_id_with_object(id) do
105 from(activity in Activity,
106 where: activity.id == ^id,
107 inner_join: o in Object,
110 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
120 def by_object_ap_id(ap_id) do
122 activity in Activity,
125 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
133 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
135 activity in Activity,
138 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
143 where: fragment("(?)->>'type' = 'Create'", activity.data)
147 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
149 activity in Activity,
152 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
157 where: fragment("(?)->>'type' = 'Create'", activity.data)
161 def create_by_object_ap_id(_), do: nil
163 def get_all_create_by_object_ap_id(ap_id) do
164 Repo.all(create_by_object_ap_id(ap_id))
167 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
168 create_by_object_ap_id(ap_id)
172 def get_create_by_object_ap_id(_), do: nil
174 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
176 activity in Activity,
179 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
184 where: fragment("(?)->>'type' = 'Create'", activity.data),
185 inner_join: o in Object,
188 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
197 def create_by_object_ap_id_with_object(_), do: nil
199 def get_create_by_object_ap_id_with_object(ap_id) do
201 |> create_by_object_ap_id_with_object()
205 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
206 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
207 def normalize(_), do: nil
209 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
210 get_create_by_object_ap_id(ap_id)
213 def get_in_reply_to_activity(_), do: nil
215 def delete_by_ap_id(id) when is_binary(id) do
221 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
226 def delete_by_ap_id(_), do: nil
228 for {ap_type, type} <- @mastodon_notification_types do
229 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
233 def mastodon_notification_type(%Activity{}), do: nil
235 def from_mastodon_notification_type(type) do
236 Map.get(@mastodon_to_ap_notification_types, type)
239 def all_by_actor_and_id(actor, status_ids \\ [])
240 def all_by_actor_and_id(_actor, []), do: []
242 def all_by_actor_and_id(actor, status_ids) do
244 |> where([s], s.id in ^status_ids)
245 |> where([s], s.actor == ^actor)
249 def increase_replies_count(id) do
257 jsonb_set(?, '{object, repliesCount}',
258 (coalesce((?->'object'->>'repliesCount')::int, 0) + 1)::varchar::jsonb, true)
265 |> Repo.update_all([])
267 {1, [activity]} -> activity
268 _ -> {:error, "Not found"}
272 def decrease_replies_count(id) do
280 jsonb_set(?, '{object, repliesCount}',
281 (greatest(0, (?->'object'->>'repliesCount')::int - 1))::varchar::jsonb, true)
288 |> Repo.update_all([])
290 {1, [activity]} -> activity
291 _ -> {:error, "Not found"}