init
This commit is contained in:
@@ -274,7 +274,7 @@ watch(isAtBottom, async (atBottom) => {
|
||||
|
||||
watch(channelId, async (newChannelId) => {
|
||||
if (newChannelId) {
|
||||
if (props.serverId) await emojiStore.fetchEmojis(props.serverId);
|
||||
await emojiStore.fetchEmojis(props.serverId ?? null);
|
||||
await messageStore.fetchMessages(newChannelId);
|
||||
await scrollToBottom();
|
||||
await markCurrentChannelRead(newChannelId);
|
||||
|
||||
@@ -43,6 +43,14 @@ export const useConversationStore = defineStore('conversation', {
|
||||
const conversation = this.conversations.find(item => item.id === id)
|
||||
if (conversation) conversation.unread_count = unreadCount
|
||||
},
|
||||
applyIncomingMessage(message: { channel_id: string; content: string }) {
|
||||
const conversation = this.conversations.find(item => item.id === message.channel_id)
|
||||
if (!conversation) return
|
||||
conversation.last_message = message.content
|
||||
conversation.unread_count += 1
|
||||
conversation.updated_at = new Date().toISOString()
|
||||
this.conversations = [conversation, ...this.conversations.filter(item => item.id !== conversation.id)]
|
||||
},
|
||||
reset() { this.conversations = []; this.loading = false }
|
||||
}
|
||||
})
|
||||
|
||||
@@ -23,11 +23,14 @@ export const useEmojiStore = defineStore("emoji", {
|
||||
emojisById: (state): Record<string, Emoji> => Object.fromEntries(state.emojis.map(emoji => [emoji.id, emoji])),
|
||||
},
|
||||
actions: {
|
||||
async fetchEmojis(serverId: string, force = false) {
|
||||
async fetchEmojis(serverId: string | null = null, force = false) {
|
||||
if (!force && this.activeServerId === serverId && this.emojis.length) return;
|
||||
this.loading = true;
|
||||
try {
|
||||
const response = await useApi().get(`/emojis?server_id=${encodeURIComponent(serverId)}`);
|
||||
const url = serverId === null
|
||||
? '/emojis'
|
||||
: `/emojis?server_id=${encodeURIComponent(serverId)}`;
|
||||
const response = await useApi().get(url);
|
||||
if (!response.ok) throw new Error(`Emoji loading failed (${response.status})`);
|
||||
this.emojis = await response.json() as Emoji[];
|
||||
this.activeServerId = serverId;
|
||||
|
||||
@@ -5,6 +5,7 @@ import {useServerStore} from "@/stores/server.ts";
|
||||
import {useAuthStore} from "@/stores/auth.ts";
|
||||
import {useNotificationStore} from "@/stores/notification.ts";
|
||||
import {useEmojiStore} from "@/stores/emoji.ts";
|
||||
import {useConversationStore} from "@/stores/conversation.ts";
|
||||
|
||||
// Change this value to adjust the maximum number of messages kept in the DOM.
|
||||
// Directional loads automatically use half of this window.
|
||||
@@ -350,7 +351,8 @@ export const useMessageStore = defineStore("message", {
|
||||
const isActiveChannel = message.channel_id === this.activeChannelId;
|
||||
if (!isActiveChannel) {
|
||||
if (fromGateway && !isOwnMessage) {
|
||||
serverStore.applyIncomingMessage(message.server_id, message.channel_id);
|
||||
if (message.server_id) serverStore.applyIncomingMessage(message.server_id, message.channel_id);
|
||||
else useConversationStore().applyIncomingMessage(message);
|
||||
notificationStore.show("Nouveau message", "Un nouveau message est arrivé dans un autre canal.");
|
||||
}
|
||||
return;
|
||||
@@ -365,7 +367,8 @@ export const useMessageStore = defineStore("message", {
|
||||
if (!this.isAtBottom && this.newestId && message.id > this.newestId) {
|
||||
this.hasMoreAfter = true;
|
||||
if (fromGateway && !isOwnMessage) {
|
||||
serverStore.applyIncomingMessage(message.server_id, message.channel_id);
|
||||
if (message.server_id) serverStore.applyIncomingMessage(message.server_id, message.channel_id);
|
||||
else useConversationStore().applyIncomingMessage(message);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -376,7 +379,8 @@ export const useMessageStore = defineStore("message", {
|
||||
if (this.isAtBottom) {
|
||||
this.scrollToBottomRequested = true;
|
||||
} else if (fromGateway && !isOwnMessage) {
|
||||
serverStore.applyIncomingMessage(message.server_id, message.channel_id);
|
||||
if (message.server_id) serverStore.applyIncomingMessage(message.server_id, message.channel_id);
|
||||
else useConversationStore().applyIncomingMessage(message);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user