<commit_before>import { model, Schema } from "mongoose";
import { Maybe, md5 } from "../../util";
import { v4 as uuid } from "uuid";
import { getConfig } from "../../typings/Configs";
import { IAccountDocument } from "../../typings";
import { AccountType, IAccountModel } from "../../typings/db/IAccountDocument";
import { debug, error, warn } from "../../util/colors";
import { Bread } from "../../typings/Bread";
import { Caching } from "../../generator/Caching";
import { MineSkinMetrics } from "../../util/metrics";
import * as Sentry from "@sentry/node";
import { MIN_ACCOUNT_DELAY } from "../../generator/Generator";

const Int32 = require("mongoose-int32");
export const AccountSchema: Schema<IAccountDocument, IAccountModel> = new Schema({
    id: {
        type: Number,
        index: true
    },
    username: {
        type: String,
        index: true
    },
    email: {
        type: String,
        index: true
    },
    uuid: {
        type: String,
        index: true
    },
    playername: String,
    authInterceptorEnabled: Boolean,
    password: String,
    passwordOld: String,
    passwordNew: String,
    security: String,
    multiSecurity: [{
        id: Number,
        answer: String
    }],
    accountType: String,
    microsoftAccount: Boolean,
    microsoftUserId: {
        type: String,
        index: true
    },
    microsoftAccessToken: String,
    microsoftRefreshToken: String,
    minecraftXboxUsername: {
        type: String,
        index: true
    },
    lastSelected: Number,
    timeAdded: {
        type: Number,
        index: true
    },
    lastUsed: {
        type: Number,
        index: true
    },
    enabled: {
        type: Boolean,
        index: true
    },
    errorCounter: Int32,
    successCounter: Int32,
    totalErrorCounter: Number,
    totalSuccessCounter: Number,
    lastGenerateSuccess: Number,
    lastErrorCode: String,
    forcedTimeoutAt: Number,
    lastTextureUrl: String,
    sameTextureCounter: Number,
    accessToken: String,
    accessTokenExpiration: Number,
    accessTokenSource: String,
    clientToken: String,
    requestIp: String,
    requestServer: {
        type: String,
        index: true
    },
    lastRequestServer: {
        type: String
    },
    type: {
        type: String,
        enum: ["internal", "external"],
        default: "internal"
    },
    discordUser: String,
    discordMessageSent: Boolean,
    sendEmails: Boolean,
    emailSent: Boolean,
    ev: Number
}, { id: false });


/// METHODS

AccountSchema.methods.getOrCreateClientToken = function (this: IAccountDocument): string {
    if (!this.clientToken) {
        this.clientToken = md5(uuid());
    }
    return this.clientToken;
};

AccountSchema.methods.updateRequestServer = function (this: IAccountDocument, newRequestServer: string | null) {
    if (this.requestServer && this.requestServer !== newRequestServer) {
        this.lastRequestServer = this.requestServer;
    }
    this.requestServer = newRequestServer;
};

AccountSchema.methods.getEmail = function (this: IAccountDocument): string {
    if (this.email) {
        return this.email;
    } else {
        this.email = this.username;
    }
    return this.email;
};

AccountSchema.methods.getAccountType = function (this: IAccountDocument): AccountType {
    if (this.accountType) {
        return this.accountType;
    }<commit_msg>log move<commit_after>import { model, Schema } from "mongoose";
import { Maybe, md5 } from "../../util";
import { v4 as uuid } from "uuid";
import { getConfig } from "../../typings/Configs";
import { IAccountDocument } from "../../typings";
import { AccountType, IAccountModel } from "../../typings/db/IAccountDocument";
import { debug, error, warn } from "../../util/colors";
import { Bread } from "../../typings/Bread";
import { Caching } from "../../generator/Caching";
import { MineSkinMetrics } from "../../util/metrics";
import * as Sentry from "@sentry/node";
import { MIN_ACCOUNT_DELAY } from "../../generator/Generator";
import { Discord } from "../../util/Discord";

const Int32 = require("mongoose-int32");
export const AccountSchema: Schema<IAccountDocument, IAccountModel> = new Schema({
    id: {
        type: Number,
        index: true
    },
    username: {
        type: String,
        index: true
    },
    email: {
        type: String,
        index: true
    },
    uuid: {
        type: String,
        index: true
    },
    playername: String,
    authInterceptorEnabled: Boolean,
    password: String,
    passwordOld: String,
    passwordNew: String,
    security: String,
    multiSecurity: [{
        id: Number,
        answer: String
    }],
    accountType: String,
    microsoftAccount: Boolean,
    microsoftUserId: {
        type: String,
        index: true
    },
    microsoftAccessToken: String,
    microsoftRefreshToken: String,
    minecraftXboxUsername: {
        type: String,
        index: true
    },
    lastSelected: Number,
    timeAdded: {
        type: Number,
        index: true
    },
    lastUsed: {
        type: Number,
        index: true
    },
    enabled: {
        type: Boolean,
        index: true
    },
    errorCounter: Int32,
    successCounter: Int32,
    totalErrorCounter: Number,
    totalSuccessCounter: Number,
    lastGenerateSuccess: Number,
    lastErrorCode: String,
    forcedTimeoutAt: Number,
    lastTextureUrl: String,
    sameTextureCounter: Number,
    accessToken: String,
    accessTokenExpiration: Number,
    accessTokenSource: String,
    clientToken: String,
    requestIp: String,
    requestServer: {
        type: String,
        index: true
    },
    lastRequestServer: {
        type: String
    },
    type: {
        type: String,
        enum: ["internal", "external"],
        default: "internal"
    },
    discordUser: String,
    discordMessageSent: Boolean,
    sendEmails: Boolean,
    emailSent: Boolean,
    ev: Number
}, { id: false });


/// METHODS

AccountSchema.methods.getOrCreateClientToken = function (this: IAccountDocument): string {
    if (!this.clientToken) {
        this.clientToken = md5(uuid());
    }
    return this.clientToken;
};

AccountSchema.methods.updateRequestServer = function (this: IAccountDocument, newRequestServer: string | null) {
    if (this.requestServer && this.requestServer !== newRequestServer) {
        this.lastRequestServer = this.requestServer;
        Discord.postDiscordMessage("👤 Account " + this.id + "/" + this.uuid + " moved to " + this.requestServer + " (was " + this.lastRequestServer + ")");
    }
    this.requestServer = newRequestServer;
};

AccountSchema.methods.getEmail = function (this: IAccountDocument): string {
    if (this.email) {
        return this.email;
    } else {
        this.email = this.username;
    }
    return this.email;
};

AccountSchema.methods.getAccountType = function (this: IAccountDocument): AccountType {
    if (this.accountType) {
        return this.accountType;
    }
{'fraction_non_alphanumeric': 0.12429866206301252, 'fraction_numerical': 0.003165012228456337, 'mean_word_length': 2.585353274883961, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '102890', 'n_tokens_mistral': 1992, 'n_tokens_neox': 1996, 'n_words': 502}