context bot resolve done

This commit is contained in:
John Preston 2015-12-29 01:06:27 +03:00
parent 77228d358d
commit 820339d390
9 changed files with 74 additions and 18 deletions

View File

@ -424,6 +424,7 @@ namespace App {
data->setBotInfoVersion(d.vbot_info_version.v); data->setBotInfoVersion(d.vbot_info_version.v);
data->botInfo->readsAllHistory = d.is_bot_chat_history(); data->botInfo->readsAllHistory = d.is_bot_chat_history();
data->botInfo->cantJoinGroups = d.is_bot_nochats(); data->botInfo->cantJoinGroups = d.is_bot_nochats();
data->botInfo->contextPlaceholder = d.has_bot_context_placeholder() ? qs(d.vbot_context_placeholder) : QString();
} else { } else {
data->setBotInfoVersion(-1); data->setBotInfoVersion(-1);
} }
@ -1430,8 +1431,8 @@ namespace App {
return ::self; return ::self;
} }
PeerData *peerByName(const QStringRef &username) { PeerData *peerByName(const QString &username) {
QStringRef uname(username.trimmed()); QString uname(username.trimmed());
for (PeersData::const_iterator i = peersData.cbegin(), e = peersData.cend(); i != e; ++i) { for (PeersData::const_iterator i = peersData.cbegin(), e = peersData.cend(); i != e; ++i) {
if (!i.value()->userName().compare(uname, Qt::CaseInsensitive)) { if (!i.value()->userName().compare(uname, Qt::CaseInsensitive)) {
return i.value()->asUser(); return i.value()->asUser();

View File

@ -132,10 +132,7 @@ namespace App {
ChatData *chat(int32 chat_id); ChatData *chat(int32 chat_id);
ChannelData *channel(int32 channel_id); ChannelData *channel(int32 channel_id);
UserData *self(); UserData *self();
PeerData *peerByName(const QStringRef &username); PeerData *peerByName(const QString &username);
inline PeerData *peerByName(const QString &username) {
return peerByName(username.midRef(0));
}
QString peerName(const PeerData *peer, bool forDialogs = false); QString peerName(const PeerData *peer, bool forDialogs = false);
PhotoData *photo(const PhotoId &photo); PhotoData *photo(const PhotoId &photo);
PhotoData *photoSet(const PhotoId &photo, PhotoData *convert, const uint64 &access, int32 date, const ImagePtr &thumb, const ImagePtr &medium, const ImagePtr &full); PhotoData *photoSet(const PhotoId &photo, PhotoData *convert, const uint64 &access, int32 date, const ImagePtr &thumb, const ImagePtr &medium, const ImagePtr &full);

View File

@ -3457,6 +3457,10 @@ void MentionsDropdown::paintEvent(QPaintEvent *e) {
} }
void MentionsDropdown::showContextResults(UserData *bot, QString query) {
}
void MentionsDropdown::showFiltered(PeerData *peer, QString start) { void MentionsDropdown::showFiltered(PeerData *peer, QString start) {
_chat = peer->asChat(); _chat = peer->asChat();
_user = peer->asUser(); _user = peer->asUser();

View File

@ -698,6 +698,7 @@ public:
void fastHide(); void fastHide();
bool clearFilteredCommands(); bool clearFilteredCommands();
void showContextResults(UserData *bot, QString query);
void showFiltered(PeerData *peer, QString start); void showFiltered(PeerData *peer, QString start);
void updateFiltered(bool toDown = false); void updateFiltered(bool toDown = false);
void setBoundings(QRect boundings); void setBoundings(QRect boundings);

View File

@ -252,7 +252,7 @@ EmojiPtr FlatTextarea::getSingleEmoji() const {
return 0; return 0;
} }
void FlatTextarea::getMentionHashtagBotCommandStart(QString &start, UserData *&contextBot, QString &lookedUpUsername) const { void FlatTextarea::getMentionHashtagBotCommandStart(QString &start, UserData *&contextBot, QString &contextBotUsername) const {
int32 pos = textCursor().position(); int32 pos = textCursor().position();
if (textCursor().anchor() != pos) return; if (textCursor().anchor() != pos) return;
@ -272,19 +272,30 @@ void FlatTextarea::getMentionHashtagBotCommandStart(QString &start, UserData *&c
break; break;
} }
if (usernameLength) { if (usernameLength) {
UserData *bot = 0; QStringRef username = text.midRef(1, usernameLength);
if (contextBot && !contextBot->username.compare(text.midRef(1, usernameLength))) { if (username != contextBotUsername) {
bot = contextBot; contextBotUsername = username.toString();
} else { PeerData *peer = App::peerByName(contextBotUsername);
PeerData *peer = App::peerByName(text.midRef(1, usernameLength));
if (peer) { if (peer) {
if (peer->isUser()) { if (peer->isUser()) {
bot = peer->asUser(); contextBot = peer->asUser();
} else { } else {
contextBot = 0;
}
} else {
contextBot = ContextBotLookingUpData;
}
}
if (contextBot == ContextBotLookingUpData) return;
if (contextBot && (!contextBot->botInfo || contextBot->botInfo->contextPlaceholder.isEmpty())) {
contextBot = 0;
} else {
start = text.mid(usernameStart + usernameLength + 1);
} }
} } else {
} contextBot = 0;
contextBotUsername = QString();
} }
} }

View File

@ -63,7 +63,7 @@ public:
QSize minimumSizeHint() const; QSize minimumSizeHint() const;
EmojiPtr getSingleEmoji() const; EmojiPtr getSingleEmoji() const;
void getMentionHashtagBotCommandStart(QString &start, UserData *&contextBot, QString &lookedUpUsername) const; void getMentionHashtagBotCommandStart(QString &start, UserData *&contextBot, QString &contextBotUsername) const;
void removeSingleEmoji(); void removeSingleEmoji();
bool hasText() const; bool hasText() const;

View File

@ -2630,6 +2630,8 @@ HistoryWidget::HistoryWidget(QWidget *parent) : TWidget(parent)
, _toHistoryEnd(this, st::historyToEnd) , _toHistoryEnd(this, st::historyToEnd)
, _collapseComments(this) , _collapseComments(this)
, _attachMention(this) , _attachMention(this)
, _contextBot(0)
, _contextBotResolveRequestId(0)
, _reportSpamPanel(this) , _reportSpamPanel(this)
, _send(this, lang(lng_send_button), st::btnSend) , _send(this, lang(lng_send_button), st::btnSend)
, _unblock(this, lang(lng_unblock_button), st::btnUnblock) , _unblock(this, lang(lng_unblock_button), st::btnUnblock)
@ -4946,6 +4948,27 @@ bool HistoryWidget::hasBroadcastToggle() const {
return _peer && _peer->isChannel() && !_peer->isMegagroup() && _peer->asChannel()->canPublish() && !_peer->asChannel()->isBroadcast(); return _peer && _peer->isChannel() && !_peer->isMegagroup() && _peer->asChannel()->canPublish() && !_peer->asChannel()->isBroadcast();
} }
void HistoryWidget::contextBotResolveDone(const MTPcontacts_ResolvedPeer &result) {
_contextBot = 0;
if (result.type() == mtpc_contacts_resolvedPeer) {
const MTPDcontacts_resolvedPeer &d(result.c_contacts_resolvedPeer());
App::feedUsers(d.vusers);
App::feedChats(d.vchats);
PeerId peerId = peerFromMTP(d.vpeer);
if (peerId && peerIsUser(peerId)) {
_contextBot = App::user(peerId);
}
}
checkMentionDropdown();
}
bool HistoryWidget::contextBotResolveFail(const RPCError &error) {
if (mtpIsFlood(error)) return false;
_contextBot = 0;
checkMentionDropdown();
return true;
}
bool HistoryWidget::isBotStart() const { bool HistoryWidget::isBotStart() const {
if (!_peer || !_peer->isUser() || !_peer->asUser()->botInfo || !_canSendMessages) return false; if (!_peer || !_peer->isUser() || !_peer->asUser()->botInfo || !_canSendMessages) return false;
return !_peer->asUser()->botInfo->startToken.isEmpty() || (_history->isEmpty() && !_history->lastMsg); return !_peer->asUser()->botInfo->startToken.isEmpty() || (_history->isEmpty() && !_history->lastMsg);
@ -5287,9 +5310,24 @@ void HistoryWidget::onFieldFocused() {
void HistoryWidget::checkMentionDropdown() { void HistoryWidget::checkMentionDropdown() {
if (!_history || _a_show.animating()) return; if (!_history || _a_show.animating()) return;
QString start; QString start, contextBotUsername(_contextBotUsername);
_field.getMentionHashtagBotCommandStart(start, _contextBot, _contextBotUsername); _field.getMentionHashtagBotCommandStart(start, _contextBot, _contextBotUsername);
if (!start.isEmpty()) { if (contextBotUsername != _contextBotUsername) {
if (_contextBotResolveRequestId) {
MTP::cancel(_contextBotResolveRequestId);
_contextBotResolveRequestId = 0;
}
if (_contextBot == ContextBotLookingUpData) {
_contextBotResolveRequestId = MTP::send(MTPcontacts_ResolveUsername(MTP_string(_contextBotUsername)), rpcDone(&HistoryWidget::contextBotResolveDone), rpcFail(&HistoryWidget::contextBotResolveFail));
return;
}
} else if (_contextBot == ContextBotLookingUpData) {
return;
}
if (_contextBot) {
_attachMention.showContextResults(_contextBot, start);
} else if (!start.isEmpty()) {
if (start.at(0) == '#' && cRecentWriteHashtags().isEmpty() && cRecentSearchHashtags().isEmpty()) Local::readRecentHashtags(); if (start.at(0) == '#' && cRecentWriteHashtags().isEmpty() && cRecentSearchHashtags().isEmpty()) Local::readRecentHashtags();
if (start.at(0) == '@' && _peer->isUser()) return; if (start.at(0) == '@' && _peer->isUser()) return;
if (start.at(0) == '/' && _peer->isUser() && !_peer->asUser()->botInfo) return; if (start.at(0) == '/' && _peer->isUser() && !_peer->asUser()->botInfo) return;

View File

@ -763,6 +763,9 @@ private:
MentionsDropdown _attachMention; MentionsDropdown _attachMention;
UserData *_contextBot; UserData *_contextBot;
QString _contextBotUsername; QString _contextBotUsername;
mtpRequestId _contextBotResolveRequestId;
void contextBotResolveDone(const MTPcontacts_ResolvedPeer &result);
bool contextBotResolveFail(const RPCError &error);
bool isBotStart() const; bool isBotStart() const;
bool isBlocked() const; bool isBlocked() const;

View File

@ -327,7 +327,7 @@ struct BotInfo {
bool inited; bool inited;
bool readsAllHistory, cantJoinGroups; bool readsAllHistory, cantJoinGroups;
int32 version; int32 version;
QString shareText, description; QString shareText, description, contextPlaceholder;
QList<BotCommand> commands; QList<BotCommand> commands;
Text text; // description Text text; // description
@ -385,6 +385,7 @@ public:
BotInfo *botInfo; BotInfo *botInfo;
}; };
static UserData * const ContextBotLookingUpData = reinterpret_cast<UserData*>(&SharedMemoryLocation0);
class ChatData : public PeerData { class ChatData : public PeerData {
public: public: