Skip to content

Instantly share code, notes, and snippets.

@lexfrl
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save lexfrl/bb2f725ee609c938d240 to your computer and use it in GitHub Desktop.

Select an option

Save lexfrl/bb2f725ee609c938d240 to your computer and use it in GitHub Desktop.
import { fbAccessToken } from "../config.js"
import assert from "assert";
import vkontakte from "vkontakte";
import Promise from "bluebird";
const join = Promise.join;
export default {
name: "users",
read(req, resource, params, config, done) {
//console.log(resource, params, config);
done(null, config);
},
/**
* Users collection: {_id, vk: {}, fb: {}, tw: {}, created, updated}
*/
update(req, resource, params, body, config, done) {
const users = req.db.collection('users');
var query = {};
var data = {};
if (params.session && params.session.mid) { // vk.com response checker
query = {
vkId: params.session.mid
};
data = {
vkId: params.session.mid,
vk: params
}
}
const vkAsync = Promise.promisify(vkontakte(params.session.sid));
join(users.updateAsync(
query, data, {
upsert: true
}),
vkAsync('friends.get', {
fields: 'uid,first_name,last_name,photo'
}), (res, friends) => { // TODO: throw if no res.value._id
users.findOneAndUpdateAsync(query, {$set: {"vk.friends": friends}})
.then((res) => {
let result = res.value;
result.vk.friends = friends;
done(null, result);
})
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment