chore: Flag users with platform used

This commit is contained in:
Tom Moor
2022-09-18 17:53:55 -04:00
parent ae697339ac
commit f8912732b8
6 changed files with 32 additions and 13 deletions

View File

@@ -10,8 +10,8 @@ import {
BeforeCreate,
} from "sequelize-typescript";
import { TeamValidation } from "@shared/validations";
import env from "@server/env";
import { ValidationError } from "@server/errors";
import isCloudHosted from "~/utils/isCloudHosted";
import Team from "./Team";
import User from "./User";
import IdModel from "./base/IdModel";
@@ -19,6 +19,8 @@ import Fix from "./decorators/Fix";
import IsFQDN from "./validators/IsFQDN";
import Length from "./validators/Length";
const isCloudHosted = env.DEPLOYMENT === "hosted";
@Table({ tableName: "team_domains", modelName: "team_domain" })
@Fix
class TeamDomain extends IdModel {

View File

@@ -1,6 +1,7 @@
import crypto from "crypto";
import { addMinutes, subMinutes } from "date-fns";
import JWT from "jsonwebtoken";
import { Context } from "koa";
import { Transaction, QueryTypes, SaveOptions, Op } from "sequelize";
import {
Table,
@@ -51,6 +52,8 @@ import NotContainsUrl from "./validators/NotContainsUrl";
export enum UserFlag {
InviteSent = "inviteSent",
InviteReminderSent = "inviteReminderSent",
DesktopWeb = "desktopWeb",
MobileWeb = "mobileWeb",
}
export enum UserRole {
@@ -264,8 +267,11 @@ class User extends ParanoidModel {
if (!this.flags) {
this.flags = {};
}
this.flags[flag] = value ? 1 : 0;
this.changed("flags", true);
const binary = value ? 1 : 0;
if (this.flags[flag] !== binary) {
this.flags[flag] = binary;
this.changed("flags", true);
}
return this.flags;
};
@@ -350,7 +356,8 @@ class User extends ParanoidModel {
.map((c) => c.id);
};
updateActiveAt = async (ip: string, force = false) => {
updateActiveAt = async (ctx: Context, force = false) => {
const { ip } = ctx.request;
const fiveMinutesAgo = subMinutes(new Date(), 5);
// ensure this is updated only every few minutes otherwise
@@ -358,13 +365,20 @@ class User extends ParanoidModel {
if (!this.lastActiveAt || this.lastActiveAt < fiveMinutesAgo || force) {
this.lastActiveAt = new Date();
this.lastActiveIp = ip;
return this.save({
hooks: false,
});
}
return this;
// Track the clients each user is using
if (ctx.userAgent.isMobile) {
this.setFlag(UserFlag.MobileWeb);
}
if (ctx.userAgent.isDesktop) {
this.setFlag(UserFlag.DesktopWeb);
}
// Save only writes to the database if there are changes
return this.save({
hooks: false,
});
};
updateSignedIn = (ip: string) => {