Skip to content

Commit

Permalink
Merge pull request #82 from cabal-club/fix-incoming-messages
Browse files Browse the repository at this point in the history
Display incoming messages in correct channel
  • Loading branch information
nikolaiwarner authored Sep 7, 2018
2 parents de085c6 + c01052c commit 456ec1e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
34 changes: 23 additions & 11 deletions app/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,39 +81,37 @@ export const changeUsername = ({addr, username}) => dispatch => {
export const getMessages = ({addr, channel, count}) => dispatch => {
if (channel.length === 0) return
var cabal = cabals[addr]
if (!cabal.client.messages) cabal.client.messages = []

let rs = cabal.messages.read(channel, {limit: count, lt: '~'})
collect(rs, (err, msgs) => {
if (err) return
msgs.reverse()
cabal.client.messages = []
cabal.client.channelMessages[channel] = []
msgs.forEach((msg) => {
let author = cabal.client.users[msg.key].name
let {type, timestamp, content} = msg.value
cabal.client.messages.push({
cabal.client.channelMessages[channel].push({
author,
content: content.text,
key: msg.key + timestamp,
time: timestamp,
type
})
})
dispatch({type: 'UPDATE_CABAL', addr, messages: cabal.client.messages})
dispatch({type: 'UPDATE_CABAL', addr, messages: cabal.client.channelMessages[channel]})
})
}

export const viewChannel = ({addr, channel}) => dispatch => {
if (channel.length === 0) return
var cabal = cabals[addr]
cabal.client.channel = channel
cabal.client.messages = []
cabal.client.channelMessagesUnread[channel] = 0
storeOnDisk()

// dont pass around swarm and watcher, only the things that matter.
dispatch({type: 'ADD_CABAL',
addr,
messages: cabal.client.messages,
messages: cabal.client.channelMessages[channel],
username: cabal.username,
users: cabal.client.users,
channel: cabal.client.channel,
Expand Down Expand Up @@ -172,14 +170,19 @@ const initializeCabal = ({addr, username, dispatch}) => {
cabal.client.channels = []
cabal.client.user = {}
cabal.client.users = {}
cabal.client.messages = []
cabal.client.channelMessages = {}
cabal.client.channelMessagesUnread = {}
cabal.client.channelListeners = {}

const onMessage = (message) => {
let {type, timestamp, content} = message.value
let channel = content.channel
if (cabal.client.users[message.key]) {
let author = cabal.client.users[message.key].name
let {type, timestamp, content} = message.value
cabal.client.messages.push({
if (!cabal.client.channelMessages[channel]) {
cabal.client.channelMessages[channel] = []
}
cabal.client.channelMessages[channel].push({
author,
content: content.text,
key: message.key + timestamp,
Expand All @@ -193,7 +196,16 @@ const initializeCabal = ({addr, username, dispatch}) => {
})
}
}
dispatch({type: 'UPDATE_CABAL', addr, messages: cabal.client.messages})
if (cabal.client.channel === channel) {
dispatch({type: 'UPDATE_CABAL', addr, messages: cabal.client.channelMessages[channel]})
} else {
if (!cabal.client.channelMessagesUnread[channel]) {
cabal.client.channelMessagesUnread[channel] = 1
} else {
cabal.client.channelMessagesUnread[channel] = cabal.client.channelMessagesUnread[channel] + 1
}
dispatch({type: 'UPDATE_CABAL', addr, channelMessagesUnread: cabal.client.channelMessagesUnread})
}
}

cabal.channels.events.on('add', (channel) => {
Expand Down
4 changes: 2 additions & 2 deletions app/containers/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function MessagesContainer (props) {
if (messages.length === 0) {
return (
<div className='messages starterMessage'>
'This is a new channel. Send a message to start things off!'
This is a new channel. Send a message to start things off!
</div>
)
}
Expand Down Expand Up @@ -58,7 +58,7 @@ export default function MessagesContainer (props) {
<div key={index} className='messages__item messages__item--emote'>
<div className='messages__item__avatar'>
<div className='messages__item__avatar__img'>
{repeatedAuthor ? null : <Avatar name={message.author} />}
{repeatedAuthor ? null : <Avatar name={message.author} />}
</div>
</div>
<div className='messages__item__metadata'>
Expand Down
4 changes: 4 additions & 0 deletions app/containers/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const mapStateToProps = state => {
addr: state.currentCabal,
cabals: state.cabals,
cabal,
channelMessagesUnread: cabal.channelMessagesUnread,
username: cabal.username
}
}
Expand Down Expand Up @@ -99,6 +100,9 @@ class SidebarScreen extends React.Component {
<div key={channel} onClick={this.selectChannel.bind(this, channel)} className={cabal.channel === channel ? 'collection__item active' : 'collection__item'}>
<div className='collection__item__icon'><img src='static/images/icon-channel.svg' /></div>
<div className='collection__item__content'>{channel}</div>
{this.props.channelMessagesUnread && this.props.channelMessagesUnread[channel] > 0 &&
<div className='collection__item__messagesUnreadCount'>{this.props.channelMessagesUnread[channel]}</div>
}
<div className='collection__item__handle' />
</div>
)}
Expand Down
18 changes: 18 additions & 0 deletions app/styles/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ form {
width: 100%;
padding: 0.25rem 1rem 0.25rem 1rem;
transition: all 0.05s ease-in-out;
position: relative;
}

.collection .collection__item:hover {
Expand Down Expand Up @@ -309,6 +310,23 @@ form {
overflow: hidden;
}

.collection .collection__item .collection__item__messagesUnreadCount {
display: table-cell;
display: inline-block;
font-size: 0.875rem;
line-height: 1.4;
color: #fff;
background-color: #693AFA;
padding: 0 0.5rem;
border-radius: 5rem;
vertical-align: middle;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
position: absolute;
right: 1rem;
}

.collection .collection__item .collection__item__handle {
display: table-cell;
width: 1.25rem;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cabal-desktop",
"version": "1.0.2",
"version": "1.0.3",
"description": "Cabal p2p offline-first desktop application",
"scripts": {
"build": "cross-env NODE_ENV=production webpack",
Expand Down

0 comments on commit 456ec1e

Please sign in to comment.