NCManageDatabase+Capabilities.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. //
  2. // NCManageDatabase+Capabilities.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 29/05/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 tableCapabilities: Object {
  27. @objc dynamic var account = ""
  28. @objc dynamic var jsondata: Data?
  29. override static func primaryKey() -> String {
  30. return "account"
  31. }
  32. }
  33. extension NCManageDatabase {
  34. func addCapabilitiesJSon(_ data: Data, account: String) {
  35. do {
  36. let realm = try Realm()
  37. try realm.write {
  38. let addObject = tableCapabilities()
  39. addObject.account = account
  40. addObject.jsondata = data
  41. realm.add(addObject, update: .all)
  42. }
  43. } catch let error {
  44. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  45. }
  46. }
  47. func getCapabilities(account: String) -> Data? {
  48. do {
  49. let realm = try Realm()
  50. guard let result = realm.objects(tableCapabilities.self).filter("account == %@", account).first else { return nil }
  51. return result.jsondata
  52. } catch let error as NSError {
  53. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  54. }
  55. return nil
  56. }
  57. func setCapabilities(account: String, data: Data? = nil) {
  58. let jsonData: Data?
  59. struct CapabilityNextcloud: Codable {
  60. struct Ocs: Codable {
  61. let meta: Meta
  62. let data: Data
  63. struct Meta: Codable {
  64. let status: String?
  65. let message: String?
  66. let statuscode: Int?
  67. }
  68. struct Data: Codable {
  69. let version: Version
  70. let capabilities: Capabilities
  71. struct Version: Codable {
  72. let string: String
  73. let major: Int
  74. }
  75. struct Capabilities: Codable {
  76. let filessharing: FilesSharing?
  77. let theming: Theming?
  78. let endtoendencryption: EndToEndEncryption?
  79. let richdocuments: RichDocuments?
  80. let activity: Activity?
  81. let notifications: Notifications?
  82. let files: Files?
  83. let userstatus: UserStatus?
  84. let external: External?
  85. let groupfolders: GroupFolders?
  86. enum CodingKeys: String, CodingKey {
  87. case filessharing = "files_sharing"
  88. case theming
  89. case endtoendencryption = "end-to-end-encryption"
  90. case richdocuments, activity, notifications, files
  91. case userstatus = "user_status"
  92. case external, groupfolders
  93. }
  94. struct FilesSharing: Codable {
  95. let apienabled: Bool?
  96. let groupsharing: Bool?
  97. let resharing: Bool?
  98. let defaultpermissions: Int?
  99. let ncpublic: Public?
  100. enum CodingKeys: String, CodingKey {
  101. case apienabled = "api_enabled"
  102. case groupsharing = "group_sharing"
  103. case resharing
  104. case defaultpermissions = "default_permissions"
  105. case ncpublic = "public"
  106. }
  107. struct Public: Codable {
  108. let upload: Bool
  109. let enabled: Bool
  110. let password: Password?
  111. let sendmail: Bool
  112. let uploadfilesdrop: Bool
  113. let multiplelinks: Bool
  114. let expiredate: ExpireDate?
  115. let expiredateinternal: ExpireDate?
  116. let expiredateremote: ExpireDate?
  117. enum CodingKeys: String, CodingKey {
  118. case upload, enabled, password
  119. case sendmail = "send_mail"
  120. case uploadfilesdrop = "upload_files_drop"
  121. case multiplelinks = "multiple_links"
  122. case expiredate = "expire_date"
  123. case expiredateinternal = "expire_date_internal"
  124. case expiredateremote = "expire_date_remote"
  125. }
  126. struct Password: Codable {
  127. let enforced: Bool?
  128. let askForOptionalPassword: Bool?
  129. }
  130. struct ExpireDate: Codable {
  131. let enforced: Bool?
  132. let days: Int?
  133. }
  134. }
  135. }
  136. struct Theming: Codable {
  137. let color: String?
  138. let colorelement: String?
  139. let colortext: String?
  140. let colorelementbright: String?
  141. let backgrounddefault: Bool?
  142. let backgroundplain: Bool?
  143. let colorelementdark: String?
  144. let name: String?
  145. let slogan: String?
  146. let url: String?
  147. let logo: String?
  148. let background: String?
  149. let logoheader: String?
  150. let favicon: String?
  151. enum CodingKeys: String, CodingKey {
  152. case color
  153. case colorelement = "color-element"
  154. case colortext = "color-text"
  155. case colorelementbright = "color-element-bright"
  156. case backgrounddefault = "background-default"
  157. case backgroundplain = "background-plain"
  158. case colorelementdark = "color-element-dark"
  159. case name, slogan, url, logo, background, logoheader, favicon
  160. }
  161. }
  162. struct EndToEndEncryption: Codable {
  163. let enabled: Bool?
  164. let apiversion: String?
  165. let keysexist: Bool?
  166. enum CodingKeys: String, CodingKey {
  167. case enabled
  168. case apiversion = "api-version"
  169. case keysexist = "keys-exist"
  170. }
  171. }
  172. struct RichDocuments: Codable {
  173. let mimetypes: [String]?
  174. }
  175. struct Activity: Codable {
  176. let apiv2: [String]?
  177. }
  178. struct Notifications: Codable {
  179. let ocsendpoints: [String]?
  180. enum CodingKeys: String, CodingKey {
  181. case ocsendpoints = "ocs-endpoints"
  182. }
  183. }
  184. struct Files: Codable {
  185. let undelete: Bool?
  186. let locking: String?
  187. let comments: Bool?
  188. let versioning: Bool?
  189. let directEditing: DirectEditing?
  190. let bigfilechunking: Bool?
  191. let versiondeletion: Bool?
  192. let versionlabeling: Bool?
  193. enum CodingKeys: String, CodingKey {
  194. case undelete, locking, comments, versioning, directEditing, bigfilechunking
  195. case versiondeletion = "version_deletion"
  196. case versionlabeling = "version_labeling"
  197. }
  198. struct DirectEditing: Codable {
  199. let url: String?
  200. let etag: String?
  201. let supportsFileId: Bool?
  202. }
  203. }
  204. struct UserStatus: Codable {
  205. let enabled: Bool?
  206. let restore: Bool?
  207. let supportsemoji: Bool?
  208. enum CodingKeys: String, CodingKey {
  209. case enabled, restore
  210. case supportsemoji = "supports_emoji"
  211. }
  212. }
  213. struct External: Codable {
  214. let v1: [String]?
  215. }
  216. struct GroupFolders: Codable {
  217. let hasGroupFolders: Bool?
  218. }
  219. }
  220. }
  221. }
  222. let ocs: Ocs
  223. }
  224. if let data = data {
  225. jsonData = data
  226. } else {
  227. do {
  228. let realm = try Realm()
  229. guard let result = realm.objects(tableCapabilities.self).filter("account == %@", account).first,
  230. let data = result.jsondata else {
  231. return
  232. }
  233. jsonData = data
  234. } catch let error as NSError {
  235. NextcloudKit.shared.nkCommonInstance.writeLog("I cannot access to database: \(error)")
  236. return
  237. }
  238. }
  239. guard let jsonData = jsonData else { return }
  240. do {
  241. let json = try JSONDecoder().decode(CapabilityNextcloud.self, from: jsonData)
  242. NCGlobal.shared.capabilityServerVersion = json.ocs.data.version.string
  243. NCGlobal.shared.capabilityServerVersionMajor = json.ocs.data.version.major
  244. NCGlobal.shared.capabilityFileSharingApiEnabled = json.ocs.data.capabilities.filessharing?.apienabled ?? false
  245. NCGlobal.shared.capabilityFileSharingDefaultPermission = json.ocs.data.capabilities.filessharing?.defaultpermissions ?? 0
  246. NCGlobal.shared.capabilityFileSharingPubPasswdEnforced = json.ocs.data.capabilities.filessharing?.ncpublic?.password?.enforced ?? false
  247. NCGlobal.shared.capabilityFileSharingPubExpireDateEnforced = json.ocs.data.capabilities.filessharing?.ncpublic?.expiredate?.enforced ?? false
  248. NCGlobal.shared.capabilityFileSharingPubExpireDateDays = json.ocs.data.capabilities.filessharing?.ncpublic?.expiredate?.days ?? 0
  249. NCGlobal.shared.capabilityFileSharingInternalExpireDateEnforced = json.ocs.data.capabilities.filessharing?.ncpublic?.expiredateinternal?.enforced ?? false
  250. NCGlobal.shared.capabilityFileSharingInternalExpireDateDays = json.ocs.data.capabilities.filessharing?.ncpublic?.expiredateinternal?.days ?? 0
  251. NCGlobal.shared.capabilityFileSharingRemoteExpireDateEnforced = json.ocs.data.capabilities.filessharing?.ncpublic?.expiredateremote?.enforced ?? false
  252. NCGlobal.shared.capabilityFileSharingRemoteExpireDateDays = json.ocs.data.capabilities.filessharing?.ncpublic?.expiredateremote?.days ?? 0
  253. NCGlobal.shared.capabilityThemingColor = json.ocs.data.capabilities.theming?.color ?? ""
  254. NCGlobal.shared.capabilityThemingColorElement = json.ocs.data.capabilities.theming?.colorelement ?? ""
  255. NCGlobal.shared.capabilityThemingColorText = json.ocs.data.capabilities.theming?.colortext ?? ""
  256. NCGlobal.shared.capabilityThemingName = json.ocs.data.capabilities.theming?.name ?? ""
  257. NCGlobal.shared.capabilityThemingSlogan = json.ocs.data.capabilities.theming?.slogan ?? ""
  258. NCGlobal.shared.capabilityE2EEEnabled = json.ocs.data.capabilities.endtoendencryption?.enabled ?? false
  259. NCGlobal.shared.capabilityE2EEApiVersion = json.ocs.data.capabilities.endtoendencryption?.apiversion ?? ""
  260. NCGlobal.shared.capabilityRichdocumentsMimetypes.removeAll()
  261. if let mimetypes = json.ocs.data.capabilities.richdocuments?.mimetypes {
  262. for mimetype in mimetypes {
  263. NCGlobal.shared.capabilityRichdocumentsMimetypes.append(mimetype)
  264. }
  265. }
  266. NCGlobal.shared.capabilityActivity.removeAll()
  267. if let activities = json.ocs.data.capabilities.activity?.apiv2 {
  268. for activity in activities {
  269. NCGlobal.shared.capabilityActivity.append(activity)
  270. }
  271. }
  272. NCGlobal.shared.capabilityNotification.removeAll()
  273. if let notifications = json.ocs.data.capabilities.notifications?.ocsendpoints {
  274. for notification in notifications {
  275. NCGlobal.shared.capabilityNotification.append(notification)
  276. }
  277. }
  278. NCGlobal.shared.capabilityFilesUndelete = json.ocs.data.capabilities.files?.undelete ?? false
  279. NCGlobal.shared.capabilityFilesLockVersion = json.ocs.data.capabilities.files?.locking ?? ""
  280. NCGlobal.shared.capabilityFilesComments = json.ocs.data.capabilities.files?.comments ?? false
  281. NCGlobal.shared.capabilityUserStatusEnabled = json.ocs.data.capabilities.files?.undelete ?? false
  282. if json.ocs.data.capabilities.external != nil {
  283. NCGlobal.shared.capabilityExternalSites = true
  284. }
  285. NCGlobal.shared.capabilityGroupfoldersEnabled = json.ocs.data.capabilities.groupfolders?.hasGroupFolders ?? false
  286. } catch let error as NSError {
  287. NextcloudKit.shared.nkCommonInstance.writeLog("I cannot access to database: \(error)")
  288. return
  289. }
  290. }
  291. }