Skip to content

Commit

Permalink
fix: message element interface & sendingMessage func
Browse files Browse the repository at this point in the history
  • Loading branch information
Redmomn committed Apr 20, 2024
1 parent 8475ae7 commit 609f523
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ type (

IMessageElement interface {
Type() ElementType
BuildElement() *message.Elem
}

ElementType int
Expand Down Expand Up @@ -297,6 +296,10 @@ func NewSendingMessage() *SendingMessage {
return &SendingMessage{}
}

func (msg *SendingMessage) GetElems() []IMessageElement {
return msg.Elements
}

// Append 要传入msg的引用
func (msg *SendingMessage) Append(e IMessageElement) *SendingMessage {
v := reflect.ValueOf(e)
Expand All @@ -306,14 +309,35 @@ func (msg *SendingMessage) Append(e IMessageElement) *SendingMessage {
return msg
}

func (msg *SendingMessage) FirstOrNil(f func(element IMessageElement) bool) IMessageElement {
for _, elem := range msg.Elements {
if f(elem) {
return elem
}
}
return nil
}

func BuildMessageElements(msgElems []IMessageElement) (msgBody *message.MessageBody) {
if len(msgElems) == 0 {
return
}
elems := make([]*message.Elem, 0, len(msgElems))
for _, elem := range msgElems {
// TODO 涉及到uid的东西应该在这里处理一下
elems = append(elems, elem.BuildElement())
var pb *message.Elem
switch e := elem.(type) {
case *TextElement:
pb = e.BuildElement()
case *AtElement:
pb = e.BuildElement()
case *ReplyElement:
pb = e.BuildElement()
case *FaceElement:
pb = e.BuildElement()
default:
continue
}
elems = append(elems, pb)
}
msgBody = &message.MessageBody{
RichText: &message.RichText{Elems: elems},
Expand Down

0 comments on commit 609f523

Please sign in to comment.