Skip to content

Commit

Permalink
✔ User Full Private Mode ~
Browse files Browse the repository at this point in the history
  • Loading branch information
bifeldy committed May 22, 2023
1 parent dd623c7 commit 0490b6c
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 62 deletions.
45 changes: 31 additions & 14 deletions src/api/controllers/anime-/anime-berkas.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ApiExcludeController } from '@nestjs/swagger';
import { Request, Response } from 'express';
import { ILike, In } from 'typeorm';

import { UserModel } from '../../../models/req-res.model';
import { RoleModel, UserModel } from '../../../models/req-res.model';

import { FilterApiKeyAccess } from '../../decorators/filter-api-key-access.decorator';

Expand All @@ -30,20 +30,37 @@ export class AnimeBerkasController {
const queryRow = parseInt(req.query['row'] as string);
const animeId = req.query['id'] ? (req.query['id'] as string).split(',').map(Number) : req.body.id;
if (Array.isArray(animeId) && animeId.length > 0) {
const [files, count] = await this.berkasRepo.findAndCount({
where: [
{
...((user?.verified) ? {
// Verified User Can See Private Berkas
} : {
private: false
}),
name: ILike(`%${req.query['q'] ? req.query['q'] : ''}%`),
anime_: {
id: In(animeId)
}
const sqlWhere = [
{
...((user?.verified) ? {
// Verified User Can See Private Berkas From Public Profile
} : {
private: false
}),
name: ILike(`%${req.query['q'] ? req.query['q'] : ''}%`),
anime_: {
id: In(animeId)
},
user_: {
private: false
}
],
}
];
const userFilesCriteria: any = {};
if (user) {
userFilesCriteria.name = ILike(`%${req.query['q'] ? req.query['q'] : ''}%`);
if (user.role === RoleModel.ADMIN || user.role === RoleModel.MODERATOR) {
// Admin & Mod Can See Private Berkas From All Private Profile
} else {
// Current User Can See Private Berkas From Their Private Profile
userFilesCriteria.user_ = {
id: user.id
};
}
sqlWhere.push(userFilesCriteria);
}
const [files, count] = await this.berkasRepo.findAndCount({
where: sqlWhere,
order: {
...((req.query['sort'] && req.query['order']) ? {
[req.query['sort'] as string]: (req.query['order'] as string).toUpperCase()
Expand Down
37 changes: 27 additions & 10 deletions src/api/controllers/berkas.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,34 @@ export class BerkasController {
const user: UserModel = res.locals['user'];
const queryPage = parseInt(req.query['page'] as string);
const queryRow = parseInt(req.query['row'] as string);
const [files, count] = await this.berkasRepo.findAndCount({
where: [
{
...((user?.verified) ? {
// Verified User Can See Private Berkas
} : {
private: false
}),
name: ILike(`%${req.query['q'] ? req.query['q'] : ''}%`)
const sqlWhere = [
{
...((user?.verified) ? {
// Verified User Can See Private Berkas From Public Profile
} : {
private: false
}),
name: ILike(`%${req.query['q'] ? req.query['q'] : ''}%`),
user_: {
private: false
}
],
}
];
const userFilesCriteria: any = {};
if (user) {
userFilesCriteria.name = ILike(`%${req.query['q'] ? req.query['q'] : ''}%`);
if (user.role === RoleModel.ADMIN || user.role === RoleModel.MODERATOR) {
// Admin & Mod Can See Private Berkas From All Private Profile
} else {
// Current User Can See Private Berkas From Their Private Profile
userFilesCriteria.user_ = {
id: user.id
};
}
sqlWhere.push(userFilesCriteria);
}
const [files, count] = await this.berkasRepo.findAndCount({
where: sqlWhere,
order: {
...((req.query['sort'] && req.query['order']) ? {
[req.query['sort'] as string]: (req.query['order'] as string).toUpperCase()
Expand Down
45 changes: 31 additions & 14 deletions src/api/controllers/dorama-/dorama-berkas.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ApiExcludeController } from '@nestjs/swagger';
import { Request, Response } from 'express';
import { ILike, In } from 'typeorm';

import { UserModel } from '../../../models/req-res.model';
import { RoleModel, UserModel } from '../../../models/req-res.model';

import { FilterApiKeyAccess } from '../../decorators/filter-api-key-access.decorator';

Expand All @@ -30,20 +30,37 @@ export class DoramaBerkasController {
const queryRow = parseInt(req.query['row'] as string);
const doramaId = req.query['id'] ? (req.query['id'] as string).split(',') : req.body.id;
if (Array.isArray(doramaId) && doramaId.length > 0) {
const [files, count] = await this.berkasRepo.findAndCount({
where: [
{
...((user?.verified) ? {
// Verified User Can See Private Berkas
} : {
private: false
}),
name: ILike(`%${req.query['q'] ? req.query['q'] : ''}%`),
dorama_: {
id: In(doramaId)
}
const sqlWhere = [
{
...((user?.verified) ? {
// Verified User Can See Private Berkas From Public Profile
} : {
private: false
}),
name: ILike(`%${req.query['q'] ? req.query['q'] : ''}%`),
dorama_: {
id: In(doramaId)
},
user_: {
private: false
}
],
}
];
const userFilesCriteria: any = {};
if (user) {
userFilesCriteria.name = ILike(`%${req.query['q'] ? req.query['q'] : ''}%`);
if (user.role === RoleModel.ADMIN || user.role === RoleModel.MODERATOR) {
// Admin & Mod Can See Private Berkas From All Private Profile
} else {
// Current User Can See Private Berkas From Their Private Profile
userFilesCriteria.user_ = {
id: user.id
};
}
sqlWhere.push(userFilesCriteria);
}
const [files, count] = await this.berkasRepo.findAndCount({
where: sqlWhere,
order: {
...((req.query['sort'] && req.query['order']) ? {
[req.query['sort'] as string]: (req.query['order'] as string).toUpperCase()
Expand Down
14 changes: 12 additions & 2 deletions src/api/controllers/fansub-/fansub-berkas.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Controller, HttpCode, HttpException, HttpStatus, Patch, Req, Res } from
import { ApiExcludeController } from '@nestjs/swagger';
import { Request, Response } from 'express';

import { UserModel } from '../../../models/req-res.model';
import { RoleModel, UserModel } from '../../../models/req-res.model';

import { FilterApiKeyAccess } from '../../decorators/filter-api-key-access.decorator';

Expand Down Expand Up @@ -39,10 +39,20 @@ export class FansubBerkasController {
.where('fansub_.id IN (:...id)', { id: fansubId })
.andWhere('berkas.name ILIKE :query', { query: `%${req.query['q'] ? req.query['q'] : ''}%` });
if (user?.verified) {
// Verified User Can See Private Berkas
// Verified User Can See Private Berkas From Public Profile
} else {
fileRepoQuery = fileRepoQuery.andWhere('berkas.private = :isPrivate', { isPrivate: false });
}
fileRepoQuery = fileRepoQuery.andWhere('user_.private = :isPrivate', { isPrivate: false });
if (user) {
fileRepoQuery = fileRepoQuery.orWhere('berkas.name ILIKE :query', { query: `%${req.query['q'] ? req.query['q'] : ''}%` });
if (user.role === RoleModel.ADMIN || user.role === RoleModel.MODERATOR) {
// Admin & Mod Can See Private Berkas From Private Profile
} else {
// Current User Can See Private Berkas From Their Private Profile
fileRepoQuery = fileRepoQuery.andWhere('user_.id = :userId', { userId: user.id });
}
}
if (req.query['sort'] && req.query['order']) {
fileRepoQuery = fileRepoQuery.orderBy(`berkas.${req.query['sort']}`, (req.query['order'] as (any)).toUpperCase());
} else {
Expand Down
44 changes: 39 additions & 5 deletions src/api/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class UserController {
@FilterApiKeyAccess()
async updateByUsername(@Req() req: Request, @Res({ passthrough: true }) res: Response): Promise<any> {
try {
if ('description' in req.body || 'new_password' in req.body || 'image_photo' in req.body || 'image_cover' in req.body) {
if ('description' in req.body || 'new_password' in req.body || 'image_photo' in req.body || 'image_cover' in req.body || 'private' in req.body) {
const user: UserModel = res.locals['user'];
const old_password = this.cs.hashPassword(req.body.old_password);
const selectedUser = await this.userRepo.findOneOrFail({
Expand All @@ -183,6 +183,9 @@ export class UserController {
if ('new_password' in req.body) {
selectedUser.password = this.cs.hashPassword(req.body.new_password);
}
if ('private' in req.body) {
selectedUser.private = req.body.private;
}
const selectedProfile = await this.profileRepo.findOneOrFail({
where: [
{ id: selectedUser.profile_.id }
Expand Down Expand Up @@ -322,9 +325,17 @@ export class UserController {
try {
const queryPage = parseInt(req.query['page'] as string);
const queryRow = parseInt(req.query['row'] as string);
const user: UserModel = res.locals['user'];
const selectedUser = await this.userRepo.findOneOrFail({
where: [
{ username: ILike(req.params['username']) }
{
username: ILike(req.params['username']),
...((user?.role === RoleModel.ADMIN || user?.role === RoleModel.MODERATOR || user?.username === req.params['username']) ? {
// Admin, Mod, & User Itself Can See Private Profile
} : {
private: false
})
}
]
});
const [files, count] = await this.berkasRepo.findAndCount({
Expand Down Expand Up @@ -402,9 +413,17 @@ export class UserController {
try {
const queryPage = parseInt(req.query['page'] as string);
const queryRow = parseInt(req.query['row'] as string);
const user: UserModel = res.locals['user'];
const selectedUser = await this.userRepo.findOneOrFail({
where: [
{ username: ILike(req.params['username']) }
{
username: ILike(req.params['username']),
...((user.role === RoleModel.ADMIN || user.role === RoleModel.MODERATOR || user.username === req.params['username']) ? {
// Admin, Mod, & User Itself Can See Private Profile
} : {
private: false
})
}
]
});
const [komens, count] = await this.komentarRepo.findAndCount({
Expand Down Expand Up @@ -464,9 +483,17 @@ export class UserController {
try {
const queryPage = parseInt(req.query['page'] as string);
const queryRow = parseInt(req.query['row'] as string);
const user: UserModel = res.locals['user'];
const selectedUser = await this.userRepo.findOneOrFail({
where: [
{ username: ILike(req.params['username']) }
{
username: ILike(req.params['username']),
...((user.role === RoleModel.ADMIN || user.role === RoleModel.MODERATOR || user.username === req.params['username']) ? {
// Admin, Mod, & User Itself Can See Private Profile
} : {
private: false
})
}
]
});
const [likedislikes, count] = await this.likeDislikeRepo.findAndCount({
Expand Down Expand Up @@ -586,7 +613,14 @@ export class UserController {
const user: UserModel = res.locals['user'];
const selectedUser = await this.userRepo.findOneOrFail({
where: [
{ username: ILike(req.params['username']) }
{
username: ILike(req.params['username']),
...((user.role === RoleModel.ADMIN || user.role === RoleModel.MODERATOR || user.username === req.params['username']) ? {
// Admin, Mod, & User Itself Can See Private Profile
} : {
private: false
})
}
]
});
const [tracks, count] = await this.trackRepo.findAndCount({
Expand Down
3 changes: 3 additions & 0 deletions src/api/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export class User implements UserModel {
@Column({ type: 'boolean', default: false })
verified: boolean;

@Column({ type: 'boolean', default: false })
private: boolean;

@Exclude()
@Column({ type: 'text', nullable: true })
session_token: string;
Expand Down
33 changes: 21 additions & 12 deletions src/app/_pages/user/user-detail/user-detail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { environment } from '../../../../environments/app/environment';
import { RoleModel } from '../../../../models/req-res.model';

import { GlobalService } from '../../../_shared/services/global.service';
import { AuthService } from '../../../_shared/services/auth.service';
import { BusyService } from '../../../_shared/services/busy.service';
import { FabService } from '../../../_shared/services/fab.service';
import { UserService } from '../../../_shared/services/user.service';
Expand All @@ -29,17 +30,7 @@ export class UserDetailComponent implements OnInit, OnDestroy {

panelData = [];

tabData: any = [
{
name: 'Berkas',
icon: 'file_copy',
type: 'table',
data: {
column: ['Proyek', /* 'Image', */ 'Nama Berkas', 'Tanggal', 'Kunjungan', 'Pemilik'],
row: []
}
}
];
tabData: any = [];

count = 0;
page = 1;
Expand All @@ -58,6 +49,7 @@ export class UserDetailComponent implements OnInit, OnDestroy {
constructor(
private router: Router,
private activatedRoute: ActivatedRoute,
private as: AuthService,
private gs: GlobalService,
private bs: BusyService,
private fs: FabService,
Expand Down Expand Up @@ -126,8 +118,25 @@ export class UserDetailComponent implements OnInit, OnDestroy {
this.panelData.push({ title: 'Tentang Saya', icon: 'info', text: this.userData.profile_.description });
this.fs.initializeFab('edit', null, 'Ubah Profil', `/user/${this.username}/edit`, false);
this.checkBanned();
this.getUserBerkas();
this.getUserGroup();
this.tabData = [];
if (
!this.userData.private ||
this.as.currentUserSubject?.value?.username === this.username ||
this.as.currentUserSubject?.value?.role === RoleModel.ADMIN ||
this.as.currentUserSubject?.value?.role === RoleModel.MODERATOR
) {
this.tabData.push({
name: 'Berkas',
icon: 'file_copy',
type: 'table',
data: {
column: ['Proyek', /* 'Image', */ 'Nama Berkas', 'Tanggal', 'Kunjungan', 'Pemilik'],
row: []
}
});
this.getUserBerkas();
}
}
},
error: err => {
Expand Down
7 changes: 6 additions & 1 deletion src/app/_pages/user/user-edit/user-edit.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@
</small>
</mat-error>
</div>
<div class="col-12" *ngIf="imagePhoto && !fg.value.image_photo">
<div class="col-12 mb-4" *ngIf="imagePhoto && !fg.value.image_photo">
<button mat-raised-button class="w-100" color="primary" type="button" [disabled]="submitted" (click)="submitPhotoImage()">
<mat-icon class="me-1">add_photo_alternate</mat-icon>
Unggah
</button>
</div>
<div class="col-12 p-3">
<mat-slide-toggle formControlName="private" matTooltip="Hanya Tampilkan Informasi Profil">
Jadikan Private, Sembunyikan Aktivitas
</mat-slide-toggle>
</div>
</div>
</div>

Expand Down
3 changes: 2 additions & 1 deletion src/app/_pages/user/user-edit/user-edit.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ export class UserEditComponent implements OnInit, OnDestroy {
old_password: [null, Validators.compose([Validators.required, Validators.minLength(8), Validators.pattern(CONSTANTS.regexEnglishKeyboardKeys)])],
new_password: [null, Validators.compose([Validators.minLength(8), Validators.pattern(CONSTANTS.regexEnglishKeyboardKeys)])],
image_photo: [null, Validators.compose([Validators.pattern(CONSTANTS.regexUrl)])],
image_cover: [null, Validators.compose([Validators.pattern(CONSTANTS.regexUrl)])]
image_cover: [null, Validators.compose([Validators.pattern(CONSTANTS.regexUrl)])],
private: [data.private, Validators.compose([Validators.required])]
});
this.image_photo = data.image_url;
this.image_photo_original = this.image_photo;
Expand Down
Loading

0 comments on commit 0490b6c

Please sign in to comment.