Skip to content

Commit

Permalink
Support users in searching for all users and groups by name in turms-…
Browse files Browse the repository at this point in the history
…service #1431
  • Loading branch information
JamesChenX committed Apr 6, 2024
1 parent c53c47a commit d14e168
Show file tree
Hide file tree
Showing 78 changed files with 4,337 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public static boolean isPreFlightRequest(HttpServerRequest request) {
&& headers.contains(HttpHeaderNames.ACCESS_CONTROL_REQUEST_METHOD);
}

public static boolean isSuccess(HttpResponseStatus status) {
int code = status.code();
return code >= 200 && code < 300;
}

public static boolean isServerError(HttpResponseStatus status) {
return status.code() >= 500;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,11 @@ public enum ResponseStatusCode {
NOT_FRIEND_TO_QUERY_MESSAGE_ATTACHMENT_INFO_IN_PRIVATE_CONVERSATION(6130,
"Only friends can query message attachments in private conversations", 403),
NOT_GROUP_MEMBER_TO_QUERY_MESSAGE_ATTACHMENT_INFO_IN_GROUP_CONVERSATION(6131,
"Only group members can query message attachments in group conversations", 403);
"Only group members can query message attachments in group conversations", 403),

// Search
SEARCHING_USER_IS_DISABLED(7100, "Searching users is disabled", 510),
SEARCHING_GROUP_IS_DISABLED(7200, "Searching groups is disabled", 510);

public static final int STATUS_CODE_LENGTH = 4;
public static final ResponseStatusCode[] VALUES = values();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.Date;

import lombok.AllArgsConstructor;
import lombok.Data;

import im.turms.server.common.access.client.dto.constant.ProfileAccessStrategy;
Expand All @@ -32,6 +33,7 @@
/**
* @author James Chen
*/
@AllArgsConstructor
@Data
@Document(User.COLLECTION_NAME)
@Sharded
Expand All @@ -46,7 +48,7 @@ public final class User extends BaseEntity {
private final byte[] password;

@Field(Fields.NAME)
private final String name;
private String name;

@Field(Fields.INTRO)
private final String intro;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ public enum ServiceType {
USER_LOCATION,
USER_PERMISSION_GROUP,

STORAGE_MESSAGE_ATTACHMENT
STORAGE_MESSAGE_ATTACHMENT,

ELASTICSEARCH_SYNC_LOG
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import im.turms.server.common.infra.lang.StringUtil;
import im.turms.server.common.infra.logging.core.logger.Logger;
import im.turms.server.common.infra.logging.core.logger.LoggerFactory;
import im.turms.server.common.infra.netty.ReferenceCountUtil;
import im.turms.server.common.infra.time.DateUtil;

/**
Expand Down Expand Up @@ -106,12 +105,16 @@ public static Map<String, String> readStringStringMapValue(InputStream src) {

public static ByteBuf write(Object value) {
int estimatedSize = estimateSize(value);
ByteBuf buffer = PooledByteBufAllocator.DEFAULT.directBuffer(estimatedSize);
return write(estimatedSize, value);
}

public static ByteBuf write(int size, Object value) {
ByteBuf buffer = PooledByteBufAllocator.DEFAULT.directBuffer(size);
try (OutputStream bufferOutputStream = new ByteBufOutputStream(buffer)) {
MAPPER.writeValue(bufferOutputStream, value);
return buffer;
} catch (Exception e) {
ReferenceCountUtil.ensureReleased(buffer);
buffer.release();
throw new IllegalArgumentException(
"Failed to write the input object as a byte buffer",
e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@

package im.turms.server.common.infra.lang;

import jakarta.annotation.Nullable;

/**
* @author James Chen
*/
public record Pair<T1, T2>(
T1 first,
T2 second
@Nullable T1 first,
@Nullable T2 second
) {

public static <T1, T2> Pair<T1, T2> of(T1 first, T2 second) {
public static <T1, T2> Pair<T1, T2> of(@Nullable T1 first, @Nullable T2 second) {
return new Pair<>(first, second);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import im.turms.server.common.infra.property.env.service.env.adminapi.AdminApiProperties;
import im.turms.server.common.infra.property.env.service.env.clientapi.ClientApiProperties;
import im.turms.server.common.infra.property.env.service.env.database.MongoProperties;
import im.turms.server.common.infra.property.env.service.env.elasticsearch.TurmsElasticsearchProperties;
import im.turms.server.common.infra.property.env.service.env.push.PushNotificationProperties;
import im.turms.server.common.infra.property.env.service.env.redis.TurmsRedisProperties;

Expand All @@ -54,6 +55,9 @@ public class ServiceProperties {
@NestedConfigurationProperty
private ClientApiProperties clientApi = new ClientApiProperties();

@NestedConfigurationProperty
private TurmsElasticsearchProperties elasticsearch = new TurmsElasticsearchProperties();

@NestedConfigurationProperty
private FakeProperties fake = new FakeProperties();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2019 The Turms Project
* https://github.com/turms-im/turms
*
* 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 im.turms.server.common.infra.property.env.service.env.elasticsearch;

import java.util.Collections;
import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import im.turms.server.common.infra.property.metadata.Description;

/**
* @author James Chen
*/
@AllArgsConstructor
@Builder(toBuilder = true)
@Data
@NoArgsConstructor
public class ElasticsearchClientProperties {

@Description("Elasticsearch URI")
private String uri = "http://localhost:9200";

@Description("Elasticsearch HTTP request headers")
private List<HttpHeaderProperties> requestHeaders = Collections.emptyList();

@Description("Elasticsearch username")
private String username = "elastic";

@Description("Elasticsearch password")
private String password = "";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2019 The Turms Project
* https://github.com/turms-im/turms
*
* 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 im.turms.server.common.infra.property.env.service.env.elasticsearch;

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

/**
* @author James Chen
*/
@AllArgsConstructor
@SuperBuilder(toBuilder = true)
@Data
@NoArgsConstructor
public class ElasticsearchGroupUseCaseProperties extends ElasticsearchUseCaseProperties {

private List<ElasticsearchIndexProperties> indexes =
List.of(new ElasticsearchIndexProperties());

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2019 The Turms Project
* https://github.com/turms-im/turms
*
* 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 im.turms.server.common.infra.property.env.service.env.elasticsearch;

import jakarta.validation.constraints.Min;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import im.turms.server.common.infra.property.metadata.Description;

/**
* @author James Chen
*/
@AllArgsConstructor
@Builder(toBuilder = true)
@Data
@NoArgsConstructor
public class ElasticsearchIndexProperties {

@Description("The index will be created for the specified language. "
+ "If the language is NONE, this index will be used as the default index for all languages that don't have a specified index for them")
private LanguageCode code = LanguageCode.NONE;

@Description("The number of shards. -1 means use the default value")
@Min(-1)
private int numberOfShards = -1;

@Description("The number of replicas. -1 means use the default value")
@Min(-1)
private int numberOfReplicas = -1;

@NestedConfigurationProperty
private ElasticsearchIndexPropertiesProperties properties =
new ElasticsearchIndexPropertiesProperties();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2019 The Turms Project
* https://github.com/turms-im/turms
*
* 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 im.turms.server.common.infra.property.env.service.env.elasticsearch;

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @author James Chen
*/
@AllArgsConstructor
@Builder(toBuilder = true)
@Data
@NoArgsConstructor
public class ElasticsearchIndexPropertiesFieldProperties {

private List<ElasticsearchIndexTextFieldProperties> textFields =
List.of(new ElasticsearchIndexTextFieldProperties());

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2019 The Turms Project
* https://github.com/turms-im/turms
*
* 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 im.turms.server.common.infra.property.env.service.env.elasticsearch;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

/**
* @author James Chen
*/
@AllArgsConstructor
@SuperBuilder(toBuilder = true)
@Data
@NoArgsConstructor
public class ElasticsearchIndexPropertiesProperties {

@NestedConfigurationProperty
private ElasticsearchIndexPropertiesFieldProperties name =
new ElasticsearchIndexPropertiesFieldProperties();

}
Loading

0 comments on commit d14e168

Please sign in to comment.