Skip to content

Commit

Permalink
handlematrix,handletwit: Edit messages and receive edits
Browse files Browse the repository at this point in the history
  • Loading branch information
PurpShell committed Oct 11, 2024
1 parent 76ce8db commit 0b610fe
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
22 changes: 18 additions & 4 deletions pkg/connector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/rs/zerolog"
"maunium.net/go/mautrix/bridge/status"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/database"
"maunium.net/go/mautrix/bridgev2/networkid"
bridgeEvt "maunium.net/go/mautrix/event"

Expand All @@ -45,10 +47,8 @@ type TwitterClient struct {
}

var (
_ bridgev2.NetworkAPI = (*TwitterClient)(nil)
_ bridgev2.ReactionHandlingNetworkAPI = (*TwitterClient)(nil)
_ bridgev2.ReadReceiptHandlingNetworkAPI = (*TwitterClient)(nil)
_ bridgev2.PushableNetworkAPI = (*TwitterClient)(nil)
_ bridgev2.NetworkAPI = (*TwitterClient)(nil)
_ bridgev2.PushableNetworkAPI = (*TwitterClient)(nil)
)

func NewTwitterClient(ctx context.Context, tc *TwitterConnector, login *bridgev2.UserLogin) *TwitterClient {
Expand Down Expand Up @@ -175,13 +175,27 @@ func (tc *TwitterClient) GetCapabilities(ctx context.Context, portal *bridgev2.P
UserMentions: true,
RoomMentions: false,

Edits: true,
EditMaxCount: 10,
EditMaxAge: 15 * time.Minute,
Captions: true,
Replies: true,
Reactions: true,
ReactionCount: 1,
}
}

func (tc *TwitterClient) convertEditToMatrix(ctx context.Context, portal *bridgev2.Portal, intent bridgev2.MatrixAPI, existing []*database.Message, data *event.XEventMessage) (*bridgev2.ConvertedEdit, error) {
data.Text = strings.TrimPrefix(data.Text, "Edited: ")
converted, err := tc.convertToMatrix(ctx, portal, intent, data)
if err != nil {
return nil, err
}
return &bridgev2.ConvertedEdit{
ModifiedParts: []*bridgev2.ConvertedEditPart{converted.Parts[0].ToEditPart(existing[0])},
}, nil
}

func (tc *TwitterClient) convertToMatrix(ctx context.Context, portal *bridgev2.Portal, intent bridgev2.MatrixAPI, msg *event.XEventMessage) (*bridgev2.ConvertedMessage, error) {
partId := networkid.PartID("")
var MessageOptionalPartID *networkid.MessageOptionalPartID
Expand Down
17 changes: 17 additions & 0 deletions pkg/connector/handlematrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package connector
import (
"context"

"github.com/google/uuid"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/database"
"maunium.net/go/mautrix/bridgev2/networkid"
Expand All @@ -22,6 +23,12 @@ var (
}
)

var (
_ bridgev2.ReactionHandlingNetworkAPI = (*TwitterClient)(nil)
_ bridgev2.ReadReceiptHandlingNetworkAPI = (*TwitterClient)(nil)
_ bridgev2.EditHandlingNetworkAPI = (*TwitterClient)(nil)
)

func (tc *TwitterClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.MatrixMessage) (message *bridgev2.MatrixMessageResponse, err error) {
conversationId := string(msg.Portal.ID)
sendDMPayload := &payload.SendDirectMessagePayload{
Expand Down Expand Up @@ -141,3 +148,13 @@ func (tc *TwitterClient) HandleMatrixReadReceipt(ctx context.Context, msg *bridg

return tc.client.MarkConversationRead(params)
}

func (tc *TwitterClient) HandleMatrixEdit(_ context.Context, edit *bridgev2.MatrixEdit) error {
_, err := tc.client.EditDirectMessage(&payload.EditDirectMessagePayload{
ConversationID: string(edit.Portal.ID),
RequestID: uuid.New().String(),
DmID: string(edit.EditTarget.ID),
Text: edit.Content.Body,
})
return err
}
12 changes: 9 additions & 3 deletions pkg/connector/handletwit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ func (tc *TwitterClient) HandleTwitterEvent(rawEvt any) {
case event.XEventMessage:
sender := evtData.Sender
isFromMe := sender.IDStr == string(tc.userLogin.ID)
msgType := bridgev2.RemoteEventMessage

if evtData.EditCount > 0 {
msgType = bridgev2.RemoteEventEdit
}
tc.connector.br.QueueRemoteEvent(tc.userLogin, &simplevent.Message[*event.XEventMessage]{
EventMeta: simplevent.EventMeta{
Type: bridgev2.RemoteEventMessage,
Type: msgType,
LogContext: func(c zerolog.Context) zerolog.Context {
return c.
Str("message_id", evtData.MessageID).
Expand All @@ -34,10 +39,11 @@ func (tc *TwitterClient) HandleTwitterEvent(rawEvt any) {
},
Timestamp: evtData.CreatedAt,
},
ID: networkid.MessageID(evtData.MessageID),

ID: networkid.MessageID(evtData.MessageID),
TargetMessage: networkid.MessageID(evtData.MessageID),
Data: &evtData,
ConvertMessageFunc: tc.convertToMatrix,
ConvertEditFunc: tc.convertEditToMatrix,
})
case event.XEventReaction:
reactionRemoteEvent := tc.wrapReaction(evtData)
Expand Down

0 comments on commit 0b610fe

Please sign in to comment.