Files

225 lines
6.8 KiB
Go

package nixmsg
import (
"context"
"encoding/json"
)
// Recall 撤回消息。
func (c *Client) Recall(ctx context.Context, id string) (RecallResult, error) {
data, err := c.request(ctx, map[string]any{"v": 1, "type": "recall", "rid": c.nextRID(), "id": id}, false)
if err != nil {
return RecallResult{}, err
}
var r RecallResult
_ = json.Unmarshal(data, &r)
return r, nil
}
// Status 查询发送状态。
func (c *Client) Status(ctx context.Context, id, cursor string, limit int) (json.RawMessage, error) {
req := map[string]any{"v": 1, "type": "status", "rid": c.nextRID(), "id": id}
if cursor != "" {
req["cursor"] = cursor
}
if limit > 0 {
req["limit"] = limit
}
return c.request(ctx, req, false)
}
// Unlock 解锁对话密码。
func (c *Client) Unlock(ctx context.Context, endpointID, talkPassword string) error {
_, err := c.request(ctx, map[string]any{
"v": 1, "type": "unlock", "rid": c.nextRID(),
"endpoint_id": endpointID, "talk_password": talkPassword,
}, false)
return err
}
// Presence 查询在线。
func (c *Client) Presence(ctx context.Context, ids []string) ([]PresenceItem, error) {
data, err := c.request(ctx, map[string]any{"v": 1, "type": "presence.get", "rid": c.nextRID(), "ids": ids}, false)
if err != nil {
return nil, err
}
var wrap struct {
Items []PresenceItem `json:"items"`
}
if json.Unmarshal(data, &wrap) == nil && wrap.Items != nil {
return wrap.Items, nil
}
var items []PresenceItem
if err := json.Unmarshal(data, &items); err == nil {
return items, nil
}
return nil, nil
}
// Directory 列目录。
func (c *Client) Directory(ctx context.Context, cursor, query string, limit int) (json.RawMessage, error) {
req := map[string]any{"v": 1, "type": "directory.list", "rid": c.nextRID()}
if cursor != "" {
req["cursor"] = cursor
}
if query != "" {
req["query"] = query
}
if limit > 0 {
req["limit"] = limit
}
return c.request(ctx, req, false)
}
// WatchPresence 订阅上下线;ids 为空且 all 为 true 表示全部。
func (c *Client) WatchPresence(ctx context.Context, ids []string, all bool) error {
req := map[string]any{"v": 1, "type": "presence.watch", "rid": c.nextRID(), "all": all}
if len(ids) > 0 {
req["ids"] = ids
}
_, err := c.request(ctx, req, false)
return err
}
// GetSelf 读取自己的资料。
func (c *Client) GetSelf(ctx context.Context) (SelfInfo, error) {
data, err := c.request(ctx, map[string]any{"v": 1, "type": "self.get", "rid": c.nextRID()}, false)
if err != nil {
return SelfInfo{}, err
}
var s SelfInfo
_ = json.Unmarshal(data, &s)
return s, nil
}
// UpdateSelf 更新资料。
func (c *Client) UpdateSelf(ctx context.Context, name string, defaultDelayMs *int64) error {
req := map[string]any{"v": 1, "type": "self.update", "rid": c.nextRID()}
if name != "" {
req["name"] = name
}
if defaultDelayMs != nil {
req["default_delay_ms"] = *defaultDelayMs
}
_, err := c.request(ctx, req, false)
return err
}
// SetTalkPassword 设置对话密码;空字符串清除。
func (c *Client) SetTalkPassword(ctx context.Context, talkPassword string) error {
_, err := c.request(ctx, map[string]any{
"v": 1, "type": "self.talk_password", "rid": c.nextRID(), "talk_password": talkPassword,
}, false)
return err
}
// ChangeLoginPassword 修改登录密码;成功后若返回新令牌会经 onSession 交出。
func (c *Client) ChangeLoginPassword(ctx context.Context, oldPassword, newPassword string) error {
data, err := c.request(ctx, map[string]any{
"v": 1, "type": "self.login_password", "rid": c.nextRID(),
"old_password": oldPassword, "new_password": newPassword,
}, false)
if err != nil {
return err
}
var d struct {
SessionToken string `json:"session_token"`
}
if json.Unmarshal(data, &d) == nil && d.SessionToken != "" {
c.mu.Lock()
c.session = d.SessionToken
if c.transport != nil {
c.transport.SetCredential(d.SessionToken)
}
c.mu.Unlock()
if c.onSession != nil {
c.cbMu.Lock()
c.onSession(d.SessionToken)
c.cbMu.Unlock()
}
}
return nil
}
// CreateGroup 建群。
func (c *Client) CreateGroup(ctx context.Context, id, name string, members []GroupMemberIn) (json.RawMessage, error) {
ms := make([]map[string]any, 0, len(members))
for _, m := range members {
ms = append(ms, map[string]any{"id": m.ID, "talk_password": m.TalkPassword})
}
return c.request(ctx, map[string]any{
"v": 1, "type": "group.create", "rid": c.nextRID(), "id": id, "name": name, "members": ms,
}, false)
}
// AddGroupMembers 加成员。
func (c *Client) AddGroupMembers(ctx context.Context, groupID string, members []GroupMemberIn) (json.RawMessage, error) {
ms := make([]map[string]any, 0, len(members))
for _, m := range members {
ms = append(ms, map[string]any{"id": m.ID, "talk_password": m.TalkPassword})
}
return c.request(ctx, map[string]any{
"v": 1, "type": "group.add", "rid": c.nextRID(), "group_id": groupID, "members": ms,
}, false)
}
// RemoveGroupMember 移除成员。
func (c *Client) RemoveGroupMember(ctx context.Context, groupID, endpointID string) error {
_, err := c.request(ctx, map[string]any{
"v": 1, "type": "group.remove", "rid": c.nextRID(), "group_id": groupID, "endpoint_id": endpointID,
}, false)
return err
}
// LeaveGroup 退群。
func (c *Client) LeaveGroup(ctx context.Context, groupID string) error {
_, err := c.request(ctx, map[string]any{"v": 1, "type": "group.leave", "rid": c.nextRID(), "group_id": groupID}, false)
return err
}
// TransferGroup 转让群主。
func (c *Client) TransferGroup(ctx context.Context, groupID, endpointID string) error {
_, err := c.request(ctx, map[string]any{
"v": 1, "type": "group.transfer", "rid": c.nextRID(), "group_id": groupID, "endpoint_id": endpointID,
}, false)
return err
}
// RenameGroup 改群名。
func (c *Client) RenameGroup(ctx context.Context, groupID, name string) error {
_, err := c.request(ctx, map[string]any{
"v": 1, "type": "group.rename", "rid": c.nextRID(), "group_id": groupID, "name": name,
}, false)
return err
}
// DissolveGroup 解散群。
func (c *Client) DissolveGroup(ctx context.Context, groupID string) error {
_, err := c.request(ctx, map[string]any{"v": 1, "type": "group.dissolve", "rid": c.nextRID(), "group_id": groupID}, false)
return err
}
// ListGroups 列出我的群。
func (c *Client) ListGroups(ctx context.Context, cursor string, limit int) (json.RawMessage, error) {
req := map[string]any{"v": 1, "type": "group.list", "rid": c.nextRID()}
if cursor != "" {
req["cursor"] = cursor
}
if limit > 0 {
req["limit"] = limit
}
return c.request(ctx, req, false)
}
// GetGroup 获取群详情。
func (c *Client) GetGroup(ctx context.Context, groupID, cursor string, limit int) (json.RawMessage, error) {
req := map[string]any{"v": 1, "type": "group.get", "rid": c.nextRID(), "group_id": groupID}
if cursor != "" {
req["cursor"] = cursor
}
if limit > 0 {
req["limit"] = limit
}
return c.request(ctx, req, false)
}