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

Ensured a user with no photo or about me story image can hibernate , closes #1139 , closes #1134 #1156

Merged
merged 6 commits into from
Nov 21, 2024

Conversation

MICHAELMUNAVU83
Copy link
Collaborator

  • I ensured if a user has been hibernated due to lack of profile photo they see these flash message
Screen.Recording.2024-11-18.at.16.47.53.mov

"You need to have a profile photo and a photo for your about me story to change your profile visibility." - If a user user has no profile photo and no about me story image

"You need to have a profile photo to change your profile visibility." - If a user has no profile photo

"You need to have a photo for your about me story to change your profile visibility." - If a user has no about me story image

@MICHAELMUNAVU83 MICHAELMUNAVU83 marked this pull request as ready for review November 18, 2024 16:03
@MICHAELMUNAVU83 MICHAELMUNAVU83 changed the title Ensured a user with no photo or about me story image can hibernate Ensured a user with no photo or about me story image can hibernate , closes #1139 Nov 19, 2024
@@ -107,6 +104,92 @@ defmodule AniminaWeb.ProfileVisibilityLive do
User.normalize(user)
end

defp maybe_change_user_state("normalize", socket, nil, false) do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please DRY this code. Something like

defp maybe_change_user_state("normalize", socket, profile_photo, about_me_photo) do
  cond do
    is_nil(profile_photo) and not about_me_photo ->
      {:noreply, show_error(socket, "You need to have a profile photo and a photo for your about me story to change your profile visibility.")}

    is_nil(profile_photo) and about_me_photo ->
      {:noreply, show_error(socket, "You need to have a profile photo to change your profile visibility.")}

    not about_me_photo ->
      {:noreply, show_error(socket, "You need to have a photo for your about me story to change your profile visibility.")}

    true ->
      handle_state_change("normalize", socket)
  end
end

defp maybe_change_user_state("hibernate", socket, _profile_photo, _about_me_photo) do
  handle_state_change("hibernate", socket)
end

defp show_error(socket, message) do
  socket
  |> put_flash(:error, with_locale(socket.assigns.language, fn -> gettext(message) end))
end

defp handle_state_change(action, socket) do
  case change_user_state(action, socket.assigns.current_user) do
    {:ok, user} ->
      {:noreply,
       socket
       |> assign(current_user: user)
       |> put_flash(
         :info,
         with_locale(socket.assigns.language, fn ->
           gettext("Profile visibility changed successfully.")
         end)
       )}

    {:error, _} ->
      {:noreply,
       socket
       |> put_flash(
         :error,
         with_locale(socket.assigns.language, fn ->
           gettext("Profile visibility change failed.")
         end)
       )}
  end
end

@MICHAELMUNAVU83 MICHAELMUNAVU83 changed the title Ensured a user with no photo or about me story image can hibernate , closes #1139 Ensured a user with no photo or about me story image can hibernate , closes #1139 , closes #1134 Nov 20, 2024
Copy link
Collaborator

@wintermeyer wintermeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure: Do we have the policies in place to ensure that the state can not be changed in these circumstances? I want to be sure that a potential black hat hacker doesn't play around with our API or form to bypass this.

@@ -107,6 +104,74 @@ defmodule AniminaWeb.ProfileVisibilityLive do
User.normalize(user)
end

defp maybe_change_user_state("normalize", socket, profile_photo, about_me_photo) do
cond do
is_nil(profile_photo) and not about_me_photo ->
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cries for more refactoring. Too much duplication. Something like:

defp maybe_change_user_state("normalize", socket, profile_photo, about_me_photo) do
  case {is_nil(profile_photo), not about_me_photo} do
    {true, true} ->
      show_visibility_error(socket, "You need to have a profile photo and a photo for your about me story to change your profile visibility.")

    {true, false} ->
      show_visibility_error(socket, "You need to have a profile photo to change your profile visibility.")

    {false, true} ->
      show_visibility_error(socket, "You need to have a photo for your about me story to change your profile visibility.")

    _ ->
      handle_state_change("normalize", socket)
  end
end

defp show_visibility_error(socket, message) do
  {:noreply,
   socket
   |> show_error(with_locale(socket.assigns.language, fn -> message end))}
end

@MICHAELMUNAVU83
Copy link
Collaborator Author

I have added a policy for reactivating to ensure we check the user has a profile photo and has about me story with an image

@wintermeyer wintermeyer merged commit 6a9ca4d into main Nov 21, 2024
1 check passed
@wintermeyer wintermeyer deleted the hibernate-change-popup branch November 21, 2024 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants