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
16 @type t :: %__MODULE__{}
17 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
19 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
20 @mastodon_notification_types %{
21 "Create" => "mention",
23 "Announce" => "reblog",
27 @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
31 schema "activities" do
33 field(:local, :boolean, default: true)
34 field(:actor, :string)
35 field(:recipients, {:array, :string}, default: [])
36 has_many(:notifications, Notification, on_delete: :delete_all)
38 # Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
39 # The foreign key is embedded in a jsonb field.
41 # To use it, you probably want to do an inner join and a preload:
44 # |> join(:inner, [activity], o in Object,
45 # on: fragment("(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
46 # o.data, activity.data, activity.data))
47 # |> preload([activity, object], [object: object])
50 # As a convenience, Activity.with_preloaded_object() sets up an inner join and preload for the
52 has_one(:object, Object, on_delete: :nothing, foreign_key: :id)
57 def with_preloaded_object(query) do
65 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
71 |> preload([activity, object], object: object)
74 def get_by_ap_id(ap_id) do
78 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
83 def change(struct, params \\ %{}) do
85 |> cast(params, [:data])
86 |> validate_required([:data])
87 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
90 def get_by_ap_id_with_object(ap_id) do
94 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
95 left_join: o in Object,
98 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
109 Repo.get(Activity, id)
112 def get_by_id_with_object(id) do
113 from(activity in Activity,
114 where: activity.id == ^id,
115 inner_join: o in Object,
118 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
128 def by_object_ap_id(ap_id) do
130 activity in Activity,
133 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
141 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
143 activity in Activity,
146 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
151 where: fragment("(?)->>'type' = 'Create'", activity.data)
155 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
157 activity in Activity,
160 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
165 where: fragment("(?)->>'type' = 'Create'", activity.data)
169 def create_by_object_ap_id(_), do: nil
171 def get_all_create_by_object_ap_id(ap_id) do
172 Repo.all(create_by_object_ap_id(ap_id))
175 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
176 create_by_object_ap_id(ap_id)
180 def get_create_by_object_ap_id(_), do: nil
182 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
184 activity in Activity,
187 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
192 where: fragment("(?)->>'type' = 'Create'", activity.data),
193 inner_join: o in Object,
196 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
205 def create_by_object_ap_id_with_object(_), do: nil
207 def get_create_by_object_ap_id_with_object(ap_id) do
209 |> create_by_object_ap_id_with_object()
213 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
214 get_create_by_object_ap_id_with_object(ap_id)
217 defp get_in_reply_to_activity_from_object(_), do: nil
219 def get_in_reply_to_activity(%Activity{data: %{"object" => object}}) do
220 get_in_reply_to_activity_from_object(Object.normalize(object))
223 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
224 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
225 def normalize(_), do: nil
227 def delete_by_ap_id(id) when is_binary(id) do
233 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
238 def delete_by_ap_id(_), do: nil
240 for {ap_type, type} <- @mastodon_notification_types do
241 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
245 def mastodon_notification_type(%Activity{}), do: nil
247 def from_mastodon_notification_type(type) do
248 Map.get(@mastodon_to_ap_notification_types, type)
251 def all_by_actor_and_id(actor, status_ids \\ [])
252 def all_by_actor_and_id(_actor, []), do: []
254 def all_by_actor_and_id(actor, status_ids) do
256 |> where([s], s.id in ^status_ids)
257 |> where([s], s.actor == ^actor)