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,32 +1,30 @@
<main [class.mobile]="breakpoint() == 'mobile'"> <main [class.mobile]="breakpoint() == 'mobile'">
@defer (when handler) { @defer (when store) {
@if (chatData != null) { <navbar backButtonDest="/chat">
<navbar backButtonDest="/chat"> <div class="data">
<div class="data"> <avatar [src]="this.store.chatData().pfp" height="50px" width="50px" [radius]="15" [name]="this.store.chatData().username"></avatar>
<avatar [src]="chatData.pfp" height="50px" width="50px" [radius]="15" [name]="chatData.username"></avatar> <div class="chat-data">
<div class="chat-data"> @if (this.store.chatData().displayName == "") {
@if (chatData.displayName == "") { <span class="main-name">{{ '@' + this.store.chatData().username }}</span>
<span class="main-name">{{ '@' + chatData.username }}</span> } @else {
} @else { <span class="main-name">{{ this.store.chatData().displayName }}</span>
<span class="main-name">{{ chatData.displayName }}</span> <span class="alt-name">{{ '@' + this.store.chatData().username }}</span>
<span class="alt-name">{{ '@' + chatData.username }}</span> }
}
</div>
</div> </div>
</div>
<div class="items-right"> <div class="items-right">
<button tuiButton appearance="flat" disabled> <button tuiButton appearance="flat" disabled>
<tui-icon icon="@tui.phone" /> <tui-icon icon="@tui.phone" />
</button> </button>
</div> </div>
</navbar> </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" [messageBoxViewModel]="store.messageBox" [messages]="store.messages()" id="scrollContainer"
(onDelete)="deleteMessage($event)" /> (onDelete)="deleteMessage($event)" />
<message-box [viewModel]="store.messageBox" />--> <message-box [viewModel]="store.messageBox" />
}
} }
</main> </main>

View File

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