2018-09-11 14:52:21 +00:00
|
|
|
//
|
|
|
|
// Filter.swift
|
|
|
|
// Pachyderm
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 9/9/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2018-09-17 23:22:37 +00:00
|
|
|
public class Filter: Decodable {
|
2018-09-11 14:52:21 +00:00
|
|
|
public let id: String
|
2018-09-17 23:22:37 +00:00
|
|
|
public let phrase: String
|
|
|
|
private let context: [String]
|
|
|
|
public let expiresAt: Date?
|
|
|
|
public let irreversible: Bool
|
|
|
|
public let wholeWord: Bool
|
2018-09-11 14:52:21 +00:00
|
|
|
|
|
|
|
public var contexts: [Context] {
|
|
|
|
get {
|
|
|
|
return context.compactMap(Context.init)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 23:22:37 +00:00
|
|
|
public static func update(_ filter: Filter, phrase: String? = nil, context: [Context]? = nil, irreversible: Bool? = nil, wholeWord: Bool? = nil, expiresAt: Date? = nil) -> Request<Filter> {
|
2020-09-15 03:25:26 +00:00
|
|
|
return Request<Filter>(method: .put, path: "/api/v1/filters/\(filter.id)", body: ParametersBody([
|
2018-09-17 23:22:37 +00:00
|
|
|
"phrase" => (phrase ?? filter.phrase),
|
|
|
|
"irreversible" => (irreversible ?? filter.irreversible),
|
|
|
|
"whole_word" => (wholeWord ?? filter.wholeWord),
|
|
|
|
"expires_at" => (expiresAt ?? filter.expiresAt)
|
|
|
|
] + "context" => (context?.contextStrings ?? filter.context)))
|
2018-09-11 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 23:22:37 +00:00
|
|
|
public static func delete(_ filter: Filter) -> Request<Empty> {
|
|
|
|
return Request<Empty>(method: .delete, path: "/api/v1/filters/\(filter.id)")
|
2018-09-11 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 }
|
|
|
|
}
|
|
|
|
}
|