<commit_before>// 
// Copyright 2021 The Matrix.org Foundation C.I.C
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation
import CoreData

internal let StringArrayDelimiter: String = ";"

@objc(MXRoomSummaryMO)
public class MXRoomSummaryMO: NSManagedObject {

    /// Fetch request to be used with any kind of result type
    /// - Returns: NSFetchRequest instance with an abstract result type
    internal static func genericFetchRequest() -> NSFetchRequest<NSFetchRequestResult> {
        return NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
    }

    /// Fetch request to be used with object result type
    /// - Returns: NSFetchRequest instance with `MXRoomSummaryMO` result type
    internal static func typedFetchRequest() -> NSFetchRequest<MXRoomSummaryMO> {
        return NSFetchRequest<MXRoomSummaryMO>(entityName: entityName)
    }

    @NSManaged public var s_identifier: String
    @NSManaged public var s_typeString: String?
    @NSManaged public var s_typeInt: Int16
    @NSManaged public var s_avatar: String?
    @NSManaged public var s_displayName: String?
    @NSManaged public var s_topic: String?
    @NSManaged public var s_creatorUserId: String
    @NSManaged public var s_aliases: String?
    @NSManaged public var s_historyVisibility: String?
    @NSManaged public var s_joinRule: String?
    @NSManaged public var s_membershipInt: Int16
    @NSManaged public var s_membershipTransitionStateInt: Int16
    @NSManaged public var s_isConferenceUserRoom: Bool
    @NSManaged public var s_others: Data?
    @NSManaged public var s_isEncrypted: Bool
    @NSManaged public var s_localUnreadEventCount: Int16
    @NSManaged public var s_notificationCount: Int16
    @NSManaged public var s_highlightCount: Int16
    @NSManaged public var s_hasAnyUnread: Bool
    @NSManaged public var s_hasAnyNotification: Bool
    @NSManaged public var s_hasAnyHighlight: Bool
    @NSManaged public var s_directUserId: String?
    @NSManaged public var s_hiddenFromUser: Bool
    @NSManaged public var s_storedHash: Int64
    @NSManaged public var s_favoriteTagOrder: String?
    @NSManaged public var s_dataTypesInt: Int64
    @NSManaged public var s_sentStatusInt: Int16
    @NSManaged public var s_parentSpaceIds: String?
    @NSManaged public var s_membersCount: MXRoomMembersCountMO?
    @NSManaged public var s_trust: MXUsersTrustLevelSummaryMO?
    @NSManaged public var s_lastMessage: MXRoomLastMessageMO?
    @NSManaged public var s_userIdsSharingLiveBeacon: String?
    
    @discardableResult
    internal static func insert(roomSummary summary: MXRoomSummaryProtocol,
                                into moc: NSManagedObjectContext) -> MXRoomSummaryMO {
        let model = MXRoomSummaryMO(context: moc)
        
        model.update(withRoomSummary: summary, in: moc)
        
        return model
    }
    
    internal func update(withRoomSummary summary: MXRoomSummaryProtocol,
                         in moc: NSManagedObjectContext) {
        s_identifier = summary.roomId
        s_typeString = summary.roomTypeString
        s_typeInt = Int16(summary.roomType.rawValue)
        s_avatar = summary.avatar
        s_displayName = summary.displayname
        s_topic = summary.topic
        s_creatorUserId = summary.creatorUserId
        s_aliases = summary.aliases.joined(separator: StringArrayDelimiter)
        s_historyVisibility = summary.historyVisibility
        s_joinRule = summary.joinRule
        s_membershipInt = Int16(summary.membership.rawValue)
        s_membershipTransitionStateInt = Int16(summary.membershipTransitionState.rawValue)
        s_isConferenceUserRoom = summary.isConferenceUserRoom
        if let others = summary.others {
            s_others = NSKeyedArchiver.archivedData(withRootObject: others)
        } else {
            s_others = nil
        }
        s_isEncrypted = summary.isEncrypted
        s_localUnreadEventCount = Int16(summary.localUnreadEventCount)
        s_notificationCount = Int16(summary.notificationCount)
        s_highlightCount = Int16(summary.highlightCount)
        s_hasAnyUnread = summary.hasAnyUnread
        s_hasAnyNotification = summary.hasAnyNotification<commit_msg>Fix crash on launch due to an insane number of unread rooms<commit_after>// 
// Copyright 2021 The Matrix.org Foundation C.I.C
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation
import CoreData

internal let StringArrayDelimiter: String = ";"

@objc(MXRoomSummaryMO)
public class MXRoomSummaryMO: NSManagedObject {

    /// Fetch request to be used with any kind of result type
    /// - Returns: NSFetchRequest instance with an abstract result type
    internal static func genericFetchRequest() -> NSFetchRequest<NSFetchRequestResult> {
        return NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
    }

    /// Fetch request to be used with object result type
    /// - Returns: NSFetchRequest instance with `MXRoomSummaryMO` result type
    internal static func typedFetchRequest() -> NSFetchRequest<MXRoomSummaryMO> {
        return NSFetchRequest<MXRoomSummaryMO>(entityName: entityName)
    }

    @NSManaged public var s_identifier: String
    @NSManaged public var s_typeString: String?
    @NSManaged public var s_typeInt: Int16
    @NSManaged public var s_avatar: String?
    @NSManaged public var s_displayName: String?
    @NSManaged public var s_topic: String?
    @NSManaged public var s_creatorUserId: String
    @NSManaged public var s_aliases: String?
    @NSManaged public var s_historyVisibility: String?
    @NSManaged public var s_joinRule: String?
    @NSManaged public var s_membershipInt: Int16
    @NSManaged public var s_membershipTransitionStateInt: Int16
    @NSManaged public var s_isConferenceUserRoom: Bool
    @NSManaged public var s_others: Data?
    @NSManaged public var s_isEncrypted: Bool
    @NSManaged public var s_localUnreadEventCount: Int16
    @NSManaged public var s_notificationCount: Int16
    @NSManaged public var s_highlightCount: Int16
    @NSManaged public var s_hasAnyUnread: Bool
    @NSManaged public var s_hasAnyNotification: Bool
    @NSManaged public var s_hasAnyHighlight: Bool
    @NSManaged public var s_directUserId: String?
    @NSManaged public var s_hiddenFromUser: Bool
    @NSManaged public var s_storedHash: Int64
    @NSManaged public var s_favoriteTagOrder: String?
    @NSManaged public var s_dataTypesInt: Int64
    @NSManaged public var s_sentStatusInt: Int16
    @NSManaged public var s_parentSpaceIds: String?
    @NSManaged public var s_membersCount: MXRoomMembersCountMO?
    @NSManaged public var s_trust: MXUsersTrustLevelSummaryMO?
    @NSManaged public var s_lastMessage: MXRoomLastMessageMO?
    @NSManaged public var s_userIdsSharingLiveBeacon: String?
    
    @discardableResult
    internal static func insert(roomSummary summary: MXRoomSummaryProtocol,
                                into moc: NSManagedObjectContext) -> MXRoomSummaryMO {
        let model = MXRoomSummaryMO(context: moc)
        
        model.update(withRoomSummary: summary, in: moc)
        
        return model
    }
    
    internal func update(withRoomSummary summary: MXRoomSummaryProtocol,
                         in moc: NSManagedObjectContext) {
        s_identifier = summary.roomId
        s_typeString = summary.roomTypeString
        s_typeInt = Int16(summary.roomType.rawValue)
        s_avatar = summary.avatar
        s_displayName = summary.displayname
        s_topic = summary.topic
        s_creatorUserId = summary.creatorUserId
        s_aliases = summary.aliases.joined(separator: StringArrayDelimiter)
        s_historyVisibility = summary.historyVisibility
        s_joinRule = summary.joinRule
        s_membershipInt = Int16(summary.membership.rawValue)
        s_membershipTransitionStateInt = Int16(summary.membershipTransitionState.rawValue)
        s_isConferenceUserRoom = summary.isConferenceUserRoom
        if let others = summary.others {
            s_others = NSKeyedArchiver.archivedData(withRootObject: others)
        } else {
            s_others = nil
        }
        s_isEncrypted = summary.isEncrypted
        s_localUnreadEventCount = Int16(min(summary.localUnreadEventCount, UInt(Int16.max)))
        s_notificationCount = Int16(min(summary.notificationCount, UInt(Int16.max)))
        s_highlightCount = Int16(min(summary.highlightCount, UInt(Int16.max)))
        s_hasAnyUnread = summary.hasAnyUnread
        s_hasAnyNotification = summary.hasAnyNotification
{'fraction_non_alphanumeric': 0.062473706352545226, 'fraction_numerical': 0.008624316365166175, 'mean_word_length': 3.5237868696479544, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3835941', 'n_tokens_mistral': 2664, 'n_tokens_neox': 2602, 'n_words': 787}