2022-01-24 04:26:42 +00:00
|
|
|
//
|
|
|
|
// InstanceFeatures.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 1/23/22.
|
|
|
|
// Copyright © 2022 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Pachyderm
|
|
|
|
|
|
|
|
struct InstanceFeatures {
|
2022-01-25 03:49:18 +00:00
|
|
|
private(set) var instanceType = InstanceType.mastodon
|
2022-04-02 17:18:14 +00:00
|
|
|
private(set) var version: Version?
|
2022-01-24 04:26:42 +00:00
|
|
|
private(set) var maxStatusChars = 500
|
2022-01-25 03:49:18 +00:00
|
|
|
|
|
|
|
var localOnlyPosts: Bool {
|
|
|
|
instanceType == .hometown || instanceType == .glitch
|
|
|
|
}
|
|
|
|
|
|
|
|
var mastodonAttachmentRestrictions: Bool {
|
|
|
|
instanceType.isMastodon
|
|
|
|
}
|
|
|
|
|
|
|
|
var pollsAndAttachments: Bool {
|
|
|
|
instanceType == .pleroma
|
|
|
|
}
|
|
|
|
|
|
|
|
var boostToOriginalAudience: Bool {
|
|
|
|
instanceType == .pleroma
|
|
|
|
}
|
2022-01-24 04:26:42 +00:00
|
|
|
|
2022-04-01 22:51:56 +00:00
|
|
|
var profilePinnedStatuses: Bool {
|
|
|
|
instanceType != .pixelfed
|
|
|
|
}
|
|
|
|
|
2022-04-02 17:18:14 +00:00
|
|
|
var trendingStatusesAndLinks: Bool {
|
|
|
|
instanceType == .mastodon && version != nil && version! >= Version(3, 5, 0)
|
|
|
|
}
|
|
|
|
|
2022-01-24 04:26:42 +00:00
|
|
|
mutating func update(instance: Instance, nodeInfo: NodeInfo?) {
|
2022-01-26 02:23:48 +00:00
|
|
|
let ver = instance.version.lowercased()
|
|
|
|
if ver.contains("glitch") {
|
2022-01-25 03:49:18 +00:00
|
|
|
instanceType = .glitch
|
|
|
|
} else if nodeInfo?.software.name == "hometown" {
|
|
|
|
instanceType = .hometown
|
2022-01-26 02:23:48 +00:00
|
|
|
} else if ver.contains("pleroma") {
|
2022-01-25 03:49:18 +00:00
|
|
|
instanceType = .pleroma
|
2022-04-01 22:51:56 +00:00
|
|
|
} else if ver.contains("pixelfed") {
|
|
|
|
instanceType = .pixelfed
|
2022-01-25 03:49:18 +00:00
|
|
|
} else {
|
|
|
|
instanceType = .mastodon
|
|
|
|
}
|
|
|
|
|
2022-04-02 17:18:14 +00:00
|
|
|
version = Version(string: ver)
|
|
|
|
|
2022-01-24 04:26:42 +00:00
|
|
|
maxStatusChars = instance.maxStatusCharacters ?? 500
|
2022-01-25 03:49:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension InstanceFeatures {
|
|
|
|
enum InstanceType: Equatable {
|
|
|
|
case mastodon // vanilla
|
|
|
|
case pleroma
|
|
|
|
case hometown
|
|
|
|
case glitch
|
2022-04-01 22:51:56 +00:00
|
|
|
case pixelfed
|
2022-01-25 03:49:18 +00:00
|
|
|
|
|
|
|
var isMastodon: Bool {
|
|
|
|
switch self {
|
|
|
|
case .mastodon, .hometown, .glitch:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2022-01-24 04:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 17:18:14 +00:00
|
|
|
|
|
|
|
extension InstanceFeatures {
|
|
|
|
struct Version: Equatable, Comparable {
|
|
|
|
let major: Int
|
|
|
|
let minor: Int
|
|
|
|
let patch: Int
|
|
|
|
|
|
|
|
init(_ major: Int, _ minor: Int, _ patch: Int) {
|
|
|
|
self.major = major
|
|
|
|
self.minor = minor
|
|
|
|
self.patch = patch
|
|
|
|
}
|
|
|
|
|
|
|
|
init?(string: String) {
|
|
|
|
let regex = try! NSRegularExpression(pattern: "^(\\d+)\\.(\\d+)\\.(\\d+).*$")
|
|
|
|
guard let match = regex.firstMatch(in: string, range: NSRange(location: 0, length: string.utf16.count)),
|
|
|
|
match.numberOfRanges == 4 else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
let majorStr = (string as NSString).substring(with: match.range(at: 1))
|
|
|
|
let minorStr = (string as NSString).substring(with: match.range(at: 2))
|
|
|
|
let patchStr = (string as NSString).substring(with: match.range(at: 3))
|
|
|
|
guard let major = Int(majorStr),
|
|
|
|
let minor = Int(minorStr),
|
|
|
|
let patch = Int(patchStr) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
self.major = major
|
|
|
|
self.minor = minor
|
|
|
|
self.patch = patch
|
|
|
|
}
|
|
|
|
|
|
|
|
static func ==(lhs: Version, rhs: Version) -> Bool {
|
|
|
|
return lhs.major == rhs.major && lhs.minor == rhs.minor && lhs.patch == rhs.patch
|
|
|
|
}
|
|
|
|
|
|
|
|
static func < (lhs: InstanceFeatures.Version, rhs: InstanceFeatures.Version) -> Bool {
|
|
|
|
if lhs.major < rhs.major {
|
|
|
|
return true
|
|
|
|
} else if lhs.major > rhs.major {
|
|
|
|
return false
|
|
|
|
} else if lhs.minor < rhs.minor {
|
|
|
|
return true
|
|
|
|
} else if lhs.minor > rhs.minor {
|
|
|
|
return false
|
|
|
|
} else if lhs.patch < rhs.patch {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|