projects
/
akkoma
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
- Actually use preloaded bookmarks in views
[akkoma]
/
lib
/
pleroma
/
activity.ex
diff --git
a/lib/pleroma/activity.ex
b/lib/pleroma/activity.ex
index 26190df2fabac32305e5fba728eadc6e844bbf66..e432fcb0738c9a333a068a6f8035e4c332c8a4e1 100644
(file)
--- a/
lib/pleroma/activity.ex
+++ b/
lib/pleroma/activity.ex
@@
-6,13
+6,17
@@
defmodule Pleroma.Activity do
use Ecto.Schema
alias Pleroma.Activity
use Ecto.Schema
alias Pleroma.Activity
+ alias Pleroma.Bookmark
alias Pleroma.Notification
alias Pleroma.Object
alias Pleroma.Repo
alias Pleroma.Notification
alias Pleroma.Object
alias Pleroma.Repo
+ import Ecto.Changeset
import Ecto.Query
@type t :: %__MODULE__{}
import Ecto.Query
@type t :: %__MODULE__{}
+ @type actor :: String.t()
+
@primary_key {:id, Pleroma.FlakeId, autogenerate: true}
# https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
@primary_key {:id, Pleroma.FlakeId, autogenerate: true}
# https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
@@
-31,8
+35,9
@@
defmodule Pleroma.Activity do
field(:data, :map)
field(:local, :boolean, default: true)
field(:actor, :string)
field(:data, :map)
field(:local, :boolean, default: true)
field(:actor, :string)
- field(:recipients, {:array, :string})
+ field(:recipients, {:array, :string}
, default: []
)
has_many(:notifications, Notification, on_delete: :delete_all)
has_many(:notifications, Notification, on_delete: :delete_all)
+ has_many(:bookmarks, Bookmark, on_delete: :delete_all)
# Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
# The foreign key is embedded in a jsonb field.
# Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
# The foreign key is embedded in a jsonb field.
@@
-41,8
+46,8
@@
defmodule Pleroma.Activity do
#
# ```
# |> join(:inner, [activity], o in Object,
#
# ```
# |> join(:inner, [activity], o in Object,
- # on: fragment("(?->>'id') = COALESCE((?
-> 'object'::text) ->> 'id'::text
)",
- # o.data, activity.data))
+ # on: fragment("(?->>'id') = COALESCE((?
)->'object'->> 'id', (?)->>'object'
)",
+ # o.data, activity.data
, activity.data
))
# |> preload([activity, object], [object: object])
# ```
#
# |> preload([activity, object], [object: object])
# ```
#
@@
-61,12
+66,19
@@
defmodule Pleroma.Activity do
o in Object,
on:
fragment(
o in Object,
on:
fragment(
- "(?->>'id') = COALESCE(
(? -> 'object'::text) ->> 'id'::text
)",
+ "(?->>'id') = COALESCE(
?->'object'->>'id', ?->>'object'
)",
o.data,
o.data,
+ activity.data,
activity.data
)
)
|> preload([activity, object], object: object)
activity.data
)
)
|> preload([activity, object], object: object)
+ |> with_preloaded_bookmarks()
+ end
+
+ def with_preloaded_bookmarks(query) do
+ query
+ |> preload(:bookmarks)
end
def get_by_ap_id(ap_id) do
end
def get_by_ap_id(ap_id) do
@@
-78,6
+90,13
@@
defmodule Pleroma.Activity do
)
end
)
end
+ def change(struct, params \\ %{}) do
+ struct
+ |> cast(params, [:data])
+ |> validate_required([:data])
+ |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
+ end
+
def get_by_ap_id_with_object(ap_id) do
Repo.one(
from(
def get_by_ap_id_with_object(ap_id) do
Repo.one(
from(
@@
-86,12
+105,14
@@
defmodule Pleroma.Activity do
left_join: o in Object,
on:
fragment(
left_join: o in Object,
on:
fragment(
- "(?->>'id') = COALESCE(
(? -> 'object'::text) ->> 'id'::text
)",
+ "(?->>'id') = COALESCE(
?->'object'->>'id', ?->>'object'
)",
o.data,
o.data,
+ activity.data,
activity.data
),
preload: [object: o]
)
activity.data
),
preload: [object: o]
)
+ |> with_preloaded_bookmarks()
)
end
)
end
@@
-105,12
+126,14
@@
defmodule Pleroma.Activity do
inner_join: o in Object,
on:
fragment(
inner_join: o in Object,
on:
fragment(
- "(?->>'id') = COALESCE(
(? -> 'object'::text) ->> 'id'::text
)",
+ "(?->>'id') = COALESCE(
?->'object'->>'id', ?->>'object'
)",
o.data,
o.data,
+ activity.data,
activity.data
),
preload: [object: o]
)
activity.data
),
preload: [object: o]
)
+ |> with_preloaded_bookmarks()
|> Repo.one()
end
|> Repo.one()
end
@@
-182,31
+205,39
@@
defmodule Pleroma.Activity do
inner_join: o in Object,
on:
fragment(
inner_join: o in Object,
on:
fragment(
- "(?->>'id') = COALESCE(
(? -> 'object'::text) ->> 'id'::text
)",
+ "(?->>'id') = COALESCE(
?->'object'->>'id', ?->>'object'
)",
o.data,
o.data,
+ activity.data,
activity.data
),
activity.data
),
- preload: [object: o]
+ preload: [object: o],
+ preload: :bookmarks
)
end
def create_by_object_ap_id_with_object(_), do: nil
)
end
def create_by_object_ap_id_with_object(_), do: nil
- def get_create_by_object_ap_id_with_object(ap_id) do
+ def get_create_by_object_ap_id_with_object(ap_id)
when is_binary(ap_id)
do
ap_id
|> create_by_object_ap_id_with_object()
|> Repo.one()
end
ap_id
|> create_by_object_ap_id_with_object()
|> Repo.one()
end
- def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
- def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
- def normalize(_), do: nil
+ def get_create_by_object_ap_id_with_object(_), do: nil
+
+ defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
+ get_create_by_object_ap_id_with_object(ap_id)
+ end
+
+ defp get_in_reply_to_activity_from_object(_), do: nil
- def get_in_reply_to_activity(%Activity{data: %{"object" =>
%{"inReplyTo" => ap_id}
}}) do
- get_
create_by_object_ap_id(ap_id
)
+ def get_in_reply_to_activity(%Activity{data: %{"object" =>
object
}}) do
+ get_
in_reply_to_activity_from_object(Object.normalize(object)
)
end
end
- def get_in_reply_to_activity(_), do: nil
+ def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
+ def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
+ def normalize(_), do: nil
def delete_by_ap_id(id) when is_binary(id) do
by_object_ap_id(id)
def delete_by_ap_id(id) when is_binary(id) do
by_object_ap_id(id)
@@
-214,6
+245,7
@@
defmodule Pleroma.Activity do
|> Repo.delete_all()
|> elem(1)
|> Enum.find(fn
|> Repo.delete_all()
|> elem(1)
|> Enum.find(fn
+ %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
%{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
_ -> nil
end)
%{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
_ -> nil
end)
@@
-241,4
+273,9
@@
defmodule Pleroma.Activity do
|> where([s], s.actor == ^actor)
|> Repo.all()
end
|> where([s], s.actor == ^actor)
|> Repo.all()
end
+
+ @spec query_by_actor(actor()) :: Ecto.Query.t()
+ def query_by_actor(actor) do
+ from(a in Activity, where: a.actor == ^actor)
+ end
end
end