From 8f581725651c4b2c213d75fc028e306bbf239d3e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 27 Aug 2021 10:15:55 +0200 Subject: [PATCH] Allow accounts to skip account setup modal --- client/src/app/app.component.html | 4 +- client/src/app/app.component.ts | 67 +++++++++------- client/src/app/app.module.ts | 8 +- client/src/app/core/users/user.model.ts | 2 + .../modal/account-setup-modal.component.ts | 73 ----------------- ...ccount-setup-warning-modal.component.html} | 10 ++- ...ccount-setup-warning-modal.component.scss} | 0 .../account-setup-warning-modal.component.ts | 79 +++++++++++++++++++ ...tml => admin-welcome-modal.component.html} | 0 ...css => admin-welcome-modal.component.scss} | 0 ...nt.ts => admin-welcome-modal.component.ts} | 20 +++-- ...instance-config-warning-modal.component.ts | 23 ++++-- server/controllers/api/users/me.ts | 1 + server/helpers/custom-validators/users.ts | 9 +-- server/initializers/constants.ts | 2 +- .../0665-no-account-warning-modal.ts | 27 +++++++ server/middlewares/validators/users.ts | 12 ++- server/models/user/user.ts | 19 +++-- server/tests/api/check-params/users.ts | 29 +++---- server/tests/api/users/users.ts | 5 +- shared/models/users/user-update-me.model.ts | 1 + shared/models/users/user.model.ts | 1 + support/doc/api/openapi.yaml | 4 + 23 files changed, 241 insertions(+), 155 deletions(-) delete mode 100644 client/src/app/modal/account-setup-modal.component.ts rename client/src/app/modal/{account-setup-modal.component.html => account-setup-warning-modal.component.html} (72%) rename client/src/app/modal/{account-setup-modal.component.scss => account-setup-warning-modal.component.scss} (100%) create mode 100644 client/src/app/modal/account-setup-warning-modal.component.ts rename client/src/app/modal/{welcome-modal.component.html => admin-welcome-modal.component.html} (100%) rename client/src/app/modal/{welcome-modal.component.scss => admin-welcome-modal.component.scss} (100%) rename client/src/app/modal/{welcome-modal.component.ts => admin-welcome-modal.component.ts} (68%) create mode 100644 server/initializers/migrations/0665-no-account-warning-modal.ts diff --git a/client/src/app/app.component.html b/client/src/app/app.component.html index da66e4ef0..6969329e8 100644 --- a/client/src/app/app.component.html +++ b/client/src/app/app.component.html @@ -61,8 +61,8 @@ - - + + diff --git a/client/src/app/app.component.ts b/client/src/app/app.component.ts index ae6046cc1..86b687173 100644 --- a/client/src/app/app.component.ts +++ b/client/src/app/app.component.ts @@ -1,5 +1,6 @@ import { Hotkey, HotkeysService } from 'angular2-hotkeys' -import { filter, map, switchMap } from 'rxjs/operators' +import { forkJoin } from 'rxjs' +import { filter, first, map } from 'rxjs/operators' import { DOCUMENT, getLocaleDirection, PlatformLocation } from '@angular/common' import { AfterViewInit, Component, Inject, LOCALE_ID, OnInit, ViewChild } from '@angular/core' import { DomSanitizer, SafeHtml } from '@angular/platform-browser' @@ -17,15 +18,15 @@ import { } from '@app/core' import { HooksService } from '@app/core/plugins/hooks.service' import { PluginService } from '@app/core/plugins/plugin.service' +import { AccountSetupWarningModalComponent } from '@app/modal/account-setup-warning-modal.component' import { CustomModalComponent } from '@app/modal/custom-modal.component' import { InstanceConfigWarningModalComponent } from '@app/modal/instance-config-warning-modal.component' -import { WelcomeModalComponent } from '@app/modal/welcome-modal.component' -import { AccountSetupModalComponent } from '@app/modal/account-setup-modal.component' +import { AdminWelcomeModalComponent } from '@app/modal/admin-welcome-modal.component' import { NgbConfig, NgbModal } from '@ng-bootstrap/ng-bootstrap' import { LoadingBarService } from '@ngx-loading-bar/core' import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' import { getShortLocale } from '@shared/core-utils/i18n' -import { BroadcastMessageLevel, HTMLServerConfig, ServerConfig, UserRole } from '@shared/models' +import { BroadcastMessageLevel, HTMLServerConfig, UserRole } from '@shared/models' import { MenuService } from './core/menu/menu.service' import { POP_STATE_MODAL_DISMISS } from './helpers' import { InstanceService } from './shared/shared-instance' @@ -38,8 +39,8 @@ import { InstanceService } from './shared/shared-instance' export class AppComponent implements OnInit, AfterViewInit { private static BROADCAST_MESSAGE_KEY = 'app-broadcast-message-dismissed' - @ViewChild('accountSetupModal') accountSetupModal: AccountSetupModalComponent - @ViewChild('welcomeModal') welcomeModal: WelcomeModalComponent + @ViewChild('accountSetupWarningModal') accountSetupWarningModal: AccountSetupWarningModalComponent + @ViewChild('adminWelcomeModal') adminWelcomeModal: AdminWelcomeModalComponent @ViewChild('instanceConfigWarningModal') instanceConfigWarningModal: InstanceConfigWarningModalComponent @ViewChild('customModal') customModal: CustomModalComponent @@ -221,33 +222,41 @@ export class AppComponent implements OnInit, AfterViewInit { } private openModalsIfNeeded () { - this.authService.userInformationLoaded - .pipe( - map(() => this.authService.getUser()), - filter(user => user.role === UserRole.ADMINISTRATOR), - switchMap(user => { - return this.serverService.getConfig() - .pipe(map(serverConfig => ({ serverConfig, user }))) - }) - ).subscribe(({ serverConfig, user }) => this._openAdminModalsIfNeeded(serverConfig, user)) + const userSub = this.authService.userInformationLoaded + .pipe(map(() => this.authService.getUser())) + + // Admin modal + userSub.pipe( + filter(user => user.role === UserRole.ADMINISTRATOR) + ).subscribe(user => this.openAdminModalsIfNeeded(user)) + + // Account modal + userSub.pipe( + filter(user => user.role !== UserRole.ADMINISTRATOR) + ).subscribe(user => this.openAccountModalsIfNeeded(user)) } - private _openAdminModalsIfNeeded (serverConfig: ServerConfig, user: User) { - if (user.noWelcomeModal !== true) return this.welcomeModal.show() + private openAdminModalsIfNeeded (user: User) { + if (this.adminWelcomeModal.shouldOpen(user)) { + return this.adminWelcomeModal.show() + } - if (user.noInstanceConfigWarningModal === true || !serverConfig.signup.allowed) return + if (!this.instanceConfigWarningModal.shouldOpenByUser(user)) return - this.instanceService.getAbout() - .subscribe(about => { - if ( - this.serverConfig.instance.name.toLowerCase() === 'peertube' || - !about.instance.terms || - !about.instance.administrator || - !about.instance.maintenanceLifetime - ) { - this.instanceConfigWarningModal.show(about) - } - }) + forkJoin([ + this.serverService.getConfig().pipe(first()), + this.instanceService.getAbout().pipe(first()) + ]).subscribe(([ config, about ]) => { + if (this.instanceConfigWarningModal.shouldOpen(config, about)) { + this.instanceConfigWarningModal.show(about) + } + }) + } + + private openAccountModalsIfNeeded (user: User) { + if (this.accountSetupWarningModal.shouldOpen(user)) { + this.accountSetupWarningModal.show(user) + } } private initHotkeys () { diff --git a/client/src/app/app.module.ts b/client/src/app/app.module.ts index 8d9c39ad8..bb20c2d83 100644 --- a/client/src/app/app.module.ts +++ b/client/src/app/app.module.ts @@ -17,8 +17,8 @@ import { ConfirmComponent } from './modal/confirm.component' import { CustomModalComponent } from './modal/custom-modal.component' import { InstanceConfigWarningModalComponent } from './modal/instance-config-warning-modal.component' import { QuickSettingsModalComponent } from './modal/quick-settings-modal.component' -import { WelcomeModalComponent } from './modal/welcome-modal.component' -import { AccountSetupModalComponent } from './modal/account-setup-modal.component' +import { AdminWelcomeModalComponent } from './modal/admin-welcome-modal.component' +import { AccountSetupWarningModalComponent } from './modal/account-setup-warning-modal.component' import { SharedActorImageModule } from './shared/shared-actor-image/shared-actor-image.module' import { SharedFormModule } from './shared/shared-forms' import { SharedGlobalIconModule } from './shared/shared-icons' @@ -54,9 +54,9 @@ export function loadConfigFactory (server: ServerService, pluginService: PluginS SuggestionComponent, HighlightPipe, - AccountSetupModalComponent, + AccountSetupWarningModalComponent, CustomModalComponent, - WelcomeModalComponent, + AdminWelcomeModalComponent, InstanceConfigWarningModalComponent, ConfirmComponent ], diff --git a/client/src/app/core/users/user.model.ts b/client/src/app/core/users/user.model.ts index 7467519c4..00078af5d 100644 --- a/client/src/app/core/users/user.model.ts +++ b/client/src/app/core/users/user.model.ts @@ -55,6 +55,7 @@ export class User implements UserServerModel { noInstanceConfigWarningModal: boolean noWelcomeModal: boolean + noAccountSetupWarningModal: boolean pluginAuth: string | null @@ -98,6 +99,7 @@ export class User implements UserServerModel { this.noInstanceConfigWarningModal = hash.noInstanceConfigWarningModal this.noWelcomeModal = hash.noWelcomeModal + this.noAccountSetupWarningModal = hash.noAccountSetupWarningModal this.notificationSettings = hash.notificationSettings diff --git a/client/src/app/modal/account-setup-modal.component.ts b/client/src/app/modal/account-setup-modal.component.ts deleted file mode 100644 index 4cb8de2c7..000000000 --- a/client/src/app/modal/account-setup-modal.component.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { Component, ElementRef, OnInit, ViewChild } from '@angular/core' -import { AuthService, ServerService, User, UserService } from '@app/core' -import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' -import { HTMLServerConfig } from '@shared/models' - -@Component({ - selector: 'my-account-setup-modal', - templateUrl: './account-setup-modal.component.html', - styleUrls: [ './account-setup-modal.component.scss' ] -}) -export class AccountSetupModalComponent implements OnInit { - @ViewChild('modal', { static: true }) modal: ElementRef - - user: User = null - ref: NgbModalRef = null - - private serverConfig: HTMLServerConfig - - constructor ( - private userService: UserService, - private authService: AuthService, - private modalService: NgbModal, - private serverService: ServerService - ) { } - - get userInformationLoaded () { - return this.authService.userInformationLoaded - } - - get instanceName () { - return this.serverConfig.instance.name - } - - get isUserRoot () { - return this.user.username === 'root' - } - - get hasAccountAvatar () { - return !!this.user.account.avatar - } - - get hasAccountDescription () { - return !!this.user.account.description - } - - ngOnInit () { - this.serverConfig = this.serverService.getHTMLConfig() - - this.authService.userInformationLoaded - .subscribe( - () => { - this.user = this.authService.getUser() - - if (this.isUserRoot) return - if (this.hasAccountAvatar && this.hasAccountDescription) return - if (this.userService.hasSignupInThisSession()) return - - this.show() - } - ) - } - - show () { - if (this.ref) return - - this.ref = this.modalService.open(this.modal, { - centered: true, - backdrop: 'static', - keyboard: false, - size: 'md' - }) - } -} diff --git a/client/src/app/modal/account-setup-modal.component.html b/client/src/app/modal/account-setup-warning-modal.component.html similarity index 72% rename from client/src/app/modal/account-setup-modal.component.html rename to client/src/app/modal/account-setup-warning-modal.component.html index d1f153835..614c0693d 100644 --- a/client/src/app/modal/account-setup-modal.component.html +++ b/client/src/app/modal/account-setup-warning-modal.component.html @@ -12,12 +12,18 @@

Help moderators and other users to know who you are by: