Skip to content

Commit

Permalink
Create user add push nickname
Browse files Browse the repository at this point in the history
  • Loading branch information
FuDongHai committed Jun 7, 2024
1 parent d886131 commit 7d0b5cd
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down Expand Up @@ -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<EMCreateUser> createUserList = new ArrayList<>();
EMCreateUser createUser = new EMCreateUser(randomUsername, randomPassword, pushNickname);
EMCreateUser createUser1 = new EMCreateUser(randomUsername1, randomPassword, pushNickname);
createUserList.add(createUser);
createUserList.add(createUser1);

List<EMUser> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,43 @@ public Mono<EMUser> create(String username, String password) {
return this.createUser.single(username, password);
}

/**
* 创建用户。
* <p>
* Server SDK 对创建的用户名有自己的限制,如果不想使用该限制,请查看此文档:
* <a href="https://docs-im-beta.easemob.com/document/server-side/java_server_sdk.html#%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9">用户名限制</a>
* <p>
* API使用示例:
* <pre> {@code
* EMService service;
* try {
* EMUser user = service.user().create("username", "password", "pushNickname").block();
* } catch (EMException e) {
* e.getErrorCode();
* e.getMessage();
* }
* }</pre>
*
* @param username 用户名,可以包含小写字母、数字、减号,有效长度1至32个字节
* @param password 密码,可以包含字母、数字、特殊符号(~!@#$%^&amp;*-_=+&lt;&gt;;:,./?),有效长度1至32字节
* @param pushNickname 推送昵称,离线推送时在接收方的客户端推送通知栏中显示的发送方的昵称。你可以自定义该昵称,长度不能超过 100 个字符。
* 提示:1. 若不设置昵称,推送时会显示发送方的用户 ID,而非昵称。
* 2. 该昵称可与用户属性中的昵称设置不同,不过我们建议这两种昵称的设置保持一致。因此,修改其中一个昵称时,也需调用相应方法对另一个进行更新,确保设置一致。更新用户属性中的昵称的方法,详见 设置用户属性。
* @return EMUser
* @see <a href="http://docs-im.easemob.com/im/server/ready/user#%E6%B3%A8%E5%86%8C%E5%8D%95%E4%B8%AA%E7%94%A8%E6%88%B7_%E6%8E%88%E6%9D%83">注册用户</a>
*/
public Mono<EMUser> 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);
}

/**
* 批量创建用户。
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class UserResource {
@JsonProperty("username")
private String username;

@JsonProperty("nickname")
private String pushNickname;

@JsonProperty("uuid")
private String uuid;

Expand All @@ -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<PushResource> pushResources) {
this.username = username;
this.pushNickname = pushNickname;
this.uuid = uuid;
this.activated = activated;
this.pushResources = pushResources;
Expand All @@ -40,6 +45,10 @@ public String getUsername() {
return username;
}

public String getPushNickname() {
return pushNickname;
}

public List<PushResource> getPushResources() {
return pushResources;
}
Expand All @@ -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 +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,32 @@ public Mono<EMUser> 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<EMUser> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
13 changes: 10 additions & 3 deletions im-sdk-core/src/main/java/com/easemob/im/server/model/EMUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<PushResource> pushResources;
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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<PushResource> pushResources) {
public EMUser(String username, String pushNickname, String uuid, Boolean canLogin, List<PushResource> 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;
Expand Down Expand Up @@ -83,6 +85,10 @@ public String getUsername() {
return this.username;
}

public String getPushNickname() {
return this.pushNickname;
}

public String getUuid() {
return this.uuid;
}
Expand Down Expand Up @@ -115,6 +121,7 @@ public int hashCode() {
@Override public String toString() {
return "EMUser{" +
"username='" + username + '\'' +
", pushNickname='" + pushNickname + '\'' +
", uuid='" + uuid + '\'' +
", canLogin=" + canLogin +
", pushResources=" + pushResources +
Expand Down

0 comments on commit 7d0b5cd

Please sign in to comment.