Merge branch 'develop' into oembed_provider
[akkoma] / lib / pleroma / user.ex
index bcc34b38f89717f1bf3bf60eb799842c7764ad08..28ff08a39f01ba1fdcf1cba223a9a33b674340ff 100644 (file)
@@ -11,6 +11,11 @@ defmodule Pleroma.User do
 
   @type t :: %__MODULE__{}
 
+  @email_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
+
+  @strict_local_nickname_regex ~r/^[a-zA-Z\d]+$/
+  @extended_local_nickname_regex ~r/^[a-zA-Z\d_-]+$/
+
   schema "users" do
     field(:bio, :string)
     field(:email, :string)
@@ -77,7 +82,6 @@ defmodule Pleroma.User do
     }
   end
 
-  @email_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
   def remote_user_creation(params) do
     params =
       params
@@ -117,7 +121,7 @@ defmodule Pleroma.User do
     struct
     |> cast(params, [:bio, :name, :avatar])
     |> unique_constraint(:nickname)
-    |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
+    |> validate_format(:nickname, local_nickname_regex())
     |> validate_length(:bio, max: 5000)
     |> validate_length(:name, min: 1, max: 100)
   end
@@ -134,7 +138,7 @@ defmodule Pleroma.User do
     struct
     |> cast(params, [:bio, :name, :follower_address, :avatar, :last_refreshed_at])
     |> unique_constraint(:nickname)
-    |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
+    |> validate_format(:nickname, local_nickname_regex())
     |> validate_length(:bio, max: 5000)
     |> validate_length(:name, max: 100)
     |> put_embed(:info, info_cng)
@@ -172,7 +176,7 @@ defmodule Pleroma.User do
       |> validate_confirmation(:password)
       |> unique_constraint(:email)
       |> unique_constraint(:nickname)
-      |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
+      |> validate_format(:nickname, local_nickname_regex())
       |> validate_format(:email, @email_regex)
       |> validate_length(:bio, max: 1000)
       |> validate_length(:name, min: 1, max: 100)
@@ -212,7 +216,7 @@ defmodule Pleroma.User do
   end
 
   def maybe_direct_follow(%User{} = follower, %User{} = followed) do
-    if !User.ap_enabled?(followed) do
+    if not User.ap_enabled?(followed) do
       follow(follower, followed)
     else
       {:ok, follower}
@@ -290,6 +294,10 @@ defmodule Pleroma.User do
     user.info.locked || false
   end
 
+  def get_by_id(id) do
+    Repo.get_by(User, id: id)
+  end
+
   def get_by_ap_id(ap_id) do
     Repo.get_by(User, ap_id: ap_id)
   end
@@ -316,11 +324,20 @@ defmodule Pleroma.User do
     Cachex.fetch!(:user_cache, key, fn _ -> get_by_ap_id(ap_id) end)
   end
 
+  def get_cached_by_id(id) do
+    key = "id:#{id}"
+    Cachex.fetch!(:user_cache, key, fn _ -> get_by_id(id) end)
+  end
+
   def get_cached_by_nickname(nickname) do
     key = "nickname:#{nickname}"
     Cachex.fetch!(:user_cache, key, fn _ -> get_or_fetch_by_nickname(nickname) end)
   end
 
+  def get_cached_by_nickname_or_id(nickname_or_id) do
+    get_cached_by_nickname(nickname_or_id) || get_cached_by_id(nickname_or_id)
+  end
+
   def get_by_nickname(nickname) do
     Repo.get_by(User, nickname: nickname)
   end
@@ -736,7 +753,8 @@ defmodule Pleroma.User do
         source_data: %{"publicKey" => %{"publicKeyPem" => public_key_pem}}
       }) do
     key =
-      :public_key.pem_decode(public_key_pem)
+      public_key_pem
+      |> :public_key.pem_decode()
       |> hd()
       |> :public_key.pem_entry_decode()
 
@@ -774,13 +792,10 @@ defmodule Pleroma.User do
   def ap_enabled?(%User{info: info}), do: info.ap_enabled
   def ap_enabled?(_), do: false
 
-  def get_or_fetch(uri_or_nickname) do
-    if String.starts_with?(uri_or_nickname, "http") do
-      get_or_fetch_by_ap_id(uri_or_nickname)
-    else
-      get_or_fetch_by_nickname(uri_or_nickname)
-    end
-  end
+  @doc "Gets or fetch a user by uri or nickname."
+  @spec get_or_fetch(String.t()) :: User.t()
+  def get_or_fetch("http" <> _host = uri), do: get_or_fetch_by_ap_id(uri)
+  def get_or_fetch(nickname), do: get_or_fetch_by_nickname(nickname)
 
   # wait a period of time and return newest version of the User structs
   # this is because we have synchronous follow APIs and need to simulate them
@@ -821,7 +836,9 @@ defmodule Pleroma.User do
         {String.trim(name, ":"), url}
       end)
 
-    CommonUtils.format_input(bio, mentions, tags, "text/plain") |> Formatter.emojify(emoji)
+    bio
+    |> CommonUtils.format_input(mentions, tags, "text/plain")
+    |> Formatter.emojify(emoji)
   end
 
   def tag(user_identifiers, tags) when is_list(user_identifiers) do
@@ -861,4 +878,12 @@ defmodule Pleroma.User do
     |> List.flatten()
     |> Enum.map(&String.downcase(&1))
   end
+
+  defp local_nickname_regex() do
+    if Pleroma.Config.get([:instance, :extended_nickname_format]) do
+      @extended_local_nickname_regex
+    else
+      @strict_local_nickname_regex
+    end
+  end
 end