From 4771e0008dd26eadbb7eaff64255a6ec914fdadb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 10 Jul 2017 19:43:21 +0200 Subject: [PATCH] Better typescript typing for a better world --- .../friend-add/friend-add.component.ts | 2 +- .../app/+admin/users/shared/user.service.ts | 11 +--- .../users/user-add/user-add.component.ts | 7 ++- .../account-details.component.ts | 3 +- client/src/app/core/auth/auth-user.model.ts | 2 +- client/src/app/shared/users/user.service.ts | 15 ++--- client/src/app/signup/signup.component.ts | 7 ++- client/src/app/videos/shared/video.service.ts | 63 +++++++++---------- .../videos/video-edit/video-add.component.ts | 27 ++++---- scripts/watch/client.sh | 2 +- scripts/watch/server.sh | 2 +- server/controllers/api/remote/pods.ts | 8 +-- server/controllers/api/remote/videos.ts | 43 ++++++++----- server/controllers/api/users.ts | 16 +++-- server/controllers/api/videos/abuse.ts | 4 +- server/controllers/api/videos/index.ts | 5 +- server/controllers/api/videos/rate.ts | 23 +++++-- server/helpers/requests.ts | 18 ++++-- server/lib/friends.ts | 14 +++-- .../lib/request/abstract-request-scheduler.ts | 22 +++++-- server/lib/request/request-scheduler.ts | 8 +-- .../request/request-video-event-scheduler.ts | 22 ++++--- .../request/request-video-qadu-scheduler.ts | 31 +++++++-- server/middlewares/secure.ts | 11 ++-- server/models/pod/pod-interface.ts | 2 +- server/models/user/user-interface.ts | 3 +- .../models/user/user-video-rate-interface.ts | 2 +- server/models/video/video-abuse-interface.ts | 2 +- .../models/video/video-blacklist-interface.ts | 2 +- server/models/video/video-interface.ts | 2 +- shared/models/index.ts | 9 +-- shared/models/pods/index.ts | 3 + shared/models/pods/pod-signature.model.ts | 4 ++ shared/models/{ => pods}/pod.model.ts | 0 shared/models/pods/remote-video/index.ts | 7 +++ .../remote-qadu-video-request.model.ts | 10 +++ .../remote-video-create-request.model.ts | 27 ++++++++ .../remote-video-event-request.model.ts | 11 ++++ .../remote-video-remove-request.model.ts | 10 +++ ...remote-video-report-abuse-request.model.ts | 12 ++++ .../remote-video-request.model.ts | 4 ++ .../remote-video-update-request.model.ts | 23 +++++++ shared/models/user-video-rate.model.ts | 7 --- shared/models/users/index.ts | 4 ++ shared/models/users/user-create.model.ts | 5 ++ shared/models/users/user-role.type.ts | 1 + shared/models/users/user-update.model.ts | 4 ++ shared/models/{ => users}/user.model.ts | 2 +- shared/models/videos/index.ts | 10 +++ .../videos/user-video-rate-update.model.ts | 5 ++ shared/models/videos/user-video-rate.model.ts | 6 ++ shared/models/videos/user-video-rate.type.ts | 1 + .../models/videos/video-abuse-create.model.ts | 3 + .../models/{ => videos}/video-abuse.model.ts | 0 .../{ => videos}/video-blacklist.model.ts | 0 shared/models/videos/video-create.model.ts | 9 +++ shared/models/videos/video-rate.type.ts | 1 + shared/models/videos/video-update.model.ts | 9 +++ shared/models/{ => videos}/video.model.ts | 0 59 files changed, 400 insertions(+), 166 deletions(-) create mode 100644 shared/models/pods/index.ts create mode 100644 shared/models/pods/pod-signature.model.ts rename shared/models/{ => pods}/pod.model.ts (100%) create mode 100644 shared/models/pods/remote-video/index.ts create mode 100644 shared/models/pods/remote-video/remote-qadu-video-request.model.ts create mode 100644 shared/models/pods/remote-video/remote-video-create-request.model.ts create mode 100644 shared/models/pods/remote-video/remote-video-event-request.model.ts create mode 100644 shared/models/pods/remote-video/remote-video-remove-request.model.ts create mode 100644 shared/models/pods/remote-video/remote-video-report-abuse-request.model.ts create mode 100644 shared/models/pods/remote-video/remote-video-request.model.ts create mode 100644 shared/models/pods/remote-video/remote-video-update-request.model.ts delete mode 100644 shared/models/user-video-rate.model.ts create mode 100644 shared/models/users/index.ts create mode 100644 shared/models/users/user-create.model.ts create mode 100644 shared/models/users/user-role.type.ts create mode 100644 shared/models/users/user-update.model.ts rename shared/models/{ => users}/user.model.ts (75%) create mode 100644 shared/models/videos/index.ts create mode 100644 shared/models/videos/user-video-rate-update.model.ts create mode 100644 shared/models/videos/user-video-rate.model.ts create mode 100644 shared/models/videos/user-video-rate.type.ts create mode 100644 shared/models/videos/video-abuse-create.model.ts rename shared/models/{ => videos}/video-abuse.model.ts (100%) rename shared/models/{ => videos}/video-blacklist.model.ts (100%) create mode 100644 shared/models/videos/video-create.model.ts create mode 100644 shared/models/videos/video-rate.type.ts create mode 100644 shared/models/videos/video-update.model.ts rename shared/models/{ => videos}/video.model.ts (100%) diff --git a/client/src/app/+admin/friends/friend-add/friend-add.component.ts b/client/src/app/+admin/friends/friend-add/friend-add.component.ts index 35cf4a1f7..0449d26a9 100644 --- a/client/src/app/+admin/friends/friend-add/friend-add.component.ts +++ b/client/src/app/+admin/friends/friend-add/friend-add.component.ts @@ -15,7 +15,7 @@ import { FriendService } from '../shared' }) export class FriendAddComponent implements OnInit { form: FormGroup - hosts = [ ] + hosts: string[] = [ ] error: string = null constructor ( diff --git a/client/src/app/+admin/users/shared/user.service.ts b/client/src/app/+admin/users/shared/user.service.ts index d52993a29..1c1cd575e 100644 --- a/client/src/app/+admin/users/shared/user.service.ts +++ b/client/src/app/+admin/users/shared/user.service.ts @@ -3,6 +3,7 @@ import 'rxjs/add/operator/catch' import 'rxjs/add/operator/map' import { AuthHttp, RestExtractor, RestDataSource, User } from '../../../shared' +import { UserCreate } from '../../../../../../shared' @Injectable() export class UserService { @@ -13,14 +14,8 @@ export class UserService { private restExtractor: RestExtractor ) {} - addUser (username: string, password: string, email: string) { - const body = { - username, - email, - password - } - - return this.authHttp.post(UserService.BASE_USERS_URL, body) + addUser (userCreate: UserCreate) { + return this.authHttp.post(UserService.BASE_USERS_URL, userCreate) .map(this.restExtractor.extractDataBool) .catch(this.restExtractor.handleError) } diff --git a/client/src/app/+admin/users/user-add/user-add.component.ts b/client/src/app/+admin/users/user-add/user-add.component.ts index 2d25ab469..0dd99eccd 100644 --- a/client/src/app/+admin/users/user-add/user-add.component.ts +++ b/client/src/app/+admin/users/user-add/user-add.component.ts @@ -11,6 +11,7 @@ import { USER_EMAIL, USER_PASSWORD } from '../../../shared' +import { UserCreate } from '../../../../../../shared' @Component({ selector: 'my-user-add', @@ -57,11 +58,11 @@ export class UserAddComponent extends FormReactive implements OnInit { addUser () { this.error = null - const { username, password, email } = this.form.value + const userCreate: UserCreate = this.form.value - this.userService.addUser(username, password, email).subscribe( + this.userService.addUser(userCreate).subscribe( () => { - this.notificationsService.success('Success', `User ${username} created.`) + this.notificationsService.success('Success', `User ${userCreate.username} created.`) this.router.navigate([ '/admin/users/list' ]) }, diff --git a/client/src/app/account/account-details/account-details.component.ts b/client/src/app/account/account-details/account-details.component.ts index d7531cb55..547f045c4 100644 --- a/client/src/app/account/account-details/account-details.component.ts +++ b/client/src/app/account/account-details/account-details.component.ts @@ -11,6 +11,7 @@ import { UserService, USER_PASSWORD } from '../../shared' +import { UserUpdate } from '../../../../../shared' @Component({ selector: 'my-account-details', @@ -50,7 +51,7 @@ export class AccountDetailsComponent extends FormReactive implements OnInit { updateDetails () { const displayNSFW = this.form.value['displayNSFW'] - const details = { + const details: UserUpdate = { displayNSFW } diff --git a/client/src/app/core/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.ts index 65c37bcfa..9cb201907 100644 --- a/client/src/app/core/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts @@ -1,5 +1,5 @@ // Do not use the barrel (dependency loop) -import { UserRole } from '../../../../../shared/models/user.model' +import { UserRole } from '../../../../../shared/models/users/user-role.type' import { User } from '../../shared/users/user.model' export type TokenOptions = { diff --git a/client/src/app/shared/users/user.service.ts b/client/src/app/shared/users/user.service.ts index e956df5b1..b479ac034 100644 --- a/client/src/app/shared/users/user.service.ts +++ b/client/src/app/shared/users/user.service.ts @@ -6,6 +6,7 @@ import 'rxjs/add/operator/map' import { AuthService } from '../../core' import { AuthHttp } from '../auth' import { RestExtractor } from '../rest' +import { UserCreate, UserUpdate } from '../../../../../shared' @Injectable() export class UserService { @@ -27,7 +28,7 @@ export class UserService { changePassword (newPassword: string) { const url = UserService.BASE_USERS_URL + this.authService.getUser().id - const body = { + const body: UserUpdate = { password: newPassword } @@ -36,7 +37,7 @@ export class UserService { .catch((res) => this.restExtractor.handleError(res)) } - updateDetails (details: { displayNSFW: boolean }) { + updateDetails (details: UserUpdate) { const url = UserService.BASE_USERS_URL + this.authService.getUser().id return this.authHttp.put(url, details) @@ -44,14 +45,8 @@ export class UserService { .catch((res) => this.restExtractor.handleError(res)) } - signup (username: string, password: string, email: string) { - const body = { - username, - email, - password - } - - return this.http.post(UserService.BASE_USERS_URL + 'register', body) + signup (userCreate: UserCreate) { + return this.http.post(UserService.BASE_USERS_URL + 'register', userCreate) .map(this.restExtractor.extractDataBool) .catch(this.restExtractor.handleError) } diff --git a/client/src/app/signup/signup.component.ts b/client/src/app/signup/signup.component.ts index bcbc8ded3..981fe3c3b 100644 --- a/client/src/app/signup/signup.component.ts +++ b/client/src/app/signup/signup.component.ts @@ -12,6 +12,7 @@ import { USER_EMAIL, USER_PASSWORD } from '../shared' +import { UserCreate } from '../../../../shared' @Component({ selector: 'my-signup', @@ -58,11 +59,11 @@ export class SignupComponent extends FormReactive implements OnInit { signup () { this.error = null - const { username, password, email } = this.form.value + const userCreate: UserCreate = this.form.value - this.userService.signup(username, password, email).subscribe( + this.userService.signup(userCreate).subscribe( () => { - this.notificationsService.success('Success', `Registration for ${username} complete.`) + this.notificationsService.success('Success', `Registration for ${userCreate.username} complete.`) this.router.navigate([ '/videos/list' ]) }, diff --git a/client/src/app/videos/shared/video.service.ts b/client/src/app/videos/shared/video.service.ts index 7658d8ff0..dc12c0637 100644 --- a/client/src/app/videos/shared/video.service.ts +++ b/client/src/app/videos/shared/video.service.ts @@ -16,7 +16,13 @@ import { UserService } from '../../shared' import { Video } from './video.model' -import { UserVideoRate, VideoRateType } from '../../../../../shared' +import { + UserVideoRate, + VideoRateType, + VideoUpdate, + VideoAbuseCreate, + UserVideoRateUpdate +} from '../../../../../shared' @Injectable() export class VideoService { @@ -35,42 +41,15 @@ export class VideoService { ) {} loadVideoCategories () { - return this.http.get(VideoService.BASE_VIDEO_URL + 'categories') - .map(this.restExtractor.extractDataGet) - .subscribe(data => { - Object.keys(data).forEach(categoryKey => { - this.videoCategories.push({ - id: parseInt(categoryKey, 10), - label: data[categoryKey] - }) - }) - }) + return this.loadVideoAttributeEnum('categories', this.videoCategories) } loadVideoLicences () { - return this.http.get(VideoService.BASE_VIDEO_URL + 'licences') - .map(this.restExtractor.extractDataGet) - .subscribe(data => { - Object.keys(data).forEach(licenceKey => { - this.videoLicences.push({ - id: parseInt(licenceKey, 10), - label: data[licenceKey] - }) - }) - }) + return this.loadVideoAttributeEnum('licences', this.videoLicences) } loadVideoLanguages () { - return this.http.get(VideoService.BASE_VIDEO_URL + 'languages') - .map(this.restExtractor.extractDataGet) - .subscribe(data => { - Object.keys(data).forEach(languageKey => { - this.videoLanguages.push({ - id: parseInt(languageKey, 10), - label: data[languageKey] - }) - }) - }) + return this.loadVideoAttributeEnum('languages', this.videoLanguages) } getVideo (id: string): Observable