Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update message/user channel list with unread counts and colors. #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 71 additions & 10 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,18 @@ function formatMessageMentions(text) {
}

function handleNewMessage(message) {
let text;
let username;
let author;

if (message.user === currentUser.id) {
username = currentUser.name;
} else {
const author = users.find(user => message.user === user.id);
if (users) {
author = users.find(user => message.user === user.id)
} else {
author = null;
}
username = (author && author.name) || UNKNOWN_USER_NAME;

notifier.notify({
Expand All @@ -103,14 +110,57 @@ function handleNewMessage(message) {
});
}

if (message.channel !== currentChannelId ||
typeof message.text === 'undefined') {
if (typeof message.text === 'undefined') {
return;
}

// Message for another channel, update its "counter"
if (message.channel !== currentChannelId) {
const channels = cachedChannelData.channels;
channel = channels.filter(chan => chan.id == message.channel)[0]
if (channel) {
regex = new RegExp(`^${channel.name}\\b`)
const channelIndex = components.channelList.fuzzyFind(regex)
var listItem = components.channelList.getItem(channelIndex)
listItem.style.fg = 'red'

var count = listItem.content.match(/\s(\d+)$/)
if (count) {
count = parseInt(count) + 1
} else {
count = 1
}
components.channelList.setItem(channelIndex, `${channel.name} ${count}`)
} else {
author = users.find(user => message.user === user.id)
if (author) {
regex = new RegExp(`^${author.name}\\b`)
const channelIndex = components.userList.fuzzyFind(regex)
var listItem = components.userList.getItem(channelIndex)
listItem.style.fg = 'red'

var count = listItem.content.match(/\s(\d+)$/)
if (count) {
count = parseInt(count) + 1
} else {
count = 1
}
components.userList.setItem(channelIndex, `${author.name} ${count}`)
// text = `@${author.name} - ${channelIndex} - #${count}`
} else {
// unknown message
// TODO
// text = `?? ${message.channel} ?? : ${JSON.stringify(message)}`
}
return
}
} else {
text = renderImageFromLink(message.text);
text = formatMessageMentions(text);
}
components.chatWindow.insertBottom(
`{bold}${username}{/bold}: ${formatMessageMentions(message.text)}`
`{bold}${username}{/bold}: ${text}`
);

components.chatWindow.scroll(SCROLL_PER_MESSAGE);
components.screen.render();
}
Expand Down Expand Up @@ -174,7 +224,7 @@ slack.init((data, ws) => {
}

const parsedUserData = JSON.parse(userData);
users = parsedUserData.members.filter(user => !user.deleted && user.id !== currentUser.id);
users = parsedUserData.members.filter(user => !user.deleted);

components.userList.setItems(users.map(slackUser => slackUser.name));
components.screen.render();
Expand All @@ -185,6 +235,8 @@ slack.init((data, ws) => {
components.channelList.setItems(['Connecting to Slack...']);
components.screen.render();

let cachedChannelData;

// set the channel list to the channels returned from slack
slack.getChannels((error, response, data) => {
if (error || response.statusCode !== 200) {
Expand All @@ -201,6 +253,8 @@ slack.getChannels((error, response, data) => {
channels.map(slackChannel => slackChannel.name)
);
components.screen.render();

cachedChannelData = channelData;
});

// event handler when user selects a channel
Expand Down Expand Up @@ -230,13 +284,14 @@ function updateMessages(data, markFn) {
}
}
}

return { text: message.text, username: username || UNKNOWN_USER_NAME };
})
.forEach((message) => {
// add messages to window
components.chatWindow.unshiftLine(
`{bold}${message.username}{/bold}: ${formatMessageMentions(message.text)}`
);

});

// mark the most recently read message
Expand All @@ -252,9 +307,12 @@ function updateMessages(data, markFn) {
}

components.userList.on('select', (data) => {
const username = data.content;
// ignore channel counts and reset styling
const username = data.content.match(/^\S+/)[0];
data.content = username;
data.style.fg = '';

// a channel was selected
// a user channel was selected
components.mainWindowTitle.setContent(`{bold}${username}{/bold}`);
components.chatWindow.setContent('Getting messages...');
components.screen.render();
Expand All @@ -274,7 +332,10 @@ components.userList.on('select', (data) => {
});

components.channelList.on('select', (data) => {
const channelName = data.content;
// ignore channel counts and reset styling
const channelName = data.content.match(/^\S+/)[0];
data.content = channelName;
data.style.fg = '';

// a channel was selected
components.mainWindowTitle.setContent(`{bold}${channelName}{/bold}`);
Expand Down