From 1cb0f1ae562a0d3ed0d42c1ac31126aa5eaf055e Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 22 Dec 2022 14:41:56 -0500 Subject: [PATCH] Fix non-mention notifications showing in Mentions tab on Pleroma e0d97cd2a8 introduced a regression on Pleroma, because specifying the allowed types of notifications in the Masto API was only added in 3.5 --- .../Pachyderm/Sources/Pachyderm/Client.swift | 8 ++++++++ Tusker/API/InstanceFeatures.swift | 4 ++++ .../NotificationsTableViewController.swift | 19 +++++++++++++------ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Packages/Pachyderm/Sources/Pachyderm/Client.swift b/Packages/Pachyderm/Sources/Pachyderm/Client.swift index dcebb6e8..9621337d 100644 --- a/Packages/Pachyderm/Sources/Pachyderm/Client.swift +++ b/Packages/Pachyderm/Sources/Pachyderm/Client.swift @@ -306,6 +306,14 @@ public class Client { return request } + public static func getNotifications(excludedTypes: [Notification.Kind], range: RequestRange = .default) -> Request<[Notification]> { + var request = Request<[Notification]>(method: .get, path: "/api/v1/notifications", queryParameters: + "exclude_types" => excludedTypes.map { $0.rawValue } + ) + request.range = range + return request + } + public static func clearNotifications() -> Request { return Request(method: .post, path: "/api/v1/notifications/clear") } diff --git a/Tusker/API/InstanceFeatures.swift b/Tusker/API/InstanceFeatures.swift index 1eec3da5..907c52c8 100644 --- a/Tusker/API/InstanceFeatures.swift +++ b/Tusker/API/InstanceFeatures.swift @@ -93,6 +93,10 @@ struct InstanceFeatures { hasMastodonVersion(4, 0, 0) } + var notificationsAllowedTypes: Bool { + hasMastodonVersion(3, 5, 0) + } + mutating func update(instance: Instance, nodeInfo: NodeInfo?) { let ver = instance.version.lowercased() if ver.contains("glitch") { diff --git a/Tusker/Screens/Notifications/NotificationsTableViewController.swift b/Tusker/Screens/Notifications/NotificationsTableViewController.swift index 5192bd9f..3c35af59 100644 --- a/Tusker/Screens/Notifications/NotificationsTableViewController.swift +++ b/Tusker/Screens/Notifications/NotificationsTableViewController.swift @@ -57,6 +57,16 @@ class NotificationsTableViewController: DiffableTimelineLikeTableViewController< tableView.register(UINib(nibName: "BasicTableViewCell", bundle: .main), forCellReuseIdentifier: unknownCell) } + private func request(range: RequestRange) -> Request<[Pachyderm.Notification]> { + if mastodonController.instanceFeatures.notificationsAllowedTypes { + return Client.getNotifications(allowedTypes: allowedTypes, range: range) + } else { + var types = Set(Notification.Kind.allCases) + allowedTypes.forEach { types.remove($0) } + return Client.getNotifications(excludedTypes: Array(types), range: range) + } + } + // MARK: - DiffableTimelineLikeTableViewController override func cellProvider(_ tableView: UITableView, _ indexPath: IndexPath, _ item: Item) -> UITableViewCell? { @@ -140,8 +150,7 @@ class NotificationsTableViewController: DiffableTimelineLikeTableViewController< } override func loadInitialItems(completion: @escaping (LoadResult) -> Void) { - let request = Client.getNotifications(allowedTypes: allowedTypes) - mastodonController.run(request) { (response) in + mastodonController.run(request(range: .default)) { (response) in switch response { case let .failure(error): completion(.failure(.client(error))) @@ -171,8 +180,7 @@ class NotificationsTableViewController: DiffableTimelineLikeTableViewController< return } - let request = Client.getNotifications(allowedTypes: allowedTypes, range: older) - mastodonController.run(request) { (response) in + mastodonController.run(request(range: older)) { (response) in switch response { case let .failure(error): completion(.failure(.client(error))) @@ -203,8 +211,7 @@ class NotificationsTableViewController: DiffableTimelineLikeTableViewController< return } - let request = Client.getNotifications(allowedTypes: allowedTypes, range: newer) - mastodonController.run(request) { (response) in + mastodonController.run(request(range: newer)) { (response) in switch response { case let .failure(error): completion(.failure(.client(error)))