Swiftgram/TelegramCore/CachedGroupData.swift
Peter 532285b062 Added support for pinning messages in basic groups and saved messages
Added support for separate channel notification settings
2018-11-03 01:13:37 +04:00

162 lines
6.9 KiB
Swift

import Foundation
#if os(macOS)
import PostboxMac
#else
import Postbox
#endif
public final class CachedPeerBotInfo: PostboxCoding, Equatable {
public let peerId: PeerId
public let botInfo: BotInfo
init(peerId: PeerId, botInfo: BotInfo) {
self.peerId = peerId
self.botInfo = botInfo
}
public init(decoder: PostboxDecoder) {
self.peerId = PeerId(decoder.decodeInt64ForKey("p", orElse: 0))
self.botInfo = decoder.decodeObjectForKey("i", decoder: { return BotInfo(decoder: $0) }) as! BotInfo
}
public func encode(_ encoder: PostboxEncoder) {
encoder.encodeInt64(self.peerId.toInt64(), forKey: "p")
encoder.encodeObject(self.botInfo, forKey: "i")
}
public static func ==(lhs: CachedPeerBotInfo, rhs: CachedPeerBotInfo) -> Bool {
return lhs.peerId == rhs.peerId && lhs.botInfo == rhs.botInfo
}
}
public final class CachedGroupData: CachedPeerData {
public let participants: CachedGroupParticipants?
public let exportedInvitation: ExportedInvitation?
public let botInfos: [CachedPeerBotInfo]
public let reportStatus: PeerReportStatus
public let pinnedMessageId: MessageId?
public let peerIds: Set<PeerId>
public let messageIds: Set<MessageId>
public let associatedHistoryMessageId: MessageId? = nil
init() {
self.participants = nil
self.exportedInvitation = nil
self.botInfos = []
self.reportStatus = .unknown
self.pinnedMessageId = nil
self.messageIds = Set()
self.peerIds = Set()
}
public init(participants: CachedGroupParticipants?, exportedInvitation: ExportedInvitation?, botInfos: [CachedPeerBotInfo], reportStatus: PeerReportStatus, pinnedMessageId: MessageId?) {
self.participants = participants
self.exportedInvitation = exportedInvitation
self.botInfos = botInfos
self.reportStatus = reportStatus
self.pinnedMessageId = pinnedMessageId
var messageIds = Set<MessageId>()
if let pinnedMessageId = self.pinnedMessageId {
messageIds.insert(pinnedMessageId)
}
self.messageIds = messageIds
var peerIds = Set<PeerId>()
if let participants = participants {
for participant in participants.participants {
peerIds.insert(participant.peerId)
}
}
for botInfo in botInfos {
peerIds.insert(botInfo.peerId)
}
self.peerIds = peerIds
}
public init(decoder: PostboxDecoder) {
let participants = decoder.decodeObjectForKey("p", decoder: { CachedGroupParticipants(decoder: $0) }) as? CachedGroupParticipants
self.participants = participants
self.exportedInvitation = decoder.decodeObjectForKey("i", decoder: { ExportedInvitation(decoder: $0) }) as? ExportedInvitation
self.botInfos = decoder.decodeObjectArrayWithDecoderForKey("b") as [CachedPeerBotInfo]
self.reportStatus = PeerReportStatus(rawValue: decoder.decodeInt32ForKey("r", orElse: 0))!
if let pinnedMessagePeerId = decoder.decodeOptionalInt64ForKey("pm.p"), let pinnedMessageNamespace = decoder.decodeOptionalInt32ForKey("pm.n"), let pinnedMessageId = decoder.decodeOptionalInt32ForKey("pm.i") {
self.pinnedMessageId = MessageId(peerId: PeerId(pinnedMessagePeerId), namespace: pinnedMessageNamespace, id: pinnedMessageId)
} else {
self.pinnedMessageId = nil
}
var messageIds = Set<MessageId>()
if let pinnedMessageId = self.pinnedMessageId {
messageIds.insert(pinnedMessageId)
}
self.messageIds = messageIds
var peerIds = Set<PeerId>()
if let participants = participants {
for participant in participants.participants {
peerIds.insert(participant.peerId)
}
}
for botInfo in self.botInfos {
peerIds.insert(botInfo.peerId)
}
self.peerIds = peerIds
}
public func encode(_ encoder: PostboxEncoder) {
if let participants = self.participants {
encoder.encodeObject(participants, forKey: "p")
} else {
encoder.encodeNil(forKey: "p")
}
if let exportedInvitation = self.exportedInvitation {
encoder.encodeObject(exportedInvitation, forKey: "i")
} else {
encoder.encodeNil(forKey: "i")
}
encoder.encodeObjectArray(self.botInfos, forKey: "b")
encoder.encodeInt32(self.reportStatus.rawValue, forKey: "r")
if let pinnedMessageId = self.pinnedMessageId {
encoder.encodeInt64(pinnedMessageId.peerId.toInt64(), forKey: "pm.p")
encoder.encodeInt32(pinnedMessageId.namespace, forKey: "pm.n")
encoder.encodeInt32(pinnedMessageId.id, forKey: "pm.i")
} else {
encoder.encodeNil(forKey: "pm.p")
encoder.encodeNil(forKey: "pm.n")
encoder.encodeNil(forKey: "pm.i")
}
}
public func isEqual(to: CachedPeerData) -> Bool {
guard let other = to as? CachedGroupData else {
return false
}
return self.participants == other.participants && self.exportedInvitation == other.exportedInvitation && self.botInfos == other.botInfos && self.reportStatus == other.reportStatus && self.pinnedMessageId == other.pinnedMessageId
}
func withUpdatedParticipants(_ participants: CachedGroupParticipants?) -> CachedGroupData {
return CachedGroupData(participants: participants, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId)
}
func withUpdatedExportedInvitation(_ exportedInvitation: ExportedInvitation?) -> CachedGroupData {
return CachedGroupData(participants: self.participants, exportedInvitation: exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId)
}
func withUpdatedBotInfos(_ botInfos: [CachedPeerBotInfo]) -> CachedGroupData {
return CachedGroupData(participants: self.participants, exportedInvitation: self.exportedInvitation, botInfos: botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId)
}
func withUpdatedReportStatus(_ reportStatus: PeerReportStatus) -> CachedGroupData {
return CachedGroupData(participants: self.participants, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: reportStatus, pinnedMessageId: self.pinnedMessageId)
}
func withUpdatedPinnedMessageId(_ pinnedMessageId: MessageId?) -> CachedGroupData {
return CachedGroupData(participants: self.participants, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: pinnedMessageId)
}
}