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 @type actor :: String.t()
19 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
21 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
22 @mastodon_notification_types %{
23 "Create" => "mention",
25 "Announce" => "reblog",
29 @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
33 schema "activities" do
35 field(:local, :boolean, default: true)
36 field(:actor, :string)
37 field(:recipients, {:array, :string}, default: [])
38 has_many(:notifications, Notification, on_delete: :delete_all)
40 # Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
41 # The foreign key is embedded in a jsonb field.
43 # To use it, you probably want to do an inner join and a preload:
46 # |> join(:inner, [activity], o in Object,
47 # on: fragment("(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
48 # o.data, activity.data, activity.data))
49 # |> preload([activity, object], [object: object])
52 # As a convenience, Activity.with_preloaded_object() sets up an inner join and preload for the
54 has_one(:object, Object, on_delete: :nothing, foreign_key: :id)
59 def with_preloaded_object(query) do
67 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
73 |> preload([activity, object], object: object)
76 def get_by_ap_id(ap_id) do
80 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
85 def change(struct, params \\ %{}) do
87 |> cast(params, [:data])
88 |> validate_required([:data])
89 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
92 def get_by_ap_id_with_object(ap_id) do
96 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
97 left_join: o in Object,
100 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
111 Repo.get(Activity, id)
114 def get_by_id_with_object(id) do
115 from(activity in Activity,
116 where: activity.id == ^id,
117 inner_join: o in Object,
120 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
130 def by_object_ap_id(ap_id) do
132 activity in Activity,
135 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
143 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
145 activity in Activity,
148 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
153 where: fragment("(?)->>'type' = 'Create'", activity.data)
157 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
159 activity in Activity,
162 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
167 where: fragment("(?)->>'type' = 'Create'", activity.data)
171 def create_by_object_ap_id(_), do: nil
173 def get_all_create_by_object_ap_id(ap_id) do
174 Repo.all(create_by_object_ap_id(ap_id))
177 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
178 create_by_object_ap_id(ap_id)
182 def get_create_by_object_ap_id(_), do: nil
184 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
186 activity in Activity,
189 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
194 where: fragment("(?)->>'type' = 'Create'", activity.data),
195 inner_join: o in Object,
198 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
207 def create_by_object_ap_id_with_object(_), do: nil
209 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
211 |> create_by_object_ap_id_with_object()
215 def get_create_by_object_ap_id_with_object(_), do: nil
217 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
218 get_create_by_object_ap_id_with_object(ap_id)
221 defp get_in_reply_to_activity_from_object(_), do: nil
223 def get_in_reply_to_activity(%Activity{data: %{"object" => object}}) do
224 get_in_reply_to_activity_from_object(Object.normalize(object))
227 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
228 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
229 def normalize(_), do: nil
231 def delete_by_ap_id(id) when is_binary(id) do
237 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
238 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
243 def delete_by_ap_id(_), do: nil
245 for {ap_type, type} <- @mastodon_notification_types do
246 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
250 def mastodon_notification_type(%Activity{}), do: nil
252 def from_mastodon_notification_type(type) do
253 Map.get(@mastodon_to_ap_notification_types, type)
256 def all_by_actor_and_id(actor, status_ids \\ [])
257 def all_by_actor_and_id(_actor, []), do: []
259 def all_by_actor_and_id(actor, status_ids) do
261 |> where([s], s.id in ^status_ids)
262 |> where([s], s.actor == ^actor)
266 @spec query_by_actor(actor()) :: Ecto.Query.t()
267 def query_by_actor(actor) do
268 from(a in Activity, where: a.actor == ^actor)