Skip to content

Commit

Permalink
Merge pull request #21 from Szzrain/feat/group-image-build
Browse files Browse the repository at this point in the history
  • Loading branch information
Redmomn authored Apr 22, 2024
2 parents c9520d2 + 37b05bf commit be32328
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 23 deletions.
20 changes: 15 additions & 5 deletions client/highway.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,22 @@ func (c *QQClient) UploadSrcByStreamAsync(commonId int, stream io.ReadSeeker, ti
// Get server URL
_, server := c.GetServiceServer()
if server == nil {
fmt.Println("Failed to get server")
return false
}
success := true
var upBlocks []UpBlock
data, err := io.ReadAll(stream)
if err != nil {
fmt.Println("Failed to read stream")
return false
}

fileSize := uint64(len(data))
offset := uint64(0)
_, err = stream.Seek(0, io.SeekStart)
if err != nil {
fmt.Println("Failed to seek stream")
return false
}

Expand All @@ -91,6 +94,7 @@ func (c *QQClient) UploadSrcByStreamAsync(commonId int, stream io.ReadSeeker, ti
buffer := make([]byte, buffersize)
payload, err := io.ReadFull(stream, buffer)
if err != nil {
fmt.Println("Failed to read stream")
return false
}
reqBody := UpBlock{
Expand All @@ -111,6 +115,10 @@ func (c *QQClient) UploadSrcByStreamAsync(commonId int, stream io.ReadSeeker, ti
if len(upBlocks) >= 4 || offset == fileSize {
for _, block := range upBlocks {
success = success && c.SendUpBlockAsync(block, server[1][0])
if !success {
fmt.Println("Failed to send block")
return false
}
}
upBlocks = nil
}
Expand Down Expand Up @@ -154,10 +162,12 @@ func (c *QQClient) SendUpBlockAsync(block UpBlock, server string) bool {
packet.WriteBytes(block.Block, false)
payload, err := SendPacketAsync(highwayHead, packet, server, isEnd)
if err != nil {
fmt.Println("Failed to send packet ", err)
return false
}
resphead, respbody, err := ParsePacket(payload)
if err != nil {
fmt.Println("Failed to parse packet ", err)
return false
}
networkLogger.Infof("Highway Block Result: %d | %d | %x | %v", resphead.ErrorCode, resphead.MsgSegHead.RetCode, resphead.BytesRspExtendInfo, respbody)
Expand All @@ -169,14 +179,14 @@ func ParsePacket(data []byte) (head *highway.RespDataHighwayHead, body *binary.R
if reader.ReadBytes(1)[0] == 0x28 {
headlength := reader.ReadU32()
bodylength := reader.ReadU32()
err = proto.Unmarshal(reader.ReadBytes(int(headlength)), &head)
if err != nil {
return nil, nil, err
}
err = proto.Unmarshal(reader.ReadBytes(int(bodylength)), body)
head = &highway.RespDataHighwayHead{}
headraw := reader.ReadBytes(int(int64(headlength)))
fmt.Println("headraw", headraw)
err = proto.Unmarshal(headraw, head)
if err != nil {
return nil, nil, err
}
body = binary.NewReader(reader.ReadBytes(int(bodylength)))
if reader.ReadBytes(1)[0] == 0x29 {
return head, body, nil
}
Expand Down
12 changes: 12 additions & 0 deletions client/message.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"fmt"
message2 "github.com/LagrangeDev/LagrangeGo/message"
"github.com/LagrangeDev/LagrangeGo/packets/pb/action"
"github.com/LagrangeDev/LagrangeGo/packets/pb/message"
Expand Down Expand Up @@ -35,6 +36,7 @@ func (c *QQClient) SendRawMessage(route *message.RoutingHead, body *message.Mess
if err != nil {
return
}
resp = &action.SendMessageResponse{}
err = proto.Unmarshal(packet.Data, resp)
if err != nil {
return
Expand Down Expand Up @@ -85,6 +87,16 @@ func preprocessMessage(client *QQClient, groupUin uint32, elements []message2.IM
elem.Display = "@" + member.MemberName
}
}
case *message2.GroupImageElement:
_, err := client.ImageUploadGroup(groupUin, elem)
if err != nil {
fmt.Println(err)
}
if elem.MsgInfo == nil {
fmt.Println("ImageUploadGroup failed")
continue
}
fmt.Println("Image MsgInfo: ", elem.MsgInfo)
default:
}
}
Expand Down
45 changes: 29 additions & 16 deletions message/elements.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package message

import (
"fmt"
"strconv"

"github.com/LagrangeDev/LagrangeGo/packets/pb/message"
Expand Down Expand Up @@ -108,11 +109,11 @@ func (e *ShortVideoElement) Type() ElementType {
return Video
}

func (e *TextElement) BuildElement() *message.Elem {
return &message.Elem{Text: &message.Text{Str: proto.Some(e.Content)}}
func (e *TextElement) BuildElement() []*message.Elem {
return []*message.Elem{{Text: &message.Text{Str: proto.Some(e.Content)}}}
}

func (e *AtElement) BuildElement() *message.Elem {
func (e *AtElement) BuildElement() []*message.Elem {
var atAll int32 = 2
if e.Target == 0 {
atAll = 1
Expand All @@ -124,13 +125,13 @@ func (e *AtElement) BuildElement() *message.Elem {
Uid: proto.Some(e.UID),
}
reserveData, _ := proto.Marshal(&reserve)
return &message.Elem{Text: &message.Text{
return []*message.Elem{{Text: &message.Text{
Str: proto.Some(e.Display),
PbReserve: reserveData,
}}
}}}
}

func (e *FaceElement) BuildElement() *message.Elem {
func (e *FaceElement) BuildElement() []*message.Elem {
faceId := int32(e.FaceID)
if e.isLargeFace {
qFace := message.QFaceExtra{
Expand All @@ -144,36 +145,48 @@ func (e *FaceElement) BuildElement() *message.Elem {
Field9: proto.Some(int32(1)),
}
qFaceData, _ := proto.Marshal(&qFace)
return &message.Elem{
return []*message.Elem{{
CommonElem: &message.CommonElem{
ServiceType: 37,
PbElem: qFaceData,
BusinessType: 1,
},
}
}}
} else {
return &message.Elem{
return []*message.Elem{{
Face: &message.Face{Index: proto.Some(faceId)},
}
}}
}
}

func (e *GroupImageElement) BuildElement() *message.Elem {
return nil
func (e *GroupImageElement) BuildElement() []*message.Elem {
common, err := proto.Marshal(e.MsgInfo)
if err != nil {
fmt.Println("ImageBuild Common Proto Marshall failed:", err)
return nil
}
msg := []*message.Elem{{
CommonElem: &message.CommonElem{
ServiceType: 48,
PbElem: common,
BusinessType: 10,
},
}}
return msg
}

func (e *FriendImageElement) BuildElement() *message.Elem {
func (e *FriendImageElement) BuildElement() []*message.Elem {
return nil
}

func (e *ReplyElement) BuildElement() *message.Elem {
func (e *ReplyElement) BuildElement() []*message.Elem {
return nil
}

func (e *VoiceElement) BuildElement() *message.Elem {
func (e *VoiceElement) BuildElement() []*message.Elem {
return nil
}

func (e *ShortVideoElement) BuildElement() *message.Elem {
func (e *ShortVideoElement) BuildElement() []*message.Elem {
return nil
}
4 changes: 2 additions & 2 deletions message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func BuildMessageElements(msgElems []IMessageElement) (msgBody *message.MessageB
}
elems := make([]*message.Elem, 0, len(msgElems))
for _, elem := range msgElems {
var pb *message.Elem
var pb []*message.Elem
switch e := elem.(type) {
case *TextElement:
pb = e.BuildElement()
Expand All @@ -337,7 +337,7 @@ func BuildMessageElements(msgElems []IMessageElement) (msgBody *message.MessageB
default:
continue
}
elems = append(elems, pb)
elems = append(elems, pb...)
}
msgBody = &message.MessageBody{
RichText: &message.RichText{Elems: elems},
Expand Down

0 comments on commit be32328

Please sign in to comment.