1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
11 @mastodon_notification_types %{
12 "Create" => "mention",
14 "Announce" => "reblog",
18 schema "activities" do
20 field(:local, :boolean, default: true)
21 field(:actor, :string)
22 field(:recipients, {:array, :string})
23 has_many(:notifications, Notification, on_delete: :delete_all)
28 def get_by_ap_id(ap_id) do
32 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
38 # Go through these and fix them everywhere.
39 # Wrong name, only returns create activities
40 def all_by_object_ap_id_q(ap_id) do
45 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
50 where: fragment("(?)->>'type' = 'Create'", activity.data)
54 # Wrong name, returns all.
55 def all_non_create_by_object_ap_id_q(ap_id) do
60 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
68 # Wrong name plz fix thx
69 def all_by_object_ap_id(ap_id) do
70 Repo.all(all_by_object_ap_id_q(ap_id))
73 def create_activity_by_object_id_query(ap_ids) do
78 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
83 where: fragment("(?)->>'type' = 'Create'", activity.data)
87 def get_create_activity_by_object_ap_id(ap_id) when is_binary(ap_id) do
88 create_activity_by_object_id_query([ap_id])
92 def get_create_activity_by_object_ap_id(_), do: nil
94 def normalize(obj) when is_map(obj), do: Activity.get_by_ap_id(obj["id"])
95 def normalize(ap_id) when is_binary(ap_id), do: Activity.get_by_ap_id(ap_id)
96 def normalize(_), do: nil
98 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
99 get_create_activity_by_object_ap_id(ap_id)
102 def get_in_reply_to_activity(_), do: nil
104 for {ap_type, type} <- @mastodon_notification_types do
105 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
109 def mastodon_notification_type(%Activity{}), do: nil