Simplify some usages of optional TL fields.

This commit is contained in:
levlam 2022-12-18 22:15:57 +03:00
parent 4240026bf7
commit f1eedfaa47
12 changed files with 47 additions and 111 deletions

View File

@ -17402,8 +17402,7 @@ void ContactsManager::on_chat_update(telegram_api::channelForbidden &channel, co
c->date = 0;
c->is_changed = true;
}
int32 unban_date = (channel.flags_ & CHANNEL_FLAG_HAS_UNBAN_DATE) != 0 ? channel.until_date_ : 0;
on_update_channel_status(c, channel_id, DialogParticipantStatus::Banned(unban_date));
on_update_channel_status(c, channel_id, DialogParticipantStatus::Banned(channel.until_date_));
// on_update_channel_usernames(c, channel_id, Usernames()); // don't know if channel usernames are empty, so don't update it
tl_object_ptr<telegram_api::chatBannedRights> banned_rights; // == nullptr
on_update_channel_default_permissions(c, channel_id, RestrictedRights(banned_rights));

View File

@ -1716,17 +1716,10 @@ InlineMessageContent create_inline_message_content(Td *td, FileId file_id,
}
case telegram_api::botInlineMessageMediaGeo::ID: {
auto inline_message = move_tl_object_as<telegram_api::botInlineMessageMediaGeo>(bot_inline_message);
if ((inline_message->flags_ & telegram_api::botInlineMessageMediaGeo::PERIOD_MASK) != 0 &&
inline_message->period_ > 0) {
auto heading = (inline_message->flags_ & telegram_api::botInlineMessageMediaGeo::HEADING_MASK) != 0
? inline_message->heading_
: 0;
auto approacing_notification_radius =
(inline_message->flags_ & telegram_api::botInlineMessageMediaGeo::PROXIMITY_NOTIFICATION_RADIUS_MASK) != 0
? inline_message->proximity_notification_radius_
: 0;
result.message_content = make_unique<MessageLiveLocation>(
Location(inline_message->geo_), inline_message->period_, heading, approacing_notification_radius);
if (inline_message->period_ > 0) {
result.message_content =
make_unique<MessageLiveLocation>(Location(inline_message->geo_), inline_message->period_,
inline_message->heading_, inline_message->proximity_notification_radius_);
} else {
result.message_content = make_unique<MessageLocation>(Location(inline_message->geo_));
}
@ -4146,10 +4139,8 @@ static auto secret_to_telegram(secret_api::documentAttributeFilename &filename)
// documentAttributeVideo flags:# round_message:flags.0?true duration:int w:int h:int = DocumentAttribute;
static auto secret_to_telegram(secret_api::documentAttributeVideo &video) {
return make_tl_object<telegram_api::documentAttributeVideo>(
(video.flags_ & secret_api::documentAttributeVideo::ROUND_MESSAGE_MASK) != 0
? telegram_api::documentAttributeVideo::ROUND_MESSAGE_MASK
: 0,
video.round_message_, false, video.duration_, video.w_, video.h_);
video.round_message_ ? telegram_api::documentAttributeVideo::ROUND_MESSAGE_MASK : 0, video.round_message_, false,
video.duration_, video.w_, video.h_);
}
static auto telegram_documentAttributeAudio(bool is_voice_note, int duration, string title, string performer,
@ -4190,8 +4181,8 @@ static auto secret_to_telegram(secret_api::documentAttributeAudio45 &audio) {
// documentAttributeAudio flags:# voice:flags.10?true duration:int title:flags.0?string
// performer:flags.1?string waveform:flags.2?bytes = DocumentAttribute;
static auto secret_to_telegram(secret_api::documentAttributeAudio &audio) {
return telegram_documentAttributeAudio((audio.flags_ & secret_api::documentAttributeAudio::VOICE_MASK) != 0,
audio.duration_, audio.title_, audio.performer_, audio.waveform_.clone());
return telegram_documentAttributeAudio(audio.voice_, audio.duration_, audio.title_, audio.performer_,
audio.waveform_.clone());
}
static auto secret_to_telegram(std::vector<tl_object_ptr<secret_api::DocumentAttribute>> &attributes) {
@ -5032,8 +5023,7 @@ unique_ptr<MessageContent> get_action_message_content(Td *td, tl_object_ptr<tele
}
case telegram_api::messageActionPhoneCall::ID: {
auto action = move_tl_object_as<telegram_api::messageActionPhoneCall>(action_ptr);
auto duration =
(action->flags_ & telegram_api::messageActionPhoneCall::DURATION_MASK) != 0 ? action->duration_ : 0;
auto duration = action->duration_;
if (duration < 0) {
LOG(ERROR) << "Receive invalid " << oneline(to_string(action));
break;

View File

@ -3444,9 +3444,7 @@ class SendMessageQuery final : public Td::ResultHandler {
std::move(sent_message->entities_));
auto message_id = MessageId(ServerMessageId(sent_message->id_));
auto ttl_period = (sent_message->flags_ & telegram_api::updateShortSentMessage::TTL_PERIOD_MASK) != 0
? sent_message->ttl_period_
: 0;
auto ttl_period = sent_message->ttl_period_;
auto update = make_tl_object<updateSentMessage>(random_id_, message_id, sent_message->date_, ttl_period);
if (dialog_id_.get_type() == DialogType::Channel) {
td_->messages_manager_->add_pending_channel_update(dialog_id_, std::move(update), sent_message->pts_,
@ -6874,10 +6872,8 @@ void MessagesManager::on_update_channel_too_long(tl_object_ptr<telegram_api::upd
}
}
int32 update_pts = (update->flags_ & UPDATE_CHANNEL_TO_LONG_FLAG_HAS_PTS) ? update->pts_ : 0;
if (d != nullptr) {
if (update_pts == 0 || update_pts > d->pts) {
if (update->pts_ == 0 || update->pts_ > d->pts) {
get_channel_difference(dialog_id, d->pts, true, "on_update_channel_too_long 1");
}
} else {
@ -14559,8 +14555,7 @@ MessagesManager::MessageInfo MessagesManager::parse_telegram_api_message(
message_info.media_album_id != 0, new_source.c_str()),
std::move(message->media_), message_info.dialog_id, is_content_read, message_info.via_bot_user_id,
&message_info.ttl, &message_info.disable_web_page_preview, new_source.c_str());
message_info.reply_markup =
message->flags_ & MESSAGE_FLAG_HAS_REPLY_MARKUP ? std::move(message->reply_markup_) : nullptr;
message_info.reply_markup = std::move(message->reply_markup_);
message_info.restriction_reasons = get_restriction_reasons(std::move(message->restriction_reason_));
message_info.author_signature = std::move(message->post_author_);
break;
@ -15800,7 +15795,7 @@ void MessagesManager::on_get_dialogs(FolderId folder_id, vector<tl_object_ptr<te
LOG(ERROR) << "Last " << last_message_id << " in " << dialog_id << " not found";
return promise.set_error(Status::Error(500, "Wrong query result returned: last message not found"));
}
FolderId dialog_folder_id((dialog->flags_ & DIALOG_FLAG_HAS_FOLDER_ID) != 0 ? dialog->folder_id_ : 0);
FolderId dialog_folder_id(dialog->folder_id_);
if (dialog_folder_id != folder_id) {
LOG(ERROR) << "Receive " << dialog_id << " in " << dialog_folder_id << " instead of " << folder_id;
continue;
@ -15853,7 +15848,7 @@ void MessagesManager::on_get_dialogs(FolderId folder_id, vector<tl_object_ptr<te
bool is_new = d->last_new_message_id == MessageId();
auto positions = get_dialog_positions(d);
set_dialog_folder_id(d, FolderId((dialog->flags_ & DIALOG_FLAG_HAS_FOLDER_ID) != 0 ? dialog->folder_id_ : 0));
set_dialog_folder_id(d, FolderId(dialog->folder_id_));
on_update_dialog_notify_settings(dialog_id, std::move(dialog->notify_settings_), source);
if (!d->notification_settings.is_synchronized && !td_->auth_manager_->is_bot()) {
@ -39529,7 +39524,7 @@ void MessagesManager::on_get_channel_difference(
}
}
set_dialog_folder_id(d, FolderId((dialog->flags_ & DIALOG_FLAG_HAS_FOLDER_ID) != 0 ? dialog->folder_id_ : 0));
set_dialog_folder_id(d, FolderId(dialog->folder_id_));
on_update_dialog_notify_settings(dialog_id, std::move(dialog->notify_settings_),
"updates.channelDifferenceTooLong");

View File

@ -1805,8 +1805,6 @@ class MessagesManager final : public Actor {
static constexpr int64 MAX_ORDINARY_DIALOG_ORDER =
9221294780217032704; // == get_dialog_order(MessageId(), MIN_PINNED_DIALOG_DATE - 1)
static constexpr int32 UPDATE_CHANNEL_TO_LONG_FLAG_HAS_PTS = 1 << 0;
static constexpr int32 CHANNEL_DIFFERENCE_FLAG_IS_FINAL = 1 << 0;
static constexpr int32 CHANNEL_DIFFERENCE_FLAG_HAS_TIMEOUT = 1 << 1;

View File

@ -1571,9 +1571,8 @@ PollId PollManager::on_get_poll(PollId poll_id, tl_object_ptr<telegram_api::poll
poll->is_updated_after_close = true;
is_changed = true;
}
int32 open_period =
(poll_server->flags_ & telegram_api::poll::CLOSE_PERIOD_MASK) != 0 ? poll_server->close_period_ : 0;
int32 close_date = (poll_server->flags_ & telegram_api::poll::CLOSE_DATE_MASK) != 0 ? poll_server->close_date_ : 0;
int32 open_period = poll_server->close_period_;
int32 close_date = poll_server->close_date_;
if (close_date == 0 || open_period == 0) {
close_date = 0;
open_period = 0;

View File

@ -272,9 +272,8 @@ static InlineKeyboardButton get_inline_keyboard_button(
}
case telegram_api::keyboardButtonSwitchInline::ID: {
auto keyboard_button = move_tl_object_as<telegram_api::keyboardButtonSwitchInline>(keyboard_button_ptr);
button.type = (keyboard_button->flags_ & telegram_api::keyboardButtonSwitchInline::SAME_PEER_MASK) != 0
? InlineKeyboardButton::Type::SwitchInlineCurrentDialog
: InlineKeyboardButton::Type::SwitchInline;
button.type = keyboard_button->same_peer_ ? InlineKeyboardButton::Type::SwitchInlineCurrentDialog
: InlineKeyboardButton::Type::SwitchInline;
button.text = std::move(keyboard_button->text_);
button.data = std::move(keyboard_button->query_);
break;

View File

@ -61,12 +61,11 @@ ScopeNotificationSettings get_scope_notification_settings(tl_object_ptr<telegram
if (settings == nullptr) {
return ScopeNotificationSettings();
}
auto mute_until = (settings->flags_ & telegram_api::peerNotifySettings::MUTE_UNTIL_MASK) == 0 ||
settings->mute_until_ <= G()->unix_time()
? 0
: settings->mute_until_;
auto show_preview =
(settings->flags_ & telegram_api::peerNotifySettings::SHOW_PREVIEWS_MASK) == 0 ? false : settings->show_previews_;
auto mute_until = settings->mute_until_;
if (mute_until <= G()->unix_time()) {
mute_until = 0;
}
auto show_preview = settings->show_previews_;
return {mute_until, get_notification_sound(settings.get()), show_preview, old_disable_pinned_message_notifications,
old_disable_mention_notifications};
}

View File

@ -16,8 +16,7 @@ void SendCodeHelper::on_sent_code(telegram_api::object_ptr<telegram_api::auth_se
phone_code_hash_ = std::move(sent_code->phone_code_hash_);
sent_code_info_ = get_sent_authentication_code_info(std::move(sent_code->type_));
next_code_info_ = get_authentication_code_info(std::move(sent_code->next_type_));
next_code_timestamp_ =
Time::now() + ((sent_code->flags_ & SENT_CODE_FLAG_HAS_TIMEOUT) != 0 ? sent_code->timeout_ : 0);
next_code_timestamp_ = Time::now() + sent_code->timeout_;
}
void SendCodeHelper::on_phone_code_hash(string &&phone_code_hash) {

View File

@ -56,10 +56,6 @@ class SendCodeHelper {
void parse(ParserT &parser);
private:
static constexpr int32 SENT_CODE_FLAG_IS_USER_REGISTERED = 1 << 0;
static constexpr int32 SENT_CODE_FLAG_HAS_NEXT_TYPE = 1 << 1;
static constexpr int32 SENT_CODE_FLAG_HAS_TIMEOUT = 1 << 2;
struct AuthenticationCodeInfo {
enum class Type : int32 { None, Message, Sms, Call, FlashCall, MissedCall, Fragment };
Type type = Type::None;

View File

@ -670,8 +670,7 @@ bool UpdatesManager::is_acceptable_message(const telegram_api::Message *message_
return false;
}
if (message->flags_ & MessagesManager::MESSAGE_FLAG_HAS_MEDIA) {
CHECK(message->media_ != nullptr);
if (message->media_ != nullptr) {
auto media_id = message->media_->get_id();
if (media_id == telegram_api::messageMediaContact::ID) {
auto message_media_contact = static_cast<const telegram_api::messageMediaContact *>(message->media_.get());
@ -953,8 +952,7 @@ void UpdatesManager::on_get_updates(tl_object_ptr<telegram_api::Updates> &&updat
update->flags_ ^= MessagesManager::MESSAGE_FLAG_HAS_MEDIA;
}
auto from_id = update->flags_ & MessagesManager::MESSAGE_FLAG_IS_OUT ? td_->contacts_manager_->get_my_id().get()
: update->user_id_;
auto from_id = update->out_ ? td_->contacts_manager_->get_my_id().get() : update->user_id_;
update->flags_ |= MessagesManager::MESSAGE_FLAG_HAS_FROM_ID;
auto message = make_tl_object<telegram_api::message>(
@ -3100,11 +3098,7 @@ void UpdatesManager::on_update(tl_object_ptr<telegram_api::updatePeerSettings> u
}
void UpdatesManager::on_update(tl_object_ptr<telegram_api::updatePeerHistoryTTL> update, Promise<Unit> &&promise) {
MessageTtl message_ttl;
if ((update->flags_ & telegram_api::updatePeerHistoryTTL::TTL_PERIOD_MASK) != 0) {
message_ttl = MessageTtl(update->ttl_period_);
}
td_->messages_manager_->on_update_dialog_message_ttl(DialogId(update->peer_), message_ttl);
td_->messages_manager_->on_update_dialog_message_ttl(DialogId(update->peer_), MessageTtl(update->ttl_period_));
promise.set_value(Unit());
}
@ -3329,13 +3323,9 @@ void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateChatUserTyping>
}
void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateChannelUserTyping> update, Promise<Unit> &&promise) {
MessageId top_thread_message_id;
if ((update->flags_ & telegram_api::updateChannelUserTyping::TOP_MSG_ID_MASK) != 0) {
top_thread_message_id = MessageId(ServerMessageId(update->top_msg_id_));
}
td_->messages_manager_->on_dialog_action(DialogId(ChannelId(update->channel_id_)), top_thread_message_id,
DialogId(update->from_id_), DialogAction(std::move(update->action_)),
get_short_update_date());
td_->messages_manager_->on_dialog_action(DialogId(ChannelId(update->channel_id_)),
MessageId(ServerMessageId(update->top_msg_id_)), DialogId(update->from_id_),
DialogAction(std::move(update->action_)), get_short_update_date());
promise.set_value(Unit());
}
@ -3444,12 +3434,8 @@ void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateChatDefaultBann
}
void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateDraftMessage> update, Promise<Unit> &&promise) {
MessageId top_thread_message_id;
if ((update->flags_ & telegram_api::updateDraftMessage::TOP_MSG_ID_MASK) != 0) {
top_thread_message_id = MessageId(ServerMessageId(update->top_msg_id_));
}
td_->messages_manager_->on_update_dialog_draft_message(DialogId(update->peer_), top_thread_message_id,
std::move(update->draft_));
td_->messages_manager_->on_update_dialog_draft_message(
DialogId(update->peer_), MessageId(ServerMessageId(update->top_msg_id_)), std::move(update->draft_));
promise.set_value(Unit());
}
@ -3460,8 +3446,7 @@ void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateDialogPinned> u
}
void UpdatesManager::on_update(tl_object_ptr<telegram_api::updatePinnedDialogs> update, Promise<Unit> &&promise) {
FolderId folder_id(update->flags_ & telegram_api::updatePinnedDialogs::FOLDER_ID_MASK ? update->folder_id_ : 0);
td_->messages_manager_->on_update_pinned_dialogs(folder_id); // TODO use update->order_
td_->messages_manager_->on_update_pinned_dialogs(FolderId(update->folder_id_)); // TODO use update->order_
promise.set_value(Unit());
}

View File

@ -124,9 +124,7 @@ class GetWebPageQuery final : public Td::ResultHandler {
if (ptr->get_id() == telegram_api::webPageNotModified::ID) {
if (web_page_id_.is_valid()) {
auto web_page = move_tl_object_as<telegram_api::webPageNotModified>(ptr);
int32 view_count = (web_page->flags_ & telegram_api::webPageNotModified::CACHED_PAGE_VIEWS_MASK) != 0
? web_page->cached_page_views_
: 0;
int32 view_count = web_page->cached_page_views_;
td_->web_pages_manager_->on_get_web_page_instant_view_view_count(web_page_id_, view_count);
return promise_.set_value(std::move(web_page_id_));
} else {
@ -495,24 +493,16 @@ WebPageId WebPagesManager::on_get_web_page(tl_object_ptr<telegram_api::WebPage>
page->title = std::move(web_page->title_);
page->description = std::move(web_page->description_);
page->photo = get_photo(td_->file_manager_.get(), std::move(web_page->photo_), owner_dialog_id);
if (web_page->flags_ & WEBPAGE_FLAG_HAS_EMBEDDED_PREVIEW) {
page->embed_url = std::move(web_page->embed_url_);
page->embed_type = std::move(web_page->embed_type_);
page->embed_url = std::move(web_page->embed_url_);
page->embed_type = std::move(web_page->embed_type_);
page->embed_dimensions = get_dimensions(web_page->embed_width_, web_page->embed_height_, "webPage");
page->duration = web_page->duration_;
if (page->duration < 0) {
LOG(ERROR) << "Receive wrong web page duration " << page->duration;
page->duration = 0;
}
if (web_page->flags_ & WEBPAGE_FLAG_HAS_EMBEDDED_PREVIEW_SIZE) {
page->embed_dimensions = get_dimensions(web_page->embed_width_, web_page->embed_height_, "webPage");
}
if (web_page->flags_ & WEBPAGE_FLAG_HAS_DURATION) {
page->duration = web_page->duration_;
if (page->duration < 0) {
LOG(ERROR) << "Receive wrong web page duration " << page->duration;
page->duration = 0;
}
}
if (web_page->flags_ & WEBPAGE_FLAG_HAS_AUTHOR) {
page->author = std::move(web_page->author_);
}
if (web_page->flags_ & WEBPAGE_FLAG_HAS_DOCUMENT) {
page->author = std::move(web_page->author_);
if (web_page->document_ != nullptr) {
int32 document_id = web_page->document_->get_id();
if (document_id == telegram_api::document::ID) {
auto parsed_document = td_->documents_manager_->on_get_document(
@ -535,7 +525,7 @@ WebPageId WebPagesManager::on_get_web_page(tl_object_ptr<telegram_api::WebPage>
}
// TODO attribute->settings_
}
if (web_page->flags_ & WEBPAGE_FLAG_HAS_INSTANT_VIEW) {
if (web_page->cached_page_ != nullptr) {
on_get_web_page_instant_view(page.get(), std::move(web_page->cached_page_), web_page->hash_, owner_dialog_id);
}
@ -1500,7 +1490,7 @@ void WebPagesManager::on_get_web_page_instant_view(WebPage *web_page, tl_object_
<< " photos, " << videos.size() << " videos and " << voice_notes.size() << " voice notes";
web_page->instant_view.page_blocks =
get_web_page_blocks(td_, std::move(page->blocks_), animations, audios, documents, photos, videos, voice_notes);
web_page->instant_view.view_count = (page->flags_ & telegram_api::page::VIEWS_MASK) != 0 ? page->views_ : 0;
web_page->instant_view.view_count = page->views_;
web_page->instant_view.is_v2 = page->v2_;
web_page->instant_view.is_rtl = page->rtl_;
web_page->instant_view.hash = hash;

View File

@ -91,19 +91,6 @@ class WebPagesManager final : public Actor {
int32 get_web_page_media_duration(WebPageId web_page_id) const;
private:
static constexpr int32 WEBPAGE_FLAG_HAS_TYPE = 1 << 0;
static constexpr int32 WEBPAGE_FLAG_HAS_SITE_NAME = 1 << 1;
static constexpr int32 WEBPAGE_FLAG_HAS_TITLE = 1 << 2;
static constexpr int32 WEBPAGE_FLAG_HAS_DESCRIPTION = 1 << 3;
static constexpr int32 WEBPAGE_FLAG_HAS_PHOTO = 1 << 4;
static constexpr int32 WEBPAGE_FLAG_HAS_EMBEDDED_PREVIEW = 1 << 5;
static constexpr int32 WEBPAGE_FLAG_HAS_EMBEDDED_PREVIEW_SIZE = 1 << 6;
static constexpr int32 WEBPAGE_FLAG_HAS_DURATION = 1 << 7;
static constexpr int32 WEBPAGE_FLAG_HAS_AUTHOR = 1 << 8;
static constexpr int32 WEBPAGE_FLAG_HAS_DOCUMENT = 1 << 9;
static constexpr int32 WEBPAGE_FLAG_HAS_INSTANT_VIEW = 1 << 10;
static constexpr int32 WEBPAGE_FLAG_HAS_DOCUMENTS = 1 << 11;
class WebPage;
class WebPageInstantView;