修复:集成聊天图片预览与真实尺寸发送
This commit is contained in:
1 parent
9eb6be2875
commit
ee228381cf
36 files changed
+331
-83
No files matched your search
@@ -45,6 +45,10 @@ type ImageInput struct {
|
||||
type ImageUpload struct {
|
||||
// URL 保存可供聊天发送的图片地址。
|
||||
URL string
|
||||
// Width 保存平台识别出的图片宽度,单位为像素;非正值表示使用协议默认尺寸。
|
||||
Width int
|
||||
// Height 保存平台识别出的图片高度,单位为像素;非正值表示使用协议默认尺寸。
|
||||
Height int
|
||||
}
|
||||
|
||||
// OutgoingRepository 定义发送用例需要的本地消息写入能力。
|
||||
@@ -62,7 +66,7 @@ type Sender interface {
|
||||
// SendText 发送文本;messageKey 用于将平台旁路事件与待发送消息关联。
|
||||
SendText(ctx context.Context, chatID, toUserID, text, messageKey string) error
|
||||
// SendImage 发送图片;messageKey 用于将平台旁路事件与待发送消息关联。
|
||||
SendImage(ctx context.Context, chatID, toUserID, imageURL string, cardID int64, messageKey string) error
|
||||
SendImage(ctx context.Context, chatID, toUserID, imageURL string, cardID int64, width, height int, messageKey string) error
|
||||
}
|
||||
|
||||
// SenderProvider 按账号标识解析当前在线发送实例。
|
||||
@@ -219,7 +223,7 @@ func (s *Service) SendImage(ctx context.Context, input ImageInput) (*Message, er
|
||||
return nil, fmt.Errorf("保存待发送图片失败: %w", err)
|
||||
}
|
||||
// sendErr 表示平台图片发送失败;失败分支会补写本地 failed 状态。
|
||||
if sendErr := sender.SendImage(ctx, session.ChatID, session.BuyerID, upload.URL, 0, message.MessageKey); sendErr != nil {
|
||||
if sendErr := sender.SendImage(ctx, session.ChatID, session.BuyerID, upload.URL, 0, upload.Width, upload.Height, message.MessageKey); sendErr != nil {
|
||||
// failed 保存图片发送失败后的本地状态。
|
||||
failed, _ := s.outgoing.SetOutgoingStatus(context.Background(), session.AccountID, message.MessageKey, "failed")
|
||||
return messagePointer(failed, message), fmt.Errorf("%w: %v", ErrSend, sendErr)
|
||||
|
||||
@@ -79,6 +79,8 @@ type sendSender struct {
|
||||
sentKey string
|
||||
// updatedCookie 保存适配器同步的刷新凭证;测试只记录是否调用,不保存真实秘密。
|
||||
updatedCookie bool
|
||||
// imageWidth、imageHeight 保存图片发送收到的像素尺寸。
|
||||
imageWidth, imageHeight int
|
||||
}
|
||||
|
||||
// SendText 记录文本发送并返回预设错误。
|
||||
@@ -88,7 +90,9 @@ func (s *sendSender) SendText(_ context.Context, _, _, _, messageKey string) err
|
||||
}
|
||||
|
||||
// SendImage 记录图片发送并返回预设错误。
|
||||
func (s *sendSender) SendImage(_ context.Context, _, _, _ string, _ int64, messageKey string) error {
|
||||
func (s *sendSender) SendImage(_ context.Context, _, _, _ string, _ int64, width, height int, messageKey string) error {
|
||||
// imageWidth、imageHeight 记录应用层透传的平台图片尺寸。
|
||||
s.imageWidth, s.imageHeight = width, height
|
||||
s.sentKey = messageKey
|
||||
return s.sendErr
|
||||
}
|
||||
@@ -192,12 +196,12 @@ func TestSendImageDoesNotExposeCredentials(t *testing.T) {
|
||||
// repository、sender、uploader 保存图片发送的测试端口。
|
||||
repository, sender := &sendRepository{}, &sendSender{}
|
||||
// uploader 保存固定图片地址的测试上传端口。
|
||||
uploader := sendUploader{result: ImageUpload{URL: "https://cdn.example/image.jpg"}}
|
||||
uploader := sendUploader{result: ImageUpload{URL: "https://cdn.example/image.jpg", Width: 1280, Height: 720}}
|
||||
// service 保存使用测试端口构造的聊天发送服务。
|
||||
service := NewWithSending(nil, repository, sendProvider{sender: sender}, uploader)
|
||||
// message 和 err 保存图片发送结果。
|
||||
message, err := service.SendImage(context.Background(), ImageInput{Session: Session{AccountID: "acc-1", ChatID: "chat-1", BuyerID: "buyer-1"}, Filename: "a.jpg", ContentType: "image/jpeg", Data: []byte("image")})
|
||||
if err != nil || message == nil || message.Content != "https://cdn.example/image.jpg" || sender.sentKey != "local-image" {
|
||||
if err != nil || message == nil || message.Content != "https://cdn.example/image.jpg" || sender.sentKey != "local-image" || sender.imageWidth != 1280 || sender.imageHeight != 720 {
|
||||
t.Fatalf("message=%+v err=%v key=%q", message, err, sender.sentKey)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user