-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathCachedChannelAdmins.swift
118 lines (94 loc) · 3.58 KB
/
CachedChannelAdmins.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// CachedChannelAdmins.swift
// Telegram
//
// Created by Mikhail Filimonov on 02/01/2019.
// Copyright © 2019 Telegram. All rights reserved.
//
import Foundation
import Postbox
import TelegramCore
import SwiftSignalKit
public enum CachedChannelAdminRank: Codable, Equatable {
case owner
case admin
case custom(String)
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: StringCodingKey.self)
let value: Int32 = try container.decode(Int32.self, forKey: "v")
switch value {
case 0:
self = .owner
case 1:
self = .admin
case 2:
self = .custom(try container.decode(String.self, forKey: "s"))
default:
self = .admin
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: StringCodingKey.self)
switch self {
case .owner:
try container.encode(0 as Int32, forKey: "v")
case .admin:
try container.encode(1 as Int32, forKey: "v")
case let .custom(rank):
try container.encode(2 as Int32, forKey: "v")
try container.encode(rank, forKey: "s")
}
}
}
public final class CachedChannelAdminRanks: Codable {
private struct DictionaryKey: Codable, Hashable {
var key: PeerId
init(_ key: PeerId) {
self.key = key
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: StringCodingKey.self)
self.key = PeerId(try container.decode(Int64.self, forKey: "k"))
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: StringCodingKey.self)
try container.encode(self.key.toInt64(), forKey: "k")
}
}
public let ranks: [PeerId: CachedChannelAdminRank]
public init(ranks: [PeerId: CachedChannelAdminRank]) {
self.ranks = ranks
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: StringCodingKey.self)
let dict = try container.decode([DictionaryKey: CachedChannelAdminRank].self, forKey: "ranks")
var mappedDict: [PeerId: CachedChannelAdminRank] = [:]
for (key, value) in dict {
mappedDict[key.key] = value
}
self.ranks = mappedDict
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: StringCodingKey.self)
var mappedDict: [DictionaryKey: CachedChannelAdminRank] = [:]
for (k, v) in self.ranks {
mappedDict[DictionaryKey(k)] = v
}
try container.encode(mappedDict, forKey: "ranks")
}
public static func cacheKey(peerId: PeerId) -> ValueBoxKey {
let key = ValueBoxKey(length: 8)
key.setInt64(0, value: peerId.toInt64())
return key
}
}
public func cachedChannelAdminRanksEntryId(peerId: PeerId) -> ItemCacheEntryId {
return ItemCacheEntryId(collectionId: 100, key: CachedChannelAdminRanks.cacheKey(peerId: peerId))
}
func updateCachedChannelAdminRanks(postbox: Postbox, peerId: PeerId, ranks: Dictionary<PeerId, CachedChannelAdminRank>) -> Signal<Void, NoError> {
return postbox.transaction { transaction -> Void in
if let entry = CodableEntry(CachedChannelAdminRanks(ranks: ranks)) {
transaction.putItemCacheEntry(id: ItemCacheEntryId(collectionId: 100, key: CachedChannelAdminRanks.cacheKey(peerId: peerId)), entry: entry)
}
}
}