-
Notifications
You must be signed in to change notification settings - Fork 264
/
datasource.go
144 lines (126 loc) · 3.57 KB
/
datasource.go
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
package server
import (
"fmt"
"net/http"
"github.com/WuKongIM/WuKongIM/pkg/network"
"github.com/WuKongIM/WuKongIM/pkg/wkstore"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
)
// IDatasource 数据源第三方应用可以提供
type IDatasource interface {
// 获取订阅者
GetSubscribers(channelID string, channelType uint8) ([]string, error)
// 获取黑名单
GetBlacklist(channelID string, channelType uint8) ([]string, error)
// 获取白名单
GetWhitelist(channelID string, channelType uint8) ([]string, error)
// 获取系统账号的uid集合 系统账号可以给任何人发消息
GetSystemUIDs() ([]string, error)
// 获取频道信息
GetChannelInfo(channelID string, channelType uint8) (*wkstore.ChannelInfo, error)
}
// Datasource Datasource
type Datasource struct {
s *Server
}
// NewDatasource 创建一个数据源
func NewDatasource(s *Server) IDatasource {
return &Datasource{
s: s,
}
}
func (d *Datasource) GetChannelInfo(channelID string, channelType uint8) (*wkstore.ChannelInfo, error) {
result, err := d.requestCMD("getChannelInfo", map[string]interface{}{
"channel_id": channelID,
"channel_type": channelType,
})
if err != nil {
return nil, err
}
var channelInfoResp ChannelInfoResp
err = wkutil.ReadJSONByByte([]byte(result), &channelInfoResp)
if err != nil {
return nil, err
}
channelInfo := channelInfoResp.ToChannelInfo()
channelInfo.ChannelID = channelID
channelInfo.ChannelType = channelType
return channelInfo, nil
}
// GetSubscribers 获取频道的订阅者
func (d *Datasource) GetSubscribers(channelID string, channelType uint8) ([]string, error) {
result, err := d.requestCMD("getSubscribers", map[string]interface{}{
"channel_id": channelID,
"channel_type": channelType,
})
if err != nil {
return nil, err
}
var subscribers []string
err = wkutil.ReadJSONByByte([]byte(result), &subscribers)
if err != nil {
return nil, err
}
return subscribers, nil
}
// GetBlacklist 获取频道的黑名单
func (d *Datasource) GetBlacklist(channelID string, channelType uint8) ([]string, error) {
result, err := d.requestCMD("getBlacklist", map[string]interface{}{
"channel_id": channelID,
"channel_type": channelType,
})
if err != nil {
return nil, err
}
var blacklists []string
err = wkutil.ReadJSONByByte([]byte(result), &blacklists)
if err != nil {
return nil, err
}
return blacklists, nil
}
// GetWhitelist 获取频道的白明单
func (d *Datasource) GetWhitelist(channelID string, channelType uint8) ([]string, error) {
result, err := d.requestCMD("getWhitelist", map[string]interface{}{
"channel_id": channelID,
"channel_type": channelType,
})
if err != nil {
return nil, err
}
var whitelists []string
err = wkutil.ReadJSONByByte([]byte(result), &whitelists)
if err != nil {
return nil, err
}
return whitelists, nil
}
// GetSystemUIDs 获取系统账号
func (d *Datasource) GetSystemUIDs() ([]string, error) {
result, err := d.requestCMD("getSystemUIDs", map[string]interface{}{})
if err != nil {
return nil, err
}
var uids []string
err = wkutil.ReadJSONByByte([]byte(result), &uids)
if err != nil {
return nil, err
}
return uids, nil
}
func (d *Datasource) requestCMD(cmd string, param map[string]interface{}) (string, error) {
dataMap := map[string]interface{}{
"cmd": cmd,
}
if param != nil {
dataMap["data"] = param
}
resp, err := network.Post(d.s.opts.Datasource.Addr, []byte(wkutil.ToJSON(dataMap)), nil)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("http状态码错误![%d]", resp.StatusCode)
}
return resp.Body, nil
}