/
Shymer123
/
Messenger
Обзор
Документация
Войти
/
Shymer123
/
Messenger
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
UI_Client/SourceFiles/Messages/messages.cpp
215 строк
8 KB
Shymer123
bugs with creating chats and not opening the chat information menu have been fixed, and chat creation has also been confirmed.
17 май 2025, 06:03
17 май 2025, 06:03
621bd0c
Код
Авторство
О чём код?
#include "messages.h" #include "DataModels/message_data.h" #include "Processes/updatedOther/UpdatedOtherProcess.h" #include "RequestResponseHandler/dependencyManager.h" #include "DataModels/user_data.h" #include <QJsonObject> #include <QJsonArray> #include <memory> Message::Message(int chatID) : m_chatID(chatID) { m_communicationHandler = DependencyManager::instance().communicationHandler(); m_userID = UserData::instance().getID(); } Message::Message(const ChatTypes& chatType, int chatID) : m_chatType(chatType), m_chatID(chatID) { m_communicationHandler = DependencyManager::instance().communicationHandler(); m_userID = UserData::instance().getID(); } Message::Message(const ChatTypes& chatType, int chatID, int participantID) : m_chatType(chatType), m_chatID(chatID), m_participantID(participantID) { m_communicationHandler = DependencyManager::instance().communicationHandler(); m_userID = UserData::instance().getID(); } int Message::getChatID() const { return m_chatID; } int Message::getUserID() const { return m_userID; } void Message::getContent(std::function<void(QList<MessageData>)> callback) const { m_communicationHandler->getMessages(m_chatID); disconnect(m_communicationHandler, &CommunicationHandler::getMessagesReceived, nullptr, nullptr); connect(m_communicationHandler, &CommunicationHandler::getMessagesReceived, this, [this, callback](const QJsonObject& response) { QJsonArray messageArray = response.value("messages").toArray(); QList<MessageData> messagesList; for(const QJsonValue& messageValue : std::as_const(messageArray)) { QJsonObject messageObject = messageValue.toObject(); MessageData messageData; int userID = messageObject.value("userID").toInt(); QString userName = messageObject.value("userName").toString(); QDateTime timeMessage = QDateTime::fromString(messageObject.value("timeMessage").toString(), "yyyy-MM-dd HH:mm:ss"); QJsonArray fileArray = messageObject.value("files").toArray(); bool hasFiles = !fileArray.isEmpty(); MessageType messageType = hasFiles ? MessageType::File : MessageType::Text; messageData.setUserID(userID); messageData.setChatID(m_chatID); messageData.setUserName(userName); messageData.setAvatar(QPixmap()); messageData.setTimestamp(timeMessage); if(messageType == MessageType::Text) { QString textMessage = messageObject.value("textMessage").toString(); messageData.setMessageType(MessageType::Text); messageData.setContent<QString>(textMessage); } else if(messageType == MessageType::File) { QString comment = messageObject.value("textMessage").toString(); QJsonObject fileInfo; QJsonArray filesList; for (const QJsonValue& fileValue : std::as_const(fileArray)) { QJsonObject fileObject = fileValue.toObject(); QJsonObject fileEntry; fileEntry["fileName"] = fileObject.value("fileName").toString(); fileEntry["fileSize"] = fileObject.value("fileSize").toInt(); fileEntry["fileID"] = fileObject.value("fileID").toInt(); filesList.append(fileEntry); } fileInfo["files"] = filesList; messageData.setMessageType(MessageType::File); messageData.setContent<QJsonObject>(fileInfo); messageData.setAdditionalData(comment); } messagesList.append(messageData); } callback(messagesList); }); } /////////////////////////////////////////////////////////// TextMessage::TextMessage(const ChatTypes& chatType, int chatID, const QString& text) : Message(chatType, chatID), m_content(text) {} TextMessage::TextMessage(const ChatTypes& chatType, int chatID, int participantID, const QString& text) : Message(chatType, chatID, participantID), m_content(text) {} void TextMessage::insertMessage(const std::shared_ptr<TextMessage>& self, const QString& tempID) const { MessageData data; data.setMessageID(tempID); data.setMessageType(MessageType::Text); data.setUserID(m_userID); data.setChatID(m_chatID); data.setUserName(UserData::instance().getUserName()); data.setTimestamp(QDateTime::currentDateTime()); data.setContent<QString>(m_content); if(m_participantID == -1 && m_chatType == ChatTypes::Group) { std::shared_ptr<Processes::UpdatedOtherProcess> process = std::make_shared<Processes::UpdatedOtherProcess>(); process->fetchParticipantsID(m_chatID, m_chatType, [self, data](const std::vector<int>& participantsID) { self->m_communicationHandler->sendMessage(data, participantsID); }); } else if(m_participantID != -1 && m_chatType == ChatTypes::Dialog) { std::vector<int> participantID = { m_participantID }; m_communicationHandler->sendMessage(data, participantID); } else if(m_participantID == -1 && m_chatType == ChatTypes::Favourite) { std::vector<int> userID = { m_userID }; m_communicationHandler->sendMessage(data, userID); } } //////////////////////////////////////////////////////////// FileMessage::FileMessage(const ChatTypes& chatType, int chatID, const QList<QString>& filesPaths, const QString& comment) : Message(chatType, chatID), m_filesPaths(filesPaths), m_comment(comment) {} FileMessage::FileMessage(const ChatTypes& chatType, int chatID, int participantID, const QList<QString>& filesPaths, const QString& comment) : Message(chatType, chatID, participantID), m_filesPaths(filesPaths), m_comment(comment) {} void FileMessage::insertMessage(const QString& tempID) const { MessageData data; data.setMessageID(tempID); data.setMessageType(MessageType::File); data.setUserID(m_userID); data.setChatID(m_chatID); data.setUserName(UserData::instance().getUserName()); data.setTimestamp(QDateTime::currentDateTime()); data.setContent<QList<QString>>(m_filesPaths); data.setAdditionalData(m_comment); if(m_participantID == -1 && m_chatType == ChatTypes::Group) { std::shared_ptr<Processes::UpdatedOtherProcess> process = std::make_shared<Processes::UpdatedOtherProcess>(); process->fetchParticipantsID(m_chatID, m_chatType, [data, this](const std::vector<int>& participantsID) { m_communicationHandler->sendMessage(data, participantsID); delete this; }); } else if(m_participantID != -1 && m_chatType == ChatTypes::Dialog) { std::vector<int> participantID = { m_participantID }; m_communicationHandler->sendMessage(data, participantID); } else if(m_participantID == -1 && m_chatType == ChatTypes::Favourite) { std::vector<int> userID = { m_userID }; m_communicationHandler->sendMessage(data, userID); } } //////////////////////////////////////////////////////////// PhotoMessage::PhotoMessage(const ChatTypes& chatType, int chatID, const QString& fileName, const QImage& image) : Message(chatType, chatID), m_fileName(fileName), m_image(image) { m_userID = UserData::instance().getID(); } void PhotoMessage::insertMessage() const { MessageData data; data.setMessageType(MessageType::Image); data.setUserID(m_userID); data.setChatID(m_chatID); data.setContent<QImage>(m_image); data.setAdditionalData(m_fileName); }