Search in sources :

Example 1 with BingRule

use of org.matrix.androidsdk.rest.model.bingrules.BingRule in project matrix-android-sdk by matrix-org.

the class EventTimeline method triggerPush.

/**
 * Trigger a push if there is a dedicated push rules which implies it.
 *
 * @param event the event
 */
private void triggerPush(Event event) {
    BingRule bingRule;
    boolean outOfTimeEvent = false;
    long maxlifetime = 0;
    long eventLifeTime = 0;
    JsonObject eventContent = event.getContentAsJsonObject();
    if (eventContent.has("lifetime")) {
        maxlifetime = eventContent.get("lifetime").getAsLong();
        eventLifeTime = System.currentTimeMillis() - event.getOriginServerTs();
        outOfTimeEvent = eventLifeTime > maxlifetime;
    }
    BingRulesManager bingRulesManager = mDataHandler.getBingRulesManager();
    // If the bing rules apply, bing
    if (!outOfTimeEvent && (bingRulesManager != null) && (null != (bingRule = bingRulesManager.fulfilledBingRule(event)))) {
        if (bingRule.shouldNotify()) {
            // bing the call events only if they make sense
            if (Event.EVENT_TYPE_CALL_INVITE.equals(event.getType())) {
                long lifeTime = event.getAge();
                if (Long.MAX_VALUE == lifeTime) {
                    lifeTime = System.currentTimeMillis() - event.getOriginServerTs();
                }
                if (lifeTime > MXCall.CALL_TIMEOUT_MS) {
                    Log.d(LOG_TAG, "handleLiveEvent : IGNORED onBingEvent rule id " + bingRule.ruleId + " event id " + event.eventId + " in " + event.roomId);
                    return;
                }
            }
            Log.d(LOG_TAG, "handleLiveEvent : onBingEvent rule id " + bingRule.ruleId + " event id " + event.eventId + " in " + event.roomId);
            mDataHandler.onBingEvent(event, mState, bingRule);
        } else {
            Log.d(LOG_TAG, "handleLiveEvent :rule id " + bingRule.ruleId + " event id " + event.eventId + " in " + event.roomId + " has a mute notify rule");
        }
    } else if (outOfTimeEvent) {
        Log.e(LOG_TAG, "handleLiveEvent : outOfTimeEvent for " + event.eventId + " in " + event.roomId);
        Log.e(LOG_TAG, "handleLiveEvent : outOfTimeEvent maxlifetime " + maxlifetime + " eventLifeTime " + eventLifeTime);
    }
}
Also used : JsonObject(com.google.gson.JsonObject) BingRulesManager(org.matrix.androidsdk.util.BingRulesManager) BingRule(org.matrix.androidsdk.rest.model.bingrules.BingRule)

Example 2 with BingRule

use of org.matrix.androidsdk.rest.model.bingrules.BingRule in project matrix-android-sdk by matrix-org.

the class BingRulesManager method updateRulesSet.

/**
 * Update mRulesSet with the new one.
 *
 * @param ruleSet the new ruleSet to apply
 */
private void updateRulesSet(BingRuleSet ruleSet) {
    synchronized (this) {
        // clear the rules list
        // it is
        mRules.clear();
        // sanity check
        if (null == ruleSet) {
            mRulesSet = new BingRuleSet();
            return;
        }
        // Ensure that the null pointers are replaced by an empty list
        if (ruleSet.override != null) {
            ruleSet.override = new ArrayList<>(ruleSet.override);
            for (BingRule rule : ruleSet.override) {
                rule.kind = BingRule.KIND_OVERRIDE;
            }
            mRules.addAll(ruleSet.override);
        } else {
            ruleSet.override = new ArrayList<>(ruleSet.override);
        }
        if (ruleSet.content != null) {
            ruleSet.content = new ArrayList<>(ruleSet.content);
            for (BingRule rule : ruleSet.content) {
                rule.kind = BingRule.KIND_CONTENT;
            }
            addContentRules(ruleSet.content);
        } else {
            ruleSet.content = new ArrayList<>();
        }
        mIsMentionOnlyMap.clear();
        if (ruleSet.room != null) {
            ruleSet.room = new ArrayList<>(ruleSet.room);
            for (BingRule rule : ruleSet.room) {
                rule.kind = BingRule.KIND_ROOM;
            }
            addRoomRules(ruleSet.room);
        } else {
            ruleSet.room = new ArrayList<>();
        }
        if (ruleSet.sender != null) {
            ruleSet.sender = new ArrayList<>(ruleSet.sender);
            for (BingRule rule : ruleSet.sender) {
                rule.kind = BingRule.KIND_SENDER;
            }
            addSenderRules(ruleSet.sender);
        } else {
            ruleSet.sender = new ArrayList<>();
        }
        if (ruleSet.underride != null) {
            ruleSet.underride = new ArrayList<>(ruleSet.underride);
            for (BingRule rule : ruleSet.underride) {
                rule.kind = BingRule.KIND_UNDERRIDE;
            }
            mRules.addAll(ruleSet.underride);
        } else {
            ruleSet.underride = new ArrayList<>();
        }
        mRulesSet = ruleSet;
        Log.d(LOG_TAG, "## updateRules() : has " + mRules.size() + " rules");
    }
}
Also used : BingRuleSet(org.matrix.androidsdk.rest.model.bingrules.BingRuleSet) BingRule(org.matrix.androidsdk.rest.model.bingrules.BingRule)

Example 3 with BingRule

use of org.matrix.androidsdk.rest.model.bingrules.BingRule in project matrix-android-sdk by matrix-org.

the class BingRulesManager method addSenderRules.

/**
 * Create a sender EventMatchConditions list from a BingRule list
 *
 * @param rules the BingRule list
 */
private void addSenderRules(List<BingRule> rules) {
    if (null != rules) {
        for (BingRule rule : rules) {
            EventMatchCondition condition = new EventMatchCondition();
            condition.kind = Condition.KIND_EVENT_MATCH;
            condition.key = "user_id";
            condition.pattern = rule.ruleId;
            rule.addCondition(condition);
            mRules.add(rule);
        }
    }
}
Also used : EventMatchCondition(org.matrix.androidsdk.rest.model.bingrules.EventMatchCondition) BingRule(org.matrix.androidsdk.rest.model.bingrules.BingRule)

Example 4 with BingRule

use of org.matrix.androidsdk.rest.model.bingrules.BingRule in project matrix-android-sdk by matrix-org.

the class BingRulesManager method getRoomNotificationState.

/**
 * Provide the room notification state
 *
 * @param roomId the room
 * @return the room notification state
 */
public RoomNotificationState getRoomNotificationState(String roomId) {
    if (TextUtils.isEmpty(roomId)) {
        return RoomNotificationState.ALL_MESSAGES;
    }
    if (mRoomNotificationStateByRoomId.containsKey(roomId)) {
        return mRoomNotificationStateByRoomId.get(roomId);
    }
    RoomNotificationState result = RoomNotificationState.ALL_MESSAGES;
    List<BingRule> bingRules = getPushRulesForRoomId(roomId);
    for (BingRule rule : bingRules) {
        if (rule.isEnabled) {
            if (rule.shouldNotNotify()) {
                result = TextUtils.equals(rule.kind, BingRule.KIND_OVERRIDE) ? RoomNotificationState.MUTE : RoomNotificationState.MENTIONS_ONLY;
                break;
            } else if (rule.shouldNotify()) {
                result = (null != rule.getNotificationSound()) ? RoomNotificationState.ALL_MESSAGES_NOISY : RoomNotificationState.ALL_MESSAGES;
            }
        }
    }
    mRoomNotificationStateByRoomId.put(roomId, result);
    return result;
}
Also used : BingRule(org.matrix.androidsdk.rest.model.bingrules.BingRule)

Example 5 with BingRule

use of org.matrix.androidsdk.rest.model.bingrules.BingRule in project matrix-android-sdk by matrix-org.

the class BingRulesManager method addRoomRules.

/**
 * Create a room EventMatchConditions list from a BingRule list
 *
 * @param rules the BingRule list
 */
private void addRoomRules(List<BingRule> rules) {
    if (null != rules) {
        for (BingRule rule : rules) {
            EventMatchCondition condition = new EventMatchCondition();
            condition.kind = Condition.KIND_EVENT_MATCH;
            condition.key = "room_id";
            condition.pattern = rule.ruleId;
            rule.addCondition(condition);
            mRules.add(rule);
        }
    }
}
Also used : EventMatchCondition(org.matrix.androidsdk.rest.model.bingrules.EventMatchCondition) BingRule(org.matrix.androidsdk.rest.model.bingrules.BingRule)

Aggregations

BingRule (org.matrix.androidsdk.rest.model.bingrules.BingRule)8 EventMatchCondition (org.matrix.androidsdk.rest.model.bingrules.EventMatchCondition)3 JsonObject (com.google.gson.JsonObject)1 MyUser (org.matrix.androidsdk.data.MyUser)1 Room (org.matrix.androidsdk.data.Room)1 BingRuleSet (org.matrix.androidsdk.rest.model.bingrules.BingRuleSet)1 Message (org.matrix.androidsdk.rest.model.message.Message)1 BingRulesManager (org.matrix.androidsdk.util.BingRulesManager)1