An IndieWeb engine for a self-hostable website. https://koype.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

profile.ex 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Koype: a IndieWeb-focused, single-tenant website engine for people.
  2. #
  3. # Copyright © 2019 Jacky Alciné <jacky.is@black.af>
  4. #
  5. # This file belongs to the Koype project.
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. defmodule Koype.Profile do
  20. @moduledoc false
  21. @required_fields ~w(nickname email)
  22. alias Koype.Setting
  23. require Logger
  24. def complete?() do
  25. case Koype.Setting.get("profile:complete") do
  26. nil ->
  27. completeness = compute_completeness()
  28. Koype.Setting.set("profile:complete", to_string(completeness))
  29. completeness
  30. completeness ->
  31. completeness == "true"
  32. end
  33. end
  34. def displayed_name, do: get(Setting.get("displayed_name", "nickname"), "koype")
  35. def set(field, value) do
  36. with(
  37. :ok <- assert_required_field_is_set(field, value),
  38. :ok <- Setting.set("hCard_#{field}", value)
  39. ) do
  40. :ok
  41. else
  42. {:error, %Ecto.Changeset{} = cs} -> {:error, cs.errors}
  43. {:error, error} -> {:error, error}
  44. end
  45. end
  46. def get(field, value \\ nil), do: Setting.get("hCard_#{field}", value)
  47. def name(), do: get("name")
  48. def nickname(), do: get("nickname")
  49. def note(), do: get("note")
  50. def email(), do: get("email")
  51. def timezone(), do: get("timezone", "Etc/UTC")
  52. def photo_uri() do
  53. Koype.Web.Router.Helpers.media_url(Koype.Web.Endpoint, :serve_profile_image)
  54. end
  55. def photo(version \\ :original) do
  56. case get("photo") do
  57. nil ->
  58. Koype.host() <> Koype.Web.LayoutView.asset_path_for(:core, "/images/default-avatar.png")
  59. path ->
  60. Koype.host() <> Koype.Storage.Image.path({path, :floating}, version)
  61. end
  62. end
  63. def set_photo(nil), do: {:error, :invalid_photo_data}
  64. def set_photo(%Plug.Upload{} = photo) do
  65. case Koype.Storage.Image.store({photo, :floating}) do
  66. {:ok, url} ->
  67. Logger.info("Uploaded #{url} as the user's avatar.")
  68. set("photo", url)
  69. {:error, _} = error ->
  70. error
  71. end
  72. end
  73. defp assert_required_field_is_set(field, nil) when field in @required_fields,
  74. do: {:error, :required_field}
  75. defp assert_required_field_is_set(_, _), do: :ok
  76. def compute_completeness() do
  77. cond do
  78. displayed_name() == "koype" -> false
  79. true -> true
  80. end
  81. end
  82. end