Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: implement HandleMatrixMembership #765

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions pkg/connector/handlematrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,51 @@ func (wa *WhatsAppClient) HandleMatrixTyping(ctx context.Context, msg *bridgev2.
}
return wa.Client.SendChatPresence(portalJID, chatPresence, mediaPresence)
}

func (wa *WhatsAppClient) HandleMatrixMembership(ctx context.Context, msg *bridgev2.MatrixMembershipChange) (bool, error) {
portalJID, err := waid.ParsePortalID(msg.Portal.ID)
if err != nil {
return false, err
}

if msg.Portal.RoomType == database.RoomTypeDM {
switch msg.Type {
case bridgev2.Invite:
return false, fmt.Errorf("cannot invite additional user to dm")
default:
return false, nil
}
}

changes := make([]types.JID, 1)
var action whatsmeow.ParticipantChange

switch msg.Type {
case bridgev2.Invite:
action = whatsmeow.ParticipantChangeAdd
case bridgev2.Leave, bridgev2.Kick:
action = whatsmeow.ParticipantChangeRemove
default:
return false, nil
}

switch target := msg.Target.(type) {
case *bridgev2.Ghost:
changes[0] = waid.ParseUserID(target.ID)
case *bridgev2.UserLogin:
ghost, err := target.Bridge.GetGhostByID(ctx, networkid.UserID(target.ID))
if err != nil {
return false, fmt.Errorf("failed to get ghost for user: %w", err)
}
changes[0] = waid.ParseUserID(ghost.ID)
default:
return false, fmt.Errorf("cannot get target intent: unknown type: %T", target)
}

_, err = wa.Client.UpdateGroupParticipants(portalJID, changes, action)
if err != nil {
return false, err
}

return true, nil
}
Loading