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
10 alias Pleroma.Notification
17 @type t :: %__MODULE__{}
18 @type actor :: String.t()
20 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
22 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
23 @mastodon_notification_types %{
24 "Create" => "mention",
26 "Announce" => "reblog",
30 @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
34 schema "activities" do
36 field(:local, :boolean, default: true)
37 field(:actor, :string)
38 field(:recipients, {:array, :string}, default: [])
39 has_many(:notifications, Notification, on_delete: :delete_all)
40 has_many(:bookmarks, Bookmark, on_delete: :delete_all)
42 # Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
43 # The foreign key is embedded in a jsonb field.
45 # To use it, you probably want to do an inner join and a preload:
48 # |> join(:inner, [activity], o in Object,
49 # on: fragment("(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
50 # o.data, activity.data, activity.data))
51 # |> preload([activity, object], [object: object])
54 # As a convenience, Activity.with_preloaded_object() sets up an inner join and preload for the
56 has_one(:object, Object, on_delete: :nothing, foreign_key: :id)
61 def with_preloaded_object(query) do
69 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
75 |> preload([activity, object], object: object)
76 |> preload(:bookmarks)
79 def get_by_ap_id(ap_id) do
83 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
88 def change(struct, params \\ %{}) do
90 |> cast(params, [:data])
91 |> validate_required([:data])
92 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
95 def get_by_ap_id_with_object(ap_id) do
99 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
100 left_join: o in Object,
103 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
108 preload: [object: o],
115 Repo.get(Activity, id)
118 def get_by_id_with_object(id) do
119 from(activity in Activity,
120 where: activity.id == ^id,
121 inner_join: o in Object,
124 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
129 preload: [object: o],
135 def by_object_ap_id(ap_id) do
137 activity in Activity,
140 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
148 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
150 activity in Activity,
153 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
158 where: fragment("(?)->>'type' = 'Create'", activity.data)
162 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
164 activity in Activity,
167 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
172 where: fragment("(?)->>'type' = 'Create'", activity.data)
176 def create_by_object_ap_id(_), do: nil
178 def get_all_create_by_object_ap_id(ap_id) do
179 Repo.all(create_by_object_ap_id(ap_id))
182 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
183 create_by_object_ap_id(ap_id)
187 def get_create_by_object_ap_id(_), do: nil
189 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
191 activity in Activity,
194 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
199 where: fragment("(?)->>'type' = 'Create'", activity.data),
200 inner_join: o in Object,
203 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
208 preload: [object: o],
213 def create_by_object_ap_id_with_object(_), do: nil
215 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
217 |> create_by_object_ap_id_with_object()
221 def get_create_by_object_ap_id_with_object(_), do: nil
223 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
224 get_create_by_object_ap_id_with_object(ap_id)
227 defp get_in_reply_to_activity_from_object(_), do: nil
229 def get_in_reply_to_activity(%Activity{data: %{"object" => object}}) do
230 get_in_reply_to_activity_from_object(Object.normalize(object))
233 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
234 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
235 def normalize(_), do: nil
237 def delete_by_ap_id(id) when is_binary(id) do
243 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
244 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
249 def delete_by_ap_id(_), do: nil
251 for {ap_type, type} <- @mastodon_notification_types do
252 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
256 def mastodon_notification_type(%Activity{}), do: nil
258 def from_mastodon_notification_type(type) do
259 Map.get(@mastodon_to_ap_notification_types, type)
262 def all_by_actor_and_id(actor, status_ids \\ [])
263 def all_by_actor_and_id(_actor, []), do: []
265 def all_by_actor_and_id(actor, status_ids) do
267 |> where([s], s.id in ^status_ids)
268 |> where([s], s.actor == ^actor)
272 @spec query_by_actor(actor()) :: Ecto.Query.t()
273 def query_by_actor(actor) do
274 from(a in Activity, where: a.actor == ^actor)