2020-07-28 11:53:17 +02: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 17:25:26 +02:00
|
|
|
"database/sql"
|
2020-07-28 11:53:17 +02:00
|
|
|
"fmt"
|
2022-08-12 13:33:31 +02:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2020-07-28 11:53:17 +02:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
2020-09-04 12:46:01 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-07-28 11:53:17 +02:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2022-08-12 13:33:31 +02:00
|
|
|
"github.com/matrix-org/gomatrix"
|
2020-07-28 11:53:17 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-06 10:55:01 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2023-04-19 16:50:33 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2020-07-28 11:53:17 +02:00
|
|
|
"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 14:17:38 +02:00
|
|
|
rsAPI api.ClientRoomserverAPI,
|
2022-05-05 20:30:38 +02:00
|
|
|
provider userapi.QuerySearchProfilesAPI,
|
2020-07-28 11:53:17 +02:00
|
|
|
searchString string,
|
|
|
|
limit int,
|
2023-04-06 10:55:01 +02:00
|
|
|
federation *fclient.FederationClient,
|
2023-04-19 16:50:33 +02:00
|
|
|
localServerName spec.ServerName,
|
2022-05-05 14:17:38 +02:00
|
|
|
) util.JSONResponse {
|
2020-07-28 11:53:17 +02:00
|
|
|
if limit < 10 {
|
|
|
|
limit = 10
|
|
|
|
}
|
|
|
|
|
|
|
|
results := map[string]authtypes.FullyQualifiedProfile{}
|
|
|
|
response := &UserDirectoryResponse{
|
|
|
|
Results: []authtypes.FullyQualifiedProfile{},
|
|
|
|
Limited: false,
|
|
|
|
}
|
|
|
|
|
2022-08-12 13:33:31 +02:00
|
|
|
// Get users we share a room with
|
|
|
|
knownUsersReq := &api.QueryKnownUsersRequest{
|
|
|
|
UserID: device.UserID,
|
|
|
|
Limit: limit,
|
2020-07-28 11:53:17 +02:00
|
|
|
}
|
2022-08-12 13:33:31 +02:00
|
|
|
knownUsersRes := &api.QueryKnownUsersResponse{}
|
|
|
|
if err := rsAPI.QueryKnownUsers(ctx, knownUsersReq, knownUsersRes); err != nil && err != sql.ErrNoRows {
|
|
|
|
return util.ErrorResponse(fmt.Errorf("rsAPI.QueryKnownUsers: %w", err))
|
2020-07-28 11:53:17 +02:00
|
|
|
}
|
|
|
|
|
2022-08-12 13:33:31 +02:00
|
|
|
knownUsersLoop:
|
|
|
|
for _, profile := range knownUsersRes.Users {
|
2020-07-28 11:53:17 +02:00
|
|
|
if len(results) == limit {
|
|
|
|
response.Limited = true
|
|
|
|
break
|
|
|
|
}
|
2022-08-12 13:33:31 +02:00
|
|
|
userID := profile.UserID
|
|
|
|
// get the full profile of the local user
|
|
|
|
localpart, serverName, _ := gomatrixserverlib.SplitID('@', userID)
|
|
|
|
if serverName == localServerName {
|
|
|
|
userReq := &userapi.QuerySearchProfilesRequest{
|
|
|
|
SearchString: localpart,
|
|
|
|
Limit: limit,
|
|
|
|
}
|
|
|
|
userRes := &userapi.QuerySearchProfilesResponse{}
|
|
|
|
if err := provider.QuerySearchProfiles(ctx, userReq, userRes); err != nil {
|
|
|
|
return util.ErrorResponse(fmt.Errorf("userAPI.QuerySearchProfiles: %w", err))
|
|
|
|
}
|
|
|
|
for _, p := range userRes.Profiles {
|
|
|
|
if strings.Contains(p.DisplayName, searchString) ||
|
|
|
|
strings.Contains(p.Localpart, searchString) {
|
|
|
|
profile.DisplayName = p.DisplayName
|
|
|
|
profile.AvatarURL = p.AvatarURL
|
|
|
|
results[userID] = profile
|
|
|
|
if len(results) == limit {
|
|
|
|
response.Limited = true
|
|
|
|
break knownUsersLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 17:25:26 +02:00
|
|
|
} else {
|
2022-08-12 13:33:31 +02:00
|
|
|
// If the username already contains the search string, don't bother hitting federation.
|
|
|
|
// This will result in missing avatars and displaynames, but saves the federation roundtrip.
|
|
|
|
if strings.Contains(localpart, searchString) {
|
|
|
|
results[userID] = profile
|
|
|
|
if len(results) == limit {
|
|
|
|
response.Limited = true
|
|
|
|
break knownUsersLoop
|
|
|
|
}
|
|
|
|
continue
|
2020-07-28 11:53:17 +02:00
|
|
|
}
|
2022-08-12 13:33:31 +02:00
|
|
|
// TODO: We should probably cache/store this
|
2022-11-15 16:05:23 +01:00
|
|
|
fedProfile, fedErr := federation.LookupProfile(ctx, localServerName, serverName, userID, "")
|
2022-08-12 13:33:31 +02:00
|
|
|
if fedErr != nil {
|
|
|
|
if x, ok := fedErr.(gomatrix.HTTPError); ok {
|
|
|
|
if x.Code == http.StatusNotFound {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 11:53:17 +02:00
|
|
|
}
|
2022-08-12 13:33:31 +02:00
|
|
|
if strings.Contains(fedProfile.DisplayName, searchString) {
|
|
|
|
profile.DisplayName = fedProfile.DisplayName
|
|
|
|
profile.AvatarURL = fedProfile.AvatarURL
|
|
|
|
results[userID] = profile
|
|
|
|
if len(results) == limit {
|
|
|
|
response.Limited = true
|
|
|
|
break knownUsersLoop
|
|
|
|
}
|
2020-07-28 11:53:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, result := range results {
|
|
|
|
response.Results = append(response.Results, result)
|
|
|
|
}
|
|
|
|
|
2022-05-05 14:17:38 +02:00
|
|
|
return util.JSONResponse{
|
2020-07-28 11:53:17 +02:00
|
|
|
Code: 200,
|
|
|
|
JSON: response,
|
|
|
|
}
|
|
|
|
}
|