<commit_before>//
//  MXSpaceNotificationCounter.swift
//  MatrixSDK
//
//  Created by <NAME> on 19/09/2021.
//

import Foundation

@objcMembers
public class MXSpaceNotificationState: NSObject {
    public var favouriteMissedDiscussionsCount: UInt = 0
    public var favouriteMissedDiscussionsHighlightedCount: UInt = 0
    public var directMissedDiscussionsCount: UInt = 0
    public var directMissedDiscussionsHighlightedCount: UInt = 0
    public var groupMissedDiscussionsCount: UInt = 0
    public var groupMissedDiscussionsHighlightedCount: UInt = 0
    public var allCount: UInt {
        return favouriteMissedDiscussionsCount + directMissedDiscussionsCount + groupMissedDiscussionsCount
    }
    public var allHighlightCount: UInt {
        return favouriteMissedDiscussionsHighlightedCount + directMissedDiscussionsHighlightedCount + groupMissedDiscussionsHighlightedCount
    }
    
    static func +(left: MXSpaceNotificationState, right: MXSpaceNotificationState) -> MXSpaceNotificationState {
        let sum = MXSpaceNotificationState()
        sum.favouriteMissedDiscussionsCount = left.favouriteMissedDiscussionsCount + right.favouriteMissedDiscussionsCount
        sum.favouriteMissedDiscussionsHighlightedCount = left.favouriteMissedDiscussionsHighlightedCount + right.favouriteMissedDiscussionsHighlightedCount
        sum.directMissedDiscussionsCount = left.directMissedDiscussionsCount + right.directMissedDiscussionsCount
        sum.directMissedDiscussionsHighlightedCount = left.directMissedDiscussionsHighlightedCount + right.directMissedDiscussionsHighlightedCount
        sum.groupMissedDiscussionsCount = left.groupMissedDiscussionsCount + right.groupMissedDiscussionsCount
        sum.groupMissedDiscussionsHighlightedCount = left.groupMissedDiscussionsHighlightedCount + right.groupMissedDiscussionsHighlightedCount
        return sum
    }
    
    static public func +=( left: inout MXSpaceNotificationState, right: MXSpaceNotificationState) {
        left = left + right
    }
}

@objcMembers
public class MXSpaceNotificationCounter: NSObject {
    
    // MARK: - Properties
    
    public var homeNotificationState = MXSpaceNotificationState()
    private var notificationStatePerSpaceId: [String:MXSpaceNotificationState] = [:]
    
    // MARK: - Public
    
    /// Compute the notification count for every spaces
    /// - Parameters:
    ///   - spaces: list of spaces of the current sessiom
    ///   - rooms: list of rooms of the current session
    ///   - flattenedParentIds: the flattened aprent ID for all rooms
    public func computeNotificationCount(for spaces:[MXSpace], with rooms:[MXRoom], flattenedParentIds: [String: Set<String>]) {
        let startDate = Date()
        MXLog.debug("[Spaces] computeNotificationCount started")

        var homeNotificationState = MXSpaceNotificationState()
        var notificationStatePerSpaceId: [String:MXSpaceNotificationState] = [:]
        
        for room in rooms {
            guard room.summary.roomType != .space else {
                continue
            }
            
            let notificationState = self.notificationState(forRoomWithId: room)
            
            homeNotificationState += notificationState
            for space in spaces {
                if flattenedParentIds[room.roomId]?.contains(space.spaceId) ?? false {
                    let storedState = notificationStatePerSpaceId[space.spaceId] ?? MXSpaceNotificationState()
                    notificationStatePerSpaceId[space.spaceId] = storedState + notificationState
                }
            }
        }
        
        self.homeNotificationState = homeNotificationState
        self.notificationStatePerSpaceId = notificationStatePerSpaceId
        
        MXLog.debug("[Spaces] computeNotificationCount ended after \(Date().timeIntervalSince(startDate))s")
    }
    
    /// Notification state for a given space
    /// - Parameters:
    ///   - spaceId: ID of the space
    /// - Returns: a `MXSpaceNotificationState` instance with the number of notifications for the given space
    public func notificationState(forSpaceWithId spaceId: String) -> MXSpaceNotificationState {
        return notificationStatePerSpaceId[spaceId] ?? MXSpaceNotificationState()
    }
    
    /// Notification state for a all spaces except for a given space
    /// - Parameters:
    ///   - spaceId: ID of the space to be excluded
    /// - Returns: a `MXSpaceNotificationState` instance with the number of notifications for all spaces
    public func notificationState(forAllSpacesExcept spaceId: String?) -> MXSpaceNotificationState {
        var notificationState = MXSpaceNotificationState()
        notificationStatePerSpaceId.forEach { (key: String, state: MXSpaceNotificationState) in
            if key != spaceId {
                notificationState += state
            }
        }
        return notificationState
    }
    
    // MARK: - Private
    
    private func notificationState(forRoomWithId room: MXRoom) -> MXSpaceNotificationState {
        let notificationState = MXSpaceNotificationState()
        guard let summary = room.summary else {
            return notificationState
        }
        
        let notificationCount = summary.notificationCount
        
        if notificationCount > 0 {
            let tags = room.accountData.tags
            if tags != nil, tags?[kMXRoomTagFavourite] != nil {
                notificationState.favouriteMissedDiscussionsCount += summary.notificationCount
                notificationState.favouriteMissedDiscussionsHighlightedCount += summary.highlightCount
            }
            
            if summary.isDirect {
                notificationState.directMissedDiscussionsCount += summary.notificationCount
                notificationState.directMissedDiscussionsHighlightedCount += summary.highlightCount
            } else if tags?.isEmpty ?? true || tags?[kMXRoomTagFavourite] != nil {
                notificationState.groupMissedDiscussionsCount += summary.notificationCount
                notificationState.groupMissedDiscussionsHighlightedCount += summary.highlightCount
            }
        } else if summary.membership == .invite {
            if room.isDirect {
                notificationState.directMissedDiscussionsHighlightedCount += 1
            } else {
                notificationState.groupMissedDiscussionsHighlightedCount += 1
            }
        }
        
        return notificationState
    }

}
<commit_msg>[spaces] several fixes for pre release<commit_after>//
//  MXSpaceNotificationCounter.swift
//  MatrixSDK
//
//  Created by <NAME> on 19/09/2021.
//

import Foundation

@objcMembers
public class MXSpaceNotificationState: NSObject {
    public var favouriteMissedDiscussionsCount: UInt = 0
    public var favouriteMissedDiscussionsHighlightedCount: UInt = 0
    public var directMissedDiscussionsCount: UInt = 0
    public var directMissedDiscussionsHighlightedCount: UInt = 0
    public var groupMissedDiscussionsCount: UInt = 0
    public var groupMissedDiscussionsHighlightedCount: UInt = 0
    public var allCount: UInt {
        return favouriteMissedDiscussionsCount + directMissedDiscussionsCount + groupMissedDiscussionsCount
    }
    public var allHighlightCount: UInt {
        return favouriteMissedDiscussionsHighlightedCount + directMissedDiscussionsHighlightedCount + groupMissedDiscussionsHighlightedCount
    }
    
    static func +(left: MXSpaceNotificationState, right: MXSpaceNotificationState) -> MXSpaceNotificationState {
        let sum = MXSpaceNotificationState()
        sum.favouriteMissedDiscussionsCount = left.favouriteMissedDiscussionsCount + right.favouriteMissedDiscussionsCount
        sum.favouriteMissedDiscussionsHighlightedCount = left.favouriteMissedDiscussionsHighlightedCount + right.favouriteMissedDiscussionsHighlightedCount
        sum.directMissedDiscussionsCount = left.directMissedDiscussionsCount + right.directMissedDiscussionsCount
        sum.directMissedDiscussionsHighlightedCount = left.directMissedDiscussionsHighlightedCount + right.directMissedDiscussionsHighlightedCount
        sum.groupMissedDiscussionsCount = left.groupMissedDiscussionsCount + right.groupMissedDiscussionsCount
        sum.groupMissedDiscussionsHighlightedCount = left.groupMissedDiscussionsHighlightedCount + right.groupMissedDiscussionsHighlightedCount
        return sum
    }
    
    static public func +=( left: inout MXSpaceNotificationState, right: MXSpaceNotificationState) {
        left = left + right
    }
}

@objcMembers
public class MXSpaceNotificationCounter: NSObject {
    
    // MARK: - Properties
    
    public var homeNotificationState = MXSpaceNotificationState()
    private var notificationStatePerSpaceId: [String:MXSpaceNotificationState] = [:]
    
    // MARK: - Public
    
    /// Compute the notification count for every spaces
    /// - Parameters:
    ///   - spaces: list of spaces of the current sessiom
    ///   - rooms: list of rooms of the current session
    ///   - flattenedParentIds: the flattened aprent ID for all rooms
    public func computeNotificationCount(for spaces:[MXSpace], with rooms:[MXRoom], flattenedParentIds: [String: Set<String>]) {
        let startDate = Date()
        MXLog.debug("[Spaces] computeNotificationCount started")

        var homeNotificationState = MXSpaceNotificationState()
        var notificationStatePerSpaceId: [String:MXSpaceNotificationState] = [:]
        
        for room in rooms {
            guard room.summary.roomType != .space else {
                continue
            }
            
            let notificationState = self.notificationState(forRoomWithId: room)
            
            homeNotificationState += notificationState
            for space in spaces {
                if flattenedParentIds[room.roomId]?.contains(space.spaceId) ?? false {
                    let storedState = notificationStatePerSpaceId[space.spaceId] ?? MXSpaceNotificationState()
                    notificationStatePerSpaceId[space.spaceId] = storedState + notificationState
                }
            }
        }
        
        self.homeNotificationState = homeNotificationState
        self.notificationStatePerSpaceId = notificationStatePerSpaceId
        
        MXLog.debug("[Spaces] computeNotificationCount ended after \(Date().timeIntervalSince(startDate))s")
    }
    
    /// Notification state for a given space
    /// - Parameters:
    ///   - spaceId: ID of the space
    /// - Returns: a `MXSpaceNotificationState` instance with the number of notifications for the given space
    public func notificationState(forSpaceWithId spaceId: String) -> MXSpaceNotificationState {
        return notificationStatePerSpaceId[spaceId] ?? MXSpaceNotificationState()
    }
    
    /// Notification state for a all spaces except for a given space
    /// - Parameters:
    ///   - spaceId: ID of the space to be excluded
    /// - Returns: a `MXSpaceNotificationState` instance with the number of notifications for all spaces
    public func notificationState(forAllSpacesExcept spaceId: String?) -> MXSpaceNotificationState {
        var notificationState = MXSpaceNotificationState()
        notificationStatePerSpaceId.forEach { (key: String, state: MXSpaceNotificationState) in
            if key != spaceId {
                notificationState += state
            }
        }
        return notificationState
    }
    
    // MARK: - Private
    
    private func notificationState(forRoomWithId room: MXRoom) -> MXSpaceNotificationState {
        let notificationState = MXSpaceNotificationState()
        guard let summary = room.summary else {
            return notificationState
        }
        
        let notificationCount = self.isRoomMentionsOnly(summary) ? summary.highlightCount : summary.notificationCount
        
        if notificationCount > 0 {
            let tags = room.accountData.tags
            if tags != nil, tags?[kMXRoomTagFavourite] != nil {
                notificationState.favouriteMissedDiscussionsCount += summary.notificationCount
                notificationState.favouriteMissedDiscussionsHighlightedCount += summary.highlightCount
            }
            
            if summary.isDirect {
                notificationState.directMissedDiscussionsCount += summary.notificationCount
                notificationState.directMissedDiscussionsHighlightedCount += summary.highlightCount
            } else if tags?.isEmpty ?? true || tags?[kMXRoomTagFavourite] != nil {
                notificationState.groupMissedDiscussionsCount += summary.notificationCount
                notificationState.groupMissedDiscussionsHighlightedCount += summary.highlightCount
            }
        } else if summary.membership == .invite {
            if room.isDirect {
                notificationState.directMissedDiscussionsHighlightedCount += 1
            } else {
                notificationState.groupMissedDiscussionsHighlightedCount += 1
            }
        }
        
        return notificationState
    }
    
    private func isRoomMentionsOnly(_ summary: MXRoomSummary) -> Bool {
        guard let rules = summary.mxSession.notificationCenter.rules.global.room as? [MXPushRule] else {
            return false
        }
        
        for rule in rules {
            guard rule.ruleId == summary.roomId, let ruleActions = rule.actions as? [MXPushRuleAction] else {
                continue
            }
            
            for ruleAction in ruleActions where ruleAction.actionType == MXPushRuleActionTypeDontNotify {
                return rule.enabled
            }
            break
        }
        
        return false
    }
}

{'fraction_non_alphanumeric': 0.06449730045235663, 'fraction_numerical': 0.0024806654020137165, 'mean_word_length': 2.773953744493392, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5100120', 'n_tokens_mistral': 3524, 'n_tokens_neox': 3309, 'n_words': 774}