2017-06-08_509bba0/509bba0_unpacked_with_node_modules/discord_app/components/SoundPlayer.js
2022-07-26 10:06:20 -07:00

129 lines
4.2 KiB
JavaScript
Executable file

/* @flow */
import React from 'react';
import PureRenderMixin from 'react-addons-pure-render-mixin';
import Flux from '../lib/flux';
import lodash from 'lodash';
import * as SoundUtils from '../utils/SoundUtils';
import {InputModes} from '../Constants';
import MediaEngineStore from '../stores/MediaEngineStore';
import RTCConnectionStore from '../stores/RTCConnectionStore';
import SpeakingStore from '../stores/SpeakingStore';
import VoiceStateStore from '../stores/VoiceStateStore';
import UserStore from '../stores/UserStore';
import GuildStore from '../stores/GuildStore';
import ChannelStore from '../stores/ChannelStore';
import SelectedChannelStore from '../stores/SelectedChannelStore';
import NotificationStore from '../stores/NotificationStore';
type Props = any;
type State = {
selfMute?: boolean,
selfDeaf?: boolean,
voiceDisconnected?: boolean,
speaking?: boolean,
tempMuted?: boolean,
voiceChannelId?: ?string,
voiceChannelUserCount?: number,
};
const SoundPlayer = React.createClass({
mixins: [
Flux.StoreListenerMixin(
// Mute/Deafen
MediaEngineStore,
// Voice Disconnected
RTCConnectionStore,
// Push to Talk
SpeakingStore,
// User Join/Leave
VoiceStateStore,
SelectedChannelStore
),
PureRenderMixin,
],
getStateFromStores() {
const voiceChannelId = SelectedChannelStore.getVoiceChannelId();
let voiceChannelUserCount = 0;
if (voiceChannelId != null) {
const channel = ChannelStore.getChannel(voiceChannelId);
const currentUser = UserStore.getCurrentUser();
if (channel != null && currentUser != null) {
lodash.forEach(VoiceStateStore.getVoiceStates(channel.getGuildId()), (user, userId) => {
if (userId !== currentUser.id && user.channelId === voiceChannelId) {
voiceChannelUserCount++;
}
});
}
}
return {
selfMute: MediaEngineStore.isSelfMute(),
selfDeaf: MediaEngineStore.isSelfDeaf(),
voiceDisconnected: RTCConnectionStore.isDisconnected(),
speaking: SpeakingStore.isCurrentUserSpeaking(),
tempMuted: MediaEngineStore.isSelfMutedTemporarily(),
voiceChannelId: voiceChannelId,
voiceChannelUserCount: voiceChannelUserCount,
};
},
componentDidUpdate(prevProps: Props, prevState: State) {
if (prevState.selfDeaf !== this.state.selfDeaf) {
this.playSound(this.state.selfDeaf ? 'deafen' : 'undeafen');
} else if (prevState.selfMute !== this.state.selfMute) {
this.playSound(this.state.selfMute ? 'mute' : 'unmute');
}
if (!prevState.voiceDisconnected && prevState.voiceDisconnected !== this.state.voiceDisconnected) {
this.playSound('disconnect');
}
if (prevState.speaking !== this.state.speaking) {
if (MediaEngineStore.getMode() === InputModes.PUSH_TO_TALK && !this.state.selfMute) {
this.playSound(this.state.speaking ? 'ptt_start' : 'ptt_stop');
}
}
if (prevState.tempMuted !== this.state.tempMuted) {
if (MediaEngineStore.getMode() === InputModes.VOICE_ACTIVITY && !this.state.selfMute) {
this.playSound(!this.state.tempMuted ? 'ptt_start' : 'ptt_stop');
}
}
if (prevState.voiceChannelId === this.state.voiceChannelId) {
const prevVoiceChannelUserCount = prevState.voiceChannelUserCount || 0;
const curVoiceChannelUserCount = this.state.voiceChannelUserCount || 0;
if (prevVoiceChannelUserCount > curVoiceChannelUserCount) {
!this.isAFK() && this.playSound('user_leave');
} else if (prevVoiceChannelUserCount < curVoiceChannelUserCount) {
!this.isAFK() && this.playSound('user_join');
}
}
},
isAFK(): boolean {
const voiceChannel = ChannelStore.getChannel(this.state.voiceChannelId);
if (voiceChannel == null) return false;
const guildId = voiceChannel.getGuildId();
if (guildId == null) return false;
const guild = GuildStore.getGuild(guildId);
return guild != null && guild.afkChannelId === voiceChannel.id;
},
playSound(sound: string) {
if (NotificationStore.isSoundDisabled(sound)) return;
SoundUtils.playSound(sound, 0.4);
},
render() {
return null;
},
});
export default SoundPlayer;
// WEBPACK FOOTER //
// ./discord_app/components/SoundPlayer.js