summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/channels.go
blob: 5779cb388dabc5c2ee5efcce2466772c5f3c6fcf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package realtime

import (
	"github.com/Jeffail/gabs"
	"github.com/matterbridge/Rocket.Chat.Go.SDK/models"
)

func (c *Client) GetChannelId(name string) (string, error) {
	rawResponse, err := c.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 *Client) GetChannelsIn() ([]models.Channel, error) {
	rawResponse, err := c.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()

	var channels []models.Channel

	for _, i := range chans {
		channels = append(channels, models.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 *Client) GetChannelSubscriptions() ([]models.ChannelSubscription, error) {
	rawResponse, err := c.ddp.Call("subscriptions/get", map[string]int{
		"$date": 0,
	})
	if err != nil {
		return nil, err
	}

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

	channelSubs, err := document.Children()

	var channelSubscriptions []models.ChannelSubscription

	for _, sub := range channelSubs {
		channelSubscription := models.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: models.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 *Client) GetChannelRoles(roomId string) error {
	_, err := c.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 *Client) CreateChannel(name string, users []string) error {
	_, err := c.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 *Client) CreateGroup(name string, users []string) error {
	_, err := c.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 *Client) JoinChannel(roomId string) error {
	_, err := c.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 *Client) LeaveChannel(roomId string) error {
	_, err := c.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 *Client) ArchiveChannel(roomId string) error {
	_, err := c.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 *Client) UnArchiveChannel(roomId string) error {
	_, err := c.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 *Client) DeleteChannel(roomId string) error {
	_, err := c.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 *Client) SetChannelTopic(roomId string, topic string) error {
	_, err := c.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 *Client) SetChannelType(roomId string, roomType string) error {
	_, err := c.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 *Client) SetChannelJoinCode(roomId string, joinCode string) error {
	_, err := c.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 *Client) SetChannelReadOnly(roomId string, readOnly bool) error {
	_, err := c.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 *Client) SetChannelDescription(roomId string, description string) error {
	_, err := c.ddp.Call("saveRoomSettings", roomId, "roomDescription", description)
	if err != nil {
		return err
	}

	return nil
}