2022-02-27 11:07:39 +09:00
|
|
|
import { DriveFiles, Followings, NoteFavorites, NoteReactions, Notes, PageLikes, PollVotes, Users } from '@/models/index.js';
|
2022-06-08 17:59:48 +09:00
|
|
|
import { awaitAll } from '@/prelude/await-all.js';
|
2022-06-14 18:01:23 +09:00
|
|
|
import define from '../../define.js';
|
|
|
|
import { ApiError } from '../../error.js';
|
2020-11-25 21:31:34 +09:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['users'],
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: false,
|
2020-11-25 21:31:34 +09:00
|
|
|
|
2022-06-10 14:25:20 +09:00
|
|
|
description: 'Show statistics about a user.',
|
|
|
|
|
2020-11-25 21:31:34 +09:00
|
|
|
errors: {
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: '9e638e45-3b25-4ef7-8f95-07e8498f1819',
|
2020-11-25 21:31:34 +09:00
|
|
|
},
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2022-06-10 14:25:20 +09: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 22:27:10 +09:00
|
|
|
} as const;
|
2020-11-25 21:31:34 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
userId: { type: 'string', format: 'misskey:id' },
|
|
|
|
},
|
|
|
|
required: ['userId'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 14:05:32 +09:00
|
|
|
export default define(meta, paramDef, async (ps, me) => {
|
2022-03-26 15:34:00 +09:00
|
|
|
const user = await Users.findOneBy({ id: ps.userId });
|
2020-11-25 21:31:34 +09:00
|
|
|
if (user == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchUser);
|
|
|
|
}
|
|
|
|
|
2022-06-08 17:59:48 +09:00
|
|
|
const result = await awaitAll({
|
|
|
|
notesCount: Notes.createQueryBuilder('note')
|
2020-11-25 21:31:34 +09:00
|
|
|
.where('note.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
repliesCount: Notes.createQueryBuilder('note')
|
2020-11-25 21:31:34 +09:00
|
|
|
.where('note.userId = :userId', { userId: user.id })
|
|
|
|
.andWhere('note.replyId IS NOT NULL')
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
renotesCount: Notes.createQueryBuilder('note')
|
2020-11-25 21:31:34 +09:00
|
|
|
.where('note.userId = :userId', { userId: user.id })
|
|
|
|
.andWhere('note.renoteId IS NOT NULL')
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
repliedCount: Notes.createQueryBuilder('note')
|
2020-11-25 21:31:34 +09:00
|
|
|
.where('note.replyUserId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
renotedCount: Notes.createQueryBuilder('note')
|
2020-11-25 21:31:34 +09:00
|
|
|
.where('note.renoteUserId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
pollVotesCount: PollVotes.createQueryBuilder('vote')
|
2020-11-25 21:31:34 +09:00
|
|
|
.where('vote.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
pollVotedCount: PollVotes.createQueryBuilder('vote')
|
2020-11-25 21:31:34 +09:00
|
|
|
.innerJoin('vote.note', 'note')
|
|
|
|
.where('note.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
localFollowingCount: Followings.createQueryBuilder('following')
|
2020-11-25 21:31:34 +09:00
|
|
|
.where('following.followerId = :userId', { userId: user.id })
|
|
|
|
.andWhere('following.followeeHost IS NULL')
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
remoteFollowingCount: Followings.createQueryBuilder('following')
|
2020-11-25 21:31:34 +09:00
|
|
|
.where('following.followerId = :userId', { userId: user.id })
|
|
|
|
.andWhere('following.followeeHost IS NOT NULL')
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
localFollowersCount: Followings.createQueryBuilder('following')
|
2020-11-25 21:31:34 +09:00
|
|
|
.where('following.followeeId = :userId', { userId: user.id })
|
|
|
|
.andWhere('following.followerHost IS NULL')
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
remoteFollowersCount: Followings.createQueryBuilder('following')
|
2020-11-25 21:31:34 +09:00
|
|
|
.where('following.followeeId = :userId', { userId: user.id })
|
|
|
|
.andWhere('following.followerHost IS NOT NULL')
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
sentReactionsCount: NoteReactions.createQueryBuilder('reaction')
|
2020-11-25 21:31:34 +09:00
|
|
|
.where('reaction.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
receivedReactionsCount: NoteReactions.createQueryBuilder('reaction')
|
2020-11-25 21:31:34 +09:00
|
|
|
.innerJoin('reaction.note', 'note')
|
|
|
|
.where('note.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
noteFavoritesCount: NoteFavorites.createQueryBuilder('favorite')
|
2020-11-25 21:31:34 +09:00
|
|
|
.where('favorite.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
pageLikesCount: PageLikes.createQueryBuilder('like')
|
2020-11-25 21:31:34 +09:00
|
|
|
.where('like.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
pageLikedCount: PageLikes.createQueryBuilder('like')
|
2020-11-25 21:31:34 +09:00
|
|
|
.innerJoin('like.page', 'page')
|
|
|
|
.where('page.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
driveFilesCount: DriveFiles.createQueryBuilder('file')
|
2020-11-25 21:31:34 +09:00
|
|
|
.where('file.userId = :userId', { userId: user.id })
|
|
|
|
.getCount(),
|
2022-06-08 17:59:48 +09:00
|
|
|
driveUsage: DriveFiles.calcDriveUsageOf(user),
|
|
|
|
});
|
2020-11-25 21:31:34 +09:00
|
|
|
|
2022-06-08 17:59:48 +09:00
|
|
|
result.followingCount = result.localFollowingCount + result.remoteFollowingCount;
|
|
|
|
result.followersCount = result.localFollowersCount + result.remoteFollowersCount;
|
|
|
|
|
|
|
|
return result;
|
2020-11-25 21:31:34 +09:00
|
|
|
});
|