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
7 alias Pleroma.{Repo, Activity, Notification}
10 @type t :: %__MODULE__{}
12 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
13 @mastodon_notification_types %{
14 "Create" => "mention",
16 "Announce" => "reblog",
20 schema "activities" do
22 field(:local, :boolean, default: true)
23 field(:actor, :string)
24 field(:recipients, {:array, :string})
25 has_many(:notifications, Notification, on_delete: :delete_all)
30 def get_by_ap_id(ap_id) do
34 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
40 Repo.get(Activity, id)
44 # Go through these and fix them everywhere.
45 # Wrong name, only returns create activities
46 def all_by_object_ap_id_q(ap_id) do
51 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
56 where: fragment("(?)->>'type' = 'Create'", activity.data)
60 # Wrong name, returns all.
61 def all_non_create_by_object_ap_id_q(ap_id) do
66 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
74 # Wrong name plz fix thx
75 def all_by_object_ap_id(ap_id) do
76 Repo.all(all_by_object_ap_id_q(ap_id))
79 def create_activity_by_object_id_query(ap_ids) do
84 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
89 where: fragment("(?)->>'type' = 'Create'", activity.data)
93 def get_create_activity_by_object_ap_id(ap_id) when is_binary(ap_id) do
94 create_activity_by_object_id_query([ap_id])
98 def get_create_activity_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_activity_by_object_ap_id(ap_id)
108 def get_in_reply_to_activity(_), do: nil
110 for {ap_type, type} <- @mastodon_notification_types do
111 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
115 def mastodon_notification_type(%Activity{}), do: nil