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 @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
29 schema "activities" do
31 field(:local, :boolean, default: true)
32 field(:actor, :string)
33 field(:recipients, {:array, :string})
34 has_many(:notifications, Notification, on_delete: :delete_all)
39 def get_by_ap_id(ap_id) do
43 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
49 Repo.get(Activity, id)
52 def by_object_ap_id(ap_id) do
57 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
65 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
70 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
75 where: fragment("(?)->>'type' = 'Create'", activity.data)
79 def create_by_object_ap_id(ap_id) do
84 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
89 where: fragment("(?)->>'type' = 'Create'", activity.data)
93 def get_all_create_by_object_ap_id(ap_id) do
94 Repo.all(create_by_object_ap_id(ap_id))
97 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
98 create_by_object_ap_id(ap_id)
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 def delete_by_ap_id(id) when is_binary(id) do
120 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
125 def delete_by_ap_id(_), do: nil
127 for {ap_type, type} <- @mastodon_notification_types do
128 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
132 def mastodon_notification_type(%Activity{}), do: nil
134 def from_mastodon_notification_type(type) do
135 Map.get(@mastodon_to_ap_notification_types, type)
138 def all_by_actor_and_id(actor, status_ids \\ [])
139 def all_by_actor_and_id(_actor, []), do: []
141 def all_by_actor_and_id(actor, status_ids) do
143 |> where([s], s.id in ^status_ids)
144 |> where([s], s.actor == ^actor)