Skip to content

Commit

Permalink
bridgev2/simplevent: add per-message sender support
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <[email protected]>
  • Loading branch information
sumnerevans committed Jan 9, 2025
1 parent e571946 commit 19c7e5d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
5 changes: 5 additions & 0 deletions bridgev2/networkinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,11 @@ type RemoteMessageWithTransactionID interface {
GetTransactionID() networkid.TransactionID
}

type RemoteMessageWithPerMessageSender interface {
RemoteMessage
GetPerMessageSender() *EventSender
}

type RemoteEdit interface {
RemoteEventWithTargetMessage
ConvertEdit(ctx context.Context, portal *Portal, intent MatrixAPI, existing []*database.Message) (*ConvertedEdit, error)
Expand Down
18 changes: 18 additions & 0 deletions bridgev2/portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2014,6 +2014,24 @@ func (portal *Portal) handleRemoteMessage(ctx context.Context, source *UserLogin
}
return
}
if messageWithPerMessageSender, ok := evt.(RemoteMessageWithPerMessageSender); ok {
perMessageGhost, err := portal.Bridge.GetGhostByID(ctx, messageWithPerMessageSender.GetSender().Sender)
if err != nil {
log.Err(err).Msg("Failed to get per-message sender")
portal.sendRemoteErrorNotice(ctx, intent, err, ts, "message")
return
}
perMessageProfile := event.BeeperPerMessageProfile{
ID: string(perMessageGhost.Intent.GetMXID()),
Displayname: perMessageGhost.Name,
}
if perMessageGhost.AvatarMXC != "" {
perMessageProfile.AvatarURL = &perMessageGhost.AvatarMXC
}
for _, part := range converted.Parts {
part.Content.BeeperPerMessageProfile = &perMessageProfile
}
}
portal.sendConvertedMessage(ctx, evt.GetID(), intent, evt.GetSender().Sender, converted, ts, getStreamOrder(evt), nil)
}

Expand Down
15 changes: 11 additions & 4 deletions bridgev2/simplevent/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ type Message[T any] struct {
TransactionID networkid.TransactionID
TargetMessage networkid.MessageID

PerMessageSender *bridgev2.EventSender

ConvertMessageFunc func(ctx context.Context, portal *bridgev2.Portal, intent bridgev2.MatrixAPI, data T) (*bridgev2.ConvertedMessage, error)
ConvertEditFunc func(ctx context.Context, portal *bridgev2.Portal, intent bridgev2.MatrixAPI, existing []*database.Message, data T) (*bridgev2.ConvertedEdit, error)
HandleExistingFunc func(ctx context.Context, portal *bridgev2.Portal, intent bridgev2.MatrixAPI, existing []*database.Message, data T) (bridgev2.UpsertResult, error)
}

var (
_ bridgev2.RemoteMessage = (*Message[any])(nil)
_ bridgev2.RemoteEdit = (*Message[any])(nil)
_ bridgev2.RemoteMessageUpsert = (*Message[any])(nil)
_ bridgev2.RemoteMessageWithTransactionID = (*Message[any])(nil)
_ bridgev2.RemoteMessage = (*Message[any])(nil)
_ bridgev2.RemoteEdit = (*Message[any])(nil)
_ bridgev2.RemoteMessageUpsert = (*Message[any])(nil)
_ bridgev2.RemoteMessageWithTransactionID = (*Message[any])(nil)
_ bridgev2.RemoteMessageWithPerMessageSender = (*Message[any])(nil)
)

func (evt *Message[T]) ConvertMessage(ctx context.Context, portal *bridgev2.Portal, intent bridgev2.MatrixAPI) (*bridgev2.ConvertedMessage, error) {
Expand All @@ -43,6 +46,10 @@ func (evt *Message[T]) ConvertEdit(ctx context.Context, portal *bridgev2.Portal,
return evt.ConvertEditFunc(ctx, portal, intent, existing, evt.Data)
}

func (evt *Message[T]) GetPerMessageSender() *bridgev2.EventSender {
return evt.PerMessageSender
}

func (evt *Message[T]) HandleExisting(ctx context.Context, portal *bridgev2.Portal, intent bridgev2.MatrixAPI, existing []*database.Message) (bridgev2.UpsertResult, error) {
return evt.HandleExistingFunc(ctx, portal, intent, existing, evt.Data)
}
Expand Down

0 comments on commit 19c7e5d

Please sign in to comment.