Add twitter URL creation logic

This commit is contained in:
Maurice Parker 2020-04-22 11:25:49 -05:00
parent c37bbe2fbb
commit 0369d976fa
2 changed files with 77 additions and 8 deletions

View File

@ -11,10 +11,25 @@ import Secrets
import OAuthSwift
import RSParser
// TODO: Beef up error handling...
public enum TwitterFeedProviderError: Error {
public enum TwitterFeedProviderError: LocalizedError {
case screenNameNotFound
case unknown
public var localizedDescription: String {
switch self {
case .screenNameNotFound:
return NSLocalizedString("Unable to determine screen name.", comment: "Screen name")
case .unknown:
return NSLocalizedString("An unknown Twitter Feed Provider error has occurred.", comment: "Screen name")
}
}
}
public enum TwitterFeedType: Int {
case homeTimeline = 0
case mentions = 1
case screenName = 2
case search = 3
}
public struct TwitterFeedProvider: FeedProvider {
@ -108,7 +123,7 @@ public struct TwitterFeedProvider: FeedProvider {
public func assignName(_ urlComponents: URLComponents, completion: @escaping (Result<String, Error>) -> Void) {
switch urlComponents.path {
case "/", "/home":
case "", "/", "/home":
let name = NSLocalizedString("Twitter Timeline", comment: "Twitter Timeline")
completion(.success(name))
@ -160,7 +175,7 @@ public struct TwitterFeedProvider: FeedProvider {
var isSearch = false
switch urlComponents.path {
case "/", "/home":
case "", "/", "/home":
parameters["count"] = 100
api = "statuses/home_timeline.json"
case "/notifications/mentions":
@ -199,6 +214,39 @@ public struct TwitterFeedProvider: FeedProvider {
}
}
public static func buildURL(_ type: TwitterFeedType, username: String?, screenName: String?, searchField: String?) -> URL? {
var components = URLComponents()
components.scheme = "https"
components.host = "twitter.com"
switch type {
case .homeTimeline:
guard let username = username else {
return nil
}
components.user = username
case .mentions:
guard let username = username else {
return nil
}
components.user = username
components.path = "/notifications/mentions"
case .screenName:
guard let screenName = screenName else {
return nil
}
components.path = "/\(screenName)"
case .search:
guard let searchField = searchField else {
return nil
}
components.path = "/search"
components.queryItems = [URLQueryItem(name: "q", value: searchField)]
}
return components.url
}
}
// MARK: OAuth1SwiftProvider

View File

@ -13,7 +13,6 @@ import Articles
import Account
class AddTwitterFeedWindowController : NSWindowController, AddFeedWindowController {
@IBOutlet weak var typePopupButton: NSPopUpButton!
@IBOutlet weak var typeDescriptionLabel: NSTextField!
@ -33,6 +32,15 @@ class AddTwitterFeedWindowController : NSWindowController, AddFeedWindowControll
private weak var delegate: AddFeedWindowControllerDelegate?
private var folderTreeController: TreeController!
private var userEnteredScreenSearch: String? {
var s = screenSearchTextField.stringValue
s = s.collapsingWhitespace
if s.isEmpty {
return nil
}
return s
}
private var userEnteredTitle: String? {
var s = nameTextField.stringValue
s = s.collapsingWhitespace
@ -57,6 +65,16 @@ class AddTwitterFeedWindowController : NSWindowController, AddFeedWindowControll
override func windowDidLoad() {
let accountMenu = NSMenu()
for feedProvider in ExtensionPointManager.shared.activeFeedProviders {
if let twitterFeedProvider = feedProvider as? TwitterFeedProvider {
let accountMenuItem = NSMenuItem()
accountMenuItem.title = "@\(twitterFeedProvider.screenName)"
accountMenu.addItem(accountMenuItem)
}
}
accountPopupButton.menu = accountMenu
folderPopupButton.menu = FolderTreeMenu.createFolderPopupMenu(with: folderTreeController.rootNode)
if let container = AddWebFeedDefaultContainer.defaultContainer {
@ -84,9 +102,12 @@ class AddTwitterFeedWindowController : NSWindowController, AddFeedWindowControll
}
@IBAction func addFeed(_ sender: Any?) {
// TODO: Build the URL...
let url = URL(string: "https://twitter.com")!
guard let type = TwitterFeedType(rawValue: typePopupButton.selectedItem?.tag ?? 0),
let atUsername = accountPopupButton.selectedItem?.title else { return }
let username = String(atUsername[atUsername.index(atUsername.startIndex, offsetBy: 1)..<atUsername.endIndex])
guard let url = TwitterFeedProvider.buildURL(type, username: username, screenName: userEnteredScreenSearch, searchField: userEnteredScreenSearch) else { return }
let container = selectedContainer()!
AddWebFeedDefaultContainer.saveDefaultContainer(container)