<commit_before>//
//  Defensive.swift
//  PartyUP
//
//  Created by <NAME> on 2016-02-28.
//  Copyright © 2016 Sandcastle Application Development. All rights reserved.
//

import Foundation

class Defensive {
    
    static let shared = Defensive()

	init() {
        if let input = NSInputStream(URL: Defensive.path) {
            input.open()
            defer { input.close() }
            var raw = [UInt8](count: 16, repeatedValue: 0)
            while input.hasBytesAvailable {
                if input.read(&raw, maxLength: raw.count) == raw.count {
                    muted.insert(NSUUID(UUIDBytes: raw))
                }
            }
        }
	}

	func mute(user: NSUUID) {
        if !muted.contains(user) {
            muted.insert(user)
            
            if let output = NSOutputStream(URL: Defensive.path, append: true) {
                var raw = [UInt8](count: 16, repeatedValue: 0)
                output.open()
                defer { output.close() }
                user.getUUIDBytes(&raw)
                output.write(&raw, maxLength: raw.count)
            }
        }
	}
    
    func muted(user: NSUUID) -> Bool {
        return muted.contains(user)
    }

	private static let path: NSURL! = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!, isDirectory: true).URLByAppendingPathComponent("MutedUsers.dat")

	private var muted = Set<NSUUID>()<commit_msg>Implement unmute for symmetry with mute.<commit_after>//
//  Defensive.swift
//  PartyUP
//
//  Created by <NAME> on 2016-02-28.
//  Copyright © 2016 Sandcastle Application Development. All rights reserved.
//

import Foundation

class Defensive {
    
    static let shared = Defensive()

	init() {
        if let input = NSInputStream(URL: Defensive.path) {
            input.open()
            defer { input.close() }
            var raw = [UInt8](count: 16, repeatedValue: 0)
            while input.hasBytesAvailable {
                if input.read(&raw, maxLength: raw.count) == raw.count {
                    muted.insert(NSUUID(UUIDBytes: raw))
                }
            }
        }
	}

	func mute(user: NSUUID) {
        if !muted.contains(user) {
            muted.insert(user)
            
            if let output = NSOutputStream(URL: Defensive.path, append: true) {
                var raw = [UInt8](count: 16, repeatedValue: 0)
                output.open()
                defer { output.close() }
                user.getUUIDBytes(&raw)
                output.write(&raw, maxLength: raw.count)
            }
        }
	}

	func unmute(user: NSUUID) {
		if muted.contains(user) {
			muted.remove(user)

			if let output = NSOutputStream(URL: Defensive.path, append: false) {
				var raw = [UInt8](count: 16, repeatedValue: 0)
				output.open()
				defer { output.close() }

				for uuid in muted {
					uuid.getUUIDBytes(&raw)
					output.write(&raw, maxLength: raw.count)
				}
			}
		}
	}

    func muted(user: NSUUID) -> Bool {
        return muted.contains(user)
    }

	private static let path: NSURL! = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!, isDirectory: true).URLByAppendingPathComponent("MutedUsers.dat")

	private var muted = Set<NSUUID>()
{'fraction_non_alphanumeric': 0.11610942249240122, 'fraction_numerical': 0.013373860182370821, 'mean_word_length': 2.027598896044158, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2846644', 'n_tokens_mistral': 1076, 'n_tokens_neox': 973, 'n_words': 226}