1package realtime
2
3import (
4	"github.com/Jeffail/gabs"
5	"github.com/matterbridge/Rocket.Chat.Go.SDK/models"
6)
7
8func (c *Client) GetChannelId(name string) (string, error) {
9	rawResponse, err := c.ddp.Call("getRoomIdByNameOrId", name)
10	if err != nil {
11		return "", err
12	}
13
14	//log.Println(rawResponse)
15
16	return rawResponse.(string), nil
17}
18
19// GetChannelsIn returns list of channels
20// Optionally includes date to get all since last check or 0 to get all
21//
22// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/get-rooms/
23func (c *Client) GetChannelsIn() ([]models.Channel, error) {
24	rawResponse, err := c.ddp.Call("rooms/get", map[string]int{
25		"$date": 0,
26	})
27	if err != nil {
28		return nil, err
29	}
30
31	document, _ := gabs.Consume(rawResponse.(map[string]interface{})["update"])
32
33	chans, err := document.Children()
34
35	var channels []models.Channel
36
37	for _, i := range chans {
38		channels = append(channels, models.Channel{
39			ID: stringOrZero(i.Path("_id").Data()),
40			//Default: stringOrZero(i.Path("default").Data()),
41			Name: stringOrZero(i.Path("name").Data()),
42			Type: stringOrZero(i.Path("t").Data()),
43		})
44	}
45
46	return channels, nil
47}
48
49// GetChannelSubscriptions gets users channel subscriptions
50// Optionally includes date to get all since last check or 0 to get all
51//
52// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/get-subscriptions
53func (c *Client) GetChannelSubscriptions() ([]models.ChannelSubscription, error) {
54	rawResponse, err := c.ddp.Call("subscriptions/get", map[string]int{
55		"$date": 0,
56	})
57	if err != nil {
58		return nil, err
59	}
60
61	document, _ := gabs.Consume(rawResponse.(map[string]interface{})["update"])
62
63	channelSubs, err := document.Children()
64
65	var channelSubscriptions []models.ChannelSubscription
66
67	for _, sub := range channelSubs {
68		channelSubscription := models.ChannelSubscription{
69			ID:          stringOrZero(sub.Path("_id").Data()),
70			Alert:       sub.Path("alert").Data().(bool),
71			Name:        stringOrZero(sub.Path("name").Data()),
72			DisplayName: stringOrZero(sub.Path("fname").Data()),
73			Open:        sub.Path("open").Data().(bool),
74			Type:        stringOrZero(sub.Path("t").Data()),
75			RoomId:      stringOrZero(sub.Path("rid").Data()),
76			User: models.User{
77				ID:       stringOrZero(sub.Path("u._id").Data()),
78				UserName: stringOrZero(sub.Path("u.username").Data()),
79			},
80			Unread: sub.Path("unread").Data().(float64),
81		}
82
83		if sub.Path("roles").Data() != nil {
84			var roles []string
85			for _, role := range sub.Path("roles").Data().([]interface{}) {
86				roles = append(roles, role.(string))
87			}
88
89			channelSubscription.Roles = roles
90		}
91
92		channelSubscriptions = append(channelSubscriptions, channelSubscription)
93	}
94
95	return channelSubscriptions, nil
96}
97
98// GetChannelRoles returns room roles
99//
100// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/get-room-roles
101func (c *Client) GetChannelRoles(roomId string) error {
102	_, err := c.ddp.Call("getRoomRoles", roomId)
103	if err != nil {
104		return err
105	}
106
107	return nil
108}
109
110// CreateChannel creates a channel
111// Takes name and users array
112//
113// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/create-channels
114func (c *Client) CreateChannel(name string, users []string) error {
115	_, err := c.ddp.Call("createChannel", name, users)
116	if err != nil {
117		return err
118	}
119
120	return nil
121}
122
123// CreateGroup creates a private group
124// Takes group name and array of users
125//
126// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/create-private-groups
127func (c *Client) CreateGroup(name string, users []string) error {
128	_, err := c.ddp.Call("createPrivateGroup", name, users)
129	if err != nil {
130		return err
131	}
132
133	return nil
134}
135
136// JoinChannel joins a channel
137// Takes roomId
138//
139// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/joining-channels
140func (c *Client) JoinChannel(roomId string) error {
141	_, err := c.ddp.Call("joinRoom", roomId)
142	if err != nil {
143		return err
144	}
145
146	return nil
147}
148
149// LeaveChannel leaves a channel
150// Takes roomId
151//
152// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/leaving-rooms
153func (c *Client) LeaveChannel(roomId string) error {
154	_, err := c.ddp.Call("leaveRoom", roomId)
155	if err != nil {
156		return err
157	}
158
159	return nil
160}
161
162// ArchiveChannel archives the channel
163// Takes roomId
164//
165// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/archive-rooms
166func (c *Client) ArchiveChannel(roomId string) error {
167	_, err := c.ddp.Call("archiveRoom", roomId)
168	if err != nil {
169		return err
170	}
171
172	return nil
173}
174
175// UnArchiveChannel unarchives the channel
176// Takes roomId
177//
178// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/unarchive-rooms
179func (c *Client) UnArchiveChannel(roomId string) error {
180	_, err := c.ddp.Call("unarchiveRoom", roomId)
181	if err != nil {
182		return err
183	}
184
185	return nil
186}
187
188// DeleteChannel deletes the channel
189// Takes roomId
190//
191// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/delete-rooms
192func (c *Client) DeleteChannel(roomId string) error {
193	_, err := c.ddp.Call("eraseRoom", roomId)
194	if err != nil {
195		return err
196	}
197
198	return nil
199}
200
201// SetChannelTopic sets channel topic
202// takes roomId and topic
203//
204// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/save-room-settings
205func (c *Client) SetChannelTopic(roomId string, topic string) error {
206	_, err := c.ddp.Call("saveRoomSettings", roomId, "roomTopic", topic)
207	if err != nil {
208		return err
209	}
210
211	return nil
212}
213
214// SetChannelType sets the channel type
215// takes roomId and roomType
216//
217// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/save-room-settings
218func (c *Client) SetChannelType(roomId string, roomType string) error {
219	_, err := c.ddp.Call("saveRoomSettings", roomId, "roomType", roomType)
220	if err != nil {
221		return err
222	}
223
224	return nil
225}
226
227// SetChannelJoinCode sets channel join code
228// takes roomId and joinCode
229//
230// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/save-room-settings
231func (c *Client) SetChannelJoinCode(roomId string, joinCode string) error {
232	_, err := c.ddp.Call("saveRoomSettings", roomId, "joinCode", joinCode)
233	if err != nil {
234		return err
235	}
236
237	return nil
238}
239
240// SetChannelReadOnly sets channel as read only
241// takes roomId and boolean
242//
243// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/save-room-settings
244func (c *Client) SetChannelReadOnly(roomId string, readOnly bool) error {
245	_, err := c.ddp.Call("saveRoomSettings", roomId, "readOnly", readOnly)
246	if err != nil {
247		return err
248	}
249
250	return nil
251}
252
253// SetChannelDescription sets channels description
254// takes roomId and description
255//
256// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/save-room-settings
257func (c *Client) SetChannelDescription(roomId string, description string) error {
258	_, err := c.ddp.Call("saveRoomSettings", roomId, "roomDescription", description)
259	if err != nil {
260		return err
261	}
262
263	return nil
264}
265