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
14 @type t :: %__MODULE__{}
15 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
17 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
18 @mastodon_notification_types %{
19 "Create" => "mention",
21 "Announce" => "reblog",
25 schema "activities" do
27 field(:local, :boolean, default: true)
28 field(:actor, :string)
29 field(:recipients, {:array, :string})
30 has_many(:notifications, Notification, on_delete: :delete_all)
35 def get_by_ap_id(ap_id) do
39 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
46 |> where([a], a.id == ^id)
47 |> restrict_disabled_users()
51 def by_object_ap_id(ap_id) do
56 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
64 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
69 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
74 where: fragment("(?)->>'type' = 'Create'", activity.data)
78 def create_by_object_ap_id(ap_id) do
83 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
88 where: fragment("(?)->>'type' = 'Create'", activity.data)
92 def get_all_create_by_object_ap_id(ap_id) do
93 Repo.all(create_by_object_ap_id(ap_id))
96 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
97 create_by_object_ap_id(ap_id)
98 |> restrict_disabled_users()
102 def get_create_by_object_ap_id(_), do: nil
104 def normalize(obj) when is_map(obj), do: Activity.get_by_ap_id(obj["id"])
105 def normalize(ap_id) when is_binary(ap_id), do: Activity.get_by_ap_id(ap_id)
106 def normalize(_), do: nil
108 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
109 get_create_by_object_ap_id(ap_id)
112 def get_in_reply_to_activity(_), do: nil
114 for {ap_type, type} <- @mastodon_notification_types do
115 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
119 def mastodon_notification_type(%Activity{}), do: nil
121 def all_by_actor_and_id(actor, status_ids \\ [])
122 def all_by_actor_and_id(_actor, []), do: []
124 def all_by_actor_and_id(actor, status_ids) do
126 |> where([s], s.id in ^status_ids)
127 |> where([s], s.actor == ^actor)
131 def restrict_disabled_users(query) do
132 from(activity in query,
135 "? not in (SELECT ap_id FROM users WHERE info->'disabled' @> 'true')",