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
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))
45 Repo.get(Activity, id)
48 def by_object_ap_id(ap_id) do
53 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
61 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
66 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
71 where: fragment("(?)->>'type' = 'Create'", activity.data)
75 def create_by_object_ap_id(ap_id) do
80 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
85 where: fragment("(?)->>'type' = 'Create'", activity.data)
89 def get_all_create_by_object_ap_id(ap_id) do
90 Repo.all(create_by_object_ap_id(ap_id))
93 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
94 create_by_object_ap_id(ap_id)
98 def get_create_by_object_ap_id(_), do: nil
100 def normalize(obj) when is_map(obj), do: Activity.get_by_ap_id(obj["id"])
101 def normalize(ap_id) when is_binary(ap_id), do: Activity.get_by_ap_id(ap_id)
102 def normalize(_), do: nil
104 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
105 get_create_by_object_ap_id(ap_id)
108 def get_in_reply_to_activity(_), do: nil
110 def delete_by_ap_id(id) when is_binary(id) do
112 |> Repo.delete_all(returning: true)
115 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
120 def delete_by_ap_id(_), do: nil
122 for {ap_type, type} <- @mastodon_notification_types do
123 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
127 def mastodon_notification_type(%Activity{}), do: nil
129 def all_by_actor_and_id(actor, status_ids \\ [])
130 def all_by_actor_and_id(_actor, []), do: []
132 def all_by_actor_and_id(actor, status_ids) do
134 |> where([s], s.id in ^status_ids)
135 |> where([s], s.actor == ^actor)