From 3cee960e6ce2fc97dd12cae62a74b0702de2bb18 Mon Sep 17 00:00:00 2001 From: Bradley Hilton Date: Fri, 16 Dec 2016 13:59:29 -0600 Subject: [PATCH] Remove some more already added endpoints and move others around --- packages/rocketchat-api/package.js | 1 + .../server/routes.extended.coffee | 125 ------------------ packages/rocketchat-api/server/v1/channels.js | 36 +++++ packages/rocketchat-api/server/v1/groups.js | 37 ++++++ .../rocketchat-api/server/v1/integrations.js | 29 ++++ 5 files changed, 103 insertions(+), 125 deletions(-) create mode 100644 packages/rocketchat-api/server/v1/integrations.js diff --git a/packages/rocketchat-api/package.js b/packages/rocketchat-api/package.js index 2396fc1a4de..bb1c07a0bf6 100644 --- a/packages/rocketchat-api/package.js +++ b/packages/rocketchat-api/package.js @@ -22,6 +22,7 @@ Package.onUse(function(api) { api.addFiles('server/v1/chat.js', 'server'); api.addFiles('server/v1/groups.js', 'server'); api.addFiles('server/v1/im.js', 'server'); + api.addFiles('server/v1/integrations.js', 'server'); api.addFiles('server/v1/misc.js', 'server'); api.addFiles('server/v1/users.js', 'server'); api.addFiles('server/v1/settings.js', 'server'); diff --git a/packages/rocketchat-api/server/routes.extended.coffee b/packages/rocketchat-api/server/routes.extended.coffee index 6ef1284b353..91e952ac3a3 100644 --- a/packages/rocketchat-api/server/routes.extended.coffee +++ b/packages/rocketchat-api/server/routes.extended.coffee @@ -59,99 +59,6 @@ RocketChat.API.v1.addRoute 'room.update', authRequired: true, return RocketChat.API.v1.failure e.name + ': ' + e.message -# Add user to a channel/group -RocketChat.API.v1.addRoute 'room.addUser', authRequired: true, - post: -> - - try - this.response.setTimeout (1000 * @userId.length) - Meteor.runAsUser this.userId, () => - (Meteor.call 'addUserToRoom', rid:@bodyParams.room, username:@bodyParams.username) - catch e - return RocketChat.API.v1.failure e.name + ': ' + e.message - - return RocketChat.API.v1.success - room: @bodyParams.room - user: @bodyParams.username - - -### Remove Rocket.Chatroom. User must also have delete-p role. - -@param `roomId`: ID of the room to be removed -@param `name`: Name of the room to be removed. - -### -RocketChat.API.v1.addRoute 'room.delete', authRequired: true, - post: -> - - if RocketChat.authz.hasPermission(@userId, 'delete-p') is false - return RocketChat.API.v1.unauthorized() - - try - Meteor.runAsUser this.userId, () => - Meteor.call 'eraseRoom', @bodyParams.roomId or @bodyParams.name - - catch e - return RocketChat.API.v1.failure e.name + ': ' + e.message - - return RocketChat.API.v1.success - room: @bodyParams.roomId or @bodyParams.name - - - -### Retrieve messages for a specific room. Requires access to the room. URL parameter accepts the room ID. - -Message history is paginated. Older messages can be retrieved by incrementing the page and items parameters. - -@queryparam page (int, default=1): Page to retrieve -@queryparam items (int, default=100): Number of items to include in each page - -### -RocketChat.API.v1.addRoute 'room/:rid/history', authRequired: true, - get: -> - if not Meteor.call('canAccessRoom', @urlParams.rid, @userId) - return RocketChat.API.v1.unauthorized() - - try - - rpage = if @queryParams.page then Number(@queryParams.page) else 1 - ritems = if @queryParams.items then Number(@queryParams.items) else 100 - msgs = RocketChat.models.Messages.findVisibleByRoomId(@urlParams.rid, - sort: - ts: -1 - skip: (rpage-1)*ritems - limit: ritems - ).fetch() - return RocketChat.API.v1.success - messages: msgs - page: rpage - items: msgs.length - total: RocketChat.models.Messages.findVisibleByRoomId(@urlParams.rid).count() - - catch e - return RocketChat.API.v1.failure e.name + ': ' + e.message - - -# Retrieve message details. Requires access to the room. URL parameters accepts the room ID and the message ID. -RocketChat.API.v1.addRoute 'room/:rid/message/:mid', authRequired: true, - get: -> - if not Meteor.call('canAccessRoom', @urlParams.rid, @userId) - return RocketChat.API.v1.unauthorized() - - try - msg = RocketChat.models.Messages.findOneById(@urlParams.mid) - if !msg - return RocketChat.API.v1.failure('Invalid message ID '+@urlParams.mid) - if msg.rid != @urlParams.rid - return RocketChat.API.v1.unauthorized('Message ' + @urlParams.mid - +' does not belong to room ' + @urlParams.rid) - return RocketChat.API.v1.success - message: msg - - catch e - return RocketChat.API.v1.failure e.name + ': ' + e.message - - ### List direct rooms currently registered with the server. Requires `admin` role. Pagination controlled by the page and items query parameters. Defaults: page=1, items=100. @@ -186,38 +93,6 @@ RocketChat.API.v1.addRoute 'room/direct.list', authRequired: true, catch e return RocketChat.API.v1.failure e.name + ': ' + e.message - -#* Integrations API: Manage *# - - -### Retrieve a paginated list of all integrations. Requires manage-integrations role. - -Pagination controlled by the page and items query parameters. Defaults: page=1, items=100. - -@queryparam page (int, default=1): Page to retrieve -@queryparam items (int, default=100): Number of items to include in each page - -### -RocketChat.API.v1.addRoute 'integrations.list', authRequired: true, - get: -> - - if RocketChat.authz.hasPermission(@userId, 'manage-integrations') is false - return RocketChat.API.v1.unauthorized() - try - this.response.setTimeout (1000) - rpage = if @queryParams.page then Number(@queryParams.page) else 1 - ritems = if @queryParams.items then Number(@queryParams.items) else 100 - integrations = RocketChat.models.Integrations.find({}, { limit: ritems, skip: (rpage-1)*ritems }).fetch() - return RocketChat.API.v1.success - integrations: integrations - page: rpage - items: integrations.length - total: RocketChat.models.Integrations.find().count() - - catch e - return RocketChat.API.v1.failure e.name + ': ' + e.message - - ### Create outgoing webhook. User must have manage-integrations role. ### RocketChat.API.v1.addRoute 'integrations/outgoingWebhook.create', authRequired: true, diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index 3db2253436a..3000a1fe7bc 100644 --- a/packages/rocketchat-api/server/v1/channels.js +++ b/packages/rocketchat-api/server/v1/channels.js @@ -167,6 +167,42 @@ RocketChat.API.v1.addRoute('channels.create', { authRequired: true }, { } }); +RocketChat.API.v1.addRoute('channels.getIntegrations', { authRequired: true }, { + get: function() { + if (!RocketChat.authz.hasPermission(this.userId, 'manage-integrations')) { + return RocketChat.API.v1.unauthorized(); + } + + const findResult = findChannelById(this.queryParams.roomId); + + if (findResult.statusCode) { + return findResult; + } + + let includeAllPublicChannels = true; + if (typeof this.queryParams.includeAllPublicChannels !== 'undefined') { + includeAllPublicChannels = this.queryParams.includeAllPublicChannels === 'true'; + } + + const channelsToSearch = [`#${findResult.name}`]; + if (includeAllPublicChannels) { + channelsToSearch.push('all_public_channels'); + } + + try { + return RocketChat.API.v1.success({ + integrations: RocketChat.models.Integrations.find({ + channel: { + $in: channelsToSearch + } + }).fetch() + }); + } catch (e) { + return RocketChat.API.v1.failure(`${e.name}: ${e.message}`); + } + } +}); + RocketChat.API.v1.addRoute('channels.history', { authRequired: true }, { get: function() { const findResult = findChannelById(this.queryParams.roomId); diff --git a/packages/rocketchat-api/server/v1/groups.js b/packages/rocketchat-api/server/v1/groups.js index d4b03f76088..aa1bf892787 100644 --- a/packages/rocketchat-api/server/v1/groups.js +++ b/packages/rocketchat-api/server/v1/groups.js @@ -94,6 +94,43 @@ RocketChat.API.v1.addRoute('groups.create', { authRequired: true }, { } }); +RocketChat.API.v1.addRoute('groups.getIntegrations', { authRequired: true }, { + get: function() { + if (!RocketChat.authz.hasPermission(this.userId, 'manage-integrations')) { + return RocketChat.API.v1.unauthorized(); + } + + const findResult = findPrivateGroupById(this.queryParams.roomId, this.userId); + + //The find method returns either with the group or the failure + if (findResult.statusCode) { + return findResult; + } + + let includeAllPrivateGroups = true; + if (typeof this.queryParams.includeAllPrivateGroups !== 'undefined') { + includeAllPrivateGroups = this.queryParams.includeAllPrivateGroups === 'true'; + } + + const channelsToSearch = [`#${findResult.name}`]; + if (includeAllPrivateGroups) { + channelsToSearch.push('all_private_groups'); + } + + try { + return RocketChat.API.v1.success({ + integrations: RocketChat.models.Integrations.find({ + channel: { + $in: channelsToSearch + } + }).fetch() + }); + } catch (e) { + return RocketChat.API.v1.failure(`${e.name}: ${e.message}`); + } + } +}); + RocketChat.API.v1.addRoute('groups.history', { authRequired: true }, { get: function() { const findResult = findPrivateGroupById(this.queryParams.roomId, this.userId); diff --git a/packages/rocketchat-api/server/v1/integrations.js b/packages/rocketchat-api/server/v1/integrations.js new file mode 100644 index 00000000000..953df144938 --- /dev/null +++ b/packages/rocketchat-api/server/v1/integrations.js @@ -0,0 +1,29 @@ +RocketChat.API.v1.addRoute('integrations.list', { authRequired: true }, { + get: function() { + if (!RocketChat.authz.hasPermission(this.userId, 'manage-integrations')) { + return RocketChat.API.v1.unauthorized(); + } + + try { + let page = 1; + if (typeof this.queryParams.page !== 'undefined') { + page = parseInt(this.queryParams.page); + } + + let limit = 20; + if (typeof this.queryParams.limit !== 'undefined') { + limit = parseInt(this.queryParams.limit); + } + + const integrations = RocketChat.models.Integrations.find({}, { limit: limit, skip: (page - 1) * limit }).fetch(); + return RocketChat.API.v1.success({ + integrations: integrations, + page: page, + items: integrations.length, + total: RocketChat.models.Integrations.find().count() + }); + } catch (e) { + return RocketChat.API.v1.failure(e.name + ': ' + e.message); + } + } +});