Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | 18x 18x 18x 18x 18x 18x 34x 34x 34x 34x 7x 7x 7x 5x 4x 4x 3x 1x 1x 2x 3x 26x 26x 4x 4x 4x 4x 3x 1x 4x 4x 3x 3x 3x 1x | /** * User blocking service. * @packageDocumentation */ import { Injectable, Inject, forwardRef } from '@nestjs/common'; import { DBService } from '../db/db.service'; import { UserService, userTableName } from '../user/user.service'; import { NBBlock } from './block.interface'; import { NBUser } from '../user/user.interface'; import { ServiceException } from '../service.exception'; /** * User blocking table name. */ export const blockTableName = 'NB_BLOCK'; /** * User blocking table service. */ @Injectable() export class BlockService { constructor( @Inject(forwardRef(() => DBService)) private readonly dbService: DBService, @Inject(forwardRef(() => UserService)) private readonly userService: UserService, ) {} /** * Block a user. * * @param userID The user's ID. * @param blockedUserID The ID of the user being blocked. * @returns The block record. */ public async blockUser( userID: string, blockedUserID: string, ): Promise<NBBlock> { const userExists = await this.userService.userExists(userID); const blockedUserExists = await this.userService.userExists(blockedUserID); if (userExists && blockedUserExists) { if (userID !== blockedUserID) { const blocked = await this.isBlocked(userID, blockedUserID); if (!blocked) { return this.dbService.create<NBBlock>(blockTableName, { userID, blockedUserID, }); } else { return this.dbService.getByFields<NBBlock>(blockTableName, { userID, blockedUserID, }); } } else { throw new ServiceException('Users cannot block themselves'); } } else { throw new ServiceException('User does not exist'); } } /** * Unblock a user. * * @param userID The user's ID. * @param blockedUserID The blocked user's ID. */ public async unblockUser( userID: string, blockedUserID: string, ): Promise<void> { await this.dbService.deleteByFields(blockTableName, { userID, blockedUserID, }); } /** * Determine whether or not a user is blocked. * * @param userID The user's ID. * @param otherUserID The other user's ID. * @returns Whether or not the user is blocked. */ public async isBlocked( userID: string, otherUserID: string, ): Promise<boolean> { const block = await this.dbService.getByFields<NBBlock>(blockTableName, { userID, blockedUserID: otherUserID, }); return !!block; } /** * Determine whether or not the other user has the user blocked. * * @param userID The user's ID. * @param otherUserID The other user's ID. * @returns Whether or not the other user has the user blocked. */ public async hasBlocked( userID: string, otherUserID: string, ): Promise<boolean> { const block = await this.dbService.getByFields<NBBlock>(blockTableName, { userID: otherUserID, blockedUserID: userID, }); return !!block; } /** * Get all of a user's block records. * * @param userID The user's ID. * @returns All of the user's block records. */ public async getBlocks(userID: string): Promise<NBBlock[]> { const userExists = await this.userService.userExists(userID); if (userExists) { return this.dbService.listByFields<NBBlock>(blockTableName, { userID }); } else { throw new ServiceException('User does not exist'); } } /** * Get all of the users a user has blocked. * * @param userID The user's ID. * @returns All of the users the user has blocked. */ public async getBlockedUsers(userID: string): Promise<NBUser[]> { const userExists = await this.userService.userExists(userID); if (userExists) { const sql = ` SELECT "${userTableName}".* FROM "${blockTableName}" JOIN "${userTableName}" ON "${blockTableName}"."blockedUserID" = "${userTableName}"."id" WHERE "${blockTableName}"."userID" = ?; `; const params = [userID]; return this.dbService.execute<NBUser>(sql, params); } else { throw new ServiceException('User does not exist'); } } } |