Skip to content

Commit

Permalink
Refactor NotifySettings in PeerData.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Dec 4, 2017
1 parent 116e3fd commit 62568da
Show file tree
Hide file tree
Showing 21 changed files with 559 additions and 287 deletions.
1 change: 1 addition & 0 deletions Telegram/Resources/langs/lang.strings
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
"lng_unblock_button" = "Unblock";
"lng_channel_mute" = "Mute";
"lng_channel_unmute" = "Unmute";
"lng_saved_messages" = "Saved messages";

"lng_dialogs_text_with_from" = "{from_part} {message}";
"lng_dialogs_text_from_wrapped" = "{from}:";
Expand Down
25 changes: 19 additions & 6 deletions Telegram/SourceFiles/apiwrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1432,23 +1432,36 @@ void ApiWrap::checkQuitPreventFinished() {
}
}

PeerData *ApiWrap::notifySettingReceived(MTPInputNotifyPeer notifyPeer, const MTPPeerNotifySettings &settings) {
PeerData *ApiWrap::notifySettingReceived(
MTPInputNotifyPeer notifyPeer,
const MTPPeerNotifySettings &settings) {
PeerData *requestedPeer = nullptr;
switch (notifyPeer.type()) {
case mtpc_inputNotifyAll: App::main()->applyNotifySetting(MTP_notifyAll(), settings); break;
case mtpc_inputNotifyUsers: App::main()->applyNotifySetting(MTP_notifyUsers(), settings); break;
case mtpc_inputNotifyChats: App::main()->applyNotifySetting(MTP_notifyChats(), settings); break;
case mtpc_inputNotifyAll:
App::main()->applyNotifySetting(MTP_notifyAll(), settings);
break;
case mtpc_inputNotifyUsers:
App::main()->applyNotifySetting(MTP_notifyUsers(), settings);
break;
case mtpc_inputNotifyChats:
App::main()->applyNotifySetting(MTP_notifyChats(), settings);
break;
case mtpc_inputNotifyPeer: {
auto &peer = notifyPeer.c_inputNotifyPeer().vpeer;
switch (peer.type()) {
case mtpc_inputPeerEmpty: App::main()->applyNotifySetting(MTP_notifyPeer(MTP_peerUser(MTP_int(0))), settings); break;
case mtpc_inputPeerEmpty: App::main()->applyNotifySetting(
MTP_notifyPeer(MTP_peerUser(MTP_int(0))),
settings);
break;
case mtpc_inputPeerSelf: requestedPeer = App::self(); break;
case mtpc_inputPeerUser: requestedPeer = App::user(peerFromUser(peer.c_inputPeerUser().vuser_id)); break;
case mtpc_inputPeerChat: requestedPeer = App::chat(peerFromChat(peer.c_inputPeerChat().vchat_id)); break;
case mtpc_inputPeerChannel: requestedPeer = App::channel(peerFromChannel(peer.c_inputPeerChannel().vchannel_id)); break;
}
if (requestedPeer) {
App::main()->applyNotifySetting(MTP_notifyPeer(peerToMTP(requestedPeer->id)), settings);
App::main()->applyNotifySetting(
MTP_notifyPeer(peerToMTP(requestedPeer->id)),
settings);
}
} break;
}
Expand Down
33 changes: 17 additions & 16 deletions Telegram/SourceFiles/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace {
using PeersData = QHash<PeerId, PeerData*>;
PeersData peersData;

using MutedPeers = QMap<PeerData*, bool>;
using MutedPeers = QMap<not_null<PeerData*>, bool>;
MutedPeers mutedPeers;

PhotosData photosData;
Expand Down Expand Up @@ -195,9 +195,6 @@ namespace {
Window::Theme::Background()->reset();

cSetOtherOnline(0);
globalNotifyAllPtr = UnknownNotifySettings;
globalNotifyUsersPtr = UnknownNotifySettings;
globalNotifyChatsPtr = UnknownNotifySettings;
clearStorageImages();
if (auto w = wnd()) {
w->updateConnectingStatus();
Expand Down Expand Up @@ -2598,28 +2595,32 @@ namespace {
return QString();
}

void regMuted(PeerData *peer, int32 changeIn) {
void regMuted(not_null<PeerData*> peer, TimeMs changeIn) {
::mutedPeers.insert(peer, true);
if (App::main()) App::main()->updateMutedIn(changeIn);
App::main()->updateMutedIn(changeIn);
}

void unregMuted(PeerData *peer) {
void unregMuted(not_null<PeerData*> peer) {
::mutedPeers.remove(peer);
}

void updateMuted() {
int32 changeInMin = 0;
for (MutedPeers::iterator i = ::mutedPeers.begin(); i != ::mutedPeers.end();) {
int32 changeIn = 0;
History *h = App::history(i.key()->id);
if (isNotifyMuted(i.key()->notify, &changeIn)) {
h->setMute(true);
if (changeIn && (!changeInMin || changeIn < changeInMin)) {
changeInMin = changeIn;
auto changeInMin = TimeMs(0);
for (auto i = ::mutedPeers.begin(); i != ::mutedPeers.end();) {
const auto history = App::historyLoaded(i.key()->id);
const auto muteFinishesIn = i.key()->notifyMuteFinishesIn();
if (muteFinishesIn > 0) {
if (history) {
history->changeMute(true);
}
if (!changeInMin || muteFinishesIn < changeInMin) {
changeInMin = muteFinishesIn;
}
++i;
} else {
h->setMute(false);
if (history) {
history->changeMute(false);
}
i = ::mutedPeers.erase(i);
}
}
Expand Down
4 changes: 2 additions & 2 deletions Telegram/SourceFiles/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ namespace App {
void stopRoundVideoPlayback();
void stopGifItems();

void regMuted(PeerData *peer, int32 changeIn);
void unregMuted(PeerData *peer);
void regMuted(not_null<PeerData*> peer, TimeMs changeIn);
void unregMuted(not_null<PeerData*> peer);
void updateMuted();

void setProxySettings(QNetworkAccessManager &manager);
Expand Down
47 changes: 30 additions & 17 deletions Telegram/SourceFiles/boxes/mute_settings_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@
#include "ui/widgets/checkbox.h"
#include "ui/widgets/labels.h"

namespace {

constexpr auto kForeverHours = 24 * 365;

} // namespace

void MuteSettingsBox::prepare() {
setTitle(langFactory(lng_disable_notifications_from_tray));
int y = 0;
auto y = 0;

object_ptr<Ui::FlatLabel> info(this, st::boxLabel);
info->setText(lang(lng_mute_box_tip));
info->moveToLeft(st::boxPadding.left(), y);
y += info->height() + st::boxLittleSkip;

auto icon = object_ptr<Ui::UserpicButton>(
const auto icon = object_ptr<Ui::UserpicButton>(
this,
controller(),
_peer,
Expand All @@ -40,27 +46,34 @@ void MuteSettingsBox::prepare() {
// the icon is always higher than this chat title
y += icon->height() + st::boxMediumSkip;

const int FOREVER = 8760; // in fact, this is mute only for 1 year
auto group = std::make_shared<Ui::RadiobuttonGroup>(FOREVER);
// in fact, this is mute only for 1 year
const auto group = std::make_shared<Ui::RadiobuttonGroup>(kForeverHours);
y += st::boxOptionListPadding.top();
for (int value : { 1, 4, 18, 72, FOREVER }) { // periods in hours
QString text;
if (value < 24) {
text = lng_mute_duration_hours(lt_count, value);
} else if (value < FOREVER) {
text = lng_rights_chat_banned_day(lt_count, value / 24);
} else {
text = lang(lng_rights_chat_banned_forever);
}
object_ptr<Ui::Radiobutton> option(this, group, value, text);
for (const auto hours : { 1, 4, 18, 72, kForeverHours }) {
const auto text = [&] {
if (hours < 24) {
return lng_mute_duration_hours(lt_count, hours);
} else if (hours < kForeverHours) {
return lng_rights_chat_banned_day(lt_count, hours / 24);
} else {
return lang(lng_rights_chat_banned_forever);
}
}();
object_ptr<Ui::Radiobutton> option(this, group, hours, text);
option->moveToLeft(st::boxPadding.left(), y);
y += option->heightNoMargins() + st::boxOptionListSkip;
}
y += st::boxOptionListPadding.bottom() - st::boxOptionListSkip + st::defaultCheckbox.margin.bottom();
y += st::boxOptionListPadding.bottom()
- st::boxOptionListSkip
+ st::defaultCheckbox.margin.bottom();

addButton(langFactory(lng_box_ok), [this, group] {
App::main()->updateNotifySetting(_peer, NotifySettingSetMuted,
SilentNotifiesDontChange, group->value() * 3600);
auto muteForSeconds = group->value() * 3600;
App::main()->updateNotifySettings(
_peer,
Data::NotifySettings::MuteChange::Mute,
Data::NotifySettings::SilentPostsChange::Ignore,
muteForSeconds);
closeBox();
});
addButton(langFactory(lng_cancel), [this] { closeBox(); });
Expand Down
Loading

0 comments on commit 62568da

Please sign in to comment.