<commit_before>//
//  MMMessageHandler.swift
//  MobileMessaging
//
//  Created by <NAME>. on 24/02/16.
//  
//

import Foundation
import CoreData

protocol MobileMessagingService {
	var isRunning: Bool { get }
	func start(_ completion: ((Bool) -> Void)?)
	func stop(_ completion: ((Bool) -> Void)?)
}

final class MMMessageHandler: MobileMessagingService {
	var isRunning: Bool = true
	func start(_ completion: ((Bool) -> Void)? = nil) {
		isRunning = true
		completion?(true)
	}
	func stop(_ completion: ((Bool) -> Void)? = nil) {
		isRunning = false
		completion?(true)
	}
	
	lazy var messageHandlingQueue = OperationQueue.mm_newSerialQueue
	lazy var messageSendingQueue = OperationQueue()
	lazy var seenPostponer = MMPostponer(executionQueue: DispatchQueue.main)
	
	deinit {
		messageHandlingQueue.cancelAllOperations()
	}
	
	let storage: MMCoreDataStorage
    
	init(storage: MMCoreDataStorage) {
		self.storage = storage
        self.evictOldMessages()
    }

    //MARK: Intenal
	func handleAPNSMessage(_ userInfo: APNSPayload, applicationState: UIApplicationState, newMessageReceivedCallback: ((APNSPayload) -> Void)? = nil, completion: ((NSError?) -> Void)? = nil) {
		guard isRunning == true else {
			completion?(nil)
			return
		}
		if let msg = MMMessageFactory.makeMessage(with: userInfo, createdDate: Date()) {
			messageHandlingQueue.addOperation(MessageHandlingOperation(messagesToHandle: [msg], messagesDeliveryMethod: .push, context: storage.newPrivateContext(), messageHandler: MobileMessaging.messageHandling, applicationState: applicationState, finishBlock: completion))
		} else {
			MMLogError("Error while converting payload:\n\(userInfo)\nto MMMessage")
			completion?(NSError.init(type: .UnknownError))
		}
	}
	
	func syncWithServer(_ completion: ((NSError?) -> Void)? = nil) {
		guard isRunning == true else {
			completion?(nil)
			return
		}
		messageHandlingQueue.addOperation(MessagesSyncOperation(context: storage.newPrivateContext(), finishBlock: completion))
	}
	
	func evictOldMessages(_ messageAge: TimeInterval? = nil, completion:((Void) -> Void)? = nil) {
		guard isRunning == true else {
			completion?()
			return
		}
		messageHandlingQueue.addOperation(MessagesEvictionOperation(context: storage.newPrivateContext(), messageMaximumAge: messageAge, finishBlock: completion))
    }
	
    func setSeen(_ messageIds: [String], completion: ((SeenStatusSendingResult) -> Void)? = nil) {
		guard isRunning == true else {
			completion?(SeenStatusSendingResult.Cancel)
			return
		}
		messageHandlingQueue.addOperation(SeenStatusPersistingOperation(messageIds: messageIds, context: storage.newPrivateContext()))
		seenPostponer.postponeBlock() {
			self.messageHandlingQueue.addOperation(SeenStatusSendingOperation(context: self.storage.newPrivateContext(), finishBlock: completion))
		}
    }
	
	func sendMessages(_ messages: [MOMessage], completion: (([MOMessage]?, NSError?) -> Void)? = nil) {
		guard isRunning == true else {
			completion?(nil, nil)
			return
		}
		messageSendingQueue.addOperation(MessagePostingOperation(messages: messages, context: storage.newPrivateContext(), finishBlock: { (result: MOMessageSendingResult) in
			
			completion?(result.value?.messages, result.error)
		}))
	}
}<commit_msg>MessageHandler still should perform eviction, sendingMO, setSeen, if pushRegistration is Disabled<commit_after>//
//  MMMessageHandler.swift
//  MobileMessaging
//
//  Created by <NAME>. on 24/02/16.
//  
//

import Foundation
import CoreData

protocol MobileMessagingService {
	var isRunning: Bool { get }
	func start(_ completion: ((Bool) -> Void)?)
	func stop(_ completion: ((Bool) -> Void)?)
}

final class MMMessageHandler: MobileMessagingService {
	var isRunning: Bool = true
	func start(_ completion: ((Bool) -> Void)? = nil) {
		isRunning = true
		completion?(true)
	}
	func stop(_ completion: ((Bool) -> Void)? = nil) {
		isRunning = false
		completion?(true)
	}
	
	lazy var messageHandlingQueue = OperationQueue.mm_newSerialQueue
	lazy var messageSendingQueue = OperationQueue()
	lazy var seenPostponer = MMPostponer(executionQueue: DispatchQueue.main)
	
	deinit {
		messageHandlingQueue.cancelAllOperations()
	}
	
	let storage: MMCoreDataStorage
    
	init(storage: MMCoreDataStorage) {
		self.storage = storage
        self.evictOldMessages()
    }

    //MARK: Intenal
	func handleAPNSMessage(_ userInfo: APNSPayload, applicationState: UIApplicationState, newMessageReceivedCallback: ((APNSPayload) -> Void)? = nil, completion: ((NSError?) -> Void)? = nil) {
		guard isRunning == true else {
			completion?(nil)
			return
		}
		if let msg = MMMessageFactory.makeMessage(with: userInfo, createdDate: Date()) {
			messageHandlingQueue.addOperation(MessageHandlingOperation(messagesToHandle: [msg], messagesDeliveryMethod: .push, context: storage.newPrivateContext(), messageHandler: MobileMessaging.messageHandling, applicationState: applicationState, finishBlock: completion))
		} else {
			MMLogError("Error while converting payload:\n\(userInfo)\nto MMMessage")
			completion?(NSError.init(type: .UnknownError))
		}
	}
	
	func syncWithServer(_ completion: ((NSError?) -> Void)? = nil) {
		guard isRunning == true else {
			completion?(nil)
			return
		}
		messageHandlingQueue.addOperation(MessagesSyncOperation(context: storage.newPrivateContext(), finishBlock: completion))
	}
	
	func evictOldMessages(_ messageAge: TimeInterval? = nil, completion:((Void) -> Void)? = nil) {
		messageHandlingQueue.addOperation(MessagesEvictionOperation(context: storage.newPrivateContext(), messageMaximumAge: messageAge, finishBlock: completion))
    }
	
    func setSeen(_ messageIds: [String], completion: ((SeenStatusSendingResult) -> Void)? = nil) {
		messageHandlingQueue.addOperation(SeenStatusPersistingOperation(messageIds: messageIds, context: storage.newPrivateContext()))
		seenPostponer.postponeBlock() {
			self.messageHandlingQueue.addOperation(SeenStatusSendingOperation(context: self.storage.newPrivateContext(), finishBlock: completion))
		}
    }
	
	func sendMessages(_ messages: [MOMessage], completion: (([MOMessage]?, NSError?) -> Void)? = nil) {
		messageSendingQueue.addOperation(MessagePostingOperation(messages: messages, context: storage.newPrivateContext(), finishBlock: { (result: MOMessageSendingResult) in
			
			completion?(result.value?.messages, result.error)
		}))
	}
}
{'fraction_non_alphanumeric': 0.11803588290840415, 'fraction_numerical': 0.0018885741265344666, 'mean_word_length': 5.2549212598425195, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '2357907', 'n_tokens_mistral': 2051, 'n_tokens_neox': 1854, 'n_words': 413}