71 lines
1.7 KiB
Swift
71 lines
1.7 KiB
Swift
|
//
|
||
|
// Filter.swift
|
||
|
// Pachyderm
|
||
|
//
|
||
|
// Created by Shadowfacts on 9/9/18.
|
||
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import Foundation
|
||
|
|
||
|
public class Filter: Decodable, ClientModel {
|
||
|
var client: Client!
|
||
|
|
||
|
public let id: String
|
||
|
public var phrase: String
|
||
|
private var context: [String]
|
||
|
public var expiresAt: Date?
|
||
|
public var irreversible: Bool
|
||
|
public var wholeWord: Bool
|
||
|
|
||
|
public var contexts: [Context] {
|
||
|
get {
|
||
|
return context.compactMap(Context.init)
|
||
|
}
|
||
|
set {
|
||
|
context = contexts.contextStrings
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public func update(completion: Client.Callback<Filter>?) {
|
||
|
let request = Request<Filter>(method: .put, path: "/api/v1/filters/\(id)", body: .parameters([
|
||
|
"phrase" => phrase,
|
||
|
"irreversible" => irreversible,
|
||
|
"whole_word" => wholeWord,
|
||
|
"expires_at" => expiresAt
|
||
|
] + "context" => context))
|
||
|
client.run(request) { result in
|
||
|
completion?(result)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public func delete(completion: @escaping Client.Callback<Empty>) {
|
||
|
let request = Request<Empty>(method: .delete, path: "/api/v1/filters/\(id)")
|
||
|
client.run(request, completion: completion)
|
||
|
}
|
||
|
|
||
|
private enum CodingKeys: String, CodingKey {
|
||
|
case id
|
||
|
case phrase
|
||
|
case context
|
||
|
case expiresAt = "expires_at"
|
||
|
case irreversible
|
||
|
case wholeWord = "whole_word"
|
||
|
}
|
||
|
}
|
||
|
|
||
|
extension Filter {
|
||
|
public enum Context: String, Decodable {
|
||
|
case home
|
||
|
case notifications
|
||
|
case `public`
|
||
|
case thread
|
||
|
}
|
||
|
}
|
||
|
|
||
|
extension Array where Element == Filter.Context {
|
||
|
var contextStrings: [String] {
|
||
|
return map { $0.rawValue }
|
||
|
}
|
||
|
}
|