2021-10-29 10:42:37 +02:00
|
|
|
defmodule Mobilizon.FollowedGroupActivity do
|
|
|
|
@moduledoc """
|
|
|
|
Provide recent elements from groups that profiles follow
|
|
|
|
"""
|
|
|
|
|
|
|
|
import Ecto.Query
|
|
|
|
alias Mobilizon.Actors.{Actor, Follower, Member}
|
|
|
|
alias Mobilizon.Events.{Event, Participant}
|
|
|
|
alias Mobilizon.Storage.Page
|
|
|
|
|
2021-11-02 19:48:38 +01:00
|
|
|
@spec user_followed_group_events(
|
|
|
|
integer() | String.t(),
|
|
|
|
DateTime.t() | nil,
|
|
|
|
integer() | nil,
|
|
|
|
integer() | nil
|
|
|
|
) :: Page.t(Event.t())
|
2021-11-22 09:30:18 +01:00
|
|
|
def user_followed_group_events(user_id, after_datetime \\ nil, page \\ nil, limit \\ nil) do
|
2021-10-29 10:42:37 +02:00
|
|
|
Event
|
2021-11-24 09:28:59 +01:00
|
|
|
|> distinct([e], [e.begins_on, e.id])
|
2021-10-29 10:42:37 +02:00
|
|
|
|> join(:left, [e], p in Participant, on: e.id == p.event_id)
|
|
|
|
|> join(:inner, [_e, p], pa in Actor, on: p.actor_id == pa.id)
|
|
|
|
|> join(:inner, [e], g in Actor, on: e.attributed_to_id == g.id)
|
|
|
|
|> join(:left, [_e, _p, _pa, g], f in Follower, on: g.id == f.target_actor_id)
|
|
|
|
|> join(:left, [_e, _p, _pa, g], m in Member, on: g.id == m.parent_id)
|
|
|
|
|> join(:inner, [_e, _p, pa, _g, f, m], a in Actor,
|
|
|
|
on: a.id == f.actor_id or a.id == m.actor_id
|
|
|
|
)
|
2021-11-02 19:48:38 +01:00
|
|
|
|> add_after_datetime_filter(after_datetime)
|
2021-10-29 10:42:37 +02:00
|
|
|
|> where(
|
2021-11-02 19:48:38 +01:00
|
|
|
[_e, p, pa, _g, f, m, a],
|
|
|
|
(f.approved or m.role in ^[:member, :moderator, :administrator, :creator]) and
|
2021-10-29 10:42:37 +02:00
|
|
|
a.user_id == ^user_id and
|
|
|
|
pa.user_id != ^user_id
|
|
|
|
)
|
|
|
|
|> preload([
|
|
|
|
:organizer_actor,
|
|
|
|
:attributed_to,
|
|
|
|
:tags,
|
|
|
|
:physical_address,
|
|
|
|
:picture
|
|
|
|
])
|
|
|
|
|> select([e, g, _f, _m, a], [
|
|
|
|
e,
|
|
|
|
g,
|
|
|
|
a
|
|
|
|
])
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
2021-11-02 19:48:38 +01:00
|
|
|
|
|
|
|
@spec add_after_datetime_filter(Ecto.Query.t(), DateTime.t() | nil) :: Ecto.Query.t()
|
|
|
|
defp add_after_datetime_filter(query, nil),
|
|
|
|
do: where(query, [e], e.begins_on > ^DateTime.utc_now())
|
|
|
|
|
|
|
|
defp add_after_datetime_filter(query, %DateTime{} = datetime),
|
|
|
|
do: where(query, [e], e.begins_on > ^datetime)
|
2021-10-29 10:42:37 +02:00
|
|
|
end
|