1
0
mirror of https://github.com/badkaktus/gorocket.git synced 2024-12-12 11:15:05 +02:00
gorocket/channels.go

587 lines
13 KiB
Go
Raw Normal View History

2020-08-24 23:21:08 +02:00
package gorocket
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
type AddAllRequest struct {
RoomId string `json:"roomId"`
ActiveUsersOnly bool `json:"activeUsersOnly,omitempty"`
}
type AddAllResponse struct {
2022-09-01 15:22:58 +02:00
Channel Channel `json:"channel"`
2020-08-24 23:21:08 +02:00
Success bool `json:"success"`
}
2022-09-01 15:22:58 +02:00
type Channel struct {
2020-08-24 23:21:08 +02:00
ID string `json:"_id"`
Name string `json:"name"`
T string `json:"t"`
Usernames []string `json:"usernames"`
Msgs int `json:"msgs"`
2022-09-01 15:22:58 +02:00
U U `json:"u"`
2020-08-24 23:21:08 +02:00
Ts time.Time `json:"ts"`
}
type SimpleChannelId struct {
RoomId string `json:"roomId,omitempty"`
}
type ChannelCountersRequest struct {
RoomId string
RoomName string
UserId string
}
type ChannelCountersResponse struct {
Joined bool `json:"joined"`
Members int `json:"members"`
Unreads int `json:"unreads"`
UnreadsFrom time.Time `json:"unreadsFrom"`
Msgs int `json:"msgs"`
Latest time.Time `json:"latest"`
UserMentions int `json:"userMentions"`
Success bool `json:"success"`
}
type CreateChannelRequest struct {
Name string `json:"name"`
Members []string `json:"members,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
}
type CreateChannelResponse struct {
2022-09-01 15:22:58 +02:00
Channel Channel `json:"channel"`
2020-08-24 23:21:08 +02:00
Success bool `json:"success"`
}
2020-08-28 22:54:20 +02:00
type SimpleChannelRequest struct {
RoomId string `json:"roomId,omitempty"`
RoomName string `json:"roomName,omitempty"`
}
type ChannelHistoryRequest struct {
RoomId string
Latest time.Time
Oldest time.Time
Inclusive bool
Offset int
Count int
Unreads bool
}
type ChannelInfoResponse struct {
2022-09-01 15:22:58 +02:00
Channel ChannelInfo `json:"channel"`
2020-08-28 22:54:20 +02:00
Success bool `json:"success"`
}
2022-09-01 15:22:58 +02:00
type ChannelInfo struct {
2020-08-28 22:54:20 +02:00
ID string `json:"_id"`
Name string `json:"name"`
Fname string `json:"fname"`
T string `json:"t"`
Msgs int `json:"msgs"`
UsersCount int `json:"usersCount"`
2022-09-01 15:22:58 +02:00
U UChat `json:"u"`
2020-08-28 22:54:20 +02:00
CustomFields struct {
} `json:"customFields"`
Broadcast bool `json:"broadcast"`
Encrypted bool `json:"encrypted"`
Ts time.Time `json:"ts"`
Ro bool `json:"ro"`
Default bool `json:"default"`
SysMes bool `json:"sysMes"`
UpdatedAt time.Time `json:"_updatedAt"`
}
type InviteChannelRequest struct {
RoomId string `json:"roomId"`
UserId string `json:"userId"`
}
type InviteChannelResponse struct {
Channel struct {
ID string `json:"_id"`
Ts time.Time `json:"ts"`
T string `json:"t"`
Name string `json:"name"`
Usernames []string `json:"usernames"`
Msgs int `json:"msgs"`
UpdatedAt time.Time `json:"_updatedAt"`
Lm time.Time `json:"lm"`
} `json:"channel"`
Success bool `json:"success"`
}
type ChannelListResponse struct {
2022-09-01 15:22:58 +02:00
Channels []ChannelList `json:"channels"`
2020-08-28 22:54:20 +02:00
Offset int `json:"offset"`
Count int `json:"count"`
Total int `json:"total"`
Success bool `json:"success"`
}
2022-09-01 15:22:58 +02:00
type ChannelList struct {
2020-08-28 22:54:20 +02:00
ID string `json:"_id"`
Name string `json:"name"`
T string `json:"t"`
Usernames []string `json:"usernames"`
Msgs int `json:"msgs"`
2022-09-01 15:22:58 +02:00
U UChat `json:"u"`
2020-08-28 22:54:20 +02:00
Ts time.Time `json:"ts"`
Ro bool `json:"ro"`
SysMes bool `json:"sysMes"`
UpdatedAt time.Time `json:"_updatedAt"`
}
type ChannelMembersResponse struct {
2022-09-01 15:22:58 +02:00
Members []Member `json:"members"`
2020-08-28 22:54:20 +02:00
Count int `json:"count"`
Offset int `json:"offset"`
Total int `json:"total"`
Success bool `json:"success"`
}
2022-09-01 15:22:58 +02:00
type Member struct {
2020-08-28 22:54:20 +02:00
ID string `json:"_id"`
Username string `json:"username"`
Name string `json:"name"`
Status string `json:"status"`
}
2020-08-29 21:56:09 +02:00
type RenameChannelRequest struct {
RoomId string `json:"roomId"`
NewName string `json:"name"`
}
type RenameChannelResponse struct {
2022-09-01 15:22:58 +02:00
Channel ChannelList `json:"channel"`
2020-08-29 21:56:09 +02:00
Success bool `json:"success"`
}
type SetAnnouncementRequest struct {
RoomId string `json:"roomId"`
Announcement string `json:"announcement"`
}
type SetAnnouncementResponse struct {
Announcement string `json:"announcement"`
Success bool `json:"success"`
}
type SetDescriptionRequest struct {
RoomId string `json:"roomId"`
Description string `json:"description"`
}
type SetDescriptionResponse struct {
Description string `json:"description"`
Success bool `json:"success"`
}
type SetTopicRequest struct {
RoomId string `json:"roomId"`
Topic string `json:"topic"`
}
type SetTopicResponse struct {
Topic string `json:"topic"`
Success bool `json:"success"`
}
2024-02-03 21:54:40 +02:00
// AddAllToChannel adds all of the users on the server to a channel.
2020-08-24 23:21:08 +02:00
func (c *Client) AddAllToChannel(params *AddAllRequest) (*AddAllResponse, error) {
opt, _ := json.Marshal(params)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/channels.addAll", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := AddAllResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
2024-02-03 21:54:40 +02:00
// ArchiveChannel archives a channel.
2020-08-24 23:21:08 +02:00
func (c *Client) ArchiveChannel(param *SimpleChannelId) (*SimpleSuccessResponse, error) {
opt, _ := json.Marshal(param)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/channels.archive", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := SimpleSuccessResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
2024-02-03 21:54:40 +02:00
// CloseChannel removes the channel from the user's list of channels.
2020-08-24 23:21:08 +02:00
func (c *Client) CloseChannel(param *SimpleChannelId) (*SimpleSuccessResponse, error) {
opt, _ := json.Marshal(param)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/channels.close", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := SimpleSuccessResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
2024-02-03 21:54:40 +02:00
// ChannelCounters gets channel counters.
2020-08-24 23:21:08 +02:00
func (c *Client) ChannelCounters(param *ChannelCountersRequest) (*ChannelCountersResponse, error) {
req, err := http.NewRequest("GET",
fmt.Sprintf("%s/%s/channels.counters", c.baseURL, c.apiVersion),
2020-08-28 22:54:20 +02:00
nil)
2020-08-24 23:21:08 +02:00
if param.RoomName == "" && param.RoomId == "" {
2024-02-03 21:54:40 +02:00
return nil, fmt.Errorf("false parameters")
2020-08-24 23:21:08 +02:00
}
url := req.URL.Query()
if param.RoomName != "" {
url.Add("roomName", param.RoomName)
}
if param.RoomId != "" {
url.Add("roomId", param.RoomId)
}
req.URL.RawQuery = url.Encode()
if err != nil {
return nil, err
}
res := ChannelCountersResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
2024-02-03 21:54:40 +02:00
// CreateChannel creates a new channel.
2020-08-24 23:21:08 +02:00
func (c *Client) CreateChannel(param *CreateChannelRequest) (*CreateChannelResponse, error) {
opt, _ := json.Marshal(param)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/channels.create", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := CreateChannelResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
2020-08-28 22:54:20 +02:00
2024-02-03 21:54:40 +02:00
// DeleteChannel delete channel.
2020-08-28 22:54:20 +02:00
func (c *Client) DeleteChannel(param *SimpleChannelRequest) (*SimpleSuccessResponse, error) {
opt, _ := json.Marshal(param)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/channels.delete", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := SimpleSuccessResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
2024-02-03 21:54:40 +02:00
// ChannelInfo get channel info.
2020-08-28 22:54:20 +02:00
func (c *Client) ChannelInfo(param *SimpleChannelRequest) (*ChannelInfoResponse, error) {
req, err := http.NewRequest("GET",
fmt.Sprintf("%s/%s/channels.info", c.baseURL, c.apiVersion),
nil)
if param.RoomName == "" && param.RoomId == "" {
2024-02-03 21:54:40 +02:00
return nil, fmt.Errorf("false parameters")
2020-08-28 22:54:20 +02:00
}
url := req.URL.Query()
if param.RoomName != "" {
url.Add("roomName", param.RoomName)
}
if param.RoomId != "" {
url.Add("roomId", param.RoomId)
}
req.URL.RawQuery = url.Encode()
if err != nil {
return nil, err
}
res := ChannelInfoResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
2024-02-03 21:54:40 +02:00
// ChannelInvite adds a user to the channel.
2020-08-28 22:54:20 +02:00
func (c *Client) ChannelInvite(param *InviteChannelRequest) (*InviteChannelResponse, error) {
opt, _ := json.Marshal(param)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/channels.invite", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := InviteChannelResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
2024-02-03 21:54:40 +02:00
// ChannelKick kick a user from the channel.
2020-08-28 22:54:20 +02:00
func (c *Client) ChannelKick(param *InviteChannelRequest) (*InviteChannelResponse, error) {
opt, _ := json.Marshal(param)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/channels.kick", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := InviteChannelResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
2024-02-03 21:54:40 +02:00
// ChannelList get channels list
2020-08-28 22:54:20 +02:00
func (c *Client) ChannelList() (*ChannelListResponse, error) {
req, err := http.NewRequest("GET",
fmt.Sprintf("%s/%s/channels.list", c.baseURL, c.apiVersion),
nil)
if err != nil {
return nil, err
}
res := ChannelListResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
2024-02-03 21:54:40 +02:00
// ChannelMembers gets channel members
2020-08-28 22:54:20 +02:00
func (c *Client) ChannelMembers(param *SimpleChannelRequest) (*ChannelMembersResponse, error) {
req, err := http.NewRequest("GET",
fmt.Sprintf("%s/%s/channels.members", c.baseURL, c.apiVersion),
nil)
if param.RoomName == "" && param.RoomId == "" {
2024-02-03 21:54:40 +02:00
return nil, fmt.Errorf("false parameters")
2020-08-28 22:54:20 +02:00
}
url := req.URL.Query()
if param.RoomName != "" {
url.Add("roomName", param.RoomName)
}
if param.RoomId != "" {
url.Add("roomId", param.RoomId)
}
req.URL.RawQuery = url.Encode()
if err != nil {
return nil, err
}
res := ChannelMembersResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
2024-02-03 21:54:40 +02:00
// OpenChannel adds the channel back to the user's list of channels.
2020-08-28 22:54:20 +02:00
func (c *Client) OpenChannel(param *SimpleChannelId) (*SimpleSuccessResponse, error) {
opt, _ := json.Marshal(param)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/channels.open", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := SimpleSuccessResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
2020-08-29 21:56:09 +02:00
2024-02-03 21:54:40 +02:00
// RenameChannel changes a channel's name.
2020-08-29 21:56:09 +02:00
func (c *Client) RenameChannel(param *RenameChannelRequest) (*RenameChannelResponse, error) {
opt, _ := json.Marshal(param)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/channels.rename", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := RenameChannelResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
2024-02-03 21:54:40 +02:00
// SetAnnouncementChannel sets the announcement for the channel.
2020-08-29 21:56:09 +02:00
func (c *Client) SetAnnouncementChannel(param *SetAnnouncementRequest) (*SetAnnouncementResponse, error) {
opt, _ := json.Marshal(param)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/channels.setAnnouncement", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := SetAnnouncementResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
2024-02-03 21:54:40 +02:00
// SetDescriptionChannel sets the Description for the channel.
2020-08-29 21:56:09 +02:00
func (c *Client) SetDescriptionChannel(param *SetDescriptionRequest) (*SetDescriptionResponse, error) {
opt, _ := json.Marshal(param)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/channels.setDescription", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := SetDescriptionResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
2024-02-03 21:54:40 +02:00
// SetTopicChannel sets the topic for the channel.
2020-08-29 21:56:09 +02:00
func (c *Client) SetTopicChannel(param *SetTopicRequest) (*SetTopicResponse, error) {
opt, _ := json.Marshal(param)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/channels.setTopic", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := SetTopicResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
2024-02-03 21:54:40 +02:00
// UnarchiveChannel unarchive a channel.
2020-08-29 21:56:09 +02:00
func (c *Client) UnarchiveChannel(param *SimpleChannelId) (*SimpleSuccessResponse, error) {
opt, _ := json.Marshal(param)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/channels.unarchive", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := SimpleSuccessResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}