-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Expand file tree
/
Copy pathdata_lastseen_status.h
More file actions
132 lines (116 loc) · 3.84 KB
/
Copy pathdata_lastseen_status.h
File metadata and controls
132 lines (116 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
namespace Data {
inline constexpr auto kLifeStartDate = 1375315200; // Let it be 01.08.2013.
class LastseenStatus final {
public:
LastseenStatus() = default;
[[nodiscard]] static LastseenStatus Recently(bool byMe = false) {
return LastseenStatus(kRecentlyValue, false, byMe);
}
[[nodiscard]] static LastseenStatus WithinWeek(bool byMe = false) {
return LastseenStatus(kWithinWeekValue, false, byMe);
}
[[nodiscard]] static LastseenStatus WithinMonth(bool byMe = false) {
return LastseenStatus(kWithinMonthValue, false, byMe);
}
[[nodiscard]] static LastseenStatus LongAgo(bool byMe = false) {
return LastseenStatus(kLongAgoValue, false, byMe);
}
[[nodiscard]] static LastseenStatus OnlineTill(
TimeId till,
bool local = false,
bool hiddenByMe = false) {
return (till >= kLifeStartDate + kSpecialValueSkip)
? LastseenStatus(till - kLifeStartDate, !local, hiddenByMe)
: LongAgo(hiddenByMe);
}
[[nodiscard]] bool isHidden() const {
return !_available;
}
[[nodiscard]] bool isRecently() const {
return !_available && (_value == kRecentlyValue);
}
[[nodiscard]] bool isWithinWeek() const {
return !_available && (_value == kWithinWeekValue);
}
[[nodiscard]] bool isWithinMonth() const {
return !_available && (_value == kWithinMonthValue);
}
[[nodiscard]] bool isLongAgo() const {
return !_available && (_value == kLongAgoValue);
}
[[nodiscard]] bool isHiddenByMe() const {
return _hiddenByMe;
}
[[nodiscard]] bool isOnline(TimeId now) const {
return (_value >= kSpecialValueSkip)
&& (kLifeStartDate + _value > now);
}
[[nodiscard]] bool isLocalOnlineValue() const {
return !_available && (_value >= kSpecialValueSkip);
}
[[nodiscard]] TimeId onlineTill() const {
return (_value >= kSpecialValueSkip)
? (kLifeStartDate + _value)
: 0;
}
[[nodiscard]] uint32 serialize() const {
return (_value & 0x3FFFFFFF)
| (_available << 30)
| (_hiddenByMe << 31);
}
[[nodiscard]] static LastseenStatus FromSerialized(uint32 value) {
auto result = LastseenStatus();
result._value = value & 0x3FFFFFFF;
result._available = (value >> 30) & 1;
result._hiddenByMe = (value >> 31) & 1;
return result.valid() ? result : LastseenStatus();
}
[[nodiscard]] static LastseenStatus FromLegacy(int32 value) {
if (value == -2) {
return LastseenStatus::Recently();
} else if (value == -3) {
return LastseenStatus::WithinWeek();
} else if (value == -4) {
return LastseenStatus::WithinMonth();
} else if (value < -30) {
return LastseenStatus::OnlineTill(-value, true);
} else if (value > 0) {
return LastseenStatus::OnlineTill(value);
}
return LastseenStatus();
}
friend inline constexpr auto operator<=>(
LastseenStatus,
LastseenStatus) = default;
friend inline constexpr bool operator==(
LastseenStatus a,
LastseenStatus b) = default;
private:
static constexpr auto kLongAgoValue = uint32(0);
static constexpr auto kRecentlyValue = uint32(1);
static constexpr auto kWithinWeekValue = uint32(2);
static constexpr auto kWithinMonthValue = uint32(3);
static constexpr auto kSpecialValueSkip = uint32(4);
static constexpr auto kValidAfter = kLifeStartDate + kSpecialValueSkip;
[[nodiscard]] bool valid() const {
constexpr auto kMaxSum = uint32(std::numeric_limits<TimeId>::max());
return (kMaxSum - _value > uint32(kLifeStartDate))
&& (!_available || (_value >= kSpecialValueSkip));
}
LastseenStatus(uint32 value, bool available, bool hiddenByMe)
: _value(value)
, _available(available ? 1 : 0)
, _hiddenByMe(hiddenByMe ? 1 : 0) {
}
uint32 _value : 30 = 0;
uint32 _available : 1 = 0;
uint32 _hiddenByMe : 1 = 0;
};
} // namespace Data