Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #3246: Parse page metadata correctly to ensure URL is correct
Browse files Browse the repository at this point in the history
  • Loading branch information
kylehickinson committed May 3, 2021
1 parent 7560c1f commit 15ee213
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
17 changes: 12 additions & 5 deletions Client/Frontend/Browser/MetadataParserHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,21 @@ class MetadataParserHelper: TabEventHandler {
}

guard let dict = result as? [String: Any],
let data = try? JSONSerialization.data(withJSONObject: dict, options: []),
let pageMetadata = try? JSONDecoder().decode(PageMetadata.self, from: data) else {
let data = try? JSONSerialization.data(withJSONObject: dict, options: []) else {
log.debug("Page contains no metadata!")
return
}

tab.pageMetadata = pageMetadata
TabEvent.post(.didLoadPageMetadata(pageMetadata), for: tab)

do {
let pageMetadata = try JSONDecoder().decode(PageMetadata.self, from: data)
tab.pageMetadata = pageMetadata
TabEvent.post(.didLoadPageMetadata(pageMetadata), for: tab)
} catch {
log.error("Failed to parse metadata: \(error)")
// To avoid issues where `pageMetadata` points to the last website to successfully
// parse metadata, set to nil
tab.pageMetadata = nil
}
}
}
}
Expand Down
17 changes: 4 additions & 13 deletions Storage/PageMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,10 @@ public struct PageMetadata: Decodable {
public let providerName: String?
public let faviconURL: String?
public let largeIconURL: String?
public let keywordsString: String?
public let keywords: Set<String>?
public let search: Link?
public let feeds: [Link]

public var keywords: Set<String> {
guard let string = keywordsString else {
return Set()
}

let strings = string.split(separator: ",", omittingEmptySubsequences: true).map(String.init)
return Set(strings)
}

enum CodingKeys: String, CodingKey {
case mediaURL = "image"
case siteURL = "url"
Expand All @@ -36,7 +27,7 @@ public struct PageMetadata: Decodable {
case providerName = "provider"
case faviconURL = "icon"
case largeIconURL = "largeIcon"
case keywordsString = "keywords"
case keywords
case search
case feeds
}
Expand All @@ -49,7 +40,7 @@ public struct PageMetadata: Decodable {
providerName: String?,
faviconURL: String? = nil,
largeIconURL: String? = nil,
keywords: String? = nil,
keywords: Set<String>? = nil,
search: Link? = nil,
feeds: [Link] = []) {
self.siteURL = siteURL
Expand All @@ -60,7 +51,7 @@ public struct PageMetadata: Decodable {
self.providerName = providerName
self.faviconURL = faviconURL
self.largeIconURL = largeIconURL
self.keywordsString = keywords
self.keywords = keywords
self.search = search
self.feeds = feeds
}
Expand Down

0 comments on commit 15ee213

Please sign in to comment.