@VisibleForTesting
    public Mono<Boolean> addOnlineDeviceIfAbsent(
            @NotNull ByteBuf localNodeId,
            @NotNull Long userId,
            @NotNull @ValidDeviceType DeviceType deviceType,
            @Nullable Map<String, String> deviceDetails,
            @Nullable UserStatus userStatus,
            int heartbeatSeconds,
            @Nullable String expectedNodeId,
            @Nullable Long expectedDeviceTimestampSeconds) {
        try {
            Validator.notNull(userId, "userId");
            Validator.notNull(deviceType, "deviceType");
            DeviceTypeUtil.validDeviceType(deviceType);
            Validator.notEquals(userStatus,
                    UserStatus.UNRECOGNIZED,
                    "The user status must not be UNRECOGNIZED");
            Validator.notEquals(userStatus,
                    UserStatus.OFFLINE,
                    "The user status must not be OFFLINE");
        } catch (ResponseException e) {
            return Mono.error(e);
        }
        int deviceDetailCount = CollectionUtil.getSize(deviceDetails);
        boolean hasDeviceDetails = deviceDetailCount > 0;
        boolean hasUserStatus = userStatus != null;
        boolean hasExpectedValues =
                expectedNodeId != null && expectedDeviceTimestampSeconds != null;
        int count = 4;
        if (hasDeviceDetails) {
            count += 3 + 2 * deviceDetailCount;
        } else if (hasExpectedValues) {
            count += 3;
        } else if (hasUserStatus) {
            count++;
        }
        int index = 0;
        ByteBuf[] args = new ByteBuf[count];
        try {
            args[index++] = ByteBufUtil.writeLong(userId);
            args[index++] = ByteBufUtil.writeByte((byte) deviceType.getNumber());
            args[index++] = localNodeId;
            args[index++] = ByteBufUtil.writeShort((short) heartbeatSeconds);
            if (hasUserStatus) {
                args[index++] = ByteBufUtil.writeByte((byte) userStatus.getNumber());
            } else {
                index++;
            }
            if (hasExpectedValues) {
                args[index++] = ByteBufUtil.writeString(expectedNodeId);
                args[index++] = ByteBufUtil.writeLong(expectedDeviceTimestampSeconds);
            } else {
                index += 2;
            }
            if (hasDeviceDetails) {
                for (Map.Entry<String, String> entry : deviceDetails.entrySet()) {
                    args[index++] = ByteBufUtil.writeString(entry.getKey());
                    args[index++] = ByteBufUtil.writeString(entry.getValue());
                }
            }
        } catch (Exception e) {
            ReferenceCountUtil.ensureReleased(args, 0, index);
            return Mono.error(new InputOutputException("Failed to encode arguments", e));
        }
        return sessionRedisClientManager.eval(userId, addOnlineUserScript, args)
                .map(buffer -> {
                    byte returnCode = buffer.readByte();
                    return switch (returnCode) {
                        case '0' -> false;
                        case '1' -> {
                            if (cacheUserSessionsStatus) {
                                Map<DeviceType, UserDeviceSessionInfo> deviceTypeToSessions =
                                        new FastEnumMap<>(DeviceType.class);
                                deviceTypeToSessions.put(deviceType,
                                        new UserDeviceSessionInfo(
                                                this.localNodeId,
                                                System.currentTimeMillis(),
                                                true));
                                userIdToStatusCache.put(userId,
                                        new UserSessionsStatus(
                                                userId,
                                                userStatus,
                                                deviceTypeToSessions));
                            }
                            yield true;
                        }
                        case '2' -> true;
                        default -> throw new RuntimeException(
                                "Unexpected return code: "
                                        + returnCode);
                    };
                });
    }
@return true if the userId:deviceType was absent (offline)
turms-server-common/src/main/java/im/turms/server/common/domain/session/service/UserStatusService.java