From 9c638c0e3b345733fcb466c478624ebb9265a1d8 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Thu, 14 Nov 2019 22:30:19 -0500 Subject: [PATCH] fix filters page for authenticated users --- lib/philomena/users/user.ex | 11 ++------ .../controllers/filter/current_controller.ex | 28 ++++++++++++------- .../templates/filter/_filter.html.slime | 2 +- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/philomena/users/user.ex b/lib/philomena/users/user.ex index 80b15478..383d1934 100644 --- a/lib/philomena/users/user.ex +++ b/lib/philomena/users/user.ex @@ -117,14 +117,9 @@ defmodule Philomena.Users.User do |> validate_required([]) end - def failure_changeset(user) do - changeset = change(user) - user = changeset.data - - user - |> change(%{ - failed_attempts: user.failed_attempts + 1, - }) + def filter_changeset(user, filter) do + change(user) + |> put_change(:current_filter_id, filter.id) end def create_totp_secret_changeset(user) do diff --git a/lib/philomena_web/controllers/filter/current_controller.ex b/lib/philomena_web/controllers/filter/current_controller.ex index 82f6b1e6..c71f0390 100644 --- a/lib/philomena_web/controllers/filter/current_controller.ex +++ b/lib/philomena_web/controllers/filter/current_controller.ex @@ -1,7 +1,8 @@ defmodule PhilomenaWeb.Filter.CurrentController do use PhilomenaWeb, :controller - alias Philomena.{Filters, Filters.Filter} + alias Philomena.{Filters, Filters.Filter, Users.User} + alias Philomena.Repo plug :load_resource, model: Filter @@ -16,16 +17,23 @@ defmodule PhilomenaWeb.Filter.CurrentController do Filters.default_filter() end - conn = - if user do - nil - else - conn - |> put_session(:filter_id, filter.id) - end + conn + |> update_filter(user, filter) + |> put_flash(:info, "Switched to filter #{filter.name}") + |> redirect(to: Routes.filter_path(conn, :index)) + end + + defp update_filter(conn, nil, filter) do + conn + |> put_session(:filter_id, filter.id) + end + + defp update_filter(conn, user, filter) do + {:ok, user} = + user + |> User.filter_changeset(filter) + |> Repo.update() conn - |> put_flash(:info, "Switched to filter #{filter.name}") - |> redirect(to: "/") end end diff --git a/lib/philomena_web/templates/filter/_filter.html.slime b/lib/philomena_web/templates/filter/_filter.html.slime index e807f8e6..36617ac3 100644 --- a/lib/philomena_web/templates/filter/_filter.html.slime +++ b/lib/philomena_web/templates/filter/_filter.html.slime @@ -24,7 +24,7 @@ strong Your current filter - else li - = button_to "Use this filter", Routes.filter_current_path(@conn, :update, @filter), method: "put", class: "button" + = button_to "Use this filter", Routes.filter_current_path(@conn, :update, id: @filter), method: "put", class: "button" p em = @filter.description