Skip to content

Commit d83b63a

Browse files
authored
client: add wrapper for /knock endpoint (#359)
1 parent 0f4c560 commit d83b63a

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

Diff for: client.go

+22
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,28 @@ func (cli *Client) JoinRoom(ctx context.Context, roomIDorAlias string, req *ReqJ
974974
return
975975
}
976976

977+
// KnockRoom requests to join a room ID or alias. See https://spec.matrix.org/v1.13/client-server-api/#post_matrixclientv3knockroomidoralias
978+
//
979+
// The last parameter contains optional extra fields and can be left nil.
980+
func (cli *Client) KnockRoom(ctx context.Context, roomIDorAlias string, req *ReqKnockRoom) (resp *RespKnockRoom, err error) {
981+
if req == nil {
982+
req = &ReqKnockRoom{}
983+
}
984+
urlPath := cli.BuildURLWithFullQuery(ClientURLPath{"v3", "knock", roomIDorAlias}, func(q url.Values) {
985+
if len(req.Via) > 0 {
986+
q["via"] = req.Via
987+
}
988+
})
989+
_, err = cli.MakeRequest(ctx, http.MethodPost, urlPath, req, &resp)
990+
if err == nil && cli.StateStore != nil {
991+
err = cli.StateStore.SetMembership(ctx, resp.RoomID, cli.UserID, event.MembershipKnock)
992+
if err != nil {
993+
err = fmt.Errorf("failed to update state store: %w", err)
994+
}
995+
}
996+
return
997+
}
998+
977999
// JoinRoomByID joins the client to a room ID. See https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3roomsroomidjoin
9781000
//
9791001
// Unlike JoinRoom, this method can only be used to join rooms that the server already knows about.

Diff for: requests.go

+5
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ type ReqJoinRoom struct {
156156
ThirdPartySigned any `json:"third_party_signed,omitempty"`
157157
}
158158

159+
type ReqKnockRoom struct {
160+
Via []string `json:"-"`
161+
Reason string `json:"reason,omitempty"`
162+
}
163+
159164
type ReqMutualRooms struct {
160165
From string `json:"-"`
161166
}

Diff for: responses.go

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ type RespJoinRoom struct {
3333
RoomID id.RoomID `json:"room_id"`
3434
}
3535

36+
// RespKnockRoom is the JSON response for https://spec.matrix.org/v1.13/client-server-api/#post_matrixclientv3knockroomidoralias
37+
type RespKnockRoom struct {
38+
RoomID id.RoomID `json:"room_id"`
39+
}
40+
3641
// RespLeaveRoom is the JSON response for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3roomsroomidleave
3742
type RespLeaveRoom struct{}
3843

0 commit comments

Comments
 (0)