mastodon-ios/Mastodon/Service/APIService/APIService+Recommend.swift

86 lines
4.0 KiB
Swift
Raw Normal View History

2021-03-31 13:29:54 +02:00
//
// APIService+Recommend.swift
// Mastodon
//
// Created by sxiaojian on 2021/3/31.
//
import Combine
2021-03-31 13:29:54 +02:00
import Foundation
import MastodonSDK
import CoreData
import CoreDataStack
import OSLog
2021-03-31 13:29:54 +02:00
extension APIService {
2021-04-21 08:46:31 +02:00
func suggestionAccount(
domain: String,
query: Mastodon.API.Suggestions.Query?,
mastodonAuthenticationBox: AuthenticationService.MastodonAuthenticationBox
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Account]>, Error> {
let authorization = mastodonAuthenticationBox.userAuthorization
return Mastodon.API.Suggestions.get(session: session, domain: domain, query: query, authorization: authorization)
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Account]>, Error> in
let log = OSLog.api
return self.backgroundManagedObjectContext.performChanges {
response.value.forEach { user in
let (mastodonUser,isCreated) = APIService.CoreData.createOrMergeMastodonUser(into: self.backgroundManagedObjectContext, for: nil, in: domain, entity: user, userCache: nil, networkDate: Date(), log: log)
let flag = isCreated ? "+" : "-"
os_log(.info, log: log, "%{public}s[%{public}ld], %{public}s: fetch mastodon user [%s](%s)%s", (#file as NSString).lastPathComponent, #line, #function, flag, mastodonUser.id, mastodonUser.username)
}
}
.setFailureType(to: Error.self)
.tryMap { result -> Mastodon.Response.Content<[Mastodon.Entity.Account]> in
switch result {
case .success:
return response
case .failure(let error):
throw error
}
}
.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
}
func suggestionAccountV2(
2021-03-31 13:29:54 +02:00
domain: String,
query: Mastodon.API.Suggestions.Query?,
2021-03-31 13:29:54 +02:00
mastodonAuthenticationBox: AuthenticationService.MastodonAuthenticationBox
2021-04-20 09:40:10 +02:00
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.V2.SuggestionAccount]>, Error> {
2021-03-31 13:29:54 +02:00
let authorization = mastodonAuthenticationBox.userAuthorization
2021-04-20 09:40:10 +02:00
return Mastodon.API.V2.Suggestions.get(session: session, domain: domain, query: query, authorization: authorization)
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.V2.SuggestionAccount]>, Error> in
let log = OSLog.api
return self.backgroundManagedObjectContext.performChanges {
2021-04-20 09:40:10 +02:00
response.value.forEach { suggestionAccount in
let user = suggestionAccount.account
let (mastodonUser,isCreated) = APIService.CoreData.createOrMergeMastodonUser(into: self.backgroundManagedObjectContext, for: nil, in: domain, entity: user, userCache: nil, networkDate: Date(), log: log)
let flag = isCreated ? "+" : "-"
os_log(.info, log: log, "%{public}s[%{public}ld], %{public}s: fetch mastodon user [%s](%s)%s", (#file as NSString).lastPathComponent, #line, #function, flag, mastodonUser.id, mastodonUser.username)
}
}
.setFailureType(to: Error.self)
2021-04-20 09:40:10 +02:00
.tryMap { result -> Mastodon.Response.Content<[Mastodon.Entity.V2.SuggestionAccount]> in
switch result {
case .success:
return response
case .failure(let error):
throw error
}
}
.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
2021-03-31 13:29:54 +02:00
}
2021-04-21 08:46:31 +02:00
2021-03-31 13:29:54 +02:00
func recommendTrends(
domain: String,
query: Mastodon.API.Trends.Query?
2021-03-31 13:29:54 +02:00
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Tag]>, Error> {
Mastodon.API.Trends.get(session: session, domain: domain, query: query)
2021-03-31 13:29:54 +02:00
}
}