fix(back): fix instances filtering

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2024-01-04 12:56:18 +01:00
parent 428537df1f
commit b3ba45e8a7
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773

View file

@ -4,6 +4,7 @@ defmodule Mobilizon.Instances do
""" """
alias Ecto.Adapters.SQL alias Ecto.Adapters.SQL
alias Mobilizon.Actors.{Actor, Follower} alias Mobilizon.Actors.{Actor, Follower}
alias Mobilizon.Federation.ActivityPub.Relay
alias Mobilizon.Instances.{Instance, InstanceActor} alias Mobilizon.Instances.{Instance, InstanceActor}
alias Mobilizon.Storage.{Page, Repo} alias Mobilizon.Storage.{Page, Repo}
import Ecto.Query import Ecto.Query
@ -22,11 +23,15 @@ defmodule Mobilizon.Instances do
order_by_options = Keyword.new([{direction, order_by}]) order_by_options = Keyword.new([{direction, order_by}])
%Actor{id: relay_id} = Relay.get_actor()
query = query =
Instance Instance
|> join(:left, [i], ia in InstanceActor, on: i.domain == ia.domain) |> join(:left, [i], ia in InstanceActor, on: i.domain == ia.domain)
|> join(:left, [_i, ia], a in Actor, on: ia.actor_id == a.id) |> join(:left, [_i, ia], a in Actor, on: ia.actor_id == a.id)
# following
|> join(:left, [_i, _ia, a], f1 in Follower, on: f1.target_actor_id == a.id) |> join(:left, [_i, _ia, a], f1 in Follower, on: f1.target_actor_id == a.id)
# followed
|> join(:left, [_i, _ia, a], f2 in Follower, on: f2.actor_id == a.id) |> join(:left, [_i, _ia, a], f2 in Follower, on: f2.actor_id == a.id)
|> select([i, ia, a, f1, f2], %{ |> select([i, ia, a, f1, f2], %{
instance: i, instance: i,
@ -45,14 +50,27 @@ defmodule Mobilizon.Instances do
if is_nil(filter_domain) or filter_domain == "" do if is_nil(filter_domain) or filter_domain == "" do
query query
else else
where(query, [i], like(i.domain, ^"%#{filter_domain}%")) where(
query,
[i, ia],
like(i.domain, ^"%#{filter_domain}%") or like(ia.instance_name, ^"%#{filter_domain}%")
)
end end
query = query =
case follow_status do case follow_status do
:following -> where(query, [i, s], s.following == true) :following ->
:followed -> where(query, [i, s], s.follower == true) where(query, [_i, _ia, _a, f1], f1.actor_id == ^relay_id and f1.approved == true)
:all -> query
:followed ->
where(
query,
[_i, _ia, _a, _f1, f2],
f2.target_actor_id == ^relay_id and f2.approved == true
)
:all ->
query
end end
%Page{elements: elements} = paged_instances = Page.build_page(query, page, limit, :domain) %Page{elements: elements} = paged_instances = Page.build_page(query, page, limit, :domain)