Skip to content
Closed
48 changes: 48 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
ifeq ($(GOPATH),)
GOPATH := $(HOME)/go
endif

#### VARIABLES ####
SHELL := /bin/bash


# Go parameters
GOCMD = go
GOCLEAN = $(GOCMD) clean
GOTEST = $(GOCMD) test
GOMETALINTER := $(BIN_DIR)/gometalinter
RICHGO := $(BIN_DIR)/richgo
CILINT := $(BIN_DIR)/golangci-lint
DEP := $(BIN_DIR)/dep
SRC = $(shell find . -type f -name '*.go' -not -path "./vendor/*" -not -name '*test.go' )

#### RULES ####
#GNU meaning of all is to compile application and be the default. Prob shouldn't run
.PHONY: all
all: lint test

.PHONY: test
test: $(RICHGO)
TZ="Australia/Brisbane" RICHGO_FORCE_COLOR=1 RICHGO_LOCAL=1 richgo test -v -cover

$(RICHGO):
go get -u github.com/kyoh86/richgo
go install github.com/kyoh86/richgo

$(GOMETALINTER):
go get -u github.com/alecthomas/gometalinter
gometalinter --install

$(CILINT):
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint

.PHONY: lint
lint: $(CILINT)
golangci-lint run ./...

$(DEP):
go get -u github.com/golang/dep/cmd/dep

.PHONY: dep
dep: $(DEP)
dep ensure
334 changes: 334 additions & 0 deletions channels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
package goRocket

import (
"bytes"
"fmt"
"log"
"net/url"

"github.com/Jeffail/gabs"
)

// ChannelsResponse used when returning channel lists
type ChannelsResponse struct {
Status
Pagination
Channels []Channel `json:"channels"`
}

// ChannelResponse on a single channel
type ChannelResponse struct {
Status
Channel Channel `json:"channel"`
}

// GetPublicChannels returns all channels that can be seen by the logged in user.
//
// https://rocket.chat/docs/developer-guides/rest-api/channels/list
func (c *RestService) GetPublicChannels() (*ChannelsResponse, error) {
response := new(ChannelsResponse)
if err := c.Get("channels.list", nil, response); err != nil {
return nil, err
}

return response, nil
}

// GetJoinedChannels returns all channels that the user has joined.
//
// https://rocket.chat/docs/developer-guides/rest-api/channels/list-joined
func (c *RestService) GetJoinedChannels(params url.Values) (*ChannelsResponse, error) {
response := new(ChannelsResponse)
if err := c.Get("channels.list.joined", params, response); err != nil {
return nil, err
}

return response, nil
}

// LeaveChannel leaves a channel. The id of the channel has to be not nil.
//
// https://rocket.chat/docs/developer-guides/rest-api/channels/leave
func (c *RestService) LeaveChannel(channel *Channel) error {
var body = fmt.Sprintf(`{ "roomId": "%s"}`, channel.ID)
return c.Post("channels.leave", bytes.NewBufferString(body), new(ChannelResponse))
}

// GetChannelInfo get information about a channel. That might be useful to update the usernames.
//
// https://rocket.chat/docs/developer-guides/rest-api/channels/info
func (c *RestService) GetChannelInfo(channel *Channel) (*Channel, error) {
response := new(ChannelResponse)
if err := c.Get("channels.info", url.Values{"roomId": []string{channel.ID}}, response); err != nil {
return nil, err
}

return &response.Channel, nil
}

// GetChannelID ..
func (c *LiveService) GetChannelID(name string) (string, error) {
rawResponse, err := c.client.ddp.Call("getRoomIdByNameOrId", name)
if err != nil {
return "", err
}

log.Println(rawResponse)

return rawResponse.(string), nil
}

// GetChannelsIn returns list of channels
// Optionally includes date to get all since last check or 0 to get all
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/get-rooms/
func (c *LiveService) GetChannelsIn() ([]Channel, error) {
rawResponse, err := c.client.ddp.Call("rooms/get", map[string]int{
"$date": 0,
})
if err != nil {
return nil, err
}

document, _ := gabs.Consume(rawResponse.(map[string]interface{})["update"])

chans, err := document.Children()
if err != nil {
log.Println(err)
}

var channels []Channel

for _, i := range chans {
channels = append(channels, Channel{
ID: stringOrZero(i.Path("_id").Data()),
//Default: stringOrZero(i.Path("default").Data()),
Name: stringOrZero(i.Path("name").Data()),
Type: stringOrZero(i.Path("t").Data()),
})
}

return channels, nil
}

// GetChannelSubscriptions gets users channel subscriptions
// Optionally includes date to get all since last check or 0 to get all
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/get-subscriptions
func (c *LiveService) GetChannelSubscriptions() ([]ChannelSubscription, error) {
rawResponse, err := c.client.ddp.Call("subscriptions/get", map[string]int{
"$date": 0,
})
if err != nil {
return nil, err
}

document, err := gabs.Consume(rawResponse.(map[string]interface{})["update"])
if err != nil {
log.Println(err)
}

channelSubs, err := document.Children()
if err != nil {
log.Println(err)
}

var channelSubscriptions []ChannelSubscription

for _, sub := range channelSubs {
channelSubscription := ChannelSubscription{
ID: stringOrZero(sub.Path("_id").Data()),
Alert: sub.Path("alert").Data().(bool),
Name: stringOrZero(sub.Path("name").Data()),
DisplayName: stringOrZero(sub.Path("fname").Data()),
Open: sub.Path("open").Data().(bool),
Type: stringOrZero(sub.Path("t").Data()),
User: User{
ID: stringOrZero(sub.Path("u._id").Data()),
UserName: stringOrZero(sub.Path("u.username").Data()),
},
Unread: sub.Path("unread").Data().(float64),
}

if sub.Path("roles").Data() != nil {
var roles []string
for _, role := range sub.Path("roles").Data().([]interface{}) {
roles = append(roles, role.(string))
}

channelSubscription.Roles = roles
}

channelSubscriptions = append(channelSubscriptions, channelSubscription)
}

return channelSubscriptions, nil
}

// GetChannelRoles returns room roles
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/get-room-roles
func (c *LiveService) GetChannelRoles(roomID string) error {
_, err := c.client.ddp.Call("getRoomRoles", roomID)
if err != nil {
return err
}

return nil
}

// CreateChannel creates a channel
// Takes name and users array
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/create-channels
func (c *LiveService) CreateChannel(name string, users []string) error {
_, err := c.client.ddp.Call("createChannel", name, users)
if err != nil {
return err
}

return nil
}

// CreateGroup creates a private group
// Takes group name and array of users
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/create-private-groups
func (c *LiveService) CreateGroup(name string, users []string) error {
_, err := c.client.ddp.Call("createPrivateGroup", name, users)
if err != nil {
return err
}

return nil
}

// JoinChannel joins a channel
// Takes roomID
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/joining-channels
func (c *LiveService) JoinChannel(roomID string) error {
_, err := c.client.ddp.Call("joinRoom", roomID)
if err != nil {
return err
}

return nil
}

// LeaveChannel leaves a channel
// Takes roomID
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/leaving-rooms
func (c *LiveService) LeaveChannel(roomID string) error {
_, err := c.client.ddp.Call("leaveRoom", roomID)
if err != nil {
return err
}

return nil
}

// ArchiveChannel archives the channel
// Takes roomID
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/archive-rooms
func (c *LiveService) ArchiveChannel(roomID string) error {
_, err := c.client.ddp.Call("archiveRoom", roomID)
if err != nil {
return err
}

return nil
}

// UnArchiveChannel unarchives the channel
// Takes roomID
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/unarchive-rooms
func (c *LiveService) UnArchiveChannel(roomID string) error {
_, err := c.client.ddp.Call("unarchiveRoom", roomID)
if err != nil {
return err
}

return nil
}

// DeleteChannel deletes the channel
// Takes roomID
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/delete-rooms
func (c *LiveService) DeleteChannel(roomID string) error {
_, err := c.client.ddp.Call("eraseRoom", roomID)
if err != nil {
return err
}

return nil
}

// SetChannelTopic sets channel topic
// takes roomID and topic
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/save-room-settings
func (c *LiveService) SetChannelTopic(roomID string, topic string) error {
_, err := c.client.ddp.Call("saveRoomSettings", roomID, "roomTopic", topic)
if err != nil {
return err
}

return nil
}

// SetChannelType sets the channel type
// takes roomID and roomType
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/save-room-settings
func (c *LiveService) SetChannelType(roomID string, roomType string) error {
_, err := c.client.ddp.Call("saveRoomSettings", roomID, "roomType", roomType)
if err != nil {
return err
}

return nil
}

// SetChannelJoinCode sets channel join code
// takes roomID and joinCode
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/save-room-settings
func (c *LiveService) SetChannelJoinCode(roomID string, joinCode string) error {
_, err := c.client.ddp.Call("saveRoomSettings", roomID, "joinCode", joinCode)
if err != nil {
return err
}

return nil
}

// SetChannelReadOnly sets channel as read only
// takes roomID and boolean
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/save-room-settings
func (c *LiveService) SetChannelReadOnly(roomID string, readOnly bool) error {
_, err := c.client.ddp.Call("saveRoomSettings", roomID, "readOnly", readOnly)
if err != nil {
return err
}

return nil
}

// SetChannelDescription sets channels description
// takes roomID and description
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/save-room-settings
func (c *LiveService) SetChannelDescription(roomID string, description string) error {
_, err := c.client.ddp.Call("saveRoomSettings", roomID, "roomDescription", description)
if err != nil {
return err
}

return nil
}
Loading