MCPcopy Create free account
hub / github.com/daodst/chat / JoinRoomByIDOrAlias

Function JoinRoomByIDOrAlias

clientapi/routing/joinroom.go:29–109  ·  view source on GitHub ↗
(
	req *http.Request,
	device *api.Device,
	rsAPI roomserverAPI.ClientRoomserverAPI,
	profileAPI api.ClientUserAPI,
	roomIDOrAlias string,
)

Source from the content-addressed store, hash-verified

27)
28
29func JoinRoomByIDOrAlias(
30 req *http.Request,
31 device *api.Device,
32 rsAPI roomserverAPI.ClientRoomserverAPI,
33 profileAPI api.ClientUserAPI,
34 roomIDOrAlias string,
35) util.JSONResponse {
36 // Prepare to ask the roomserver to perform the room join.
37 joinReq := roomserverAPI.PerformJoinRequest{
38 RoomIDOrAlias: roomIDOrAlias,
39 UserID: device.UserID,
40 Content: map[string]interface{}{},
41 }
42 joinRes := roomserverAPI.PerformJoinResponse{}
43
44 // Check to see if any ?server_name= query parameters were
45 // given in the request.
46 if serverNames, ok := req.URL.Query()["server_name"]; ok {
47 for _, serverName := range serverNames {
48 joinReq.ServerNames = append(
49 joinReq.ServerNames,
50 gomatrixserverlib.ServerName(serverName),
51 )
52 }
53 }
54
55 // If content was provided in the request then include that
56 // in the request. It'll get used as a part of the membership
57 // event content.
58 _ = httputil.UnmarshalJSONRequest(req, &joinReq.Content)
59
60 // Work out our localpart for the client profile request.
61
62 // Request our profile content to populate the request content with.
63 res := &api.QueryProfileResponse{}
64 err := profileAPI.QueryProfile(req.Context(), &api.QueryProfileRequest{UserID: device.UserID}, res)
65 if err != nil || !res.UserExists {
66 if !res.UserExists {
67 util.GetLogger(req.Context()).Error("Unable to query user profile, no profile found.")
68 return util.JSONResponse{
69 Code: http.StatusInternalServerError,
70 JSON: jsonerror.Unknown("Unable to query user profile, no profile found."),
71 }
72 }
73
74 util.GetLogger(req.Context()).WithError(err).Error("UserProfileAPI.QueryProfile failed")
75 } else {
76 joinReq.Content["displayname"] = res.DisplayName
77 joinReq.Content["avatar_url"] = res.AvatarURL
78 }
79
80 // Ask the roomserver to perform the join.
81 done := make(chan util.JSONResponse, 1)
82 go func() {
83 defer close(done)
84 rsAPI.PerformJoin(req.Context(), &joinReq, &joinRes)
85 if joinRes.Error != nil {
86 done <- joinRes.Error.JSONResponse()

Callers 1

SetupFunction · 0.85

Calls 5

ContextMethod · 0.80
JSONResponseMethod · 0.80
QueryProfileMethod · 0.65
PerformJoinMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected