Add images to chat

master
Torjus 2018-07-18 12:00:55 +02:00
parent 977ffd9d24
commit c416d0c402
9 changed files with 94 additions and 112 deletions

View File

@ -1051,7 +1051,7 @@ export default class RoomClient
break; break;
} }
// This means: server wants to change MY displayName // This means: server wants to change MY user information
case 'auth': case 'auth':
{ {
logger.debug('got auth event from server', request.data); logger.debug('got auth event from server', request.data);
@ -1060,6 +1060,9 @@ export default class RoomClient
if (request.data.verified == true) if (request.data.verified == true)
{ {
this.changeDisplayName(request.data.name); this.changeDisplayName(request.data.name);
this._dispatch(stateActions.setPicture(request.data.picture));
this._dispatch(requestActions.notify( this._dispatch(requestActions.notify(
{ {
text : `Authenticated successfully: ${request.data}` text : `Authenticated successfully: ${request.data}`
@ -1102,7 +1105,7 @@ export default class RoomClient
logger.debug('Got chat from "%s"', peerName); logger.debug('Got chat from "%s"', peerName);
this._dispatch( this._dispatch(
stateActions.addResponseMessage(chatMessage)); stateActions.addResponseMessage({ ...chatMessage, peerName }));
break; break;
} }

View File

@ -14,7 +14,8 @@ class Chat extends Component
onSendMessage, onSendMessage,
disabledInput, disabledInput,
autofocus, autofocus,
displayName displayName,
picture
} = this.props; } = this.props;
return ( return (
@ -22,7 +23,7 @@ class Chat extends Component
<MessageList /> <MessageList />
<form <form
data-component='Sender' data-component='Sender'
onSubmit={(e) => { onSendMessage(e, displayName); }} onSubmit={(e) => { onSendMessage(e, displayName, picture); }}
> >
<input <input
type='text' type='text'
@ -59,14 +60,15 @@ const mapStateToProps = (state) =>
{ {
return { return {
disabledInput : state.chatbehavior.disabledInput, disabledInput : state.chatbehavior.disabledInput,
displayName : state.me.displayName displayName : state.me.displayName,
picture : state.me.picture
}; };
}; };
const mapDispatchToProps = (dispatch) => const mapDispatchToProps = (dispatch) =>
{ {
return { return {
onSendMessage : (event, displayName) => onSendMessage : (event, displayName, picture) =>
{ {
event.preventDefault(); event.preventDefault();
const userInput = event.target.message.value; const userInput = event.target.message.value;
@ -74,7 +76,7 @@ const mapDispatchToProps = (dispatch) =>
if (userInput) if (userInput)
{ {
dispatch(stateActions.addUserMessage(userInput)); dispatch(stateActions.addUserMessage(userInput));
dispatch(requestActions.sendChatMessage(userInput, displayName)); dispatch(requestActions.sendChatMessage(userInput, displayName, picture));
} }
event.target.message.value = ''; event.target.message.value = '';
} }

View File

@ -50,9 +50,14 @@ class MessageList extends Component
{ {
const messageTime = new Date(message.time); const messageTime = new Date(message.time);
const picture = message.sender === 'response' ?
message.picture : this.props.myPicture;
return ( return (
<div className='message' key={i}> <div className='message' key={i}>
<div className={message.sender}> <div className={message.sender}>
{picture && <img src={picture} />}
<div <div
className='message-text' className='message-text'
// eslint-disable-next-line react/no-danger // eslint-disable-next-line react/no-danger
@ -82,7 +87,8 @@ MessageList.propTypes =
const mapStateToProps = (state) => const mapStateToProps = (state) =>
{ {
return { return {
chatmessages : state.chatmessages chatmessages : state.chatmessages,
myPicture : state.me.picture
}; };
}; };

View File

@ -3,9 +3,7 @@ import
createNewMessage createNewMessage
} from './helper'; } from './helper';
const initialState = []; const chatmessages = (state = [], action) =>
const chatmessages = (state = initialState, action) =>
{ {
switch (action.type) switch (action.type)
{ {

View File

@ -1,10 +1,11 @@
export function createNewMessage(text, sender, name) export function createNewMessage(text, sender, name, picture)
{ {
return { return {
type : 'message', type : 'message',
text, text,
time : Date.now(), time : Date.now(),
name, name,
sender sender,
picture
}; };
} }

View File

@ -21,7 +21,8 @@ const initialState =
audioOnlyInProgress : false, audioOnlyInProgress : false,
raiseHand : false, raiseHand : false,
raiseHandInProgress : false, raiseHandInProgress : false,
restartIceInProgress : false restartIceInProgress : false,
picture : null
}; };
const me = (state = initialState, action) => const me = (state = initialState, action) =>
@ -164,6 +165,11 @@ const me = (state = initialState, action) =>
return { ...state, restartIceInProgress: flag }; return { ...state, restartIceInProgress: flag };
} }
case 'SET_PICTURE':
{
return { ...state, picture: action.payload.picture };
}
default: default:
return state; return state;
} }

View File

@ -1,126 +1,86 @@
const initialState = {}; import omit from 'lodash/omit';
const peers = (state = initialState, action) => const peer = (state, action) =>
{
switch (action.type)
{
case 'ADD_PEER':
return action.payload.peer;
case 'SET_PEER_DISPLAY_NAME':
return { ...state, displayName: action.payload.displayName };
case 'SET_PEER_VIDEO_IN_PROGRESS':
return { ...state, peerVideoInProgress: action.payload.flag };
case 'SET_PEER_AUDIO_IN_PROGRESS':
return { ...state, peerAudioInProgress: action.payload.flag };
case 'SET_PEER_SCREEN_IN_PROGRESS':
return { ...state, peerScreenInProgress: action.payload.flag };
case 'SET_PEER_RAISE_HAND_STATE':
return { ...state, raiseHandState: action.payload.raiseHandState };
case 'ADD_CONSUMER':
{
const consumers = [ ...state.consumers, action.payload.consumer.id ];
return { ...state, consumers };
}
case 'REMOVE_CONSUMER':
{
const consumers = state.consumers.filter((consumer) => consumer !== action.payload.consumerId);
return { ...state, consumers };
}
default:
return state;
}
};
const peers = (state = {}, action) =>
{ {
switch (action.type) switch (action.type)
{ {
case 'ADD_PEER': case 'ADD_PEER':
{ {
const { peer } = action.payload; return { ...state, [action.payload.peer.name]: peer(undefined, action) };
return { ...state, [peer.name]: peer };
} }
case 'REMOVE_PEER': case 'REMOVE_PEER':
{ {
const { peerName } = action.payload; return omit(state, [ action.payload.peerName ]);
const newState = { ...state };
delete newState[peerName];
return newState;
} }
case 'SET_PEER_DISPLAY_NAME': case 'SET_PEER_DISPLAY_NAME':
{
const { displayName, peerName } = action.payload;
const peer = state[peerName];
if (!peer)
throw new Error('no Peer found');
const newPeer = { ...peer, displayName };
return { ...state, [newPeer.name]: newPeer };
}
case 'SET_PEER_VIDEO_IN_PROGRESS': case 'SET_PEER_VIDEO_IN_PROGRESS':
{
const { peerName, flag } = action.payload;
const peer = state[peerName];
if (!peer)
throw new Error('no Peer found');
const newPeer = { ...peer, peerVideoInProgress: flag };
return { ...state, [newPeer.name]: newPeer };
}
case 'SET_PEER_AUDIO_IN_PROGRESS': case 'SET_PEER_AUDIO_IN_PROGRESS':
{
const { peerName, flag } = action.payload;
const peer = state[peerName];
if (!peer)
throw new Error('no Peer found');
const newPeer = { ...peer, peerAudioInProgress: flag };
return { ...state, [newPeer.name]: newPeer };
}
case 'SET_PEER_SCREEN_IN_PROGRESS': case 'SET_PEER_SCREEN_IN_PROGRESS':
{
const { peerName, flag } = action.payload;
const peer = state[peerName];
if (!peer)
throw new Error('no Peer found');
const newPeer = { ...peer, peerScreenInProgress: flag };
return { ...state, [newPeer.name]: newPeer };
}
case 'SET_PEER_RAISE_HAND_STATE': case 'SET_PEER_RAISE_HAND_STATE':
{
const { peerName, raiseHandState } = action.payload;
const peer = state[peerName];
if (!peer)
throw new Error('no Peer found');
const newPeer = { ...peer, raiseHandState };
return { ...state, [newPeer.name]: newPeer };
}
case 'ADD_CONSUMER': case 'ADD_CONSUMER':
{ {
const { consumer, peerName } = action.payload; const oldPeer = state[action.payload.peerName];
const peer = state[peerName];
if (!peer) if (!oldPeer)
throw new Error('no Peer found for new Consumer'); {
throw new Error('no Peer found');
}
const newConsumers = [ ...peer.consumers, consumer.id ]; return { ...state, [oldPeer.name]: peer(oldPeer, action) };
const newPeer = { ...peer, consumers: newConsumers };
return { ...state, [newPeer.name]: newPeer };
} }
case 'REMOVE_CONSUMER': case 'REMOVE_CONSUMER':
{ {
const { consumerId, peerName } = action.payload; const oldPeer = state[action.payload.peerName];
const peer = state[peerName];
// NOTE: This means that the Peer was closed before, so it's ok. // NOTE: This means that the Peer was closed before, so it's ok.
if (!peer) if (!oldPeer)
return state; return state;
const idx = peer.consumers.indexOf(consumerId); return { ...state, [oldPeer.name]: peer(oldPeer, action) };
if (idx === -1)
throw new Error('Consumer not found');
const newConsumers = peer.consumers.slice();
newConsumers.splice(idx, 1);
const newPeer = { ...peer, consumers: newConsumers };
return { ...state, [newPeer.name]: newPeer };
} }
default: default:

View File

@ -184,9 +184,9 @@ export const installExtension = () =>
}; };
}; };
export const sendChatMessage = (text, name) => export const sendChatMessage = (text, name, picture) =>
{ {
const message = createNewMessage(text, 'response', name); const message = createNewMessage(text, 'response', name, picture);
return { return {
type : 'SEND_CHAT_MESSAGE', type : 'SEND_CHAT_MESSAGE',

View File

@ -418,3 +418,9 @@ export const dropMessages = () =>
type : 'DROP_MESSAGES' type : 'DROP_MESSAGES'
}; };
}; };
export const setPicture = (picture) =>
({
type : 'SET_PICTURE',
payload : { picture }
});