server: Fix typing for user token
ci/woodpecker/push/lint-foundkey-js Pipeline was successful Details
ci/woodpecker/push/lint-backend Pipeline was successful Details
ci/woodpecker/push/lint-client Pipeline was successful Details
ci/woodpecker/push/build Pipeline was successful Details
ci/woodpecker/push/test Pipeline was successful Details
ci/woodpecker/pr/lint-foundkey-js Pipeline was successful Details
ci/woodpecker/pr/lint-backend Pipeline failed Details
ci/woodpecker/pr/build Pipeline was successful Details
ci/woodpecker/pr/lint-client Pipeline failed Details
ci/woodpecker/pr/test Pipeline failed Details

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 })
@Column('char', {
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;
@ -234,10 +234,12 @@ export class User {
export interface ILocalUser extends User {
host: null;
token: string;
}
export interface IRemoteUser extends User {
host: string;
token: null;
}
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;
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 {
return user.host == null;
}

View File

@ -1,10 +1,10 @@
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 { Cache } from '@/misc/cache.js';
import { subscriber } from '@/db/redis.js';
export const userByIdCache = new Cache<CacheableUser>(
export const userByIdCache = new Cache<User>(
Infinity,
async (id) => await Users.findOneBy({ id }) ?? undefined,
);
@ -12,7 +12,7 @@ export const localUserByNativeTokenCache = new Cache<CacheableLocalUser>(
Infinity,
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,
async (uri) => await Users.findOneBy({ uri }) ?? undefined,
);