Tusker/Pachyderm/Sources/Pachyderm/Model/Mention.swift

45 lines
1.4 KiB
Swift

//
// Mention.swift
// Pachyderm
//
// Created by Shadowfacts on 9/9/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import Foundation
import WebURL
import WebURLFoundationExtras
public class Mention: Codable {
public let url: URL
public let username: String
public let acct: String
/// The instance-local ID of the user being mentioned.
public let id: String
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.username = try container.decode(String.self, forKey: .username)
self.acct = try container.decode(String.self, forKey: .acct)
self.id = try container.decode(String.self, forKey: .id)
do {
let webURL = try container.decode(WebURL.self, forKey: .url)
if let url = URL(webURL) {
self.url = url
} else {
let s = try? container.decode(String.self, forKey: .url)
throw DecodingError.dataCorruptedError(forKey: .url, in: container, debugDescription: "unable to convert WebURL \(s?.debugDescription ?? "nil") to URL")
}
} catch {
self.url = try container.decode(URL.self, forKey: .url)
}
}
private enum CodingKeys: String, CodingKey {
case url
case username
case acct
case id
}
}