Bubble/Threaded/Data/Instance.swift

102 lines
3.0 KiB
Swift
Raw Normal View History

2023-12-29 11:17:37 +01:00
//Made by Lumaa
import Foundation
public struct Instance: Codable, Sendable {
2024-04-26 20:11:54 +02:00
static let blocklistUrl: URL? = URL(string: "https://codeberg.org/oliphant/blocklists/raw/branch/main/blocklists/_unified_tier0_blocklist.csv")
@MainActor
2024-06-23 16:17:52 +02:00
static func getBlocklist() async -> [String] {
var final: [String] = []
//locate the file you want to use
guard let filelink = Instance.blocklistUrl else {
return []
}
//convert that file into one long string
2024-06-23 16:17:52 +02:00
var string = ""
do {
2024-06-23 16:17:52 +02:00
let data = try await URLSession.shared.data(from: filelink).0
string = String(data: data, encoding: .utf8) ?? ""
} catch {
print(error)
return []
}
//now split that string into an array of "rows" of data. Each row is a string.
2024-06-23 16:17:52 +02:00
var rows = string.components(separatedBy: "\n")
//if you have a header row, remove it here
rows.removeFirst()
//now loop around each row, and split it into each of its columns
for row in rows {
let columns = row.components(separatedBy: ",")
//check that we have enough columns
if columns.count > 0 {
let instanceUrl = columns[0]
final.append(instanceUrl)
}
}
return final
}
2023-12-29 11:17:37 +01:00
public struct Stats: Codable, Sendable {
public let userCount: Int
public let statusCount: Int
public let domainCount: Int
}
public struct Configuration: Codable, Sendable {
public struct Statuses: Codable, Sendable {
public let maxCharacters: Int
public let maxMediaAttachments: Int
}
public struct Polls: Codable, Sendable {
public let maxOptions: Int
public let maxCharactersPerOption: Int
public let minExpiration: Int
public let maxExpiration: Int
}
public let statuses: Statuses
public let polls: Polls
}
public struct Rule: Codable, Identifiable, Sendable {
public let id: String
public let text: String
}
public struct URLs: Codable, Sendable {
public let streamingApi: URL?
}
public let title: String
public let shortDescription: String
public let email: String
public let version: String
public let stats: Stats
public let languages: [String]?
public let registrations: Bool
public let thumbnail: URL?
public let configuration: Configuration?
public let rules: [Rule]?
public let urls: URLs?
}
public struct InstanceApp: Codable, Identifiable {
public let id: String
public let name: String
public let website: URL?
public let redirectUri: String
public let clientId: String
public let clientSecret: String
public let vapidKey: String?
}
extension InstanceApp: Sendable {}