forked from shadowfacts/Tusker
68 lines
2.0 KiB
Swift
68 lines
2.0 KiB
Swift
//
|
|
// Notification.swift
|
|
// Pachyderm
|
|
//
|
|
// Created by Shadowfacts on 9/9/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public class Notification: Decodable {
|
|
public let id: String
|
|
public let kind: Kind
|
|
public let createdAt: Date
|
|
public let account: Account
|
|
public let status: Status?
|
|
|
|
public required init(from decoder: Decoder) throws {
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
self.id = try container.decode(String.self, forKey: .id)
|
|
if let kind = try? container.decode(Kind.self, forKey: .kind) {
|
|
self.kind = kind
|
|
} else if let s = try? container.decode(String.self, forKey: .kind),
|
|
s == "status" {
|
|
// represent notifications of other people posting as regular mentions for now
|
|
self.kind = .mention
|
|
} else {
|
|
self.kind = .unknown
|
|
}
|
|
self.createdAt = try container.decode(Date.self, forKey: .createdAt)
|
|
self.account = try container.decode(Account.self, forKey: .account)
|
|
if container.contains(.status) {
|
|
self.status = try container.decode(Status.self, forKey: .status)
|
|
} else {
|
|
self.status = nil
|
|
}
|
|
}
|
|
|
|
public static func dismiss(id notificationID: String) -> Request<Empty> {
|
|
return Request<Empty>(method: .post, path: "/api/v1/notifications/dismiss", body: ParametersBody([
|
|
"id" => notificationID
|
|
]))
|
|
}
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case id
|
|
case kind = "type"
|
|
case createdAt = "created_at"
|
|
case account
|
|
case status
|
|
}
|
|
}
|
|
|
|
extension Notification {
|
|
public enum Kind: String, Decodable, CaseIterable {
|
|
case mention
|
|
case reblog
|
|
case favourite
|
|
case follow
|
|
case followRequest = "follow_request"
|
|
case poll
|
|
case unknown
|
|
}
|
|
}
|
|
|
|
extension Notification: Identifiable {}
|