2023-07-09 11:16:52 +00:00
|
|
|
package libgm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
|
2023-07-09 11:16:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ConversationBuilderError struct {
|
|
|
|
errMsg string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cbe *ConversationBuilderError) Error() string {
|
|
|
|
return fmt.Sprintf("Failed to build conversation builder: %s", cbe.errMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConversationBuilder struct {
|
|
|
|
conversationId string
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
actionStatus gmproto.ConversationActionStatus
|
|
|
|
status gmproto.ConversationStatus
|
|
|
|
muteStatus *gmproto.ConversationMuteStatus
|
2023-07-09 11:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cb *ConversationBuilder) SetConversationId(conversationId string) *ConversationBuilder {
|
|
|
|
cb.conversationId = conversationId
|
|
|
|
return cb
|
|
|
|
}
|
|
|
|
|
|
|
|
// For block, unblock, block & report
|
2023-07-17 13:51:31 +00:00
|
|
|
func (cb *ConversationBuilder) SetConversationActionStatus(actionStatus gmproto.ConversationActionStatus) *ConversationBuilder {
|
2023-07-09 11:16:52 +00:00
|
|
|
cb.actionStatus = actionStatus
|
|
|
|
return cb
|
|
|
|
}
|
|
|
|
|
|
|
|
// For archive, unarchive, delete
|
2023-07-17 13:51:31 +00:00
|
|
|
func (cb *ConversationBuilder) SetConversationStatus(status gmproto.ConversationStatus) *ConversationBuilder {
|
2023-07-09 11:16:52 +00:00
|
|
|
cb.status = status
|
|
|
|
return cb
|
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
func (cb *ConversationBuilder) SetMuteStatus(muteStatus *gmproto.ConversationMuteStatus) *ConversationBuilder {
|
2023-07-09 11:16:52 +00:00
|
|
|
cb.muteStatus = muteStatus
|
|
|
|
return cb
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cb *ConversationBuilder) Build(protoMessage proto.Message) (proto.Message, error) {
|
|
|
|
if cb.conversationId == "" {
|
|
|
|
return nil, &ConversationBuilderError{errMsg: "conversationID can not be empty"}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch protoMessage.(type) {
|
2023-07-17 23:57:20 +00:00
|
|
|
case *gmproto.UpdateConversationRequest:
|
2023-07-09 11:16:52 +00:00
|
|
|
payload, failedBuild := cb.buildUpdateConversationPayload()
|
|
|
|
if failedBuild != nil {
|
|
|
|
return nil, failedBuild
|
|
|
|
}
|
|
|
|
return payload, nil
|
|
|
|
default:
|
2023-07-09 20:32:19 +00:00
|
|
|
panic("Invalid protoMessage conversation builder type")
|
2023-07-09 11:16:52 +00:00
|
|
|
}
|
|
|
|
return nil, &ConversationBuilderError{errMsg: "failed to build for unknown reasons"}
|
|
|
|
}
|
|
|
|
|
2023-07-17 23:57:20 +00:00
|
|
|
func (cb *ConversationBuilder) buildUpdateConversationPayload() (*gmproto.UpdateConversationRequest, error) {
|
2023-07-09 11:16:52 +00:00
|
|
|
if cb.actionStatus == 0 && cb.status == 0 && cb.muteStatus == nil {
|
|
|
|
return nil, &ConversationBuilderError{errMsg: "actionStatus, status & muteStatus can not be empty when updating conversation, set atleast 1"}
|
|
|
|
}
|
|
|
|
|
2023-07-17 23:57:20 +00:00
|
|
|
payload := &gmproto.UpdateConversationRequest{}
|
2023-07-09 11:16:52 +00:00
|
|
|
|
|
|
|
if cb.actionStatus != 0 {
|
|
|
|
payload.Action = cb.actionStatus
|
2023-07-17 13:51:31 +00:00
|
|
|
payload.Action5 = &gmproto.ConversationAction5{
|
2023-07-09 11:16:52 +00:00
|
|
|
Field2: true,
|
|
|
|
}
|
|
|
|
payload.ConversationID = cb.conversationId
|
|
|
|
} else if cb.status != 0 || cb.muteStatus != nil {
|
2023-07-17 13:51:31 +00:00
|
|
|
payload.Data = &gmproto.UpdateConversationData{ConversationID: cb.conversationId}
|
2023-07-09 11:16:52 +00:00
|
|
|
if cb.muteStatus != nil {
|
2023-07-17 13:51:31 +00:00
|
|
|
payload.Data.Data = &gmproto.UpdateConversationData_Mute{Mute: *cb.muteStatus}
|
2023-07-09 11:16:52 +00:00
|
|
|
} else if cb.status != 0 {
|
2023-07-17 13:51:31 +00:00
|
|
|
payload.Data.Data = &gmproto.UpdateConversationData_Status{Status: cb.status}
|
2023-07-09 11:16:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) NewConversationBuilder() *ConversationBuilder {
|
|
|
|
return &ConversationBuilder{}
|
|
|
|
}
|