Skip to content

Commit

Permalink
feat: play incoming message sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriansaliou committed Oct 7, 2023
1 parent 432d9b4 commit a8667ef
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
17 changes: 16 additions & 1 deletion src/broker/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -88,13 +89,27 @@ class BrokerDelegate implements ProseClientDelegate {
): Promise<void> {
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(
Expand Down
15 changes: 12 additions & 3 deletions src/store/tables/inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<InboxEntryMessage>) {
insertMessages(jid: JID, messages: Array<InboxEntryMessage>): boolean {
const container = this.assert(jid).messages;

// Initialize inserted marker
let hasInserted = false;

messages.forEach(message => {
// Acquire message identifier
const messageId = message.id;
Expand All @@ -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);
Expand All @@ -178,6 +185,8 @@ const $inbox = defineStore("inbox", {
} as EventMessageGeneric);
}
});

return hasInserted;
},

updateMessage(jid: JID, id: string, message: InboxEntryMessage): boolean {
Expand Down
8 changes: 7 additions & 1 deletion src/utilities/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export enum AudioSound {
* CONSTANTS
* ************************************************************************* */

const AUDIO_MASTER_VOLUME = 30;

const AUDIO_TYPES_PIPELINE = [
{
format: AudioFormat.OGA,
Expand Down Expand Up @@ -91,7 +93,11 @@ class UtilitiesAudio {
}
}

async play(sound: AudioSound, loop = false, volume = 50): Promise<void> {
async play(
sound: AudioSound,
loop = false,
volume = AUDIO_MASTER_VOLUME
): Promise<void> {
// Can play sound? (otherwise ignore)
if (this.__format !== null) {
try {
Expand Down
6 changes: 6 additions & 0 deletions src/views/start/StartLogin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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",

Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit a8667ef

Please sign in to comment.