projects
/
akkoma
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge branch 'feature/sql-thread-sat' into 'develop'
[akkoma]
/
lib
/
pleroma
/
activity.ex
diff --git
a/lib/pleroma/activity.ex
b/lib/pleroma/activity.ex
index 7845c264a22a1255d634fea1510b055775d5fe51..4a09194786f3f58ca4c998fd67d99cb0a8dc4f40 100644
(file)
--- a/
lib/pleroma/activity.ex
+++ b/
lib/pleroma/activity.ex
@@
-10,6
+10,7
@@
defmodule Pleroma.Activity do
alias Pleroma.Notification
alias Pleroma.Object
alias Pleroma.Repo
alias Pleroma.Notification
alias Pleroma.Object
alias Pleroma.Repo
+ alias Pleroma.User
import Ecto.Changeset
import Ecto.Query
import Ecto.Changeset
import Ecto.Query
@@
-36,8
+37,9
@@
defmodule Pleroma.Activity do
field(:local, :boolean, default: true)
field(:actor, :string)
field(:recipients, {:array, :string}, default: [])
field(:local, :boolean, default: true)
field(:actor, :string)
field(:recipients, {:array, :string}, default: [])
+ # This is a fake relation, do not use outside of with_preloaded_bookmark/get_bookmark
+ has_one(:bookmark, Bookmark)
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.
@@
-73,9
+75,18
@@
defmodule Pleroma.Activity do
)
)
|> preload([activity, object], object: object)
)
)
|> preload([activity, object], object: object)
- |> preload(:bookmarks)
end
end
+ def with_preloaded_bookmark(query, %User{} = user) do
+ from([a] in query,
+ left_join: b in Bookmark,
+ on: b.user_id == ^user.id and b.activity_id == a.id,
+ preload: [bookmark: b]
+ )
+ end
+
+ def with_preloaded_bookmark(query, _), do: query
+
def get_by_ap_id(ap_id) do
Repo.one(
from(
def get_by_ap_id(ap_id) do
Repo.one(
from(
@@
-85,6
+96,16
@@
defmodule Pleroma.Activity do
)
end
)
end
+ def get_bookmark(%Activity{} = activity, %User{} = user) do
+ if Ecto.assoc_loaded?(activity.bookmark) do
+ activity.bookmark
+ else
+ Bookmark.get(user.id, activity.id)
+ end
+ end
+
+ def get_bookmark(_, _), do: nil
+
def change(struct, params \\ %{}) do
struct
|> cast(params, [:data])
def change(struct, params \\ %{}) do
struct
|> cast(params, [:data])
@@
-105,14
+126,16
@@
defmodule Pleroma.Activity do
activity.data,
activity.data
),
activity.data,
activity.data
),
- preload: [object: o],
- preload: :bookmarks
+ preload: [object: o]
)
)
end
def get_by_id(id) do
)
)
end
def get_by_id(id) do
- Repo.get(Activity, id)
+ Activity
+ |> where([a], a.id == ^id)
+ |> restrict_deactivated_users()
+ |> Repo.one()
end
def get_by_id_with_object(id) do
end
def get_by_id_with_object(id) do
@@
-126,8
+149,7
@@
defmodule Pleroma.Activity do
activity.data,
activity.data
),
activity.data,
activity.data
),
- preload: [object: o],
- preload: :bookmarks
+ preload: [object: o]
)
|> Repo.one()
end
)
|> Repo.one()
end
@@
-181,6
+203,7
@@
defmodule Pleroma.Activity do
def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
create_by_object_ap_id(ap_id)
def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
create_by_object_ap_id(ap_id)
+ |> restrict_deactivated_users()
|> Repo.one()
end
|> Repo.one()
end
@@
-205,8
+228,7
@@
defmodule Pleroma.Activity do
activity.data,
activity.data
),
activity.data,
activity.data
),
- preload: [object: o],
- preload: :bookmarks
+ preload: [object: o]
)
end
)
end
@@
-269,8
+291,41
@@
defmodule Pleroma.Activity do
|> Repo.all()
end
|> Repo.all()
end
+ def follow_requests_for_actor(%Pleroma.User{ap_id: ap_id}) do
+ from(
+ a in Activity,
+ where:
+ fragment(
+ "? ->> 'type' = 'Follow'",
+ a.data
+ ),
+ where:
+ fragment(
+ "? ->> 'state' = 'pending'",
+ a.data
+ ),
+ where:
+ fragment(
+ "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
+ a.data,
+ a.data,
+ ^ap_id
+ )
+ )
+ end
+
@spec query_by_actor(actor()) :: Ecto.Query.t()
def query_by_actor(actor) do
from(a in Activity, where: a.actor == ^actor)
end
@spec query_by_actor(actor()) :: Ecto.Query.t()
def query_by_actor(actor) do
from(a in Activity, where: a.actor == ^actor)
end
+
+ def restrict_deactivated_users(query) do
+ from(activity in query,
+ where:
+ fragment(
+ "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')",
+ activity.actor
+ )
+ )
+ end
end
end