Created
July 27, 2015 10:28
-
-
Save lexfrl/6814a72f0a29fd262556 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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