gmessages/libgm/message_builder.go

173 lines
4.1 KiB
Go
Raw Normal View History

2023-06-30 11:05:33 +00:00
package libgm
2023-06-30 09:54:08 +00:00
import (
"errors"
"go.mau.fi/mautrix-gmessages/libgm/binary"
"go.mau.fi/mautrix-gmessages/libgm/util"
)
var (
errContentNotSet = errors.New("failed to build MessageBuilder: content must be larger than length 0")
2023-06-30 13:26:46 +00:00
errConversationIdNotSet = errors.New("failed to build MessageBuilder: conversationID is empty")
errSelfParticipantIdNotSet = errors.New("failed to build MessageBuilder: selfParticipantID is empty")
2023-06-30 09:54:08 +00:00
)
type MessageBuilder struct {
client *Client
content string
2023-06-30 13:26:46 +00:00
conversationID string
tmpID string
selfParticipantID string
2023-06-30 09:54:08 +00:00
images []*MediaUpload
err error
}
// Add this method to retrieve the stored error
func (mb *MessageBuilder) Err() error {
return mb.err
}
func (mb *MessageBuilder) GetImages() []*MediaUpload {
return mb.images
}
func (mb *MessageBuilder) GetContent() string {
return mb.content
}
func (mb *MessageBuilder) SetContent(content string) *MessageBuilder {
mb.content = content
return mb
}
2023-06-30 13:26:46 +00:00
func (mb *MessageBuilder) GetConversationID() string {
return mb.conversationID
2023-06-30 09:54:08 +00:00
}
2023-06-30 13:26:46 +00:00
func (mb *MessageBuilder) SetConversationID(conversationId string) *MessageBuilder {
mb.conversationID = conversationId
2023-06-30 09:54:08 +00:00
return mb
}
2023-06-30 13:26:46 +00:00
func (mb *MessageBuilder) GetSelfParticipantID() string {
return mb.selfParticipantID
2023-06-30 09:54:08 +00:00
}
// sendmessage function will set this automatically but if u want to set it yourself feel free
2023-06-30 13:26:46 +00:00
func (mb *MessageBuilder) SetSelfParticipantID(participantId string) *MessageBuilder {
mb.selfParticipantID = participantId
2023-06-30 09:54:08 +00:00
return mb
}
2023-06-30 13:26:46 +00:00
func (mb *MessageBuilder) GetTmpID() string {
return mb.tmpID
2023-06-30 09:54:08 +00:00
}
// sendmessage function will set this automatically but if u want to set it yourself feel free
2023-06-30 13:26:46 +00:00
func (mb *MessageBuilder) SetTmpID(tmpId string) *MessageBuilder {
mb.tmpID = tmpId
2023-06-30 09:54:08 +00:00
return mb
}
func (mb *MessageBuilder) Build() (*binary.SendMessagePayload, error) {
2023-06-30 13:26:46 +00:00
if mb.conversationID == "" {
2023-06-30 09:54:08 +00:00
return nil, errConversationIdNotSet
}
2023-06-30 13:26:46 +00:00
if mb.selfParticipantID == "" {
2023-06-30 09:54:08 +00:00
return nil, errSelfParticipantIdNotSet
}
if mb.content == "" {
return nil, errContentNotSet
}
2023-06-30 13:26:46 +00:00
if mb.tmpID == "" {
mb.tmpID = util.GenerateTmpId()
2023-06-30 09:54:08 +00:00
}
return mb.newSendConversationMessage(), nil
}
func (c *Client) NewMessageBuilder() *MessageBuilder {
mb := &MessageBuilder{
client: c,
}
tmpId := util.GenerateTmpId()
2023-06-30 13:26:46 +00:00
mb.SetTmpID(tmpId)
2023-06-30 09:54:08 +00:00
return mb
}
func (mb *MessageBuilder) newSendConversationMessage() *binary.SendMessagePayload {
2023-06-30 13:26:46 +00:00
convID := mb.GetConversationID()
2023-06-30 09:54:08 +00:00
content := mb.GetContent()
2023-06-30 13:26:46 +00:00
selfParticipantID := mb.GetSelfParticipantID()
tmpID := mb.GetTmpID()
2023-06-30 09:54:08 +00:00
messageInfo := make([]*binary.MessageInfo, 0)
messageInfo = append(messageInfo, &binary.MessageInfo{Data: &binary.MessageInfo_MessageContent{
MessageContent: &binary.MessageContent{
Content: content,
},
}})
mb.appendImagesPayload(&messageInfo)
sendMsgPayload := &binary.SendMessagePayload{
2023-06-30 13:26:46 +00:00
ConversationID: convID,
2023-06-30 09:54:08 +00:00
MessagePayload: &binary.MessagePayload{
2023-06-30 13:26:46 +00:00
TmpID: tmpID,
ConversationID: convID,
SelfParticipantID: selfParticipantID,
2023-06-30 09:54:08 +00:00
MessageInfo: messageInfo,
2023-06-30 13:26:46 +00:00
TmpID2: tmpID,
2023-06-30 09:54:08 +00:00
},
2023-06-30 13:26:46 +00:00
TmpID: tmpID,
2023-06-30 09:54:08 +00:00
}
if len(content) > 0 {
sendMsgPayload.MessagePayload.MessagePayloadContent = &binary.MessagePayloadContent{
MessageContent: &binary.MessageContent{
Content: content,
},
}
}
mb.client.Logger.Debug().Any("sendMsgPayload", sendMsgPayload).Msg("sendMessagePayload")
return sendMsgPayload
}
func (mb *MessageBuilder) appendImagesPayload(messageInfo *[]*binary.MessageInfo) {
if len(mb.images) <= 0 {
return
}
for _, media := range mb.images {
imgData := mb.newImageContent(media)
*messageInfo = append(*messageInfo, imgData)
}
}
func (mb *MessageBuilder) newImageContent(media *MediaUpload) *binary.MessageInfo {
imageMessage := &binary.MessageInfo{
Data: &binary.MessageInfo_ImageContent{
ImageContent: &binary.ImageContent{
SomeNumber: media.Image.GetImageType().Type,
2023-06-30 13:26:46 +00:00
ImageID: media.MediaID,
2023-06-30 09:54:08 +00:00
ImageName: media.Image.GetImageName(),
Size: media.Image.GetImageSize(),
DecryptionKey: media.Image.GetImageCryptor().GetKey(),
},
},
}
mb.client.Logger.Debug().Any("imageMessage", imageMessage).Msg("New Image Content")
return imageMessage
}