NCManageDatabase+Activity.swift 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //
  2. // NCManageDatabase+Activity.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 30.11.21.
  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 RealmSwift
  25. import NCCommunication
  26. import SwiftyJSON
  27. extension NCManageDatabase {
  28. @objc func addActivity(_ activities: [NCCommunicationActivity], account: String) {
  29. let realm = try! Realm()
  30. do {
  31. try realm.write {
  32. for activity in activities {
  33. let addObjectActivity = tableActivity()
  34. addObjectActivity.account = account
  35. addObjectActivity.idActivity = activity.idActivity
  36. addObjectActivity.idPrimaryKey = account + String(activity.idActivity)
  37. addObjectActivity.date = activity.date
  38. addObjectActivity.app = activity.app
  39. addObjectActivity.type = activity.type
  40. addObjectActivity.user = activity.user
  41. addObjectActivity.subject = activity.subject
  42. if let subject_rich = activity.subject_rich,
  43. let json = JSON(subject_rich).array {
  44. addObjectActivity.subjectRich = json[0].stringValue
  45. if json.count > 1,
  46. let dict = json[1].dictionary {
  47. for (key, value) in dict {
  48. let addObjectActivitySubjectRich = tableActivitySubjectRich()
  49. let dict = value as JSON
  50. addObjectActivitySubjectRich.account = account
  51. if dict["id"].intValue > 0 {
  52. addObjectActivitySubjectRich.id = String(dict["id"].intValue)
  53. } else {
  54. addObjectActivitySubjectRich.id = dict["id"].stringValue
  55. }
  56. addObjectActivitySubjectRich.name = dict["name"].stringValue
  57. addObjectActivitySubjectRich.idPrimaryKey = account
  58. + String(activity.idActivity)
  59. + addObjectActivitySubjectRich.id
  60. + addObjectActivitySubjectRich.name
  61. addObjectActivitySubjectRich.key = key
  62. addObjectActivitySubjectRich.idActivity = activity.idActivity
  63. addObjectActivitySubjectRich.link = dict["link"].stringValue
  64. addObjectActivitySubjectRich.path = dict["path"].stringValue
  65. addObjectActivitySubjectRich.type = dict["type"].stringValue
  66. realm.add(addObjectActivitySubjectRich, update: .all)
  67. }
  68. }
  69. }
  70. if let previews = activity.previews,
  71. let json = JSON(previews).array {
  72. for preview in json {
  73. let addObjectActivityPreview = tableActivityPreview()
  74. addObjectActivityPreview.account = account
  75. addObjectActivityPreview.idActivity = activity.idActivity
  76. addObjectActivityPreview.fileId = preview["fileId"].intValue
  77. addObjectActivityPreview.filename = preview["filename"].stringValue
  78. addObjectActivityPreview.idPrimaryKey = account + String(activity.idActivity) + String(addObjectActivityPreview.fileId)
  79. addObjectActivityPreview.source = preview["source"].stringValue
  80. addObjectActivityPreview.link = preview["link"].stringValue
  81. addObjectActivityPreview.mimeType = preview["mimeType"].stringValue
  82. addObjectActivityPreview.view = preview["view"].stringValue
  83. addObjectActivityPreview.isMimeTypeIcon = preview["isMimeTypeIcon"].boolValue
  84. realm.add(addObjectActivityPreview, update: .all)
  85. }
  86. }
  87. addObjectActivity.icon = activity.icon
  88. addObjectActivity.link = activity.link
  89. addObjectActivity.message = activity.message
  90. addObjectActivity.objectType = activity.object_type
  91. addObjectActivity.objectId = activity.object_id
  92. addObjectActivity.objectName = activity.object_name
  93. realm.add(addObjectActivity, update: .all)
  94. }
  95. }
  96. } catch let error {
  97. NCCommunicationCommon.shared.writeLog("Could not write to database: \(error)")
  98. }
  99. }
  100. func getActivity(predicate: NSPredicate, filterFileId: String?) -> (all: [tableActivity], filter: [tableActivity]) {
  101. let realm = try! Realm()
  102. let results = realm.objects(tableActivity.self).filter(predicate).sorted(byKeyPath: "idActivity", ascending: false)
  103. let allActivity = Array(results.map(tableActivity.init))
  104. guard let filterFileId = filterFileId else {
  105. return (all: allActivity, filter: allActivity)
  106. }
  107. // comments are loaded seperately, see NCManageDatabase.getComments
  108. let filtered = allActivity.filter({ String($0.objectId) == filterFileId && $0.type != "comments" })
  109. return (all: allActivity, filter: filtered)
  110. }
  111. @objc func getActivitySubjectRich(account: String, idActivity: Int, key: String) -> tableActivitySubjectRich? {
  112. let realm = try! Realm()
  113. let results = realm.objects(tableActivitySubjectRich.self).filter("account == %@ && idActivity == %d && key == %@", account, idActivity, key).first
  114. return results.map { tableActivitySubjectRich.init(value: $0) }
  115. }
  116. @objc func getActivitySubjectRich(account: String, idActivity: Int, id: String) -> tableActivitySubjectRich? {
  117. let realm = try! Realm()
  118. let results = realm.objects(tableActivitySubjectRich.self).filter("account == %@ && idActivity == %d && id == %@", account, idActivity, id).first
  119. return results.map { tableActivitySubjectRich.init(value: $0) }
  120. }
  121. @objc func getActivityPreview(account: String, idActivity: Int, orderKeysId: [String]) -> [tableActivityPreview] {
  122. let realm = try! Realm()
  123. var results: [tableActivityPreview] = []
  124. for id in orderKeysId {
  125. if let result = realm.objects(tableActivityPreview.self).filter("account == %@ && idActivity == %d && fileId == %d", account, idActivity, Int(id) ?? 0).first {
  126. results.append(result)
  127. }
  128. }
  129. return results
  130. }
  131. func updateLatestActivityId(activityFirstKnown: Int, activityLastGiven: Int, account: String) {
  132. let realm = try! Realm()
  133. do {
  134. try realm.write {
  135. let newRecentActivity = tableActivityLatestId()
  136. newRecentActivity.activityFirstKnown = activityFirstKnown
  137. newRecentActivity.activityLastGiven = activityLastGiven
  138. newRecentActivity.account = account
  139. realm.add(newRecentActivity, update: .all)
  140. }
  141. } catch {
  142. NCCommunicationCommon.shared.writeLog("Could not write to database: \(error)")
  143. }
  144. }
  145. func getLatestActivityId(account: String) -> tableActivityLatestId? {
  146. let realm = try! Realm()
  147. return realm.objects(tableActivityLatestId.self).filter("account == %@", account).first
  148. }
  149. // MARK: -
  150. // MARK: Table Comments
  151. @objc func addComments(_ comments: [NCCommunicationComments], account: String, objectId: String) {
  152. let realm = try! Realm()
  153. do {
  154. try realm.safeWrite {
  155. let results = realm.objects(tableComments.self).filter("account == %@ AND objectId == %@", account, objectId)
  156. realm.delete(results)
  157. for comment in comments {
  158. let object = tableComments()
  159. object.account = account
  160. object.actorDisplayName = comment.actorDisplayName
  161. object.actorId = comment.actorId
  162. object.actorType = comment.actorType
  163. object.creationDateTime = comment.creationDateTime as NSDate
  164. object.isUnread = comment.isUnread
  165. object.message = comment.message
  166. object.messageId = comment.messageId
  167. object.objectId = comment.objectId
  168. object.objectType = comment.objectType
  169. object.path = comment.path
  170. object.verb = comment.verb
  171. realm.add(object, update: .all)
  172. }
  173. }
  174. } catch let error {
  175. NCCommunicationCommon.shared.writeLog("Could not write to database: \(error)")
  176. }
  177. }
  178. @objc func getComments(account: String, objectId: String) -> [tableComments] {
  179. let realm = try! Realm()
  180. let results = realm.objects(tableComments.self).filter("account == %@ AND objectId == %@", account, objectId).sorted(byKeyPath: "creationDateTime", ascending: false)
  181. return Array(results.map(tableComments.init))
  182. }
  183. }