Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RocketChat API Add private channel #3866

Closed
JohnBueno opened this issue Jul 26, 2016 · 4 comments · Fixed by #5140
Closed

RocketChat API Add private channel #3866

JohnBueno opened this issue Jul 26, 2016 · 4 comments · Fixed by #5140

Comments

@JohnBueno
Copy link

Your Rocket.Chat version: Most current

I am looking at using the RocketChat API as documented here: https://rocket.chat/docs/developer-guides/rest-api/

I had 2 questions:

  1. This document references both "channels" and "rooms" are channels and rooms the same thing?
    ex: "Join a room" vs. "Create a channel"
  2. I see a channel creation endpoint (http://localhost:3000/api/v1/channels.create) when this creates a channel I believe it is public. Is it possible to pass a flag to make channels private when they are created via the API?
@JamesHGreen
Copy link
Contributor

just another dev here but yes you can use api/v1/groups.create it passes it a p flag makeing it a private channel other api methods are here (packages/rocketchat-api/server/routes.coffee. hope that helps

@ryannealmes
Copy link

I was confused about this as well. I am working on my own version of the API (more restful) for our project. Still figuring everything out. Anyway, if you want you can create a separate package for an API of your own (not ideal). So far I have gotten the code below to do what I want, but you could always modify it.

room_types = { 
  private: 'p',
  public: 'c',
  direct_message: 'd'
}

RocketChat.API.v2.addRoute 'rooms', authRequired: true,
  get: ->
    params = {}

    if RocketChat.authz.hasRole( this.userId, 'admin') is not true
      return RocketChat.API.v2.unauthorized()

    return { groups: RocketChat.models.Rooms.find().fetch() }

  post: ->
    if RocketChat.authz.hasRole( this.userId, 'admin') is not true
      return RocketChat.API.v2.unauthorized()

    if not @bodyParams.name?
      return RocketChat.API.v2.failure 'Body param "name" is required'

    if not @bodyParams.type?
      return RocketChat.API.v2.failure 'Body param "type" is required'

    id = undefined

    try
      if @bodyParams.type == 'p'
        Meteor.runAsUser this.userId, =>
          id = Meteor.call 'createPrivateGroup', @bodyParams.name, []
      else if @bodyParams.type == 'c'
        Meteor.runAsUser this.userId, =>
          id = Meteor.call 'createChannel', @bodyParams.name, []
      else if @bodyParams.type == 'd'
        return RocketChat.API.v2.failure 'API does not allow for direct message creation'
      else
        return RocketChat.API.v2.failure 'Invalid room type'
    catch e
      return RocketChat.API.v2.failure e.name + ': ' + e.message

    return RocketChat.API.v2.success
      room: RocketChat.models.Rooms.findOne({_id: id.rid})

RocketChat.API.v2.addRoute 'rooms/:type', authRequired: true,
  get: ->
    params = {}

    if RocketChat.authz.hasRole( this.userId, 'admin') is not true
      return RocketChat.API.v2.unauthorized()

    if not @urlParams.type?
      return RocketChat.API.v2.failure 'Query param "type" is required'

    return { rooms: RocketChat.models.Rooms.find({t: room_types[@urlParams.type]}).fetch() }    

Hoping I can make some useful contributions if I get somewhere concrete with the API. Still new to meteor.

@graywolf336
Copy link
Contributor

@ryannealmes Any new contributions please try and use es6 instead of coffescript. Also, feel free to join us in the #dev channel on the demo server so that we can chat about flushing out the v2 rest api as we would like to make it "Restful".

@graywolf336
Copy link
Contributor

As for the original message, it is possible now via the rest api to create private groups. https://rocket.chat/docs/developer-guides/rest-api/groups will have the details of it here in a day or two as I'm still working on finishing the rest api documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants