Updated DmComponent (partially) to V3

This commit is contained in:
Noel Maróti
2026-11-29 18:25:06 +01:00
parent 7149ebad10
commit 8fc0da520c
2 changed files with 93 additions and 74 deletions

View File

@@ -1,15 +1,14 @@
<main [class.mobile]="breakpoint() == 'mobile'">
@defer (when handler) {
@if (chatData != null) {
@defer (when store) {
<navbar backButtonDest="/chat">
<div class="data">
<avatar [src]="chatData.pfp" height="50px" width="50px" [radius]="15" [name]="chatData.username"></avatar>
<avatar [src]="this.store.chatData().pfp" height="50px" width="50px" [radius]="15" [name]="this.store.chatData().username"></avatar>
<div class="chat-data">
@if (chatData.displayName == "") {
<span class="main-name">{{ '@' + chatData.username }}</span>
@if (this.store.chatData().displayName == "") {
<span class="main-name">{{ '@' + this.store.chatData().username }}</span>
} @else {
<span class="main-name">{{ chatData.displayName }}</span>
<span class="alt-name">{{ '@' + chatData.username }}</span>
<span class="main-name">{{ this.store.chatData().displayName }}</span>
<span class="alt-name">{{ '@' + this.store.chatData().username }}</span>
}
</div>
</div>
@@ -21,12 +20,11 @@
</div>
</navbar>
<!--<messages (scrollend)="handleMessagesScroll($event)" [loading]="store.messagesStatus() == LoadStatus.loading"
<messages (scrollend)="handleMessagesScroll($event)" [loading]="store.messagesState() == LoadState.CLEAN_LOADING"
[messageBoxViewModel]="store.messageBox" [messages]="store.messages()" id="scrollContainer"
(onDelete)="deleteMessage($event)" />
<message-box [viewModel]="store.messageBox" />-->
}
<message-box [viewModel]="store.messageBox" />
}
</main>

View File

@@ -1,29 +1,27 @@
import {Component, inject, OnInit, signal} from '@angular/core';
import {DmStorage, LoadStatus, 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 {TUI_BREAKPOINT, 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 {FileUploadProgressListener} from '@chatenium/chatenium-sdk/domain/fileUploadService.schema';
import {Attachment} from '@chatenium/chatenium-sdk/domain/common.schema';
import {MessagesViewModel} from '../elements/messages/messages-viewmodel';
import {v4 as uuidv4} from 'uuid';
import { Component, inject, OnInit, signal } from '@angular/core';
import { LoadStatus, ServiceManager } from '../../service-manager';
import { ActivatedRoute } from '@angular/router';
import { IndexedDB } from '../../storage/indexed-db';
import { Navbar } from '../elements/navbar/navbar';
import { TUI_BREAKPOINT, TuiButton, TuiIcon, TuiNotificationService } from '@taiga-ui/core';
import { FileDataWithPreview, MessageBox } from '../elements/message-box/message-box';
import { CHATENIUM_SDK } from '../../chatenium.token';
import { Contact, createDMWrapper, DirectMessagingApi, WrappedDMHandler } from 'chatenium-sdk-preview';
import { CacheService } from '../../cache';
import {
Contact,
createDMWrapper,
DirectMessagingApi,
Message,
WrappedDMHandler,
} from 'chatenium-sdk-preview';
import { CacheService, DmStorage, LoadState } from '../../cache';
import { Avatar } from '../elements/avatar/avatar';
import { JsonPipe } from '@angular/common';
import { MessageBoxViewModel } from '../elements/message-box/message-box-viewmodel';
import { MessagesViewModel } from '../elements/messages/messages-viewmodel';
import { Messages } from '../elements/messages/messages';
@Component({
selector: 'app-dm',
imports: [Navbar, Oimg, TuiButton, TuiIcon, MessageBox, Messages, Avatar, JsonPipe],
imports: [Navbar, TuiButton, TuiIcon, MessageBox, Avatar, Messages],
templateUrl: './dm.html',
styleUrl: './dm.scss',
})
@@ -32,19 +30,29 @@ export class Dm implements OnInit {
cache = inject(CacheService);
serviceManager = inject(ServiceManager);
route = inject(ActivatedRoute);
notification = inject(TuiNotificationService);
indexedDb = inject(IndexedDB);
breakpoint = inject(TUI_BREAKPOINT);
handler: WrappedDMHandler<DirectMessagingApi> | null = null;
chatData: Contact | null = null;
chatid = '';
contactId = '';
get store(): DmStorage {
return this.serviceManager.dmServices()[this.chatid];
return this.cache.dms()[this.contactId];
}
async sendMessage(message: string, files: FileDataWithPreview[] | null) {
console.log('sendMessage', message, files);
try {
const resp = await this.store.handler.sendMessage({
content: message,
});
const data = resp.data as Message;
console.log(data);
} catch (e) {
this.notification.open(`Failed sending DM: ${e}`, { appearance: 'negative' }).subscribe();
console.error(e);
}
/*
if (!files && message.trim() == '') return;
const session = this.serviceManager.currentSession();
@@ -121,11 +129,11 @@ export class Dm implements OnInit {
);
this.updateTempMessage(tempMsgId, respMessage);
this.scrollToBottom('smooth');
}
}*/
}
async deleteMessage(messageId: string) {
const i = this.store.messages().findIndex((m) => m.msgid == messageId);
/*const i = this.store.messages().findIndex((m) => m.msgid == messageId);
if (i != -1) {
const originalMessage: Message = JSON.parse(JSON.stringify(this.store.messages()[i]));
this.store.messages().splice(i, 1);
@@ -134,6 +142,23 @@ export class Dm implements OnInit {
} catch (e) {
this.store.messages().splice(i, 0, originalMessage);
}
}*/
}
async loadMessages() {
try {
const resp = await this.store.handler.getMessages(0);
const data = resp.data as Message[];
console.log(
`Successfully fetched ${data.length} message(s) in DM ${this.store.chatData().contactId}`,
);
console.log(data);
this.store.messages.set(data);
this.store.messagesState.set(LoadState.LOADED);
} catch (e) {
this.notification
.open('Loading direct messages failed.', { appearance: 'negative' })
.subscribe();
}
}
@@ -171,6 +196,7 @@ export class Dm implements OnInit {
}
uploadProgressUpdate(tempMsgId: string, fileId: string, allChunks: number, chunksDone: number) {
/*
console.log(tempMsgId, fileId, allChunks, chunksDone);
this.store.messages.update((messages) =>
messages.map((m) => {
@@ -191,10 +217,11 @@ export class Dm implements OnInit {
}),
};
}),
);
);*/
}
updateTempMessage(tempMsgId: string, message: Message) {
/*
this.store.messages.update((messages) =>
messages.map((m) => {
if (m.msgid !== tempMsgId) return m;
@@ -209,11 +236,12 @@ export class Dm implements OnInit {
}),
};
}),
);
);*/
}
// The chatid parameter ensures isolation
onWsListen(action: string, message: string, chatid: string) {
/*
const data = JSON.parse(message);
if (data.chatid === chatid) {
const targetStore = this.serviceManager.dmServices()[chatid];
@@ -225,13 +253,13 @@ export class Dm implements OnInit {
break;
}
}
}
}*/
}
ngOnInit() {
this.route.params.subscribe(async (params) => {
const contactId = params['contactId'];
this.chatid = contactId;
this.contactId = contactId;
console.log(`Loading DM chat ${contactId}...`);
const chatData = this.cache.contacts().find((c) => c.contactId === contactId);
@@ -243,30 +271,20 @@ export class Dm implements OnInit {
return;
}
this.chatData = chatData;
this.handler = createDMWrapper(this.SDK.directMessaging, chatData.contactId);
console.log(`Finished initializing chat: ${contactId}`);
/*if (!this.serviceManager.dmServices()[contactId]) {
const newStore = {
chatData: signal<Chat>(chatData),
if (!this.cache.dms()[contactId]) {
console.log(`No store yet, creating service store for DM: ${contactId}`);
this.cache.dms()[contactId] = {
handler: createDMWrapper(this.SDK.directMessaging, chatData.contactId),
chatData: signal<Contact>(chatData),
messages: signal<Message[]>([]),
messagesStatus: signal<LoadStatus>(LoadStatus.loading),
messagesState: signal<LoadState>(LoadState.CLEAN_LOADING),
messageBox: new MessageBoxViewModel((msg, files) => this.sendMessage(msg, files)),
messagesVm: new MessagesViewModel(),
wsListener: (action, data) => this.onWsListen(action, data, contactId),
} as DmStorage;
newStore.service = new DMService(
session.userData.userid,
session.token,
contactId,
this.indexedDb.getApi(),
(action, data) => newStore.wsListener(action, data)
);
this.serviceManager.dmServices()[contactId] = newStore;
await this.loadMessages();
/*
const currentStore = this.serviceManager.dmServices()[contactId];
try {
const messagesCache = await currentStore.service.getQuick();
@@ -285,8 +303,11 @@ export class Dm implements OnInit {
} else {
this.restoreScrollState()
}*/
}
console.log(`Finished initializing chat: ${contactId}`);
});
}
protected readonly LoadStatus = LoadStatus;
protected readonly LoadState = LoadState;
}