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"
|
2019-07-10 17:28:33 +00:00
|
|
|
#include "base/unixtime.h"
|
2020-11-04 15:50:17 +00:00
|
|
|
#include "base/qt_adapters.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);
|
|
|
|
}
|
2019-07-10 17:28:33 +00:00
|
|
|
const auto nowFull = base::unixtime::parse(now);
|
2020-11-04 15:50:17 +00:00
|
|
|
const auto tomorrow = base::QDateToDateTime(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()) {
|
2019-06-19 15:09:03 +00:00
|
|
|
return tr::lng_status_service_notifications(tr::now);
|
2019-03-12 10:36:33 +00:00
|
|
|
} else if (user->isSupport()) {
|
2019-06-19 15:09:03 +00:00
|
|
|
return tr::lng_status_support(tr::now);
|
2019-03-12 10:36:33 +00:00
|
|
|
} else if (user->isBot()) {
|
2019-06-19 15:09:03 +00:00
|
|
|
return tr::lng_status_bot(tr::now);
|
2019-03-12 10:36:33 +00:00
|
|
|
} else if (user->isServiceUser()) {
|
2019-06-19 15:09:03 +00:00
|
|
|
return tr::lng_status_support(tr::now);
|
2017-12-17 08:13:26 +00:00
|
|
|
}
|
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:
|
2019-06-19 15:09:03 +00:00
|
|
|
case -1: return tr::lng_status_offline(tr::now);
|
|
|
|
case -2: return tr::lng_status_recently(tr::now);
|
|
|
|
case -3: return tr::lng_status_last_week(tr::now);
|
|
|
|
case -4: return tr::lng_status_last_month(tr::now);
|
2017-12-17 08:13:26 +00:00
|
|
|
}
|
|
|
|
return (-online > now)
|
2019-06-19 15:09:03 +00:00
|
|
|
? tr::lng_status_online(tr::now)
|
|
|
|
: tr::lng_status_recently(tr::now);
|
2017-12-17 08:13:26 +00:00
|
|
|
} else if (online > now) {
|
2019-06-19 15:09:03 +00:00
|
|
|
return tr::lng_status_online(tr::now);
|
2017-12-17 08:13:26 +00:00
|
|
|
}
|
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,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatAdminRights mask) {
|
2017-11-21 09:20:56 +00:00
|
|
|
return FlagsValueWithMask(AdminRightsValue(channel), mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto AdminRightValue(
|
|
|
|
not_null<ChannelData*> channel,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatAdminRight 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,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatAdminRights mask) {
|
2019-01-05 10:50:04 +00:00
|
|
|
return FlagsValueWithMask(AdminRightsValue(chat), mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto AdminRightValue(
|
|
|
|
not_null<ChatData*> chat,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatAdminRight flag) {
|
2019-01-05 10:50:04 +00:00
|
|
|
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,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatRestrictions mask) {
|
2017-11-21 09:20:56 +00:00
|
|
|
return FlagsValueWithMask(RestrictionsValue(channel), mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto RestrictionValue(
|
|
|
|
not_null<ChannelData*> channel,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatRestriction 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,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatRestrictions mask) {
|
2019-01-05 10:50:04 +00:00
|
|
|
return FlagsValueWithMask(DefaultRestrictionsValue(channel), mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto DefaultRestrictionValue(
|
|
|
|
not_null<ChannelData*> channel,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatRestriction flag) {
|
2019-01-05 10:50:04 +00:00
|
|
|
return SingleFlagValue(DefaultRestrictionsValue(channel), flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto DefaultRestrictionsValue(not_null<ChatData*> chat) {
|
|
|
|
return chat->defaultRestrictionsValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto DefaultRestrictionsValue(
|
|
|
|
not_null<ChatData*> chat,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatRestrictions mask) {
|
2019-01-05 10:50:04 +00:00
|
|
|
return FlagsValueWithMask(DefaultRestrictionsValue(chat), mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto DefaultRestrictionValue(
|
|
|
|
not_null<ChatData*> chat,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatRestriction flag) {
|
2019-01-05 10:50:04 +00:00
|
|
|
return SingleFlagValue(DefaultRestrictionsValue(chat), flag);
|
|
|
|
}
|
|
|
|
|
2017-11-21 09:20:56 +00:00
|
|
|
rpl::producer<bool> CanWriteValue(UserData *user) {
|
|
|
|
using namespace rpl::mappers;
|
2020-09-11 15:33:26 +00:00
|
|
|
|
|
|
|
if (user->isRepliesChat()) {
|
|
|
|
return rpl::single(false);
|
|
|
|
}
|
2021-07-08 11:19:12 +00:00
|
|
|
return PeerFlagValue(user, UserDataFlag::Deleted)
|
2017-11-21 09:20:56 +00:00
|
|
|
| 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
|
2021-07-08 12:11:46 +00:00
|
|
|
| ChatDataFlag::Deactivated
|
|
|
|
| ChatDataFlag::Forbidden
|
|
|
|
| ChatDataFlag::Left
|
|
|
|
| ChatDataFlag::Creator
|
|
|
|
| ChatDataFlag::Kicked;
|
2019-01-05 10:50:04 +00:00
|
|
|
return rpl::combine(
|
|
|
|
PeerFlagsValue(chat, mask),
|
|
|
|
AdminRightsValue(chat),
|
|
|
|
DefaultRestrictionValue(
|
|
|
|
chat,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatRestriction::SendMessages),
|
2019-01-05 10:50:04 +00:00
|
|
|
[](
|
2021-07-08 12:11:46 +00:00
|
|
|
ChatDataFlags flags,
|
2019-01-05 10:50:04 +00:00
|
|
|
Data::Flags<ChatAdminRights>::Change adminRights,
|
|
|
|
bool defaultSendMessagesRestriction) {
|
2019-01-22 13:05:06 +00:00
|
|
|
const auto amOutFlags = 0
|
2021-07-08 12:11:46 +00:00
|
|
|
| ChatDataFlag::Deactivated
|
|
|
|
| ChatDataFlag::Forbidden
|
|
|
|
| ChatDataFlag::Left
|
|
|
|
| ChatDataFlag::Kicked;
|
2019-01-22 13:05:06 +00:00
|
|
|
return !(flags & amOutFlags)
|
2021-07-08 12:11:46 +00:00
|
|
|
&& ((flags & ChatDataFlag::Creator)
|
2021-07-08 10:34:06 +00:00
|
|
|
|| (adminRights.value != ChatAdminRights(0))
|
2019-01-05 10:50:04 +00:00
|
|
|
|| !defaultSendMessagesRestriction);
|
|
|
|
});
|
2017-11-21 09:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<bool> CanWriteValue(ChannelData *channel) {
|
2021-07-08 13:11:09 +00:00
|
|
|
using Flag = ChannelDataFlag;
|
2019-01-05 10:50:04 +00:00
|
|
|
const auto mask = 0
|
2021-07-08 13:11:09 +00:00
|
|
|
| Flag::Left
|
|
|
|
| Flag::HasLink
|
|
|
|
| Flag::Forbidden
|
|
|
|
| Flag::Creator
|
|
|
|
| Flag::Broadcast;
|
2017-11-21 09:20:56 +00:00
|
|
|
return rpl::combine(
|
|
|
|
PeerFlagsValue(channel, mask),
|
|
|
|
AdminRightValue(
|
|
|
|
channel,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatAdminRight::PostMessages),
|
2017-11-21 09:20:56 +00:00
|
|
|
RestrictionValue(
|
|
|
|
channel,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatRestriction::SendMessages),
|
2019-01-05 10:50:04 +00:00
|
|
|
DefaultRestrictionValue(
|
|
|
|
channel,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatRestriction::SendMessages),
|
2017-11-21 09:20:56 +00:00
|
|
|
[](
|
2021-07-08 13:11:09 +00:00
|
|
|
ChannelDataFlags flags,
|
2017-11-21 09:20:56 +00:00
|
|
|
bool postMessagesRight,
|
2019-01-05 10:50:04 +00:00
|
|
|
bool sendMessagesRestriction,
|
|
|
|
bool defaultSendMessagesRestriction) {
|
2021-07-08 13:11:09 +00:00
|
|
|
const auto notAmInFlags = Flag::Left | Flag::Forbidden;
|
2020-09-11 11:14:24 +00:00
|
|
|
const auto allowed = !(flags & notAmInFlags)
|
2021-07-08 13:11:09 +00:00
|
|
|
|| (flags & Flag::HasLink);
|
2020-09-11 11:14:24 +00:00
|
|
|
return allowed && (postMessagesRight
|
2021-07-08 13:11:09 +00:00
|
|
|
|| (flags & Flag::Creator)
|
|
|
|
|| (!(flags & Flag::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);
|
|
|
|
}
|
2020-10-22 07:53:56 +00:00
|
|
|
Unexpected("Bad peer value in CanWriteValue");
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is duplicated in PeerData::canPinMessages().
|
|
|
|
rpl::producer<bool> CanPinMessagesValue(not_null<PeerData*> peer) {
|
|
|
|
using namespace rpl::mappers;
|
|
|
|
if (const auto user = peer->asUser()) {
|
2021-07-08 11:19:12 +00:00
|
|
|
return PeerFlagsValue(
|
2020-10-22 07:53:56 +00:00
|
|
|
user,
|
2021-07-08 11:19:12 +00:00
|
|
|
UserDataFlag::CanPinMessages
|
|
|
|
) | rpl::map(_1 != UserDataFlag(0));
|
2020-10-22 07:53:56 +00:00
|
|
|
} else if (const auto chat = peer->asChat()) {
|
|
|
|
const auto mask = 0
|
2021-07-08 12:11:46 +00:00
|
|
|
| ChatDataFlag::Deactivated
|
|
|
|
| ChatDataFlag::Forbidden
|
|
|
|
| ChatDataFlag::Left
|
|
|
|
| ChatDataFlag::Creator
|
|
|
|
| ChatDataFlag::Kicked;
|
2020-10-22 07:53:56 +00:00
|
|
|
return rpl::combine(
|
|
|
|
PeerFlagsValue(chat, mask),
|
2021-07-08 10:34:06 +00:00
|
|
|
AdminRightValue(chat, ChatAdminRight::PinMessages),
|
|
|
|
DefaultRestrictionValue(chat, ChatRestriction::PinMessages),
|
2021-07-08 12:11:46 +00:00
|
|
|
[](
|
|
|
|
ChatDataFlags flags,
|
2020-10-22 07:53:56 +00:00
|
|
|
bool adminRightAllows,
|
|
|
|
bool defaultRestriction) {
|
|
|
|
const auto amOutFlags = 0
|
2021-07-08 12:11:46 +00:00
|
|
|
| ChatDataFlag::Deactivated
|
|
|
|
| ChatDataFlag::Forbidden
|
|
|
|
| ChatDataFlag::Left
|
|
|
|
| ChatDataFlag::Kicked;
|
2020-10-22 07:53:56 +00:00
|
|
|
return !(flags & amOutFlags)
|
2021-07-08 12:11:46 +00:00
|
|
|
&& ((flags & ChatDataFlag::Creator)
|
2020-10-22 07:53:56 +00:00
|
|
|
|| adminRightAllows
|
|
|
|
|| !defaultRestriction);
|
|
|
|
});
|
|
|
|
} else if (const auto megagroup = peer->asMegagroup()) {
|
|
|
|
if (megagroup->amCreator()) {
|
|
|
|
return rpl::single(true);
|
|
|
|
}
|
|
|
|
return rpl::combine(
|
2021-07-08 10:34:06 +00:00
|
|
|
AdminRightValue(megagroup, ChatAdminRight::PinMessages),
|
|
|
|
DefaultRestrictionValue(megagroup, ChatRestriction::PinMessages),
|
2021-07-08 13:11:09 +00:00
|
|
|
PeerFlagsValue(
|
|
|
|
megagroup,
|
|
|
|
ChannelDataFlag::Username | ChannelDataFlag::Location),
|
2020-10-22 07:53:56 +00:00
|
|
|
megagroup->restrictionsValue()
|
|
|
|
) | rpl::map([=](
|
|
|
|
bool adminRightAllows,
|
|
|
|
bool defaultRestriction,
|
2021-07-08 13:11:09 +00:00
|
|
|
ChannelDataFlags usernameOrLocation,
|
2020-10-22 07:53:56 +00:00
|
|
|
Data::Flags<ChatRestrictions>::Change restrictions) {
|
|
|
|
return adminRightAllows
|
2021-07-08 13:11:09 +00:00
|
|
|
|| (!usernameOrLocation
|
2020-10-22 07:53:56 +00:00
|
|
|
&& !defaultRestriction
|
2021-07-08 10:34:06 +00:00
|
|
|
&& !(restrictions.value & ChatRestriction::PinMessages));
|
2020-10-22 07:53:56 +00:00
|
|
|
});
|
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
|
|
|
if (channel->amCreator()) {
|
|
|
|
return rpl::single(true);
|
|
|
|
}
|
2021-07-08 10:34:06 +00:00
|
|
|
return AdminRightValue(channel, ChatAdminRight::EditMessages);
|
2020-10-22 07:53:56 +00:00
|
|
|
}
|
|
|
|
Unexpected("Peer type in CanPinMessagesValue.");
|
2017-11-21 09:20:56 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 12:28:25 +00:00
|
|
|
rpl::producer<bool> CanManageGroupCallValue(not_null<PeerData*> peer) {
|
2021-07-08 10:34:06 +00:00
|
|
|
const auto flag = ChatAdminRight::ManageCall;
|
2021-04-05 12:28:25 +00:00
|
|
|
if (const auto chat = peer->asChat()) {
|
|
|
|
return chat->amCreator()
|
|
|
|
? (rpl::single(true) | rpl::type_erased())
|
|
|
|
: AdminRightValue(chat, flag);
|
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
|
|
|
return channel->amCreator()
|
|
|
|
? (rpl::single(true) | rpl::type_erased())
|
|
|
|
: AdminRightValue(channel, flag);
|
|
|
|
}
|
|
|
|
return rpl::single(false);
|
|
|
|
}
|
|
|
|
|
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);
|
2021-01-23 03:29:50 +00:00
|
|
|
return std::clamp(
|
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-07-18 08:51:11 +00:00
|
|
|
if (user->isServiceUser() || user->isBot()) {
|
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) {
|
2019-06-19 15:09:03 +00:00
|
|
|
return tr::lng_status_lastseen_now(tr::now);
|
2017-12-17 08:13:26 +00:00
|
|
|
} else if (minutes < 60) {
|
2019-06-19 16:39:25 +00:00
|
|
|
return tr::lng_status_lastseen_minutes(tr::now, lt_count, minutes);
|
2017-12-17 08:13:26 +00:00
|
|
|
}
|
|
|
|
const auto hours = (now - online) / 3600;
|
|
|
|
if (hours < 12) {
|
2019-06-19 16:39:25 +00:00
|
|
|
return tr::lng_status_lastseen_hours(tr::now, lt_count, hours);
|
2017-12-17 08:13:26 +00:00
|
|
|
}
|
2019-07-10 17:28:33 +00:00
|
|
|
const auto onlineFull = base::unixtime::parse(online);
|
|
|
|
const auto nowFull = base::unixtime::parse(now);
|
2017-12-17 08:13:26 +00:00
|
|
|
if (onlineFull.date() == nowFull.date()) {
|
|
|
|
const auto onlineTime = onlineFull.time().toString(cTimeFormat());
|
2019-06-19 16:39:25 +00:00
|
|
|
return tr::lng_status_lastseen_today(tr::now, lt_time, onlineTime);
|
2017-12-17 08:13:26 +00:00
|
|
|
} else if (onlineFull.date().addDays(1) == nowFull.date()) {
|
|
|
|
const auto onlineTime = onlineFull.time().toString(cTimeFormat());
|
2019-06-19 16:39:25 +00:00
|
|
|
return tr::lng_status_lastseen_yesterday(tr::now, lt_time, onlineTime);
|
2017-12-17 08:13:26 +00:00
|
|
|
}
|
2021-10-07 05:37:42 +00:00
|
|
|
const auto date = onlineFull.date().toString(cDateFormat());
|
2019-06-19 16:39:25 +00:00
|
|
|
return tr::lng_status_lastseen_date(tr::now, lt_date, date);
|
2017-12-17 08:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-07-10 17:28:33 +00:00
|
|
|
const auto onlineFull = base::unixtime::parse(user->onlineTill);
|
|
|
|
const auto nowFull = base::unixtime::parse(now);
|
2017-12-17 08:13:26 +00:00
|
|
|
if (onlineFull.date() == nowFull.date()) {
|
|
|
|
const auto onlineTime = onlineFull.time().toString(cTimeFormat());
|
2019-06-19 16:39:25 +00:00
|
|
|
return tr::lng_status_lastseen_today(tr::now, lt_time, onlineTime);
|
2017-12-17 08:13:26 +00:00
|
|
|
} else if (onlineFull.date().addDays(1) == nowFull.date()) {
|
|
|
|
const auto onlineTime = onlineFull.time().toString(cTimeFormat());
|
2019-06-19 16:39:25 +00:00
|
|
|
return tr::lng_status_lastseen_yesterday(tr::now, lt_time, onlineTime);
|
2017-12-17 08:13:26 +00:00
|
|
|
}
|
2021-10-07 05:37:42 +00:00
|
|
|
const auto date = onlineFull.date().toString(cDateFormat());
|
2017-12-17 08:13:26 +00:00
|
|
|
const auto time = onlineFull.time().toString(cTimeFormat());
|
2019-06-19 16:39:25 +00:00
|
|
|
return tr::lng_status_lastseen_date_time(tr::now, lt_date, date, lt_time, time);
|
2017-12-17 08:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-07-18 08:51:11 +00:00
|
|
|
if (user->isServiceUser() || user->isBot()) {
|
2017-12-17 08:13:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return OnlineTextActive(user->onlineTill, now);
|
|
|
|
}
|
|
|
|
|
2020-12-02 10:52:19 +00:00
|
|
|
bool IsUserOnline(not_null<UserData*> user) {
|
|
|
|
return OnlineTextActive(user, base::unixtime::now());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChannelHasActiveCall(not_null<ChannelData*> channel) {
|
2021-07-08 13:11:09 +00:00
|
|
|
return (channel->flags() & ChannelDataFlag::CallNotEmpty);
|
2019-06-17 14:37:29 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 09:20:56 +00:00
|
|
|
} // namespace Data
|