gmessages/libgm/message_builder.go

187 lines
4.5 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
replyToMessageID string
2023-06-30 09:54:08 +00:00
images []*MediaUpload
err 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) GetConversationID() string {
return mb.conversationID
}
func (mb *MessageBuilder) GetSelfParticipantID() string {
return mb.selfParticipantID
}
func (mb *MessageBuilder) GetTmpID() string {
return mb.tmpID
}
2023-06-30 09:54:08 +00:00
func (mb *MessageBuilder) SetContent(content string) *MessageBuilder {
mb.content = content
return mb
}
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
}
// 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
}
// messageID of the message to reply to
func (mb *MessageBuilder) SetReplyMessage(messageId string) *MessageBuilder {
mb.replyToMessageID = messageId
return mb
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 {
convId := mb.GetConversationID()
2023-06-30 09:54:08 +00:00
content := mb.GetContent()
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{
ConversationID: convId,
2023-06-30 09:54:08 +00:00
MessagePayload: &binary.MessagePayload{
TmpID: tmpId,
ConversationID: convId,
SelfParticipantID: selfParticipantId,
2023-06-30 09:54:08 +00:00
MessageInfo: messageInfo,
TmpID2: tmpId,
2023-06-30 09:54:08 +00:00
},
TmpID: tmpId,
2023-06-30 09:54:08 +00:00
}
2023-06-30 09:54:08 +00:00
if len(content) > 0 {
sendMsgPayload.MessagePayload.MessagePayloadContent = &binary.MessagePayloadContent{
MessageContent: &binary.MessageContent{
Content: content,
},
}
}
if mb.replyToMessageID != "" {
sendMsgPayload.IsReply = true
sendMsgPayload.Reply = &binary.ReplyPayload{MessageID: mb.replyToMessageID}
}
2023-06-30 09:54:08 +00:00
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_MediaContent{
MediaContent: &binary.MediaContent{
Format: binary.MediaFormats(media.Image.GetImageType().Type),
MediaID: media.MediaID,
MediaName: media.Image.GetImageName(),
2023-06-30 09:54:08 +00:00
Size: media.Image.GetImageSize(),
DecryptionKey: media.Image.GetImageCryptor().GetKey(),
},
},
}
mb.client.Logger.Debug().Any("imageMessage", imageMessage).Msg("New Media Content")
2023-06-30 09:54:08 +00:00
return imageMessage
}