UNPKG

pai-net

Version:
211 lines (167 loc) 5.17 kB
const { PAICodeCommand, PAICodeModule } = require('@pai-tech/pai-code'); const { /* Models */ User, Bot, BotAvatar, BotMessage, /* API Gateway */ UsersApi, UserBotsApi, BotAvatarsApi, BotMessagesApi, /* OAuth */ PAIOAuthClient, OAuthAccessToken, PasswordGrantType, BotGrantType, OAuthApi, OAuthManager } = require('@pai-tech/pai-net-sdk'); class PCM_PAI_NET extends PAICodeModule { constructor() { let infoText = ` welcome to pai-net: functions: 1. login(username,password) return success 2. bot-login(username,password,bot) return success 3. get-user() return user {required: login()} 4. get-bot() return bot {required: bot-login()} `; super(infoText); this.OAuthManager = null; const CLIENT_ID = 'TEST_CLIENT_ID_FOR_DEV'; const CLIENT_SECRET = 'TEST_CLIENT_SECRET_FOR_DEV'; // create PAI Client let client = new PAIOAuthClient(CLIENT_ID,CLIENT_SECRET,null); this.oAuthClient = client; this.apis = { users: new UsersApi(), bots: null, avatars: new BotAvatarsApi(), messages: new BotMessagesApi() }; this.user = null; this.bot = null; } get user() { return this._user; } set user(user) { this._user = user; if(user) { this.apis.bots = new UserBotsApi(user._id); this.apis.bots.authorize(this.OAuthManager); } } /** * load basic module commands from super * and load all the functions for this module */ load() { super.load(this); this.load_method_with_command("login",'login'); this.load_method_with_command("bot-login",'bot_login'); this.load_method_with_command("get-user",'get_user'); this.load_method_with_command("get-bot",'get_bot'); } /** * * @param {PAICodeCommand} cmd * @return {Promise<Boolean>} success */ login(cmd) { return new Promise( async (resolve,reject) => { let username = cmd.params["2"].value; let password = cmd.params["3"].value; // Create grant type let grantType = new PasswordGrantType(username,password); // init OAuth manager let oAuthManager = new OAuthManager(this.oAuthClient,grantType); let accessToken = await oAuthManager.getAccessToken(); let success = (accessToken && accessToken.access_token != null); if(success) { this.OAuthManager = oAuthManager; await this.authorized_apis(); this.user = await this.apis.users.me(); } return resolve(success); }); } /** * * @param {PAICodeCommand} cmd * @return {Promise<Boolean>} success */ bot_login(cmd) { return new Promise( async (resolve,reject) => { let username = cmd.params["2"].value; let password = cmd.params["3"].value; let botId = cmd.params["4"].value; // Create grant type let grantType = new BotGrantType(username,password,botId); // init OAuth manager let oAuthManager = new OAuthManager(this.oAuthClient,grantType); let accessToken = await oAuthManager.getAccessToken(); let success = (accessToken && accessToken.access_token != null); if(success) { this.OAuthManager = oAuthManager; await this.authorized_apis(); this.user = await this.apis.users.me(); this.bot = await this.apis.bots.me(); } return resolve(success); }); } /** * */ async authorized_apis(){ for (var key in this.apis) { let api = this.apis[key]; if(api) api.authorize(this.OAuthManager); } } /** * * @param {PAICodeCommand} cmd * @return {Promise<User>} */ get_user(cmd) { return new Promise( async (resolve,reject) => { if(!this.user) { this.user = await this.apis.users.me(); } resolve(this.user); }); } /** * * @param {PAICodeCommand} cmd * @return {Promise<Bot>} */ get_bot(cmd) { return new Promise( async (resolve,reject) => { if(!this.bot) { this.bot = this.apis.bots.me(); if(!this.user) this.user = this.apis.users.me(); } resolve(this.bot); }); } } module.exports = PCM_PAI_NET;