123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- //
- // NCManageDatabase+Activity.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 13/11/23.
- // Copyright © 2021 Marino Faggiana. All rights reserved.
- //
- // Author Marino Faggiana <marino.faggiana@nextcloud.com>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- import Foundation
- import UIKit
- import RealmSwift
- import NextcloudKit
- import SwiftyJSON
- class tableActivity: Object, DateCompareable {
- var dateKey: Date { date as Date }
- @objc dynamic var account = ""
- @objc dynamic var idPrimaryKey = ""
- @objc dynamic var action = "Activity"
- @objc dynamic var date = NSDate()
- @objc dynamic var idActivity: Int = 0
- @objc dynamic var app = ""
- @objc dynamic var type = ""
- @objc dynamic var user = ""
- @objc dynamic var subject = ""
- @objc dynamic var subjectRich = ""
- let subjectRichItem = List<tableActivitySubjectRich>()
- @objc dynamic var icon = ""
- @objc dynamic var link = ""
- @objc dynamic var message = ""
- @objc dynamic var objectType = ""
- @objc dynamic var objectId: Int = 0
- @objc dynamic var objectName = ""
- @objc dynamic var note = ""
- @objc dynamic var selector = ""
- @objc dynamic var verbose: Bool = false
- override static func primaryKey() -> String {
- return "idPrimaryKey"
- }
- }
- class tableActivityLatestId: Object {
- @objc dynamic var account = ""
- @objc dynamic var activityFirstKnown: Int = 0
- @objc dynamic var activityLastGiven: Int = 0
- override static func primaryKey() -> String {
- return "account"
- }
- }
- class tableActivityPreview: Object {
- @objc dynamic var account = ""
- @objc dynamic var filename = ""
- @objc dynamic var idPrimaryKey = ""
- @objc dynamic var idActivity: Int = 0
- @objc dynamic var source = ""
- @objc dynamic var link = ""
- @objc dynamic var mimeType = ""
- @objc dynamic var fileId: Int = 0
- @objc dynamic var view = ""
- @objc dynamic var isMimeTypeIcon: Bool = false
- override static func primaryKey() -> String {
- return "idPrimaryKey"
- }
- }
- class tableActivitySubjectRich: Object {
- @objc dynamic var account = ""
- @objc dynamic var idActivity: Int = 0
- @objc dynamic var idPrimaryKey = ""
- @objc dynamic var id = ""
- @objc dynamic var key = ""
- @objc dynamic var link = ""
- @objc dynamic var name = ""
- @objc dynamic var path = ""
- @objc dynamic var type = ""
- override static func primaryKey() -> String {
- return "idPrimaryKey"
- }
- }
- extension NCManageDatabase {
- func addActivity(_ activities: [NKActivity], account: String) {
- do {
- let realm = try Realm()
- try realm.write {
- for activity in activities {
- let addObjectActivity = tableActivity()
- addObjectActivity.account = account
- addObjectActivity.idActivity = activity.idActivity
- addObjectActivity.idPrimaryKey = account + String(activity.idActivity)
- addObjectActivity.date = activity.date as NSDate
- addObjectActivity.app = activity.app
- addObjectActivity.type = activity.type
- addObjectActivity.user = activity.user
- addObjectActivity.subject = activity.subject
- if let subjectRich = activity.subjectRich,
- let json = JSON(subjectRich).array {
- addObjectActivity.subjectRich = json[0].stringValue
- if json.count > 1,
- let dict = json[1].dictionary {
- for (key, value) in dict {
- let addObjectActivitySubjectRich = tableActivitySubjectRich()
- let dict = value as JSON
- addObjectActivitySubjectRich.account = account
- if dict["id"].intValue > 0 {
- addObjectActivitySubjectRich.id = String(dict["id"].intValue)
- } else {
- addObjectActivitySubjectRich.id = dict["id"].stringValue
- }
- addObjectActivitySubjectRich.name = dict["name"].stringValue
- addObjectActivitySubjectRich.idPrimaryKey = account
- + String(activity.idActivity)
- + addObjectActivitySubjectRich.id
- + addObjectActivitySubjectRich.name
- addObjectActivitySubjectRich.key = key
- addObjectActivitySubjectRich.idActivity = activity.idActivity
- addObjectActivitySubjectRich.link = dict["link"].stringValue
- addObjectActivitySubjectRich.path = dict["path"].stringValue
- addObjectActivitySubjectRich.type = dict["type"].stringValue
- realm.add(addObjectActivitySubjectRich, update: .all)
- }
- }
- }
- if let previews = activity.previews,
- let json = JSON(previews).array {
- for preview in json {
- let addObjectActivityPreview = tableActivityPreview()
- addObjectActivityPreview.account = account
- addObjectActivityPreview.idActivity = activity.idActivity
- addObjectActivityPreview.fileId = preview["fileId"].intValue
- addObjectActivityPreview.filename = preview["filename"].stringValue
- addObjectActivityPreview.idPrimaryKey = account + String(activity.idActivity) + String(addObjectActivityPreview.fileId)
- addObjectActivityPreview.source = preview["source"].stringValue
- addObjectActivityPreview.link = preview["link"].stringValue
- addObjectActivityPreview.mimeType = preview["mimeType"].stringValue
- addObjectActivityPreview.view = preview["view"].stringValue
- addObjectActivityPreview.isMimeTypeIcon = preview["isMimeTypeIcon"].boolValue
- realm.add(addObjectActivityPreview, update: .all)
- }
- }
- addObjectActivity.icon = activity.icon
- addObjectActivity.link = activity.link
- addObjectActivity.message = activity.message
- addObjectActivity.objectType = activity.objectType
- addObjectActivity.objectId = activity.objectId
- addObjectActivity.objectName = activity.objectName
- realm.add(addObjectActivity, update: .all)
- }
- }
- } catch let error {
- NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
- }
- }
- func getActivity(predicate: NSPredicate, filterFileId: String?) -> (all: [tableActivity], filter: [tableActivity]) {
- do {
- let realm = try Realm()
- realm.refresh()
- let results = realm.objects(tableActivity.self).filter(predicate).sorted(byKeyPath: "idActivity", ascending: false)
- let allActivity = Array(results.map(tableActivity.init))
- guard let filterFileId = filterFileId else {
- return (all: allActivity, filter: allActivity)
- }
- // comments are loaded seperately, see NCManageDatabase.getComments
- let filtered = allActivity.filter({ String($0.objectId) == filterFileId && $0.type != "comments" })
- return (all: allActivity, filter: filtered)
- } catch let error as NSError {
- NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
- }
- return([], [])
- }
- func getActivitySubjectRich(account: String, idActivity: Int, key: String) -> tableActivitySubjectRich? {
- do {
- let realm = try Realm()
- realm.refresh()
- let results = realm.objects(tableActivitySubjectRich.self).filter("account == %@ && idActivity == %d && key == %@", account, idActivity, key).first
- return results.map { tableActivitySubjectRich.init(value: $0) }
- } catch let error as NSError {
- NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
- }
- return nil
- }
- func getActivitySubjectRich(account: String, idActivity: Int, id: String) -> tableActivitySubjectRich? {
- do {
- let realm = try Realm()
- realm.refresh()
- let results = realm.objects(tableActivitySubjectRich.self).filter("account == %@ && idActivity == %d && id == %@", account, idActivity, id)
- var activitySubjectRich = results.first
- if results.count == 2 {
- for result in results {
- if result.key == "newfile" {
- activitySubjectRich = result
- }
- }
- }
- return activitySubjectRich.map { tableActivitySubjectRich.init(value: $0) }
- } catch let error as NSError {
- NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
- }
- return nil
- }
- func getActivityPreview(account: String, idActivity: Int, orderKeysId: [String]) -> [tableActivityPreview] {
- var results: [tableActivityPreview] = []
- do {
- let realm = try Realm()
- realm.refresh()
- for id in orderKeysId {
- if let result = realm.objects(tableActivityPreview.self).filter("account == %@ && idActivity == %d && fileId == %d", account, idActivity, Int(id) ?? 0).first {
- results.append(result)
- }
- }
- return results
- } catch let error as NSError {
- NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
- }
- return []
- }
- func updateLatestActivityId(activityFirstKnown: Int, activityLastGiven: Int, account: String) {
- do {
- let realm = try Realm()
- try realm.write {
- let newRecentActivity = tableActivityLatestId()
- newRecentActivity.activityFirstKnown = activityFirstKnown
- newRecentActivity.activityLastGiven = activityLastGiven
- newRecentActivity.account = account
- realm.add(newRecentActivity, update: .all)
- }
- } catch {
- NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
- }
- }
- func getLatestActivityId(account: String) -> tableActivityLatestId? {
- do {
- let realm = try Realm()
- realm.refresh()
- return realm.objects(tableActivityLatestId.self).filter("account == %@", account).first
- } catch let error as NSError {
- NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
- }
- return nil
- }
- }
|