1package rest
2
3import (
4	"bytes"
5	"fmt"
6	"net/url"
7
8	"github.com/matterbridge/Rocket.Chat.Go.SDK/models"
9)
10
11type ChannelsResponse struct {
12	Status
13	models.Pagination
14	Channels []models.Channel `json:"channels"`
15}
16
17type ChannelResponse struct {
18	Status
19	Channel models.Channel `json:"channel"`
20}
21
22type GroupsResponse struct {
23	Status
24	models.Pagination
25	Groups []models.Channel `json:"groups"`
26}
27
28type GroupResponse struct {
29	Status
30	Group models.Channel `json:"group"`
31}
32
33// GetPublicChannels returns all channels that can be seen by the logged in user.
34//
35// https://rocket.chat/docs/developer-guides/rest-api/channels/list
36func (c *Client) GetPublicChannels() (*ChannelsResponse, error) {
37	response := new(ChannelsResponse)
38	if err := c.Get("channels.list", nil, response); err != nil {
39		return nil, err
40	}
41
42	return response, nil
43}
44
45// GetPrivateGroups returns all channels that can be seen by the logged in user.
46//
47// https://rocket.chat/docs/developer-guides/rest-api/groups/list
48func (c *Client) GetPrivateGroups() (*GroupsResponse, error) {
49	response := new(GroupsResponse)
50	if err := c.Get("groups.list", nil, response); err != nil {
51		return nil, err
52	}
53
54	return response, nil
55}
56
57
58// GetJoinedChannels returns all channels that the user has joined.
59//
60// https://rocket.chat/docs/developer-guides/rest-api/channels/list-joined
61func (c *Client) GetJoinedChannels(params url.Values) (*ChannelsResponse, error) {
62	response := new(ChannelsResponse)
63	if err := c.Get("channels.list.joined", params, response); err != nil {
64		return nil, err
65	}
66
67	return response, nil
68}
69
70// LeaveChannel leaves a channel. The id of the channel has to be not nil.
71//
72// https://rocket.chat/docs/developer-guides/rest-api/channels/leave
73func (c *Client) LeaveChannel(channel *models.Channel) error {
74	var body = fmt.Sprintf(`{ "roomId": "%s"}`, channel.ID)
75	return c.Post("channels.leave", bytes.NewBufferString(body), new(ChannelResponse))
76}
77
78// GetChannelInfo get information about a channel. That might be useful to update the usernames.
79//
80// https://rocket.chat/docs/developer-guides/rest-api/channels/info
81func (c *Client) GetChannelInfo(channel *models.Channel) (*models.Channel, error) {
82	response := new(ChannelResponse)
83	switch {
84	case channel.Name != "" && channel.ID == "":
85		if err := c.Get("channels.info", url.Values{"roomName": []string{channel.Name}}, response); err != nil {
86			return nil, err
87		}
88	default:
89		if err := c.Get("channels.info", url.Values{"roomId": []string{channel.ID}}, response); err != nil {
90			return nil, err
91		}
92	}
93
94	return &response.Channel, nil
95}
96
97// GetGroupInfo get information about a group. That might be useful to update the usernames.
98//
99// https://rocket.chat/docs/developer-guides/rest-api/groups/info
100func (c *Client) GetGroupInfo(channel *models.Channel) (*models.Channel, error) {
101	response := new(GroupResponse)
102	switch {
103	case channel.Name != "" && channel.ID == "":
104		if err := c.Get("groups.info", url.Values{"roomName": []string{channel.Name}}, response); err != nil {
105			return nil, err
106		}
107	default:
108		if err := c.Get("groups.info", url.Values{"roomId": []string{channel.ID}}, response); err != nil {
109			return nil, err
110		}
111	}
112	return &response.Group, nil
113}
114