refactor: fix type
This commit is contained in:
parent
44a01c4b5a
commit
d39465085c
5 changed files with 51 additions and 18 deletions
|
@ -1,6 +1,5 @@
|
||||||
import { db } from '@/db/postgre.js';
|
import { db } from '@/db/postgre.js';
|
||||||
import { DriveFile } from '@/models/entities/drive-file.js';
|
import { DriveFile } from '@/models/entities/drive-file.js';
|
||||||
import { Users, DriveFolders } from '../index.js';
|
|
||||||
import { User } from '@/models/entities/user.js';
|
import { User } from '@/models/entities/user.js';
|
||||||
import { toPuny } from '@/misc/convert-host.js';
|
import { toPuny } from '@/misc/convert-host.js';
|
||||||
import { awaitAll, Promiseable } from '@/prelude/await-all.js';
|
import { awaitAll, Promiseable } from '@/prelude/await-all.js';
|
||||||
|
@ -9,6 +8,7 @@ import config from '@/config/index.js';
|
||||||
import { query, appendQuery } from '@/prelude/url.js';
|
import { query, appendQuery } from '@/prelude/url.js';
|
||||||
import { Meta } from '@/models/entities/meta.js';
|
import { Meta } from '@/models/entities/meta.js';
|
||||||
import { fetchMeta } from '@/misc/fetch-meta.js';
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
||||||
|
import { Users, DriveFolders } from '../index.js';
|
||||||
|
|
||||||
type PackOptions = {
|
type PackOptions = {
|
||||||
detail?: boolean,
|
detail?: boolean,
|
||||||
|
@ -111,7 +111,40 @@ export const DriveFileRepository = db.getRepository(DriveFile).extend({
|
||||||
|
|
||||||
async pack(
|
async pack(
|
||||||
src: DriveFile['id'] | DriveFile,
|
src: DriveFile['id'] | DriveFile,
|
||||||
options?: PackOptions
|
options?: PackOptions,
|
||||||
|
): Promise<Packed<'DriveFile'>> {
|
||||||
|
const opts = Object.assign({
|
||||||
|
detail: false,
|
||||||
|
self: false,
|
||||||
|
}, options);
|
||||||
|
|
||||||
|
const file = typeof src === 'object' ? src : await this.findOneByOrFail({ id: src });
|
||||||
|
|
||||||
|
return await awaitAll<Packed<'DriveFile'>>({
|
||||||
|
id: file.id,
|
||||||
|
createdAt: file.createdAt.toISOString(),
|
||||||
|
name: file.name,
|
||||||
|
type: file.type,
|
||||||
|
md5: file.md5,
|
||||||
|
size: file.size,
|
||||||
|
isSensitive: file.isSensitive,
|
||||||
|
blurhash: file.blurhash,
|
||||||
|
properties: opts.self ? file.properties : this.getPublicProperties(file),
|
||||||
|
url: opts.self ? file.url : this.getPublicUrl(file, false),
|
||||||
|
thumbnailUrl: this.getPublicUrl(file, true),
|
||||||
|
comment: file.comment,
|
||||||
|
folderId: file.folderId,
|
||||||
|
folder: opts.detail && file.folderId ? DriveFolders.pack(file.folderId, {
|
||||||
|
detail: true,
|
||||||
|
}) : null,
|
||||||
|
userId: opts.withUser ? file.userId : null,
|
||||||
|
user: (opts.withUser && file.userId) ? Users.pack(file.userId) : null,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
async packNullable(
|
||||||
|
src: DriveFile['id'] | DriveFile,
|
||||||
|
options?: PackOptions,
|
||||||
): Promise<Packed<'DriveFile'> | null> {
|
): Promise<Packed<'DriveFile'> | null> {
|
||||||
const opts = Object.assign({
|
const opts = Object.assign({
|
||||||
detail: false,
|
detail: false,
|
||||||
|
@ -145,9 +178,9 @@ export const DriveFileRepository = db.getRepository(DriveFile).extend({
|
||||||
|
|
||||||
async packMany(
|
async packMany(
|
||||||
files: (DriveFile['id'] | DriveFile)[],
|
files: (DriveFile['id'] | DriveFile)[],
|
||||||
options?: PackOptions
|
options?: PackOptions,
|
||||||
) {
|
): Promise<Packed<'DriveFile'>[]> {
|
||||||
const items = await Promise.all(files.map(f => this.pack(f, options)));
|
const items = await Promise.all(files.map(f => this.packNullable(f, options)));
|
||||||
return items.filter(x => x != null);
|
return items.filter((x): x is Packed<'DriveFile'> => x != null);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import { db } from '@/db/postgre.js';
|
import { db } from '@/db/postgre.js';
|
||||||
import { Page } from '@/models/entities/page.js';
|
import { Page } from '@/models/entities/page.js';
|
||||||
import { Packed } from '@/misc/schema.js';
|
import { Packed } from '@/misc/schema.js';
|
||||||
import { Users, DriveFiles, PageLikes } from '../index.js';
|
|
||||||
import { awaitAll } from '@/prelude/await-all.js';
|
import { awaitAll } from '@/prelude/await-all.js';
|
||||||
import { DriveFile } from '@/models/entities/drive-file.js';
|
import { DriveFile } from '@/models/entities/drive-file.js';
|
||||||
import { User } from '@/models/entities/user.js';
|
import { User } from '@/models/entities/user.js';
|
||||||
|
import { Users, DriveFiles, PageLikes } from '../index.js';
|
||||||
|
|
||||||
export const PageRepository = db.getRepository(Page).extend({
|
export const PageRepository = db.getRepository(Page).extend({
|
||||||
async pack(
|
async pack(
|
||||||
|
@ -14,7 +14,7 @@ export const PageRepository = db.getRepository(Page).extend({
|
||||||
const meId = me ? me.id : null;
|
const meId = me ? me.id : null;
|
||||||
const page = typeof src === 'object' ? src : await this.findOneByOrFail({ id: src });
|
const page = typeof src === 'object' ? src : await this.findOneByOrFail({ id: src });
|
||||||
|
|
||||||
const attachedFiles: Promise<DriveFile | undefined>[] = [];
|
const attachedFiles: Promise<DriveFile | null>[] = [];
|
||||||
const collectFile = (xs: any[]) => {
|
const collectFile = (xs: any[]) => {
|
||||||
for (const x of xs) {
|
for (const x of xs) {
|
||||||
if (x.type === 'image') {
|
if (x.type === 'image') {
|
||||||
|
@ -73,7 +73,7 @@ export const PageRepository = db.getRepository(Page).extend({
|
||||||
script: page.script,
|
script: page.script,
|
||||||
eyeCatchingImageId: page.eyeCatchingImageId,
|
eyeCatchingImageId: page.eyeCatchingImageId,
|
||||||
eyeCatchingImage: page.eyeCatchingImageId ? await DriveFiles.pack(page.eyeCatchingImageId) : null,
|
eyeCatchingImage: page.eyeCatchingImageId ? await DriveFiles.pack(page.eyeCatchingImageId) : null,
|
||||||
attachedFiles: DriveFiles.packMany(await Promise.all(attachedFiles)),
|
attachedFiles: DriveFiles.packMany((await Promise.all(attachedFiles)).filter((x): x is DriveFile => x != null)),
|
||||||
likedCount: page.likedCount,
|
likedCount: page.likedCount,
|
||||||
isLiked: meId ? await PageLikes.findOneBy({ pageId: page.id, userId: meId }).then(x => x != null) : undefined,
|
isLiked: meId ? await PageLikes.findOneBy({ pageId: page.id, userId: meId }).then(x => x != null) : undefined,
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import { toArray, unique } from '@/prelude/array.js';
|
|
||||||
import { IObject, isMention, IApMention } from '../type.js';
|
|
||||||
import { resolvePerson } from './person.js';
|
|
||||||
import promiseLimit from 'promise-limit';
|
import promiseLimit from 'promise-limit';
|
||||||
import Resolver from '../resolver.js';
|
import { toArray, unique } from '@/prelude/array.js';
|
||||||
import { CacheableUser, User } from '@/models/entities/user.js';
|
import { CacheableUser, User } from '@/models/entities/user.js';
|
||||||
|
import { IObject, isMention, IApMention } from '../type.js';
|
||||||
|
import Resolver from '../resolver.js';
|
||||||
|
import { resolvePerson } from './person.js';
|
||||||
|
|
||||||
export async function extractApMentions(tags: IObject | IObject[] | null | undefined) {
|
export async function extractApMentions(tags: IObject | IObject[] | null | undefined) {
|
||||||
const hrefs = unique(extractApMentionObjects(tags).map(x => x.href as string));
|
const hrefs = unique(extractApMentionObjects(tags).map(x => x.href as string));
|
||||||
|
@ -12,7 +12,7 @@ export async function extractApMentions(tags: IObject | IObject[] | null | undef
|
||||||
|
|
||||||
const limit = promiseLimit<CacheableUser | null>(2);
|
const limit = promiseLimit<CacheableUser | null>(2);
|
||||||
const mentionedUsers = (await Promise.all(
|
const mentionedUsers = (await Promise.all(
|
||||||
hrefs.map(x => limit(() => resolvePerson(x, resolver).catch(() => null)))
|
hrefs.map(x => limit(() => resolvePerson(x, resolver).catch(() => null))),
|
||||||
)).filter((x): x is CacheableUser => x != null);
|
)).filter((x): x is CacheableUser => x != null);
|
||||||
|
|
||||||
return mentionedUsers;
|
return mentionedUsers;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import define from '../../../define.js';
|
|
||||||
import { DriveFiles } from '@/models/index.js';
|
import { DriveFiles } from '@/models/index.js';
|
||||||
|
import define from '../../../define.js';
|
||||||
|
|
||||||
export const meta = {
|
export const meta = {
|
||||||
tags: ['drive'],
|
tags: ['drive'],
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import define from '../../../define.js';
|
|
||||||
import { ApiError } from '../../../error.js';
|
|
||||||
import { DriveFile } from '@/models/entities/drive-file.js';
|
import { DriveFile } from '@/models/entities/drive-file.js';
|
||||||
import { DriveFiles, Users } from '@/models/index.js';
|
import { DriveFiles, Users } from '@/models/index.js';
|
||||||
|
import define from '../../../define.js';
|
||||||
|
import { ApiError } from '../../../error.js';
|
||||||
|
|
||||||
export const meta = {
|
export const meta = {
|
||||||
tags: ['drive'],
|
tags: ['drive'],
|
||||||
|
@ -51,7 +51,7 @@ export const paramDef = {
|
||||||
|
|
||||||
// eslint-disable-next-line import/no-default-export
|
// eslint-disable-next-line import/no-default-export
|
||||||
export default define(meta, paramDef, async (ps, user) => {
|
export default define(meta, paramDef, async (ps, user) => {
|
||||||
let file: DriveFile | undefined;
|
let file: DriveFile | null = null;
|
||||||
|
|
||||||
if (ps.fileId) {
|
if (ps.fileId) {
|
||||||
file = await DriveFiles.findOneBy({ id: ps.fileId });
|
file = await DriveFiles.findOneBy({ id: ps.fileId });
|
||||||
|
|
Loading…
Reference in a new issue