From d638fc4ab1c43ff8b05e74503fe2477cdf2376d4 Mon Sep 17 00:00:00 2001 From: jauggy Date: Wed, 11 Dec 2024 15:10:37 +1100 Subject: [PATCH] Purge inactive friends and avoids --- .../account/libs/relationship_lib.ex | 21 +++++ .../live/account/relationship/index.ex | 79 +++++++++++++++++++ .../live/account/relationship/index.html.heex | 54 +++++++++++++ lib/teiserver_web/router.ex | 1 + 4 files changed, 155 insertions(+) diff --git a/lib/teiserver/account/libs/relationship_lib.ex b/lib/teiserver/account/libs/relationship_lib.ex index e186021cb..5cafec271 100644 --- a/lib/teiserver/account/libs/relationship_lib.ex +++ b/lib/teiserver/account/libs/relationship_lib.ex @@ -388,4 +388,25 @@ defmodule Teiserver.Account.RelationshipLib do results.rows end + + # Deletes inactive users in the ignore/avoid/block list of a user + # Returns the number of deletions + def delete_inactive_ignores_avoids_blocks(user_id, days_not_logged_in) do + query = """ + delete from account_relationships ar + using account_users au + where au.id = ar.to_user_id + and ar.from_user_id = $1 + and (au.last_login is null OR + abs(DATE_PART('day', (now()- au.last_login ))) > $2); + """ + + results = + Ecto.Adapters.SQL.query!(Repo, query, [ + user_id, + days_not_logged_in + ]) + + results.num_rows + end end diff --git a/lib/teiserver_web/live/account/relationship/index.ex b/lib/teiserver_web/live/account/relationship/index.ex index 596b47829..215470b62 100644 --- a/lib/teiserver_web/live/account/relationship/index.ex +++ b/lib/teiserver_web/live/account/relationship/index.ex @@ -1,5 +1,7 @@ defmodule TeiserverWeb.Account.RelationshipLive.Index do @moduledoc false + alias Teiserver.Account.FriendLib + alias Teiserver.Account.RelationshipLib use TeiserverWeb, :live_view alias Teiserver.Account @@ -12,6 +14,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"]) {:ok, socket} end @@ -54,6 +58,12 @@ defmodule TeiserverWeb.Account.RelationshipLive.Index do |> update_user_search end + defp apply_action(socket, :clean, _params) do + socket + |> assign(:page_title, "Relationships - Cleanup") + |> assign(:tab, :clean) + end + @impl true def handle_event("show-help", _, socket) do {:noreply, socket |> assign(:show_help, true)} @@ -255,6 +265,58 @@ defmodule TeiserverWeb.Account.RelationshipLive.Index do {:noreply, socket} end + def handle_event("purge-avoids", _params, socket) do + userid = socket.assigns.current_user.id + days = get_purge_days_cutoff(socket) + num_rows = RelationshipLib.delete_inactive_ignores_avoids_blocks(userid, days) + + socket = + socket |> assign(:purge_avoids_message, "#{num_rows} inactive users purged.") + + {:noreply, socket} + end + + def handle_event("purge-friends", _params, socket) do + days_cutoff = get_purge_days_cutoff(socket) + + # Get all friends of this user + friends = socket.assigns[:friends] + + num_friends_deleted = + Enum.map(friends, fn friend -> + last_login = friend.other_user.last_login + days = abs(DateTime.diff(last_login, Timex.now(), :day)) + should_delete? = days > days_cutoff + + if(should_delete?) do + # Delete friend + Account.delete_friend(friend) + end + + should_delete? + end) + |> Enum.filter(fn deleted? -> deleted? == true end) + |> length() + + socket = + socket |> assign(:purge_friends_message, "#{num_friends_deleted} inactive friends purged.") + + {:noreply, socket} + end + + @doc """ + Handles the dropdown for purge cutoff time + """ + @impl true + def handle_event("update-purge-cutoff", event, socket) do + [key] = event["_target"] + value = event[key] + + {:noreply, + socket + |> assign(:purge_cutoff, value)} + end + defp update_user_search( %{assigns: %{live_action: :search, search_terms: terms} = assigns} = socket ) do @@ -413,4 +475,21 @@ defmodule TeiserverWeb.Account.RelationshipLive.Index do |> assign(:ignores, ignores) |> 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) + + cond do + type == "year" -> + number * 365 + + type == "month" -> + number * 365.0 / 12 + + true -> + raise("Incorrect value assigned to :purge_cutoff") + end + end end diff --git a/lib/teiserver_web/live/account/relationship/index.html.heex b/lib/teiserver_web/live/account/relationship/index.html.heex index 3c6f2fcc2..9c0b1bb73 100644 --- a/lib/teiserver_web/live/account/relationship/index.html.heex +++ b/lib/teiserver_web/live/account/relationship/index.html.heex @@ -23,6 +23,9 @@ <.tab_nav url={~p"/account/relationship/search"} selected={@tab == :search}> Search users + <.tab_nav url={~p"/account/relationship/clean"} selected={@tab == :clean}> + Cleanup + @@ -412,3 +415,54 @@ + +
+
+

+ Avoid Cleanup +

+ +
+ + Purge users from your ignore, avoid, and block list that have not logged in for: + + <.input + type="select" + options={@purge_cutoff_options} + name="algorithm" + value={@purge_cutoff} + phx-change="update-purge-cutoff" + /> +
+ Purge inactive ignores / avoids / blocks +
+ <%= if(assigns[:purge_avoids_message]) do %> +

<%= @purge_avoids_message %>

+ <% end %> +
+
+
+
+

+ Friend Cleanup +

+
+ + Purge users from your friend list that have not logged in for: + + <.input + type="select" + options={@purge_cutoff_options} + name="algorithm" + value={@purge_cutoff} + phx-change="update-purge-cutoff" + /> +
+ Purge inactive Friends +
+ <%= if(assigns[:purge_friends_message]) do %> +

<%= @purge_friends_message %>

+ <% end %> +
+
+
diff --git a/lib/teiserver_web/router.ex b/lib/teiserver_web/router.ex index af71d69a0..d43965432 100644 --- a/lib/teiserver_web/router.ex +++ b/lib/teiserver_web/router.ex @@ -226,6 +226,7 @@ defmodule TeiserverWeb.Router do live "/relationship/follow", RelationshipLive.Index, :follow live "/relationship/avoid", RelationshipLive.Index, :avoid live "/relationship/search", RelationshipLive.Index, :search + live "/relationship/clean", RelationshipLive.Index, :clean end live_session :account_settings,