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'::text) ->> 'id'::text)",
45 # o.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'::text) ->> 'id'::text)",
69 |> preload([activity, object], object: object)
72 def get_by_ap_id(ap_id) do
76 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
81 def get_by_ap_id_with_object(ap_id) do
85 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
86 inner_join: o in Object,
89 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
99 Repo.get(Activity, id)
102 def get_by_id_with_object(id) do
103 from(activity in Activity,
104 where: activity.id == ^id,
105 inner_join: o in Object,
108 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
117 def by_object_ap_id(ap_id) do
119 activity in Activity,
122 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
130 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
132 activity in Activity,
135 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
140 where: fragment("(?)->>'type' = 'Create'", activity.data)
144 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
146 activity in Activity,
149 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
154 where: fragment("(?)->>'type' = 'Create'", activity.data)
158 def create_by_object_ap_id(_), do: nil
160 def get_all_create_by_object_ap_id(ap_id) do
161 Repo.all(create_by_object_ap_id(ap_id))
164 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
165 create_by_object_ap_id(ap_id)
169 def get_create_by_object_ap_id(_), do: nil
171 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
173 activity in Activity,
176 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
181 where: fragment("(?)->>'type' = 'Create'", activity.data),
182 inner_join: o in Object,
185 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
193 def create_by_object_ap_id_with_object(_), do: nil
195 def get_create_by_object_ap_id_with_object(ap_id) do
197 |> create_by_object_ap_id_with_object()
201 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
202 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
203 def normalize(_), do: nil
205 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
206 get_create_by_object_ap_id(ap_id)
209 def get_in_reply_to_activity(_), do: nil
211 def delete_by_ap_id(id) when is_binary(id) do
217 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
222 def delete_by_ap_id(_), do: nil
224 for {ap_type, type} <- @mastodon_notification_types do
225 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
229 def mastodon_notification_type(%Activity{}), do: nil
231 def from_mastodon_notification_type(type) do
232 Map.get(@mastodon_to_ap_notification_types, type)
235 def all_by_actor_and_id(actor, status_ids \\ [])
236 def all_by_actor_and_id(_actor, []), do: []
238 def all_by_actor_and_id(actor, status_ids) do
240 |> where([s], s.id in ^status_ids)
241 |> where([s], s.actor == ^actor)