session-ios/SessionMessagingKit/Open Groups/Models/OpenGroup.swift

68 lines
2.5 KiB
Swift
Raw Normal View History

import Sodium
import SessionUtilitiesKit
2021-03-24 02:37:33 +01:00
// FIXME: We need to leave the @objc name as `SNOpenGroupV2` otherwise YapDatabase won't be able to decode it
2021-03-24 02:37:33 +01:00
@objc(SNOpenGroupV2)
public final class OpenGroup: NSObject, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility
2021-03-24 05:12:58 +01:00
@objc public let server: String
@objc public let room: String
2021-03-24 02:37:33 +01:00
public let id: String
2021-05-07 06:08:52 +02:00
@objc public let publicKey: String
@objc public let name: String
@objc public let groupDescription: String? // API key is 'description'
2021-03-26 00:39:51 +01:00
/// The ID with which the image can be retrieved from the server.
2021-03-25 06:16:08 +01:00
public let imageID: String?
/// Monotonic room information counter that increases each time the room's metadata changes
public let infoUpdates: Int64
2021-03-24 02:37:33 +01:00
public init(
server: String,
room: String,
publicKey: String,
name: String,
groupDescription: String?,
imageID: String?,
infoUpdates: Int64
) {
2021-03-24 02:37:33 +01:00
self.server = server.lowercased()
self.room = room
self.id = "\(server).\(room)"
self.publicKey = publicKey
self.name = name
self.groupDescription = groupDescription
2021-03-25 04:17:49 +01:00
self.imageID = imageID
self.infoUpdates = infoUpdates
2021-03-24 02:37:33 +01:00
}
// MARK: - Coding
2021-03-24 02:37:33 +01:00
public init?(coder: NSCoder) {
server = coder.decodeObject(forKey: "server") as! String
room = coder.decodeObject(forKey: "room") as! String
self.id = "\(server).\(room)"
publicKey = coder.decodeObject(forKey: "publicKey") as! String
name = coder.decodeObject(forKey: "name") as! String
groupDescription = coder.decodeObject(forKey: "groupDescription") as? String
2021-03-25 06:16:08 +01:00
imageID = coder.decodeObject(forKey: "imageID") as! String?
infoUpdates = ((coder.decodeObject(forKey: "infoUpdates") as? Int64) ?? 0)
2021-03-24 02:37:33 +01:00
super.init()
}
public func encode(with coder: NSCoder) {
coder.encode(server, forKey: "server")
coder.encode(room, forKey: "room")
coder.encode(publicKey, forKey: "publicKey")
coder.encode(name, forKey: "name")
if let groupDescription = groupDescription { coder.encode(groupDescription, forKey: "groupDescription") }
2021-03-25 06:16:08 +01:00
if let imageID = imageID { coder.encode(imageID, forKey: "imageID") }
coder.encode(infoUpdates, forKey: "infoUpdates")
2021-03-24 02:37:33 +01:00
}
2021-03-25 04:17:49 +01:00
override public var description: String { "\(name) (Server: \(server), Room: \(room)" }
2021-03-24 02:37:33 +01:00
}