( filter: FilterTemplate | FilterNooks, userId: UserId, )
| 111 | templateIds: RemoteTemplateId[] |
| 112 | } |
| 113 | export async function assertIsMod( |
| 114 | filter: FilterTemplate | FilterNooks, |
| 115 | userId: UserId, |
| 116 | ) { |
| 117 | let expectedLength = -1 |
| 118 | const { c: modCount } = await db |
| 119 | .selectFrom('nook') |
| 120 | .select(db.fn.count<SqliteCount>('nook.id').as('c')) |
| 121 | .where((eb) => |
| 122 | eb.exists( |
| 123 | eb |
| 124 | .selectFrom((eb) => |
| 125 | sql`json_each(${eb.ref('nook.moderators')})`.as('json_each'), |
| 126 | ) |
| 127 | .select(sql`1`.as('_')) |
| 128 | .where(sql`json_each.value`, '=', userId), |
| 129 | ), |
| 130 | ) |
| 131 | .$if('nooks' in filter, (eb) => { |
| 132 | const nooks = (filter as FilterNooks).nooks |
| 133 | expectedLength = nooks.length |
| 134 | return eb.where('nook.id', 'in', nooks) |
| 135 | }) |
| 136 | .$if('templateIds' in filter, (eb) => { |
| 137 | const templateIds = (filter as FilterTemplate).templateIds |
| 138 | expectedLength = templateIds.length |
| 139 | return eb |
| 140 | .innerJoin('template', 'template.nook', 'nook.id') |
| 141 | .where('template.id', 'in', templateIds.map(fromBase64Url)) |
| 142 | }) |
| 143 | .executeTakeFirstOrThrow() |
| 144 | if (expectedLength === -1) throwExp('ya goofed') |
| 145 | if (modCount !== expectedLength) { |
| 146 | throw new TRPCError({ |
| 147 | code: 'UNAUTHORIZED', |
| 148 | message: "You're not a mod.", |
| 149 | }) |
| 150 | } |
| 151 | } |
no test coverage detected