Skip to content

Commit

Permalink
Update based on feedback
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
jauggy committed Dec 15, 2024
1 parent d638fc4 commit 1934cbb
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 18 deletions.
3 changes: 2 additions & 1 deletion lib/teiserver/account/libs/account_test_lib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ defmodule Teiserver.Account.AccountTestLib do
name: data["name"] || "name_#{r}",
email: data["email"] || "email_#{r}",
colour: data["colour"] || "colour",
icon: data["icon"] || "icon"
icon: data["icon"] || "icon",
last_login: data["last_login"] || Timex.now()
},
:script
)
Expand Down
2 changes: 2 additions & 0 deletions lib/teiserver/account/libs/relationship_lib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ defmodule Teiserver.Account.RelationshipLib do
days_not_logged_in
])

decache_relationships(user_id)

results.num_rows
end
end
41 changes: 25 additions & 16 deletions lib/teiserver_web/live/account/relationship/index.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule TeiserverWeb.Account.RelationshipLive.Index do
@moduledoc false
alias Teiserver.Account.FriendLib
alias Teiserver.Account.RelationshipLib
use TeiserverWeb, :live_view
alias Teiserver.Account
Expand All @@ -14,8 +13,8 @@ defmodule TeiserverWeb.Account.RelationshipLive.Index do
|> assign(:view_colour, Account.RelationshipLib.colour())
|> assign(:show_help, false)
|> put_empty_relationships
|> assign(:purge_cutoff, "6 months")
|> assign(:purge_cutoff_options, ["1 month", "3 months", "6 months", "1 year"])
|> assign(:purge_cutoff, get_default_purge_cutoff_option())
|> assign(:purge_cutoff_options, get_purge_cutoff_options())

{:ok, socket}
end
Expand Down Expand Up @@ -267,7 +266,8 @@ defmodule TeiserverWeb.Account.RelationshipLive.Index do

def handle_event("purge-avoids", _params, socket) do
userid = socket.assigns.current_user.id
days = get_purge_days_cutoff(socket)
duration = socket.assigns[:purge_cutoff]
days = get_purge_days_cutoff(duration)
num_rows = RelationshipLib.delete_inactive_ignores_avoids_blocks(userid, days)

socket =
Expand All @@ -277,7 +277,8 @@ defmodule TeiserverWeb.Account.RelationshipLive.Index do
end

def handle_event("purge-friends", _params, socket) do
days_cutoff = get_purge_days_cutoff(socket)
duration = socket.assigns[:purge_cutoff]
days_cutoff = get_purge_days_cutoff(duration)

# Get all friends of this user
friends = socket.assigns[:friends]
Expand Down Expand Up @@ -476,20 +477,28 @@ defmodule TeiserverWeb.Account.RelationshipLive.Index do
|> assign(:blocks, blocks)
end

def get_purge_days_cutoff(socket) do
duration = socket.assigns[:purge_cutoff]
[_, number, type] = Regex.run(~r/(\d) (month|year)/, duration)
{number, _} = Integer.parse(number)
def get_purge_cutoff_options() do
["1 month", "3 months", "6 months", "1 year"]
end

cond do
type == "year" ->
number * 365
def get_default_purge_cutoff_option() do
"6 months"
end

type == "month" ->
number * 365.0 / 12
@spec get_purge_days_cutoff(String.t()) :: float()
def get_purge_days_cutoff(duration) do
with [_, raw_number, type] <- Regex.run(~r/(\d)+ (month|year)/, duration),
{number, ""} <- Integer.parse(raw_number) do
cond do
type == "year" ->
number * 365

true ->
raise("Incorrect value assigned to :purge_cutoff")
type == "month" ->
number * 365.0 / 12
end
else
nil -> {:error, "invalid duration passed: #{duration}"}
{_, _rest} -> {:error, "invalid number in duration #{duration}"}
end
end
end
56 changes: 56 additions & 0 deletions test/teiserver/account/relationship_lib_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
defmodule Teiserver.Account.RelationshipLibTest do
use Teiserver.DataCase, async: true
alias Teiserver.Account
alias Teiserver.Account.FriendLib

alias Teiserver.Account.AccountTestLib
alias Teiserver.Account.RelationshipLib

test "purging inactive relationships" do
# Create two accounts
user1 = AccountTestLib.user_fixture()
user2 = AccountTestLib.user_fixture()

old_login = DateTime.add(Timex.now(), -31, :day)

user3 =
AccountTestLib.user_fixture(%{
"last_login" => old_login
})

assert user1.id != nil
assert user2.id != nil
assert user3.id != nil

RelationshipLib.avoid_user(user1.id, user2.id)
RelationshipLib.avoid_user(user1.id, user3.id)

avoid_list = RelationshipLib.list_userids_avoided_by_userid(user1.id)
assert Enum.member?(avoid_list, user2.id)
assert Enum.member?(avoid_list, user3.id)

# Purge old relationships
RelationshipLib.delete_inactive_ignores_avoids_blocks(user1.id, 30)

avoid_list = RelationshipLib.list_userids_avoided_by_userid(user1.id)
assert Enum.member?(avoid_list, user2.id)
refute Enum.member?(avoid_list, user3.id)

# Add users to ignore list
RelationshipLib.ignore_user(user1.id, user2.id)
RelationshipLib.ignore_user(user1.id, user3.id)

# Check ignores
ignore_list = RelationshipLib.list_userids_ignored_by_userid(user1.id)
assert Enum.member?(ignore_list, user2.id)
assert Enum.member?(ignore_list, user3.id)

# Purge ignores
RelationshipLib.delete_inactive_ignores_avoids_blocks(user1.id, 30)

# Check ignores again
ignore_list = RelationshipLib.list_userids_ignored_by_userid(user1.id)
assert Enum.member?(ignore_list, user2.id)
refute Enum.member?(ignore_list, user3.id)
end
end
21 changes: 20 additions & 1 deletion test/teiserver_web/live/account/relationship/index_live_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule TeiserverWeb.Account.RelationshipLive.IndexLiveTest do
use TeiserverWeb.ConnCase, async: true

alias TeiserverWeb.Account.RelationshipLive.Index
alias Central.Helpers.GeneralTestLib

test "account relationship endpoints requires authentication" do
Expand All @@ -24,4 +24,23 @@ defmodule TeiserverWeb.Account.RelationshipLive.IndexLiveTest do
conn = get(conn, ~p"/account/relationship")
html_response(conn, 200)
end

test "purge cutoff options are valid" do
options = Index.get_purge_cutoff_options()
assert length(options) > 0

default_option = Index.get_default_purge_cutoff_option()
assert Enum.member?(options, default_option)

Enum.map(options, fn option ->
assert is_number(Index.get_purge_days_cutoff(option))
end)
end

test "get_purge_days_cutoff returns error" do
assert Index.get_purge_days_cutoff("20 days") == {:error, "invalid duration passed: 20 days"}

assert Index.get_purge_days_cutoff("x months") ==
{:error, "invalid duration passed: x months"}
end
end

0 comments on commit 1934cbb

Please sign in to comment.