2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-09-05 08:55:59 +00:00

Allow fast bot mode without support mode.

This commit is contained in:
John Preston
2025-02-12 11:21:10 +04:00
parent 3f24627f54
commit 2b8eec8666
10 changed files with 150 additions and 98 deletions

View File

@@ -483,79 +483,6 @@ UserInfo Helper::infoCurrent(not_null<UserData*> user) const {
return (i != end(_userInformation)) ? i->second : UserInfo();
}
void Helper::readFastButtonModeBots() {
_readFastButtonModeBots = true;
auto f = QFile(FastButtonModeIdsPath(_session));
if (!f.open(QIODevice::ReadOnly)) {
return;
}
const auto data = f.readAll();
const auto json = QJsonDocument::fromJson(data);
if (!json.isObject()) {
return;
}
const auto object = json.object();
const auto array = object.value(u"ids"_q).toArray();
for (const auto &value : array) {
const auto bareId = value.toString().toULongLong();
_fastButtonModeBots.emplace(PeerId(bareId));
}
}
void Helper::writeFastButtonModeBots() {
auto array = QJsonArray();
for (const auto &id : _fastButtonModeBots) {
array.append(QString::number(id.value));
}
auto object = QJsonObject();
object[u"ids"_q] = array;
auto f = QFile(FastButtonModeIdsPath(_session));
if (f.open(QIODevice::WriteOnly)) {
f.write(QJsonDocument(object).toJson(QJsonDocument::Indented));
}
}
bool Helper::fastButtonMode(not_null<PeerData*> peer) const {
if (!_readFastButtonModeBots) {
const_cast<Helper*>(this)->readFastButtonModeBots();
}
return _fastButtonModeBots.contains(peer->id);
}
rpl::producer<bool> Helper::fastButtonModeValue(
not_null<PeerData*> peer) const {
return rpl::single(
fastButtonMode(peer)
) | rpl::then(_fastButtonModeBotsChanges.events(
) | rpl::filter([=](PeerId id) {
return (peer->id == id);
}) | rpl::map([=] {
return fastButtonMode(peer);
}));
}
void Helper::setFastButtonMode(not_null<PeerData*> peer, bool fast) {
if (fast == fastButtonMode(peer)) {
return;
} else if (fast) {
_fastButtonModeBots.emplace(peer->id);
} else {
_fastButtonModeBots.remove(peer->id);
}
if (_fastButtonModeBots.empty()) {
QFile(FastButtonModeIdsPath(_session)).remove();
} else {
writeFastButtonModeBots();
}
_fastButtonModeBotsChanges.fire_copy(peer->id);
if (const auto history = peer->owner().history(peer)) {
if (const auto item = history->lastMessage()) {
history->owner().requestItemRepaint(item);
}
}
}
void Helper::editInfo(
not_null<Window::SessionController*> controller,
not_null<UserData*> user) {
@@ -624,6 +551,83 @@ Templates &Helper::templates() {
return _templates;
}
FastButtonsBots::FastButtonsBots(not_null<Main::Session*> session)
: _session(session) {
}
bool FastButtonsBots::enabled(not_null<PeerData*> peer) const {
if (!_read) {
const_cast<FastButtonsBots*>(this)->read();
}
return _bots.contains(peer->id);
}
rpl::producer<bool> FastButtonsBots::enabledValue(
not_null<PeerData*> peer) const {
return rpl::single(
enabled(peer)
) | rpl::then(_changes.events(
) | rpl::filter([=](PeerId id) {
return (peer->id == id);
}) | rpl::map([=] {
return enabled(peer);
}));
}
void FastButtonsBots::setEnabled(not_null<PeerData*> peer, bool value) {
if (value == enabled(peer)) {
return;
} else if (value) {
_bots.emplace(peer->id);
} else {
_bots.remove(peer->id);
}
if (_bots.empty()) {
QFile(FastButtonModeIdsPath(_session)).remove();
} else {
write();
}
_changes.fire_copy(peer->id);
if (const auto history = peer->owner().history(peer)) {
if (const auto item = history->lastMessage()) {
history->owner().requestItemRepaint(item);
}
}
}
void FastButtonsBots::write() {
auto array = QJsonArray();
for (const auto &id : _bots) {
array.append(QString::number(id.value));
}
auto object = QJsonObject();
object[u"ids"_q] = array;
auto f = QFile(FastButtonModeIdsPath(_session));
if (f.open(QIODevice::WriteOnly)) {
f.write(QJsonDocument(object).toJson(QJsonDocument::Indented));
}
}
void FastButtonsBots::read() {
_read = true;
auto f = QFile(FastButtonModeIdsPath(_session));
if (!f.open(QIODevice::ReadOnly)) {
return;
}
const auto data = f.readAll();
const auto json = QJsonDocument::fromJson(data);
if (!json.isObject()) {
return;
}
const auto object = json.object();
const auto array = object.value(u"ids"_q).toArray();
for (const auto &value : array) {
const auto bareId = value.toString().toULongLong();
_bots.emplace(PeerId(bareId));
}
}
QString ChatOccupiedString(not_null<History*> history) {
const auto hand = QString::fromUtf8("\xe2\x9c\x8b\xef\xb8\x8f");
const auto name = ParseOccupationName(history);