NCManageDatabase+Comments.swift 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // NCManageDatabase+Comments.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/11/23.
  6. // Copyright © 2023 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 NextcloudKit
  26. class tableComments: Object, DateCompareable {
  27. var dateKey: Date { creationDateTime as Date }
  28. @objc dynamic var account = ""
  29. @objc dynamic var actorDisplayName = ""
  30. @objc dynamic var actorId = ""
  31. @objc dynamic var actorType = ""
  32. @objc dynamic var creationDateTime = NSDate()
  33. @objc dynamic var isUnread: Bool = false
  34. @objc dynamic var message = ""
  35. @objc dynamic var messageId = ""
  36. @objc dynamic var objectId = ""
  37. @objc dynamic var objectType = ""
  38. @objc dynamic var path = ""
  39. @objc dynamic var verb = ""
  40. override static func primaryKey() -> String {
  41. return "messageId"
  42. }
  43. }
  44. extension NCManageDatabase {
  45. func addComments(_ comments: [NKComments], account: String, objectId: String) {
  46. do {
  47. let realm = try Realm()
  48. try realm.write {
  49. let results = realm.objects(tableComments.self).filter("account == %@ AND objectId == %@", account, objectId)
  50. realm.delete(results)
  51. for comment in comments {
  52. let object = tableComments()
  53. object.account = account
  54. object.actorDisplayName = comment.actorDisplayName
  55. object.actorId = comment.actorId
  56. object.actorType = comment.actorType
  57. object.creationDateTime = comment.creationDateTime as NSDate
  58. object.isUnread = comment.isUnread
  59. object.message = comment.message
  60. object.messageId = comment.messageId
  61. object.objectId = comment.objectId
  62. object.objectType = comment.objectType
  63. object.path = comment.path
  64. object.verb = comment.verb
  65. realm.add(object, update: .all)
  66. }
  67. }
  68. } catch let error {
  69. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  70. }
  71. }
  72. func getComments(account: String, objectId: String) -> [tableComments] {
  73. do {
  74. let realm = try Realm()
  75. realm.refresh()
  76. let results = realm.objects(tableComments.self).filter("account == %@ AND objectId == %@", account, objectId).sorted(byKeyPath: "creationDateTime", ascending: false)
  77. return Array(results.map(tableComments.init))
  78. } catch let error as NSError {
  79. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  80. }
  81. return []
  82. }
  83. }