2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { DriveFileEntityService } from '@/core/entities/DriveFileEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-22 23:21:31 +02:00
|
|
|
import type { UsersRepository, NotesRepository, FollowingsRepository, DriveFilesRepository, NoteReactionsRepository, PageLikesRepository, NoteFavoritesRepository, PollVotesRepository } from '@/models/index.js';
|
2022-06-14 11:01:23 +02:00
|
|
|
import { ApiError } from '../../error.js';
|
2020-11-25 13:31:34 +01:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['users'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: false,
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2022-06-10 07:25:20 +02:00
|
|
|
description: 'Show statistics about a user.',
|
|
|
|
|
2020-11-25 13:31:34 +01:00
|
|
|
errors: {
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: '9e638e45-3b25-4ef7-8f95-07e8498f1819',
|
2020-11-25 13:31:34 +01:00
|
|
|
},
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2022-06-10 07:25:20 +02:00
|
|
|
|
|
|
|
res: {
|
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
properties: {
|
|
|
|
notesCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
repliesCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
renotesCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
repliedCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
renotedCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
pollVotesCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
pollVotedCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
localFollowingCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
remoteFollowingCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
localFollowersCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
remoteFollowersCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
followingCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
followersCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
sentReactionsCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
receivedReactionsCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
noteFavoritesCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
pageLikesCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
pageLikedCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
driveFilesCount: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
},
|
|
|
|
driveUsage: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
description: 'Drive usage in bytes',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
userId: { type: 'string', format: 'misskey:id' },
|
|
|
|
},
|
|
|
|
required: ['userId'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.followingsRepository)
|
|
|
|
private followingsRepository: FollowingsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.noteReactionsRepository)
|
|
|
|
private noteReactionsRepository: NoteReactionsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.pageLikesRepository)
|
|
|
|
private pageLikesRepository: PageLikesRepository,
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Inject(DI.noteFavoritesRepository)
|
|
|
|
private noteFavoritesRepository: NoteFavoritesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.pollVotesRepository)
|
|
|
|
private pollVotesRepository: PollVotesRepository,
|
|
|
|
|
|
|
|
private driveFileEntityService: DriveFileEntityService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const user = await this.usersRepository.findOneBy({ id: ps.userId });
|
|
|
|
if (user == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await awaitAll({
|
|
|
|
notesCount: this.notesRepository.createQueryBuilder('note')
|
|
|
|
.where('note.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
|
|
|
repliesCount: this.notesRepository.createQueryBuilder('note')
|
|
|
|
.where('note.userId = :userId', { userId: user.id })
|
|
|
|
.andWhere('note.replyId IS NOT NULL')
|
|
|
|
.getCount(),
|
|
|
|
renotesCount: this.notesRepository.createQueryBuilder('note')
|
|
|
|
.where('note.userId = :userId', { userId: user.id })
|
|
|
|
.andWhere('note.renoteId IS NOT NULL')
|
|
|
|
.getCount(),
|
|
|
|
repliedCount: this.notesRepository.createQueryBuilder('note')
|
|
|
|
.where('note.replyUserId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
|
|
|
renotedCount: this.notesRepository.createQueryBuilder('note')
|
|
|
|
.where('note.renoteUserId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
|
|
|
pollVotesCount: this.pollVotesRepository.createQueryBuilder('vote')
|
|
|
|
.where('vote.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
|
|
|
pollVotedCount: this.pollVotesRepository.createQueryBuilder('vote')
|
|
|
|
.innerJoin('vote.note', 'note')
|
|
|
|
.where('note.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
|
|
|
localFollowingCount: this.followingsRepository.createQueryBuilder('following')
|
|
|
|
.where('following.followerId = :userId', { userId: user.id })
|
|
|
|
.andWhere('following.followeeHost IS NULL')
|
|
|
|
.getCount(),
|
|
|
|
remoteFollowingCount: this.followingsRepository.createQueryBuilder('following')
|
|
|
|
.where('following.followerId = :userId', { userId: user.id })
|
|
|
|
.andWhere('following.followeeHost IS NOT NULL')
|
|
|
|
.getCount(),
|
|
|
|
localFollowersCount: this.followingsRepository.createQueryBuilder('following')
|
|
|
|
.where('following.followeeId = :userId', { userId: user.id })
|
|
|
|
.andWhere('following.followerHost IS NULL')
|
|
|
|
.getCount(),
|
|
|
|
remoteFollowersCount: this.followingsRepository.createQueryBuilder('following')
|
|
|
|
.where('following.followeeId = :userId', { userId: user.id })
|
|
|
|
.andWhere('following.followerHost IS NOT NULL')
|
|
|
|
.getCount(),
|
|
|
|
sentReactionsCount: this.noteReactionsRepository.createQueryBuilder('reaction')
|
|
|
|
.where('reaction.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
|
|
|
receivedReactionsCount: this.noteReactionsRepository.createQueryBuilder('reaction')
|
|
|
|
.innerJoin('reaction.note', 'note')
|
|
|
|
.where('note.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
|
|
|
noteFavoritesCount: this.noteFavoritesRepository.createQueryBuilder('favorite')
|
|
|
|
.where('favorite.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
|
|
|
pageLikesCount: this.pageLikesRepository.createQueryBuilder('like')
|
|
|
|
.where('like.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
|
|
|
pageLikedCount: this.pageLikesRepository.createQueryBuilder('like')
|
|
|
|
.innerJoin('like.page', 'page')
|
|
|
|
.where('page.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
|
|
|
driveFilesCount: this.driveFilesRepository.createQueryBuilder('file')
|
|
|
|
.where('file.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
|
|
|
driveUsage: this.driveFileEntityService.calcDriveUsageOf(user),
|
|
|
|
});
|
|
|
|
|
2022-09-22 23:21:31 +02:00
|
|
|
return {
|
|
|
|
...result,
|
|
|
|
followingCount: result.localFollowingCount + result.remoteFollowingCount,
|
|
|
|
followersCount: result.localFollowersCount + result.remoteFollowersCount,
|
|
|
|
};
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|