修复:集成聊天图片预览与真实尺寸发送
This commit is contained in:
1 parent
9eb6be2875
commit
ee228381cf
36 files changed
+331
-83
No files matched your search
@@ -878,8 +878,9 @@ func (a *Account) MarkChatRead(ctx context.Context, chatID string, messageIDs []
|
||||
}
|
||||
|
||||
// SendImage 通过当前 WebSocket 给买家发送图片消息。当前仅支持可直接访问的 CDN/公网 URL。
|
||||
func (a *Account) SendImage(ctx context.Context, chatID, toUserID, imageURL string, cardID int64) error {
|
||||
return a.outgoing.sendImage(ctx, chatID, toUserID, imageURL, cardID)
|
||||
// width/height 为图片真实尺寸,单位为像素;传入非正值时由 WebSocket 使用默认尺寸。
|
||||
func (a *Account) SendImage(ctx context.Context, chatID, toUserID, imageURL string, cardID int64, width, height int) error {
|
||||
return a.outgoing.sendImage(ctx, chatID, toUserID, imageURL, cardID, width, height)
|
||||
}
|
||||
|
||||
// FetchChatHistory reuses the account's registered IM connection. Keeping this
|
||||
|
||||
@@ -57,8 +57,8 @@ func (c *outgoingMessageCoordinator) sendText(ctx context.Context, chatID, toUse
|
||||
}
|
||||
|
||||
// sendImage 使用当前已注册 WebSocket 发送可直接访问的远程图片。
|
||||
// ctx 是调用方取消边界;cardID 仅用于兼容 MessageSender 契约,当前协议发送不直接使用它。
|
||||
func (c *outgoingMessageCoordinator) sendImage(ctx context.Context, chatID, toUserID, imageURL string, cardID int64) error {
|
||||
// ctx 是调用方取消边界;cardID 仅用于兼容 MessageSender 契约,当前协议发送不直接使用它;width/height 为图片像素尺寸。
|
||||
func (c *outgoingMessageCoordinator) sendImage(ctx context.Context, chatID, toUserID, imageURL string, cardID int64, width, height int) error {
|
||||
// a 是当前协调器绑定的账号 facade;它用于维持与文本发送一致的初始化检查。
|
||||
a := c.account
|
||||
if a == nil {
|
||||
@@ -80,7 +80,7 @@ func (c *outgoingMessageCoordinator) sendImage(ctx context.Context, chatID, toUs
|
||||
sendCtx, cancel := context.WithTimeout(ctx, 8*time.Second)
|
||||
defer cancel()
|
||||
_ = cardID // cardID 由上层动作检查点持久化,协议图片发送本身不携带该字段。
|
||||
return conn.SendImage(sendCtx, myID, chatID, toUserID, imageURL, 800, 600)
|
||||
return conn.SendImage(sendCtx, myID, chatID, toUserID, imageURL, width, height)
|
||||
}
|
||||
|
||||
// currentSenderState 返回可用 WebSocket 与账号 unb 身份快照;持锁范围只覆盖快照读取。
|
||||
|
||||
@@ -37,7 +37,7 @@ type AIReplier interface {
|
||||
// MessageSender 是回复服务发送文本/图片所需的最小接口。
|
||||
type MessageSender interface {
|
||||
SendText(ctx context.Context, chatID, toUserID, text string) error
|
||||
SendImage(ctx context.Context, chatID, toUserID, imageURL string, cardID int64) error
|
||||
SendImage(ctx context.Context, chatID, toUserID, imageURL string, cardID int64, width, height int) error
|
||||
}
|
||||
|
||||
// ReplyService 单账号回复服务。
|
||||
@@ -97,7 +97,7 @@ func (r *ReplyService) Handle(ctx context.Context, m ChatMessage) error {
|
||||
}
|
||||
if res.ImageURL != "" && !record.ImageSent {
|
||||
if // err 用于本次流程后续判断的err
|
||||
err := r.sender.SendImage(ctx, m.ChatID, m.SenderUserID, res.ImageURL, 0); err != nil {
|
||||
err := r.sender.SendImage(ctx, m.ChatID, m.SenderUserID, res.ImageURL, 0, 0, 0); err != nil {
|
||||
r.logger.Error("发送回复图片失败", "err", err)
|
||||
r.markReplyFailure(ctx, res, m, err)
|
||||
return err
|
||||
|
||||
@@ -63,7 +63,7 @@ func (r *recordingSender) SendText(_ context.Context, chatID, toUserID, text str
|
||||
}
|
||||
|
||||
// SendImage 封装Send图片业务协调。
|
||||
func (r *recordingSender) SendImage(_ context.Context, chatID, toUserID, url string, cardID int64) error {
|
||||
func (r *recordingSender) SendImage(_ context.Context, chatID, toUserID, url string, cardID int64, _, _ int) error {
|
||||
if r.imageErr != nil {
|
||||
return r.imageErr
|
||||
}
|
||||
|
||||
@@ -127,6 +127,8 @@ type fakeWSConn struct {
|
||||
registeredTok string
|
||||
sentTexts []string
|
||||
sentImages []string
|
||||
imageWidths []int
|
||||
imageHeights []int
|
||||
heartbeatDone chan struct{}
|
||||
closeCh chan struct{}
|
||||
closeOnce sync.Once
|
||||
@@ -237,13 +239,36 @@ func (f *fakeWSConn) SendText(_ context.Context, _, _, _, text string) error {
|
||||
}
|
||||
|
||||
// SendImage 封装Send图片业务协调。
|
||||
func (f *fakeWSConn) SendImage(_ context.Context, _, _, _, url string, _, _ int) error {
|
||||
func (f *fakeWSConn) SendImage(_ context.Context, _, _, _, url string, width, height int) error {
|
||||
f.mu.Lock()
|
||||
f.sentImages = append(f.sentImages, url)
|
||||
f.imageWidths = append(f.imageWidths, width)
|
||||
f.imageHeights = append(f.imageHeights, height)
|
||||
f.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestAccountSendImagePreservesDimensions 验证账号运行时将调用方提供的真实图片尺寸交给 WebSocket。
|
||||
func TestAccountSendImagePreservesDimensions(t *testing.T) {
|
||||
// account 是未启动但已具备账号身份的运行时对象。
|
||||
account := New(Config{CookieID: "image-size-test", CookieStr: "unb=me"})
|
||||
// conn 是记录图片 URL 和像素尺寸的 WebSocket 测试替身。
|
||||
conn := &fakeWSConn{}
|
||||
account.runtimeMu.Lock()
|
||||
account.conn = conn
|
||||
account.runtimeMu.Unlock()
|
||||
// sendErr 表示账号运行时向 WebSocket 发送图片的结果。
|
||||
sendErr := account.SendImage(context.Background(), "chat", "buyer", "https://cdn.example/image.jpg", 0, 1920, 1080)
|
||||
if sendErr != nil {
|
||||
t.Fatalf("SendImage() error=%v", sendErr)
|
||||
}
|
||||
conn.mu.Lock()
|
||||
defer conn.mu.Unlock()
|
||||
if len(conn.imageWidths) != 1 || conn.imageWidths[0] != 1920 || len(conn.imageHeights) != 1 || conn.imageHeights[0] != 1080 {
|
||||
t.Fatalf("图片尺寸未透传 widths=%v heights=%v", conn.imageWidths, conn.imageHeights)
|
||||
}
|
||||
}
|
||||
|
||||
// fakeDialer 按预设序列返回连接或错误,第 N 次(1-based)调用返回 dialResults[N-1]。
|
||||
type fakeDialer struct {
|
||||
mu sync.Mutex
|
||||
|
||||
Reference in new issue
Block a user