Add username storage on WebFeed

This commit is contained in:
Maurice Parker 2020-04-16 18:19:49 -05:00
parent d43bf5d57a
commit 660cf29305
7 changed files with 45 additions and 22 deletions

View File

@ -25,7 +25,6 @@ public struct TwitterFeedProvider: FeedProvider {
private static let userPaths = ["/home", "/notifications"]
private static let reservedPaths = ["/search", "/explore", "/messages", "/i", "/compose"]
public var userID: String
public var screenName: String
private var oauthToken: String
@ -34,20 +33,18 @@ public struct TwitterFeedProvider: FeedProvider {
private var client: OAuthSwiftClient
public init?(tokenSuccess: OAuthSwift.TokenSuccess) {
guard let userID = tokenSuccess.parameters["user_id"] as? String,
let screenName = tokenSuccess.parameters["screen_name"] as? String else {
guard let screenName = tokenSuccess.parameters["screen_name"] as? String else {
return nil
}
self.userID = userID
self.screenName = screenName
self.oauthToken = tokenSuccess.credential.oauthToken
self.oauthTokenSecret = tokenSuccess.credential.oauthTokenSecret
let tokenCredentials = Credentials(type: .oauthAccessToken, username: userID, secret: oauthToken)
let tokenCredentials = Credentials(type: .oauthAccessToken, username: screenName, secret: oauthToken)
try? CredentialsManager.storeCredentials(tokenCredentials, server: Self.server)
let tokenSecretCredentials = Credentials(type: .oauthAccessTokenSecret, username: userID, secret: oauthTokenSecret)
let tokenSecretCredentials = Credentials(type: .oauthAccessTokenSecret, username: screenName, secret: oauthTokenSecret)
try? CredentialsManager.storeCredentials(tokenSecretCredentials, server: Self.server)
client = OAuthSwiftClient(consumerKey: Secrets.twitterConsumerKey,
@ -57,12 +54,11 @@ public struct TwitterFeedProvider: FeedProvider {
version: .oauth1)
}
public init?(userID: String, screenName: String) {
self.userID = userID
public init?(screenName: String) {
self.screenName = screenName
guard let tokenCredentials = try? CredentialsManager.retrieveCredentials(type: .oauthAccessToken, server: Self.server, username: userID),
let tokenSecretCredentials = try? CredentialsManager.retrieveCredentials(type: .oauthAccessTokenSecret, server: Self.server, username: userID) else {
guard let tokenCredentials = try? CredentialsManager.retrieveCredentials(type: .oauthAccessToken, server: Self.server, username: screenName),
let tokenSecretCredentials = try? CredentialsManager.retrieveCredentials(type: .oauthAccessTokenSecret, server: Self.server, username: screenName) else {
return nil
}
@ -82,7 +78,7 @@ public struct TwitterFeedProvider: FeedProvider {
}
let bestUserName = username != nil ? username : urlComponents.user
if bestUserName == userID {
if bestUserName == screenName {
return .owner
}

View File

@ -122,8 +122,18 @@ final class LocalAccountDelegate: AccountDelegate {
switch result {
case .success(let name):
let feed = account.createWebFeed(with: name, url: url.absoluteString, webFeedID: url.absoluteString, homePageURL: nil)
// Move the user to the WebFeed and out of the URL
var newURLComponents = urlComponents
newURLComponents.user = nil
guard let newURL = newURLComponents.url else {
completion(.failure(AccountError.createErrorNotFound))
return
}
let feed = account.createWebFeed(with: name, url: newURL.absoluteString, webFeedID: newURL.absoluteString, homePageURL: nil)
feed.editedName = name
feed.username = urlComponents.user
feedProvider.refresh(feed) { result in
self.refreshProgress.completeTask()

View File

@ -77,6 +77,15 @@ public final class WebFeed: Feed, Renamable, Hashable {
}
}
public var username: String? {
get {
return metadata.username
}
set {
metadata.username = newValue
}
}
public var name: String?
public var authors: Set<Author>? {

View File

@ -21,6 +21,7 @@ final class WebFeedMetadata: Codable {
case homePageURL
case iconURL
case faviconURL
case username
case editedName
case authors
case contentHash
@ -63,6 +64,14 @@ final class WebFeedMetadata: Codable {
}
}
var username: String? {
didSet {
if username != oldValue {
valueDidChange(.username)
}
}
}
var editedName: String? {
didSet {
if editedName != oldValue {

View File

@ -15,7 +15,7 @@ enum ExtensionPointIdentifer: Hashable {
case marsEdit
case microblog
#endif
case twitter(String, String)
case twitter(String)
var extensionPointType: ExtensionPoint.Type {
switch self {
@ -42,10 +42,9 @@ enum ExtensionPointIdentifer: Hashable {
"type": "microblog"
]
#endif
case .twitter(let userID, let screenName):
case .twitter(let screenName):
return [
"type": "twitter",
"userID": userID,
"screenName": screenName
]
}
@ -62,8 +61,8 @@ enum ExtensionPointIdentifer: Hashable {
self = ExtensionPointIdentifer.microblog
#endif
case "twitter":
guard let userID = userInfo["userID"] as? String, let screenName = userInfo["screenName"] as? String else { return nil }
self = ExtensionPointIdentifer.twitter(userID, screenName)
guard let screenName = userInfo["screenName"] as? String else { return nil }
self = ExtensionPointIdentifer.twitter(screenName)
default:
return nil
}
@ -77,9 +76,8 @@ enum ExtensionPointIdentifer: Hashable {
case .microblog:
hasher.combine("microblog")
#endif
case .twitter(let userID, let screenName):
case .twitter(let screenName):
hasher.combine("twitter")
hasher.combine(userID)
hasher.combine(screenName)
}
}

View File

@ -36,6 +36,7 @@ final class ExtensionPointManager: FeedProviderManagerDelegate {
}
return available
}
var activeSendToCommands: [SendToCommand] {
@ -122,8 +123,8 @@ private extension ExtensionPointManager {
case .microblog:
return SendToMicroBlogCommand()
#endif
case .twitter(let userID, let screenName):
return TwitterFeedProvider(userID: userID, screenName: screenName)
case .twitter(let screenName):
return TwitterFeedProvider(screenName: screenName)
}
}

View File

@ -19,7 +19,7 @@ extension TwitterFeedProvider: ExtensionPoint {
}()
var extensionPointID: ExtensionPointIdentifer {
return ExtensionPointIdentifer.twitter(userID, screenName)
return ExtensionPointIdentifer.twitter(screenName)
}
var title: String {