forked from CPP-Final-Project/Chat_Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoomController.cpp
185 lines (150 loc) · 6.42 KB
/
RoomController.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "RoomController.h"
#include "RoomStorage_Service.h"
RoomController::RoomController(QObject* parent) : QObject(parent)
{
}
RoomController::~RoomController() = default;
QSharedPointer<RoomController> RoomController::instance()
{
if (!shp_instance)
{
shp_instance = QSharedPointer<RoomController>(new RoomController());
}
return shp_instance;
}
//void RoomController::initRoom()
//{
// loadMsgHistory(msg_history_path);
//}
void RoomController::userEntry(const quint32& room_id, QSharedPointer<SrvUser> user_)
{
RoomStorage_Service::getInstance()->addConnecntedUserToRoom(room_id, user_);
QJsonObject connected_message;
connected_message[QStringLiteral("type")] = QStringLiteral("newuser");
connected_message[QStringLiteral("username")] = user_->getUserName();
broadcastSend(connected_message, room_id, user_);
user_->setRoomId(room_id);
}
void RoomController::userLeave(QSharedPointer<SrvUser> user_)
{
quint32 room_id = user_->getRoomId();
if (!room_id)
{
PLOGW << "Room leave request wo room number.";
return;
}
if(RoomStorage_Service::getInstance()->deleteConnecntedUserFromRoom(room_id, user_))
{
QJsonObject disconnectedMessage;
disconnectedMessage[QStringLiteral("type")] = QStringLiteral("userdisconnected");
disconnectedMessage[QStringLiteral("username")] = user_->getUserName();
broadcastSend(disconnectedMessage, room_id, nullptr);
QJsonObject disconnectedMessageforUser;
disconnectedMessageforUser[QStringLiteral("type")] = QStringLiteral("exitRoom");
disconnectedMessageforUser[QStringLiteral("success")] = "success";
sendJson(user_, disconnectedMessageforUser);
user_->setRoomId(0);
}
else
{
PLOGE << "User was not deleted";
}
}
void RoomController::jsonMsgReceived(const quint32& room_id_, QSharedPointer<SrvUser> sender_, const QJsonObject& message_)
{
QJsonObject userMessage;
userMessage[QStringLiteral("type")] = QStringLiteral("message");
userMessage[QStringLiteral("sender")] = sender_->getUserName();
userMessage[QStringLiteral("messagebody")] = message_;
broadcastSend(userMessage, room_id_, nullptr);
}
void RoomController::roomListRequest(QSharedPointer<SrvUser> user_)
{
auto rooms_ptr_list = RoomStorage_Service::getInstance()->getRooms();
QJsonObject roomList;
QJsonArray rooms;
//id name description topic is_private
foreach(const auto room, rooms_ptr_list)
{
QJsonObject room_json;
room_json[QStringLiteral("id")] = int(room->getId());
room_json[QStringLiteral("name")] = room->getName();
room_json[QStringLiteral("description")] = room->getDescription();
room_json[QStringLiteral("topic")] = room->getTopicName();
room_json[QStringLiteral("is_private")] = room->isPrivate();
rooms.append(std::move(room_json));
}
roomList[QStringLiteral("type")] = QStringLiteral("roomList");
roomList[QStringLiteral("rooms")] = rooms;
sendJson(user_, roomList);
}
void RoomController::messageHystoryRequest(quint32 room_id_, QSharedPointer<SrvUser> user_, QDateTime message_time_, quint32 pool_size_)
{
auto future = RoomStorage_Service::getInstance()->getMessages(room_id_, message_time_, pool_size_);
QFutureWatcher<decltype(future.result())> watcher;
QObject::connect(&watcher, &QFutureWatcher<decltype(future.result())>::finished, [&, user_]() {
QJsonObject messageList;
QJsonArray messages;
auto messages_set = std::move(future.result());
foreach (auto message, messages_set)
{
QJsonObject message_json;
QJsonObject jsonObject;
if (message->getLikes().size() != 0) {
QMapIterator<QString, bool> it(message->getLikes());
while (it.hasNext()) {
it.next();
jsonObject.insert(it.key(), QJsonValue(it.value()));
}
}
message_json[QStringLiteral("id")] = message->getId().toString();
message_json[QStringLiteral("dateTime")] = message->getDateTime().toString();
message_json[QStringLiteral("text")] = message->getText();
message_json[QStringLiteral("parentid")] = message->getParentId();
message_json[QStringLiteral("nickname")] = message->getNickname();
message_json[QStringLiteral("mediaid")] = message->getMedia();
message_json[QStringLiteral("rtl")] = message->isRtl();
message_json[QStringLiteral("likes")] = jsonObject;
messages.append(message_json);
}
sendJson(user_, messageList);
});
//Тут запрос истории
//Тут преобразование в JSON
}
void RoomController::createRoom(QSharedPointer<SrvUser> sender_, const QJsonObject& room_)
{
auto room = QSharedPointer<SrvRoom>::create(
room_[QStringLiteral("name")].toString(),
room_[QStringLiteral("description")].toString(),
room_[QStringLiteral("topic")].toString(),
room_[QStringLiteral("is_private")].toBool(),
room_[QStringLiteral("password")].toString()
);
/*room.setName(room_[QStringLiteral("name")].toString());
room.setDescription(room_[QStringLiteral("description")].toString());
room.setTopicName(room_[QStringLiteral("topic")].toString());
room.setPassword(room_[QStringLiteral("password")].toString());
room.setPrivate(room_[QStringLiteral("is_private")].toBool());*/
auto future = RoomStorage_Service::getInstance()->createRoom(room);
QFutureWatcher<decltype(future.result())> watcher;
connect(&watcher, &QFutureWatcher<decltype(future.result())>::finished, [&, sender_]() {
QJsonObject res;
res[QStringLiteral("createChat")] = "success";
sendJson(sender_, res);
});
}
void RoomController::broadcastSend(const QJsonObject& message_,const quint32& room_id_, const QSharedPointer<SrvUser>& exclude_)
{
for (const auto& user : RoomStorage_Service::getInstance()->getRoom(room_id_)->getConnectedUsers()) {
Q_ASSERT(user);
if (user == exclude_)
continue;
sendJson(user, message_);
}
}
void RoomController::sendJson(const QSharedPointer<SrvUser>& destination, const QJsonObject& message)
{
Q_ASSERT(destination);
destination->sendJson(message);
}