chore: Track lastActiveAt for teams (#6491)
This commit is contained in:
@@ -122,10 +122,13 @@ export default function auth(options: AuthenticationOptions = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
// not awaiting the promise here so that the request is not blocked
|
||||
// not awaiting the promises here so that the request is not blocked
|
||||
user.updateActiveAt(ctx).catch((err) => {
|
||||
Logger.error("Failed to update user activeAt", err);
|
||||
});
|
||||
user.team?.updateActiveAt().catch((err) => {
|
||||
Logger.error("Failed to update team activeAt", err);
|
||||
});
|
||||
|
||||
ctx.state.auth = {
|
||||
user,
|
||||
|
||||
11
server/migrations/20240204185157-team-last-active-at.js
Normal file
11
server/migrations/20240204185157-team-last-active-at.js
Normal file
@@ -0,0 +1,11 @@
|
||||
module.exports = {
|
||||
up: async (queryInterface, Sequelize) => {
|
||||
await queryInterface.addColumn("teams", "lastActiveAt", {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: true,
|
||||
});
|
||||
},
|
||||
down: async (queryInterface, Sequelize) => {
|
||||
await queryInterface.removeColumn("teams", "lastActiveAt");
|
||||
},
|
||||
};
|
||||
@@ -3,6 +3,7 @@ import fs from "fs";
|
||||
import path from "path";
|
||||
import { URL } from "url";
|
||||
import util from "util";
|
||||
import { subMinutes } from "date-fns";
|
||||
import {
|
||||
InferAttributes,
|
||||
InferCreationAttributes,
|
||||
@@ -163,6 +164,10 @@ class Team extends ParanoidModel<
|
||||
@Column
|
||||
suspendedAt: Date | null;
|
||||
|
||||
@IsDate
|
||||
@Column
|
||||
lastActiveAt: Date | null;
|
||||
|
||||
// getters
|
||||
|
||||
/**
|
||||
@@ -248,6 +253,27 @@ class Team extends ParanoidModel<
|
||||
TeamPreferenceDefaults[preference] ??
|
||||
false;
|
||||
|
||||
/**
|
||||
* Updates the lastActiveAt timestamp to the current time.
|
||||
*
|
||||
* @param force Whether to force the update even if the last update was recent
|
||||
* @returns A promise that resolves with the updated team
|
||||
*/
|
||||
public updateActiveAt = async (force = false) => {
|
||||
const fiveMinutesAgo = subMinutes(new Date(), 5);
|
||||
|
||||
// ensure this is updated only every few minutes otherwise
|
||||
// we'll be constantly writing to the DB as API requests happen
|
||||
if (!this.lastActiveAt || this.lastActiveAt < fiveMinutesAgo || force) {
|
||||
this.lastActiveAt = new Date();
|
||||
}
|
||||
|
||||
// Save only writes to the database if there are changes
|
||||
return this.save({
|
||||
hooks: false,
|
||||
});
|
||||
};
|
||||
|
||||
provisionFirstCollection = async (userId: string) => {
|
||||
await this.sequelize!.transaction(async (transaction) => {
|
||||
const collection = await Collection.create(
|
||||
@@ -356,6 +382,10 @@ class Team extends ParanoidModel<
|
||||
// Set here rather than in TeamPreferenceDefaults as we only want to enable by default for new
|
||||
// workspaces.
|
||||
model.setPreference(TeamPreference.MembersCanInvite, true);
|
||||
|
||||
// Set last active at on creation.
|
||||
model.lastActiveAt = new Date();
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user