Skip to content

Commit

Permalink
Add presence function
Browse files Browse the repository at this point in the history
  • Loading branch information
FuDongHai committed Sep 9, 2024
1 parent 26fc54a commit 01e7ad6
Show file tree
Hide file tree
Showing 21 changed files with 1,369 additions and 0 deletions.
404 changes: 404 additions & 0 deletions dependency-reduced-pom.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package com.easemob.im.server.api.presence;

import com.easemob.im.server.api.AbstractIT;
import com.easemob.im.server.api.presence.subscribe.PresenceUserStatusSubscribeResult;
import com.easemob.im.server.api.util.Utilities;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

public class PresenceIT extends AbstractIT {

PresenceIT() {super();}

@Test
void testGetUserStatus() {
String randomUsername = Utilities.randomUserName();
String randomPassword = Utilities.randomPassword();
String resource = "ios_100000";
String status = "1";
String ext = "在线";

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

assertDoesNotThrow(() -> this.service.presence().setUserStatus(randomUsername, resource, status, ext)
.block(Utilities.IT_TIMEOUT));

List<PresenceUserStatusResource> userStatusList = assertDoesNotThrow(() -> this.service.presence().getUserStatus("u1", Arrays.asList(randomUsername))
.block(Utilities.IT_TIMEOUT));
assertNotNull(userStatusList);

userStatusList.forEach(userStatus -> {
Map<String, String> us = (Map<String, String>) userStatus.getStatus();
assertEquals(status, us.get(resource));
assertEquals(ext, userStatus.getExt());
});

List<PresenceUserStatusResource> userStatusList1 = assertDoesNotThrow(() -> this.service.presence().getUserStatus("u1", Arrays.asList("u10000"))
.block(Utilities.IT_TIMEOUT));
assertNotNull(userStatusList1);

userStatusList1.forEach(userStatus -> {
List us = (List) userStatus.getStatus();
assertEquals(0, us.size());
});

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

@Test
void testSubscribeUserStatus() throws InterruptedException {
String randomUsername1 = Utilities.randomUserName();
String randomUsername2 = Utilities.randomUserName();
String randomUsername3 = Utilities.randomUserName();
String randomPassword = Utilities.randomPassword();
String resource = "ios_100000";
String status = "1";
String ext = "在线";

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

assertDoesNotThrow(() -> this.service.presence().setUserStatus(randomUsername1, resource, status, ext)
.block(Utilities.IT_TIMEOUT));
assertDoesNotThrow(() -> this.service.presence().setUserStatus(randomUsername2, resource, status, ext)
.block(Utilities.IT_TIMEOUT));

List<PresenceUserStatusSubscribeResource> subscribeUserStatusList = assertDoesNotThrow(() -> this.service.presence().subscribeUserStatus(randomUsername3, Arrays.asList(randomUsername1, randomUsername2), 86400L)
.block(Utilities.IT_TIMEOUT));
assertNotNull(subscribeUserStatusList);
assertEquals(2, subscribeUserStatusList.size());

Thread.sleep(10000);

PresenceUserStatusSubscribeResult
subscribeResult = assertDoesNotThrow(() -> this.service.presence().getSubscribeList(randomUsername3, 1, 10)
.block(Utilities.IT_TIMEOUT));
assertNotNull(subscribeResult);
assertNotNull(subscribeResult.getSubscribeResourceList());
assertEquals("2", subscribeResult.getTotalNumber());

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

@Test
void testUnsubscribeUserStatus() throws InterruptedException {
String randomUsername1 = Utilities.randomUserName();
String randomUsername2 = Utilities.randomUserName();
String randomPassword = Utilities.randomPassword();
String resource = "ios_100000";
String status = "1";
String ext = "在线";

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

assertDoesNotThrow(() -> this.service.presence().setUserStatus(randomUsername1, resource, status, ext)
.block(Utilities.IT_TIMEOUT));

assertDoesNotThrow(() -> this.service.presence().subscribeUserStatus(randomUsername2, Arrays.asList(randomUsername1), 86400L)
.block(Utilities.IT_TIMEOUT));

Thread.sleep(10000);

PresenceUserStatusSubscribeResult
subscribeResult = assertDoesNotThrow(() -> this.service.presence().getSubscribeList(randomUsername2, 1, 10)
.block(Utilities.IT_TIMEOUT));
assertNotNull(subscribeResult);
assertNotNull(subscribeResult.getSubscribeResourceList());
assertEquals("1", subscribeResult.getTotalNumber());

subscribeResult.getSubscribeResourceList().forEach(subscribeResource -> {
assertEquals(randomUsername1, subscribeResource.getUid());
});

assertDoesNotThrow(() -> this.service.presence().unsubscribeUserStatus(randomUsername2, Arrays.asList(randomUsername1))
.block(Utilities.IT_TIMEOUT));

Thread.sleep(10000);

PresenceUserStatusSubscribeResult
subscribeResult1 = assertDoesNotThrow(() -> this.service.presence().getSubscribeList(randomUsername2, 1, 10)
.block(Utilities.IT_TIMEOUT));
assertNotNull(subscribeResult1);
assertEquals("0", subscribeResult1.getTotalNumber());

subscribeResult.getSubscribeResourceList().forEach(subscribeResource -> {
assertEquals(randomUsername1, subscribeResource.getUid());
});

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

@Test
void testGetOnlineCount() throws InterruptedException {
// String randomOwnerUsername = Utilities.randomUserName();
// String randomPassword = Utilities.randomPassword();
//
// String randomMemberUsername = Utilities.randomUserName();
// List<String> members = new ArrayList<>();
// members.add(randomMemberUsername);
// String resource = "ios_100000";
// String status = "1";
// String ext = "在线";
//
// assertDoesNotThrow(() -> this.service.user().create(randomOwnerUsername, randomPassword)
// .block(Utilities.IT_TIMEOUT));
// assertDoesNotThrow(() -> this.service.user().create(randomMemberUsername, randomPassword)
// .block(Utilities.IT_TIMEOUT));
//
// String groupId = assertDoesNotThrow(() -> this.service.group()
// .createPublicGroup(randomOwnerUsername, "group", "group description", members, 200,
// true).block(Utilities.IT_TIMEOUT));
//
// assertDoesNotThrow(() -> this.service.presence().setUserStatus(randomOwnerUsername, resource, status, ext)
// .block(Utilities.IT_TIMEOUT));
//
// assertDoesNotThrow(() -> this.service.presence().setUserStatus(randomMemberUsername, resource, status, ext)
// .block(Utilities.IT_TIMEOUT));
//
// Thread.sleep(3000);
//
// Integer onlineCount = assertDoesNotThrow(() -> this.service.presence().getUserOnlineCount(groupId, 1)
// .block(Utilities.IT_TIMEOUT));
// assertEquals(2, onlineCount);
//
// assertDoesNotThrow(
// () -> this.service.group().destroyGroup(groupId).block(Utilities.IT_TIMEOUT));
// assertDoesNotThrow(() -> this.service.user().delete(randomOwnerUsername)
// .block(Utilities.IT_TIMEOUT));
// assertDoesNotThrow(() -> this.service.user().delete(randomMemberUsername)
// .block(Utilities.IT_TIMEOUT));
}

}
20 changes: 20 additions & 0 deletions im-sdk-core/src/main/java/com/easemob/im/server/EMService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.easemob.im.server.api.metadata.MetadataApi;
import com.easemob.im.server.api.moderation.ModerationApi;
import com.easemob.im.server.api.mute.MuteApi;
import com.easemob.im.server.api.presence.PresenceApi;
import com.easemob.im.server.api.push.PushApi;
import com.easemob.im.server.api.token.TokenApi;
import com.easemob.im.server.api.room.RoomApi;
Expand Down Expand Up @@ -50,6 +51,8 @@ public class EMService {

private final MuteApi muteApi;

private final PresenceApi presenceApi;

public EMService(EMProperties properties) {
log.debug("EMService properties: {}", properties);
this.context = new DefaultContext(properties);
Expand All @@ -66,6 +69,7 @@ public EMService(EMProperties properties) {
this.pushApi = new PushApi(this.context);
this.moderationApi = new ModerationApi(this.context);
this.muteApi = new MuteApi(this.context);
this.presenceApi = new PresenceApi(this.context);
}

public Context getContext() {
Expand Down Expand Up @@ -265,4 +269,20 @@ public ModerationApi moderation() {
public MuteApi mute() {
return this.muteApi;
}

/**
* Presence用户在线状态API.
* 支持:<br>
* - 设置用户在线状态信息<br>
* - 批量订阅在线状态<br>
* - 批量获取在线状态信息<br>
* - 查询单个群组的在线成员数量<br>
* - 取消订阅多个用户的在线状态<br>
* - 查询订阅列表<br>
*
* @return {@code MuteApi}
*/
public PresenceApi presence() {
return this.presenceApi;
}
}
Loading

0 comments on commit 01e7ad6

Please sign in to comment.