2017-11-21 09:20:56 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-11-21 09:20:56 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-11-21 09:20:56 +00:00
|
|
|
*/
|
|
|
|
#include "data/data_peer_values.h"
|
|
|
|
|
2017-12-17 08:13:26 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_channel.h"
|
|
|
|
#include "data/data_chat.h"
|
|
|
|
#include "data/data_user.h"
|
2017-12-17 08:13:26 +00:00
|
|
|
|
2017-11-21 09:20:56 +00:00
|
|
|
namespace Data {
|
2017-12-17 08:13:26 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kMinOnlineChangeTimeout = crl::time(1000);
|
|
|
|
constexpr auto kMaxOnlineChangeTimeout = 86400 * crl::time(1000);
|
2018-02-03 19:52:35 +00:00
|
|
|
constexpr auto kSecondsInDay = 86400;
|
2017-12-17 08:13:26 +00:00
|
|
|
|
|
|
|
int OnlinePhraseChangeInSeconds(TimeId online, TimeId now) {
|
|
|
|
if (online <= 0) {
|
|
|
|
if (-online > now) {
|
|
|
|
return (-online - now);
|
|
|
|
}
|
2018-03-09 11:44:42 +00:00
|
|
|
return std::numeric_limits<TimeId>::max();
|
2017-12-17 08:13:26 +00:00
|
|
|
}
|
|
|
|
if (online > now) {
|
|
|
|
return online - now;
|
|
|
|
}
|
|
|
|
const auto minutes = (now - online) / 60;
|
|
|
|
if (minutes < 60) {
|
|
|
|
return (minutes + 1) * 60 - (now - online);
|
|
|
|
}
|
|
|
|
const auto hours = (now - online) / 3600;
|
|
|
|
if (hours < 12) {
|
|
|
|
return (hours + 1) * 3600 - (now - online);
|
|
|
|
}
|
2018-02-03 19:52:35 +00:00
|
|
|
const auto nowFull = ParseDateTime(now);
|
2017-12-17 08:13:26 +00:00
|
|
|
const auto tomorrow = QDateTime(nowFull.date().addDays(1));
|
2018-03-09 11:44:42 +00:00
|
|
|
return std::max(static_cast<TimeId>(nowFull.secsTo(tomorrow)), 0);
|
2017-12-17 08:13:26 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<QString> OnlineTextSpecial(not_null<UserData*> user) {
|
2019-03-12 10:36:33 +00:00
|
|
|
if (user->isNotificationsUser()) {
|
2017-12-17 08:13:26 +00:00
|
|
|
return lang(lng_status_service_notifications);
|
2019-03-12 10:36:33 +00:00
|
|
|
} else if (user->isSupport()) {
|
|
|
|
return lang(lng_status_support);
|
|
|
|
} else if (user->isBot()) {
|
2017-12-17 08:13:26 +00:00
|
|
|
return lang(lng_status_bot);
|
2019-03-12 10:36:33 +00:00
|
|
|
} else if (user->isServiceUser()) {
|
2017-12-17 08:13:26 +00:00
|
|
|
return lang(lng_status_support);
|
|
|
|
}
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-12-17 08:13:26 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<QString> OnlineTextCommon(TimeId online, TimeId now) {
|
2017-12-17 08:13:26 +00:00
|
|
|
if (online <= 0) {
|
|
|
|
switch (online) {
|
|
|
|
case 0:
|
|
|
|
case -1: return lang(lng_status_offline);
|
|
|
|
case -2: return lang(lng_status_recently);
|
|
|
|
case -3: return lang(lng_status_last_week);
|
|
|
|
case -4: return lang(lng_status_last_month);
|
|
|
|
}
|
|
|
|
return (-online > now)
|
|
|
|
? lang(lng_status_online)
|
|
|
|
: lang(lng_status_recently);
|
|
|
|
} else if (online > now) {
|
|
|
|
return lang(lng_status_online);
|
|
|
|
}
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-12-17 08:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2017-11-21 09:20:56 +00:00
|
|
|
|
|
|
|
inline auto AdminRightsValue(not_null<ChannelData*> channel) {
|
|
|
|
return channel->adminRightsValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto AdminRightsValue(
|
|
|
|
not_null<ChannelData*> channel,
|
2019-01-03 14:39:19 +00:00
|
|
|
MTPDchatAdminRights::Flags mask) {
|
2017-11-21 09:20:56 +00:00
|
|
|
return FlagsValueWithMask(AdminRightsValue(channel), mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto AdminRightValue(
|
|
|
|
not_null<ChannelData*> channel,
|
2019-01-03 14:39:19 +00:00
|
|
|
MTPDchatAdminRights::Flag flag) {
|
2017-11-21 09:20:56 +00:00
|
|
|
return SingleFlagValue(AdminRightsValue(channel), flag);
|
|
|
|
}
|
|
|
|
|
2019-01-05 10:50:04 +00:00
|
|
|
inline auto AdminRightsValue(not_null<ChatData*> chat) {
|
|
|
|
return chat->adminRightsValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto AdminRightsValue(
|
|
|
|
not_null<ChatData*> chat,
|
|
|
|
MTPDchatAdminRights::Flags mask) {
|
|
|
|
return FlagsValueWithMask(AdminRightsValue(chat), mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto AdminRightValue(
|
|
|
|
not_null<ChatData*> chat,
|
|
|
|
MTPDchatAdminRights::Flag flag) {
|
|
|
|
return SingleFlagValue(AdminRightsValue(chat), flag);
|
|
|
|
}
|
|
|
|
|
2017-11-21 09:20:56 +00:00
|
|
|
inline auto RestrictionsValue(not_null<ChannelData*> channel) {
|
|
|
|
return channel->restrictionsValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto RestrictionsValue(
|
|
|
|
not_null<ChannelData*> channel,
|
2019-01-03 14:39:19 +00:00
|
|
|
MTPDchatBannedRights::Flags mask) {
|
2017-11-21 09:20:56 +00:00
|
|
|
return FlagsValueWithMask(RestrictionsValue(channel), mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto RestrictionValue(
|
|
|
|
not_null<ChannelData*> channel,
|
2019-01-03 14:39:19 +00:00
|
|
|
MTPDchatBannedRights::Flag flag) {
|
2017-11-21 09:20:56 +00:00
|
|
|
return SingleFlagValue(RestrictionsValue(channel), flag);
|
|
|
|
}
|
|
|
|
|
2019-01-05 10:50:04 +00:00
|
|
|
inline auto DefaultRestrictionsValue(not_null<ChannelData*> channel) {
|
|
|
|
return channel->defaultRestrictionsValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto DefaultRestrictionsValue(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
MTPDchatBannedRights::Flags mask) {
|
|
|
|
return FlagsValueWithMask(DefaultRestrictionsValue(channel), mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto DefaultRestrictionValue(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
MTPDchatBannedRights::Flag flag) {
|
|
|
|
return SingleFlagValue(DefaultRestrictionsValue(channel), flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto DefaultRestrictionsValue(not_null<ChatData*> chat) {
|
|
|
|
return chat->defaultRestrictionsValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto DefaultRestrictionsValue(
|
|
|
|
not_null<ChatData*> chat,
|
|
|
|
MTPDchatBannedRights::Flags mask) {
|
|
|
|
return FlagsValueWithMask(DefaultRestrictionsValue(chat), mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto DefaultRestrictionValue(
|
|
|
|
not_null<ChatData*> chat,
|
|
|
|
MTPDchatBannedRights::Flag flag) {
|
|
|
|
return SingleFlagValue(DefaultRestrictionsValue(chat), flag);
|
|
|
|
}
|
|
|
|
|
2017-11-21 09:20:56 +00:00
|
|
|
rpl::producer<bool> PeerFlagValue(
|
|
|
|
ChatData *chat,
|
|
|
|
MTPDchat_ClientFlag flag) {
|
|
|
|
return PeerFlagValue(chat, static_cast<MTPDchat::Flag>(flag));
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<bool> PeerFlagValue(
|
|
|
|
ChannelData *channel,
|
|
|
|
MTPDchannel_ClientFlag flag) {
|
|
|
|
return PeerFlagValue(channel, static_cast<MTPDchannel::Flag>(flag));
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<bool> CanWriteValue(UserData *user) {
|
|
|
|
using namespace rpl::mappers;
|
|
|
|
return PeerFlagValue(user, MTPDuser::Flag::f_deleted)
|
|
|
|
| rpl::map(!_1);
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<bool> CanWriteValue(ChatData *chat) {
|
|
|
|
using namespace rpl::mappers;
|
2019-01-05 10:50:04 +00:00
|
|
|
const auto mask = 0
|
2017-11-21 09:20:56 +00:00
|
|
|
| MTPDchat::Flag::f_deactivated
|
|
|
|
| MTPDchat_ClientFlag::f_forbidden
|
|
|
|
| MTPDchat::Flag::f_left
|
2019-01-05 10:50:04 +00:00
|
|
|
| MTPDchat::Flag::f_creator
|
2017-11-21 09:20:56 +00:00
|
|
|
| MTPDchat::Flag::f_kicked;
|
2019-01-05 10:50:04 +00:00
|
|
|
return rpl::combine(
|
|
|
|
PeerFlagsValue(chat, mask),
|
|
|
|
AdminRightsValue(chat),
|
|
|
|
DefaultRestrictionValue(
|
|
|
|
chat,
|
|
|
|
MTPDchatBannedRights::Flag::f_send_messages),
|
|
|
|
[](
|
|
|
|
MTPDchat::Flags flags,
|
|
|
|
Data::Flags<ChatAdminRights>::Change adminRights,
|
|
|
|
bool defaultSendMessagesRestriction) {
|
2019-01-22 13:05:06 +00:00
|
|
|
const auto amOutFlags = 0
|
2019-01-05 10:50:04 +00:00
|
|
|
| MTPDchat::Flag::f_deactivated
|
|
|
|
| MTPDchat_ClientFlag::f_forbidden
|
|
|
|
| MTPDchat::Flag::f_left
|
|
|
|
| MTPDchat::Flag::f_kicked;
|
2019-01-22 13:05:06 +00:00
|
|
|
return !(flags & amOutFlags)
|
2019-01-05 10:50:04 +00:00
|
|
|
&& ((flags & MTPDchat::Flag::f_creator)
|
|
|
|
|| (adminRights.value != MTPDchatAdminRights::Flags(0))
|
|
|
|
|| !defaultSendMessagesRestriction);
|
|
|
|
});
|
2017-11-21 09:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<bool> CanWriteValue(ChannelData *channel) {
|
2019-01-05 10:50:04 +00:00
|
|
|
const auto mask = 0
|
2017-11-21 09:20:56 +00:00
|
|
|
| MTPDchannel::Flag::f_left
|
|
|
|
| MTPDchannel_ClientFlag::f_forbidden
|
|
|
|
| MTPDchannel::Flag::f_creator
|
|
|
|
| MTPDchannel::Flag::f_broadcast;
|
|
|
|
return rpl::combine(
|
|
|
|
PeerFlagsValue(channel, mask),
|
|
|
|
AdminRightValue(
|
|
|
|
channel,
|
2019-01-03 14:39:19 +00:00
|
|
|
MTPDchatAdminRights::Flag::f_post_messages),
|
2017-11-21 09:20:56 +00:00
|
|
|
RestrictionValue(
|
|
|
|
channel,
|
2019-01-03 14:39:19 +00:00
|
|
|
MTPDchatBannedRights::Flag::f_send_messages),
|
2019-01-05 10:50:04 +00:00
|
|
|
DefaultRestrictionValue(
|
|
|
|
channel,
|
|
|
|
MTPDchatBannedRights::Flag::f_send_messages),
|
2017-11-21 09:20:56 +00:00
|
|
|
[](
|
|
|
|
MTPDchannel::Flags flags,
|
|
|
|
bool postMessagesRight,
|
2019-01-05 10:50:04 +00:00
|
|
|
bool sendMessagesRestriction,
|
|
|
|
bool defaultSendMessagesRestriction) {
|
|
|
|
const auto notAmInFlags = 0
|
2017-11-21 09:20:56 +00:00
|
|
|
| MTPDchannel::Flag::f_left
|
|
|
|
| MTPDchannel_ClientFlag::f_forbidden;
|
|
|
|
return !(flags & notAmInFlags)
|
|
|
|
&& (postMessagesRight
|
|
|
|
|| (flags & MTPDchannel::Flag::f_creator)
|
|
|
|
|| (!(flags & MTPDchannel::Flag::f_broadcast)
|
2019-01-05 10:50:04 +00:00
|
|
|
&& !sendMessagesRestriction
|
|
|
|
&& !defaultSendMessagesRestriction));
|
2017-11-21 09:20:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<bool> CanWriteValue(not_null<PeerData*> peer) {
|
|
|
|
if (auto user = peer->asUser()) {
|
|
|
|
return CanWriteValue(user);
|
|
|
|
} else if (auto chat = peer->asChat()) {
|
|
|
|
return CanWriteValue(chat);
|
|
|
|
} else if (auto channel = peer->asChannel()) {
|
|
|
|
return CanWriteValue(channel);
|
|
|
|
}
|
|
|
|
Unexpected("Bad peer value in CanWriteValue()");
|
|
|
|
}
|
|
|
|
|
2017-12-17 08:13:26 +00:00
|
|
|
TimeId SortByOnlineValue(not_null<UserData*> user, TimeId now) {
|
2019-03-12 10:36:33 +00:00
|
|
|
if (user->isServiceUser() || user->isBot()) {
|
2017-12-17 08:13:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const auto online = user->onlineTill;
|
|
|
|
if (online <= 0) {
|
|
|
|
switch (online) {
|
|
|
|
case 0:
|
|
|
|
case -1: return online;
|
|
|
|
|
|
|
|
case -2: {
|
2018-02-03 19:52:35 +00:00
|
|
|
return now - 3 * kSecondsInDay;
|
2017-12-17 08:13:26 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case -3: {
|
2018-02-03 19:52:35 +00:00
|
|
|
return now - 7 * kSecondsInDay;
|
2017-12-17 08:13:26 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case -4: {
|
2018-02-03 19:52:35 +00:00
|
|
|
return now - 30 * kSecondsInDay;
|
2017-12-17 08:13:26 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
return -online;
|
|
|
|
}
|
|
|
|
return online;
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time OnlineChangeTimeout(TimeId online, TimeId now) {
|
2017-12-17 08:13:26 +00:00
|
|
|
const auto result = OnlinePhraseChangeInSeconds(online, now);
|
|
|
|
Assert(result >= 0);
|
|
|
|
return snap(
|
2019-02-19 06:57:53 +00:00
|
|
|
result * crl::time(1000),
|
2017-12-17 08:13:26 +00:00
|
|
|
kMinOnlineChangeTimeout,
|
|
|
|
kMaxOnlineChangeTimeout);
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time OnlineChangeTimeout(not_null<UserData*> user, TimeId now) {
|
2019-03-12 10:36:33 +00:00
|
|
|
if (user->isServiceUser() || user->botInfo) {
|
2017-12-17 08:13:26 +00:00
|
|
|
return kMaxOnlineChangeTimeout;
|
|
|
|
}
|
|
|
|
return OnlineChangeTimeout(user->onlineTill, now);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString OnlineText(TimeId online, TimeId now) {
|
|
|
|
if (const auto common = OnlineTextCommon(online, now)) {
|
|
|
|
return *common;
|
|
|
|
}
|
|
|
|
const auto minutes = (now - online) / 60;
|
|
|
|
if (!minutes) {
|
|
|
|
return lang(lng_status_lastseen_now);
|
|
|
|
} else if (minutes < 60) {
|
|
|
|
return lng_status_lastseen_minutes(lt_count, minutes);
|
|
|
|
}
|
|
|
|
const auto hours = (now - online) / 3600;
|
|
|
|
if (hours < 12) {
|
|
|
|
return lng_status_lastseen_hours(lt_count, hours);
|
|
|
|
}
|
2018-02-03 19:52:35 +00:00
|
|
|
const auto onlineFull = ParseDateTime(online);
|
|
|
|
const auto nowFull = ParseDateTime(now);
|
2017-12-17 08:13:26 +00:00
|
|
|
if (onlineFull.date() == nowFull.date()) {
|
|
|
|
const auto onlineTime = onlineFull.time().toString(cTimeFormat());
|
|
|
|
return lng_status_lastseen_today(lt_time, onlineTime);
|
|
|
|
} else if (onlineFull.date().addDays(1) == nowFull.date()) {
|
|
|
|
const auto onlineTime = onlineFull.time().toString(cTimeFormat());
|
|
|
|
return lng_status_lastseen_yesterday(lt_time, onlineTime);
|
|
|
|
}
|
|
|
|
const auto date = onlineFull.date().toString(qsl("dd.MM.yy"));
|
|
|
|
return lng_status_lastseen_date(lt_date, date);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString OnlineText(not_null<UserData*> user, TimeId now) {
|
|
|
|
if (const auto special = OnlineTextSpecial(user)) {
|
|
|
|
return *special;
|
|
|
|
}
|
|
|
|
return OnlineText(user->onlineTill, now);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString OnlineTextFull(not_null<UserData*> user, TimeId now) {
|
|
|
|
if (const auto special = OnlineTextSpecial(user)) {
|
|
|
|
return *special;
|
|
|
|
} else if (const auto common = OnlineTextCommon(user->onlineTill, now)) {
|
|
|
|
return *common;
|
|
|
|
}
|
2018-02-03 19:52:35 +00:00
|
|
|
const auto onlineFull = ParseDateTime(user->onlineTill);
|
|
|
|
const auto nowFull = ParseDateTime(now);
|
2017-12-17 08:13:26 +00:00
|
|
|
if (onlineFull.date() == nowFull.date()) {
|
|
|
|
const auto onlineTime = onlineFull.time().toString(cTimeFormat());
|
|
|
|
return lng_status_lastseen_today(lt_time, onlineTime);
|
|
|
|
} else if (onlineFull.date().addDays(1) == nowFull.date()) {
|
|
|
|
const auto onlineTime = onlineFull.time().toString(cTimeFormat());
|
|
|
|
return lng_status_lastseen_yesterday(lt_time, onlineTime);
|
|
|
|
}
|
|
|
|
const auto date = onlineFull.date().toString(qsl("dd.MM.yy"));
|
|
|
|
const auto time = onlineFull.time().toString(cTimeFormat());
|
|
|
|
return lng_status_lastseen_date_time(lt_date, date, lt_time, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OnlineTextActive(TimeId online, TimeId now) {
|
|
|
|
if (online <= 0) {
|
|
|
|
switch (online) {
|
|
|
|
case 0:
|
|
|
|
case -1:
|
|
|
|
case -2:
|
|
|
|
case -3:
|
|
|
|
case -4: return false;
|
|
|
|
}
|
|
|
|
return (-online > now);
|
|
|
|
}
|
|
|
|
return (online > now);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OnlineTextActive(not_null<UserData*> user, TimeId now) {
|
2019-03-12 10:36:33 +00:00
|
|
|
if (user->isServiceUser() || user->botInfo) {
|
2017-12-17 08:13:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return OnlineTextActive(user->onlineTill, now);
|
|
|
|
}
|
|
|
|
|
2017-11-21 09:20:56 +00:00
|
|
|
} // namespace Data
|