/
NeoChapay
/
telegraf
Обзор
Документация
Войти
/
NeoChapay
/
telegraf
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/chatdata.cpp
356 строк
11 KB
mbarashkov
Достаточно серьёзно оптимизирована работа с getUserInformation
26 дек 2024, 19:57
26 дек 2024, 19:57
36e41b5
Код
Авторство
О чём код?
#include "telegrafutils.h" #include "chatlistmodel.h" ChatListModel::ChatData::ChatData(TDLibWrapper *tdLibWrapper, const QVariantMap &data) : tdLibWrapper(tdLibWrapper), chatData(data), chatId(data.value(ID).toLongLong()), order(data.value(ORDER).toLongLong()), groupId(0), verified(false), archived(false), memberStatus(TDLibWrapper::ChatMemberStatusUnknown), secretChatState(TDLibWrapper::SecretChatStateUnknown) { const QVariantMap type(data.value(TYPE).toMap()); switch (chatType = TDLibWrapper::chatTypeFromString(type.value(_TYPE).toString())) { case TDLibWrapper::ChatTypeBasicGroup: groupId = type.value(BASIC_GROUP_ID).toLongLong(); break; case TDLibWrapper::ChatTypeSuperGroup: groupId = type.value(SUPERGROUP_ID).toLongLong(); break; case TDLibWrapper::ChatTypeUnknown: case TDLibWrapper::ChatTypePrivate: case TDLibWrapper::ChatTypeSecret: case TDLibWrapper::ChatTypeBot: break; } } int ChatListModel::ChatData::compareTo(const ChatData *other) const { /*if (order == other->order) { return (chatId < other->chatId) ? 1 : -1; } else { // This puts most recent ones to the top of the list return (order < other->order) ? 1 : -1; }*/ return senderMessageDate() < other->senderMessageDate() ? 1 : -1; } bool ChatListModel::ChatData::setOrder(const QString &newOrder) { if (!newOrder.isEmpty()) { chatData.insert(ORDER, newOrder); order = newOrder.toLongLong(); return true; } return false; } inline const QVariant ChatListModel::ChatData::lastMessage(const QString &key) const { return chatData.value(LAST_MESSAGE).toMap().value(key); } QString ChatListModel::ChatData::title() const { return chatData.value(TITLE).toString(); } int ChatListModel::ChatData::unreadCount() const { return chatData.value(UNREAD_COUNT).toInt(); } int ChatListModel::ChatData::unreadMentionCount() const { return chatData.value(UNREAD_MENTION_COUNT).toInt(); } QVariant ChatListModel::ChatData::availableReactions() const { return chatData.value(AVAILABLE_REACTIONS); } int ChatListModel::ChatData::unreadReactionCount() const { return chatData.value(UNREAD_REACTION_COUNT).toInt(); } QVariantMap ChatListModel::ChatData::photoSmall() const { return chatData.value(PHOTO).toMap().value(SMALL).toMap(); } qlonglong ChatListModel::ChatData::lastReadInboxMessageId() const { return chatData.value(LAST_READ_INBOX_MESSAGE_ID).toLongLong(); } qlonglong ChatListModel::ChatData::senderUserId() const { return lastMessage(SENDER_ID).toMap().value(USER_ID).toLongLong(); } qlonglong ChatListModel::ChatData::senderChatId() const { return lastMessage(SENDER_ID).toMap().value(CHAT_ID).toLongLong(); } bool ChatListModel::ChatData::senderIsChat() const { return lastMessage(SENDER_ID).toMap().value(_TYPE).toString() == "messageSenderChat"; } qlonglong ChatListModel::ChatData::senderMessageDate() const { return lastMessage(DATE).toLongLong(); } QString ChatListModel::ChatData::senderMessageText() const { qlonglong myUserId = tdLibWrapper->getMyUserId(); return TelegrafUtils::getMessageShortText(tdLibWrapper, lastMessage(CONTENT).toMap(), isChannel(), myUserId, lastMessage(SENDER_ID).toMap() ); } QString ChatListModel::ChatData::senderMessageStatus() const { qlonglong myUserId = tdLibWrapper->getMyUserId(); if (isChannel() || myUserId != senderUserId() || myUserId == chatId) { return ""; } if (lastMessage(ID) == chatData.value(LAST_READ_OUTBOX_MESSAGE_ID)) { return " ✅"; } else { QVariantMap lastMessage = chatData.value(LAST_MESSAGE).toMap(); if (lastMessage.contains(SENDING_STATE)) { QVariantMap sendingState = lastMessage.value(SENDING_STATE).toMap(); if (sendingState.value(_TYPE).toString() == "messageSendingStatePending") { return " 🕙"; } else { return " ❌"; } } else { return " ☑️"; } } } qlonglong ChatListModel::ChatData::draftMessageDate() const { QVariantMap draft = chatData.value(DRAFT_MESSAGE).toMap(); if(draft.isEmpty()) { return qlonglong(0); } return draft.value(DATE).toLongLong(); } QString ChatListModel::ChatData::draftMessageText() const { QVariantMap draft = chatData.value(DRAFT_MESSAGE).toMap(); if(draft.isEmpty()) { return QString(); } return draft.value("input_message_text").toMap().value(TEXT).toMap().value(TEXT).toString(); } bool ChatListModel::ChatData::isBasicGroup() const { return chatType == TDLibWrapper::ChatTypeBasicGroup; } bool ChatListModel::ChatData::isSuperGroup() const { return chatType == TDLibWrapper::ChatTypeSuperGroup; } bool ChatListModel::ChatData::isGroup() const { return isSuperGroup() || isBasicGroup(); } bool ChatListModel::ChatData::isPrivate() const { return chatType == TDLibWrapper::ChatTypePrivate; } qlonglong ChatListModel::ChatData::respondentUserId() const { //NOTE: will work only for chatType = ChatTypePrivate return chatData.value(TYPE).toMap().value(USER_ID).toLongLong(); } bool ChatListModel::ChatData::isContact() const { auto userId = respondentUserId(); if (tdLibWrapper->hasUserInformation(userId)) { return tdLibWrapper->getUserInformation(respondentUserId()).value("is_contact").toBool(); } return false; } bool ChatListModel::ChatData::isBot() const { //need to refactor auto userId = respondentUserId(); if (tdLibWrapper->hasUserInformation(userId)) { auto type = tdLibWrapper->getUserInformation(respondentUserId()).value(TYPE).toMap().value(_TYPE).toString(); return type.contains("userTypeBot"); } return false;//chatType == TDLibWrapper::ChatTypeBot; } bool ChatListModel::ChatData::isMute() const { return chatData.value(NOTIFICATION_SETTINGS).toMap().value("mute_for").toULongLong() > 0; } bool ChatListModel::ChatData::isChannel() const { return chatData.value(TYPE).toMap().value(IS_CHANNEL).toBool(); } bool ChatListModel::ChatData::isHidden() const { // Cover all enum values so that compiler warns us when/if enum gets extended switch (chatType) { case TDLibWrapper::ChatTypeBasicGroup: case TDLibWrapper::ChatTypeSuperGroup: switch (memberStatus) { case TDLibWrapper::ChatMemberStatusLeft: case TDLibWrapper::ChatMemberStatusUnknown: case TDLibWrapper::ChatMemberStatusBanned: return true; case TDLibWrapper::ChatMemberStatusCreator: case TDLibWrapper::ChatMemberStatusAdministrator: case TDLibWrapper::ChatMemberStatusMember: case TDLibWrapper::ChatMemberStatusRestricted: if (chatData.value(LAST_MESSAGE).isNull()) { return true; } break; } break; case TDLibWrapper::ChatTypeUnknown: return true; case TDLibWrapper::ChatTypePrivate: case TDLibWrapper::ChatTypeBot: if (chatData.value(LAST_MESSAGE).isNull()) { return true; } break; case TDLibWrapper::ChatTypeSecret: if (secretChatState == TDLibWrapper::SecretChatStateClosed) { return true; } break; } return false; } bool ChatListModel::ChatData::isMarkedAsUnread() const { return chatData.value(IS_MARKED_AS_UNREAD).toBool(); } bool ChatListModel::ChatData::isPinned() const { return chatData.value(IS_PINNED).toBool(); } bool ChatListModel::ChatData::updateUnreadCount(int count) { const int prevUnreadCount(unreadCount()); chatData.insert(UNREAD_COUNT, count); return prevUnreadCount != unreadCount(); } bool ChatListModel::ChatData::updateLastReadInboxMessageId(qlonglong messageId) { const qlonglong prevLastReadInboxMessageId(lastReadInboxMessageId()); chatData.insert(LAST_READ_INBOX_MESSAGE_ID, messageId); return prevLastReadInboxMessageId != lastReadInboxMessageId(); } QVector<int> ChatListModel::ChatData::updateLastMessage(const QVariantMap &message) { const qlonglong prevSenderUserId(senderUserId()); const qlonglong prevSenderMessageDate(senderMessageDate()); const QString prevSenderMessageText(senderMessageText()); const QString prevSenderMessageStatus(senderMessageStatus()); chatData.insert(LAST_MESSAGE, message); QVector<int> changedRoles; changedRoles.append(RoleDisplay); if (prevSenderUserId != senderUserId()) { changedRoles.append(RoleLastMessageSenderId); } if (prevSenderMessageDate != senderMessageDate()) { changedRoles.append(RoleLastMessageDate); } if (prevSenderMessageText != senderMessageText()) { changedRoles.append(RoleFilter); changedRoles.append(RoleLastMessageText); } if (prevSenderMessageStatus != senderMessageStatus()) { changedRoles.append(RoleLastMessageStatus); } return changedRoles; } QVector<int> ChatListModel::ChatData::updateGroup(const TDLibWrapper::Group *group) { QVector<int> changedRoles; if (group && groupId == group->groupId) { const TDLibWrapper::ChatMemberStatus groupMemberStatus = group->chatMemberStatus(); if (memberStatus != groupMemberStatus) { memberStatus = groupMemberStatus; changedRoles.append(RoleChatMemberStatus); } // There's no "is_verified" in "basic_group" but that's ok since // it naturally becomes false const bool groupIsVerified = group->groupInfo.value(IS_VERIFIED).toBool(); if (verified != groupIsVerified) { verified = groupIsVerified; changedRoles.append(RoleIsVerified); } } return changedRoles; } QVector<int> ChatListModel::ChatData::updateSecretChat(const QVariantMap &secretChatDetails) { QVector<int> changedRoles; TDLibWrapper::SecretChatState newSecretChatState = TDLibWrapper::secretChatStateFromString(secretChatDetails.value("state").toMap().value(_TYPE).toString()); if (newSecretChatState != secretChatState) { secretChatState = newSecretChatState; changedRoles.append(RoleSecretChatState); } return changedRoles; } ChatListModel::ChatData* ChatListModel::ChatData::clone() { QVariantMap clonedChatData; QList<QString> keys = chatData.keys(); for(int i = 0; i < keys.count(); i++) { clonedChatData.insert(keys[i], QVariant(chatData[keys[i]])); } ChatData* res = new ChatData(tdLibWrapper, clonedChatData); res->chatId = chatId; res->order = order; res->groupId = groupId; res->verified = verified; res->archived = archived; res->chatType = chatType; res->memberStatus = memberStatus; res->secretChatState = secretChatState; return res; }