Skip to content

Commit

Permalink
Merge pull request #84 from cabal-club/fix-initial-username-and-channel
Browse files Browse the repository at this point in the history
Fix setting username and channel on initial load/creation of a cabal
  • Loading branch information
nikolaiwarner authored Sep 9, 2018
2 parents 73a325b + ae2d465 commit f1d6eeb
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 223 deletions.
49 changes: 27 additions & 22 deletions app/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
const mkdir = promisify(fs.mkdir)

const DEFAULT_CHANNEL = 'default'

var cabals = {}

export const viewCabal = ({addr}) => dispatch => {
Expand Down Expand Up @@ -57,8 +59,6 @@ export const updateCabal = (opts) => dispatch => {
}
export const joinChannel = ({addr, channel}) => dispatch => {
if (channel.length > 0) {
// var cabal = cabals[addr]
// cabal.joinChannel(channel)
dispatch(viewChannel({addr, channel}))
}
}
Expand All @@ -73,7 +73,7 @@ export const leaveChannel = ({addr, channel}) => dispatch => {

export const changeUsername = ({addr, username}) => dispatch => {
var currentCabal = cabals[addr]
currentCabal.username = username || catnames.random()
currentCabal.username = username
currentCabal.publishNick(username)
dispatch({ type: 'UPDATE_CABAL', addr, username })
}
Expand Down Expand Up @@ -140,7 +140,6 @@ export const addCabal = ({addr, input, username}) => dispatch => {
initializeCabal({addr, username, dispatch})
} else {
// Create new Cabal
username = username || catnames.random()
var tempDir = path.join(homedir(), '.cabal-desktop/.tmp')
var newCabal = Cabal(tempDir, null, {username})
newCabal.getLocalKey((err, key) => {
Expand All @@ -151,6 +150,7 @@ export const addCabal = ({addr, input, username}) => dispatch => {
}

const initializeCabal = ({addr, username, dispatch}) => {
username = username || 'conspirator'
var dir = path.join(homedir(), '.cabal-desktop', addr)
var cabal = Cabal(dir, addr ? 'cabal://' + addr : null, {username})

Expand All @@ -164,11 +164,12 @@ const initializeCabal = ({addr, username, dispatch}) => {
cabal.key = addr
var swarm = Swarm(cabal)

cabal.username = username
cabal.client.swarm = swarm
cabal.client.addr = addr
cabal.client.channel = 'default'
cabal.client.channel = DEFAULT_CHANNEL
cabal.client.channels = []
cabal.client.user = {}
cabal.client.user = {name: username}
cabal.client.users = {}
cabal.client.channelMessages = {}
cabal.client.channelMessagesUnread = {}
Expand Down Expand Up @@ -209,16 +210,21 @@ const initializeCabal = ({addr, username, dispatch}) => {
}

cabal.channels.events.on('add', (channel) => {
cabal.client.channels.push(channel)
if (!cabal.client.channelListeners[channel]) {
cabal.messages.events.on(channel, onMessage)
cabal.client.channelListeners[channel] = onMessage
if (!cabal.client.channels.includes(channel)) {
cabal.client.channels.push(channel)
if (!cabal.client.channelListeners[channel]) {
cabal.messages.events.on(channel, onMessage)
cabal.client.channelListeners[channel] = onMessage
}
}
})
cabal.channels.get((err, channels) => {
if (err) return console.error(err)
cabal.client.channels = channels
dispatch(joinChannel({addr, channel: 'default'}))
if (cabal.client.channels.length === 0) {
cabal.client.channels.push(DEFAULT_CHANNEL)
}
dispatch(joinChannel({addr, channel: DEFAULT_CHANNEL}))
cabal.client.channels.forEach((channel) => {
if (!cabal.client.channelListeners[channel]) {
cabal.messages.events.on(channel, onMessage)
Expand All @@ -230,25 +236,24 @@ const initializeCabal = ({addr, username, dispatch}) => {
cabal.users.getAll((err, users) => {
if (err) return
cabal.client.users = users
// Correct any bad user data
Object.keys(cabal.client.users).forEach((key) => {
let user = cabal.client.users[key]
if (!user.name || user.name.trim().length === 0) {
user.name = user.name || `conspirator ${user.key.substring(0, 4)}`
}
cabal.client.users[key] = user
})

const updateLocalKey = () => {
cabal.getLocalKey((err, lkey) => {
if (err) return
if (!Object.keys(cabal.client.users).includes(lkey)) {
cabal.client.users[lkey] = {
local: true,
online: true,
key: lkey,
name: cabal.client.user.name || 'conspirator'
}
}
Object.keys(cabal.client.users).forEach((key) => {
if (key === lkey) {
cabal.client.user = cabal.client.users[key]
cabal.client.user.local = true
cabal.client.user.online = true
cabal.client.user.key = key
cabal.username = cabal.client.user.name || catnames.random()
cabal.username = cabal.client.user.name
cabal.publishNick(cabal.username)
}
})
Expand Down Expand Up @@ -330,7 +335,7 @@ const storeOnDisk = async () => {
(acc, addr) => ({
...acc,
[addr]: JSON.stringify({
username: cabals[addr].client.user.name,
username: cabals[addr].username || 'conspirator',
addr: cabals[addr].client.addr
})
}),
Expand Down
6 changes: 3 additions & 3 deletions app/containers/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ export default function MessagesContainer (props) {
return (
<div key={index} className='messages__item'>
<div className='messages__item__avatar'>
{repeatedAuthor ? null : <Avatar name={message.author} />}
{repeatedAuthor ? null : <Avatar name={message.author || 'conspirator'} />}
</div>
<div className='messages__item__metadata'>
{repeatedAuthor ? null : <div className='messages__item__metadata__name'>{message.author}<span>{moment(message.time).format('h:mm A')}</span></div>}
{repeatedAuthor ? null : <div className='messages__item__metadata__name'>{message.author || 'conspirator'}<span>{moment(message.time).format('h:mm A')}</span></div>}
<div className={repeatedAuthor ? 'text indent' : 'text'}>
{enrichText(message.content)}
</div>
Expand All @@ -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 || 'conspirator'} />}
</div>
</div>
<div className='messages__item__metadata'>
Expand Down
2 changes: 1 addition & 1 deletion app/containers/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class SidebarScreen extends React.Component {
<img src='static/images/icon-status-offline.svg' />
}
</div>
<div className='collection__item__content'>{user.name}</div>
<div className='collection__item__content'>{user.name || user.key.substring(0, 6)}</div>
<div className='collection__item__handle' />
</div>
)}
Expand Down
Loading

0 comments on commit f1d6eeb

Please sign in to comment.