Skip to content

Commit

Permalink
fix: automatically decide on inbox messages insert/restore mode, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriansaliou committed Jun 16, 2024
1 parent 9211352 commit 2a2d8dc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
18 changes: 3 additions & 15 deletions src/assemblies/inbox/InboxMessaging.vue
Original file line number Diff line number Diff line change
Expand Up @@ -966,21 +966,8 @@ export default {
result.lastMessageId
);

// Check if should insert or restore messages?
// Notice: this is required if there are already messages in the \
// store, that could be more recent than those messages, eg. if \
// a new message was received in-band and the room is opened later.
// Important: acquire insert mode AFTER loading messages and \
// BEFORE inserting them to the store, since the loading could \
// have taken quite some time, and some messages might have been \
// inserted in the store mid-way.
const insertMode =
this.messages.length > 0
? InboxInsertMode.Restore
: InboxInsertMode.Insert;

// Insert messages to store
Store.$inbox.insertCoreMessages(room, result.messages, insertMode);
Store.$inbox.insertCoreMessages(room, result.messages);
} catch (error) {
// Alert of load error
this.$log.error(
Expand Down Expand Up @@ -1048,7 +1035,8 @@ export default {
result.lastMessageId
);

// Insert messages to store
// Insert messages to store (in restore mode since messages are \
// definitely from past history)
Store.$inbox.insertCoreMessages(
room,
result.messages,
Expand Down
13 changes: 10 additions & 3 deletions src/broker/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import mitt from "mitt";

// PROJECT: STORES
import Store from "@/store";
import { fromCoreMessage as inboxMessageFromCore } from "@/store/tables/inbox";
import {
fromCoreMessage as inboxMessageFromCore,
InboxInsertMode
} from "@/store/tables/inbox";

// PROJECT: UTILITIES
import { AudioSound, default as UtilitiesAudio } from "@/utilities/audio";
Expand Down Expand Up @@ -139,8 +142,12 @@ class BrokerDelegate implements ProseClientDelegate {

const messages = await room.loadMessagesWithIDs(messageIDs);

// Insert all appended messages
const hasInserted = Store.$inbox.insertCoreMessages(room, messages);
// Insert all appended messages (in insert mode since messages got appended)
const hasInserted = Store.$inbox.insertCoreMessages(
room,
messages,
InboxInsertMode.Insert
);

// Trigger a notification? (only for messages from remote users)
if (hasInserted === true) {
Expand Down
36 changes: 34 additions & 2 deletions src/store/tables/inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ interface InboxEntry {
}

interface InboxEntryMessage extends MessagingStoreMessageData {
timestamp: number;
archiveId?: ArchiveID;
}

Expand Down Expand Up @@ -153,6 +154,7 @@ const fromCoreMessage = function (
archiveId: message.archiveId,
type: message.type,
date: message.date.toISOString(),
timestamp: message.date.getTime(),
from: message.user.jid,
content: message.content,

Expand Down Expand Up @@ -305,12 +307,42 @@ const $inbox = defineStore("inbox", {

insertCoreMessages(
room: Room,
messages: CoreMessage[],
mode = InboxInsertMode.Insert
messages: Array<CoreMessage>,
mode?: InboxInsertMode
): boolean {
const container = this.assert(room.id).messages;

// Define common insert markers
let hasInserted = false,
lastInsertError: Error | null = null;

// Automatically decide on the insert mode? (if none provided)
// Notice: this compares the first message entry date with the last core \
// message date. If the first container message is more recent than \
// the last core message to be inserted, then it means the whole block \
// of messages to insert should be prepended BEFORE existing messages. \
// This is required if there are already messages in the store, that \
// could be more recent than those messages, eg. if a new message was \
// received in-band and the room is opened later.
// Important: acquire insert mode AFTER loading messages and \
// BEFORE inserting them to the store, since the loading could \
// have taken quite some time, and some messages might have been \
// inserted in the store mid-way.
if (mode === undefined) {
if (
container.list.length > 0 &&
messages.length > 0 &&
container.list[0].timestamp >=
messages[messages.length - 1].date.getTime()
) {
// Use restore mode
mode = InboxInsertMode.Restore;
} else {
// Use insert mode (default)
mode = InboxInsertMode.Insert;
}
}

// Insert or restore messages (forwards or backwards)
// Notice: instead of allocating a new Array by using the simple \
// 'Array.slice().reverse()' method, rather use zero-cost forward or \
Expand Down

0 comments on commit 2a2d8dc

Please sign in to comment.