404 lines
12 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, Inject, ViewChild, OnDestroy } from '@angular/core';
2019-11-08 13:35:39 +09:00
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
2019-12-13 14:38:42 +09:00
import { KEY_LOGIN_RES_INFO, KEY_VER_INFO } from '@app/types';
2019-11-08 13:35:39 +09:00
import { SessionStorageService } from '@ucap-webmessenger/web-storage';
import { Store, select } from '@ngrx/store';
import * as AppStore from '@app/store';
import * as ChatStore from '@app/store/messenger/chat';
import * as SyncStore from '@app/store/messenger/sync';
2019-11-29 18:24:51 +09:00
import * as AuthenticationStore from '@app/store/account/authentication';
2019-11-08 13:35:39 +09:00
import { UserInfo, GroupDetailData } from '@ucap-webmessenger/protocol-sync';
2019-11-08 13:35:39 +09:00
import {
UserInfoSS,
UserInfoF,
UserInfoDN
} from '@ucap-webmessenger/protocol-query';
2019-11-29 18:24:51 +09:00
import {
DialogService,
ConfirmDialogComponent,
ConfirmDialogData,
ConfirmDialogResult,
SnackBarService,
AlertDialogComponent,
AlertDialogResult,
AlertDialogData
2019-11-29 18:24:51 +09:00
} from '@ucap-webmessenger/ui';
2019-11-08 13:35:39 +09:00
import { VersionInfo2Response } from '@ucap-webmessenger/api-public';
import { LoginResponse } from '@ucap-webmessenger/protocol-authentication';
2019-12-17 11:44:59 +09:00
import { map, take, finalize, catchError } from 'rxjs/operators';
import { Subscription, of } from 'rxjs';
2019-11-29 18:24:51 +09:00
import {
SelectGroupDialogComponent,
SelectGroupDialogData,
SelectGroupDialogResult
} from '../group/select-group.dialog.component';
2019-12-03 18:59:11 +09:00
import { FileUploadItem } from '@ucap-webmessenger/api';
import { CommonApiService } from '@ucap-webmessenger/api-common';
import { OpenProfileOptions } from '@ucap-webmessenger/protocol-buddy';
2019-11-29 18:24:51 +09:00
import { EnvironmentsInfo, KEY_ENVIRONMENTS_INFO } from '@app/types';
import { StatusCode } from '@ucap-webmessenger/api';
2019-12-11 08:18:32 +09:00
import {
MessageWriteDialogComponent,
MessageWriteDialogResult,
MessageWriteDialogData
} from '../message/message-write.dialog.component';
import {
CallService,
PromptMessageStatusCode
} from '@ucap-webmessenger/api-prompt';
import { NGXLogger } from 'ngx-logger';
2019-12-18 09:56:00 +09:00
import { SmsUtils } from '@ucap-webmessenger/daesang';
import { NativeService, UCAP_NATIVE_SERVICE } from '@ucap-webmessenger/native';
import { environment } from '../../../../../environments/environment';
2020-01-08 12:30:39 +09:00
import { TranslateService } from '@ngx-translate/core';
2019-11-08 13:35:39 +09:00
export interface ProfileDialogData {
userInfo: UserInfo | UserInfoSS | UserInfoF | UserInfoDN;
openProfileOptions?: OpenProfileOptions;
2019-11-08 13:35:39 +09:00
}
export interface ProfileDialogResult {}
@Component({
selector: 'app-profile.dialog',
templateUrl: './profile.dialog.component.html',
styleUrls: ['./profile.dialog.component.scss']
})
export class ProfileDialogComponent implements OnInit, OnDestroy {
2019-11-29 18:24:51 +09:00
userInfo: UserInfo | UserInfoSS | UserInfoF | UserInfoDN;
2019-11-08 13:35:39 +09:00
loginRes: LoginResponse;
sessionVerinfo: VersionInfo2Response;
2019-11-29 18:24:51 +09:00
environmentsInfo: EnvironmentsInfo;
2019-11-08 13:35:39 +09:00
isMe: boolean;
isBuddy: boolean;
isFavorit: boolean;
editableProfileImage: boolean;
2019-11-08 13:35:39 +09:00
selectAllBuddy2Subscription: Subscription;
2019-11-08 13:35:39 +09:00
constructor(
public dialogRef: MatDialogRef<ProfileDialogData, ProfileDialogResult>,
2019-12-18 09:56:00 +09:00
@Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService,
2019-11-08 13:35:39 +09:00
@Inject(MAT_DIALOG_DATA) public data: ProfileDialogData,
private dialogService: DialogService,
private sessionStorageService: SessionStorageService,
2019-11-29 18:24:51 +09:00
private commonApiService: CommonApiService,
private callService: CallService,
2019-11-29 18:24:51 +09:00
private snackBarService: SnackBarService,
2020-01-08 12:30:39 +09:00
private translateService: TranslateService,
private store: Store<any>,
private logger: NGXLogger
2019-11-08 13:35:39 +09:00
) {
this.sessionVerinfo = this.sessionStorageService.get<VersionInfo2Response>(
KEY_VER_INFO
);
this.loginRes = this.sessionStorageService.get<LoginResponse>(
KEY_LOGIN_RES_INFO
);
2019-11-29 18:24:51 +09:00
this.environmentsInfo = this.sessionStorageService.get<EnvironmentsInfo>(
KEY_ENVIRONMENTS_INFO
);
this.userInfo = data.userInfo;
this.editableProfileImage =
environment.productConfig.CommonSetting.editableProfileImage;
2019-11-08 13:35:39 +09:00
}
ngOnInit() {
this.isMe = this.loginRes.userSeq === this.data.userInfo.seq;
this.selectAllBuddy2Subscription = this.store
.pipe(
select(AppStore.MessengerSelector.SyncSelector.selectAllBuddy2),
map(buddyList => {
const users = buddyList.filter(
buddy => buddy.seq === this.data.userInfo.seq
);
this.isBuddy = users.length > 0;
if (this.isBuddy) {
this.isFavorit = users[0].isFavorit;
}
})
)
.subscribe();
}
ngOnDestroy(): void {
if (!!this.selectAllBuddy2Subscription) {
this.selectAllBuddy2Subscription.unsubscribe();
}
2019-11-08 13:35:39 +09:00
}
onClickChat(userInfo: UserInfoSS) {
2019-11-08 13:35:39 +09:00
if (userInfo.seq === this.loginRes.userSeq) {
this.store.dispatch(
ChatStore.openRoom({ userSeqList: [this.loginRes.talkWithMeBotSeq] })
);
} else {
this.store.dispatch(ChatStore.openRoom({ userSeqList: [userInfo.seq] }));
}
this.dialogRef.close();
}
onClickSendMessage(userInfo: UserInfoSS) {
2019-12-11 08:18:32 +09:00
if (userInfo.seq !== this.loginRes.userSeq) {
this.dialogRef.close();
this.dialogService.open<
MessageWriteDialogComponent,
MessageWriteDialogData,
MessageWriteDialogResult
>(MessageWriteDialogComponent, {
width: '600px',
height: '600px',
disableClose: true,
hasBackdrop: false,
data: {
loginRes: this.loginRes,
environmentsInfo: this.environmentsInfo,
receiverList: [userInfo]
}
});
}
}
async onClickSendClickToCall(calleeNumber: string) {
const madn = this.loginRes.madn;
if (!madn || madn.trim().length === 0) {
this.dialogService.open<
AlertDialogComponent,
AlertDialogData,
AlertDialogResult
>(AlertDialogComponent, {
data: {
2020-01-08 12:30:39 +09:00
title: this.translateService.instant('call.errors.label'),
html: this.translateService.instant('call.errors.cannotCallToUser')
}
});
2020-01-08 12:30:39 +09:00
return false;
}
calleeNumber = calleeNumber.replace(/\D/g, '');
if (!!calleeNumber && calleeNumber.length > 0) {
const result = await this.dialogService.open<
ConfirmDialogComponent,
ConfirmDialogData,
ConfirmDialogResult
>(ConfirmDialogComponent, {
width: '360px',
data: {
2020-01-08 12:30:39 +09:00
title: this.translateService.instant('call.callTo'),
html: this.translateService.instant('call.callWithNumber', {
phoneNumber: calleeNumber
})
}
});
if (!!result && !!result.choice && result.choice) {
this.callService
.sendCall({
userSeq: this.loginRes.userSeq,
deviceType: this.environmentsInfo.deviceType,
tokenKey: this.loginRes.tokenString,
calleeNumber
})
.pipe(
take(1),
map(res => {
if (res.responseCode === PromptMessageStatusCode.Success) {
this.logger.debug('SUCCESS');
this.logger.debug(res);
} else {
this.logger.error(res);
}
2019-12-17 11:44:59 +09:00
}),
catchError(error => of(this.logger.debug(error)))
)
.subscribe();
}
} else {
this.dialogService.open<
AlertDialogComponent,
AlertDialogData,
AlertDialogResult
>(AlertDialogComponent, {
data: {
2020-01-08 12:30:39 +09:00
title: this.translateService.instant('call.errors.label'),
html: this.translateService.instant(
'call.errors.cannotCallToUserWithoutPhomeNumber'
)
}
});
}
}
2019-12-18 09:56:00 +09:00
onClickSendSms(calleeNumber: string) {
const smsUtil = new SmsUtils(
this.sessionStorageService,
this.nativeService
);
if (!smsUtil.getAuthSms()) {
this.dialogService.open<
AlertDialogComponent,
AlertDialogData,
AlertDialogResult
>(AlertDialogComponent, {
data: {
2020-01-08 12:30:39 +09:00
title: this.translateService.instant('sms.errors.label'),
html: this.translateService.instant('sms.errors.haveNoPermission')
2019-12-18 09:56:00 +09:00
}
});
return false;
}
calleeNumber = calleeNumber.replace(/\D/g, '');
if (!!calleeNumber && calleeNumber.length > 0) {
smsUtil.openSendSms(this.loginRes.tokenString, [calleeNumber]);
} else {
// this.dialogService.open<
// AlertDialogComponent,
// AlertDialogData,
// AlertDialogResult
// >(AlertDialogComponent, {
// data: {
// title: '',
// html: `상대방 번호가 없어 전화를 걸 수 없습니다.`
// }
// });
}
}
onClickToggleFavorit(param: { userInfo: UserInfoSS; isFavorit: boolean }) {
this.store.dispatch(
SyncStore.updateBuddy({
seq: param.userInfo.seq,
isFavorit: param.isFavorit
})
);
}
async onClickToggleBuddy(param: { userInfo: UserInfoSS; isBuddy: boolean }) {
if (param.isBuddy) {
// 동료추가.
const result = await this.dialogService.open<
SelectGroupDialogComponent,
SelectGroupDialogData,
SelectGroupDialogResult
>(SelectGroupDialogComponent, {
width: '600px',
data: {
2020-01-08 12:30:39 +09:00
title: this.translateService.instant('group.selectTargetGroup')
}
});
if (!!result && !!result.choice && result.choice) {
if (!!result.group) {
const oldGroup: GroupDetailData = result.group;
const trgtUserSeq: number[] = [];
result.group.userSeqs.map(seq => trgtUserSeq.push(seq));
trgtUserSeq.push(param.userInfo.seq);
this.store.dispatch(
SyncStore.updateGroupMember({
oldGroup,
trgtUserSeq
})
);
}
}
} else {
// 동료삭제.
const result = await this.dialogService.open<
ConfirmDialogComponent,
ConfirmDialogData,
ConfirmDialogResult
>(ConfirmDialogComponent, {
width: '360px',
data: {
2020-01-08 12:30:39 +09:00
title: this.translateService.instant('group.removeBuddyFromGroup'),
html: this.translateService.instant(
'group.confirmRemoveBuddyFromGroup',
{ target: `${param.userInfo.name} ${param.userInfo.grade}` }
)
}
});
if (!!result && !!result.choice && result.choice) {
this.store.dispatch(
SyncStore.delBuddyAndClear({ seq: param.userInfo.seq })
);
2019-11-29 18:24:51 +09:00
this.isBuddy = false;
}
}
}
2019-11-29 18:24:51 +09:00
onUploadProfileImage(profileImageFileUploadItem: FileUploadItem) {
this.commonApiService
.fileProfileSave(
{
userSeq: this.loginRes.userSeq,
deviceType: this.environmentsInfo.deviceType,
token: this.loginRes.tokenString,
file: profileImageFileUploadItem.file,
fileUploadItem: profileImageFileUploadItem
},
this.sessionVerinfo.profileUploadUrl
)
.pipe(
take(1),
map(res => {
if (!res) {
return;
}
if (StatusCode.Success === res.statusCode) {
return res;
} else {
throw res;
}
}),
finalize(() => {
setTimeout(() => {
profileImageFileUploadItem.uploadingProgress$ = undefined;
}, 1000);
})
)
.subscribe(
res => {
const userInfo = {
...this.loginRes.userInfo,
profileImageFile: res.profileSubDir
};
this.store.dispatch(
AuthenticationStore.updateLoginRes({
loginRes: {
...this.loginRes,
userInfo
}
})
);
this.userInfo = userInfo as any;
},
error => {
this.snackBarService.open(
2020-01-08 12:30:39 +09:00
this.translateService.instant(
'profile.errors.failToChangeProfileImage'
),
2019-11-29 18:24:51 +09:00
'',
{
duration: 3000,
verticalPosition: 'bottom'
}
);
}
);
}
2019-11-08 13:35:39 +09:00
}