From 522d4e5c8edf6a23ee2b77291ca8cfc8c54ab87f Mon Sep 17 00:00:00 2001 From: Bradley Hilton Date: Fri, 6 Jan 2017 13:39:59 -0200 Subject: [PATCH] Move the api creation from coffee to js --- packages/rocketchat-api/package.js | 3 +- packages/rocketchat-api/server/api.coffee | 99 --------------- packages/rocketchat-api/server/api.js | 139 ++++++++++++++++++++++ 3 files changed, 140 insertions(+), 101 deletions(-) delete mode 100644 packages/rocketchat-api/server/api.coffee create mode 100644 packages/rocketchat-api/server/api.js diff --git a/packages/rocketchat-api/package.js b/packages/rocketchat-api/package.js index b04d76b01cc..30cb29b7077 100644 --- a/packages/rocketchat-api/package.js +++ b/packages/rocketchat-api/package.js @@ -7,14 +7,13 @@ Package.describe({ Package.onUse(function(api) { api.use([ - 'coffeescript', 'underscore', 'ecmascript', 'rocketchat:lib', 'nimble:restivus' ]); - api.addFiles('server/api.coffee', 'server'); + api.addFiles('server/api.js', 'server'); api.addFiles('server/settings.js', 'server'); api.addFiles('server/default/info.js', 'server'); diff --git a/packages/rocketchat-api/server/api.coffee b/packages/rocketchat-api/server/api.coffee deleted file mode 100644 index 485f3ffc839..00000000000 --- a/packages/rocketchat-api/server/api.coffee +++ /dev/null @@ -1,99 +0,0 @@ -class API extends Restivus - constructor: -> - @authMethods = [] - @defaultFieldsToExclude = - joinCode: 0 - $loki: 0 - meta: 0 - super - - addAuthMethod: (method) -> - @authMethods.push method - - success: (result={}) -> - if _.isObject(result) - result.success = true - - return {} = - statusCode: 200 - body: result - - failure: (result, errorType) -> - if _.isObject(result) - result.success = false - else - result = - success: false - error: result - if errorType? - result.errorType = errorType - - return {} = - statusCode: 400 - body: result - - unauthorized: (msg) -> - return {} = - statusCode: 403 - body: - success: false - error: msg or 'unauthorized' - - # If the count query param is higher than the "API_Upper_Count_Limit" setting, then we limit that - # If the count query param isn't defined, then we set it to the "API_Default_Count" setting - # If the count is zero, then that means unlimited and is only allowed if the setting "API_Allow_Infinite_Count" is true - getPaginationItems: (req) -> - hardUpperLimit = if RocketChat.settings.get('API_Upper_Count_Limit') <= 0 then 100 else RocketChat.settings.get('API_Upper_Count_Limit') - defaultCount = if RocketChat.settings.get('API_Default_Count') <= 0 then 50 else RocketChat.settings.get('API_Default_Count') - offset = if req.queryParams.offset then parseInt(req.queryParams.offset) else 0 - # Ensure count is an appropiate amount - if typeof req.queryParams.count != 'undefined' - count = parseInt(req.queryParams.count) - else - count = defaultCount - - if count > hardUpperLimit - count = hardUpperLimit - - if count == 0 and !RocketChat.settings.get('API_Allow_Infinite_Count') - count = defaultCount - - return {} = - offset: offset - count: count - -RocketChat.API = {} - -getUserAuth = -> - return { - token: 'services.resume.loginTokens.hashedToken' - user: -> - if @bodyParams?.payload? - @bodyParams = JSON.parse @bodyParams.payload - - for method in RocketChat.API.v1.authMethods - result = method.apply @, arguments - if result not in [undefined, null, false] - return result - - if @request.headers['x-auth-token'] - token = Accounts._hashLoginToken @request.headers['x-auth-token'] - - return {} = - userId: @request.headers['x-user-id'] - token: token - } - - -RocketChat.API.v1 = new API - version: 'v1' - useDefaultAuth: true - prettyJson: true - enableCors: false - auth: getUserAuth() - -RocketChat.API.default = new API - useDefaultAuth: true - prettyJson: true - enableCors: false - auth: getUserAuth() diff --git a/packages/rocketchat-api/server/api.js b/packages/rocketchat-api/server/api.js new file mode 100644 index 00000000000..8d69f20f90e --- /dev/null +++ b/packages/rocketchat-api/server/api.js @@ -0,0 +1,139 @@ +/* global Restivus */ +class API extends Restivus { + constructor(properties) { + super(properties); + this.authMethods = []; + this.defaultFieldsToExclude = { + joinCode: 0, + $loki: 0, + meta: 0 + }; + } + + addAuthMethod(method) { + this.authMethods.push(method); + } + + success(result={}) { + if (_.isObject(result)) { + result.success = true; + } + + return { + statusCode: 200, + body: result + }; + } + + failure(result, errorType) { + if (_.isObject(result)) { + result.success = false; + } else { + result = { + success: false, + error: result + }; + + if (errorType) { + result.errorType = errorType; + } + } + + return { + statusCode: 400, + body: result + }; + } + + + unauthorized(msg) { + return { + statusCode: 403, + body: { + success: false, + error: msg ? msg : 'unauthorized' + } + }; + } + + // If the count query param is higher than the "API_Upper_Count_Limit" setting, then we limit that + // If the count query param isn't defined, then we set it to the "API_Default_Count" setting + // If the count is zero, then that means unlimited and is only allowed if the setting "API_Allow_Infinite_Count" is true + getPaginationItems(req) { + const hardUpperLimit = RocketChat.settings.get('API_Upper_Count_Limit') <= 0 ? 100 : RocketChat.settings.get('API_Upper_Count_Limit'); + const defaultCount = RocketChat.settings.get('API_Default_Count') <= 0 ? 50 : RocketChat.settings.get('API_Default_Count'); + const offset = req.queryParams.offset ? parseInt(req.queryParams.offset) : 0; + let count = defaultCount; + + // Ensure count is an appropiate amount + if (typeof req.queryParams.count !== 'undefined') { + count = parseInt(req.queryParams.count); + } else { + count = defaultCount; + } + + if (count > hardUpperLimit) { + count = hardUpperLimit; + } + + if (count === 0 && !RocketChat.settings.get('API_Allow_Infinite_Count')) { + count = defaultCount; + } + + return { + offset, + count + }; + } +} + +RocketChat.API = {}; + +const getUserAuth = function _getUserAuth() { + const invalidResults = [undefined, null, false]; + return { + token: 'services.resume.loginTokens.hashedToken', + user: function() { + if (this.bodyParams && this.bodyParams.payload) { + this.bodyParams = JSON.parse(this.bodyParams.payload); + } + + for (let i = 0; i < RocketChat.API.v1.authMethods.length; i++) { + const method = RocketChat.API.v1.authMethods[i]; + + if (typeof method === 'function') { + const result = method.apply(this, arguments); + if (!invalidResults.includes(result)) { + return result; + } + } + } + + let token; + if (this.request.headers['x-auth-token']) { + token = Accounts._hashLoginToken(this.request.headers['x-auth-token']); + } + + return { + userId: this.request.headers['x-user-id'], + token + }; + } + }; +}; + + +RocketChat.API.v1 = new API({ + version: 'v1', + useDefaultAuth: true, + prettyJson: true, + enableCors: false, + auth: getUserAuth() +}); + +RocketChat.API.default = new API({ + useDefaultAuth: true, + prettyJson: true, + enableCors: false, + auth: getUserAuth() +});