2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { AccessTokensRepository } from '@/models/index.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2020-03-28 07:52:52 +00:00
|
|
|
|
|
|
|
export const meta = {
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: true,
|
2020-03-28 07:52:52 +00:00
|
|
|
|
|
|
|
secure: true,
|
2022-02-19 05:05:32 +00:00
|
|
|
} as const;
|
2020-03-28 07:52:52 +00:00
|
|
|
|
2022-02-20 04:15:40 +00:00
|
|
|
export const paramDef = {
|
2022-02-19 05:05:32 +00:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
sort: { type: 'string', enum: ['+createdAt', '-createdAt', '+lastUsedAt', '-lastUsedAt'] },
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2022-02-19 05:05:32 +00:00
|
|
|
required: [],
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2020-03-28 07:52:52 +00:00
|
|
|
|
2022-01-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 18:27:08 +00:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.accessTokensRepository)
|
|
|
|
private accessTokensRepository: AccessTokensRepository,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const query = this.accessTokensRepository.createQueryBuilder('token')
|
2023-05-30 08:27:47 +00:00
|
|
|
.where('token.userId = :userId', { userId: me.id })
|
|
|
|
.leftJoinAndSelect('token.app', 'app');
|
2020-03-28 07:52:52 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
switch (ps.sort) {
|
|
|
|
case '+createdAt': query.orderBy('token.createdAt', 'DESC'); break;
|
|
|
|
case '-createdAt': query.orderBy('token.createdAt', 'ASC'); break;
|
|
|
|
case '+lastUsedAt': query.orderBy('token.lastUsedAt', 'DESC'); break;
|
|
|
|
case '-lastUsedAt': query.orderBy('token.lastUsedAt', 'ASC'); break;
|
|
|
|
default: query.orderBy('token.id', 'ASC'); break;
|
|
|
|
}
|
2020-03-28 07:52:52 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const tokens = await query.getMany();
|
2020-03-28 07:52:52 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
return await Promise.all(tokens.map(token => ({
|
|
|
|
id: token.id,
|
2023-05-30 08:27:47 +00:00
|
|
|
name: token.name ?? token.app?.name,
|
2022-09-17 18:27:08 +00:00
|
|
|
createdAt: token.createdAt,
|
|
|
|
lastUsedAt: token.lastUsedAt,
|
|
|
|
permission: token.permission,
|
|
|
|
})));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|