63 lines
1.8 KiB
Swift
63 lines
1.8 KiB
Swift
//
|
|
// Filter.swift
|
|
// Pachyderm
|
|
//
|
|
// Created by Shadowfacts on 9/9/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public struct Filter: FilterProtocol, Decodable {
|
|
public let id: String
|
|
public let phrase: String
|
|
private let context: [String]
|
|
public let expiresAt: Date?
|
|
public let irreversible: Bool
|
|
public let wholeWord: Bool
|
|
|
|
public var contexts: [Context] {
|
|
get {
|
|
return context.compactMap(Context.init)
|
|
}
|
|
}
|
|
|
|
public static func update(_ filter: some FilterProtocol, phrase: String? = nil, context: [Context]? = nil, irreversible: Bool? = nil, wholeWord: Bool? = nil, expiresIn: TimeInterval? = nil) -> Request<Filter> {
|
|
return Request<Filter>(method: .put, path: "/api/v1/filters/\(filter.id)", body: ParametersBody([
|
|
"phrase" => (phrase ?? filter.phrase),
|
|
"irreversible" => (irreversible ?? filter.irreversible),
|
|
"whole_word" => (wholeWord ?? filter.wholeWord),
|
|
"expires_in" => (expiresIn ?? filter.expiresAt?.timeIntervalSinceNow),
|
|
] + "context" => (context ?? filter.contexts).contextStrings))
|
|
}
|
|
|
|
public static func delete(_ filterID: String) -> Request<Empty> {
|
|
return Request<Empty>(method: .delete, path: "/api/v1/filters/\(filterID)")
|
|
}
|
|
|
|
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, CaseIterable {
|
|
case home
|
|
case notifications
|
|
case `public`
|
|
case thread
|
|
case account
|
|
}
|
|
}
|
|
|
|
extension Array where Element == Filter.Context {
|
|
var contextStrings: [String] {
|
|
return map { $0.rawValue }
|
|
}
|
|
}
|