Guard against invalid URLs

This commit is contained in:
Justin Mazzocchi 2021-01-31 13:59:26 -08:00
parent 59d3c78c31
commit 51529a7ebf
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
8 changed files with 20 additions and 14 deletions

View file

@ -15,7 +15,7 @@ struct AccountRecord: ContentDatabaseRecord, Hashable {
let followingCount: Int let followingCount: Int
let statusesCount: Int let statusesCount: Int
let note: HTML let note: HTML
let url: URL let url: String
let avatar: URL let avatar: URL
let avatarStatic: URL let avatarStatic: URL
let header: URL let header: URL

View file

@ -21,7 +21,7 @@ struct StatusRecord: ContentDatabaseRecord, Hashable {
let favouritesCount: Int let favouritesCount: Int
let repliesCount: Int let repliesCount: Int
let application: Application? let application: Application?
let url: URL? let url: String?
let inReplyToId: Status.Id? let inReplyToId: Status.Id?
let inReplyToAccountId: Account.Id? let inReplyToAccountId: Account.Id?
let reblogId: Status.Id? let reblogId: Status.Id?

View file

@ -33,7 +33,7 @@ public extension Identity {
public let identityId: Identity.Id public let identityId: Identity.Id
public let username: String public let username: String
public let displayName: String public let displayName: String
public let url: URL public let url: String
public let avatar: URL public let avatar: URL
public let avatarStatic: URL public let avatarStatic: URL
public let header: URL public let header: URL
@ -52,8 +52,8 @@ public extension Identity {
} }
var handle: String { var handle: String {
if let account = account, let host = account.url.host { if let urlString = account?.url, let url = URL(string: urlString), let host = url.host {
return account.url.lastPathComponent.appending("@").appending(host) return url.lastPathComponent.appending("@").appending(host)
} }
return instance?.title ?? url.host ?? url.absoluteString return instance?.title ?? url.host ?? url.absoluteString

View file

@ -13,7 +13,7 @@ public final class Account: Codable, Identifiable {
public let followingCount: Int public let followingCount: Int
public let statusesCount: Int public let statusesCount: Int
public let note: HTML public let note: HTML
public let url: URL public let url: String
public let avatar: URL public let avatar: URL
public let avatarStatic: URL public let avatarStatic: URL
public let header: URL public let header: URL
@ -35,7 +35,7 @@ public final class Account: Codable, Identifiable {
followingCount: Int, followingCount: Int,
statusesCount: Int, statusesCount: Int,
note: HTML, note: HTML,
url: URL, url: String,
avatar: URL, avatar: URL,
avatarStatic: URL, avatarStatic: URL,
header: URL, header: URL,

View file

@ -29,7 +29,7 @@ public final class Status: Codable, Identifiable {
public let favouritesCount: Int public let favouritesCount: Int
@DecodableDefault.Zero public private(set) var repliesCount: Int @DecodableDefault.Zero public private(set) var repliesCount: Int
public let application: Application? public let application: Application?
public let url: URL? public let url: String?
public let inReplyToId: Status.Id? public let inReplyToId: Status.Id?
public let inReplyToAccountId: Account.Id? public let inReplyToAccountId: Account.Id?
public let reblog: Status? public let reblog: Status?
@ -60,7 +60,7 @@ public final class Status: Codable, Identifiable {
favouritesCount: Int, favouritesCount: Int,
repliesCount: Int, repliesCount: Int,
application: Application?, application: Application?,
url: URL?, url: String?,
inReplyToId: Status.Id?, inReplyToId: Status.Id?,
inReplyToAccountId: Account.Id?, inReplyToAccountId: Account.Id?,
reblog: Status?, reblog: Status?,

View file

@ -34,10 +34,10 @@ public struct AccountService {
public extension AccountService { public extension AccountService {
var isLocal: Bool { var isLocal: Bool {
account.url.host == mastodonAPIClient.instanceURL.host URL(string: account.url)?.host == mastodonAPIClient.instanceURL.host
} }
var domain: String? { account.url.host } var domain: String? { URL(string: account.url)?.host }
func follow() -> AnyPublisher<Never, Error> { func follow() -> AnyPublisher<Never, Error> {
relationshipAction(.accountsFollow(id: account.id)) relationshipAction(.accountsFollow(id: account.id))

View file

@ -38,7 +38,7 @@ public extension ReportViewModel {
var accountName: String { "@".appending(accountService.account.acct) } var accountName: String { "@".appending(accountService.account.acct) }
var accountHost: String { var accountHost: String {
accountService.account.url.host ?? "" URL(string: accountService.account.url)?.host ?? ""
} }
var isLocalAccount: Bool { accountService.isLocal } var isLocalAccount: Bool { accountService.isLocal }

View file

@ -121,7 +121,11 @@ public extension StatusViewModel {
var muted: Bool { statusService.status.displayStatus.muted } var muted: Bool { statusService.status.displayStatus.muted }
var sharingURL: URL? { statusService.status.displayStatus.url } var sharingURL: URL? {
guard let urlString = statusService.status.displayStatus.url else { return nil }
return URL(string: urlString)
}
var isPollExpired: Bool { statusService.status.displayStatus.poll?.expired ?? true } var isPollExpired: Bool { statusService.status.displayStatus.poll?.expired ?? true }
@ -297,7 +301,9 @@ public extension StatusViewModel {
} }
func shareStatus() { func shareStatus() {
guard let url = statusService.status.displayStatus.url else { return } guard let urlString = statusService.status.displayStatus.url,
let url = URL(string: urlString)
else { return }
eventsSubject.send(Just(.share(url)).setFailureType(to: Error.self).eraseToAnyPublisher()) eventsSubject.send(Just(.share(url)).setFailureType(to: Error.self).eraseToAnyPublisher())
} }