Skip to content
This repository has been archived by the owner on Jun 26, 2024. It is now read-only.

Commit

Permalink
fix #34 and #36
Browse files Browse the repository at this point in the history
  • Loading branch information
trazyn committed Sep 19, 2017
1 parent bb4026e commit d34636a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 44 deletions.
85 changes: 47 additions & 38 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ async function getIcon(cookies, userid, src) {

icon = avatarMap[userid];

if (!icon && src) {
if (!icon && cookies && src) {
try {
let response = await axios({
url: src,
Expand All @@ -282,7 +282,6 @@ async function getIcon(cookies, userid, src) {

icon = `${avatarPath.name}/${userid}.jpg`;
response.data.pipe(fs.createWriteStream(icon));
console.log('write %s', icon);
} catch (ex) {
console.error(ex);
icon = avatarPlaceholder;
Expand Down Expand Up @@ -523,45 +522,55 @@ const createMainWindow = () => {

ipcMain.on('menu-update', async(event, args) => {
var { cookies, contacts, conversations } = args;
var conversationsMenu = mainMenu.find(e => e.label === 'Conversations');
var contactsMenu = mainMenu.find(e => e.label === 'Contacts');
var shouldUpdate = false;

contacts = JSON.parse(contacts);
conversations = JSON.parse(conversations);
contacts = JSON.parse(contacts);

if (conversations.length) {
shouldUpdate = true;

conversations = await Promise.all(
conversations.map(async(e, index) => {
return {
label: e.RemarkName || e.NickName,
accelerator: `Cmd+${index}`,
icon: await getIcon(cookies, e.UserName, e.HeadImgUrl),
click() {
mainWindow.show();
mainWindow.webContents.send('message-chatto', {
id: e.UserName,
});
}
};
})
);
conversationsMenu.submenu = conversations;
}

if (contacts.length) {
shouldUpdate = true;

contacts = await Promise.all(
contacts.map(async e => {
return {
label: e.RemarkName || e.NickName,
icon: await getIcon(cookies, e.UserName, e.HeadImgUrl),
click() {
mainWindow.show();
mainWindow.webContents.send('show-userinfo', {
id: e.UserName,
});
}
};
})
);
contactsMenu.submenu = contacts;
}

conversations = await Promise.all(
conversations.map(async(e, index) => {
return {
label: e.RemarkName || e.NickName,
accelerator: `Cmd+${index}`,
icon: await getIcon(cookies, e.UserName, e.HeadImgUrl),
click() {
mainWindow.show();
mainWindow.webContents.send('message-chatto', {
id: e.UserName,
});
}
};
})
);

contacts = await Promise.all(
contacts.map(async e => {
return {
label: e.RemarkName || e.NickName,
icon: await getIcon(cookies, e.UserName, e.HeadImgUrl),
click() {
mainWindow.show();
mainWindow.webContents.send('show-userinfo', {
id: e.UserName,
});
}
};
})
);

mainMenu.find(e => e.label === 'Conversations').submenu = conversations;
mainMenu.find(e => e.label === 'Contacts').submenu = contacts;

createMenu();
shouldUpdate && createMenu();
});

ipcMain.on('message-unread', (event, args) => {
Expand Down
23 changes: 17 additions & 6 deletions src/js/stores/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ function hasUnreadMessage(messages) {
});
}

async function updateMenus(sessions) {
async function updateMenus({ conversations = [], contacts = [] }) {
ipcRenderer.send('menu-update', {
conversations: JSON.stringify(sessions),
contacts: JSON.stringify(contacts.memberList),
conversations: JSON.stringify(conversations),
contacts: JSON.stringify(contacts),
cookies: await helper.getCookie(),
});
}
Expand Down Expand Up @@ -240,7 +240,10 @@ class Chat {
});

self.sessions.replace(sorted);
updateMenus(self.sessions);
updateMenus({
conversations: self.sessions.slice(0, 10),
contacts: contacts.memberList,
});
return res;
}

Expand Down Expand Up @@ -402,7 +405,9 @@ class Chat {
self.messages.set(from, list);

hasUnreadMessage(self.messages);
updateMenus(self.sessions);
updateMenus({
conversations: self.sessions.slice(0, 10),
});
}

@action async sendTextMessage(auth, message, isForward) {
Expand Down Expand Up @@ -946,7 +951,6 @@ class Chat {
}

self.messages.set(userid, list);
updateMenus(self.sessions);
}

@action async sticky(user) {
Expand Down Expand Up @@ -976,6 +980,9 @@ class Chat {
});
self.sessions.replace(sorted);

updateMenus({
conversations: sorted.slice(0, 10)
});
return true;
}

Expand All @@ -985,6 +992,10 @@ class Chat {
@action removeChat(user) {
var sessions = self.sessions.filter(e => e.UserName !== user.UserName);
self.sessions.replace(sessions);

updateMenus({
conversations: sessions.slice(0, 10)
});
}

@action empty(user) {
Expand Down

0 comments on commit d34636a

Please sign in to comment.