Search in sources :

Example 6 with BingRule

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

the class BingRulesManager method updateRoomNotificationState.

/**
 * Update the notification state of a dedicated room
 *
 * @param roomId   the room id
 * @param state    the new state
 * @param listener the asynchronous callback
 */
public void updateRoomNotificationState(final String roomId, final RoomNotificationState state, final onBingRuleUpdateListener listener) {
    List<BingRule> bingRules = getPushRulesForRoomId(roomId);
    deleteRules(bingRules, new onBingRuleUpdateListener() {

        @Override
        public void onBingRuleUpdateSuccess() {
            if (state == RoomNotificationState.ALL_MESSAGES) {
                forceRulesRefresh(null, listener);
            } else {
                BingRule rule;
                if (state == RoomNotificationState.ALL_MESSAGES_NOISY) {
                    rule = new BingRule(BingRule.KIND_ROOM, roomId, true, false, true);
                } else {
                    rule = new BingRule((state == RoomNotificationState.MENTIONS_ONLY) ? BingRule.KIND_ROOM : BingRule.KIND_OVERRIDE, roomId, false, null, false);
                    EventMatchCondition condition = new EventMatchCondition();
                    condition.key = "room_id";
                    condition.pattern = roomId;
                    rule.addCondition(condition);
                }
                addRule(rule, listener);
            }
        }

        @Override
        public void onBingRuleUpdateFailure(String errorMessage) {
            listener.onBingRuleUpdateFailure(errorMessage);
        }
    });
}
Also used : EventMatchCondition(org.matrix.androidsdk.rest.model.bingrules.EventMatchCondition) BingRule(org.matrix.androidsdk.rest.model.bingrules.BingRule)

Example 7 with BingRule

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

the class BingRulesManager method fulfilledBingRule.

/**
 * Returns the first notifiable bing rule which fulfills its condition with this event.
 *
 * @param event             the event
 * @param highlightRuleOnly true to only check the highlight rule
 * @return the first matched bing rule, null if none
 */
private BingRule fulfilledBingRule(Event event, boolean highlightRuleOnly) {
    // sanity check
    if (null == event) {
        Log.e(LOG_TAG, "## fulfilledBingRule() : null event");
        return null;
    }
    if (!mIsInitialized) {
        Log.e(LOG_TAG, "## fulfilledBingRule() : not initialized");
        return null;
    }
    if (0 == mRules.size()) {
        Log.e(LOG_TAG, "## fulfilledBingRule() : no rules");
        return null;
    }
    // do not trigger notification for oneself messages
    if ((null != event.getSender()) && TextUtils.equals(event.getSender(), mMyUserId)) {
        return null;
    }
    String eventType = event.getType();
    // some types are not bingable
    if (TextUtils.equals(eventType, Event.EVENT_TYPE_PRESENCE) || TextUtils.equals(eventType, Event.EVENT_TYPE_TYPING) || TextUtils.equals(eventType, Event.EVENT_TYPE_REDACTION) || TextUtils.equals(eventType, Event.EVENT_TYPE_RECEIPT) || TextUtils.equals(eventType, Event.EVENT_TYPE_TAGS)) {
        return null;
    }
    // GA issue
    final ArrayList<BingRule> rules;
    synchronized (this) {
        rules = new ArrayList<>(mRules);
    }
    // Go down the rule list until we find a match
    for (BingRule bingRule : rules) {
        if (bingRule.isEnabled && (!highlightRuleOnly || bingRule.shouldHighlight())) {
            boolean isFullfilled = false;
            // so their ruleId defines the method
            if (BingRule.RULE_ID_CONTAIN_USER_NAME.equals(bingRule.ruleId) || BingRule.RULE_ID_CONTAIN_DISPLAY_NAME.equals(bingRule.ruleId)) {
                if (Event.EVENT_TYPE_MESSAGE.equals(event.getType())) {
                    Message message = JsonUtils.toMessage(event.getContent());
                    MyUser myUser = mSession.getMyUser();
                    String pattern = null;
                    if (BingRule.RULE_ID_CONTAIN_USER_NAME.equals(bingRule.ruleId)) {
                        if (mMyUserId.indexOf(":") >= 0) {
                            pattern = mMyUserId.substring(1, mMyUserId.indexOf(":"));
                        } else {
                            pattern = mMyUserId;
                        }
                    } else if (BingRule.RULE_ID_CONTAIN_DISPLAY_NAME.equals(bingRule.ruleId)) {
                        pattern = myUser.displayname;
                        if ((null != mSession.getDataHandler()) && (null != mSession.getDataHandler().getStore())) {
                            Room room = mSession.getDataHandler().getStore().getRoom(event.roomId);
                            if ((null != room) && (null != room.getLiveState())) {
                                String disambiguousedName = room.getLiveState().getMemberName(mMyUserId);
                                if (!TextUtils.equals(disambiguousedName, mMyUserId)) {
                                    pattern = Pattern.quote(disambiguousedName);
                                }
                            }
                        }
                    }
                    if (!TextUtils.isEmpty(pattern)) {
                        isFullfilled = caseInsensitiveFind(pattern, message.body);
                    }
                }
            } else if (BingRule.RULE_ID_FALLBACK.equals(bingRule.ruleId)) {
                isFullfilled = true;
            } else {
                // some default rules define conditions
                // so use them instead of doing a custom treatment
                // RULE_ID_ONE_TO_ONE_ROOM
                // RULE_ID_SUPPRESS_BOTS_NOTIFICATIONS
                isFullfilled = eventMatchesConditions(event, bingRule.conditions);
            }
            if (isFullfilled) {
                return bingRule;
            }
        }
    }
    // no rules are fulfilled
    return null;
}
Also used : MyUser(org.matrix.androidsdk.data.MyUser) Message(org.matrix.androidsdk.rest.model.message.Message) Room(org.matrix.androidsdk.data.Room) BingRule(org.matrix.androidsdk.rest.model.bingrules.BingRule)

Example 8 with BingRule

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

the class EventUtils method shouldHighlight.

/**
 * Whether the given event should be highlighted in its chat room.
 *
 * @param session the session.
 * @param event   the event
 * @return whether the event is important and should be highlighted
 */
public static boolean shouldHighlight(MXSession session, Event event) {
    // sanity check
    if ((null == session) || (null == event)) {
        return false;
    }
    boolean res = false;
    // search if the event fulfills a rule
    BingRule rule = session.fulfillRule(event);
    if (null != rule) {
        res = rule.shouldHighlight();
        if (res) {
            Log.d(LOG_TAG, "## shouldHighlight() : the event " + event.roomId + "/" + event.eventId + " is higlighted by " + rule);
        }
    }
    return res;
}
Also used : 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