mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-09-07 20:43:12 +02:00
This commit introduces the ability for you to add messages locally to a chat. These messages are displayed only on the client and are not sent to the server. Key changes include: - Data Layer: - Added a new `LocalMessage` class inheriting from `AyuMessageBase` in `ayu/data/entities.h`. - Implemented functions in `ayu/data/messages_storage.cpp` and `.h` (`addLocalMessage`, `getLocalMessages`, `hasLocalMessages`) to manage the storage of these messages in the AyuGram SQLite database, including creating a new table for local messages. - UI and Display: - Modified `ayu/ui/message_history/history_item.cpp` to ensure `LocalMessage` objects are rendered in the chat history. - Added a visual distinction for local messages by prepending "[Local] " to their text content. - UI Flow: - Implemented a new dialog box (`AddLocalMessageBox` in `boxes/add_local_message_box.cpp` and `.h`) for composing local messages, allowing you to specify a sender name (defaults to the current user) and the message text. - Added a context menu option ("Add Local Message") to the message input field in `HistoryWidget` to launch this dialog. - Testing: - Defined a suite of manual test cases covering data storage, retrieval, UI display, and the creation flow via the new UI, ensuring the feature's correctness and usability. This feature enhances AyuGram by allowing you to annotate chats or add notes directly within the message flow, visible only to yourselves.
166 lines
5 KiB
C++
166 lines
5 KiB
C++
// This is the source code of AyuGram for Desktop.
|
|
//
|
|
// We do not and cannot prevent the use of our code,
|
|
// but be respectful and credit the original author.
|
|
//
|
|
// Copyright @Radolyn, 2025
|
|
#include "ayu/data/messages_storage.h"
|
|
|
|
#include "ayu/ayu_constants.h"
|
|
#include "ayu/data/ayu_database.h"
|
|
#include "ayu/utils/ayu_mapper.h"
|
|
#include "ayu/utils/telegram_helpers.h"
|
|
|
|
#include "base/unixtime.h"
|
|
|
|
#include "data/data_forum_topic.h"
|
|
#include "data/data_session.h"
|
|
|
|
#include "history/history.h"
|
|
#include "history/history_item.h"
|
|
#include "history/history_item_components.h"
|
|
|
|
#include "main/main_session.h"
|
|
|
|
namespace AyuMessages {
|
|
|
|
template<typename DerivedMessage>
|
|
std::vector<AyuMessageBase> convertToBase(const std::vector<DerivedMessage> &messages) {
|
|
std::vector<AyuMessageBase> based;
|
|
based.reserve(messages.size());
|
|
for (const auto &msg : messages) {
|
|
based.push_back(static_cast<AyuMessageBase>(msg));
|
|
}
|
|
return based;
|
|
}
|
|
|
|
void map(not_null<HistoryItem*> item, AyuMessageBase &message) {
|
|
const ID userId = item->history()->owner().session().userId().bare & PeerId::kChatTypeMask;
|
|
|
|
message.userId = userId;
|
|
message.dialogId = getDialogIdFromPeer(item->history()->peer);
|
|
message.groupedId = item->groupId().raw();
|
|
message.peerId = item->history()->peer->id.value & PeerId::kChatTypeMask;
|
|
message.fromId = item->from()->id.value & PeerId::kChatTypeMask;
|
|
if (item->topic()) {
|
|
message.topicId = item->topicRootId().bare;
|
|
} else {
|
|
message.topicId = 0;
|
|
}
|
|
message.messageId = item->id.bare;
|
|
message.date = item->date();
|
|
message.flags = AyuMapper::mapItemFlagsToMTPFlags(item);
|
|
|
|
if (const auto edited = item->Get<HistoryMessageEdited>()) {
|
|
message.editDate = edited->date;
|
|
} else {
|
|
message.editDate = base::unixtime::now();
|
|
}
|
|
|
|
message.views = item->viewsCount();
|
|
message.fwdFlags = 0;
|
|
message.fwdFromId = 0;
|
|
// message.fwdName
|
|
message.fwdDate = 0;
|
|
// message.fwdPostAuthor
|
|
if (const auto msgsigned = item->Get<HistoryMessageSigned>()) {
|
|
message.postAuthor = msgsigned->author.toStdString();
|
|
}
|
|
message.replyFlags = 0;
|
|
message.replyMessageId = 0;
|
|
message.replyPeerId = 0;
|
|
message.replyTopId = 0;
|
|
message.replyForumTopic = false;
|
|
// message.replySerialized
|
|
// message.replyMarkupSerialized
|
|
message.entityCreateDate = base::unixtime::now();
|
|
|
|
auto serializedText = AyuMapper::serializeTextWithEntities(item);
|
|
message.text = serializedText.first;
|
|
message.textEntities = serializedText.second;
|
|
|
|
// todo: implement mapping
|
|
message.mediaPath = "/";
|
|
// message.hqThumbPath
|
|
message.documentType = DOCUMENT_TYPE_NONE;
|
|
// message.documentSerialized
|
|
// message.thumbsSerialized
|
|
// message.documentAttributesSerialized
|
|
// message.mimeType
|
|
}
|
|
|
|
void addEditedMessage(not_null<HistoryItem *> item) {
|
|
EditedMessage message;
|
|
map(item, message);
|
|
|
|
if (message.text.empty()) {
|
|
return;
|
|
}
|
|
|
|
AyuDatabase::addEditedMessage(message);
|
|
}
|
|
|
|
std::vector<AyuMessageBase> getEditedMessages(not_null<HistoryItem*> item, ID minId, ID maxId, int totalLimit) {
|
|
const ID userId = item->history()->owner().session().userId().bare & PeerId::kChatTypeMask;
|
|
const auto dialogId = getDialogIdFromPeer(item->history()->peer);
|
|
const auto msgId = item->id.bare;
|
|
|
|
return convertToBase(AyuDatabase::getEditedMessages(userId, dialogId, msgId, minId, maxId, totalLimit));
|
|
}
|
|
|
|
bool hasRevisions(not_null<HistoryItem*> item) {
|
|
const ID userId = item->history()->owner().session().userId().bare & PeerId::kChatTypeMask;
|
|
const auto dialogId = getDialogIdFromPeer(item->history()->peer);
|
|
const auto msgId = item->id.bare;
|
|
|
|
return AyuDatabase::hasRevisions(userId, dialogId, msgId);
|
|
}
|
|
|
|
void addDeletedMessage(not_null<HistoryItem*> item) {
|
|
DeletedMessage message;
|
|
map(item, message);
|
|
|
|
if (message.text.empty()) {
|
|
return;
|
|
}
|
|
|
|
AyuDatabase::addDeletedMessage(message);
|
|
}
|
|
|
|
std::vector<AyuMessageBase>
|
|
getDeletedMessages(not_null<PeerData*> peer, ID topicId, ID minId, ID maxId, int totalLimit) {
|
|
const ID userId = peer->session().userId().bare & PeerId::kChatTypeMask;
|
|
return convertToBase(
|
|
AyuDatabase::getDeletedMessages(userId, getDialogIdFromPeer(peer), topicId, minId, maxId, totalLimit));
|
|
}
|
|
|
|
bool hasDeletedMessages(not_null<PeerData*> peer, ID topicId) {
|
|
const ID userId = peer->session().userId().bare & PeerId::kChatTypeMask;
|
|
return AyuDatabase::hasDeletedMessages(userId, getDialogIdFromPeer(peer), topicId);
|
|
}
|
|
|
|
void addLocalMessage(not_null<HistoryItem*> item) {
|
|
LocalMessage message;
|
|
map(item, message);
|
|
|
|
// Optionally, add checks similar to addEditedMessage or addDeletedMessage
|
|
// if (message.text.empty()) {
|
|
// return;
|
|
// }
|
|
|
|
AyuDatabase::addLocalMessage(message);
|
|
}
|
|
|
|
std::vector<AyuMessageBase>
|
|
getLocalMessages(not_null<PeerData*> peer, ID topicId, ID minId, ID maxId, int totalLimit) {
|
|
const ID userId = peer->session().userId().bare & PeerId::kChatTypeMask;
|
|
return convertToBase(
|
|
AyuDatabase::getLocalMessages(userId, getDialogIdFromPeer(peer), topicId, minId, maxId, totalLimit));
|
|
}
|
|
|
|
bool hasLocalMessages(not_null<PeerData*> peer, ID topicId) {
|
|
const ID userId = peer->session().userId().bare & PeerId::kChatTypeMask;
|
|
return AyuDatabase::hasLocalMessages(userId, getDialogIdFromPeer(peer), topicId);
|
|
}
|
|
|
|
}
|