NCManageDatabase+Activity.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //
  2. // NCManageDatabase+Activity.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/11/23.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import UIKit
  25. import RealmSwift
  26. import NextcloudKit
  27. import SwiftyJSON
  28. class tableActivity: Object, DateCompareable {
  29. var dateKey: Date { date as Date }
  30. @objc dynamic var account = ""
  31. @objc dynamic var idPrimaryKey = ""
  32. @objc dynamic var action = "Activity"
  33. @objc dynamic var date = NSDate()
  34. @objc dynamic var idActivity: Int = 0
  35. @objc dynamic var app = ""
  36. @objc dynamic var type = ""
  37. @objc dynamic var user = ""
  38. @objc dynamic var subject = ""
  39. @objc dynamic var subjectRich = ""
  40. let subjectRichItem = List<tableActivitySubjectRich>()
  41. @objc dynamic var icon = ""
  42. @objc dynamic var link = ""
  43. @objc dynamic var message = ""
  44. @objc dynamic var objectType = ""
  45. @objc dynamic var objectId: Int = 0
  46. @objc dynamic var objectName = ""
  47. @objc dynamic var note = ""
  48. @objc dynamic var selector = ""
  49. @objc dynamic var verbose: Bool = false
  50. override static func primaryKey() -> String {
  51. return "idPrimaryKey"
  52. }
  53. }
  54. class tableActivityLatestId: Object {
  55. @objc dynamic var account = ""
  56. @objc dynamic var activityFirstKnown: Int = 0
  57. @objc dynamic var activityLastGiven: Int = 0
  58. override static func primaryKey() -> String {
  59. return "account"
  60. }
  61. }
  62. class tableActivityPreview: Object {
  63. @objc dynamic var account = ""
  64. @objc dynamic var filename = ""
  65. @objc dynamic var idPrimaryKey = ""
  66. @objc dynamic var idActivity: Int = 0
  67. @objc dynamic var source = ""
  68. @objc dynamic var link = ""
  69. @objc dynamic var mimeType = ""
  70. @objc dynamic var fileId: Int = 0
  71. @objc dynamic var view = ""
  72. @objc dynamic var isMimeTypeIcon: Bool = false
  73. override static func primaryKey() -> String {
  74. return "idPrimaryKey"
  75. }
  76. }
  77. class tableActivitySubjectRich: Object {
  78. @objc dynamic var account = ""
  79. @objc dynamic var idActivity: Int = 0
  80. @objc dynamic var idPrimaryKey = ""
  81. @objc dynamic var id = ""
  82. @objc dynamic var key = ""
  83. @objc dynamic var link = ""
  84. @objc dynamic var name = ""
  85. @objc dynamic var path = ""
  86. @objc dynamic var type = ""
  87. override static func primaryKey() -> String {
  88. return "idPrimaryKey"
  89. }
  90. }
  91. extension NCManageDatabase {
  92. func addActivity(_ activities: [NKActivity], account: String) {
  93. do {
  94. let realm = try Realm()
  95. try realm.write {
  96. for activity in activities {
  97. let addObjectActivity = tableActivity()
  98. addObjectActivity.account = account
  99. addObjectActivity.idActivity = activity.idActivity
  100. addObjectActivity.idPrimaryKey = account + String(activity.idActivity)
  101. addObjectActivity.date = activity.date as NSDate
  102. addObjectActivity.app = activity.app
  103. addObjectActivity.type = activity.type
  104. addObjectActivity.user = activity.user
  105. addObjectActivity.subject = activity.subject
  106. if let subjectRich = activity.subjectRich,
  107. let json = JSON(subjectRich).array {
  108. addObjectActivity.subjectRich = json[0].stringValue
  109. if json.count > 1,
  110. let dict = json[1].dictionary {
  111. for (key, value) in dict {
  112. let addObjectActivitySubjectRich = tableActivitySubjectRich()
  113. let dict = value as JSON
  114. addObjectActivitySubjectRich.account = account
  115. if dict["id"].intValue > 0 {
  116. addObjectActivitySubjectRich.id = String(dict["id"].intValue)
  117. } else {
  118. addObjectActivitySubjectRich.id = dict["id"].stringValue
  119. }
  120. addObjectActivitySubjectRich.name = dict["name"].stringValue
  121. addObjectActivitySubjectRich.idPrimaryKey = account
  122. + String(activity.idActivity)
  123. + addObjectActivitySubjectRich.id
  124. + addObjectActivitySubjectRich.name
  125. addObjectActivitySubjectRich.key = key
  126. addObjectActivitySubjectRich.idActivity = activity.idActivity
  127. addObjectActivitySubjectRich.link = dict["link"].stringValue
  128. addObjectActivitySubjectRich.path = dict["path"].stringValue
  129. addObjectActivitySubjectRich.type = dict["type"].stringValue
  130. realm.add(addObjectActivitySubjectRich, update: .all)
  131. }
  132. }
  133. }
  134. if let previews = activity.previews,
  135. let json = JSON(previews).array {
  136. for preview in json {
  137. let addObjectActivityPreview = tableActivityPreview()
  138. addObjectActivityPreview.account = account
  139. addObjectActivityPreview.idActivity = activity.idActivity
  140. addObjectActivityPreview.fileId = preview["fileId"].intValue
  141. addObjectActivityPreview.filename = preview["filename"].stringValue
  142. addObjectActivityPreview.idPrimaryKey = account + String(activity.idActivity) + String(addObjectActivityPreview.fileId)
  143. addObjectActivityPreview.source = preview["source"].stringValue
  144. addObjectActivityPreview.link = preview["link"].stringValue
  145. addObjectActivityPreview.mimeType = preview["mimeType"].stringValue
  146. addObjectActivityPreview.view = preview["view"].stringValue
  147. addObjectActivityPreview.isMimeTypeIcon = preview["isMimeTypeIcon"].boolValue
  148. realm.add(addObjectActivityPreview, update: .all)
  149. }
  150. }
  151. addObjectActivity.icon = activity.icon
  152. addObjectActivity.link = activity.link
  153. addObjectActivity.message = activity.message
  154. addObjectActivity.objectType = activity.objectType
  155. addObjectActivity.objectId = activity.objectId
  156. addObjectActivity.objectName = activity.objectName
  157. realm.add(addObjectActivity, update: .all)
  158. }
  159. }
  160. } catch let error {
  161. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  162. }
  163. }
  164. func getActivity(predicate: NSPredicate, filterFileId: String?) -> (all: [tableActivity], filter: [tableActivity]) {
  165. do {
  166. let realm = try Realm()
  167. realm.refresh()
  168. let results = realm.objects(tableActivity.self).filter(predicate).sorted(byKeyPath: "idActivity", ascending: false)
  169. let allActivity = Array(results.map(tableActivity.init))
  170. guard let filterFileId = filterFileId else {
  171. return (all: allActivity, filter: allActivity)
  172. }
  173. // comments are loaded seperately, see NCManageDatabase.getComments
  174. let filtered = allActivity.filter({ String($0.objectId) == filterFileId && $0.type != "comments" })
  175. return (all: allActivity, filter: filtered)
  176. } catch let error as NSError {
  177. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  178. }
  179. return([], [])
  180. }
  181. func getActivitySubjectRich(account: String, idActivity: Int, key: String) -> tableActivitySubjectRich? {
  182. do {
  183. let realm = try Realm()
  184. realm.refresh()
  185. let results = realm.objects(tableActivitySubjectRich.self).filter("account == %@ && idActivity == %d && key == %@", account, idActivity, key).first
  186. return results.map { tableActivitySubjectRich.init(value: $0) }
  187. } catch let error as NSError {
  188. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  189. }
  190. return nil
  191. }
  192. func getActivitySubjectRich(account: String, idActivity: Int, id: String) -> tableActivitySubjectRich? {
  193. do {
  194. let realm = try Realm()
  195. realm.refresh()
  196. let results = realm.objects(tableActivitySubjectRich.self).filter("account == %@ && idActivity == %d && id == %@", account, idActivity, id)
  197. var activitySubjectRich = results.first
  198. if results.count == 2 {
  199. for result in results {
  200. if result.key == "newfile" {
  201. activitySubjectRich = result
  202. }
  203. }
  204. }
  205. return activitySubjectRich.map { tableActivitySubjectRich.init(value: $0) }
  206. } catch let error as NSError {
  207. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  208. }
  209. return nil
  210. }
  211. func getActivityPreview(account: String, idActivity: Int, orderKeysId: [String]) -> [tableActivityPreview] {
  212. var results: [tableActivityPreview] = []
  213. do {
  214. let realm = try Realm()
  215. realm.refresh()
  216. for id in orderKeysId {
  217. if let result = realm.objects(tableActivityPreview.self).filter("account == %@ && idActivity == %d && fileId == %d", account, idActivity, Int(id) ?? 0).first {
  218. results.append(result)
  219. }
  220. }
  221. return results
  222. } catch let error as NSError {
  223. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  224. }
  225. return []
  226. }
  227. func updateLatestActivityId(activityFirstKnown: Int, activityLastGiven: Int, account: String) {
  228. do {
  229. let realm = try Realm()
  230. try realm.write {
  231. let newRecentActivity = tableActivityLatestId()
  232. newRecentActivity.activityFirstKnown = activityFirstKnown
  233. newRecentActivity.activityLastGiven = activityLastGiven
  234. newRecentActivity.account = account
  235. realm.add(newRecentActivity, update: .all)
  236. }
  237. } catch {
  238. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  239. }
  240. }
  241. func getLatestActivityId(account: String) -> tableActivityLatestId? {
  242. do {
  243. let realm = try Realm()
  244. realm.refresh()
  245. return realm.objects(tableActivityLatestId.self).filter("account == %@", account).first
  246. } catch let error as NSError {
  247. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  248. }
  249. return nil
  250. }
  251. }