-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase_reaction_handler.go
More file actions
64 lines (57 loc) · 2.2 KB
/
base_reaction_handler.go
File metadata and controls
64 lines (57 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package agentremote
import (
"context"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/database"
)
// ReactionTarget provides the bridge-specific context that BaseReactionHandler
// needs to validate and route approval reactions.
type ReactionTarget interface {
GetUserLogin() *bridgev2.UserLogin
GetApprovalHandler() ApprovalReactionHandler
}
// BaseReactionHandler is an embeddable mixin that implements the three reaction
// interface methods (PreHandleMatrixReaction, HandleMatrixReaction,
// HandleMatrixReactionRemove) for bridges whose reaction handling is limited to
// approval prompt reactions.
type BaseReactionHandler struct {
Target ReactionTarget
}
func (h BaseReactionHandler) PreHandleMatrixReaction(_ context.Context, msg *bridgev2.MatrixReaction) (bridgev2.MatrixReactionPreResponse, error) {
return PreHandleApprovalReaction(msg)
}
func (h BaseReactionHandler) HandleMatrixReaction(ctx context.Context, msg *bridgev2.MatrixReaction) (*database.Reaction, error) {
if h.Target == nil || msg == nil || msg.Event == nil || msg.Portal == nil {
return &database.Reaction{}, nil
}
login := h.Target.GetUserLogin()
if login != nil && IsMatrixBotUser(ctx, login.Bridge, msg.Event.Sender) {
return &database.Reaction{}, nil
}
// Best-effort persistence guard for reaction.sender_id -> ghost.id FK.
if err := EnsureSyntheticReactionSenderGhost(ctx, login, msg.Event.Sender); err != nil {
logger := loggerForLogin(ctx, login)
logEvt := logger.Warn().Err(err).Stringer("sender_mxid", msg.Event.Sender)
if login != nil {
logEvt = logEvt.Str("user_login_id", string(login.ID))
}
logEvt.Msg("Failed to ensure synthetic Matrix reaction sender ghost")
}
if handler := h.Target.GetApprovalHandler(); handler != nil {
handler.HandleReaction(ctx, msg)
}
return &database.Reaction{}, nil
}
func (h BaseReactionHandler) HandleMatrixReactionRemove(ctx context.Context, msg *bridgev2.MatrixReactionRemove) error {
if h.Target == nil || msg == nil {
return nil
}
approvalHandler := h.Target.GetApprovalHandler()
if approvalHandler == nil {
return nil
}
if handler, ok := approvalHandler.(ApprovalReactionRemoveHandler); ok {
handler.HandleReactionRemove(ctx, msg)
}
return nil
}