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
10 alias Pleroma.Notification
18 @type t :: %__MODULE__{}
19 @type actor :: String.t()
21 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
23 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
24 @mastodon_notification_types %{
25 "Create" => "mention",
27 "Announce" => "reblog",
31 @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
35 schema "activities" do
37 field(:local, :boolean, default: true)
38 field(:actor, :string)
39 field(:recipients, {:array, :string}, default: [])
40 # This is a fake relation, do not use outside of with_preloaded_bookmark/get_bookmark
41 has_one(:bookmark, Bookmark)
42 has_many(:notifications, Notification, on_delete: :delete_all)
44 # Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
45 # The foreign key is embedded in a jsonb field.
47 # To use it, you probably want to do an inner join and a preload:
50 # |> join(:inner, [activity], o in Object,
51 # on: fragment("(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
52 # o.data, activity.data, activity.data))
53 # |> preload([activity, object], [object: object])
56 # As a convenience, Activity.with_preloaded_object() sets up an inner join and preload for the
58 has_one(:object, Object, on_delete: :nothing, foreign_key: :id)
63 def with_preloaded_object(query) do
71 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
77 |> preload([activity, object], object: object)
80 def with_preloaded_bookmark(query, %User{} = user) do
82 left_join: b in Bookmark,
83 on: b.user_id == ^user.id and b.activity_id == a.id,
84 preload: [bookmark: b]
88 def with_preloaded_bookmark(query, _), do: query
90 def get_by_ap_id(ap_id) do
94 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
99 def get_bookmark(%Activity{} = activity, %User{} = user) do
100 if Ecto.assoc_loaded?(activity.bookmark) do
103 Bookmark.get(user.id, activity.id)
107 def get_bookmark(_, _), do: nil
109 def change(struct, params \\ %{}) do
111 |> cast(params, [:data])
112 |> validate_required([:data])
113 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
116 def get_by_ap_id_with_object(ap_id) do
119 activity in Activity,
120 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
121 left_join: o in Object,
124 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
135 Repo.get(Activity, id)
138 def get_by_id_with_object(id) do
139 from(activity in Activity,
140 where: activity.id == ^id,
141 inner_join: o in Object,
144 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
154 def by_object_ap_id(ap_id) do
156 activity in Activity,
159 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
167 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
169 activity in Activity,
172 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
177 where: fragment("(?)->>'type' = 'Create'", activity.data)
181 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
183 activity in Activity,
186 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
191 where: fragment("(?)->>'type' = 'Create'", activity.data)
195 def create_by_object_ap_id(_), do: nil
197 def get_all_create_by_object_ap_id(ap_id) do
198 Repo.all(create_by_object_ap_id(ap_id))
201 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
202 create_by_object_ap_id(ap_id)
206 def get_create_by_object_ap_id(_), do: nil
208 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
210 activity in Activity,
213 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
218 where: fragment("(?)->>'type' = 'Create'", activity.data),
219 inner_join: o in Object,
222 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
231 def create_by_object_ap_id_with_object(_), do: nil
233 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
235 |> create_by_object_ap_id_with_object()
239 def get_create_by_object_ap_id_with_object(_), do: nil
241 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
242 get_create_by_object_ap_id_with_object(ap_id)
245 defp get_in_reply_to_activity_from_object(_), do: nil
247 def get_in_reply_to_activity(%Activity{data: %{"object" => object}}) do
248 get_in_reply_to_activity_from_object(Object.normalize(object))
251 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
252 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
253 def normalize(_), do: nil
255 def delete_by_ap_id(id) when is_binary(id) do
261 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
262 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
267 def delete_by_ap_id(_), do: nil
269 for {ap_type, type} <- @mastodon_notification_types do
270 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
274 def mastodon_notification_type(%Activity{}), do: nil
276 def from_mastodon_notification_type(type) do
277 Map.get(@mastodon_to_ap_notification_types, type)
280 def all_by_actor_and_id(actor, status_ids \\ [])
281 def all_by_actor_and_id(_actor, []), do: []
283 def all_by_actor_and_id(actor, status_ids) do
285 |> where([s], s.id in ^status_ids)
286 |> where([s], s.actor == ^actor)
290 def follow_requests_for_actor(%Pleroma.User{ap_id: ap_id}) do
295 "? ->> 'type' = 'Follow'",
300 "? ->> 'state' = 'pending'",
305 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
313 @spec query_by_actor(actor()) :: Ecto.Query.t()
314 def query_by_actor(actor) do
315 from(a in Activity, where: a.actor == ^actor)