Skip to content

Commit

Permalink
Support users in searching for all users and groups by name in clients
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesChenX committed Apr 12, 2024
1 parent 0b8e985 commit 11094e3
Show file tree
Hide file tree
Showing 12 changed files with 368 additions and 2 deletions.
17 changes: 17 additions & 0 deletions turms-client-cpp/include/turms/client/service/group_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,23 @@ class GroupService : private boost::noncopyable {
const boost::optional<time_point>& lastUpdatedDate = boost::none)
-> boost::future<Response<std::vector<Group>>>;

/**
* Search for groups.
*
* @param name search for groups whose name matches name.
* @param highlight whether to highlight the name.
* If true, the highlighted parts of the name will be paired with '\u0002' and '\u0003'.
* @param skip the number of groups to skip.
* @param limit the max number of groups to return.
* @return a list of groups sorted in descending relevance.
* @throws ResponseException if an error occurs.
*/
auto searchGroups(const std::string& name,
bool highlight = false,
const boost::optional<int>& skip = boost::none,
const boost::optional<int>& limit = boost::none)
-> boost::future<Response<std::vector<Group>>>;

/**
* Find group IDs that the logged-in user has joined.
*
Expand Down
17 changes: 17 additions & 0 deletions turms-client-cpp/include/turms/client/service/user_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,23 @@ class UserService : private boost::noncopyable, private std::enable_shared_from_
const boost::optional<time_point>& lastUpdatedDate = boost::none)
-> boost::future<Response<std::vector<UserInfo>>>;

/**
* Search for user profiles.
*
* @param name search for user profiles whose name matches name.
* @param highlight whether to highlight the name.
* If true, the highlighted parts of the name will be paired with '\u0002' and '\u0003'.
* @param skip the number of user profiles to skip.
* @param limit the max number of user profiles to return.
* @return a list of user profiles sorted in descending relevance.
* @throws ResponseException if an error occurs.
*/
auto searchUserProfiles(const std::string& name,
bool highlight = false,
const boost::optional<int>& skip = boost::none,
const boost::optional<int>& limit = boost::none)
-> boost::future<Response<std::vector<UserInfo>>>;

/**
* Find nearby users.
*
Expand Down
31 changes: 31 additions & 0 deletions turms-client-cpp/src/turms/client/service/group_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,37 @@ auto GroupService::queryGroups(const std::unordered_set<int64_t>& groupIds,
});
}

auto UserService::searchGroups(const std::string& name,
bool highlight = false,
const boost::optional<int>& skip = boost::none,
const boost::optional<int>& limit = boost::none)
-> boost::future<Response<std::vector<Group>>> {
if (name.empty()) {
return boost::make_ready_future<>(Response<Group>::emptyList());
}
TurmsRequest turmsRequest;
auto* request = turmsRequest.mutable_query_groups_request();
request->set_name(name);
if (highlight) {
request->add_fields_to_highlight(1);
}
if (skip) {
request->set_skip(*skip);
}
if (limit) {
request->set_limit(*limit);
}
return turmsClient_.driver()
.send(turmsRequest)
.then([](boost::future<TurmsNotification> response) {
return Response<std::vector<Group>>{
response.get(), [](const TurmsNotification::Data& data) {
const auto& groups = data.groups_with_version().groups();
return std::vector<Group>{groups.cbegin(), groups.cend()};
}};
});
}

auto GroupService::queryJoinedGroupIds(const boost::optional<time_point>& lastUpdatedDate)
-> boost::future<Response<boost::optional<LongsWithVersion>>> {
TurmsRequest turmsRequest;
Expand Down
31 changes: 31 additions & 0 deletions turms-client-cpp/src/turms/client/service/user_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,37 @@ auto UserService::queryUserProfiles(const std::unordered_set<int64_t>& userIds,
});
}

auto UserService::searchUserProfiles(const std::string& name,
bool highlight = false,
const boost::optional<int>& skip = boost::none,
const boost::optional<int>& limit = boost::none)
-> boost::future<Response<std::vector<UserInfo>>> {
if (name.empty()) {
return boost::make_ready_future<>(Response<UserInfo>::emptyList());
}
TurmsRequest turmsRequest;
auto* request = turmsRequest.mutable_query_user_profiles_request();
request->set_name(name);
if (highlight) {
request->add_fields_to_highlight(1);
}
if (skip) {
request->set_skip(*skip);
}
if (limit) {
request->set_limit(*limit);
}
return turmsClient_.driver()
.send(turmsRequest)
.then([](boost::future<TurmsNotification> response) {
return Response<std::vector<UserInfo>>{
response.get(), [](const TurmsNotification::Data& data) {
const auto& userInfos = data.user_infos_with_version().user_infos();
return std::vector<UserInfo>{userInfos.cbegin(), userInfos.cend()};
}};
});
}

auto UserService::queryNearbyUsers(float latitude,
float longitude,
const boost::optional<int>& maxCount,
Expand Down
25 changes: 25 additions & 0 deletions turms-client-dart/lib/src/service/group_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,31 @@ class GroupService {
return n.toResponse((data) => data.groupsWithVersion.groups);
}

/// Search for groups.
///
/// **Params**:
/// * `name`: Search for groups whose name matches [name].
/// * `highlight`: Whether to highlight the name.
/// If true, the highlighted parts of the name will be paired with '\u0002' and '\u0003'.
/// * `skip`: The number of groups to skip.
/// * `limit`: The max number of groups to return.
///
/// **Returns**: A list of groups sorted in descending relevance.
///
/// **Throws**: [ResponseException] if an error occurs.
Future<Response<List<Group>>> searchGroups(String name,
{bool highlight = false, int? skip, int? limit}) async {
if (name.isEmpty) {
return Future.value(Response.emptyList());
}
final n = await _turmsClient.driver.send(QueryGroupsRequest(
name: name,
fieldsToHighlight: highlight ? [1] : null,
skip: skip,
limit: limit));
return n.toResponse((data) => data.groupsWithVersion.groups);
}

/// Find group IDs that the logged-in user has joined.
///
/// **Params**:
Expand Down
25 changes: 25 additions & 0 deletions turms-client-dart/lib/src/service/user_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,31 @@ class UserService {
return n.toResponse((data) => data.userInfosWithVersion.userInfos);
}

/// Search for user profiles.
///
/// **Params**:
/// * `name`: Search for user profiles whose name matches [name].
/// * `highlight`: Whether to highlight the name.
/// If true, the highlighted parts of the name will be paired with '\u0002' and '\u0003'.
/// * `skip`: The number of user profiles to skip.
/// * `limit`: The max number of user profiles to return.
///
/// **Returns**: A list of user profiles sorted in descending relevance.
///
/// **Throws**: [ResponseException] if an error occurs.
Future<Response<List<UserInfo>>> searchUserProfiles(String name,
{bool highlight = false, int? skip, int? limit}) async {
if (name.isEmpty) {
return Future.value(Response.emptyList());
}
final n = await _turmsClient.driver.send(QueryUserProfilesRequest(
name: name,
fieldsToHighlight: highlight ? [1] : null,
skip: skip,
limit: limit));
return n.toResponse((data) => data.userInfosWithVersion.userInfos);
}

/// Find nearby users.
///
/// **Params**:
Expand Down
35 changes: 34 additions & 1 deletion turms-client-js/src/service/group-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,40 @@ export default class GroupService {
return this._turmsClient.driver.send({
queryGroupsRequest: {
groupIds: CollectionUtil.uniqueArray(groupIds),
lastUpdatedDate: DataParser.getDateTimeStr(lastUpdatedDate)
lastUpdatedDate: DataParser.getDateTimeStr(lastUpdatedDate),
fieldsToHighlight: []
}
}).then(n => Response.fromNotification(n, (data) =>
NotificationUtil.transform(data.groupsWithVersion?.groups)));
}

/**
* Search for groups.
*
* @param name - search for groups whose name matches {@link name}.
* @param highlight - whether to highlight the name.
* If true, the highlighted parts of the name will be paired with '\u0002' and '\u0003'.
* @param skip - the number of groups to skip.
* @param limit - the max number of groups to return.
* @returns a list of groups sorted in descending relevance.
* @throws {@link ResponseError} if an error occurs.
*/
searchGroups({
name, highlight, skip, limit
}: {
name: string,
highlight?: boolean, skip?: number, limit?: number
}): Promise<Response<ParsedModel.Group[]>> {
if (!name) {
return Promise.resolve(Response.emptyList());
}
return this._turmsClient.driver.send({
queryGroupsRequest: {
groupIds: [],
name: name,
fieldsToHighlight: highlight ? [1] : [],
skip: skip,
limit: limit
}
}).then(n => Response.fromNotification(n, (data) =>
NotificationUtil.transform(data.groupsWithVersion?.groups)));
Expand Down
35 changes: 34 additions & 1 deletion turms-client-js/src/service/user-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,45 @@ export default class UserService {
return this._turmsClient.driver.send({
queryUserProfilesRequest: {
userIds: CollectionUtil.uniqueArray(userIds),
lastUpdatedDate: DataParser.getDateTimeStr(lastUpdatedDate)
lastUpdatedDate: DataParser.getDateTimeStr(lastUpdatedDate),
fieldsToHighlight: []
}
}).then(n => Response.fromNotification(n, data =>
NotificationUtil.transformOrEmpty(data.userInfosWithVersion?.userInfos)));
}

/**
* Search for user profiles.
*
* @param name - search for user profiles whose name matches {@link name}.
* @param highlight - whether to highlight the name.
* If true, the highlighted parts of the name will be paired with '\u0002' and '\u0003'.
* @param skip - the number of user profiles to skip.
* @param limit - the max number of user profiles to return.
* @returns a list of user profiles sorted in descending relevance.
* @throws {@link ResponseError} if an error occurs.
*/
searchUserProfiles({
name, highlight, skip, limit
}: {
name: string,
highlight?: boolean, skip?: number, limit?: number
}): Promise<Response<ParsedModel.UserInfo[]>> {
if (!name) {
return Promise.resolve(Response.emptyList());
}
return this._turmsClient.driver.send({
queryUserProfilesRequest: {
userIds: [],
name: name,
fieldsToHighlight: highlight ? [1] : [],
skip: skip,
limit: limit
}
}).then(n => Response.fromNotification(n, (data) =>
NotificationUtil.transform(data.userInfosWithVersion?.userInfos)));
}

/**
* Find nearby users.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,41 @@ class GroupService(private val turmsClient: TurmsClient) {
}
}

/**
* Search for groups.
*
* @param name search for groups whose name matches [name].
* @param highlight whether to highlight the name.
* If true, the highlighted parts of the name will be paired with '\u0002' and '\u0003'.
* @param skip the number of groups to skip.
* @param limit the max number of groups to return.
* @return a list of groups sorted in descending relevance.
* @throws ResponseException if an error occurs.
*/
suspend fun searchGroups(
name: String,
highlight: Boolean = false,
skip: Int? = null,
limit: Int? = null,
): Response<List<Group>> =
if (name.isBlank()) {
Response.emptyList()
} else {
turmsClient.driver
.send(
QueryGroupsRequest.newBuilder().apply {
this.name = name
if (highlight) {
addFieldsToHighlight(1)
}
skip?.let { this.skip = it }
limit?.let { this.limit = it }
},
).toResponse {
it.groupsWithVersion.groupsList
}
}

/**
* Find group IDs that the logged-in user has joined.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,41 @@ class UserService(private val turmsClient: TurmsClient) {
}
}

/**
* Search for user profiles.
*
* @param name search for user profiles whose name matches [name].
* @param highlight whether to highlight the name.
* If true, the highlighted parts of the name will be paired with '\u0002' and '\u0003'.
* @param skip the number of user profiles to skip.
* @param limit the max number of user profiles to return.
* @return a list of user profiles sorted in descending relevance.
* @throws ResponseException if an error occurs.
*/
suspend fun searchUserProfiles(
name: String,
highlight: Boolean = false,
skip: Int? = null,
limit: Int? = null,
): Response<List<UserInfo>> =
if (name.isBlank()) {
Response.emptyList()
} else {
turmsClient.driver
.send(
QueryUserProfilesRequest.newBuilder().apply {
this.name = name
if (highlight) {
addFieldsToHighlight(1)
}
skip?.let { this.skip = it }
limit?.let { this.limit = it }
},
).toResponse {
it.userInfosWithVersion.userInfosList
}
}

/**
* Find nearby users.
*
Expand Down
Loading

0 comments on commit 11094e3

Please sign in to comment.