diff --git a/im-sdk-core/src/integration-test/java/com/easemob/im/server/api/user/UserIT.java b/im-sdk-core/src/integration-test/java/com/easemob/im/server/api/user/UserIT.java index b3867efbd..b1b676608 100644 --- a/im-sdk-core/src/integration-test/java/com/easemob/im/server/api/user/UserIT.java +++ b/im-sdk-core/src/integration-test/java/com/easemob/im/server/api/user/UserIT.java @@ -6,6 +6,7 @@ import com.easemob.im.server.exception.EMNotFoundException; import com.easemob.im.server.model.EMBlock; import com.easemob.im.server.model.EMCreateUser; +import com.easemob.im.server.model.EMUser; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -27,8 +28,28 @@ void testUserLifeCycles() { String randomPassword = Utilities.randomPassword(); assertDoesNotThrow(() -> this.service.user().create(randomUsername, randomPassword) .block(Utilities.IT_TIMEOUT)); + EMUser getUser = assertDoesNotThrow( + () -> this.service.user().get(randomUsername).block(Utilities.IT_TIMEOUT)); + assertNull(getUser.getPushNickname()); assertDoesNotThrow( + () -> this.service.user().delete(randomUsername).block(Utilities.IT_TIMEOUT)); + assertThrows(EMNotFoundException.class, () -> this.service.user().get(randomUsername).block(Utilities.IT_TIMEOUT)); + } + + @Test + void testUserWithPushNicknameLifeCycles() { + String randomUsername = Utilities.randomUserName(); + String randomPassword = Utilities.randomPassword(); + String pushNickname = "推送昵称"; + EMUser createUser = assertDoesNotThrow(() -> this.service.user().create(randomUsername, randomPassword, pushNickname) + .block(Utilities.IT_TIMEOUT)); + assertEquals(pushNickname, createUser.getPushNickname()); + + EMUser getUser = assertDoesNotThrow( + () -> this.service.user().get(randomUsername).block(Utilities.IT_TIMEOUT)); + assertEquals(pushNickname, getUser.getPushNickname()); + assertDoesNotThrow( () -> this.service.user().delete(randomUsername).block(Utilities.IT_TIMEOUT)); assertThrows(EMNotFoundException.class, @@ -63,6 +84,41 @@ void testBatchCreateUser() { () -> this.service.user().get(randomUsername1).block(Utilities.IT_TIMEOUT)); } + @Test + void testBatchCreateUserWithPushNickname() { + String randomUsername = Utilities.randomUserName(); + String randomUsername1 = Utilities.randomUserName(); + String randomPassword = Utilities.randomPassword(); + String pushNickname = "推送昵称"; + + List createUserList = new ArrayList<>(); + EMCreateUser createUser = new EMCreateUser(randomUsername, randomPassword, pushNickname); + EMCreateUser createUser1 = new EMCreateUser(randomUsername1, randomPassword, pushNickname); + createUserList.add(createUser); + createUserList.add(createUser1); + + List createUsers = assertDoesNotThrow(() -> this.service.user().create(createUserList) + .block(Utilities.IT_TIMEOUT)); + createUsers.get(0).getPushNickname(); + + EMUser getUser = assertDoesNotThrow( + () -> this.service.user().get(randomUsername).block(Utilities.IT_TIMEOUT)); + assertEquals(pushNickname, getUser.getPushNickname()); + + EMUser getUser1 = assertDoesNotThrow( + () -> this.service.user().get(randomUsername1).block(Utilities.IT_TIMEOUT)); + assertEquals(pushNickname, getUser1.getPushNickname()); + + assertDoesNotThrow( + () -> this.service.user().delete(randomUsername).block(Utilities.IT_TIMEOUT)); + assertDoesNotThrow( + () -> this.service.user().delete(randomUsername1).block(Utilities.IT_TIMEOUT)); + assertThrows(EMNotFoundException.class, + () -> this.service.user().get(randomUsername).block(Utilities.IT_TIMEOUT)); + assertThrows(EMNotFoundException.class, + () -> this.service.user().get(randomUsername1).block(Utilities.IT_TIMEOUT)); + } + @Test void testUserForceLogout() { String randomUsername = Utilities.randomUserName(); diff --git a/im-sdk-core/src/main/java/com/easemob/im/server/api/user/UserApi.java b/im-sdk-core/src/main/java/com/easemob/im/server/api/user/UserApi.java index 29a8572c1..df201d334 100644 --- a/im-sdk-core/src/main/java/com/easemob/im/server/api/user/UserApi.java +++ b/im-sdk-core/src/main/java/com/easemob/im/server/api/user/UserApi.java @@ -89,6 +89,43 @@ public Mono create(String username, String password) { return this.createUser.single(username, password); } + /** + * 创建用户。 + *

+ * Server SDK 对创建的用户名有自己的限制,如果不想使用该限制,请查看此文档: + * 用户名限制 + *

+ * API使用示例: + *

 {@code
+     * EMService service;
+     * try {
+     *     EMUser user = service.user().create("username", "password", "pushNickname").block();
+     * } catch (EMException e) {
+     *     e.getErrorCode();
+     *     e.getMessage();
+     * }
+     * }
+ * + * @param username 用户名,可以包含小写字母、数字、减号,有效长度1至32个字节 + * @param password 密码,可以包含字母、数字、特殊符号(~!@#$%^&*-_=+<>;:,./?),有效长度1至32字节 + * @param pushNickname 推送昵称,离线推送时在接收方的客户端推送通知栏中显示的发送方的昵称。你可以自定义该昵称,长度不能超过 100 个字符。 + * 提示:1. 若不设置昵称,推送时会显示发送方的用户 ID,而非昵称。 + * 2. 该昵称可与用户属性中的昵称设置不同,不过我们建议这两种昵称的设置保持一致。因此,修改其中一个昵称时,也需调用相应方法对另一个进行更新,确保设置一致。更新用户属性中的昵称的方法,详见 设置用户属性。 + * @return EMUser + * @see 注册用户 + */ + public Mono create(String username, String password, String pushNickname) { + try { + if (context.getProperties().getValidateUserName()) { + EMUser.validateUsername(username); + } + EMUser.validatePassword(password); + } catch (EMInvalidArgumentException e) { + return Mono.error(e); + } + return this.createUser.single(username, password, pushNickname); + } + /** * 批量创建用户。 *

diff --git a/im-sdk-core/src/main/java/com/easemob/im/server/api/user/UserResource.java b/im-sdk-core/src/main/java/com/easemob/im/server/api/user/UserResource.java index 18a59c0b4..d3c03ed57 100644 --- a/im-sdk-core/src/main/java/com/easemob/im/server/api/user/UserResource.java +++ b/im-sdk-core/src/main/java/com/easemob/im/server/api/user/UserResource.java @@ -11,6 +11,9 @@ public class UserResource { @JsonProperty("username") private String username; + @JsonProperty("nickname") + private String pushNickname; + @JsonProperty("uuid") private String uuid; @@ -23,10 +26,12 @@ public class UserResource { @JsonCreator public UserResource( @JsonProperty("username") String username, + @JsonProperty("nickname") String pushNickname, @JsonProperty("uuid") String uuid, @JsonProperty("activated") boolean activated, @JsonProperty("pushInfo") List pushResources) { this.username = username; + this.pushNickname = pushNickname; this.uuid = uuid; this.activated = activated; this.pushResources = pushResources; @@ -40,6 +45,10 @@ public String getUsername() { return username; } + public String getPushNickname() { + return pushNickname; + } + public List getPushResources() { return pushResources; } @@ -49,12 +58,13 @@ public boolean isActivated() { } public EMUser toEMUser() { - return new EMUser(this.username, this.uuid, this.activated, this.pushResources); + return new EMUser(this.username, this.pushNickname, this.uuid, this.activated, this.pushResources); } @Override public String toString() { return "UserResource{" + "username='" + username + '\'' + + ", pushNickname='" + pushNickname + '\'' + ", uuid='" + uuid + '\'' + ", activated=" + activated + ", pushResources=" + pushResources + diff --git a/im-sdk-core/src/main/java/com/easemob/im/server/api/user/create/CreateUser.java b/im-sdk-core/src/main/java/com/easemob/im/server/api/user/create/CreateUser.java index 1063a49d8..02df0cbce 100644 --- a/im-sdk-core/src/main/java/com/easemob/im/server/api/user/create/CreateUser.java +++ b/im-sdk-core/src/main/java/com/easemob/im/server/api/user/create/CreateUser.java @@ -24,7 +24,32 @@ public Mono single(String username, String password) { .flatMap(httpClient -> httpClient.post() .uri("/users") .send(Mono.create(sink -> sink.success(this.context.getCodec() - .encode(new CreateUserRequest(username, password))))) + .encode(new CreateUserRequest(username, password, null))))) + .responseSingle( + (rsp, buf) -> { + return buf.switchIfEmpty( + Mono.error(new EMUnknownException("response is null"))) + .flatMap(byteBuf -> { + ErrorMapper mapper = new DefaultErrorMapper(); + mapper.statusCode(rsp); + mapper.checkError(byteBuf); + return Mono.just(byteBuf); + }); + })) + .map(byteBuf -> { + UserGetResponse userGetResponse = + this.context.getCodec().decode(byteBuf, UserGetResponse.class); + byteBuf.release(); + return userGetResponse.getEMUser(username.toLowerCase()); + }); + } + + public Mono single(String username, String password, String pushNickname) { + return this.context.getHttpClient() + .flatMap(httpClient -> httpClient.post() + .uri("/users") + .send(Mono.create(sink -> sink.success(this.context.getCodec() + .encode(new CreateUserRequest(username, password, pushNickname))))) .responseSingle( (rsp, buf) -> { return buf.switchIfEmpty( diff --git a/im-sdk-core/src/main/java/com/easemob/im/server/api/user/create/CreateUserRequest.java b/im-sdk-core/src/main/java/com/easemob/im/server/api/user/create/CreateUserRequest.java index 033bf5a00..9ec92f21d 100644 --- a/im-sdk-core/src/main/java/com/easemob/im/server/api/user/create/CreateUserRequest.java +++ b/im-sdk-core/src/main/java/com/easemob/im/server/api/user/create/CreateUserRequest.java @@ -10,11 +10,16 @@ public class CreateUserRequest { @JsonProperty("password") private String password; + @JsonProperty("nickname") + private String pushNickname; + @JsonCreator public CreateUserRequest(@JsonProperty("username") String username, - @JsonProperty("password") String password) { + @JsonProperty("password") String password, + @JsonProperty("nickname") String pushNickname) { this.username = username; this.password = password; + this.pushNickname = pushNickname; } public String getUsername() { diff --git a/im-sdk-core/src/main/java/com/easemob/im/server/model/EMCreateUser.java b/im-sdk-core/src/main/java/com/easemob/im/server/model/EMCreateUser.java index ff7d9b5f8..484277847 100644 --- a/im-sdk-core/src/main/java/com/easemob/im/server/model/EMCreateUser.java +++ b/im-sdk-core/src/main/java/com/easemob/im/server/model/EMCreateUser.java @@ -11,10 +11,22 @@ public class EMCreateUser { @JsonProperty("password") private String password; + @JsonProperty("nickname") + private String pushNickname; + @JsonCreator public EMCreateUser(@JsonProperty("username") String username, @JsonProperty("password") String password) { this.username = username; this.password = password; } + + @JsonCreator + public EMCreateUser(@JsonProperty("username") String username, + @JsonProperty("password") String password, + @JsonProperty("nickname") String pushNickname) { + this.username = username; + this.password = password; + this.pushNickname = pushNickname; + } } diff --git a/im-sdk-core/src/main/java/com/easemob/im/server/model/EMUser.java b/im-sdk-core/src/main/java/com/easemob/im/server/model/EMUser.java index 68e489f31..3b9abaad2 100644 --- a/im-sdk-core/src/main/java/com/easemob/im/server/model/EMUser.java +++ b/im-sdk-core/src/main/java/com/easemob/im/server/model/EMUser.java @@ -16,6 +16,7 @@ public class EMUser extends EMEntity { Pattern.compile("^[a-zA-Z0-9~!@#$%^&*\\-_=+<>;:,./?]{1,32}$"); private final String username; + private final String pushNickname; private final String uuid; private final Boolean canLogin; private final List pushResources; @@ -27,7 +28,7 @@ public class EMUser extends EMEntity { */ @Deprecated public EMUser(String username, Boolean canLogin) { - this(username, null, canLogin, null); + this(username, null, null, canLogin, null); } /** @@ -36,7 +37,7 @@ public EMUser(String username, Boolean canLogin) { * @param canLogin 是否可登录 */ public EMUser(String username, String uuid, Boolean canLogin) { - this(username, uuid, canLogin, null); + this(username, null, uuid, canLogin, null); } /** @@ -45,13 +46,14 @@ public EMUser(String username, String uuid, Boolean canLogin) { * @param canLogin 是否可登录 * @param pushResources 推送信息,例如 deviceId、deviceToken */ - public EMUser(String username, String uuid, Boolean canLogin, List pushResources) { + public EMUser(String username, String pushNickname, String uuid, Boolean canLogin, List pushResources) { super(EntityType.USER); if (StringUtil.isNullOrEmpty(username)) { throw new EMInvalidArgumentException("username cannot be blank"); } super.id(username); this.username = username; + this.pushNickname = pushNickname; this.uuid = uuid; this.canLogin = canLogin; this.pushResources = pushResources; @@ -83,6 +85,10 @@ public String getUsername() { return this.username; } + public String getPushNickname() { + return this.pushNickname; + } + public String getUuid() { return this.uuid; } @@ -115,6 +121,7 @@ public int hashCode() { @Override public String toString() { return "EMUser{" + "username='" + username + '\'' + + ", pushNickname='" + pushNickname + '\'' + ", uuid='" + uuid + '\'' + ", canLogin=" + canLogin + ", pushResources=" + pushResources +