mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.1 KiB
Swift
102 lines
3.1 KiB
Swift
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
import Foundation
|
|
import GRDB
|
|
import SessionUtilitiesKit
|
|
|
|
public struct ClosedGroup: Codable, Identifiable, FetchableRecord, PersistableRecord, TableRecord, ColumnExpressible {
|
|
public static var databaseTableName: String { "closedGroup" }
|
|
internal static let threadForeignKey = ForeignKey([Columns.threadId], to: [SessionThread.Columns.id])
|
|
private static let thread = belongsTo(SessionThread.self, using: threadForeignKey)
|
|
internal static let keyPairs = hasMany(
|
|
ClosedGroupKeyPair.self,
|
|
using: ClosedGroupKeyPair.closedGroupForeignKey
|
|
)
|
|
public static let members = hasMany(GroupMember.self, using: GroupMember.closedGroupForeignKey)
|
|
|
|
public typealias Columns = CodingKeys
|
|
public enum CodingKeys: String, CodingKey, ColumnExpression {
|
|
case threadId
|
|
case name
|
|
case formationTimestamp
|
|
}
|
|
|
|
public var id: String { threadId } // Identifiable
|
|
public var publicKey: String { threadId }
|
|
|
|
/// The id for the thread this closed group belongs to
|
|
///
|
|
/// **Note:** This value will always be publicKey for the closed group
|
|
public let threadId: String
|
|
public let name: String
|
|
public let formationTimestamp: TimeInterval
|
|
|
|
// MARK: - Relationships
|
|
|
|
public var thread: QueryInterfaceRequest<SessionThread> {
|
|
request(for: ClosedGroup.thread)
|
|
}
|
|
|
|
public var keyPairs: QueryInterfaceRequest<ClosedGroupKeyPair> {
|
|
request(for: ClosedGroup.keyPairs)
|
|
}
|
|
|
|
public var allMembers: QueryInterfaceRequest<GroupMember> {
|
|
request(for: ClosedGroup.members)
|
|
}
|
|
|
|
public var members: QueryInterfaceRequest<GroupMember> {
|
|
request(for: ClosedGroup.members)
|
|
.filter(GroupMember.Columns.role == GroupMember.Role.standard)
|
|
}
|
|
|
|
public var zombies: QueryInterfaceRequest<GroupMember> {
|
|
request(for: ClosedGroup.members)
|
|
.filter(GroupMember.Columns.role == GroupMember.Role.zombie)
|
|
}
|
|
|
|
public var moderators: QueryInterfaceRequest<GroupMember> {
|
|
request(for: ClosedGroup.members)
|
|
.filter(GroupMember.Columns.role == GroupMember.Role.moderator)
|
|
}
|
|
|
|
public var admins: QueryInterfaceRequest<GroupMember> {
|
|
request(for: ClosedGroup.members)
|
|
.filter(GroupMember.Columns.role == GroupMember.Role.admin)
|
|
}
|
|
|
|
// MARK: - Initialization
|
|
|
|
public init(
|
|
threadId: String,
|
|
name: String,
|
|
formationTimestamp: TimeInterval
|
|
) {
|
|
self.threadId = threadId
|
|
self.name = name
|
|
self.formationTimestamp = formationTimestamp
|
|
}
|
|
}
|
|
|
|
// MARK: - Mutation
|
|
|
|
public extension ClosedGroup {
|
|
func with(name: String) -> ClosedGroup {
|
|
return ClosedGroup(
|
|
threadId: threadId,
|
|
name: name,
|
|
formationTimestamp: formationTimestamp
|
|
)
|
|
}
|
|
}
|
|
|
|
// MARK: - GRDB Interactions
|
|
|
|
public extension ClosedGroup {
|
|
func fetchLatestKeyPair(_ db: Database) throws -> ClosedGroupKeyPair? {
|
|
return try keyPairs
|
|
.order(ClosedGroupKeyPair.Columns.receivedTimestamp.desc)
|
|
.fetchOne(db)
|
|
}
|
|
}
|