Skip to content

Commit 17eecd6

Browse files
committed
Merge branch 'v4.0.7' into 'master'
feat: Add interfaces for clearing historical messages and user management of the development environment See merge request public-server/server-sdk-go!11
2 parents 5ebfa70 + 2c3646b commit 17eecd6

7 files changed

Lines changed: 232 additions & 4 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ func main() {
126126
| | TagSet | Add user tag ||
127127
| | TagBatchSet | Batch add user tags ||
128128
| | TagGet | Get user tags ||
129+
| | UserQuery | Query users with pagination (functional options) ||
130+
| | UserDelUsers | Delete users in batch (up to 100 per request) ||
129131
| | GroupMuteAdd | Add global group mute, user can’t send messages in any group | |
130132
| | GroupMuteRemove | Remove global group mute | |
131133
| | GroupMuteGetList | Get global group mute list | |
@@ -151,6 +153,7 @@ func main() {
151153
| | SystemBroadcast | Send broadcast message, max 2 per hour, 3 per day. ||
152154
| [Message history](https://github.com/rongcloud/server-sdk-go/blob/master/sdk/message_test.go) | HistoryGet | Get message history download URL ||
153155
| | HistoryRemove | Delete message history ||
156+
| | ConversationMessageHistoryClean | Clean conversation history (functional options) ||
154157
| [Broadcast push](https://github.com/rongcloud/server-sdk-go/blob/master/sdk/push_test.go) | PushSend | Send push, max 2 per hour, 3 per day. ||
155158
| [Group](https://github.com/rongcloud/server-sdk-go/blob/master/sdk/group_test.go) | GroupCreate | Create group ||
156159
| | GroupSync | Sync group relationships ||

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ func main() {
127127
| | TagSet | 添加用户标签 ||
128128
| | TagBatchSet | 批量添加用户标签 ||
129129
| | TagGet | 获取用户标签 ||
130+
| | UserQuery | 查询应用内用户(分页,可选参数) ||
131+
| | UserDelUsers | 删除用户(批量,单次最多 100 个) ||
130132
| | GroupMuteAdd | 添加全局群组禁言用户,添加后用户在应用下的所有群组中都不能发送消息 | |
131133
| | GroupMuteRemove | 移除全局群组禁言用户 | |
132134
| | GroupMuteGetList | 获取全局群组禁言用户列表 | |
@@ -152,6 +154,7 @@ func main() {
152154
| | SystemBroadcast | 发送广播消息,单个应用每小时只能发送 2 次,每天最多发送 3 次。 ||
153155
| [消息历史记录](https://github.com/rongcloud/server-sdk-go/blob/master/sdk/message_test.go) | HistoryGet | 消息历史记录下载地址获取 ||
154156
| | HistoryRemove | 消息历史记录删除方法 ||
157+
| | ConversationMessageHistoryClean | 清除会话历史消息(函数式可选项) ||
155158
| | GetPrivateHistoryMessage | 获取单聊历史消息 ||
156159
| | GetGroupHistoryMessage | 获取群聊历史消息 ||
157160
| | GetUltraGroupHistoryMessage | 获取超级群历史消息 ||

sdk/message.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,3 +2334,65 @@ func (rc *RongCloud) GetChatroomHistoryMessage(model QueryHistoryMessageModel) (
23342334

23352335
return result, nil
23362336
}
2337+
2338+
// conversationCleanOptions: options for cleaning conversation history
2339+
type conversationCleanOptions struct {
2340+
msgTimestamp int64
2341+
}
2342+
2343+
// ConversationCleanOption defines functional options for cleaning
2344+
type ConversationCleanOption func(*conversationCleanOptions)
2345+
2346+
// WithCleanMsgTimestamp sets to clean all messages before the given millisecond timestamp
2347+
func WithCleanMsgTimestamp(ts int64) ConversationCleanOption {
2348+
return func(o *conversationCleanOptions) { o.msgTimestamp = ts }
2349+
}
2350+
2351+
func buildConversationCleanOptions(opts []ConversationCleanOption) conversationCleanOptions {
2352+
def := conversationCleanOptions{}
2353+
for _, opt := range opts {
2354+
opt(&def)
2355+
}
2356+
return def
2357+
}
2358+
2359+
// ConversationMessageHistoryClean cleans history messages of a conversation
2360+
// API: POST /conversation/message/history/clean.json
2361+
// Parameters:
2362+
// - conversationType: conversation type. 1 (one-to-one chat), 3 (group chat), 4 (chatroom), 6 (system)
2363+
// - fromUserId: for one-to-one chat/group chat/system: specified user ID; for chatroom: operator user ID
2364+
// - targetId: target conversation ID
2365+
// - msgTimestamp (optional): clean all messages before the given millisecond timestamp; omit to clean the entire conversation history
2366+
func (rc *RongCloud) ConversationMessageHistoryClean(conversationType, fromUserId, targetId string, options ...ConversationCleanOption) error {
2367+
if conversationType == "" {
2368+
return RCErrorNew(1002, "Parameter 'conversationType' is required")
2369+
}
2370+
2371+
if fromUserId == "" {
2372+
return RCErrorNew(1002, "Parameter 'fromUserId' is required")
2373+
}
2374+
2375+
if targetId == "" {
2376+
return RCErrorNew(1002, "Parameter 'targetId' is required")
2377+
}
2378+
2379+
ext := buildConversationCleanOptions(options)
2380+
2381+
req := httplib.Post(rc.rongCloudURI + "/conversation/message/history/clean.json")
2382+
req.SetTimeout(time.Second*rc.timeout, time.Second*rc.timeout)
2383+
rc.fillHeader(req)
2384+
2385+
req.Param("conversationType", conversationType)
2386+
req.Param("fromUserId", fromUserId)
2387+
req.Param("targetId", targetId)
2388+
2389+
if ext.msgTimestamp > 0 {
2390+
req.Param("msgTimestamp", strconv.FormatInt(ext.msgTimestamp, 10))
2391+
}
2392+
2393+
_, err := rc.do(req)
2394+
if err != nil {
2395+
rc.urlError(err)
2396+
}
2397+
return err
2398+
}

sdk/message_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,3 +779,19 @@ func TestRongCloud_GetChatroomHistoryMessage(t *testing.T) {
779779
}
780780
t.Logf("GetChatroomHistoryMessage response: \n%s", string(jsonResp))
781781
}
782+
783+
func TestRongCloud_ConversationMessageHistoryClean(t *testing.T) {
784+
rc := NewRongCloud(
785+
"n19jmcy59f1q9",
786+
"CuhqdZMeuLsKj",
787+
REGION_BJ,
788+
)
789+
790+
// 最小调用校验:群组会话(3),示例参数
791+
err := rc.ConversationMessageHistoryClean("3", "uid_test", "gid_test", WithCleanMsgTimestamp(1566281295943))
792+
if err != nil {
793+
t.Logf("ConversationMessageHistoryClean returned error (acceptable in unit env): %v", err)
794+
} else {
795+
t.Log("ConversationMessageHistoryClean ok")
796+
}
797+
}

sdk/rongcloud.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const (
5757
// ReqType Body type
5858
ReqType = "json"
5959
// USERAGENT SDK name
60-
USERAGENT = "rc-go-sdk/4.0.6"
60+
USERAGENT = "rc-go-sdk/4.0.7"
6161
// DEFAULTTIMEOUT Default timeout, 10 seconds
6262
DEFAULTTIMEOUT = 10
6363
// DEFAULT_KEEPALIVE Default HTTP keepalive time, 30 seconds

sdk/user.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,3 +1144,121 @@ func (rc *RongCloud) UserProfilBatchQuery(userId string) (*UserProfileQueryRespo
11441144
}
11451145
return &userProfileQueryResponse, nil
11461146
}
1147+
1148+
// UserQueryItem represents a single user record returned by /user/query.json
1149+
type UserQueryItem struct {
1150+
UserId string `json:"userId"`
1151+
UserName string `json:"userName"`
1152+
UserPortrait string `json:"userPortrait"`
1153+
RegTime string `json:"regTime"`
1154+
}
1155+
1156+
// UserQueryResponse is the response of /user/query.json (non-200 codes are raised by rc.do)
1157+
type UserQueryResponse struct {
1158+
Total int `json:"total"`
1159+
Users []UserQueryItem `json:"users"`
1160+
}
1161+
1162+
// UserQueryOrder represents sort order for user query.
1163+
type UserQueryOrder int
1164+
1165+
const (
1166+
UserQueryOrderAsc UserQueryOrder = 0
1167+
UserQueryOrderDesc UserQueryOrder = 1
1168+
)
1169+
1170+
// userQueryParams holds query params with defaults applied.
1171+
type userQueryParams struct {
1172+
page int
1173+
pageSize int
1174+
order UserQueryOrder
1175+
}
1176+
1177+
// UserQueryOption is a functional option to customize user query parameters.
1178+
type UserQueryOption func(*userQueryParams)
1179+
1180+
// WithPage sets the page number (must be > 0; otherwise default is used).
1181+
func WithPage(page int) UserQueryOption {
1182+
return func(p *userQueryParams) {
1183+
if page > 0 {
1184+
p.page = page
1185+
}
1186+
}
1187+
}
1188+
1189+
// WithPageSize sets page size (clamped to [1, 100]; default 20).
1190+
func WithPageSize(size int) UserQueryOption {
1191+
return func(p *userQueryParams) {
1192+
if size <= 0 {
1193+
return
1194+
}
1195+
if size > 100 {
1196+
size = 100
1197+
}
1198+
p.pageSize = size
1199+
}
1200+
}
1201+
1202+
// WithOrder sets sort order (0 asc, 1 desc; invalid values fallback to asc).
1203+
func WithOrder(order UserQueryOrder) UserQueryOption {
1204+
return func(p *userQueryParams) {
1205+
if order != UserQueryOrderAsc && order != UserQueryOrderDesc {
1206+
order = UserQueryOrderAsc
1207+
}
1208+
p.order = order
1209+
}
1210+
}
1211+
1212+
// UserQuery queries users with functional options.
1213+
// Defaults: page=1, pageSize=20, order=asc.
1214+
func (rc *RongCloud) UserQuery(opts ...UserQueryOption) (UserQueryResponse, error) {
1215+
params := userQueryParams{
1216+
page: 1,
1217+
pageSize: 20,
1218+
order: UserQueryOrderAsc,
1219+
}
1220+
for _, opt := range opts {
1221+
opt(&params)
1222+
}
1223+
1224+
req := httplib.Post(rc.rongCloudURI + "/user/query.json")
1225+
req.SetTimeout(time.Second*rc.timeout, time.Second*rc.timeout)
1226+
rc.fillHeader(req)
1227+
req.Param("page", strconv.Itoa(params.page))
1228+
req.Param("pageSize", strconv.Itoa(params.pageSize))
1229+
req.Param("order", strconv.Itoa(int(params.order)))
1230+
1231+
res, err := rc.do(req)
1232+
var result = UserQueryResponse{}
1233+
if err != nil {
1234+
return result, err
1235+
}
1236+
if err := json.Unmarshal(res, &result); err != nil {
1237+
return result, err
1238+
}
1239+
return result, nil
1240+
}
1241+
1242+
// UserDelUsers deletes users (up to 100 per request).
1243+
// @param userIds User IDs to delete, length 1-100
1244+
// @return error
1245+
func (rc *RongCloud) UserDelUsers(userIds []string) error {
1246+
if len(userIds) == 0 {
1247+
return RCErrorNew(1002, "Paramer 'userIds' is required")
1248+
}
1249+
if len(userIds) > 100 {
1250+
return RCErrorNew(1002, "Length of paramer 'userIds' must be less than or equal to 100")
1251+
}
1252+
1253+
req := httplib.Post(rc.rongCloudURI + "/user/delusers.json")
1254+
req.SetTimeout(time.Second*rc.timeout, time.Second*rc.timeout)
1255+
rc.fillHeader(req)
1256+
for _, id := range userIds {
1257+
req.Param("userId", id)
1258+
}
1259+
_, err := rc.do(req)
1260+
if err != nil {
1261+
rc.urlError(err)
1262+
}
1263+
return err
1264+
}

sdk/user_test.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package sdk
1010

1111
import (
12-
"fmt"
1312
"os"
1413
"testing"
1514
)
@@ -37,7 +36,7 @@ func TestRongCloud_UserInfoGet(t *testing.T) {
3736
t.Errorf("UserInfoGet err:%v", err)
3837
return
3938
} else {
40-
t.Log(fmt.Sprintf("suc %+v", res))
39+
t.Logf("suc %+v", res)
4140
}
4241
}
4342

@@ -51,7 +50,7 @@ func TestRongCloud_UserBlockPushPeriodGet(t *testing.T) {
5150
t.Errorf("UserTokenExpire err:%v", err)
5251
return
5352
} else {
54-
t.Log(fmt.Sprintf("suc %+v", res))
53+
t.Logf("suc %+v", res)
5554
}
5655
}
5756

@@ -529,3 +528,30 @@ func TestRongCloud_UserProfilBatchQuery(t *testing.T) {
529528
}
530529
t.Logf("res: %+v", result)
531530
}
531+
532+
func TestRongCloud_UserQuery(t *testing.T) {
533+
rc := NewRongCloud(
534+
os.Getenv("APP_KEY"),
535+
os.Getenv("APP_SECRET"),
536+
REGION_BJ,
537+
)
538+
res, err := rc.UserQuery(WithPage(1), WithPageSize(10), WithOrder(UserQueryOrderDesc))
539+
if err != nil {
540+
t.Errorf("UserQuery err:%v", err)
541+
return
542+
}
543+
t.Logf("UserQuery suc:%+v", res)
544+
}
545+
546+
func TestRongCloud_UserDelUsers(t *testing.T) {
547+
rc := NewRongCloud(
548+
os.Getenv("APP_KEY"),
549+
os.Getenv("APP_SECRET"),
550+
REGION_BJ,
551+
)
552+
if err := rc.UserDelUsers([]string{"acbsdsdsfsd", "acxdskmldsmgds"}); err != nil {
553+
t.Errorf("UserDelUsers err:%v", err)
554+
return
555+
}
556+
t.Log("UserDelUsers suc")
557+
}

0 commit comments

Comments
 (0)