|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: syuilo and misskey-project |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 4 | + */ |
| 5 | + |
| 6 | +import { Inject, Injectable } from '@nestjs/common'; |
| 7 | +import { Brackets } from 'typeorm'; |
| 8 | +import { DI } from '@/di-symbols.js'; |
| 9 | +import type { |
| 10 | + FollowingsRepository, |
| 11 | + UserProfilesRepository, |
| 12 | +} from '@/models/_.js'; |
| 13 | +import { Endpoint } from '@/server/api/endpoint-base.js'; |
| 14 | +import { UserEntityService } from '@/core/entities/UserEntityService.js'; |
| 15 | +import type { Packed } from '@/misc/json-schema.js'; |
| 16 | + |
| 17 | +export const meta = { |
| 18 | + tags: ['users'], |
| 19 | + |
| 20 | + requireCredential: true, |
| 21 | + kind: 'read:account', |
| 22 | + |
| 23 | + description: 'Find users who have a birthday on the specified range.', |
| 24 | + |
| 25 | + res: { |
| 26 | + type: 'array', |
| 27 | + optional: false, nullable: false, |
| 28 | + items: { |
| 29 | + type: 'object', |
| 30 | + optional: false, nullable: false, |
| 31 | + properties: { |
| 32 | + id: { |
| 33 | + type: 'string', |
| 34 | + optional: false, nullable: false, |
| 35 | + format: 'misskey:id', |
| 36 | + }, |
| 37 | + birthday: { |
| 38 | + type: 'string', |
| 39 | + optional: false, nullable: false, |
| 40 | + }, |
| 41 | + user: { |
| 42 | + type: 'object', |
| 43 | + optional: false, nullable: false, |
| 44 | + ref: 'UserLite', |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | +} as const; |
| 50 | + |
| 51 | +export const paramDef = { |
| 52 | + type: 'object', |
| 53 | + properties: { |
| 54 | + limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 }, |
| 55 | + offset: { type: 'integer', default: 0 }, |
| 56 | + birthday: { |
| 57 | + oneOf: [{ |
| 58 | + type: 'object', |
| 59 | + properties: { |
| 60 | + month: { type: 'integer', minimum: 1, maximum: 12 }, |
| 61 | + day: { type: 'integer', minimum: 1, maximum: 31 }, |
| 62 | + }, |
| 63 | + required: ['month', 'day'], |
| 64 | + }, { |
| 65 | + type: 'object', |
| 66 | + properties: { |
| 67 | + begin: { |
| 68 | + type: 'object', |
| 69 | + properties: { |
| 70 | + month: { type: 'integer', minimum: 1, maximum: 12 }, |
| 71 | + day: { type: 'integer', minimum: 1, maximum: 31 }, |
| 72 | + }, |
| 73 | + required: ['month', 'day'], |
| 74 | + }, |
| 75 | + end: { |
| 76 | + type: 'object', |
| 77 | + properties: { |
| 78 | + month: { type: 'integer', minimum: 1, maximum: 12 }, |
| 79 | + day: { type: 'integer', minimum: 1, maximum: 31 }, |
| 80 | + }, |
| 81 | + required: ['month', 'day'], |
| 82 | + }, |
| 83 | + }, |
| 84 | + required: ['begin', 'end'], |
| 85 | + }], |
| 86 | + }, |
| 87 | + }, |
| 88 | + required: ['birthday'], |
| 89 | +} as const; |
| 90 | + |
| 91 | +@Injectable() |
| 92 | +export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export |
| 93 | + constructor( |
| 94 | + @Inject(DI.userProfilesRepository) |
| 95 | + private userProfilesRepository: UserProfilesRepository, |
| 96 | + @Inject(DI.followingsRepository) |
| 97 | + private followingsRepository: FollowingsRepository, |
| 98 | + |
| 99 | + private userEntityService: UserEntityService, |
| 100 | + ) { |
| 101 | + super(meta, paramDef, async (ps, me) => { |
| 102 | + const query = this.followingsRepository |
| 103 | + .createQueryBuilder('following') |
| 104 | + .andWhere('following.followerId = :userId', { userId: me.id }) |
| 105 | + .innerJoin(this.userProfilesRepository.metadata.targetName, 'followeeProfile', 'followeeProfile.userId = following.followeeId'); |
| 106 | + |
| 107 | + if (Object.hasOwn(ps.birthday, 'begin') && Object.hasOwn(ps.birthday, 'end')) { |
| 108 | + const range = ps.birthday as { begin: { month: number; day: number }; end: { month: number; day: number }; }; |
| 109 | + |
| 110 | + // 誕生日は mmdd の形式の最大4桁の数字(例: 8月30日 → 830)でインデックスが効くようになっているので、その形式に変換 |
| 111 | + const begin = range.begin.month * 100 + range.begin.day; |
| 112 | + const end = range.end.month * 100 + range.end.day; |
| 113 | + |
| 114 | + if (begin <= end) { |
| 115 | + query.andWhere('get_birthday_date(followeeProfile.birthday) BETWEEN :begin AND :end', { begin, end }); |
| 116 | + } else { |
| 117 | + // 12/31 から 1/1 の範囲を取得するために OR で対応 |
| 118 | + query.andWhere(new Brackets(qb => { |
| 119 | + qb.where('get_birthday_date(followeeProfile.birthday) BETWEEN :begin AND 1231', { begin }); |
| 120 | + qb.orWhere('get_birthday_date(followeeProfile.birthday) BETWEEN 101 AND :end', { end }); |
| 121 | + })); |
| 122 | + } |
| 123 | + } else { |
| 124 | + const { month, day } = ps.birthday as { month: number; day: number }; |
| 125 | + // なぜか get_birthday_date() = :birthday だとインデックスが効かないので、BETWEEN で対応 |
| 126 | + query.andWhere('get_birthday_date(followeeProfile.birthday) BETWEEN :birthday AND :birthday', { birthday: month * 100 + day }); |
| 127 | + } |
| 128 | + |
| 129 | + query.select('following.followeeId', 'user_id'); |
| 130 | + query.addSelect('get_birthday_date(followeeProfile.birthday)', 'birthday_date'); |
| 131 | + query.orderBy('birthday_date', 'ASC'); |
| 132 | + |
| 133 | + const birthdayUsers = await query |
| 134 | + .offset(ps.offset).limit(ps.limit) |
| 135 | + .getRawMany<{ birthday_date: number; user_id: string }>(); |
| 136 | + |
| 137 | + const users = new Map<string, Packed<'UserLite'>>(( |
| 138 | + await this.userEntityService.packMany( |
| 139 | + birthdayUsers.map(u => u.user_id), |
| 140 | + me, |
| 141 | + { schema: 'UserLite' }, |
| 142 | + ) |
| 143 | + ).map(u => [u.id, u])); |
| 144 | + |
| 145 | + return birthdayUsers |
| 146 | + .map(item => { |
| 147 | + const birthday = new Date(); |
| 148 | + birthday.setHours(0, 0, 0, 0); |
| 149 | + // item.birthday_date は mmdd の形式の最大4桁の数字(例: 8月30日 → 830)で出力されるので、日付に戻してDateオブジェクトに設定 |
| 150 | + birthday.setMonth(Math.floor(item.birthday_date / 100) - 1, item.birthday_date % 100); |
| 151 | + |
| 152 | + if (birthday.getTime() < new Date().setHours(0, 0, 0, 0)) { |
| 153 | + birthday.setFullYear(new Date().getFullYear() + 1); |
| 154 | + } |
| 155 | + |
| 156 | + const birthdayStr = `${birthday.getFullYear()}-${(birthday.getMonth() + 1).toString().padStart(2, '0')}-${(birthday.getDate()).toString().padStart(2, '0')}`; |
| 157 | + return { |
| 158 | + id: item.user_id, |
| 159 | + birthday: birthdayStr, |
| 160 | + user: users.get(item.user_id), |
| 161 | + }; |
| 162 | + }) |
| 163 | + .filter(item => item.user != null) |
| 164 | + .map(item => item as { id: string; birthday: string; user: Packed<'UserLite'> }); |
| 165 | + }); |
| 166 | + } |
| 167 | +} |
0 commit comments