2023-01-03 14:09:22 +01:00
|
|
|
//
|
|
|
|
// https://mczachurski.dev
|
|
|
|
// Copyright © 2023 Marcin Czachurski and the repository contributors.
|
|
|
|
// Licensed under the MIT License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import CoreData
|
2023-02-19 10:32:38 +01:00
|
|
|
import PixelfedKit
|
2023-01-03 14:09:22 +01:00
|
|
|
|
2023-01-28 21:09:31 +01:00
|
|
|
/// Service responsible for managing home timeline.
|
2023-01-21 18:01:17 +01:00
|
|
|
public class HomeTimelineService {
|
|
|
|
public static let shared = HomeTimelineService()
|
2023-01-05 11:55:20 +01:00
|
|
|
private init() { }
|
2023-01-03 14:09:22 +01:00
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
public func loadOnBottom(for account: AccountModel) async throws -> Int {
|
2023-01-03 14:09:22 +01:00
|
|
|
// Load data from API and operate on CoreData on background context.
|
|
|
|
let backgroundContext = CoreDataHandler.shared.newBackgroundContext()
|
|
|
|
|
2023-02-07 10:20:24 +01:00
|
|
|
// Get minimum downloaded stauts id.
|
2023-01-31 12:20:49 +01:00
|
|
|
let oldestStatus = StatusDataHandler.shared.getMinimumStatus(accountId: account.id, viewContext: backgroundContext)
|
2023-01-03 14:09:22 +01:00
|
|
|
|
|
|
|
guard let oldestStatus = oldestStatus else {
|
2023-01-11 13:16:43 +01:00
|
|
|
return 0
|
2023-01-03 14:09:22 +01:00
|
|
|
}
|
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
// Load data on bottom of the list.
|
|
|
|
let newStatuses = try await self.load(for: account, on: backgroundContext, maxId: oldestStatus.id)
|
2023-01-20 13:47:38 +01:00
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
// Save data into database.
|
2023-01-20 13:47:38 +01:00
|
|
|
try backgroundContext.save()
|
2023-01-31 12:20:49 +01:00
|
|
|
|
|
|
|
// Return amount of newly downloaded statuses.
|
2023-01-26 15:10:47 +01:00
|
|
|
return newStatuses.count
|
2023-01-03 14:09:22 +01:00
|
|
|
}
|
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
public func loadOnTop(for account: AccountModel) async throws -> String? {
|
2023-01-03 14:09:22 +01:00
|
|
|
// Load data from API and operate on CoreData on background context.
|
|
|
|
let backgroundContext = CoreDataHandler.shared.newBackgroundContext()
|
|
|
|
|
2023-01-28 21:09:31 +01:00
|
|
|
// Refresh/load home timeline (refreshing on top downloads always first 40 items).
|
|
|
|
// TODO: When Apple introduce good way to show new items without scroll to top then we can change that method.
|
2023-01-31 12:20:49 +01:00
|
|
|
let lastSeenStatusId = try await self.refresh(for: account, on: backgroundContext)
|
2023-01-20 13:47:38 +01:00
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
// Save data into database.
|
2023-01-20 13:47:38 +01:00
|
|
|
try backgroundContext.save()
|
2023-01-31 12:20:49 +01:00
|
|
|
|
|
|
|
// Return id of last seen status.
|
|
|
|
return lastSeenStatusId
|
2023-01-03 14:09:22 +01:00
|
|
|
}
|
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
public func save(lastSeenStatusId: String, for account: AccountModel) async throws {
|
|
|
|
// Load data from API and operate on CoreData on background context.
|
|
|
|
let backgroundContext = CoreDataHandler.shared.newBackgroundContext()
|
|
|
|
|
|
|
|
// Save information about last seen status.
|
|
|
|
guard let accountDataFromDb = AccountDataHandler.shared.getAccountData(accountId: account.id, viewContext: backgroundContext) else {
|
|
|
|
throw DatabaseError.cannotDownloadAccount
|
|
|
|
}
|
|
|
|
|
|
|
|
accountDataFromDb.lastSeenStatusId = lastSeenStatusId
|
|
|
|
try backgroundContext.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
public func update(status statusData: StatusData, basedOn status: Status, for account: AccountModel) async throws -> StatusData? {
|
2023-01-28 21:09:31 +01:00
|
|
|
// Load data from API and operate on CoreData on background context.
|
|
|
|
let backgroundContext = CoreDataHandler.shared.newBackgroundContext()
|
|
|
|
|
|
|
|
// Update status data in database.
|
|
|
|
self.copy(from: status, to: statusData, on: backgroundContext)
|
|
|
|
try backgroundContext.save()
|
|
|
|
|
|
|
|
return statusData
|
|
|
|
}
|
|
|
|
|
2023-02-09 19:24:41 +01:00
|
|
|
public func update(attachment: AttachmentData, withData imageData: Data, imageWidth: Double, imageHeight: Double) {
|
2023-01-28 21:09:31 +01:00
|
|
|
attachment.data = imageData
|
2023-02-09 19:24:41 +01:00
|
|
|
attachment.metaImageWidth = Int32(imageWidth)
|
|
|
|
attachment.metaImageHeight = Int32(imageHeight)
|
2023-01-28 21:09:31 +01:00
|
|
|
self.setExifProperties(in: attachment, from: imageData)
|
|
|
|
|
|
|
|
CoreDataHandler.shared.save()
|
|
|
|
}
|
|
|
|
|
2023-02-07 10:20:24 +01:00
|
|
|
public func amountOfNewStatuses(for account: AccountModel) async -> Int {
|
|
|
|
guard let accessToken = account.accessToken else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load data from API and operate on CoreData on background context.
|
|
|
|
let backgroundContext = CoreDataHandler.shared.newBackgroundContext()
|
|
|
|
|
|
|
|
// Get maximimum downloaded stauts id.
|
|
|
|
let newestStatus = StatusDataHandler.shared.getMaximumStatus(accountId: account.id, viewContext: backgroundContext)
|
|
|
|
guard let newestStatus else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-02-19 10:43:37 +01:00
|
|
|
let client = PixelfedClient(baseURL: account.serverUrl).getAuthenticated(token: accessToken)
|
2023-02-07 10:20:24 +01:00
|
|
|
var amountOfStatuses = 0
|
|
|
|
var newestStatusId = newestStatus.id
|
|
|
|
|
|
|
|
while(true) {
|
|
|
|
do {
|
|
|
|
let downloadedStatuses = try await client.getHomeTimeline(minId: newestStatusId, limit: 40)
|
|
|
|
guard let firstStatus = downloadedStatuses.first else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
amountOfStatuses = amountOfStatuses + downloadedStatuses.count
|
|
|
|
newestStatusId = firstStatus.id
|
|
|
|
} catch {
|
|
|
|
ErrorService.shared.handle(error, message: "Error during downloading new statuses for amountof new statuses.")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return amountOfStatuses
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
private func refresh(for account: AccountModel, on backgroundContext: NSManagedObjectContext) async throws -> String? {
|
|
|
|
guard let accessToken = account.accessToken else {
|
|
|
|
return nil
|
2023-01-20 13:47:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve statuses from API.
|
2023-02-19 10:43:37 +01:00
|
|
|
let client = PixelfedClient(baseURL: account.serverUrl).getAuthenticated(token: accessToken)
|
2023-01-20 13:47:38 +01:00
|
|
|
let statuses = try await client.getHomeTimeline(limit: 40)
|
|
|
|
|
2023-01-28 21:09:31 +01:00
|
|
|
// Retrieve all statuses from database.
|
2023-01-31 12:20:49 +01:00
|
|
|
let dbStatuses = StatusDataHandler.shared.getAllStatuses(accountId: account.id)
|
|
|
|
let lastSeenStatusId = dbStatuses.last?.rebloggedStatusId ?? dbStatuses.last?.id
|
2023-01-20 13:47:38 +01:00
|
|
|
|
2023-01-28 21:09:31 +01:00
|
|
|
// Remove statuses that are not in 40 downloaded once.
|
2023-01-20 13:47:38 +01:00
|
|
|
var dbStatusesToRemove: [StatusData] = []
|
|
|
|
for dbStatus in dbStatuses {
|
|
|
|
if !statuses.contains(where: { status in status.id == dbStatus.id }) {
|
|
|
|
dbStatusesToRemove.append(dbStatus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !dbStatusesToRemove.isEmpty {
|
2023-01-31 12:20:49 +01:00
|
|
|
StatusDataHandler.shared.remove(accountId: account.id, statuses: dbStatusesToRemove)
|
2023-01-20 13:47:38 +01:00
|
|
|
}
|
2023-01-20 16:57:25 +01:00
|
|
|
|
|
|
|
// Add statuses which are not existing in database, but has been downloaded via API.
|
|
|
|
var statusesToAdd: [Status] = []
|
|
|
|
for status in statuses {
|
2023-01-28 21:09:31 +01:00
|
|
|
if !dbStatuses.contains(where: { statusData in statusData.id == status.id }) {
|
2023-01-20 16:57:25 +01:00
|
|
|
statusesToAdd.append(status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-28 21:09:31 +01:00
|
|
|
// Save statuses in database.
|
2023-01-20 16:57:25 +01:00
|
|
|
if !statusesToAdd.isEmpty {
|
2023-01-31 12:20:49 +01:00
|
|
|
_ = try await self.save(statuses: statusesToAdd, for: account, on: backgroundContext)
|
2023-01-20 16:57:25 +01:00
|
|
|
}
|
2023-01-31 12:20:49 +01:00
|
|
|
|
|
|
|
return lastSeenStatusId
|
2023-01-20 13:47:38 +01:00
|
|
|
}
|
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
private func load(for account: AccountModel,
|
2023-01-28 21:09:31 +01:00
|
|
|
on backgroundContext: NSManagedObjectContext,
|
|
|
|
minId: String? = nil,
|
|
|
|
maxId: String? = nil
|
|
|
|
) async throws -> [Status] {
|
2023-01-31 12:20:49 +01:00
|
|
|
guard let accessToken = account.accessToken else {
|
2023-01-20 13:47:38 +01:00
|
|
|
return []
|
2023-01-03 14:09:22 +01:00
|
|
|
}
|
2023-01-31 12:20:49 +01:00
|
|
|
|
2023-01-03 14:09:22 +01:00
|
|
|
// Retrieve statuses from API.
|
2023-02-19 10:43:37 +01:00
|
|
|
let client = PixelfedClient(baseURL: account.serverUrl).getAuthenticated(token: accessToken)
|
2023-01-10 07:16:54 +01:00
|
|
|
let statuses = try await client.getHomeTimeline(maxId: maxId, minId: minId, limit: 20)
|
2023-01-20 16:57:25 +01:00
|
|
|
|
2023-01-28 21:09:31 +01:00
|
|
|
// Save statuses in database.
|
2023-01-31 12:20:49 +01:00
|
|
|
return try await self.save(statuses: statuses, for: account, on: backgroundContext)
|
2023-01-20 16:57:25 +01:00
|
|
|
}
|
|
|
|
|
2023-01-28 21:09:31 +01:00
|
|
|
private func save(statuses: [Status],
|
2023-01-31 12:20:49 +01:00
|
|
|
for account: AccountModel,
|
2023-01-28 21:09:31 +01:00
|
|
|
on backgroundContext: NSManagedObjectContext
|
|
|
|
) async throws -> [Status] {
|
2023-01-31 12:20:49 +01:00
|
|
|
|
|
|
|
guard let accountDataFromDb = AccountDataHandler.shared.getAccountData(accountId: account.id, viewContext: backgroundContext) else {
|
2023-01-28 21:09:31 +01:00
|
|
|
throw DatabaseError.cannotDownloadAccount
|
|
|
|
}
|
2023-01-10 07:16:54 +01:00
|
|
|
|
2023-01-26 15:10:47 +01:00
|
|
|
// Proceed statuses with images only.
|
|
|
|
let statusesWithImages = statuses.getStatusesWithImagesOnly()
|
|
|
|
|
2023-01-04 17:56:01 +01:00
|
|
|
// Save status data in database.
|
2023-01-26 15:10:47 +01:00
|
|
|
for status in statusesWithImages {
|
2023-01-20 13:47:38 +01:00
|
|
|
let statusData = StatusDataHandler.shared.createStatusDataEntity(viewContext: backgroundContext)
|
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
statusData.pixelfedAccount = accountDataFromDb
|
|
|
|
accountDataFromDb.addToStatuses(statusData)
|
2023-01-11 13:16:43 +01:00
|
|
|
|
2023-01-26 15:10:47 +01:00
|
|
|
self.copy(from: status, to: statusData, on: backgroundContext)
|
2023-01-04 17:56:01 +01:00
|
|
|
}
|
2023-01-26 15:10:47 +01:00
|
|
|
|
|
|
|
return statusesWithImages
|
2023-01-05 11:55:20 +01:00
|
|
|
}
|
|
|
|
|
2023-01-26 15:10:47 +01:00
|
|
|
private func copy(from status: Status,
|
|
|
|
to statusData: StatusData,
|
|
|
|
on backgroundContext: NSManagedObjectContext
|
|
|
|
) {
|
2023-01-05 11:55:20 +01:00
|
|
|
statusData.copyFrom(status)
|
2023-01-04 17:56:01 +01:00
|
|
|
|
2023-01-26 15:10:47 +01:00
|
|
|
for attachment in status.getAllImageMediaAttachments() {
|
2023-01-03 14:09:22 +01:00
|
|
|
|
2023-01-04 17:56:01 +01:00
|
|
|
// Save attachment in database.
|
|
|
|
let attachmentData = statusData.attachments().first { item in item.id == attachment.id }
|
2023-01-08 14:50:37 +01:00
|
|
|
?? AttachmentDataHandler.shared.createAttachmnentDataEntity(viewContext: backgroundContext)
|
2023-01-04 17:56:01 +01:00
|
|
|
|
2023-01-05 11:55:20 +01:00
|
|
|
attachmentData.copyFrom(attachment)
|
2023-01-04 17:56:01 +01:00
|
|
|
attachmentData.statusId = statusData.id
|
2023-01-26 15:10:47 +01:00
|
|
|
|
2023-01-04 17:56:01 +01:00
|
|
|
if attachmentData.isInserted {
|
|
|
|
attachmentData.statusRelation = statusData
|
2023-01-29 19:11:44 +01:00
|
|
|
statusData.addToAttachmentsRelation(attachmentData)
|
2023-01-03 14:09:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-10 07:16:54 +01:00
|
|
|
|
2023-01-26 15:10:47 +01:00
|
|
|
private func setExifProperties(in attachmentData: AttachmentData, from imageData: Data) {
|
|
|
|
// Read exif information.
|
|
|
|
if let exifProperties = imageData.getExifData() {
|
|
|
|
if let make = exifProperties.getExifValue("Make"), let model = exifProperties.getExifValue("Model") {
|
|
|
|
attachmentData.exifCamera = "\(make) \(model)"
|
|
|
|
}
|
|
|
|
|
|
|
|
// "Lens" or "Lens Model"
|
|
|
|
if let lens = exifProperties.getExifValue("Lens") {
|
|
|
|
attachmentData.exifLens = lens
|
|
|
|
}
|
|
|
|
|
|
|
|
if let createData = exifProperties.getExifValue("CreateDate") {
|
|
|
|
attachmentData.exifCreatedDate = createData
|
|
|
|
}
|
|
|
|
|
|
|
|
if let focalLenIn35mmFilm = exifProperties.getExifValue("FocalLenIn35mmFilm"),
|
|
|
|
let fNumber = exifProperties.getExifValue("FNumber")?.calculateExifNumber(),
|
|
|
|
let exposureTime = exifProperties.getExifValue("ExposureTime"),
|
|
|
|
let photographicSensitivity = exifProperties.getExifValue("PhotographicSensitivity") {
|
|
|
|
attachmentData.exifExposure = "\(focalLenIn35mmFilm)mm, f/\(fNumber), \(exposureTime)s, ISO \(photographicSensitivity)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-03 14:09:22 +01:00
|
|
|
}
|