Skip to content

Commit

Permalink
Add batch modify user push nickname function
Browse files Browse the repository at this point in the history
  • Loading branch information
FuDongHai committed Jun 28, 2024
1 parent 84be941 commit e632cfc
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public class AbstractIT {

public AbstractIT() {
String realm = System.getenv("IM_REALM");
// String appkey = System.getenv("IM_APPKEY");
String appkey = "62242102#90";
String appkey = System.getenv("IM_APPKEY");
String baseUri = System.getenv("IM_BASE_URI");

EMProperties properties = null;
Expand All @@ -28,11 +27,8 @@ public AbstractIT() {
.build();

} else {
// String clientId = System.getenv("IM_CLIENT_ID");
// String clientSecret = System.getenv("IM_CLIENT_SECRET");

String clientId = "YXA6a1jQXZnASMeiRB_z6Vo9wA";
String clientSecret = "YXA6LDJ_YHmppwgccxHNEZmyMnjWy1E";
String clientId = System.getenv("IM_CLIENT_ID");
String clientSecret = System.getenv("IM_CLIENT_SECRET");

properties = EMProperties.builder()
.setRealm(EMProperties.Realm.EASEMOB_REALM)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
import com.easemob.im.server.api.util.Utilities;
import com.easemob.im.server.model.EMConversationType;
import com.easemob.im.server.model.EMNotificationType;
import com.easemob.im.server.model.EMPushNickname;
import com.easemob.im.server.model.EMUser;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class PushIT extends AbstractIT {

Expand All @@ -28,6 +34,33 @@ void testUpdateUserNickname() {
() -> this.service.user().delete(randomUsername).block(Utilities.IT_TIMEOUT));
}

@Test
void testUpdateUserNicknames() {
String randomUsername = Utilities.randomUserName();
String randomUsername1 = Utilities.randomUserName();
String randomPassword = Utilities.randomPassword();

assertDoesNotThrow(() -> this.service.user().create(randomUsername, randomPassword)
.block(Utilities.IT_TIMEOUT));
assertDoesNotThrow(() -> this.service.user().create(randomUsername1, randomPassword)
.block(Utilities.IT_TIMEOUT));

List<EMPushNickname> pushNicknames = new ArrayList<>();
pushNicknames.add(new EMPushNickname(randomUsername, "推送昵称-1"));
pushNicknames.add(new EMPushNickname(randomUsername1, "推送昵称-2"));

assertDoesNotThrow(() -> this.service.push().updateUserNicknames(pushNicknames)
.block(Utilities.IT_TIMEOUT));

EMUser user = this.service.user().get(randomUsername).block(Utilities.IT_TIMEOUT);
assertEquals("推送昵称-1", user.getPushNickname());

assertDoesNotThrow(
() -> this.service.user().delete(randomUsername).block(Utilities.IT_TIMEOUT));
assertDoesNotThrow(
() -> this.service.user().delete(randomUsername1).block(Utilities.IT_TIMEOUT));
}

@Test
void testNotificationDisplayStyle() {
String randomUsername = Utilities.randomUserName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import com.easemob.im.server.exception.EMInvalidArgumentException;
import com.easemob.im.server.model.EMConversationType;
import com.easemob.im.server.model.EMNotificationType;
import com.easemob.im.server.model.EMPushNickname;
import com.easemob.im.server.model.EMUser;
import reactor.core.publisher.Mono;

import java.util.List;

/**
* 推送API。
*/
Expand Down Expand Up @@ -66,6 +69,32 @@ public Mono<Void> updateUserNickname(String username, String nickname) {
return this.updateUserNickname.update(username, nickname);
}

/**
* 批量设置离线推送时显示的昵称。
* <p>
* API使用示例:
* <pre> {@code
* EMService service;
*
* try {
* List<EMPushNickname> pushNicknames = new ArrayList<>();
* pushNicknames.add(new EMPushNickname("user1", "推送昵称-1"));
* pushNicknames.add(new EMPushNickname("user2", "推送昵称-2"));
*
* service.push().updateUserNicknames(pushNicknames).block();
* } catch (EMException e) {
* e.getErrorCode();
* e.getMessage();
* }
* }</pre>
*
* @param pushNicknames 需要设置离线推送时显示的昵称列表,EMPushNickname 中包含用户名以及推送昵称,推送昵称长度不能超过 100 个字符。 支持以下字符集: \- 26 个小写英文字母 a-z; \- 26 个大写英文字母 A-Z; \- 10 个数字 0-9; \- 中文; \- 特殊字符。
* @return {@code Mono}
*/
public Mono<Void> updateUserNicknames(List<EMPushNickname> pushNicknames) {
return this.updateUserNickname.update(pushNicknames);
}

/**
* 设置推送消息展示方式,指客户端的推送通知栏展示消息的样式。
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import com.easemob.im.server.api.DefaultErrorMapper;
import com.easemob.im.server.api.ErrorMapper;
import com.easemob.im.server.exception.EMUnknownException;
import com.easemob.im.server.model.EMPushNickname;
import io.netty.util.ReferenceCounted;
import reactor.core.publisher.Mono;

import java.util.List;

public class UpdateUserNickname {

private Context context;
Expand Down Expand Up @@ -35,4 +38,24 @@ public Mono<Void> update(String username, String nickname) {
.then();
}

public Mono<Void> update(List<EMPushNickname> pushNicknames) {
return this.context.getHttpClient()
.flatMap(httpClient -> httpClient.put()
.uri("/push/nickname")
.send(Mono.create(sink -> sink.success(this.context.getCodec()
.encode(pushNicknames))))
.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);
});
}))
.doOnSuccess(ReferenceCounted::release)
.then();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.easemob.im.server.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class EMPushNickname {

@JsonProperty("username")
private String username;

@JsonProperty("push_nickname")
private String pushNickname;

@JsonCreator
public EMPushNickname(@JsonProperty("username") String username,
@JsonProperty("push_nickname") String pushNickname) {
this.username = username;
this.pushNickname = pushNickname;
}
}

0 comments on commit e632cfc

Please sign in to comment.