void PhoneWidget::submit() { if (_sentRequest || isHidden()) return; const auto phone = fullNumber(); if (!AllowPhoneAttempt(phone)) { showPhoneError(langFactory(lng_bad_phone)); _phone->setFocus(); return; } hidePhoneError(); _checkRequest->start(1000); _sentPhone = phone; Core::App().mtp()->setUserPhone(_sentPhone); //_sentRequest = MTP::send(MTPauth_CheckPhone(MTP_string(_sentPhone)), rpcDone(&PhoneWidget::phoneCheckDone), rpcFail(&PhoneWidget::phoneSubmitFail)); _sentRequest = MTP::send( MTPauth_SendCode( MTP_string(_sentPhone), MTP_int(ApiId), MTP_string(ApiHash), MTP_codeSettings( MTP_flags(0), MTPstring())), rpcDone(&PhoneWidget::phoneSubmitDone), rpcFail(&PhoneWidget::phoneSubmitFail)); }
void PhoneWidget::submit() { if (_sentRequest || isHidden()) return; if (!App::isValidPhone(fullNumber())) { showPhoneError(langFactory(lng_bad_phone)); _phone->setFocus(); return; } hidePhoneError(); _checkRequest->start(1000); _sentPhone = fullNumber(); Messenger::Instance().mtp()->setUserPhone(_sentPhone); //_sentRequest = MTP::send(MTPauth_CheckPhone(MTP_string(_sentPhone)), rpcDone(&PhoneWidget::phoneCheckDone), rpcFail(&PhoneWidget::phoneSubmitFail)); _sentRequest = MTP::send( MTPauth_SendCode( MTP_flags(0), MTP_string(_sentPhone), MTPBool(), MTP_int(ApiId), MTP_string(ApiHash)), rpcDone(&PhoneWidget::phoneSubmitDone), rpcFail(&PhoneWidget::phoneSubmitFail)); }
// For testing: Just apply random subset or your sticker sets as archived. bool ApplyArchivedResultFake() { auto sets = QVector<MTPStickerSetCovered>(); for (auto &set : Auth().data().stickerSetsRef()) { if ((set.flags & MTPDstickerSet::Flag::f_installed_date) && !(set.flags & MTPDstickerSet_ClientFlag::f_special)) { if (rand_value<uint32>() % 128 < 64) { const auto data = MTP_stickerSet( MTP_flags(set.flags | MTPDstickerSet::Flag::f_archived), MTP_int(set.installDate), MTP_long(set.id), MTP_long(set.access), MTP_string(set.title), MTP_string(set.shortName), MTP_photoSizeEmpty(MTP_string(QString())), MTP_int(0), MTP_int(set.count), MTP_int(set.hash)); sets.push_back(MTP_stickerSetCovered( data, MTP_documentEmpty(MTP_long(0)))); } } } if (sets.size() > 3) { sets = sets.mid(0, 3); } auto fakeResult = MTP_messages_stickerSetInstallResultArchive( MTP_vector<MTPStickerSetCovered>(sets)); ApplyArchivedResult(fakeResult.c_messages_stickerSetInstallResultArchive()); return true; }
void ConfirmPhoneBox::checkPhoneAndHash() { if (_sendCodeRequestId) { return; } MTPaccount_SendConfirmPhoneCode::Flags flags = 0; _sendCodeRequestId = MTP::send(MTPaccount_SendConfirmPhoneCode(MTP_flags(flags), MTP_string(_hash), MTPBool()), rpcDone(&ConfirmPhoneBox::sendCodeDone), rpcFail(&ConfirmPhoneBox::sendCodeFail)); }
void PhoneWidget::toSignUp() { hideError(); // Hide error, but leave the signup label visible. _checkRequest->start(1000); _sentRequest = MTP::send(MTPauth_SendCode(MTP_flags(0), MTP_string(_sentPhone), MTPBool(), MTP_int(ApiId), MTP_string(ApiHash)), rpcDone(&PhoneWidget::phoneSubmitDone), rpcFail(&PhoneWidget::phoneSubmitFail)); }
void PinMessageBox::onPin() { if (_requestId) return; MTPchannels_UpdatePinnedMessage::Flags flags = 0; if (!_notify.checked()) { flags |= MTPchannels_UpdatePinnedMessage::Flag::f_silent; } _requestId = MTP::send(MTPchannels_UpdatePinnedMessage(MTP_flags(flags), _channel->inputChannel, MTP_int(_msgId)), rpcDone(&PinMessageBox::pinDone), rpcFail(&PinMessageBox::pinFail)); }
void IntroPhone::toSignUp() { disableAll(); showError(QString()); checkRequest.start(1000); MTPauth_SendCode::Flags flags = 0; sentRequest = MTP::send(MTPauth_SendCode(MTP_flags(flags), MTP_string(sentPhone), MTPBool(), MTP_int(ApiId), MTP_string(ApiHash), MTP_string(Sandbox::LangSystemISO())), rpcDone(&IntroPhone::phoneSubmitDone), rpcFail(&IntroPhone::phoneSubmitFail)); }
void SendDataCommon::addToHistory(const Result *owner, History *history, MTPDmessage::Flags flags, MsgId msgId, UserId fromId, MTPint mtpDate, UserId viaBotId, MsgId replyToId, const MTPReplyMarkup &markup) const { SentMTPMessageFields fields = getSentMessageFields(); if (!fields.entities.c_vector().v.isEmpty()) { flags |= MTPDmessage::Flag::f_entities; } history->addNewMessage(MTP_message(MTP_flags(flags), MTP_int(msgId), MTP_int(fromId), peerToMTP(history->peer->id), MTPnullFwdHeader, MTP_int(viaBotId), MTP_int(replyToId), mtpDate, fields.text, fields.media, markup, fields.entities, MTP_int(1), MTPint()), NewMessageUnread); }
void ChangePhoneBox::EnterPhone::submit() { if (_requestId) { return; } hideError(); auto phoneNumber = _phone->getLastText().trimmed(); _requestId = MTP::send(MTPaccount_SendChangePhoneCode(MTP_flags(0), MTP_string(phoneNumber), MTP_bool(false)), rpcDone(crl::guard(this, [this, phoneNumber](const MTPauth_SentCode &result) { return sendPhoneDone(phoneNumber, result); })), rpcFail(crl::guard(this, [this, phoneNumber](const RPCError &error) { return sendPhoneFail(phoneNumber, error); }))); }
void PhoneWidget::phoneCheckDone(const MTPauth_CheckedPhone &result) { stopCheck(); auto &d = result.c_auth_checkedPhone(); if (mtpIsTrue(d.vphone_registered)) { hidePhoneError(); _checkRequest->start(1000); _sentRequest = MTP::send(MTPauth_SendCode(MTP_flags(0), MTP_string(_sentPhone), MTPBool(), MTP_int(ApiId), MTP_string(ApiHash)), rpcDone(&PhoneWidget::phoneSubmitDone), rpcFail(&PhoneWidget::phoneSubmitFail)); } else { showSignup(); _sentRequest = 0; } }
bool DcOptions::loadFromFile(const QString &path) { QVector<MTPDcOption> options; QFile f(path); if (!f.open(QIODevice::ReadOnly)) { LOG(("MTP Error: could not read '%1'").arg(path)); return false; } QTextStream stream(&f); stream.setCodec("UTF-8"); while (!stream.atEnd()) { auto line = stream.readLine(); auto components = line.split(QRegularExpression(R"(\s)"), QString::SkipEmptyParts); if (components.isEmpty() || components[0].startsWith('#')) { continue; } auto error = [line] { LOG(("MTP Error: in .tdesktop-endpoints expected 'dcId host port [tcpo_only] [media_only]', got '%1'").arg(line)); return false; }; if (components.size() < 3) { return error(); } auto dcId = components[0].toInt(); auto ip = components[1]; auto port = components[2].toInt(); auto host = QHostAddress(); if (dcId <= 0 || dcId >= internal::kDcShift || !host.setAddress(ip) || port <= 0) { return error(); } auto flags = MTPDdcOption::Flags(0); if (host.protocol() == QAbstractSocket::IPv6Protocol) { flags |= MTPDdcOption::Flag::f_ipv6; } for (auto &option : components.mid(3)) { if (option.startsWith('#')) { break; } else if (option == qstr("tcpo_only")) { flags |= MTPDdcOption::Flag::f_tcpo_only; } else if (option == qstr("media_only")) { flags |= MTPDdcOption::Flag::f_media_only; } else { return error(); } } options.push_back(MTP_dcOption(MTP_flags(flags), MTP_int(dcId), MTP_string(ip), MTP_int(port))); }
void IntroPhone::phoneCheckDone(const MTPauth_CheckedPhone &result) { stopCheck(); const auto &d(result.c_auth_checkedPhone()); if (mtpIsTrue(d.vphone_registered)) { disableAll(); showError(QString()); checkRequest.start(1000); MTPauth_SendCode::Flags flags = 0; sentRequest = MTP::send(MTPauth_SendCode(MTP_flags(flags), MTP_string(sentPhone), MTPBool(), MTP_int(ApiId), MTP_string(ApiHash), MTP_string(Sandbox::LangSystemISO())), rpcDone(&IntroPhone::phoneSubmitDone), rpcFail(&IntroPhone::phoneSubmitFail)); } else { showError(lang(lng_bad_phone_noreg), true); enableAll(true); sentRequest = 0; } }
void Result::createDocument() { if (_document) return; if (!_thumb_url.isEmpty()) { _thumb = ImagePtr(_thumb_url, QSize(90, 90)); } QString mime = _content_type; QVector<MTPDocumentAttribute> attributes; QSize dimensions(_width, _height); if (_type == Type::Gif) { const char *filename = (mime == qstr("video/mp4") ? "animation.gif.mp4" : "animation.gif"); attributes.push_back(MTP_documentAttributeFilename(MTP_string(filename))); attributes.push_back(MTP_documentAttributeAnimated()); attributes.push_back(MTP_documentAttributeVideo(MTP_int(_duration), MTP_int(_width), MTP_int(_height))); } else if (_type == Type::Video) { attributes.push_back(MTP_documentAttributeVideo(MTP_int(_duration), MTP_int(_width), MTP_int(_height))); } else if (_type == Type::Audio) { MTPDdocumentAttributeAudio::Flags flags = 0; if (mime == qstr("audio/ogg")) { flags |= MTPDdocumentAttributeAudio::Flag::f_voice; } else { QStringList p = mimeTypeForName(mime).globPatterns(); QString pattern = p.isEmpty() ? QString() : p.front(); QString extension = pattern.isEmpty() ? qsl(".unknown") : pattern.replace('*', QString()); QString filename = filedialogDefaultName(qsl("inline"), extension, QString(), true); attributes.push_back(MTP_documentAttributeFilename(MTP_string(filename))); } attributes.push_back(MTP_documentAttributeAudio(MTP_flags(flags), MTP_int(_duration), MTPstring(), MTPstring(), MTPbytes())); } auto documentId = rand_value<DocumentId>(); _document = App::documentSet(documentId, nullptr, 0, 0, unixtime(), attributes, mime, _thumb, MTP::maindc(), 0, StorageImageLocation()); _document->setContentUrl(_content_url); }
void FastShareMessage(not_null<HistoryItem*> item) { struct ShareData { ShareData(not_null<PeerData*> peer, MessageIdsList &&ids) : peer(peer) , msgIds(std::move(ids)) { } not_null<PeerData*> peer; MessageIdsList msgIds; base::flat_set<mtpRequestId> requests; }; const auto data = std::make_shared<ShareData>( item->history()->peer, Auth().data().itemOrItsGroup(item)); const auto isGroup = (Auth().data().groups().find(item) != nullptr); const auto isGame = item->getMessageBot() && item->media() && (item->media()->game() != nullptr); const auto canCopyLink = item->hasDirectLink() || isGame; auto copyCallback = [data]() { if (auto main = App::main()) { if (auto item = App::histItemById(data->msgIds[0])) { if (item->hasDirectLink()) { QApplication::clipboard()->setText(item->directLink()); Ui::Toast::Show(lang(lng_channel_public_link_copied)); } else if (const auto bot = item->getMessageBot()) { if (const auto media = item->media()) { if (const auto game = media->game()) { const auto link = Messenger::Instance().createInternalLinkFull( bot->username + qsl("?game=") + game->shortName); QApplication::clipboard()->setText(link); Ui::Toast::Show(lang(lng_share_game_link_copied)); } } } } } }; auto submitCallback = [data, isGroup](const QVector<PeerData*> &result) { if (!data->requests.empty()) { return; // Share clicked already. } auto items = Auth().data().idsToItems(data->msgIds); if (items.empty() || result.empty()) { return; } auto restrictedSomewhere = false; auto restrictedEverywhere = true; auto firstError = QString(); for (const auto peer : result) { const auto error = GetErrorTextForForward(peer, items); if (!error.isEmpty()) { if (firstError.isEmpty()) { firstError = error; } restrictedSomewhere = true; continue; } restrictedEverywhere = false; } if (restrictedEverywhere) { Ui::show( Box<InformBox>(firstError), LayerOption::KeepOther); return; } auto doneCallback = [data](const MTPUpdates &updates, mtpRequestId requestId) { if (auto main = App::main()) { main->sentUpdatesReceived(updates); } data->requests.remove(requestId); if (data->requests.empty()) { Ui::Toast::Show(lang(lng_share_done)); Ui::hideLayer(); } }; const auto sendFlags = MTPmessages_ForwardMessages::Flag(0) | MTPmessages_ForwardMessages::Flag::f_with_my_score | (isGroup ? MTPmessages_ForwardMessages::Flag::f_grouped : MTPmessages_ForwardMessages::Flag(0)); auto msgIds = QVector<MTPint>(); msgIds.reserve(data->msgIds.size()); for (const auto fullId : data->msgIds) { msgIds.push_back(MTP_int(fullId.msg)); } auto generateRandom = [&] { auto result = QVector<MTPlong>(data->msgIds.size()); for (auto &value : result) { value = rand_value<MTPlong>(); } return result; }; if (auto main = App::main()) { for (const auto peer : result) { if (!GetErrorTextForForward(peer, items).isEmpty()) { continue; } auto request = MTPmessages_ForwardMessages( MTP_flags(sendFlags), data->peer->input, MTP_vector<MTPint>(msgIds), MTP_vector<MTPlong>(generateRandom()), peer->input); auto callback = doneCallback; auto requestId = MTP::send(request, rpcDone(std::move(callback))); data->requests.insert(requestId); } } }; auto filterCallback = [isGame](PeerData *peer) { if (peer->canWrite()) { if (auto channel = peer->asChannel()) { return isGame ? (!channel->isBroadcast()) : true; } return true; } return false; }; auto copyLinkCallback = canCopyLink ? base::lambda<void()>(std::move(copyCallback)) : base::lambda<void()>(); Ui::show(Box<ShareBox>( std::move(copyLinkCallback), std::move(submitCallback), std::move(filterCallback))); }
void FileLoadTask::process() { const QString stickerMime = qsl("image/webp"); _result = MakeShared<FileLoadResult>(_id, _to, _caption); QString filename, filemime; qint64 filesize = 0; QByteArray filedata; uint64 thumbId = 0; QString thumbname = "thumb.jpg"; QByteArray thumbdata; auto animated = false; auto song = false; auto gif = false; auto voice = (_type == SendMediaType::Audio); auto fullimage = base::take(_image); auto info = _filepath.isEmpty() ? QFileInfo() : QFileInfo(_filepath); if (info.exists()) { if (info.isDir()) { _result->filesize = -1; return; } filesize = info.size(); filemime = mimeTypeForFile(info).name(); filename = info.fileName(); auto opaque = (filemime != stickerMime); fullimage = App::readImage(_filepath, 0, opaque, &animated); } else if (!_content.isEmpty()) { filesize = _content.size(); if (voice) { filename = filedialogDefaultName(qsl("audio"), qsl(".ogg"), QString(), true); filemime = "audio/ogg"; } else { auto mimeType = mimeTypeForData(_content); filemime = mimeType.name(); if (filemime != stickerMime) { fullimage = Images::prepareOpaque(std_::move(fullimage)); } if (filemime == "image/jpeg") { filename = filedialogDefaultName(qsl("photo"), qsl(".jpg"), QString(), true); } else if (filemime == "image/png") { filename = filedialogDefaultName(qsl("image"), qsl(".png"), QString(), true); } else { QString ext; QStringList patterns = mimeType.globPatterns(); if (!patterns.isEmpty()) { ext = patterns.front().replace('*', QString()); } filename = filedialogDefaultName(qsl("file"), ext, QString(), true); } } } else if (!fullimage.isNull() && fullimage.width() > 0) { if (_type == SendMediaType::Photo) { auto w = fullimage.width(), h = fullimage.height(); if (w >= 20 * h || h >= 20 * w) { _type = SendMediaType::File; } else { filesize = -1; // Fill later. filemime = mimeTypeForName("image/jpeg").name(); filename = filedialogDefaultName(qsl("image"), qsl(".jpg"), QString(), true); } } if (_type == SendMediaType::File) { filemime = mimeTypeForName("image/png").name(); filename = filedialogDefaultName(qsl("image"), qsl(".png"), QString(), true); { QBuffer buffer(&_content); fullimage.save(&buffer, "PNG"); } filesize = _content.size(); } fullimage = Images::prepareOpaque(std_::move(fullimage)); } _result->filesize = (int32)qMin(filesize, qint64(INT_MAX)); if (!filesize || filesize > App::kFileSizeLimit) { return; } PreparedPhotoThumbs photoThumbs; QVector<MTPPhotoSize> photoSizes; QPixmap thumb; QVector<MTPDocumentAttribute> attributes(1, MTP_documentAttributeFilename(MTP_string(filename))); MTPPhotoSize thumbSize(MTP_photoSizeEmpty(MTP_string(""))); MTPPhoto photo(MTP_photoEmpty(MTP_long(0))); MTPDocument document(MTP_documentEmpty(MTP_long(0))); if (!voice) { if (filemime == qstr("audio/mp3") || filemime == qstr("audio/m4a") || filemime == qstr("audio/aac") || filemime == qstr("audio/ogg") || filemime == qstr("audio/flac") || filename.endsWith(qstr(".mp3"), Qt::CaseInsensitive) || filename.endsWith(qstr(".m4a"), Qt::CaseInsensitive) || filename.endsWith(qstr(".aac"), Qt::CaseInsensitive) || filename.endsWith(qstr(".ogg"), Qt::CaseInsensitive) || filename.endsWith(qstr(".flac"), Qt::CaseInsensitive)) { QImage cover; QByteArray coverBytes, coverFormat; MTPDocumentAttribute audioAttribute = audioReadSongAttributes(_filepath, _content, cover, coverBytes, coverFormat); if (audioAttribute.type() == mtpc_documentAttributeAudio) { attributes.push_back(audioAttribute); song = true; if (!cover.isNull()) { // cover to thumb int32 cw = cover.width(), ch = cover.height(); if (cw < 20 * ch && ch < 20 * cw) { QPixmap full = (cw > 90 || ch > 90) ? App::pixmapFromImageInPlace(cover.scaled(90, 90, Qt::KeepAspectRatio, Qt::SmoothTransformation)) : App::pixmapFromImageInPlace(std_::move(cover)); { QByteArray thumbFormat = "JPG"; int32 thumbQuality = 87; QBuffer buffer(&thumbdata); full.save(&buffer, thumbFormat, thumbQuality); } thumb = full; thumbSize = MTP_photoSize(MTP_string(""), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(full.width()), MTP_int(full.height()), MTP_int(0)); thumbId = rand_value<uint64>(); } } } } if (filemime == qstr("video/mp4") || filename.endsWith(qstr(".mp4"), Qt::CaseInsensitive) || animated) { QImage cover; MTPDocumentAttribute animatedAttribute = Media::Clip::readAttributes(_filepath, _content, cover); if (animatedAttribute.type() == mtpc_documentAttributeVideo) { int32 cw = cover.width(), ch = cover.height(); if (cw < 20 * ch && ch < 20 * cw) { attributes.push_back(MTP_documentAttributeAnimated()); attributes.push_back(animatedAttribute); gif = true; QPixmap full = (cw > 90 || ch > 90) ? App::pixmapFromImageInPlace(cover.scaled(90, 90, Qt::KeepAspectRatio, Qt::SmoothTransformation)) : App::pixmapFromImageInPlace(std_::move(cover)); { QByteArray thumbFormat = "JPG"; int32 thumbQuality = 87; QBuffer buffer(&thumbdata); full.save(&buffer, thumbFormat, thumbQuality); } thumb = full; thumbSize = MTP_photoSize(MTP_string(""), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(full.width()), MTP_int(full.height()), MTP_int(0)); thumbId = rand_value<uint64>(); if (filename.endsWith(qstr(".mp4"), Qt::CaseInsensitive)) { filemime = qstr("video/mp4"); } } } } } if (!fullimage.isNull() && fullimage.width() > 0 && !song && !gif && !voice) { auto w = fullimage.width(), h = fullimage.height(); attributes.push_back(MTP_documentAttributeImageSize(MTP_int(w), MTP_int(h))); if (w < 20 * h && h < 20 * w) { if (animated) { attributes.push_back(MTP_documentAttributeAnimated()); } else if (_type != SendMediaType::File) { auto thumb = (w > 100 || h > 100) ? App::pixmapFromImageInPlace(fullimage.scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation)) : QPixmap::fromImage(fullimage); photoThumbs.insert('s', thumb); photoSizes.push_back(MTP_photoSize(MTP_string("s"), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(thumb.width()), MTP_int(thumb.height()), MTP_int(0))); auto medium = (w > 320 || h > 320) ? App::pixmapFromImageInPlace(fullimage.scaled(320, 320, Qt::KeepAspectRatio, Qt::SmoothTransformation)) : QPixmap::fromImage(fullimage); photoThumbs.insert('m', medium); photoSizes.push_back(MTP_photoSize(MTP_string("m"), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(medium.width()), MTP_int(medium.height()), MTP_int(0))); auto full = (w > 1280 || h > 1280) ? App::pixmapFromImageInPlace(fullimage.scaled(1280, 1280, Qt::KeepAspectRatio, Qt::SmoothTransformation)) : QPixmap::fromImage(fullimage); photoThumbs.insert('y', full); photoSizes.push_back(MTP_photoSize(MTP_string("y"), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(full.width()), MTP_int(full.height()), MTP_int(0))); { QBuffer buffer(&filedata); full.save(&buffer, "JPG", 87); } MTPDphoto::Flags photoFlags = 0; photo = MTP_photo(MTP_flags(photoFlags), MTP_long(_id), MTP_long(0), MTP_int(unixtime()), MTP_vector<MTPPhotoSize>(photoSizes)); if (filesize < 0) { filesize = _result->filesize = filedata.size(); } } QByteArray thumbFormat = "JPG"; int32 thumbQuality = 87; if (!animated && filemime == stickerMime && w > 0 && h > 0 && w <= StickerMaxSize && h <= StickerMaxSize && filesize < StickerInMemory) { MTPDdocumentAttributeSticker::Flags stickerFlags = 0; attributes.push_back(MTP_documentAttributeSticker(MTP_flags(stickerFlags), MTP_string(""), MTP_inputStickerSetEmpty(), MTPMaskCoords())); thumbFormat = "webp"; thumbname = qsl("thumb.webp"); } QPixmap full = (w > 90 || h > 90) ? App::pixmapFromImageInPlace(fullimage.scaled(90, 90, Qt::KeepAspectRatio, Qt::SmoothTransformation)) : QPixmap::fromImage(fullimage, Qt::ColorOnly); { QBuffer buffer(&thumbdata); full.save(&buffer, thumbFormat, thumbQuality); } thumb = full; thumbSize = MTP_photoSize(MTP_string(""), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(full.width()), MTP_int(full.height()), MTP_int(0)); thumbId = rand_value<uint64>(); } } if (voice) { attributes[0] = MTP_documentAttributeAudio(MTP_flags(MTPDdocumentAttributeAudio::Flag::f_voice | MTPDdocumentAttributeAudio::Flag::f_waveform), MTP_int(_duration), MTPstring(), MTPstring(), MTP_bytes(documentWaveformEncode5bit(_waveform))); attributes.resize(1); document = MTP_document(MTP_long(_id), MTP_long(0), MTP_int(unixtime()), MTP_string(filemime), MTP_int(filesize), thumbSize, MTP_int(MTP::maindc()), MTP_int(0), MTP_vector<MTPDocumentAttribute>(attributes)); } else if (_type != SendMediaType::Photo) { document = MTP_document(MTP_long(_id), MTP_long(0), MTP_int(unixtime()), MTP_string(filemime), MTP_int(filesize), thumbSize, MTP_int(MTP::maindc()), MTP_int(0), MTP_vector<MTPDocumentAttribute>(attributes)); _type = SendMediaType::File; } _result->type = _type; _result->filepath = _filepath; _result->content = _content; _result->filename = filename; _result->filemime = filemime; _result->setFileData(filedata); _result->thumbId = thumbId; _result->thumbname = thumbname; _result->setThumbData(thumbdata); _result->thumb = thumb; _result->photo = photo; _result->document = document; _result->photoThumbs = photoThumbs; }
void FileLoadTask::process() { const QString stickerMime = qsl("image/webp"); _result = FileLoadResultPtr(new FileLoadResult(_id, _to, _originalText)); QString filename, filemime; qint64 filesize = 0; QByteArray filedata; uint64 thumbId = 0; QString thumbname = "thumb.jpg"; QByteArray thumbdata; bool animated = false, song = false, gif = false, voice = (_type == PrepareAudio); QImage fullimage = _image; if (!_filepath.isEmpty()) { QFileInfo info(_filepath); if (info.isDir()) { _result->filesize = -1; return; } filesize = info.size(); filemime = mimeTypeForFile(info).name(); filename = info.fileName(); if (filesize <= MaxUploadPhotoSize && !voice) { bool opaque = (filemime != stickerMime); fullimage = App::readImage(_filepath, 0, opaque, &animated); } } else if (!_content.isEmpty()) { filesize = _content.size(); if (voice) { filename = filedialogDefaultName(qsl("audio"), qsl(".ogg"), QString(), true); filemime = "audio/ogg"; } else { MimeType mimeType = mimeTypeForData(_content); filemime = mimeType.name(); if (filesize <= MaxUploadPhotoSize && !voice) { bool opaque = (filemime != stickerMime); fullimage = App::readImage(_content, 0, opaque, &animated); } if (filemime == "image/jpeg") { filename = filedialogDefaultName(qsl("image"), qsl(".jpg"), QString(), true); } else { QString ext; QStringList patterns = mimeType.globPatterns(); if (!patterns.isEmpty()) { ext = patterns.front().replace('*', QString()); } filename = filedialogDefaultName(qsl("file"), ext, QString(), true); } } } else if (!_image.isNull()) { _image = QImage(); filemime = mimeTypeForName("image/png").name(); filename = filedialogDefaultName(qsl("image"), qsl(".png"), QString(), true); { QBuffer buffer(&_content); fullimage.save(&buffer, "PNG"); } filesize = _content.size(); if (fullimage.hasAlphaChannel()) { QImage solid(fullimage.width(), fullimage.height(), QImage::Format_ARGB32_Premultiplied); solid.fill(st::white->c); { QPainter(&solid).drawImage(0, 0, fullimage); } fullimage = solid; } } _result->filesize = (int32)qMin(filesize, qint64(INT_MAX)); if (!filesize || filesize > MaxUploadDocumentSize) { return; } PreparedPhotoThumbs photoThumbs; QVector<MTPPhotoSize> photoSizes; QPixmap thumb; QVector<MTPDocumentAttribute> attributes(1, MTP_documentAttributeFilename(MTP_string(filename))); MTPPhotoSize thumbSize(MTP_photoSizeEmpty(MTP_string(""))); MTPPhoto photo(MTP_photoEmpty(MTP_long(0))); MTPDocument document(MTP_documentEmpty(MTP_long(0))); if (!voice) { if (filemime == qstr("audio/mp3") || filemime == qstr("audio/m4a") || filemime == qstr("audio/aac") || filemime == qstr("audio/ogg") || filemime == qstr("audio/flac") || filename.endsWith(qstr(".mp3"), Qt::CaseInsensitive) || filename.endsWith(qstr(".m4a"), Qt::CaseInsensitive) || filename.endsWith(qstr(".aac"), Qt::CaseInsensitive) || filename.endsWith(qstr(".ogg"), Qt::CaseInsensitive) || filename.endsWith(qstr(".flac"), Qt::CaseInsensitive)) { QImage cover; QByteArray coverBytes, coverFormat; MTPDocumentAttribute audioAttribute = audioReadSongAttributes(_filepath, _content, cover, coverBytes, coverFormat); if (audioAttribute.type() == mtpc_documentAttributeAudio) { attributes.push_back(audioAttribute); song = true; if (!cover.isNull()) { // cover to thumb int32 cw = cover.width(), ch = cover.height(); if (cw < 20 * ch && ch < 20 * cw) { QPixmap full = (cw > 90 || ch > 90) ? QPixmap::fromImage(cover.scaled(90, 90, Qt::KeepAspectRatio, Qt::SmoothTransformation), Qt::ColorOnly) : QPixmap::fromImage(cover, Qt::ColorOnly); { QByteArray thumbFormat = "JPG"; int32 thumbQuality = 87; QBuffer buffer(&thumbdata); full.save(&buffer, thumbFormat, thumbQuality); } thumb = full; thumbSize = MTP_photoSize(MTP_string(""), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(full.width()), MTP_int(full.height()), MTP_int(0)); thumbId = rand_value<uint64>(); } } } } if (filemime == qstr("video/mp4") || filename.endsWith(qstr(".mp4"), Qt::CaseInsensitive) || animated) { QImage cover; MTPDocumentAttribute animatedAttribute = clipReadAnimatedAttributes(_filepath, _content, cover); if (animatedAttribute.type() == mtpc_documentAttributeVideo) { int32 cw = cover.width(), ch = cover.height(); if (cw < 20 * ch && ch < 20 * cw) { attributes.push_back(MTP_documentAttributeAnimated()); attributes.push_back(animatedAttribute); gif = true; QPixmap full = (cw > 90 || ch > 90) ? QPixmap::fromImage(cover.scaled(90, 90, Qt::KeepAspectRatio, Qt::SmoothTransformation), Qt::ColorOnly) : QPixmap::fromImage(cover, Qt::ColorOnly); { QByteArray thumbFormat = "JPG"; int32 thumbQuality = 87; QBuffer buffer(&thumbdata); full.save(&buffer, thumbFormat, thumbQuality); } thumb = full; thumbSize = MTP_photoSize(MTP_string(""), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(full.width()), MTP_int(full.height()), MTP_int(0)); thumbId = rand_value<uint64>(); if (filename.endsWith(qstr(".mp4"), Qt::CaseInsensitive)) { filemime = qstr("video/mp4"); } } } } } if (!fullimage.isNull() && fullimage.width() > 0 && !song && !gif && !voice) { int32 w = fullimage.width(), h = fullimage.height(); attributes.push_back(MTP_documentAttributeImageSize(MTP_int(w), MTP_int(h))); if (w < 20 * h && h < 20 * w) { if (animated) { attributes.push_back(MTP_documentAttributeAnimated()); } else if (_type != PrepareDocument) { QPixmap thumb = (w > 100 || h > 100) ? QPixmap::fromImage(fullimage.scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation), Qt::ColorOnly) : QPixmap::fromImage(fullimage); photoThumbs.insert('s', thumb); photoSizes.push_back(MTP_photoSize(MTP_string("s"), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(thumb.width()), MTP_int(thumb.height()), MTP_int(0))); QPixmap medium = (w > 320 || h > 320) ? QPixmap::fromImage(fullimage.scaled(320, 320, Qt::KeepAspectRatio, Qt::SmoothTransformation), Qt::ColorOnly) : QPixmap::fromImage(fullimage); photoThumbs.insert('m', medium); photoSizes.push_back(MTP_photoSize(MTP_string("m"), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(medium.width()), MTP_int(medium.height()), MTP_int(0))); QPixmap full = (w > 1280 || h > 1280) ? QPixmap::fromImage(fullimage.scaled(1280, 1280, Qt::KeepAspectRatio, Qt::SmoothTransformation), Qt::ColorOnly) : QPixmap::fromImage(fullimage); photoThumbs.insert('y', full); photoSizes.push_back(MTP_photoSize(MTP_string("y"), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(full.width()), MTP_int(full.height()), MTP_int(0))); { QBuffer buffer(&filedata); full.save(&buffer, "JPG", 77); } photo = MTP_photo(MTP_long(_id), MTP_long(0), MTP_int(unixtime()), MTP_vector<MTPPhotoSize>(photoSizes)); } QByteArray thumbFormat = "JPG"; int32 thumbQuality = 87; if (!animated && filemime == stickerMime && w > 0 && h > 0 && w <= StickerMaxSize && h <= StickerMaxSize && filesize < StickerInMemory) { attributes.push_back(MTP_documentAttributeSticker(MTP_string(""), MTP_inputStickerSetEmpty())); thumbFormat = "webp"; thumbname = qsl("thumb.webp"); } QPixmap full = (w > 90 || h > 90) ? QPixmap::fromImage(fullimage.scaled(90, 90, Qt::KeepAspectRatio, Qt::SmoothTransformation), Qt::ColorOnly) : QPixmap::fromImage(fullimage, Qt::ColorOnly); { QBuffer buffer(&thumbdata); full.save(&buffer, thumbFormat, thumbQuality); } thumb = full; thumbSize = MTP_photoSize(MTP_string(""), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(full.width()), MTP_int(full.height()), MTP_int(0)); thumbId = rand_value<uint64>(); } } if (voice) { attributes[0] = MTP_documentAttributeAudio(MTP_flags(MTPDdocumentAttributeAudio::Flag::f_voice | MTPDdocumentAttributeAudio::Flag::f_waveform), MTP_int(_duration), MTPstring(), MTPstring(), MTP_bytes(documentWaveformEncode5bit(_waveform))); attributes.resize(1); document = MTP_document(MTP_long(_id), MTP_long(0), MTP_int(unixtime()), MTP_string(filemime), MTP_int(filesize), thumbSize, MTP_int(MTP::maindc()), MTP_vector<MTPDocumentAttribute>(attributes)); } else { document = MTP_document(MTP_long(_id), MTP_long(0), MTP_int(unixtime()), MTP_string(filemime), MTP_int(filesize), thumbSize, MTP_int(MTP::maindc()), MTP_vector<MTPDocumentAttribute>(attributes)); if (photo.type() == mtpc_photoEmpty) { _type = PrepareDocument; } } _result->type = _type; _result->filepath = _filepath; _result->content = _content; _result->filename = filename; _result->filemime = filemime; _result->setFileData(filedata); _result->thumbId = thumbId; _result->thumbname = thumbname; _result->setThumbData(thumbdata); _result->thumb = thumb; _result->photo = photo; _result->document = document; _result->photoThumbs = photoThumbs; }
MTPmessages_Search PrepareSearchRequest( not_null<PeerData*> peer, Storage::SharedMediaType type, const QString &query, MsgId messageId, SparseIdsLoadDirection direction) { const auto filter = [&] { using Type = Storage::SharedMediaType; switch (type) { case Type::Photo: return MTP_inputMessagesFilterPhotos(); case Type::Video: return MTP_inputMessagesFilterVideo(); case Type::PhotoVideo: return MTP_inputMessagesFilterPhotoVideo(); case Type::MusicFile: return MTP_inputMessagesFilterMusic(); case Type::File: return MTP_inputMessagesFilterDocument(); case Type::VoiceFile: return MTP_inputMessagesFilterVoice(); case Type::RoundVoiceFile: return MTP_inputMessagesFilterRoundVoice(); case Type::RoundFile: return MTP_inputMessagesFilterRoundVideo(); case Type::GIF: return MTP_inputMessagesFilterGif(); case Type::Link: return MTP_inputMessagesFilterUrl(); case Type::ChatPhoto: return MTP_inputMessagesFilterChatPhotos(); } return MTP_inputMessagesFilterEmpty(); }(); const auto minId = 0; const auto maxId = 0; const auto limit = messageId ? kSharedMediaLimit : 0; const auto offsetId = [&] { switch (direction) { case SparseIdsLoadDirection::Before: case SparseIdsLoadDirection::Around: return messageId; case SparseIdsLoadDirection::After: return messageId + 1; } Unexpected("Direction in PrepareSearchRequest"); }(); const auto addOffset = [&] { switch (direction) { case SparseIdsLoadDirection::Before: return 0; case SparseIdsLoadDirection::Around: return -limit / 2; case SparseIdsLoadDirection::After: return -limit; } Unexpected("Direction in PrepareSearchRequest"); }(); return MTPmessages_Search( MTP_flags(0), peer->input, MTP_string(query), MTP_inputUserEmpty(), filter, MTP_int(0), MTP_int(0), MTP_int(offsetId), MTP_int(addOffset), MTP_int(limit), MTP_int(maxId), MTP_int(minId)); }
MTPVector<MTPDocumentAttribute> Result::adjustAttributes( const MTPVector<MTPDocumentAttribute> &existing, const MTPstring &mimeType) { auto result = existing.v; const auto find = [&](mtpTypeId attributeType) { return ranges::find( result, attributeType, [](const MTPDocumentAttribute &value) { return value.type(); }); }; const auto exists = [&](mtpTypeId attributeType) { return find(attributeType) != result.cend(); }; const auto mime = qs(mimeType); if (_type == Type::Gif) { if (!exists(mtpc_documentAttributeFilename)) { auto filename = (mime == qstr("video/mp4") ? "animation.gif.mp4" : "animation.gif"); result.push_back(MTP_documentAttributeFilename( MTP_string(filename))); } if (!exists(mtpc_documentAttributeAnimated)) { result.push_back(MTP_documentAttributeAnimated()); } } else if (_type == Type::Audio) { const auto audio = find(mtpc_documentAttributeAudio); if (audio != result.cend()) { using Flag = MTPDdocumentAttributeAudio::Flag; if (mime == qstr("audio/ogg")) { // We always treat audio/ogg as a voice message. // It was that way before we started to get attributes here. const auto &fields = audio->c_documentAttributeAudio(); if (!(fields.vflags.v & Flag::f_voice)) { *audio = MTP_documentAttributeAudio( MTP_flags(fields.vflags.v | Flag::f_voice), fields.vduration, fields.vtitle, fields.vperformer, fields.vwaveform); } } const auto &fields = audio->c_documentAttributeAudio(); if (!exists(mtpc_documentAttributeFilename) && !(fields.vflags.v & Flag::f_voice)) { const auto p = Core::MimeTypeForName(mime).globPatterns(); auto pattern = p.isEmpty() ? QString() : p.front(); const auto extension = pattern.isEmpty() ? qsl(".unknown") : pattern.replace('*', QString()); const auto filename = filedialogDefaultName( qsl("inline"), extension, QString(), true); result.push_back( MTP_documentAttributeFilename(MTP_string(filename))); } } } return MTP_vector<MTPDocumentAttribute>(std::move(result)); }
MTPChannelBannedRights ChannelData::KickedRestrictedRights() { using Flag = MTPDchannelBannedRights::Flag; auto flags = Flag::f_view_messages | Flag::f_send_messages | Flag::f_send_media | Flag::f_embed_links | Flag::f_send_stickers | Flag::f_send_gifs | Flag::f_send_games | Flag::f_send_inline; return MTP_channelBannedRights(MTP_flags(flags), MTP_int(std::numeric_limits<int32>::max())); }