NCManageDatabase+Capabilities.swift 16 KB

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