Move the api creation from coffee to js

pull/5411/head
Bradley Hilton 9 years ago
parent 71d7dc8233
commit 522d4e5c8e
No known key found for this signature in database
GPG Key ID: 0666B2C24C43C358
  1. 3
      packages/rocketchat-api/package.js
  2. 99
      packages/rocketchat-api/server/api.coffee
  3. 139
      packages/rocketchat-api/server/api.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');

@ -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()

@ -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()
});
Loading…
Cancel
Save