Search in sources :

Example 1 with UserState

use of org.roof.im.user.UserState in project roof-im by madfroglx.

the class CreateSessionVoNode method doNode.

public NodeResult<List<SessionVo>> doNode(Request request, List<Session> sessions) {
    // TODO session缓存
    if (sessions == null || sessions.size() == 0) {
        return new NodeResult<>(NO_INCOMPLETE_SESSION);
    }
    List<SessionVo> sessionVos = new ArrayList<>();
    String username = request.getUsername();
    for (Session session : sessions) {
        String receiver;
        if (StringUtils.equals(session.getReceiver(), username)) {
            receiver = session.getSender();
        } else {
            receiver = session.getReceiver();
        }
        String userState = ImConstant.OFFLINE;
        List<UserState> userStates = userStateService.getStates(receiver);
        if (userStates != null) {
            for (UserState state : userStates) {
                if (StringUtils.equals(state.getState(), ImConstant.ONLINE)) {
                    userState = ImConstant.ONLINE;
                }
            }
        }
        SessionVo sessionVo = new SessionVo();
        sessionVo.setSessionId(session.getId());
        sessionVo.setReceiver(receiver);
        sessionVo.setStartTime(session.getStartTime());
        sessionVo.setEndTime(session.getEndTime());
        sessionVo.setRealEndTime(session.getRealEndTime());
        sessionVo.setState(session.getState());
        sessionVo.setUserState(userState);
        sessionVos.add(sessionVo);
    }
    NodeResult<List<SessionVo>> result = new NodeResult<>(QUERY_INCOMPLETE_SESSION_SUCCESS);
    result.setData(sessionVos);
    return result;
}
Also used : SessionVo(org.roof.im.chain.handler.SessionVo) UserState(org.roof.im.user.UserState) NodeResult(com.roof.chain.support.NodeResult) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Session(org.roof.im.session.Session)

Example 2 with UserState

use of org.roof.im.user.UserState in project roof-im by madfroglx.

the class MessageHandlerNode method doNode.

public NodeResult doNode(MessageRequest request, Message message, ValueStack valueStack) {
    String receiver = request.getReceiver();
    if (!userService.exist(receiver)) {
        return new NodeResult(RECEIVER_NOT_EXISTS);
    }
    List<UserState> states = userStateService.getStates(receiver);
    if (states == null || states.size() == 0) {
        NodeResult r = new NodeResult(RECEIVER_OFFLINE);
        r.setData(message);
        return r;
    }
    valueStack.set(ImConstant.USER_STATES, states);
    return new NodeResult(MESSAGE_HANDLE_SUCCESS);
}
Also used : UserState(org.roof.im.user.UserState) NodeResult(com.roof.chain.support.NodeResult)

Example 3 with UserState

use of org.roof.im.user.UserState in project roof-im by madfroglx.

the class OfflineHandlerNode method doNode.

@Override
public String doNode(OfflineRequest request, ValueStack valueStack) {
    String username = request.getUsername();
    String serverName = serverNameBuilder.getName();
    List<UserState> userStates = userStateService.getStates(username);
    if (userStates == null || userStates.size() == 0) {
        return USER_NOT_ONLINE;
    }
    for (Iterator<UserState> iterator = userStates.iterator(); iterator.hasNext(); ) {
        UserState next = iterator.next();
        if (StringUtils.equals(next.getServerName(), serverName)) {
            iterator.remove();
            if (userStates.size() == 0) {
                userStateService.offline(username);
            } else {
                userStateService.online(username, userStates);
            }
            return OFFLINE_SUCCESS;
        }
    }
    return USER_NOT_ONLINE;
}
Also used : UserState(org.roof.im.user.UserState)

Example 4 with UserState

use of org.roof.im.user.UserState in project roof-im by madfroglx.

the class OfflineTest method testSuccess.

@Test
public void testSuccess() throws Exception {
    List<UserState> states = new ArrayList<>();
    UserState userState = new UserState();
    userState.setConnectId("1");
    userState.setClientType(ClientType.h5.name());
    userState.setUsername("abc");
    userState.setServerName(serverNameBuilder.getName());
    states.add(userState);
    UserState userState2 = new UserState();
    userState2.setConnectId("1");
    userState2.setClientType("applet");
    userState2.setUsername("abc");
    userState2.setServerName(serverNameBuilder.getName());
    states.add(userState2);
    userStateService.online("abc", states);
    OfflineRequest offlineRequest = createRequest();
    ValueStack valueStack = createValueStack(offlineRequest);
    enterChain.doChain(valueStack);
}
Also used : OfflineRequest(org.roof.im.request.OfflineRequest) UserState(org.roof.im.user.UserState) ValueStack(com.roof.chain.api.ValueStack) GenericValueStack(com.roof.chain.support.GenericValueStack) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with UserState

use of org.roof.im.user.UserState in project roof-im by madfroglx.

the class RequestMessagePublishNode method doNode.

public NodeResult doNode(Message message, List<UserState> userStates, ValueStack valueStack) {
    for (UserState userState : userStates) {
        boolean result = false;
        try {
            Message c = new Message();
            BeanUtils.copyProperties(message, c);
            c.setClientType(userState.getClientType());
            result = messagePublisher.publish(c, userState.getServerName(), timeout, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        if (!result) {
            LOGGER.error("push message error: {} ,{}", userState.getUsername(), JSONObject.toJSONString(message));
        }
    }
    NodeResult result = new NodeResult(PUBLISH_SUCCESS);
    result.setData(message);
    return result;
}
Also used : UserState(org.roof.im.user.UserState) Message(org.roof.im.message.Message) NodeResult(com.roof.chain.support.NodeResult)

Aggregations

UserState (org.roof.im.user.UserState)8 NodeResult (com.roof.chain.support.NodeResult)4 ArrayList (java.util.ArrayList)3 ValueStack (com.roof.chain.api.ValueStack)1 GenericValueStack (com.roof.chain.support.GenericValueStack)1 List (java.util.List)1 Test (org.junit.Test)1 SessionVo (org.roof.im.chain.handler.SessionVo)1 Message (org.roof.im.message.Message)1 OfflineRequest (org.roof.im.request.OfflineRequest)1 Session (org.roof.im.session.Session)1