|
| 1 | +// |
| 2 | +// Copyright 2021 The Matrix.org Foundation C.I.C |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +import Foundation |
| 18 | + |
| 19 | +@objc |
| 20 | +public extension MXEvent { |
| 21 | + |
| 22 | + /// Flag indicating the receiver event should be highlighted |
| 23 | + /// - Parameter session: session instance to read notification rules from |
| 24 | + /// - Returns: true if clients should highlight the receiver event |
| 25 | + func shouldBeHighlighted(inSession session: MXSession) -> Bool { |
| 26 | + let displayNameChecker = MXPushRuleDisplayNameCondtionChecker(matrixSession: session, |
| 27 | + currentUserDisplayName: nil) |
| 28 | + |
| 29 | + if displayNameChecker.isCondition(nil, satisfiedBy: self, roomState: nil, withJsonDict: nil) { |
| 30 | + return true |
| 31 | + } |
| 32 | + guard let rule = session.notificationCenter.rule(matching: self, roomState: nil) else { |
| 33 | + return false |
| 34 | + } |
| 35 | + |
| 36 | + var isHighlighted = false |
| 37 | + |
| 38 | + // Check whether is there an highlight tweak on it |
| 39 | + for ruleAction in rule.actions ?? [] { |
| 40 | + guard let action = ruleAction as? MXPushRuleAction else { continue } |
| 41 | + guard action.actionType == MXPushRuleActionTypeSetTweak else { continue } |
| 42 | + guard action.parameters["set_tweak"] as? String == "highlight" else { continue } |
| 43 | + // Check the highlight tweak "value" |
| 44 | + // If not present, highlight. Else check its value before highlighting |
| 45 | + if nil == action.parameters["value"] || true == (action.parameters["value"] as? Bool) { |
| 46 | + isHighlighted = true |
| 47 | + break |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return isHighlighted |
| 52 | + } |
| 53 | + |
| 54 | +} |
0 commit comments