Use server time in dialogs list sorting.
This way it won't mess up when you change your local time.
This commit is contained in:
		
							parent
							
								
									f68cefbdc1
								
							
						
					
					
						commit
						9972f7b90e
					
				
					 10 changed files with 47 additions and 42 deletions
				
			
		|  | @ -636,7 +636,7 @@ void ApiWrap::historyDialogEntryApplied(not_null<History*> history) { | ||||||
| 			if (!chat->haveLeft()) { | 			if (!chat->haveLeft()) { | ||||||
| 				Local::addSavedPeer( | 				Local::addSavedPeer( | ||||||
| 					history->peer, | 					history->peer, | ||||||
| 					history->chatsListDate()); | 					ParseDateTime(history->chatsListTimeId())); | ||||||
| 			} | 			} | ||||||
| 		} else if (const auto channel = history->peer->asChannel()) { | 		} else if (const auto channel = history->peer->asChannel()) { | ||||||
| 			const auto inviter = channel->inviter; | 			const auto inviter = channel->inviter; | ||||||
|  | @ -653,12 +653,11 @@ void ApiWrap::historyDialogEntryApplied(not_null<History*> history) { | ||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if (!history->chatsListDate().isNull() | 	if (history->chatsListTimeId() != 0 && history->loadedAtBottom()) { | ||||||
| 		&& history->loadedAtBottom()) { |  | ||||||
| 		if (const auto channel = history->peer->asChannel()) { | 		if (const auto channel = history->peer->asChannel()) { | ||||||
| 			const auto inviter = channel->inviter; | 			const auto inviter = channel->inviter; | ||||||
| 			if (inviter != 0 | 			if (inviter != 0 | ||||||
| 				&& history->chatsListDate() <= ParseDateTime(channel->inviteDate) | 				&& history->chatsListTimeId() <= channel->inviteDate | ||||||
| 				&& channel->amIn()) { | 				&& channel->amIn()) { | ||||||
| 				if (const auto from = App::userLoaded(inviter)) { | 				if (const auto from = App::userLoaded(inviter)) { | ||||||
| 					history->insertJoinedMessage(true); | 					history->insertJoinedMessage(true); | ||||||
|  |  | ||||||
|  | @ -125,6 +125,14 @@ QDateTime ParseDateTime(TimeId serverTime) { | ||||||
| 	return QDateTime::fromTime_t(serverTime - unixtimeDelta); | 	return QDateTime::fromTime_t(serverTime - unixtimeDelta); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | TimeId ServerTimeFromParsed(const QDateTime &date) { | ||||||
|  | 	if (date.isNull()) { | ||||||
|  | 		return TimeId(0); | ||||||
|  | 	} | ||||||
|  | 	QReadLocker locker(&unixtimeLock); | ||||||
|  | 	return date.toTime_t() + unixtimeDelta; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // Precise timing functions / rand init
 | // Precise timing functions / rand init
 | ||||||
| 
 | 
 | ||||||
| struct CRYPTO_dynlock_value { | struct CRYPTO_dynlock_value { | ||||||
|  |  | ||||||
|  | @ -196,6 +196,7 @@ uint64 msgid(); | ||||||
| int GetNextRequestId(); | int GetNextRequestId(); | ||||||
| 
 | 
 | ||||||
| QDateTime ParseDateTime(TimeId serverTime); | QDateTime ParseDateTime(TimeId serverTime); | ||||||
|  | TimeId ServerTimeFromParsed(const QDateTime &date); | ||||||
| 
 | 
 | ||||||
| inline void mylocaltime(struct tm * _Tm, const time_t * _Time) { | inline void mylocaltime(struct tm * _Tm, const time_t * _Time) { | ||||||
| #ifdef Q_OS_WIN | #ifdef Q_OS_WIN | ||||||
|  |  | ||||||
|  | @ -150,7 +150,7 @@ void Feed::unregisterOne(not_null<ChannelData*> channel) { | ||||||
| void Feed::updateLastMessage(not_null<HistoryItem*> item) { | void Feed::updateLastMessage(not_null<HistoryItem*> item) { | ||||||
| 	if (justUpdateLastMessage(item)) { | 	if (justUpdateLastMessage(item)) { | ||||||
| 		if (_lastMessage && *_lastMessage) { | 		if (_lastMessage && *_lastMessage) { | ||||||
| 			setChatsListDate(ItemDateTime(*_lastMessage)); | 			setChatsListTimeId((*_lastMessage)->date()); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | @ -304,7 +304,7 @@ void Feed::setLastMessageFromChannels() { | ||||||
| 
 | 
 | ||||||
| void Feed::updateChatsListDate() { | void Feed::updateChatsListDate() { | ||||||
| 	if (_lastMessage && *_lastMessage) { | 	if (_lastMessage && *_lastMessage) { | ||||||
| 		setChatsListDate(ItemDateTime(*_lastMessage)); | 		setChatsListTimeId((*_lastMessage)->date()); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -19,11 +19,11 @@ namespace { | ||||||
| 
 | 
 | ||||||
| auto DialogsPosToTopShift = 0; | auto DialogsPosToTopShift = 0; | ||||||
| 
 | 
 | ||||||
| uint64 DialogPosFromDate(const QDateTime &date) { | uint64 DialogPosFromDate(TimeId date) { | ||||||
| 	if (date.isNull()) { | 	if (!date) { | ||||||
| 		return 0; | 		return 0; | ||||||
| 	} | 	} | ||||||
| 	return (uint64(date.toTime_t()) << 32) | (++DialogsPosToTopShift); | 	return (uint64(date) << 32) | (++DialogsPosToTopShift); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| uint64 ProxyPromotedDialogPos() { | uint64 ProxyPromotedDialogPos() { | ||||||
|  | @ -73,7 +73,7 @@ void Entry::updateChatListSortPosition() { | ||||||
| 		? ProxyPromotedDialogPos() | 		? ProxyPromotedDialogPos() | ||||||
| 		: isPinnedDialog() | 		: isPinnedDialog() | ||||||
| 		? PinnedDialogPos(_pinnedIndex) | 		? PinnedDialogPos(_pinnedIndex) | ||||||
| 		: DialogPosFromDate(adjustChatListDate()); | 		: DialogPosFromDate(adjustChatListTimeId()); | ||||||
| 	if (needUpdateInChatList()) { | 	if (needUpdateInChatList()) { | ||||||
| 		setChatListExistence(true); | 		setChatListExistence(true); | ||||||
| 	} | 	} | ||||||
|  | @ -94,8 +94,8 @@ void Entry::setChatListExistence(bool exists) { | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| QDateTime Entry::adjustChatListDate() const { | TimeId Entry::adjustChatListTimeId() const { | ||||||
| 	return chatsListDate(); | 	return chatsListTimeId(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void Entry::changedInChatListHook(Dialogs::Mode list, bool added) { | void Entry::changedInChatListHook(Dialogs::Mode list, bool added) { | ||||||
|  | @ -128,13 +128,13 @@ PositionChange Entry::adjustByPosInChatList( | ||||||
| 	return { movedFrom, movedTo }; | 	return { movedFrom, movedTo }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void Entry::setChatsListDate(QDateTime date) { | void Entry::setChatsListTimeId(TimeId date) { | ||||||
| 	if (!_lastMessageDate.isNull() && _lastMessageDate >= date) { | 	if (_lastMessageTimeId && _lastMessageTimeId >= date) { | ||||||
| 		if (!inChatList(Dialogs::Mode::All)) { | 		if (!inChatList(Dialogs::Mode::All)) { | ||||||
| 			return; | 			return; | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	_lastMessageDate = date; | 	_lastMessageTimeId = date; | ||||||
| 	updateChatListSortPosition(); | 	updateChatListSortPosition(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -65,7 +65,7 @@ public: | ||||||
| 		return _sortKeyInChatList; | 		return _sortKeyInChatList; | ||||||
| 	} | 	} | ||||||
| 	void updateChatListSortPosition(); | 	void updateChatListSortPosition(); | ||||||
| 	void setChatsListDate(QDateTime date); | 	void setChatsListTimeId(TimeId date); | ||||||
| 	virtual void updateChatListExistence(); | 	virtual void updateChatListExistence(); | ||||||
| 	bool needUpdateInChatList() const; | 	bool needUpdateInChatList() const; | ||||||
| 
 | 
 | ||||||
|  | @ -94,8 +94,8 @@ public: | ||||||
| 		paintUserpic(p, rtl() ? (w - x - size) : x, y, size); | 		paintUserpic(p, rtl() ? (w - x - size) : x, y, size); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	QDateTime chatsListDate() const { | 	TimeId chatsListTimeId() const { | ||||||
| 		return _lastMessageDate; | 		return _lastMessageTimeId; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	virtual ~Entry() = default; | 	virtual ~Entry() = default; | ||||||
|  | @ -104,7 +104,7 @@ public: | ||||||
| 	mutable Text lastItemTextCache; | 	mutable Text lastItemTextCache; | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
| 	virtual QDateTime adjustChatListDate() const; | 	virtual TimeId adjustChatListTimeId() const; | ||||||
| 	virtual void changedInChatListHook(Dialogs::Mode list, bool added); | 	virtual void changedInChatListHook(Dialogs::Mode list, bool added); | ||||||
| 	virtual void changedChatListPinHook(); | 	virtual void changedChatListPinHook(); | ||||||
| 
 | 
 | ||||||
|  | @ -118,7 +118,7 @@ private: | ||||||
| 	uint64 _sortKeyInChatList = 0; | 	uint64 _sortKeyInChatList = 0; | ||||||
| 	int _pinnedIndex = 0; | 	int _pinnedIndex = 0; | ||||||
| 	bool _isProxyPromoted = false; | 	bool _isProxyPromoted = false; | ||||||
| 	QDateTime _lastMessageDate; | 	TimeId _lastMessageTimeId = 0; | ||||||
| 
 | 
 | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1784,9 +1784,9 @@ void DialogsInner::applyDialog(const MTPDdialog &dialog) { | ||||||
| 	history->applyDialog(dialog); | 	history->applyDialog(dialog); | ||||||
| 
 | 
 | ||||||
| 	if (!history->useProxyPromotion() && !history->isPinnedDialog()) { | 	if (!history->useProxyPromotion() && !history->isPinnedDialog()) { | ||||||
| 		const auto date = history->chatsListDate(); | 		const auto date = history->chatsListTimeId(); | ||||||
| 		if (!date.isNull()) { | 		if (date != 0) { | ||||||
| 			addSavedPeersAfter(date); | 			addSavedPeersAfter(ParseDateTime(date)); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	_contactsNoDialogs->del(history); | 	_contactsNoDialogs->del(history); | ||||||
|  | @ -1818,7 +1818,7 @@ void DialogsInner::addSavedPeersAfter(const QDateTime &date) { | ||||||
| 	auto &saved = cRefSavedPeersByTime(); | 	auto &saved = cRefSavedPeersByTime(); | ||||||
| 	while (!saved.isEmpty() && (date.isNull() || date < saved.lastKey())) { | 	while (!saved.isEmpty() && (date.isNull() || date < saved.lastKey())) { | ||||||
| 		const auto history = App::history(saved.last()->id); | 		const auto history = App::history(saved.last()->id); | ||||||
| 		history->setChatsListDate(saved.lastKey()); | 		history->setChatsListTimeId(ServerTimeFromParsed(saved.lastKey())); | ||||||
| 		_contactsNoDialogs->del(history); | 		_contactsNoDialogs->del(history); | ||||||
| 		saved.remove(saved.lastKey(), saved.last()); | 		saved.remove(saved.lastKey(), saved.last()); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -1827,14 +1827,11 @@ std::shared_ptr<AdminLog::LocalIdManager> History::adminLogIdManager() { | ||||||
| 	return result; | 	return result; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| QDateTime History::adjustChatListDate() const { | TimeId History::adjustChatListTimeId() const { | ||||||
| 	const auto result = chatsListDate(); | 	const auto result = chatsListTimeId(); | ||||||
| 	if (const auto draft = cloudDraft()) { | 	if (const auto draft = cloudDraft()) { | ||||||
| 		if (!Data::draftIsNull(draft)) { | 		if (!Data::draftIsNull(draft)) { | ||||||
| 			const auto draftResult = ParseDateTime(draft->date); | 			return std::max(result, draft->date); | ||||||
| 			if (draftResult > result) { |  | ||||||
| 				return draftResult; |  | ||||||
| 			} |  | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return result; | 	return result; | ||||||
|  | @ -2182,7 +2179,7 @@ void History::setLastMessage(HistoryItem *item) { | ||||||
| 		if (const auto feed = peer->feed()) { | 		if (const auto feed = peer->feed()) { | ||||||
| 			feed->updateLastMessage(item); | 			feed->updateLastMessage(item); | ||||||
| 		} | 		} | ||||||
| 		setChatsListDate(ItemDateTime(item)); | 		setChatsListTimeId(item->date()); | ||||||
| 	} else if (!_lastMessage || *_lastMessage) { | 	} else if (!_lastMessage || *_lastMessage) { | ||||||
| 		_lastMessage = nullptr; | 		_lastMessage = nullptr; | ||||||
| 		updateChatListEntry(); | 		updateChatListEntry(); | ||||||
|  | @ -2500,8 +2497,8 @@ HistoryService *History::insertJoinedMessage(bool unread) { | ||||||
| 					inviter, | 					inviter, | ||||||
| 					flags); | 					flags); | ||||||
| 				addNewInTheMiddle(_joinedMessage, blockIndex, itemIndex); | 				addNewInTheMiddle(_joinedMessage, blockIndex, itemIndex); | ||||||
| 				const auto lastDate = chatsListDate(); | 				const auto lastDate = chatsListTimeId(); | ||||||
| 				if (lastDate.isNull() || ParseDateTime(inviteDate) >= lastDate) { | 				if (!lastDate || inviteDate >= lastDate) { | ||||||
| 					setLastMessage(_joinedMessage); | 					setLastMessage(_joinedMessage); | ||||||
| 					if (unread) { | 					if (unread) { | ||||||
| 						newItemAdded(_joinedMessage); | 						newItemAdded(_joinedMessage); | ||||||
|  |  | ||||||
|  | @ -445,7 +445,7 @@ private: | ||||||
| 		not_null<Element*> view); | 		not_null<Element*> view); | ||||||
| 	void removeNotification(not_null<HistoryItem*> item); | 	void removeNotification(not_null<HistoryItem*> item); | ||||||
| 
 | 
 | ||||||
| 	QDateTime adjustChatListDate() const override; | 	TimeId adjustChatListTimeId() const override; | ||||||
| 	void changedInChatListHook(Dialogs::Mode list, bool added) override; | 	void changedInChatListHook(Dialogs::Mode list, bool added) override; | ||||||
| 	void changedChatListPinHook() override; | 	void changedChatListPinHook() override; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -334,14 +334,14 @@ void Controller::showJumpToDate(Dialogs::Key chat, QDate requestedDate) { | ||||||
| 						return history->blocks.front()->messages.front()->dateTime().date(); | 						return history->blocks.front()->messages.front()->dateTime().date(); | ||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
| 			} else if (!history->chatsListDate().isNull()) { | 			} else if (history->chatsListTimeId() != 0) { | ||||||
| 				return history->chatsListDate().date(); | 				return ParseDateTime(history->chatsListTimeId()).date(); | ||||||
| 			} | 			} | ||||||
| 		} else if (const auto feed = chat.feed()) { | 		} else if (const auto feed = chat.feed()) { | ||||||
| 			/*if (chatScrollPosition(feed)) { // #TODO feeds save position
 | 			/*if (chatScrollPosition(feed)) { // #TODO feeds save position
 | ||||||
| 
 | 
 | ||||||
| 			} else */if (!feed->chatsListDate().isNull()) { | 			} else */if (feed->chatsListTimeId() != 0) { | ||||||
| 				return feed->chatsListDate().date(); | 				return ParseDateTime(feed->chatsListTimeId()).date(); | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		return QDate::currentDate(); | 		return QDate::currentDate(); | ||||||
|  | @ -351,12 +351,12 @@ void Controller::showJumpToDate(Dialogs::Key chat, QDate requestedDate) { | ||||||
| 			if (const auto channel = history->peer->migrateTo()) { | 			if (const auto channel = history->peer->migrateTo()) { | ||||||
| 				history = App::historyLoaded(channel); | 				history = App::historyLoaded(channel); | ||||||
| 			} | 			} | ||||||
| 			if (history && !history->chatsListDate().isNull()) { | 			if (history && history->chatsListTimeId() != 0) { | ||||||
| 				return history->chatsListDate().date(); | 				return ParseDateTime(history->chatsListTimeId()).date(); | ||||||
| 			} | 			} | ||||||
| 		} else if (const auto feed = chat.feed()) { | 		} else if (const auto feed = chat.feed()) { | ||||||
| 			if (!feed->chatsListDate().isNull()) { | 			if (feed->chatsListTimeId() != 0) { | ||||||
| 				return feed->chatsListDate().date(); | 				return ParseDateTime(feed->chatsListTimeId()).date(); | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		return QDate::currentDate(); | 		return QDate::currentDate(); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 John Preston
						John Preston