Skip to content

Instantly share code, notes, and snippets.

@lucianboboc
Created October 7, 2017 21:36
Show Gist options
  • Save lucianboboc/b3998bea9b316e04f5c57a7711dd2f8a to your computer and use it in GitHub Desktop.
Save lucianboboc/b3998bea9b316e04f5c57a7711dd2f8a to your computer and use it in GitHub Desktop.
Message Filter Extension
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