2020-07-28 10:53:17 +01:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-28 16:25:26 +01:00
|
|
|
"database/sql"
|
2020-07-28 10:53:17 +01:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
2020-09-04 11:46:01 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-07-28 10:53:17 +01:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UserDirectoryResponse struct {
|
|
|
|
Results []authtypes.FullyQualifiedProfile `json:"results"`
|
|
|
|
Limited bool `json:"limited"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func SearchUserDirectory(
|
|
|
|
ctx context.Context,
|
|
|
|
device *userapi.Device,
|
2022-05-05 13:17:38 +01:00
|
|
|
userAPI userapi.ClientUserAPI,
|
|
|
|
rsAPI api.ClientRoomserverAPI,
|
2022-03-28 16:25:26 +01:00
|
|
|
provider userapi.UserDirectoryProvider,
|
2020-07-28 10:53:17 +01:00
|
|
|
serverName gomatrixserverlib.ServerName,
|
|
|
|
searchString string,
|
|
|
|
limit int,
|
2022-05-05 13:17:38 +01:00
|
|
|
) util.JSONResponse {
|
2020-07-28 10:53:17 +01:00
|
|
|
if limit < 10 {
|
|
|
|
limit = 10
|
|
|
|
}
|
|
|
|
|
|
|
|
results := map[string]authtypes.FullyQualifiedProfile{}
|
|
|
|
response := &UserDirectoryResponse{
|
|
|
|
Results: []authtypes.FullyQualifiedProfile{},
|
|
|
|
Limited: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
// First start searching local users.
|
|
|
|
userReq := &userapi.QuerySearchProfilesRequest{
|
|
|
|
SearchString: searchString,
|
|
|
|
Limit: limit,
|
|
|
|
}
|
|
|
|
userRes := &userapi.QuerySearchProfilesResponse{}
|
2022-03-28 16:25:26 +01:00
|
|
|
if err := provider.QuerySearchProfiles(ctx, userReq, userRes); err != nil {
|
2022-05-05 13:17:38 +01:00
|
|
|
return util.ErrorResponse(fmt.Errorf("userAPI.QuerySearchProfiles: %w", err))
|
2020-07-28 10:53:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range userRes.Profiles {
|
|
|
|
if len(results) == limit {
|
|
|
|
response.Limited = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-03-28 16:25:26 +01:00
|
|
|
var userID string
|
|
|
|
if user.ServerName != "" {
|
|
|
|
userID = fmt.Sprintf("@%s:%s", user.Localpart, user.ServerName)
|
|
|
|
} else {
|
|
|
|
userID = fmt.Sprintf("@%s:%s", user.Localpart, serverName)
|
|
|
|
}
|
2020-07-28 10:53:17 +01:00
|
|
|
if _, ok := results[userID]; !ok {
|
|
|
|
results[userID] = authtypes.FullyQualifiedProfile{
|
|
|
|
UserID: userID,
|
|
|
|
DisplayName: user.DisplayName,
|
|
|
|
AvatarURL: user.AvatarURL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then, if we have enough room left in the response,
|
|
|
|
// start searching for known users from joined rooms.
|
|
|
|
|
|
|
|
if len(results) <= limit {
|
2020-09-04 11:46:01 +01:00
|
|
|
stateReq := &api.QueryKnownUsersRequest{
|
2020-07-28 10:53:17 +01:00
|
|
|
UserID: device.UserID,
|
|
|
|
SearchString: searchString,
|
|
|
|
Limit: limit - len(results),
|
|
|
|
}
|
2020-09-04 11:46:01 +01:00
|
|
|
stateRes := &api.QueryKnownUsersResponse{}
|
2022-03-28 16:25:26 +01:00
|
|
|
if err := rsAPI.QueryKnownUsers(ctx, stateReq, stateRes); err != nil && err != sql.ErrNoRows {
|
2022-05-05 13:17:38 +01:00
|
|
|
return util.ErrorResponse(fmt.Errorf("rsAPI.QueryKnownUsers: %w", err))
|
2020-07-28 10:53:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range stateRes.Users {
|
|
|
|
if len(results) == limit {
|
|
|
|
response.Limited = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := results[user.UserID]; !ok {
|
|
|
|
results[user.UserID] = user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, result := range results {
|
|
|
|
response.Results = append(response.Results, result)
|
|
|
|
}
|
|
|
|
|
2022-05-05 13:17:38 +01:00
|
|
|
return util.JSONResponse{
|
2020-07-28 10:53:17 +01:00
|
|
|
Code: 200,
|
|
|
|
JSON: response,
|
|
|
|
}
|
|
|
|
}
|