diff --git a/TelegramCore/CachedChannelParticipants.swift b/TelegramCore/CachedChannelParticipants.swift index f680a85ef7..b4ea35f2a3 100644 --- a/TelegramCore/CachedChannelParticipants.swift +++ b/TelegramCore/CachedChannelParticipants.swift @@ -40,9 +40,33 @@ public struct ChannelParticipantAdminInfo: Coding, Equatable { } } +public struct ChannelParticipantBannedInfo: Coding, Equatable { + public let rights: TelegramChannelBannedRights + public let restrictedBy: PeerId + + public init(rights: TelegramChannelBannedRights, restrictedBy: PeerId) { + self.rights = rights + self.restrictedBy = restrictedBy + } + + public init(decoder: Decoder) { + self.rights = decoder.decodeObjectForKey("r", decoder: { TelegramChannelBannedRights(decoder: $0) }) as! TelegramChannelBannedRights + self.restrictedBy = PeerId(decoder.decodeInt64ForKey("p", orElse: 0)) + } + + public func encode(_ encoder: Encoder) { + encoder.encodeObject(self.rights, forKey: "r") + encoder.encodeInt64(self.restrictedBy.toInt64(), forKey: "p") + } + + public static func ==(lhs: ChannelParticipantBannedInfo, rhs: ChannelParticipantBannedInfo) -> Bool { + return lhs.rights == rhs.rights && lhs.restrictedBy == rhs.restrictedBy + } +} + public enum ChannelParticipant: Coding, Equatable { case creator(id: PeerId) - case member(id: PeerId, invitedAt: Int32, adminInfo: ChannelParticipantAdminInfo?, banInfo: TelegramChannelBannedRights?) + case member(id: PeerId, invitedAt: Int32, adminInfo: ChannelParticipantAdminInfo?, banInfo: ChannelParticipantBannedInfo?) public var peerId: PeerId { switch self { @@ -85,7 +109,7 @@ public enum ChannelParticipant: Coding, Equatable { public init(decoder: Decoder) { switch decoder.decodeInt32ForKey("r", orElse: 0) { case ChannelParticipantValue.member.rawValue: - self = .member(id: PeerId(decoder.decodeInt64ForKey("i", orElse: 0)), invitedAt: decoder.decodeInt32ForKey("t", orElse: 0), adminInfo: decoder.decodeObjectForKey("ai", decoder: { ChannelParticipantAdminInfo(decoder: $0) }) as? ChannelParticipantAdminInfo, banInfo: decoder.decodeObjectForKey("bi", decoder: { TelegramChannelBannedRights(decoder: $0) }) as? TelegramChannelBannedRights) + self = .member(id: PeerId(decoder.decodeInt64ForKey("i", orElse: 0)), invitedAt: decoder.decodeInt32ForKey("t", orElse: 0), adminInfo: decoder.decodeObjectForKey("ai", decoder: { ChannelParticipantAdminInfo(decoder: $0) }) as? ChannelParticipantAdminInfo, banInfo: decoder.decodeObjectForKey("bi", decoder: { ChannelParticipantBannedInfo(decoder: $0) }) as? ChannelParticipantBannedInfo) case ChannelParticipantValue.creator.rawValue: self = .creator(id: PeerId(decoder.decodeInt64ForKey("i", orElse: 0))) default: @@ -144,10 +168,12 @@ extension ChannelParticipant { self = .member(id: PeerId(namespace: Namespaces.Peer.CloudUser, id: userId), invitedAt: date, adminInfo: nil, banInfo: nil) case let .channelParticipantCreator(userId): self = .creator(id: PeerId(namespace: Namespaces.Peer.CloudUser, id: userId)) - case let .channelParticipantKicked(userId, _, date): - self = .member(id: PeerId(namespace: Namespaces.Peer.CloudUser, id: userId), invitedAt: date, adminInfo: nil, banInfo: TelegramChannelBannedRights(flags: [.banReadMessages], untilDate: Int32.max)) - case let .channelParticipantBanned(userId, _, date, bannedRights): - self = .member(id: PeerId(namespace: Namespaces.Peer.CloudUser, id: userId), invitedAt: date, adminInfo: nil, banInfo: TelegramChannelBannedRights(apiBannedRights: bannedRights)) + case let .channelParticipantKicked(userId, restrictedBy, date): + let banInfo = ChannelParticipantBannedInfo(rights: TelegramChannelBannedRights(flags: [.banReadMessages], untilDate: Int32.max), restrictedBy: PeerId(namespace: Namespaces.Peer.CloudUser, id: restrictedBy)) + self = .member(id: PeerId(namespace: Namespaces.Peer.CloudUser, id: userId), invitedAt: date, adminInfo: nil, banInfo: banInfo) + case let .channelParticipantBanned(userId, restrictedBy, date, bannedRights): + let banInfo = ChannelParticipantBannedInfo(rights: TelegramChannelBannedRights(apiBannedRights: bannedRights), restrictedBy: PeerId(namespace: Namespaces.Peer.CloudUser, id: restrictedBy)) + self = .member(id: PeerId(namespace: Namespaces.Peer.CloudUser, id: userId), invitedAt: date, adminInfo: nil, banInfo: banInfo) case let .channelParticipantAdmin(flags, userId, _, promotedBy, date, adminRights): self = .member(id: PeerId(namespace: Namespaces.Peer.CloudUser, id: userId), invitedAt: date, adminInfo: ChannelParticipantAdminInfo(rights: TelegramChannelAdminRights(apiAdminRights: adminRights), promotedBy: PeerId(namespace: Namespaces.Peer.CloudUser, id: promotedBy), canBeEditedByAccountPeer: (flags & (1 << 0)) != 0), banInfo: nil) case let .channelParticipantSelf(userId, _, date): diff --git a/TelegramCore/ChannelAdmins.swift b/TelegramCore/ChannelAdmins.swift index 2592439224..16b5fe0422 100644 --- a/TelegramCore/ChannelAdmins.swift +++ b/TelegramCore/ChannelAdmins.swift @@ -20,18 +20,18 @@ public func channelAdmins(account: Account, peerId: PeerId) -> Signal<[RenderedC var items: [RenderedChannelParticipant] = [] var peers: [PeerId: Peer] = [:] - var status:[PeerId: PeerPresence] = [:] + var presences:[PeerId: PeerPresence] = [:] for user in users { let peer = TelegramUser(user: user) peers[peer.id] = peer if let presence = TelegramUserPresence(apiUser: user) { - status[peer.id] = presence + presences[peer.id] = presence } } for participant in CachedChannelParticipants(apiParticipants: participants).participants { if let peer = peers[participant.peerId] { - items.append(RenderedChannelParticipant(participant: participant, peer: peer, peers: peers, presence: status[peer.id])) + items.append(RenderedChannelParticipant(participant: participant, peer: peer, peers: peers, presences: presences)) } } diff --git a/TelegramCore/ChannelBlacklist.swift b/TelegramCore/ChannelBlacklist.swift index d230b2d3a5..61c44bd340 100644 --- a/TelegramCore/ChannelBlacklist.swift +++ b/TelegramCore/ChannelBlacklist.swift @@ -31,18 +31,18 @@ private func fetchChannelBlacklist(account: Account, peerId: PeerId, filter: Cha switch result { case let .channelParticipants(_, participants, users): var peers: [PeerId: Peer] = [:] - var status:[PeerId: PeerPresence] = [:] + var presences:[PeerId: PeerPresence] = [:] for user in users { let peer = TelegramUser(user: user) peers[peer.id] = peer if let presence = TelegramUserPresence(apiUser: user) { - status[peer.id] = presence + presences[peer.id] = presence } } for participant in CachedChannelParticipants(apiParticipants: participants).participants { if let peer = peers[participant.peerId] { - items.append(RenderedChannelParticipant(participant: participant, peer: peer, peers: peers, presence: status[peer.id])) + items.append(RenderedChannelParticipant(participant: participant, peer: peer, peers: peers, presences: presences)) } } @@ -90,10 +90,10 @@ public struct ChannelBlacklist { var updatedBanned = updated.banned if case .member(_, _, _, let maybeBanInfo) = participant.participant, let banInfo = maybeBanInfo { - if banInfo.flags.contains(.banReadMessages) { + if banInfo.rights.flags.contains(.banReadMessages) { updatedBanned.insert(participant, at: 0) } else { - if !banInfo.flags.isEmpty { + if !banInfo.rights.flags.isEmpty { updatedRestricted.insert(participant, at: 0) } } @@ -147,9 +147,9 @@ public func updateChannelMemberBannedRights(account: Account, peerId: PeerId, me break case let .member(_, _, _, banInfo): if let banInfo = banInfo { - if banInfo.flags.contains(.banReadMessages) { + if banInfo.rights.flags.contains(.banReadMessages) { wasKicked = true - } else if !banInfo.flags.isEmpty { + } else if !banInfo.rights.flags.isEmpty { wasBanned = true } } diff --git a/TelegramCore/ChannelMembers.swift b/TelegramCore/ChannelMembers.swift index 11fe86a455..4beddd0764 100644 --- a/TelegramCore/ChannelMembers.swift +++ b/TelegramCore/ChannelMembers.swift @@ -31,18 +31,18 @@ public func channelMembers(account: Account, peerId: PeerId, filter: ChannelMemb switch result { case let .channelParticipants(_, participants, users): var peers: [PeerId: Peer] = [:] - var status:[PeerId: PeerPresence] = [:] + var presences:[PeerId: PeerPresence] = [:] for user in users { let peer = TelegramUser(user: user) peers[peer.id] = peer if let presence = TelegramUserPresence(apiUser: user) { - status[peer.id] = presence + presences[peer.id] = presence } } for participant in CachedChannelParticipants(apiParticipants: participants).participants { if let peer = peers[participant.peerId] { - items.append(RenderedChannelParticipant(participant: participant, peer: peer, peers: peers, presence: status[peer.id])) + items.append(RenderedChannelParticipant(participant: participant, peer: peer, peers: peers, presences: presences)) } } diff --git a/TelegramCore/ChannelParticipants.swift b/TelegramCore/ChannelParticipants.swift index b4b366b4a7..a36d2bf28e 100644 --- a/TelegramCore/ChannelParticipants.swift +++ b/TelegramCore/ChannelParticipants.swift @@ -13,22 +13,15 @@ public struct RenderedChannelParticipant: Equatable { public let participant: ChannelParticipant public let peer: Peer public let peers: [PeerId: Peer] - public let presence:PeerPresence? - public init(participant: ChannelParticipant, peer: Peer, peers: [PeerId: Peer] = [:], presence: PeerPresence? = nil) { + public let presences:[PeerId: PeerPresence] + public init(participant: ChannelParticipant, peer: Peer, peers: [PeerId: Peer] = [:], presences:[PeerId : PeerPresence] = [:]) { self.participant = participant self.peer = peer self.peers = peers - self.presence = presence + self.presences = presences } public static func ==(lhs: RenderedChannelParticipant, rhs: RenderedChannelParticipant) -> Bool { - if let lhsPresence = lhs.presence, let rhsPresence = rhs.presence { - if !lhsPresence.isEqual(to: rhsPresence) { - return false - } - } else if(lhs.presence != nil) != (rhs.presence != nil) { - return false - } return lhs.participant == rhs.participant && lhs.peer.isEqual(rhs.peer) } }