aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/telegram/PhotoSizeSource.cpp
blob: 575544e0badd300d263e287eb396de39949147ef (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/telegram/PhotoSizeSource.h"

#include "td/telegram/ChannelId.h"
#include "td/telegram/ChatId.h"
#include "td/telegram/UserId.h"

#include "td/utils/common.h"
#include "td/utils/Slice.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/StackAllocator.h"
#include "td/utils/tl_helpers.h"
#include "td/utils/tl_storers.h"

namespace td {

tl_object_ptr<telegram_api::InputPeer> PhotoSizeSource::DialogPhoto::get_input_peer() const {
  switch (dialog_id.get_type()) {
    case DialogType::User: {
      UserId user_id = dialog_id.get_user_id();
      return make_tl_object<telegram_api::inputPeerUser>(user_id.get(), dialog_access_hash);
    }
    case DialogType::Chat: {
      ChatId chat_id = dialog_id.get_chat_id();
      return make_tl_object<telegram_api::inputPeerChat>(chat_id.get());
    }
    case DialogType::Channel: {
      ChannelId channel_id = dialog_id.get_channel_id();
      return make_tl_object<telegram_api::inputPeerChannel>(channel_id.get(), dialog_access_hash);
    }
    case DialogType::SecretChat:
      return nullptr;
    case DialogType::None:
      return make_tl_object<telegram_api::inputPeerEmpty>();
    default:
      UNREACHABLE();
      return nullptr;
  }
}

FileType PhotoSizeSource::get_file_type(const char *source) const {
  switch (get_type(source)) {
    case Type::Thumbnail:
      return thumbnail().file_type;
    case Type::DialogPhotoSmall:
    case Type::DialogPhotoBig:
    case Type::DialogPhotoSmallLegacy:
    case Type::DialogPhotoBigLegacy:
      return FileType::ProfilePhoto;
    case Type::StickerSetThumbnail:
    case Type::StickerSetThumbnailLegacy:
    case Type::StickerSetThumbnailVersion:
      return FileType::Thumbnail;
    case Type::Legacy:
    case Type::FullLegacy:
    default:
      UNREACHABLE();
      return FileType::Thumbnail;
  }
}

string PhotoSizeSource::get_unique(const char *source) const {
  auto compare_type = get_compare_type(source);
  if (compare_type != 2 && compare_type != 3) {
    return string(1, static_cast<char>(compare_type));
  }

  auto ptr = StackAllocator::alloc(16);
  MutableSlice data = ptr.as_slice();
  TlStorerUnsafe storer(data.ubegin());
  if (compare_type == 2) {
    storer.store_slice(Slice("\x02"));
  }
  td::store(get_compare_volume_id(), storer);
  td::store(get_compare_local_id(), storer);
  auto size = storer.get_buf() - data.ubegin();
  CHECK(size <= 13);
  return string(data.begin(), size);
}

int32 PhotoSizeSource::get_compare_type(const char *source) const {
  switch (get_type(source)) {
    case Type::Legacy:
      break;
    case Type::Thumbnail: {
      auto type = thumbnail().thumbnail_type.type;
      if (!(0 <= type && type <= 127)) {
        LOG(FATAL) << source << ' ' << *this;
      }
      if (type == 'a') {
        return 0;
      }
      if (type == 'c') {
        return 1;
      }
      return type + 5;
    }
    case Type::DialogPhotoSmall:
      return 0;
    case Type::DialogPhotoBig:
      return 1;
    case Type::StickerSetThumbnail:
      LOG(FATAL) << source << ' ' << *this;
      break;
    case Type::FullLegacy:
    case Type::DialogPhotoSmallLegacy:
    case Type::DialogPhotoBigLegacy:
    case Type::StickerSetThumbnailLegacy:
      return 3;
    case Type::StickerSetThumbnailVersion:
      return 2;
    default:
      break;
  }
  UNREACHABLE();
  return -1;
}

int64 PhotoSizeSource::get_compare_volume_id() const {
  switch (get_type("get_compare_volume_id")) {
    case Type::FullLegacy:
      return full_legacy().volume_id;
    case Type::DialogPhotoSmallLegacy:
    case Type::DialogPhotoBigLegacy:
      return dialog_photo_legacy().volume_id;
    case Type::StickerSetThumbnailLegacy:
      return sticker_set_thumbnail_legacy().volume_id;
    case Type::StickerSetThumbnailVersion:
      return sticker_set_thumbnail_version().sticker_set_id;
    default:
      UNREACHABLE();
      return 0;
  }
}

int32 PhotoSizeSource::get_compare_local_id() const {
  switch (get_type("get_compare_volume_id")) {
    case Type::FullLegacy:
      return full_legacy().local_id;
    case Type::DialogPhotoSmallLegacy:
    case Type::DialogPhotoBigLegacy:
      return dialog_photo_legacy().local_id;
    case Type::StickerSetThumbnailLegacy:
      return sticker_set_thumbnail_legacy().local_id;
    case Type::StickerSetThumbnailVersion:
      return sticker_set_thumbnail_version().version;
    default:
      UNREACHABLE();
      return 0;
  }
}

bool PhotoSizeSource::unique_less(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs) {
  auto lhs_compare_type = lhs.get_compare_type("unique_less");
  auto rhs_compare_type = rhs.get_compare_type("unique_less");
  if (lhs_compare_type != rhs_compare_type) {
    return lhs_compare_type < rhs_compare_type;
  }
  if (lhs_compare_type != 2 && lhs_compare_type != 3) {
    return false;
  }
  auto lhs_volume_id = lhs.get_compare_volume_id();
  auto rhs_volume_id = rhs.get_compare_volume_id();
  if (lhs_volume_id != rhs_volume_id) {
    return lhs_volume_id < rhs_volume_id;
  }
  return lhs.get_compare_local_id() < rhs.get_compare_local_id();
}

bool PhotoSizeSource::unique_equal(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs) {
  auto lhs_compare_type = lhs.get_compare_type("unique_equal");
  auto rhs_compare_type = rhs.get_compare_type("unique_equal");
  if (lhs_compare_type != rhs_compare_type) {
    return false;
  }
  if (lhs_compare_type != 2 && lhs_compare_type != 3) {
    return true;
  }
  return lhs.get_compare_volume_id() == rhs.get_compare_volume_id() &&
         lhs.get_compare_local_id() == rhs.get_compare_local_id();
}

string PhotoSizeSource::get_unique_name(int64 photo_id, const char *source) const {
  switch (get_type(source)) {
    case Type::Thumbnail: {
      int32 type = thumbnail().thumbnail_type.type;
      CHECK(0 <= type && type <= 127);
      return PSTRING() << photo_id << '_' << type;
    }
    case Type::DialogPhotoSmall:
      return to_string(photo_id);
    case Type::DialogPhotoBig:
      return PSTRING() << photo_id << '_' << 1;
    case Type::StickerSetThumbnailVersion:
      return PSTRING() << sticker_set_thumbnail_version().sticker_set_id << '_'
                       << static_cast<uint32>(sticker_set_thumbnail_version().version);
    case Type::Legacy:
    case Type::StickerSetThumbnail:
    case Type::FullLegacy:
    case Type::DialogPhotoSmallLegacy:
    case Type::DialogPhotoBigLegacy:
    case Type::StickerSetThumbnailLegacy:
    default:
      UNREACHABLE();
      break;
  }
  return string();
}

static bool operator==(const PhotoSizeSource::Legacy &lhs, const PhotoSizeSource::Legacy &rhs) {
  UNREACHABLE();
  return false;
}

static bool operator==(const PhotoSizeSource::Thumbnail &lhs, const PhotoSizeSource::Thumbnail &rhs) {
  return lhs.file_type == rhs.file_type && lhs.thumbnail_type == rhs.thumbnail_type;
}

static bool operator==(const PhotoSizeSource::DialogPhoto &lhs, const PhotoSizeSource::DialogPhoto &rhs) {
  return lhs.dialog_id == rhs.dialog_id && lhs.dialog_access_hash == rhs.dialog_access_hash;
}

static bool operator==(const PhotoSizeSource::DialogPhotoSmall &lhs, const PhotoSizeSource::DialogPhotoSmall &rhs) {
  return static_cast<const PhotoSizeSource::DialogPhoto &>(lhs) ==
         static_cast<const PhotoSizeSource::DialogPhoto &>(rhs);
}

static bool operator==(const PhotoSizeSource::DialogPhotoBig &lhs, const PhotoSizeSource::DialogPhotoBig &rhs) {
  return static_cast<const PhotoSizeSource::DialogPhoto &>(lhs) ==
         static_cast<const PhotoSizeSource::DialogPhoto &>(rhs);
}

static bool operator==(const PhotoSizeSource::StickerSetThumbnail &lhs,
                       const PhotoSizeSource::StickerSetThumbnail &rhs) {
  return lhs.sticker_set_id == rhs.sticker_set_id && lhs.sticker_set_access_hash == rhs.sticker_set_access_hash;
}

static bool operator==(const PhotoSizeSource::FullLegacy &lhs, const PhotoSizeSource::FullLegacy &rhs) {
  return lhs.volume_id == rhs.volume_id && lhs.local_id == rhs.local_id && lhs.secret == rhs.secret;
}

static bool operator==(const PhotoSizeSource::DialogPhotoLegacy &lhs, const PhotoSizeSource::DialogPhotoLegacy &rhs) {
  return static_cast<const PhotoSizeSource::DialogPhoto &>(lhs) ==
             static_cast<const PhotoSizeSource::DialogPhoto &>(rhs) &&
         lhs.volume_id == rhs.volume_id && lhs.local_id == rhs.local_id;
}

static bool operator==(const PhotoSizeSource::DialogPhotoSmallLegacy &lhs,
                       const PhotoSizeSource::DialogPhotoSmallLegacy &rhs) {
  return static_cast<const PhotoSizeSource::DialogPhotoLegacy &>(lhs) ==
         static_cast<const PhotoSizeSource::DialogPhotoLegacy &>(rhs);
}

static bool operator==(const PhotoSizeSource::DialogPhotoBigLegacy &lhs,
                       const PhotoSizeSource::DialogPhotoBigLegacy &rhs) {
  return static_cast<const PhotoSizeSource::DialogPhotoLegacy &>(lhs) ==
         static_cast<const PhotoSizeSource::DialogPhotoLegacy &>(rhs);
}

static bool operator==(const PhotoSizeSource::StickerSetThumbnailLegacy &lhs,
                       const PhotoSizeSource::StickerSetThumbnailLegacy &rhs) {
  return static_cast<const PhotoSizeSource::StickerSetThumbnail &>(lhs) ==
             static_cast<const PhotoSizeSource::StickerSetThumbnail &>(rhs) &&
         lhs.volume_id == rhs.volume_id && lhs.local_id == rhs.local_id;
}

static bool operator==(const PhotoSizeSource::StickerSetThumbnailVersion &lhs,
                       const PhotoSizeSource::StickerSetThumbnailVersion &rhs) {
  return static_cast<const PhotoSizeSource::StickerSetThumbnail &>(lhs) ==
             static_cast<const PhotoSizeSource::StickerSetThumbnail &>(rhs) &&
         lhs.version == rhs.version;
}

bool operator==(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs) {
  return lhs.variant_ == rhs.variant_;
}

bool operator!=(const PhotoSizeSource &lhs, const PhotoSizeSource &rhs) {
  return !(lhs == rhs);
}

StringBuilder &operator<<(StringBuilder &string_builder, const PhotoSizeSource &source) {
  switch (source.get_type("operator<<")) {
    case PhotoSizeSource::Type::Legacy:
      return string_builder << "PhotoSizeSourceLegacy[]";
    case PhotoSizeSource::Type::Thumbnail:
      return string_builder << "PhotoSizeSourceThumbnail[" << source.thumbnail().file_type
                            << ", type = " << source.thumbnail().thumbnail_type << ']';
    case PhotoSizeSource::Type::DialogPhotoSmall:
      return string_builder << "PhotoSizeSourceChatPhotoSmall[]";
    case PhotoSizeSource::Type::DialogPhotoBig:
      return string_builder << "PhotoSizeSourceChatPhotoBig[]";
    case PhotoSizeSource::Type::StickerSetThumbnail:
      return string_builder << "PhotoSizeSourceStickerSetThumbnail[" << source.sticker_set_thumbnail().sticker_set_id
                            << ']';
    case PhotoSizeSource::Type::FullLegacy:
      return string_builder << "PhotoSizeSourceFullLegacy[]";
    case PhotoSizeSource::Type::DialogPhotoSmallLegacy:
      return string_builder << "PhotoSizeSourceChatPhotoSmallLegacy[]";
    case PhotoSizeSource::Type::DialogPhotoBigLegacy:
      return string_builder << "PhotoSizeSourceChatPhotoBigLegacy[]";
    case PhotoSizeSource::Type::StickerSetThumbnailLegacy:
      return string_builder << "PhotoSizeSourceStickerSetThumbnailLegacy["
                            << source.sticker_set_thumbnail().sticker_set_id << ']';
    case PhotoSizeSource::Type::StickerSetThumbnailVersion:
      return string_builder << "PhotoSizeSourceStickerSetThumbnailVersion["
                            << source.sticker_set_thumbnail().sticker_set_id << '_'
                            << source.sticker_set_thumbnail_version().version << ']';
    default:
      UNREACHABLE();
      return string_builder;
  }
}

}  // namespace td