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

Commit

Permalink
fix #34 #36
Browse files Browse the repository at this point in the history
  • Loading branch information
trazyn committed Sep 19, 2017
1 parent b853540 commit bb4026e
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 53 deletions.
100 changes: 65 additions & 35 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { app, powerMonitor, BrowserWindow, Tray, Menu, ipcMain, clipboard, shell
import windowStateKeeper from 'electron-window-state';
import AutoLaunch from 'auto-launch';
import { autoUpdater } from 'electron-updater';
import axios from 'axios';

import pkg from './package.json';

Expand Down Expand Up @@ -256,15 +257,40 @@ let mainMenu = [
]
}
];
let avatarPath = tmp.dirSync();
let avatarMap = {};
let avatarPlaceholder = `${__dirname}/src/assets/images/user-fallback.png`;

function getIcon(data) {
var fallback = nativeImage.createFromPath(`${__dirname}/src/assets/images/user-fallback.png`);
var icon = data ? nativeImage.createFromDataURL(data) : fallback;
async function getIcon(cookies, userid, src) {
var icon = src || avatarPlaceholder;

return icon.resize({
width: 24,
height: 24,
});
icon = avatarMap[userid];

if (!icon && src) {
try {
let response = await axios({
url: src,
method: 'get',
responseType: 'stream',
headers: {
Cookie: cookies,
Host: 'wx.qq.com',
Referer: 'https://wx.qq.com/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8',
},
});

icon = `${avatarPath.name}/${userid}.jpg`;
response.data.pipe(fs.createWriteStream(icon));
console.log('write %s', icon);
} catch (ex) {
console.error(ex);
icon = avatarPlaceholder;
}
}

avatarMap[userid] = icon;
return nativeImage.createFromPath(icon).resize({ width: 24, height: 24 });
}

function checkForUpdates() {
Expand Down Expand Up @@ -495,38 +521,42 @@ const createMainWindow = () => {
}
});

ipcMain.on('menu-update', (event, args) => {
var { contacts, conversations } = args;
ipcMain.on('menu-update', async(event, args) => {
var { cookies, contacts, conversations } = args;

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

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

contacts = contacts.map(e => {
return {
label: e.RemarkName || e.NickName,
icon: getIcon(e.icon),
click() {
mainWindow.show();
mainWindow.webContents.send('show-userinfo', {
id: e.UserName,
});
}
};
});
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;
Expand Down
18 changes: 3 additions & 15 deletions src/js/stores/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,10 @@ function hasUnreadMessage(messages) {
}

async function updateMenus(sessions) {
var conversationsWithIcon = await Promise.all(
sessions.map(async e => {
e.icon = await helper.getDataURL(e.HeadImgUrl);
return e;
})
);
var contactsWithIcon = await Promise.all(
contacts.memberList.map(async e => {
e.icon = await helper.getDataURL(e.HeadImgUrl);
return e;
})
);

ipcRenderer.send('menu-update', {
conversations: JSON.stringify(conversationsWithIcon),
contacts: JSON.stringify(contactsWithIcon),
conversations: JSON.stringify(sessions),
contacts: JSON.stringify(contacts.memberList),
cookies: await helper.getCookie(),
});
}

Expand Down
21 changes: 20 additions & 1 deletion src/js/stores/contacts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import { observable, action } from 'mobx';
import { ipcRenderer } from 'electron';
import axios from 'axios';
import pinyin from 'han';

Expand Down Expand Up @@ -153,19 +154,31 @@ class Contacts {
});

if (response.data.BaseResponse.Ret === 0) {
var shouldUpdate = false;

response.data.ContactList.map(e => {
var index = self.memberList.findIndex(user => user.UserName === e.UserName);
var user = self.resolveUser(auth, e);

if (!user) return;

shouldUpdate = true;

if (index !== -1) {
self.memberList[index] = user;
} else {
// This contact is not in your contact list, eg: Temprary chat room
self.memberList.push(user);
}
});

if (shouldUpdate) {
// Update contact in menu
ipcRenderer.send('menu-update', {
contacts: JSON.stringify(self.memberList),
cookies: await helper.getCookie(),
});
}
} else {
throw new Error(`Failed to get user: ${list}`);
}
Expand Down Expand Up @@ -203,8 +216,14 @@ class Contacts {
self.showGroup = showGroup;
}

@action deleteUser(id) {
@action async deleteUser(id) {
self.memberList = self.memberList.filter(e => e.UserName !== id);

// Update contact in menu
ipcRenderer.send('menu-update', {
contacts: JSON.stringify(self.memberList),
cookies: await helper.getCookie(),
});
}

@action async updateUser(user) {
Expand Down
24 changes: 22 additions & 2 deletions src/js/utils/helper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import { remote, ipcRenderer } from 'electron';
import axios from 'axios';

import session from '../stores/session';

Expand Down Expand Up @@ -146,10 +147,29 @@ const helper = {
var value = {
name,
};
var cookies = remote.getCurrentWindow().webContents.session.cookies;

if (!name) {
return new Promise((resolve, reject) => {
cookies.get({ url: axios.defaults.baseURL }, (error, cookies) => {
let string = '';

if (error) {
return resolve('');
}

for (var i = cookies.length; --i >= 0;) {
let item = cookies[i];
string += `${item.name}=${item.value} ;`;
}

resolve(string);
});
});
}

return new Promise((resolve, reject) => {
var session = remote.getCurrentWindow().webContents.session;
session.cookies.get(value, (err, cookies) => {
cookies.get(value, (err, cookies) => {
if (err) {
reject(err);
} else {
Expand Down

0 comments on commit bb4026e

Please sign in to comment.