summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/RoleService.ts
diff options
context:
space:
mode:
authorHazelnoot <acomputerdog@gmail.com>2025-05-09 23:02:47 -0400
committerHazelnoot <acomputerdog@gmail.com>2025-05-14 23:24:51 -0400
commit57d32ea900d9b4c197b33ab3c6259109fb11fdae (patch)
tree6fd2b08746119896ea2c83c28dd0836d5a197faf /packages/backend/src/core/RoleService.ts
parentadd new featured tab for "users popular locally" (diff)
downloadsharkey-57d32ea900d9b4c197b33ab3c6259109fb11fdae.tar.gz
sharkey-57d32ea900d9b4c197b33ab3c6259109fb11fdae.tar.bz2
sharkey-57d32ea900d9b4c197b33ab3c6259109fb11fdae.zip
enforce "can trend" role policy in trending user page
Diffstat (limited to 'packages/backend/src/core/RoleService.ts')
-rw-r--r--packages/backend/src/core/RoleService.ts14
1 files changed, 8 insertions, 6 deletions
diff --git a/packages/backend/src/core/RoleService.ts b/packages/backend/src/core/RoleService.ts
index c3b60837cf..9e14c771c0 100644
--- a/packages/backend/src/core/RoleService.ts
+++ b/packages/backend/src/core/RoleService.ts
@@ -361,8 +361,9 @@ export class RoleService implements OnApplicationShutdown, OnModuleInit {
}
@bindThis
- public async getUserAssigns(userId: MiUser['id']) {
+ public async getUserAssigns(user: MiUser | MiUser['id']) {
const now = Date.now();
+ const userId = typeof(user) === 'object' ? user.id : user;
let assigns = await this.roleAssignmentByUserIdCache.fetch(userId, () => this.roleAssignmentsRepository.findBy({ userId }));
// 期限切れのロールを除外
assigns = assigns.filter(a => a.expiresAt == null || (a.expiresAt.getTime() > now));
@@ -370,12 +371,12 @@ export class RoleService implements OnApplicationShutdown, OnModuleInit {
}
@bindThis
- public async getUserRoles(userId: MiUser['id']) {
+ public async getUserRoles(userId: MiUser | MiUser['id']) {
const roles = await this.rolesCache.fetch(() => this.rolesRepository.findBy({}));
const followStats = await this.cacheService.getFollowStats(userId);
const assigns = await this.getUserAssigns(userId);
const assignedRoles = roles.filter(r => assigns.map(x => x.roleId).includes(r.id));
- const user = roles.some(r => r.target === 'conditional') ? await this.cacheService.findUserById(userId) : null;
+ const user = typeof(userId) === 'object' ? userId : roles.some(r => r.target === 'conditional') ? await this.cacheService.findUserById(userId) : null;
const matchedCondRoles = roles.filter(r => r.target === 'conditional' && this.evalCond(user!, assignedRoles, r.condFormula, followStats));
return [...assignedRoles, ...matchedCondRoles];
}
@@ -384,8 +385,9 @@ export class RoleService implements OnApplicationShutdown, OnModuleInit {
* 指定ユーザーのバッジロール一覧取得
*/
@bindThis
- public async getUserBadgeRoles(userId: MiUser['id']) {
+ public async getUserBadgeRoles(userOrId: MiUser | MiUser['id']) {
const now = Date.now();
+ const userId = typeof(userOrId) === 'object' ? userOrId.id : userOrId;
let assigns = await this.roleAssignmentByUserIdCache.fetch(userId, () => this.roleAssignmentsRepository.findBy({ userId }));
// 期限切れのロールを除外
assigns = assigns.filter(a => a.expiresAt == null || (a.expiresAt.getTime() > now));
@@ -395,7 +397,7 @@ export class RoleService implements OnApplicationShutdown, OnModuleInit {
const assignedBadgeRoles = assignedRoles.filter(r => r.asBadge);
const badgeCondRoles = roles.filter(r => r.asBadge && (r.target === 'conditional'));
if (badgeCondRoles.length > 0) {
- const user = roles.some(r => r.target === 'conditional') ? await this.cacheService.findUserById(userId) : null;
+ const user = typeof(userId) === 'object' ? userId : roles.some(r => r.target === 'conditional') ? await this.cacheService.findUserById(userId) : null;
const matchedBadgeCondRoles = badgeCondRoles.filter(r => this.evalCond(user!, assignedRoles, r.condFormula, followStats));
return [...assignedBadgeRoles, ...matchedBadgeCondRoles];
} else {
@@ -404,7 +406,7 @@ export class RoleService implements OnApplicationShutdown, OnModuleInit {
}
@bindThis
- public async getUserPolicies(userId: MiUser['id'] | null): Promise<RolePolicies> {
+ public async getUserPolicies(userId: MiUser | MiUser['id'] | null): Promise<RolePolicies> {
const basePolicies = { ...DEFAULT_POLICIES, ...this.meta.policies };
if (userId == null) return basePolicies;