Add active user count
authorEgor Kislitsyn <egor@kislitsyn.com>
Fri, 22 Jan 2021 20:37:49 +0000 (00:37 +0400)
committerEgor Kislitsyn <egor@kislitsyn.com>
Wed, 27 Jan 2021 14:20:06 +0000 (18:20 +0400)
lib/pleroma/user.ex
lib/pleroma/web/mastodon_api/views/instance_view.ex
lib/pleroma/web/plugs/user_tracking_plug.ex [new file with mode: 0644]
lib/pleroma/web/router.ex
priv/repo/migrations/20210122151424_add_last_active_at_to_users.exs [new file with mode: 0644]
test/pleroma/web/mastodon_api/controllers/status_controller_test.exs
test/pleroma/web/pleroma_api/controllers/conversation_controller_test.exs

index e422b59f18c8d0d63148f0d4007b46d6c721d453..1dde653357920c3358ee1b1b239508cca576084e 100644 (file)
@@ -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,
@@ -2444,4 +2445,18 @@ 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(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)
+    |> Repo.aggregate(:count)
+  end
 end
index 1edbdbe11de2c3f2d74dc2a09f20b23b384ade47..73205fb6db500e31205bffc3d278b4da15b6620b 100644 (file)
@@ -45,6 +45,7 @@ defmodule Pleroma.Web.MastodonAPI.InstanceView do
           fields_limits: fields_limits(),
           post_formats: Config.get([:instance, :allowed_post_formats])
         },
+        stats: %{mau: Pleroma.User.active_user_count()},
         vapid_public_key: Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
       }
     }
diff --git a/lib/pleroma/web/plugs/user_tracking_plug.ex b/lib/pleroma/web/plugs/user_tracking_plug.ex
new file mode 100644 (file)
index 0000000..c9a988f
--- /dev/null
@@ -0,0 +1,30 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.Plugs.UserTrackingPlug do
+  alias Pleroma.User
+
+  import Plug.Conn, only: [assign: 3]
+
+  @update_interval :timer.hours(24)
+
+  def init(opts), do: opts
+
+  def call(%{assigns: %{user: %User{id: id} = user}} = conn, _) when not is_nil(id) do
+    with true <- needs_update?(user),
+         {:ok, user} <- User.update_last_active_at(user) do
+      assign(conn, :user, user)
+    else
+      _ -> conn
+    end
+  end
+
+  def call(conn, _), do: conn
+
+  defp needs_update?(%User{last_active_at: nil}), do: true
+
+  defp needs_update?(%User{last_active_at: last_active_at}) do
+    NaiveDateTime.diff(NaiveDateTime.utc_now(), last_active_at, :millisecond) >= @update_interval
+  end
+end
index a9e332fa1c4ce3e298d3d2337385b7ad497df268..7521f5dc34fce8eeab5731a777e850f17188886f 100644 (file)
@@ -56,6 +56,7 @@ defmodule Pleroma.Web.Router do
     plug(Pleroma.Web.Plugs.UserEnabledPlug)
     plug(Pleroma.Web.Plugs.SetUserSessionIdPlug)
     plug(Pleroma.Web.Plugs.EnsureUserTokenAssignsPlug)
+    plug(Pleroma.Web.Plugs.UserTrackingPlug)
   end
 
   pipeline :base_api do
diff --git a/priv/repo/migrations/20210122151424_add_last_active_at_to_users.exs b/priv/repo/migrations/20210122151424_add_last_active_at_to_users.exs
new file mode 100644 (file)
index 0000000..9671e49
--- /dev/null
@@ -0,0 +1,11 @@
+defmodule Pleroma.Repo.Migrations.AddLastActiveAtToUsers do
+  use Ecto.Migration
+
+  def change do
+    alter table(:users) do
+      add(:last_active_at, :naive_datetime)
+    end
+
+    create_if_not_exists(index(:users, [:last_active_at]))
+  end
+end
index a647cd57fc3732c4be7773301ded9a7da8180daf..3c73eb514597637625adab4d36d5dbedfd64bf00 100644 (file)
@@ -263,6 +263,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
 
       fake_conn =
         conn
+        |> assign(:user, refresh_record(conn.assigns.user))
         |> put_req_header("content-type", "application/json")
         |> post("/api/v1/statuses", %{
           "status" =>
index 98a23aaaadd9cecc332c920fb34b5888318f4105..54f2c5a58c10812ec08bd2e9afb6a4f89d7606fa 100644 (file)
@@ -104,7 +104,7 @@ defmodule Pleroma.Web.PleromaAPI.ConversationControllerTest do
     [participation] = Participation.for_user(user)
     participation = Repo.preload(participation, :recipients)
 
-    assert user in participation.recipients
+    assert refresh_record(user) in participation.recipients
     assert other_user in participation.recipients
   end