Merge branch 'token-activity' into shigusegubu-new

This commit is contained in:
Henry Jameson 2026-11-09 17:13:55 +02:00
commit 612be88ee1
6 changed files with 33 additions and 1 deletions

View file

@ -21,6 +21,7 @@ defmodule Pleroma.Web.OAuth.Token do
field(:refresh_token, :string)
field(:scopes, {:array, :string}, default: [])
field(:valid_until, :naive_datetime_usec)
field(:last_active, :naive_datetime_usec)
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
belongs_to(:app, App)
@ -96,6 +97,13 @@ defmodule Pleroma.Web.OAuth.Token do
|> validate_required([:valid_until])
end
@spec update_activity(Token.t()) :: {:ok, Token.t()} | {:error, Ecto.Changeset.t()}
def update_activity(token) do
token
|> Ecto.Changeset.change(last_active: NaiveDateTime.utc_now())
|> Repo.update()
end
@spec create(App.t(), User.t(), map()) :: {:ok, Token.t()} | {:error, Ecto.Changeset.t()}
def create(%App{} = app, %User{} = user, attrs \\ %{}) do
with {:ok, token} <- do_create(app, user, attrs) do

View file

@ -17,6 +17,7 @@ defmodule Pleroma.Web.OAuth.TokenView do
%{
id: token_entry.id,
valid_until: token_entry.valid_until,
last_active: token_entry.last_active,
app_name: token_entry.app.client_name,
scopes: token_entry.scopes,
is_current_token: token_entry.id == current_token.id

View file

@ -24,6 +24,8 @@ defmodule Pleroma.Web.Plugs.OAuthPlug do
with {:ok, token_str} <- fetch_token_str(conn) do
with {:ok, user, user_token} <- fetch_user_and_token(token_str),
false <- Token.expired?(user_token) do
Token.update_activity(user_token)
conn
|> assign(:token, user_token)
|> assign(:user, user)