diff --git a/CHANGELOG.md b/CHANGELOG.md
index c4f3867a2..905e35f26 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -49,6 +49,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Admin API: An endpoint to manage frontends.
- Streaming API: Add follow relationships updates.
- WebPush: Introduce `pleroma:chat_mention` and `pleroma:emoji_reaction` notification types
+- Mastodon API: Add monthly active users to `/api/v1/instance` (`pleroma.stats.mau`)
### Fixed
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex
index e422b59f1..06cdb42af 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -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,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
diff --git a/lib/pleroma/web/mastodon_api/views/instance_view.ex b/lib/pleroma/web/mastodon_api/views/instance_view.ex
index 1edbdbe11..73205fb6d 100644
--- a/lib/pleroma/web/mastodon_api/views/instance_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/instance_view.ex
@@ -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
index 000000000..c9a988f00
--- /dev/null
+++ b/lib/pleroma/web/plugs/user_tracking_plug.ex
@@ -0,0 +1,30 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors
+# 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
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index a9e332fa1..7521f5dc3 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -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
index 000000000..9671e495b
--- /dev/null
+++ b/priv/repo/migrations/20210122151424_add_last_active_at_to_users.exs
@@ -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
diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs
index b4df22c2c..ae6fc4c0d 100644
--- a/test/pleroma/user_test.exs
+++ b/test/pleroma/user_test.exs
@@ -2248,4 +2248,43 @@ defmodule Pleroma.UserTest do
user = insert(:user, ap_id: "https://lain.com/users/lain", nickname: "lain")
assert User.get_host(user) == "lain.com"
end
+
+ test "update_last_active_at/1" do
+ user = insert(:user)
+ assert is_nil(user.last_active_at)
+
+ test_started_at = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
+
+ assert {:ok, user} = User.update_last_active_at(user)
+
+ assert user.last_active_at >= test_started_at
+ assert user.last_active_at <= NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
+
+ last_active_at =
+ NaiveDateTime.utc_now()
+ |> NaiveDateTime.add(-:timer.hours(24), :millisecond)
+ |> NaiveDateTime.truncate(:second)
+
+ assert {:ok, user} =
+ user
+ |> cast(%{last_active_at: last_active_at}, [:last_active_at])
+ |> User.update_and_set_cache()
+
+ assert user.last_active_at == last_active_at
+ assert {:ok, user} = User.update_last_active_at(user)
+ assert user.last_active_at >= test_started_at
+ assert user.last_active_at <= NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
+ end
+
+ test "active_user_count/1" do
+ insert(:user)
+ insert(:user, %{local: false})
+ insert(:user, %{last_active_at: Timex.shift(NaiveDateTime.utc_now(), weeks: -5)})
+ insert(:user, %{last_active_at: Timex.shift(NaiveDateTime.utc_now(), weeks: -3)})
+ insert(:user, %{last_active_at: NaiveDateTime.utc_now()})
+
+ assert User.active_user_count() == 2
+ assert User.active_user_count(6) == 3
+ assert User.active_user_count(1) == 1
+ end
end
diff --git a/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs
index 0d4eebb73..b99856659 100644
--- a/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs
+++ b/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs
@@ -47,6 +47,7 @@ defmodule Pleroma.Web.MastodonAPI.InstanceControllerTest do
assert result["pleroma"]["metadata"]["federation"]
assert result["pleroma"]["metadata"]["fields_limits"]
assert result["pleroma"]["vapid_public_key"]
+ assert result["pleroma"]["stats"]["mau"] == 0
assert email == from_config_email
assert thumbnail == from_config_thumbnail
diff --git a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs
index a647cd57f..3c73eb514 100644
--- a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs
+++ b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs
@@ -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" =>
diff --git a/test/pleroma/web/pleroma_api/controllers/conversation_controller_test.exs b/test/pleroma/web/pleroma_api/controllers/conversation_controller_test.exs
index 98a23aaaa..54f2c5a58 100644
--- a/test/pleroma/web/pleroma_api/controllers/conversation_controller_test.exs
+++ b/test/pleroma/web/pleroma_api/controllers/conversation_controller_test.exs
@@ -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
diff --git a/test/pleroma/web/plugs/user_tracking_plug_test.exs b/test/pleroma/web/plugs/user_tracking_plug_test.exs
new file mode 100644
index 000000000..8e9d59b99
--- /dev/null
+++ b/test/pleroma/web/plugs/user_tracking_plug_test.exs
@@ -0,0 +1,58 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.Plugs.UserTrackingPlugTest do
+ use Pleroma.Web.ConnCase, async: true
+
+ import Pleroma.Factory
+
+ alias Pleroma.Web.Plugs.UserTrackingPlug
+
+ test "updates last_active_at for a new user", %{conn: conn} do
+ user = insert(:user)
+
+ assert is_nil(user.last_active_at)
+
+ test_started_at = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
+
+ %{assigns: %{user: user}} =
+ conn
+ |> assign(:user, user)
+ |> UserTrackingPlug.call(%{})
+
+ assert user.last_active_at >= test_started_at
+ assert user.last_active_at <= NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
+ end
+
+ test "doesn't update last_active_at if it was updated recently", %{conn: conn} do
+ last_active_at =
+ NaiveDateTime.utc_now()
+ |> NaiveDateTime.add(-:timer.hours(1), :millisecond)
+ |> NaiveDateTime.truncate(:second)
+
+ user = insert(:user, %{last_active_at: last_active_at})
+
+ %{assigns: %{user: user}} =
+ conn
+ |> assign(:user, user)
+ |> UserTrackingPlug.call(%{})
+
+ assert user.last_active_at == last_active_at
+ end
+
+ test "skips updating last_active_at if user ID is nil", %{conn: conn} do
+ %{assigns: %{user: user}} =
+ conn
+ |> assign(:user, %Pleroma.User{})
+ |> UserTrackingPlug.call(%{})
+
+ assert is_nil(user.last_active_at)
+ end
+
+ test "does nothing if user is not present", %{conn: conn} do
+ %{assigns: assigns} = UserTrackingPlug.call(conn, %{})
+
+ refute Map.has_key?(assigns, :user)
+ end
+end