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__{}
11 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
13 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
14 @mastodon_notification_types %{
15 "Create" => "mention",
17 "Announce" => "reblog",
21 schema "activities" do
23 field(:local, :boolean, default: true)
24 field(:actor, :string)
25 field(:recipients, {:array, :string})
26 has_many(:notifications, Notification, on_delete: :delete_all)
31 def get_by_ap_id(ap_id) do
35 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
41 Repo.get(Activity, id)
45 # Go through these and fix them everywhere.
46 # Wrong name, only returns create activities
47 def all_by_object_ap_id_q(ap_id) do
52 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
57 where: fragment("(?)->>'type' = 'Create'", activity.data)
61 # Wrong name, returns all.
62 def all_non_create_by_object_ap_id_q(ap_id) do
67 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
75 # Wrong name plz fix thx
76 def all_by_object_ap_id(ap_id) do
77 Repo.all(all_by_object_ap_id_q(ap_id))
80 def create_activity_by_object_id_query(ap_ids) do
85 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
90 where: fragment("(?)->>'type' = 'Create'", activity.data)
94 def get_create_activity_by_object_ap_id(ap_id) when is_binary(ap_id) do
95 create_activity_by_object_id_query([ap_id])
99 def get_create_activity_by_object_ap_id(_), do: nil
101 def normalize(obj) when is_map(obj), do: Activity.get_by_ap_id(obj["id"])
102 def normalize(ap_id) when is_binary(ap_id), do: Activity.get_by_ap_id(ap_id)
103 def normalize(_), do: nil
105 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
106 get_create_activity_by_object_ap_id(ap_id)
109 def get_in_reply_to_activity(_), do: nil
111 for {ap_type, type} <- @mastodon_notification_types do
112 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
116 def mastodon_notification_type(%Activity{}), do: nil