Skip to content

Instantly share code, notes, and snippets.

@lexfrl
Created July 27, 2015 10:28
Show Gist options
  • Save lexfrl/6814a72f0a29fd262556 to your computer and use it in GitHub Desktop.
Save lexfrl/6814a72f0a29fd262556 to your computer and use it in GitHub Desktop.
const ObjectID = mongodb.ObjectID;
var cacheVk = {};
/**
* @TODO: add token refresh logicб fallback ect
*/
export function vk (userId, db = null, f = null, collName = "users", tokPath = "vk.session.sid") {
let project = {};
project[tokPath] = 1;
return f && f(db, userId, collName, tokPath) ||
_.isObject(userId) && !db && P.promisify(vkontakte(_.get(userId, tokPath))) ||
cacheVk[userId] && new P.promisify(vkontakte(cacheVk[userId])) ||
db.collection(collName)
.findOneAsync({_id: new ObjectID(userId)}, project)
.then((res) => {
let token = _.get(res, tokPath);
if (typeof token === "string") {
cacheVk[userId] = token;
}
return P.promisify(vkontakte(token));
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment