320 lines
8.0 KiB
TypeScript
Raw Normal View History

2019-09-18 15:02:21 +09:00
import { Injectable } from '@angular/core';
2019-10-10 10:43:06 +09:00
import { Observable, Subject } from 'rxjs';
import { map, take, takeWhile, share, tap, filter } from 'rxjs/operators';
2019-09-23 14:38:22 +09:00
import { ProtocolService } from '@ucap-webmessenger/protocol';
import {
SVC_TYPE_ROOM,
SSVC_TYPE_ROOM_OPEN_REQ,
SSVC_TYPE_ROOM_OPEN2_REQ,
SSVC_TYPE_ROOM_OPEN3_REQ,
2019-09-23 15:18:17 +09:00
SSVC_TYPE_ROOM_OPEN4_REQ,
2019-09-23 17:00:05 +09:00
SSVC_TYPE_ROOM_INVITE_REQ,
SSVC_TYPE_ROOM_INFO_ROOM,
SSVC_TYPE_ROOM_INFO_RES,
SSVC_TYPE_ROOM_INFO_REQ,
SSVC_TYPE_ROOM_INFO_USER,
SSVC_TYPE_ROOM_INFO_USER2,
SSVC_TYPE_ROOM_EXIT_REQ,
SSVC_TYPE_ROOM_EXIT_ALL_REQ,
SSVC_TYPE_ROOM_EXIT_FORCING_REQ,
SSVC_TYPE_ROOM_USER_STATUS_REQ,
SSVC_TYPE_ROOM_UPD_REQ,
SSVC_TYPE_ROOM_INFO_UPD_REQ,
2019-10-10 10:43:06 +09:00
SSVC_TYPE_ROOM_FONT_UPD_REQ,
SSVC_TYPE_ROOM_INVITE_NOTI,
SSVC_TYPE_ROOM_EXIT_NOTI,
SSVC_TYPE_ROOM_EXIT_FORCING_NOTI,
SSVC_TYPE_ROOM_FONT_UPD_NOTI
2019-09-23 14:38:22 +09:00
} from '../types/service';
import {
2019-09-25 16:13:33 +09:00
OpenRequest,
encodeOpen,
decodeOpen,
OpenResponse,
Open2Request,
Open2Response,
decodeOpen2,
encodeOpen2,
Open3Request,
Open3Response,
encodeOpen3,
decodeOpen3,
Open4Request,
Open4Response,
encodeOpen4,
decodeOpen4
2019-10-07 10:08:14 +09:00
} from '../protocols/open';
2019-09-23 15:18:17 +09:00
import {
2019-09-25 16:13:33 +09:00
InviteRequest,
InviteResponse,
encodeInvite,
2019-10-10 10:43:06 +09:00
decodeInvite,
decodeInviteNotification,
InviteNotification
2019-10-07 10:08:14 +09:00
} from '../protocols/invite';
2019-09-23 17:00:05 +09:00
import {
2019-09-25 16:13:33 +09:00
InfoRequest,
InfoResponse,
InfoData,
UserShortData,
UserData,
encodeInfo,
decodeInfoData,
decodeInfo,
decodeUserShortData,
decodeUserData,
UserStatusOfflineRequest,
UserStatusOfflineResponse,
encodeUserStatusOffline,
decodeUserStatusOffline
2019-10-07 10:08:14 +09:00
} from '../protocols/info';
import {
2019-09-25 16:13:33 +09:00
ExitRequest,
ExitResponse,
encodeExit,
decodeExit,
ExitAllRequest,
encodeAllExit,
ExitForcingRequest,
ExitForcingResponse,
encodeExitForcing,
2019-09-25 17:33:44 +09:00
decodeExitForcing,
decodeAllExit,
2019-10-10 10:43:06 +09:00
ExitAllResponse,
decodeExitForcingNotification,
decodeExitNotification,
ExitNotification,
ExitForcingNotification
2019-10-07 10:08:14 +09:00
} from '../protocols/exit';
import {
2019-09-25 16:13:33 +09:00
UpdateRequest,
UpdateResponse,
encodeUpdate,
decodeUpdate,
UpdateTimerSetRequest,
UpdateTimerSetResponse,
encodeUpdateTimerSet,
decodeUpdateTimerSet,
UpdateFontRequest,
UpdateFontResponse,
encodeUpdateFont,
2019-10-10 10:43:06 +09:00
decodeUpdateFont,
decodeUpdateFontNotification,
UpdateFontNotification
2019-10-07 10:08:14 +09:00
} from '../protocols/update';
2019-09-23 14:38:22 +09:00
2019-10-10 10:43:06 +09:00
type Notifications =
| UpdateFontNotification
| InviteNotification
| ExitNotification
| ExitForcingNotification;
2019-09-18 15:02:21 +09:00
@Injectable({
providedIn: 'root'
})
export class RoomProtocolService {
2019-10-10 10:43:06 +09:00
private notificationSubject: Subject<Notifications>;
public notification$: Observable<Notifications>;
constructor(private protocolService: ProtocolService) {
this.notificationSubject = new Subject();
this.notification$ = this.notificationSubject.asObservable().pipe(share());
this.protocolService.serverMessage
.pipe(
filter(message => message.serviceType === SVC_TYPE_ROOM),
tap(message => {
switch (message.subServiceType) {
case SSVC_TYPE_ROOM_INVITE_NOTI:
{
2019-10-10 12:14:01 +09:00
this.notificationSubject.next(
decodeInviteNotification(message)
);
2019-10-10 10:43:06 +09:00
}
break;
case SSVC_TYPE_ROOM_EXIT_NOTI:
{
2019-10-10 12:14:01 +09:00
this.notificationSubject.next(decodeExitNotification(message));
2019-10-10 10:43:06 +09:00
}
break;
case SSVC_TYPE_ROOM_EXIT_FORCING_NOTI:
{
2019-10-10 12:14:01 +09:00
this.notificationSubject.next(
decodeExitForcingNotification(message)
);
2019-10-10 10:43:06 +09:00
}
break;
case SSVC_TYPE_ROOM_FONT_UPD_NOTI:
{
2019-10-10 12:14:01 +09:00
this.notificationSubject.next(
decodeUpdateFontNotification(message)
);
2019-10-10 10:43:06 +09:00
}
break;
default:
break;
}
})
)
.subscribe();
}
2019-09-23 14:38:22 +09:00
2019-09-25 16:13:33 +09:00
public open(req: OpenRequest): Observable<OpenResponse> {
2019-09-23 14:38:22 +09:00
return this.protocolService
2019-09-25 16:13:33 +09:00
.call(SVC_TYPE_ROOM, SSVC_TYPE_ROOM_OPEN_REQ, ...encodeOpen(req))
2019-09-23 15:17:48 +09:00
.pipe(
take(1),
2019-09-25 16:13:33 +09:00
map(res => decodeOpen(res))
2019-09-23 15:17:48 +09:00
);
2019-09-23 14:38:22 +09:00
}
2019-09-25 16:13:33 +09:00
public open2(req: Open2Request): Observable<Open2Response> {
2019-09-23 14:38:22 +09:00
return this.protocolService
2019-09-25 16:13:33 +09:00
.call(SVC_TYPE_ROOM, SSVC_TYPE_ROOM_OPEN2_REQ, ...encodeOpen2(req))
2019-09-23 15:17:48 +09:00
.pipe(
take(1),
2019-09-25 16:13:33 +09:00
map(res => decodeOpen2(res))
2019-09-23 15:17:48 +09:00
);
2019-09-23 14:38:22 +09:00
}
2019-09-25 16:13:33 +09:00
public open3(req: Open3Request): Observable<Open3Response> {
2019-09-23 14:38:22 +09:00
return this.protocolService
2019-09-25 16:13:33 +09:00
.call(SVC_TYPE_ROOM, SSVC_TYPE_ROOM_OPEN3_REQ, ...encodeOpen3(req))
2019-09-23 15:17:48 +09:00
.pipe(
take(1),
2019-09-25 16:13:33 +09:00
map(res => decodeOpen3(res))
2019-09-23 15:17:48 +09:00
);
2019-09-23 14:38:22 +09:00
}
2019-09-25 16:13:33 +09:00
public open4(req: Open4Request): Observable<Open4Response> {
2019-09-23 14:38:22 +09:00
return this.protocolService
2019-09-25 16:13:33 +09:00
.call(SVC_TYPE_ROOM, SSVC_TYPE_ROOM_OPEN4_REQ, ...encodeOpen4(req))
2019-09-23 15:17:48 +09:00
.pipe(
take(1),
2019-09-25 16:13:33 +09:00
map(res => decodeOpen4(res))
2019-09-23 15:17:48 +09:00
);
2019-09-23 14:38:22 +09:00
}
2019-09-23 15:18:17 +09:00
2019-09-25 16:13:33 +09:00
public invite(req: InviteRequest): Observable<InviteResponse> {
2019-09-23 15:18:17 +09:00
return this.protocolService
2019-09-25 16:13:33 +09:00
.call(SVC_TYPE_ROOM, SSVC_TYPE_ROOM_INVITE_REQ, ...encodeInvite(req))
2019-09-23 15:18:17 +09:00
.pipe(
take(1),
2019-09-25 16:13:33 +09:00
map(res => decodeInvite(res))
2019-09-23 15:18:17 +09:00
);
}
2019-09-23 17:00:05 +09:00
2019-09-25 16:13:33 +09:00
public info(
req: InfoRequest
): Observable<InfoResponse | InfoData | UserShortData | UserData> {
2019-09-23 17:00:05 +09:00
return this.protocolService
2019-09-25 16:13:33 +09:00
.call(SVC_TYPE_ROOM, SSVC_TYPE_ROOM_INFO_REQ, ...encodeInfo(req))
2019-09-23 17:00:05 +09:00
.pipe(
2019-09-25 15:35:06 +09:00
takeWhile(res => SSVC_TYPE_ROOM_INFO_RES !== res.subServiceType, true),
2019-09-23 17:00:05 +09:00
map(res => {
if (SSVC_TYPE_ROOM_INFO_ROOM === res.subServiceType) {
2019-10-10 12:14:01 +09:00
return decodeInfoData(res);
2019-09-23 17:00:05 +09:00
} else if (SSVC_TYPE_ROOM_INFO_USER === res.subServiceType) {
2019-10-10 12:14:01 +09:00
return decodeUserShortData(res);
2019-09-23 17:00:05 +09:00
} else if (SSVC_TYPE_ROOM_INFO_USER2 === res.subServiceType) {
2019-10-10 12:14:01 +09:00
return decodeUserData(res);
2019-09-23 17:00:05 +09:00
}
2019-10-10 12:14:01 +09:00
return decodeInfo(res);
2019-09-23 17:00:05 +09:00
})
);
}
2019-09-25 16:13:33 +09:00
public userStatusOffline(
req: UserStatusOfflineRequest
): Observable<UserStatusOfflineResponse> {
return this.protocolService
.call(
SVC_TYPE_ROOM,
SSVC_TYPE_ROOM_USER_STATUS_REQ,
2019-09-25 16:13:33 +09:00
...encodeUserStatusOffline(req)
)
.pipe(
2019-09-25 17:33:44 +09:00
take(1),
map(res => {
2019-09-25 16:13:33 +09:00
return decodeUserStatusOffline(res);
})
);
}
2019-09-25 16:13:33 +09:00
public exit(req: ExitRequest): Observable<ExitResponse> {
return this.protocolService
2019-09-25 16:13:33 +09:00
.call(SVC_TYPE_ROOM, SSVC_TYPE_ROOM_EXIT_REQ, ...encodeExit(req))
.pipe(
2019-09-25 17:33:44 +09:00
take(1),
map(res => {
2019-09-25 16:13:33 +09:00
return decodeExit(res);
})
);
}
2019-09-25 17:33:44 +09:00
public exitAll(req: ExitAllRequest): Observable<ExitAllResponse> {
return this.protocolService
2019-09-25 16:13:33 +09:00
.call(SVC_TYPE_ROOM, SSVC_TYPE_ROOM_EXIT_ALL_REQ, ...encodeAllExit(req))
.pipe(
2019-09-25 17:33:44 +09:00
take(1),
map(res => {
2019-09-25 17:33:44 +09:00
return decodeAllExit(res);
})
);
}
2019-09-25 16:13:33 +09:00
public exitForcing(req: ExitForcingRequest): Observable<ExitForcingResponse> {
return this.protocolService
.call(
SVC_TYPE_ROOM,
SSVC_TYPE_ROOM_EXIT_FORCING_REQ,
2019-09-25 16:13:33 +09:00
...encodeExitForcing(req)
)
.pipe(
2019-09-25 17:33:44 +09:00
take(1),
map(res => {
2019-09-25 16:13:33 +09:00
return decodeExitForcing(res);
})
);
}
2019-09-25 16:13:33 +09:00
public update(req: UpdateRequest): Observable<UpdateResponse> {
return this.protocolService
2019-09-25 16:13:33 +09:00
.call(SVC_TYPE_ROOM, SSVC_TYPE_ROOM_UPD_REQ, ...encodeUpdate(req))
.pipe(
2019-09-25 17:33:44 +09:00
take(1),
map(res => {
2019-09-25 16:13:33 +09:00
return decodeUpdate(res);
})
);
}
2019-09-25 16:13:33 +09:00
public updateTimerSet(
req: UpdateTimerSetRequest
): Observable<UpdateTimerSetResponse> {
return this.protocolService
.call(
SVC_TYPE_ROOM,
SSVC_TYPE_ROOM_INFO_UPD_REQ,
2019-09-25 16:13:33 +09:00
...encodeUpdateTimerSet(req)
)
.pipe(
2019-09-25 17:33:44 +09:00
take(1),
map(res => {
2019-09-25 16:13:33 +09:00
return decodeUpdateTimerSet(res);
})
);
}
2019-09-25 16:13:33 +09:00
public updateFont(req: UpdateFontRequest): Observable<UpdateFontResponse> {
return this.protocolService
.call(
SVC_TYPE_ROOM,
SSVC_TYPE_ROOM_FONT_UPD_REQ,
2019-09-25 16:13:33 +09:00
...encodeUpdateFont(req)
)
.pipe(
2019-09-25 17:33:44 +09:00
take(1),
map(res => {
2019-09-25 16:13:33 +09:00
return decodeUpdateFont(res);
})
);
}
2019-09-18 15:02:21 +09:00
}