Skip to content
Snippets Groups Projects
Commit c5350a59 authored by Lukas Mauß's avatar Lukas Mauß Committed by Klaus-Dieter Quibeldey-Cirkel
Browse files

Show error message and navigate to home if user enters non existing room with direct link

parent 1d0f374d
No related merge requests found
......@@ -2,11 +2,14 @@ import { Injectable } from '@angular/core';
import { Room } from '../../models/room';
import { UserRole } from '../../models/user-roles.enum';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Observable, throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { AuthenticationService } from './authentication.service';
import { BaseHttpService } from './base-http.service';
import { EventService } from '../util/event.service';
import { NotificationService } from '../util/notification.service';
import { Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
const httpOptions = {
headers: new HttpHeaders({})
......@@ -25,7 +28,10 @@ export class RoomService extends BaseHttpService {
constructor(
private http: HttpClient,
private eventService: EventService,
private authService: AuthenticationService
private authService: AuthenticationService,
private translateService: TranslateService,
private notificationService: NotificationService,
private router: Router
) {
super();
}
......@@ -33,7 +39,7 @@ export class RoomService extends BaseHttpService {
getCreatorRooms(): Observable<Room[]> {
const connectionUrl = this.apiUrl.base + this.apiUrl.rooms + this.apiUrl.findRooms;
return this.http.post<Room[]>(connectionUrl, {
properties: { ownerId: this.authService.getUser().id },
properties: {ownerId: this.authService.getUser().id},
externalFilters: {}
}).pipe(
tap((rooms) => {
......@@ -49,7 +55,7 @@ export class RoomService extends BaseHttpService {
const connectionUrl = this.apiUrl.base + this.apiUrl.rooms + this.apiUrl.findRooms;
return this.http.post<Room[]>(connectionUrl, {
properties: {},
externalFilters: { inHistoryOfUserId: this.authService.getUser().id }
externalFilters: {inHistoryOfUserId: this.authService.getUser().id}
}).pipe(
tap((rooms) => {
for (const r of rooms) {
......@@ -60,7 +66,7 @@ export class RoomService extends BaseHttpService {
);
}
addRoom(room: Room, exc?: () => void ): Observable<Room> {
addRoom(room: Room, exc?: () => void): Observable<Room> {
delete room.id;
delete room.revision;
const connectionUrl = this.apiUrl.base + this.apiUrl.rooms + '/';
......@@ -73,29 +79,29 @@ export class RoomService extends BaseHttpService {
if (exc) {
exc();
}
return this.handleError<Room>(`add Room ${room}`);
return this.handleError<Room>(`add Room ${ room }`);
})
);
}
getRoom(id: string): Observable<Room> {
const connectionUrl = `${ this.apiUrl.base + this.apiUrl.rooms }/${ id }`;
const connectionUrl = `${ this.apiUrl.base + this.apiUrl.rooms }/${ id }`;
return this.http.get<Room>(connectionUrl).pipe(
tap(room => this.setRoomId(room)),
catchError(this.handleError<Room>(`getRoom keyword=${ id }`))
catchError(this.handleRoomError<Room>(`getRoom keyword=${ id }`))
);
}
getRoomByShortId(shortId: string): Observable<Room> {
const connectionUrl = `${ this.apiUrl.base + this.apiUrl.rooms }/~${ shortId }`;
const connectionUrl = `${ this.apiUrl.base + this.apiUrl.rooms }/~${ shortId }`;
return this.http.get<Room>(connectionUrl).pipe(
tap(room => this.setRoomId(room)),
catchError(this.handleError<Room>(`getRoom shortId=${ shortId }`))
catchError(this.handleRoomError<Room>(`getRoom shortId=${ shortId }`))
);
}
getErrorHandledRoomByShortId(shortId: string, err: () => void ): Observable<Room> {
const connectionUrl = `${ this.apiUrl.base + this.apiUrl.rooms }/~${ shortId }`;
getErrorHandledRoomByShortId(shortId: string, err: () => void): Observable<Room> {
const connectionUrl = `${ this.apiUrl.base + this.apiUrl.rooms }/~${ shortId }`;
return this.http.get<Room>(connectionUrl).pipe(
tap(room => this.setRoomId(room)),
catchError(() => {
......@@ -107,11 +113,11 @@ export class RoomService extends BaseHttpService {
addToHistory(roomId: string): void {
const connectionUrl = `${ this.apiUrl.base + this.apiUrl.user }/${ this.authService.getUser().id }/roomHistory`;
this.http.post(connectionUrl, { roomId, lastVisit: this.joinDate.getTime() }, httpOptions).subscribe();
this.http.post(connectionUrl, {roomId, lastVisit: this.joinDate.getTime()}, httpOptions).subscribe();
}
removeFromHistory(roomId: string): Observable<Room> {
const connectionUrl = `${ this.apiUrl.base + this.apiUrl.user }/${ this.authService.getUser().id }/roomHistory/${roomId}`;
const connectionUrl = `${ this.apiUrl.base + this.apiUrl.user }/${ this.authService.getUser().id }/roomHistory/${ roomId }`;
return this.http.delete<Room>(connectionUrl, httpOptions).pipe(
tap(() => ''),
catchError(this.handleError<Room>('deleteRoom'))
......@@ -120,7 +126,7 @@ export class RoomService extends BaseHttpService {
updateRoom(updatedRoom: Room): Observable<Room> {
const connectionUrl = `${ this.apiUrl.base + this.apiUrl.rooms }/~${ updatedRoom.shortId }`;
return this.http.put(connectionUrl, updatedRoom , httpOptions).pipe(
return this.http.put(connectionUrl, updatedRoom, httpOptions).pipe(
tap(() => ''),
catchError(this.handleError<any>('updateRoom'))
);
......@@ -137,4 +143,17 @@ export class RoomService extends BaseHttpService {
setRoomId(room: Room): void {
localStorage.setItem('roomId', room.id);
}
handleRoomError<T>(operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
console.error(error);
if (error.status === 404) {
this.translateService.get('room-list.room-not-exist').subscribe(msg => {
this.notificationService.show(msg);
this.router.navigateByUrl('');
});
}
return throwError(error);
};
}
}
......@@ -283,7 +283,8 @@
"token": "Bonus-Token",
"token-time": "Token-Zeitstempel",
"no-comments": "Es sind noch keine Fragen vorhanden.",
"export-comments": "Fragen speichern"
"export-comments": "Fragen speichern",
"room-not-exist": "Es tut uns leid, aber die Sitzung, der Sie beitreten möchten, existiert nicht.\nBitte informieren Sie die Person, die Ihnen diesen Link gegeben hat."
},
"session": {
"create-session": "Speichern",
......
......@@ -285,7 +285,8 @@
"token": "Bonus token",
"token-time": "Token timestamp",
"no-comments": "There are no questions yet.",
"export-comments": "Save questions"
"export-comments": "Save questions",
"room-not-exist": "We are sorry, but the session you want to join does not exist.\nPlease inform the person who gave you this direct link."
},
"session": {
"create-session": "Save",
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment