Merge remote-tracking branch 'remotes/origin/develop' into feature/object-hashtags...
[akkoma] / lib / pleroma / user.ex
index e53a0f313d168fca912d0f4b246211c913155d43..b69709db40e53304942dd34a318df0c92a8addd2 100644 (file)
@@ -110,9 +110,9 @@ defmodule Pleroma.User do
     field(:follower_count, :integer, default: 0)
     field(:following_count, :integer, default: 0)
     field(:is_locked, :boolean, default: false)
-    field(:confirmation_pending, :boolean, default: false)
+    field(:is_confirmed, :boolean, default: true)
     field(:password_reset_pending, :boolean, default: false)
-    field(:approval_pending, :boolean, default: false)
+    field(:is_approved, :boolean, default: true)
     field(:registration_reason, :string, default: nil)
     field(:confirmation_token, :string, default: nil)
     field(:default_scope, :string, default: "public")
@@ -146,6 +146,7 @@ defmodule Pleroma.User do
     field(:inbox, :string)
     field(:shared_inbox, :string)
     field(:accepts_chat_messages, :boolean, default: nil)
+    field(:last_active_at, :naive_datetime)
 
     embeds_one(
       :notification_settings,
@@ -289,16 +290,8 @@ defmodule Pleroma.User do
   @spec account_status(User.t()) :: account_status()
   def account_status(%User{is_active: false}), do: :deactivated
   def account_status(%User{password_reset_pending: true}), do: :password_reset_pending
-  def account_status(%User{local: true, approval_pending: true}), do: :approval_pending
-
-  def account_status(%User{local: true, confirmation_pending: true}) do
-    if Config.get([:instance, :account_activation_required]) do
-      :confirmation_pending
-    else
-      :active
-    end
-  end
-
+  def account_status(%User{local: true, is_approved: false}), do: :approval_pending
+  def account_status(%User{local: true, is_confirmed: false}), do: :confirmation_pending
   def account_status(%User{}), do: :active
 
   @spec visible_for(User.t(), User.t() | nil) ::
@@ -700,23 +693,23 @@ defmodule Pleroma.User do
     reason_limit = Config.get([:instance, :registration_reason_length], 500)
     params = Map.put_new(params, :accepts_chat_messages, true)
 
-    need_confirmation? =
-      if is_nil(opts[:need_confirmation]) do
-        Config.get([:instance, :account_activation_required])
+    confirmed? =
+      if is_nil(opts[:confirmed]) do
+        !Config.get([:instance, :account_activation_required])
       else
-        opts[:need_confirmation]
+        opts[:confirmed]
       end
 
-    need_approval? =
-      if is_nil(opts[:need_approval]) do
-        Config.get([:instance, :account_approval_required])
+    approved? =
+      if is_nil(opts[:approved]) do
+        !Config.get([:instance, :account_approval_required])
       else
-        opts[:need_approval]
+        opts[:approved]
       end
 
     struct
-    |> confirmation_changeset(need_confirmation: need_confirmation?)
-    |> approval_changeset(need_approval: need_approval?)
+    |> confirmation_changeset(set_confirmation: confirmed?)
+    |> approval_changeset(set_approval: approved?)
     |> cast(params, [
       :bio,
       :raw_bio,
@@ -804,20 +797,20 @@ defmodule Pleroma.User do
     end
   end
 
-  def post_register_action(%User{confirmation_pending: true} = user) do
+  def post_register_action(%User{is_confirmed: false} = user) do
     with {:ok, _} <- try_send_confirmation_email(user) do
       {:ok, user}
     end
   end
 
-  def post_register_action(%User{approval_pending: true} = user) do
+  def post_register_action(%User{is_approved: false} = user) do
     with {:ok, _} <- send_user_approval_email(user),
          {:ok, _} <- send_admin_approval_emails(user) do
       {:ok, user}
     end
   end
 
-  def post_register_action(%User{approval_pending: false, confirmation_pending: false} = user) do
+  def post_register_action(%User{is_approved: true, is_confirmed: true} = user) do
     with {:ok, user} <- autofollow_users(user),
          {:ok, _} <- autofollowing_users(user),
          {:ok, user} <- set_cache(user),
@@ -878,7 +871,7 @@ defmodule Pleroma.User do
   def send_welcome_email(_), do: {:ok, :noop}
 
   @spec try_send_confirmation_email(User.t()) :: {:ok, :enqueued | :noop}
-  def try_send_confirmation_email(%User{confirmation_pending: true, email: email} = user)
+  def try_send_confirmation_email(%User{is_confirmed: false, email: email} = user)
       when is_binary(email) do
     if Config.get([:instance, :account_activation_required]) do
       send_confirmation_email(user)
@@ -942,7 +935,7 @@ defmodule Pleroma.User do
     deny_follow_blocked = Config.get([:user, :deny_follow_blocked])
 
     cond do
-      followed.is_active == false ->
+      not followed.is_active ->
         {:error, "Could not follow user: #{followed.nickname} is deactivated."}
 
       deny_follow_blocked and blocks?(followed, follower) ->
@@ -1620,8 +1613,8 @@ defmodule Pleroma.User do
     end)
   end
 
-  def approve(%User{approval_pending: true} = user) do
-    with chg <- change(user, approval_pending: false),
+  def approve(%User{is_approved: false} = user) do
+    with chg <- change(user, is_approved: true),
          {:ok, user} <- update_and_set_cache(chg) do
       post_register_action(user)
       {:ok, user}
@@ -1638,8 +1631,8 @@ defmodule Pleroma.User do
     end)
   end
 
-  def confirm(%User{confirmation_pending: true} = user) do
-    with chg <- confirmation_changeset(user, need_confirmation: false),
+  def confirm(%User{is_confirmed: false} = user) do
+    with chg <- confirmation_changeset(user, set_confirmation: true),
          {:ok, user} <- update_and_set_cache(chg) do
       post_register_action(user)
       {:ok, user}
@@ -1678,9 +1671,9 @@ defmodule Pleroma.User do
       follower_count: 0,
       following_count: 0,
       is_locked: false,
-      confirmation_pending: false,
+      is_confirmed: true,
       password_reset_pending: false,
-      approval_pending: false,
+      is_approved: true,
       registration_reason: nil,
       confirmation_token: nil,
       domain_blocks: [],
@@ -2038,6 +2031,15 @@ defmodule Pleroma.User do
     |> hd()
   end
 
+  def full_nickname(%User{} = user) do
+    if String.contains?(user.nickname, "@") do
+      user.nickname
+    else
+      %{host: host} = URI.parse(user.ap_id)
+      user.nickname <> "@" <> host
+    end
+  end
+
   def full_nickname(nickname_or_mention),
     do: String.trim_leading(nickname_or_mention, "@")
 
@@ -2134,10 +2136,10 @@ defmodule Pleroma.User do
     updated_user
   end
 
-  @spec need_confirmation(User.t(), boolean()) :: {:ok, User.t()} | {:error, Changeset.t()}
-  def need_confirmation(%User{} = user, bool) do
+  @spec set_confirmation(User.t(), boolean()) :: {:ok, User.t()} | {:error, Changeset.t()}
+  def set_confirmation(%User{} = user, bool) do
     user
-    |> confirmation_changeset(need_confirmation: bool)
+    |> confirmation_changeset(set_confirmation: bool)
     |> update_and_set_cache()
   end
 
@@ -2305,27 +2307,26 @@ defmodule Pleroma.User do
   end
 
   @spec confirmation_changeset(User.t(), keyword()) :: Changeset.t()
-  def confirmation_changeset(user, need_confirmation: need_confirmation?) do
+  def confirmation_changeset(user, set_confirmation: confirmed?) do
     params =
-      if need_confirmation? do
+      if confirmed? do
         %{
-          confirmation_pending: true,
-          confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
+          is_confirmed: true,
+          confirmation_token: nil
         }
       else
         %{
-          confirmation_pending: false,
-          confirmation_token: nil
+          is_confirmed: false,
+          confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
         }
       end
 
-    cast(user, params, [:confirmation_pending, :confirmation_token])
+    cast(user, params, [:is_confirmed, :confirmation_token])
   end
 
   @spec approval_changeset(User.t(), keyword()) :: Changeset.t()
-  def approval_changeset(user, need_approval: need_approval?) do
-    params = if need_approval?, do: %{approval_pending: true}, else: %{approval_pending: false}
-    cast(user, params, [:approval_pending])
+  def approval_changeset(user, set_approval: approved?) do
+    cast(user, %{is_approved: approved?}, [:is_approved])
   end
 
   def add_pinnned_activity(user, %Pleroma.Activity{id: id}) do
@@ -2453,4 +2454,19 @@ defmodule Pleroma.User do
   def get_host(%User{ap_id: ap_id} = _user) do
     URI.parse(ap_id).host
   end
+
+  def update_last_active_at(%__MODULE__{local: true} = user) do
+    user
+    |> cast(%{last_active_at: NaiveDateTime.utc_now()}, [:last_active_at])
+    |> update_and_set_cache()
+  end
+
+  def active_user_count(weeks \\ 4) do
+    active_after = Timex.shift(NaiveDateTime.utc_now(), weeks: -weeks)
+
+    __MODULE__
+    |> where([u], u.last_active_at >= ^active_after)
+    |> where([u], u.local == true)
+    |> Repo.aggregate(:count)
+  end
 end