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
15 @type t :: %__MODULE__{}
16 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
18 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
19 @mastodon_notification_types %{
20 "Create" => "mention",
22 "Announce" => "reblog",
26 @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
30 schema "activities" do
32 field(:local, :boolean, default: true)
33 field(:actor, :string)
34 field(:recipients, {:array, :string})
35 has_many(:notifications, Notification, on_delete: :delete_all)
37 # Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
38 # The foreign key is embedded in a jsonb field.
40 # To use it, you probably want to do an inner join and a preload:
43 # |> join(:inner, [activity], o in Object,
44 # fragment("(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)", o.data, activity.data))
45 # |> preload([activity, object], [object: object])
47 has_one(:object, Object, on_delete: :nothing, foreign_key: :id)
52 def get_by_ap_id(ap_id) do
56 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
62 Repo.get(Activity, id)
65 def get_by_id_with_object(id) do
66 from(activity in Activity,
67 where: activity.id == ^id,
68 inner_join: o in Object,
71 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
80 def by_object_ap_id(ap_id) do
85 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
93 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
98 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
103 where: fragment("(?)->>'type' = 'Create'", activity.data)
107 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
109 activity in Activity,
112 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
117 where: fragment("(?)->>'type' = 'Create'", activity.data)
121 def create_by_object_ap_id(_), do: nil
123 def get_all_create_by_object_ap_id(ap_id) do
124 Repo.all(create_by_object_ap_id(ap_id))
127 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
128 create_by_object_ap_id(ap_id)
132 def get_create_by_object_ap_id(_), do: nil
134 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
136 activity in Activity,
139 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
144 where: fragment("(?)->>'type' = 'Create'", activity.data),
145 inner_join: o in Object,
148 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
156 def create_by_object_ap_id_with_object(_), do: nil
158 def get_create_by_object_ap_id_with_object(ap_id) do
160 |> create_by_object_ap_id_with_object()
164 def normalize(obj) when is_map(obj), do: Activity.get_by_ap_id(obj["id"])
165 def normalize(ap_id) when is_binary(ap_id), do: Activity.get_by_ap_id(ap_id)
166 def normalize(_), do: nil
168 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
169 get_create_by_object_ap_id(ap_id)
172 def get_in_reply_to_activity(_), do: nil
174 def delete_by_ap_id(id) when is_binary(id) do
180 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
185 def delete_by_ap_id(_), do: nil
187 for {ap_type, type} <- @mastodon_notification_types do
188 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
192 def mastodon_notification_type(%Activity{}), do: nil
194 def from_mastodon_notification_type(type) do
195 Map.get(@mastodon_to_ap_notification_types, type)
198 def all_by_actor_and_id(actor, status_ids \\ [])
199 def all_by_actor_and_id(_actor, []), do: []
201 def all_by_actor_and_id(actor, status_ids) do
203 |> where([s], s.id in ^status_ids)
204 |> where([s], s.actor == ^actor)