diff --git a/src/broker/delegate.ts b/src/broker/delegate.ts index 6a6a6b6c..71637ae2 100644 --- a/src/broker/delegate.ts +++ b/src/broker/delegate.ts @@ -22,6 +22,7 @@ import { fromCoreMessage as inboxMessageFromCore } from "@/store/tables/inbox"; // PROJECT: UTILITIES import logger from "@/utilities/logger"; +import { default as UtilitiesAudio, AudioSound } from "@/utilities/audio"; // PROJECT: BROKER import mitt from "mitt"; @@ -88,13 +89,27 @@ class BrokerDelegate implements ProseClientDelegate { ): Promise { const messages = await client.loadMessagesWithIDs(conversation, messageIDs); - Store.$inbox.insertMessages( + // Insert all appended messages + const hasInserted = Store.$inbox.insertMessages( conversation, messages.map(message => { return inboxMessageFromCore(message); }) ); + + // Play incoming message sound? (only for messages from remote users) + if (hasInserted === true) { + const selfJIDRaw = Store.$account.getLocalJID().toString(); + + const firstNonSelfMessage = messages.find(message => { + return message.from !== selfJIDRaw; + }); + + if (firstNonSelfMessage) { + UtilitiesAudio.play(AudioSound.AlertMessageReceive); + } + } } messagesDeleted( diff --git a/src/store/tables/inbox.ts b/src/store/tables/inbox.ts index f011ecf7..1919acb4 100644 --- a/src/store/tables/inbox.ts +++ b/src/store/tables/inbox.ts @@ -146,13 +146,16 @@ const $inbox = defineStore("inbox", { return entries[jidString]; }, - insertMessage(jid: JID, message: InboxEntryMessage) { - this.insertMessages(jid, [message]); + insertMessage(jid: JID, message: InboxEntryMessage): boolean { + return this.insertMessages(jid, [message]); }, - insertMessages(jid: JID, messages: Array) { + insertMessages(jid: JID, messages: Array): boolean { const container = this.assert(jid).messages; + // Initialize inserted marker + let hasInserted = false; + messages.forEach(message => { // Acquire message identifier const messageId = message.id; @@ -166,6 +169,10 @@ const $inbox = defineStore("inbox", { // Should insert message? (does not exist) if (wasUpdated !== true) { + // Mark as inserted + hasInserted = true; + + // Insert message in its container this.$patch(() => { container.byId[messageId] = message; container.list.push(message); @@ -178,6 +185,8 @@ const $inbox = defineStore("inbox", { } as EventMessageGeneric); } }); + + return hasInserted; }, updateMessage(jid: JID, id: string, message: InboxEntryMessage): boolean { diff --git a/src/utilities/audio.ts b/src/utilities/audio.ts index f158d4e8..4bfe7c5e 100644 --- a/src/utilities/audio.ts +++ b/src/utilities/audio.ts @@ -35,6 +35,8 @@ export enum AudioSound { * CONSTANTS * ************************************************************************* */ +const AUDIO_MASTER_VOLUME = 30; + const AUDIO_TYPES_PIPELINE = [ { format: AudioFormat.OGA, @@ -91,7 +93,11 @@ class UtilitiesAudio { } } - async play(sound: AudioSound, loop = false, volume = 50): Promise { + async play( + sound: AudioSound, + loop = false, + volume = AUDIO_MASTER_VOLUME + ): Promise { // Can play sound? (otherwise ignore) if (this.__format !== null) { try { diff --git a/src/views/start/StartLogin.vue b/src/views/start/StartLogin.vue index 9b2a9b51..405d9364 100644 --- a/src/views/start/StartLogin.vue +++ b/src/views/start/StartLogin.vue @@ -41,6 +41,9 @@ import StartServerIdentity from "@/components/start/StartServerIdentity.vue"; // PROJECT: STORES import Store from "@/store"; +// PROJECT: UTILITIES +import { default as UtilitiesAudio, AudioSound } from "@/utilities/audio"; + export default { name: "StartLogin", @@ -69,6 +72,9 @@ export default { // Show success alert BaseAlert.success("Authenticated", "Welcome back!"); + // Play success sound + UtilitiesAudio.play(AudioSound.AlertActionSuccess); + // Redirect to dashboard this.$router.push({ name: "app"