server: Fix typing for user token

Also fix a comment in the User model that wrongly states that the token
is null if the user is local, when it's the opposite.
This commit is contained in:
Norm 2022-12-07 19:32:05 -05:00
parent cbfd866122
commit 3cf673960b
3 changed files with 7 additions and 5 deletions

View file

@ -214,7 +214,7 @@ export class User {
@Index({ unique: true }) @Index({ unique: true })
@Column('char', { @Column('char', {
length: 16, nullable: true, unique: true, length: 16, nullable: true, unique: true,
comment: 'The native access token of the User. It will be null if the origin of the user is local.', comment: 'The native access token of local users, or null.',
}) })
public token: string | null; public token: string | null;
@ -234,10 +234,12 @@ export class User {
export interface ILocalUser extends User { export interface ILocalUser extends User {
host: null; host: null;
token: string;
} }
export interface IRemoteUser extends User { export interface IRemoteUser extends User {
host: string; host: string;
token: null;
} }
export type CacheableLocalUser = ILocalUser; export type CacheableLocalUser = ILocalUser;

View file

@ -35,7 +35,7 @@ const locationSchema = { type: 'string', minLength: 1, maxLength: 50 } as const;
const birthdaySchema = { type: 'string', pattern: /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/.toString().slice(1, -1) } as const; const birthdaySchema = { type: 'string', pattern: /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/.toString().slice(1, -1) } as const;
function isLocalUser(user: User): user is ILocalUser; function isLocalUser(user: User): user is ILocalUser;
function isLocalUser<T extends { host: User['host'] }>(user: T): user is T & { host: null; }; function isLocalUser<T extends { host: User['host'] }>(user: T): user is T & { host: null; token: string; };
function isLocalUser(user: User | { host: User['host'] }): boolean { function isLocalUser(user: User | { host: User['host'] }): boolean {
return user.host == null; return user.host == null;
} }

View file

@ -1,10 +1,10 @@
import { IsNull } from 'typeorm'; import { IsNull } from 'typeorm';
import { CacheableLocalUser, CacheableUser, ILocalUser } from '@/models/entities/user.js'; import { CacheableLocalUser, ILocalUser, User } from '@/models/entities/user.js';
import { Users } from '@/models/index.js'; import { Users } from '@/models/index.js';
import { Cache } from '@/misc/cache.js'; import { Cache } from '@/misc/cache.js';
import { subscriber } from '@/db/redis.js'; import { subscriber } from '@/db/redis.js';
export const userByIdCache = new Cache<CacheableUser>( export const userByIdCache = new Cache<User>(
Infinity, Infinity,
async (id) => await Users.findOneBy({ id }) ?? undefined, async (id) => await Users.findOneBy({ id }) ?? undefined,
); );
@ -12,7 +12,7 @@ export const localUserByNativeTokenCache = new Cache<CacheableLocalUser>(
Infinity, Infinity,
async (token) => await Users.findOneBy({ token, host: IsNull() }) as ILocalUser | null ?? undefined, async (token) => await Users.findOneBy({ token, host: IsNull() }) as ILocalUser | null ?? undefined,
); );
export const uriPersonCache = new Cache<CacheableUser>( export const uriPersonCache = new Cache<User>(
Infinity, Infinity,
async (uri) => await Users.findOneBy({ uri }) ?? undefined, async (uri) => await Users.findOneBy({ uri }) ?? undefined,
); );