Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display active users only in Activity Log filter #3084

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions lib/trento/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ defmodule Trento.Users do
|> Repo.all()
end

@doc """
Returns all usernames tupled with the deleted_at timestamp, including those for users that are soft-deleted.
"""
@spec list_all_usernames :: list({String.t(), DateTime.t()})
def list_all_usernames do
User
|> select([u], {u.username, u.deleted_at})
|> Repo.all()
end

def get_user(id) do
case User
|> where([u], is_nil(u.deleted_at) and u.id == ^id)
Expand Down
17 changes: 2 additions & 15 deletions lib/trento_web/channels/activity_log_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,13 @@ defmodule TrentoWeb.ActivityLogChannel do

@impl true
def handle_info(:after_join, socket) do
all_usernames_ts = Users.list_all_usernames()
collapsed_usernames = collapse_usernames(all_usernames_ts)
all_active_users = Enum.map(Users.list_users(), & &1.username)

users = ["system" | collapsed_usernames]
users = ["system" | all_active_users]
push(socket, "al_users_pushed", %{users: users})
Process.send_after(self(), :after_join, @refresh_interval)
{:noreply, socket}
end

defp allowed?(user_id, current_user_id), do: String.to_integer(user_id) == current_user_id

defp collapse_usernames(usernames_ts) do
usernames_ts
|> Enum.map(fn
{username, nil} ->
username

{username, deleted_at} ->
String.trim_trailing(username, "__" <> DateTime.to_string(deleted_at))
end)
|> Enum.uniq()
end
end
19 changes: 0 additions & 19 deletions test/trento/users_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,6 @@ defmodule Trento.UsersTest do
assert length(users) == 1
end

test "list_all_usernames returns all usernames tupled with the deleted_at field, including those for deleted users" do
%{username: username1, deleted_at: deleted_at1} = insert(:user)

%{username: username2, deleted_at: deleted_at2} =
insert(:user, deleted_at: DateTime.utc_now())

sorter_fn = fn {username, _deleted_at} -> username end

inserted_sorted_usernames =
Enum.sort_by([{username1, deleted_at1}, {username2, deleted_at2}], sorter_fn)

sorted_usernames =
Enum.sort_by(Users.list_all_usernames(), sorter_fn)

assert inserted_sorted_usernames == sorted_usernames
assert length(sorted_usernames) == 2
assert length(inserted_sorted_usernames) == 2
end

test "get_user returns a user when the user exist" do
%{id: user_id} = insert(:user)

Expand Down
Loading