1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Plugs.UserTrackingPlug do
8 import Plug.Conn, only: [assign: 3]
10 @update_interval :timer.hours(24)
12 def init(opts), do: opts
14 def call(%{assigns: %{user: %User{id: id} = user}} = conn, _) when not is_nil(id) do
15 with true <- needs_update?(user),
16 {:ok, user} <- User.update_last_active_at(user) do
17 assign(conn, :user, user)
23 def call(conn, _), do: conn
25 defp needs_update?(%User{last_active_at: nil}), do: true
27 defp needs_update?(%User{last_active_at: last_active_at}) do
28 NaiveDateTime.diff(NaiveDateTime.utc_now(), last_active_at, :millisecond) >= @update_interval