2019-04-07 12:50:36 +00:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
2021-08-19 12:55:45 +00:00
|
|
|
import { Users } from '../index';
|
2021-08-19 13:04:15 +00:00
|
|
|
import { Blocking } from '@/models/entities/blocking';
|
|
|
|
import { awaitAll } from '@/prelude/await-all';
|
2021-08-19 12:55:45 +00:00
|
|
|
import { SchemaType } from '@/misc/schema';
|
2021-08-19 13:04:15 +00:00
|
|
|
import { User } from '@/models/entities/user';
|
2019-04-23 13:35:26 +00:00
|
|
|
|
|
|
|
export type PackedBlocking = SchemaType<typeof packedBlockingSchema>;
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
@EntityRepository(Blocking)
|
|
|
|
export class BlockingRepository extends Repository<Blocking> {
|
|
|
|
public async pack(
|
|
|
|
src: Blocking['id'] | Blocking,
|
2021-03-24 02:05:37 +00:00
|
|
|
me?: { id: User['id'] } | null | undefined
|
2019-04-23 13:35:26 +00:00
|
|
|
): Promise<PackedBlocking> {
|
2021-02-13 06:33:38 +00:00
|
|
|
const blocking = typeof src === 'object' ? src : await this.findOneOrFail(src);
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2019-04-23 13:35:26 +00:00
|
|
|
return await awaitAll({
|
2019-04-07 12:50:36 +00:00
|
|
|
id: blocking.id,
|
2019-04-23 13:35:26 +00:00
|
|
|
createdAt: blocking.createdAt.toISOString(),
|
|
|
|
blockeeId: blocking.blockeeId,
|
2019-04-07 12:50:36 +00:00
|
|
|
blockee: Users.pack(blocking.blockeeId, me, {
|
|
|
|
detail: true
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
2019-04-25 04:27:07 +00:00
|
|
|
|
|
|
|
public packMany(
|
|
|
|
blockings: any[],
|
2021-03-24 02:05:37 +00:00
|
|
|
me: { id: User['id'] }
|
2019-04-25 04:27:07 +00:00
|
|
|
) {
|
|
|
|
return Promise.all(blockings.map(x => this.pack(x, me)));
|
|
|
|
}
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
2019-04-23 13:35:26 +00:00
|
|
|
|
|
|
|
export const packedBlockingSchema = {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
format: 'id',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
format: 'date-time',
|
|
|
|
},
|
|
|
|
blockeeId: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
blockee: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2021-09-11 16:12:23 +00:00
|
|
|
ref: 'User' as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|