Tusker/Packages/Pachyderm/Sources/Pachyderm/Model/Notification.swift

70 lines
2.1 KiB
Swift

//
// Notification.swift
// Pachyderm
//
// Created by Shadowfacts on 9/9/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import Foundation
import WebURL
public struct Notification: Decodable, Sendable {
public let id: String
public let kind: Kind
public let createdAt: Date
public let account: Account
public let status: Status?
// Only present for pleroma emoji reactions
// Either an emoji or :shortcode: (for akkoma custom emoji reactions)
public let emoji: String?
public let emojiURL: WebURL?
public 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 {
self.kind = .unknown
}
self.createdAt = try container.decode(Date.self, forKey: .createdAt)
self.account = try container.decode(Account.self, forKey: .account)
self.status = try container.decodeIfPresent(Status.self, forKey: .status)
self.emoji = try container.decodeIfPresent(String.self, forKey: .emoji)
self.emojiURL = try container.decodeIfPresent(WebURL.self, forKey: .emojiURL)
}
public static func dismiss(id notificationID: String) -> Request<Empty> {
return Request<Empty>(method: .post, path: "/api/v1/notifications/\(notificationID)/dismiss")
}
private enum CodingKeys: String, CodingKey {
case id
case kind = "type"
case createdAt = "created_at"
case account
case status
case emoji
case emojiURL = "emoji_url"
}
}
extension Notification {
public enum Kind: String, Decodable, CaseIterable, Sendable {
case mention
case reblog
case favourite
case follow
case followRequest = "follow_request"
case poll
case update
case status
case emojiReaction = "pleroma:emoji_reaction"
case unknown
}
}
extension Notification: Identifiable {}