128 lines
4.1 KiB
TypeScript
128 lines
4.1 KiB
TypeScript
import {Component, inject, OnInit, signal} from '@angular/core';
|
|
import {DmStorage, ServiceManager} from '../../service-manager';
|
|
import {ActivatedRoute} from '@angular/router';
|
|
import {DMService} from '@chatenium/chatenium-sdk/services/dmService';
|
|
import {IndexedDB} from '../../storage/indexed-db';
|
|
import {Navbar} from '../elements/navbar/navbar';
|
|
import {Oimg} from '../elements/oimg/oimg';
|
|
import {TuiButton, TuiIcon} from '@taiga-ui/core';
|
|
import {FileDataWithPreview, MessageBox} from '../elements/message-box/message-box';
|
|
import {Messages} from '../elements/messages/messages';
|
|
import {Chat} from '@chatenium/chatenium-sdk/domain/chatService.schema';
|
|
import {Message} from '@chatenium/chatenium-sdk/domain/dmService.schema';
|
|
import {MessageBoxViewModel} from '../elements/message-box/message-box-viewmodel';
|
|
import {WebSocketHandler} from '@chatenium/chatenium-sdk/core/webSocketHandler';
|
|
import {FileData, FileUploadProgressListener} from '@chatenium/chatenium-sdk/domain/fileUploadService.schema';
|
|
import {Attachment} from '@chatenium/chatenium-sdk/domain/common.schema';
|
|
|
|
@Component({
|
|
selector: 'app-dm',
|
|
imports: [
|
|
Navbar,
|
|
Oimg,
|
|
TuiButton,
|
|
TuiIcon,
|
|
MessageBox,
|
|
Messages
|
|
],
|
|
templateUrl: './dm.html',
|
|
styleUrl: './dm.scss',
|
|
})
|
|
export class Dm implements OnInit {
|
|
serviceManager = inject(ServiceManager)
|
|
route = inject(ActivatedRoute)
|
|
indexedDb = inject(IndexedDB)
|
|
|
|
chatid = ""
|
|
|
|
get store(): DmStorage {
|
|
return this.serviceManager.dmServices()[this.chatid]
|
|
}
|
|
|
|
async sendMessage(message: string, files: FileDataWithPreview[] | null) {
|
|
const session = this.serviceManager.currentSession();
|
|
if (session != null) {
|
|
await this.store.service.sendMessage(message, null, null, files, <FileUploadProgressListener>{
|
|
fileProgressUpdate: (fileId, allChunks, chunksDone) => {
|
|
this.uploadProgressUpdate(fileId, allChunks, chunksDone)
|
|
}
|
|
})
|
|
|
|
let attachments: Attachment[] = []
|
|
files?.forEach(file => {
|
|
attachments.push({
|
|
fileName: file.name,
|
|
fileId: file.fileId,
|
|
type: file.type,
|
|
format: file.extension,
|
|
path: file.preview,
|
|
height: file.height,
|
|
width: file.width,
|
|
})
|
|
})
|
|
|
|
this.store.messages.update(value => [...value, {
|
|
message: message,
|
|
chatid: this.chatid,
|
|
files: attachments,
|
|
replyTo: "",
|
|
author: session.userData.userid,
|
|
seen: false,
|
|
msgid: "",
|
|
forwardedFrom: "",
|
|
isEdited: false,
|
|
sent_at: {T: 0, I: 0},
|
|
replyToId: "",
|
|
forwardedFromName: ""
|
|
}])
|
|
}
|
|
}
|
|
|
|
uploadProgressUpdate(fileId: string, allChunks: number, chunksDone: number) {
|
|
console.log(fileId, allChunks, chunksDone)
|
|
}
|
|
|
|
onWsListen(action: string, message: string) {
|
|
console.log(action, message)
|
|
switch (action) {
|
|
case "newMessage": {
|
|
this.store.messages.update(messages => [...messages, JSON.parse(message)])
|
|
}
|
|
}
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.params.subscribe(async params => {
|
|
const chatid = params['chatid']
|
|
this.chatid = chatid
|
|
console.log(this.serviceManager.chats())
|
|
const session = this.serviceManager.currentSession();
|
|
const chatData = this.serviceManager.chats().find(chat => chat.chatid == chatid)
|
|
|
|
// Setup storage
|
|
if (!this.serviceManager.dmServices()[chatid] && session != null && chatData != null) {
|
|
this.serviceManager.dmServices()[chatid] = {
|
|
service: new DMService(
|
|
session.userData.userid,
|
|
session.token,
|
|
chatid,
|
|
this.indexedDb.getApi(),
|
|
(action, data) => {
|
|
this.onWsListen(action, data)
|
|
}
|
|
),
|
|
chatData: signal<Chat>(chatData),
|
|
messages: signal<Message[]>([]),
|
|
messageBox: new MessageBoxViewModel(
|
|
(msg, files) => this.sendMessage(msg, files),
|
|
)
|
|
}
|
|
}
|
|
|
|
this.store.messages.set(await this.serviceManager.dmServices()[chatid].service.get())
|
|
console.log(WebSocketHandler.getInstance().connId)
|
|
await this.store.service.joinWebSocketRoom()
|
|
})
|
|
}
|
|
}
|