NCManageDatabase+Comments.swift 3.4 KB

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