Created
October 7, 2017 21:36
-
-
Save lucianboboc/b3998bea9b316e04f5c57a7711dd2f8a to your computer and use it in GitHub Desktop.
Message Filter Extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class MessageFilterExtension: ILMessageFilterExtension { | |
var words:[Item] = [] | |
let stack = CoreDataStack() | |
func loadItems() { | |
let context = stack.persistentContainer.viewContext | |
let itemDAO = ItemDAO(managedObjectContext: context) | |
let allItems = itemDAO.fetchItmes() | |
self.words = allItems.flatMap({ item in | |
return item.value != nil ? item : nil | |
}) | |
} | |
} | |
extension MessageFilterExtension: ILMessageFilterQueryHandling { | |
func handle(_ queryRequest: ILMessageFilterQueryRequest, context: ILMessageFilterExtensionContext, completion: @escaping (ILMessageFilterQueryResponse) -> Void) { | |
let action = self.offlineAction(for: queryRequest) | |
let response = ILMessageFilterQueryResponse() | |
response.action = action | |
completion(response) | |
} | |
private func offlineAction(for queryRequest: ILMessageFilterQueryRequest) -> ILMessageFilterAction { | |
guard let messageBody = queryRequest.messageBody?.lowercased() else { return .none } | |
self.loadItems() | |
for word in self.words { | |
if let value = word.value, | |
messageBody.contains(value.lowercased()) { | |
return .filter | |
} | |
} | |
return .allow | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment