Skip to content

Commit 1dbad4b

Browse files
authored
handlematrix: support inviting user to group chat (#82)
1 parent 32c1638 commit 1dbad4b

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

pkg/connector/handlematrix.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var (
4949
_ bridgev2.TypingHandlingNetworkAPI = (*TwitterClient)(nil)
5050
_ bridgev2.ChatViewingNetworkAPI = (*TwitterClient)(nil)
5151
_ bridgev2.DeleteChatHandlingNetworkAPI = (*TwitterClient)(nil)
52+
_ bridgev2.MembershipHandlingNetworkAPI = (*TwitterClient)(nil)
5253
_ bridgev2.RoomAvatarHandlingNetworkAPI = (*TwitterClient)(nil)
5354
)
5455

@@ -290,3 +291,28 @@ func (tc *TwitterClient) HandleMatrixRoomAvatar(ctx context.Context, msg *bridge
290291
}
291292
return false, errors.New("avatar not found")
292293
}
294+
295+
func (tc *TwitterClient) HandleMatrixMembership(ctx context.Context, msg *bridgev2.MatrixMembershipChange) (bool, error) {
296+
if msg.Type != bridgev2.Invite {
297+
return false, errors.New("unsupported membership change type")
298+
}
299+
if msg.Portal.RoomType == database.RoomTypeDM {
300+
return false, errors.New("cannot change members for DM")
301+
}
302+
303+
var participantID string
304+
switch target := msg.Target.(type) {
305+
case *bridgev2.Ghost:
306+
participantID = string(target.ID)
307+
case *bridgev2.UserLogin:
308+
participantID = string(target.ID)
309+
}
310+
_, err := tc.client.AddParticipants(ctx, &payload.AddParticipantsPayload{
311+
ConversationID: string(msg.Portal.ID),
312+
AddedParticipants: []string{participantID},
313+
})
314+
if err != nil {
315+
return false, err
316+
}
317+
return true, nil
318+
}

pkg/twittermeow/data/endpoints/endpoints.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const (
3333
PIN_CONVERSATION_URL = BASE_URL + "/i/api/graphql/o0aymgGiJY-53Y52YSUGVA/DMPinnedInboxAppend_Mutation"
3434
UNPIN_CONVERSATION_URL = BASE_URL + "/i/api/graphql/_TQxP2Rb0expwVP9ktGrTQ/DMPinnedInboxDelete_Mutation"
3535
GET_PINNED_CONVERSATIONS_URL = BASE_URL + "/i/api/graphql/_gBQBgClVuMQb8efxWkbbQ/DMPinnedInboxQuery"
36+
ADD_PARTICIPANTS_URL = BASE_URL + "/i/api/graphql/oBwyQ0_xVbAQ8FAyG0pCRA/AddParticipantsMutation"
3637
ADD_REACTION_URL = BASE_URL + "/i/api/graphql/VyDyV9pC2oZEj6g52hgnhA/useDMReactionMutationAddMutation"
3738
REMOVE_REACTION_URL = BASE_URL + "/i/api/graphql/bV_Nim3RYHsaJwMkTXJ6ew/useDMReactionMutationRemoveMutation"
3839
SEND_TYPING_NOTIFICATION = BASE_URL + "/i/api/graphql/HL96-xZ3Y81IEzAdczDokg/useTypingNotifierMutation"

pkg/twittermeow/data/payload/json.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ type PinAndUnpinConversationVariables struct {
5050
Label LabelType `json:"label,omitempty"`
5151
}
5252

53+
type AddParticipantsPayload struct {
54+
ConversationID string `json:"conversationId"`
55+
AddedParticipants []string `json:"addedParticipants"`
56+
}
57+
5358
type ReactionActionPayload struct {
5459
ConversationID string `json:"conversationId"`
5560
MessageID string `json:"messageId"`

pkg/twittermeow/data/response/messaging.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,12 @@ type ReactionResponse struct {
6666
} `json:"create_dm_reaction,omitempty"`
6767
} `json:"data,omitempty"`
6868
}
69+
70+
type AddParticipantsResponse struct {
71+
Data struct {
72+
AddParticipants struct {
73+
Typename string `json:"__typename,omitempty"`
74+
AddedUsers []string `json:"added_users,omitempty"`
75+
} `json:"add_participants,omitempty"`
76+
} `json:"data,omitempty"`
77+
}

pkg/twittermeow/messaging.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,32 @@ func (c *Client) UpdateConversationAvatar(ctx context.Context, conversationID st
374374

375375
return nil
376376
}
377+
378+
func (c *Client) AddParticipants(ctx context.Context, variables *payload.AddParticipantsPayload) (*response.AddParticipantsResponse, error) {
379+
graphQlPayload := payload.GraphQLPayload{
380+
Variables: variables,
381+
QueryID: "oBwyQ0_xVbAQ8FAyG0pCRA",
382+
}
383+
384+
url := endpoints.ADD_PARTICIPANTS_URL
385+
386+
jsonBody, err := graphQlPayload.Encode()
387+
if err != nil {
388+
return nil, err
389+
}
390+
391+
_, respBody, err := c.makeAPIRequest(ctx, apiRequestOpts{
392+
URL: url,
393+
Method: http.MethodPost,
394+
WithClientUUID: true,
395+
Body: jsonBody,
396+
Origin: endpoints.BASE_URL,
397+
ContentType: types.ContentTypeJSON,
398+
})
399+
if err != nil {
400+
return nil, err
401+
}
402+
403+
data := response.AddParticipantsResponse{}
404+
return &data, json.Unmarshal(respBody, &data)
405+
}

0 commit comments

Comments
 (0)