<commit_before>//
//  Auth.swift
//  Rocket.Chat
//
//  Created by <NAME> on 7/7/16.
//  Copyright © 2016 Rocket.Chat. All rights reserved.
//

import Foundation
import RealmSwift

final class Auth: Object {
    // Server
    @objc dynamic var serverURL = ""
    @objc dynamic var serverVersion = ""

    var apiHost: URL? {
        guard
            let socketURL = URL(string: serverURL, scheme: "https"),
            var components = URLComponents(url: socketURL, resolvingAgainstBaseURL: true)
        else {
            return nil
        }

        components.path = ""

        return components.url
    }

    @objc dynamic var settings: AuthSettings?

    // Token
    @objc dynamic var token: String?
    @objc dynamic var tokenExpires: Date?
    @objc dynamic var lastAccess: Date?
    @objc dynamic var lastSubscriptionFetch: Date?

    // User
    @objc dynamic var userId: String?

    var user: User? {
        guard let userId = userId else { return nil }
        return self.realm?.object(ofType: User.self, forPrimaryKey: userId)
    }

    // Subscriptions
    let subscriptions = LinkingObjects(fromType: Subscription.self, property: "auth")

    // Primary key from Auth 
    override static func primaryKey() -> String? {
        return "serverURL"
    }
}

extension Auth {
    enum CanDeleteMessageResult {
        case allowed
        case timeElapsed
        case differentUser
        case serverBlocked
        case notActionable
        case unknown
    }

    func canDeleteMessage(_ message: Message) -> CanDeleteMessageResult {
        guard
            let createdAt = message.createdAt,
            let user = user,
            let settings = settings
        else {
            return .unknown
        }

        guard message.type.actionable else { return .notActionable }

        if user.hasPermission(.forceDeleteMessage, realm: self.realm) { return .allowed }

        func timeElapsed() -> Bool {
            if settings.messageAllowDeletingBlockDeleteInMinutes < 1 { return false }
            return Date.serverDate.timeIntervalSince(createdAt)/60 > Double(settings.messageAllowDeletingBlockDeleteInMinutes)
        }

        if user.hasPermission(.deleteMessage, realm: self.realm) { return timeElapsed() ? .timeElapsed : .allowed }

        if message.user != user { return .differentUser }
        if !settings.messageAllowDeleting { return .serverBlocked }

        if timeElapsed() { return .timeElapsed }

        return .allowed
    }
}<commit_msg>Fix code style<commit_after>//
//  Auth.swift
//  Rocket.Chat
//
//  Created by <NAME> on 7/7/16.
//  Copyright © 2016 Rocket.Chat. All rights reserved.
//

import Foundation
import RealmSwift

final class Auth: Object {
    // Server
    @objc dynamic var serverURL = ""
    @objc dynamic var serverVersion = ""

    var apiHost: URL? {
        guard
            let socketURL = URL(string: serverURL, scheme: "https"),
            var components = URLComponents(url: socketURL, resolvingAgainstBaseURL: true)
        else {
            return nil
        }

        components.path = ""

        return components.url
    }

    @objc dynamic var settings: AuthSettings?

    // Token
    @objc dynamic var token: String?
    @objc dynamic var tokenExpires: Date?
    @objc dynamic var lastAccess: Date?
    @objc dynamic var lastSubscriptionFetch: Date?

    // User
    @objc dynamic var userId: String?

    var user: User? {
        guard let userId = userId else { return nil }
        return self.realm?.object(ofType: User.self, forPrimaryKey: userId)
    }

    // Subscriptions
    let subscriptions = LinkingObjects(fromType: Subscription.self, property: "auth")

    // Primary key from Auth 
    override static func primaryKey() -> String? {
        return "serverURL"
    }
}

extension Auth {
    enum CanDeleteMessageResult {
        case allowed
        case timeElapsed
        case differentUser
        case serverBlocked
        case notActionable
        case unknown
    }

    func canDeleteMessage(_ message: Message) -> CanDeleteMessageResult {
        guard
            let createdAt = message.createdAt,
            let user = user,
            let settings = settings
        else {
            return .unknown
        }

        if !message.type.actionable {
            return .notActionable
        }

        if user.hasPermission(.forceDeleteMessage, realm: self.realm) {
            return .allowed
        }

        func timeElapsed() -> Bool {
            if settings.messageAllowDeletingBlockDeleteInMinutes < 1 {
                return false
            }

            return Date.serverDate.timeIntervalSince(createdAt)/60 > Double(settings.messageAllowDeletingBlockDeleteInMinutes)
        }

        if user.hasPermission(.deleteMessage, realm: self.realm) {
            return timeElapsed() ? .timeElapsed : .allowed
        }

        if message.user != user { return .differentUser }
        if !settings.messageAllowDeleting { return .serverBlocked }

        if timeElapsed() { return .timeElapsed }

        return .allowed
    }
}
{'fraction_non_alphanumeric': 0.08403857508364496, 'fraction_numerical': 0.004329856327494588, 'mean_word_length': 2.123540258143823, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '790015', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1349, 'n_words': 397}