Commit 65978c70 authored by Ronan's avatar Ronan

feat(MainDb): insert participants on chat room insertion

parent 418a2cfe
......@@ -52,7 +52,7 @@ public:
explicit Participant (const IdentityAddress &address);
explicit Participant (IdentityAddress &&address);
const IdentityAddress& getAddress () const;
const IdentityAddress &getAddress () const;
bool isAdmin () const;
private:
......
......@@ -80,40 +80,16 @@ shared_ptr<ChatRoom> CorePrivate::createBasicChatRoom (const ChatRoomId &chatRoo
void CorePrivate::insertChatRoom (const shared_ptr<ChatRoom> &chatRoom) {
L_ASSERT(chatRoom);
L_Q();
const ChatRoomId &chatRoomId = chatRoom->getChatRoomId();
deleteChatRoom(chatRoomId);
q->deleteChatRoom(chatRoom);
chatRooms.push_back(chatRoom);
chatRoomsById[chatRoomId] = chatRoom;
}
void CorePrivate::deleteChatRoom (const ChatRoomId &chatRoomId) {
auto it = chatRoomsById.find(chatRoomId);
if (it != chatRoomsById.end()) {
auto it = find_if(chatRooms.begin(), chatRooms.end(), [&chatRoomId](const shared_ptr<ChatRoom> &chatRoom) {
return chatRoomId == chatRoom->getChatRoomId();
});
if (it != chatRooms.end()) {
chatRooms.erase(it);
return;
}
lError() << "Unable to remove chat room: (peer=" <<
chatRoomId.getPeerAddress().asString() << ", local=" << chatRoomId.getLocalAddress().asString() << ").";
}
chatRoomsById[chatRoom->getChatRoomId()] = chatRoom;
}
void CorePrivate::insertChatRoomWithDb (const shared_ptr<ChatRoom> &chatRoom) {
L_ASSERT(chatRoom->getState() == ChatRoom::State::Created);
mainDb->insertChatRoom(
chatRoom->getChatRoomId(),
chatRoom->getCapabilities(),
chatRoom->getSubject()
);
}
void CorePrivate::deleteChatRoomWithDb (const ChatRoomId &chatRoomId) {
deleteChatRoom(chatRoomId);
mainDb->deleteChatRoom(chatRoomId);
mainDb->insertChatRoom(chatRoom);
}
// -----------------------------------------------------------------------------
......@@ -209,7 +185,21 @@ shared_ptr<ChatRoom> Core::getOrCreateBasicChatRoomFromUri (const string &peerAd
void Core::deleteChatRoom (const shared_ptr<const ChatRoom> &chatRoom) {
CorePrivate *d = chatRoom->getCore()->getPrivate();
d->deleteChatRoomWithDb(chatRoom->getChatRoomId());
const ChatRoomId &chatRoomId = chatRoom->getChatRoomId();
auto it = d->chatRoomsById.find(chatRoomId);
if (it != d->chatRoomsById.end()) {
auto it = find(d->chatRooms.begin(), d->chatRooms.end(), chatRoom);
if (it != d->chatRooms.end()) {
d->chatRooms.erase(it);
return;
}
lError() << "Unable to remove chat room: (peer=" <<
chatRoomId.getPeerAddress().asString() << ", local=" << chatRoomId.getLocalAddress().asString() << ").";
}
d->mainDb->deleteChatRoom(chatRoomId);
}
LINPHONE_END_NAMESPACE
......@@ -39,9 +39,6 @@ public:
LinphoneCore *cCore = nullptr;
private:
void deleteChatRoom (const ChatRoomId &chatRoomId);
void deleteChatRoomWithDb (const ChatRoomId &chatRoomId);
std::list<std::shared_ptr<ChatRoom>> chatRooms;
std::unordered_map<ChatRoomId, std::shared_ptr<ChatRoom>> chatRoomsById;
......
......@@ -47,10 +47,9 @@ private:
long long peerSipAddressId,
long long localSipAddressId,
int capabilities,
const tm &creationTime,
const std::string &subject
const tm &creationTime
);
long long insertChatRoom (const ChatRoomId &chatRoomId, int capabilities, const tm &creationTime, const std::string &subject);
long long insertChatRoom (const std::shared_ptr<ChatRoom> &chatRoom);
void insertChatRoomParticipant (long long chatRoomId, long long sipAddressId, bool isAdmin);
void insertChatMessageParticipant (long long messageEventId, long long sipAddressId, int state);
......
......@@ -186,42 +186,59 @@ MainDb::MainDb (const shared_ptr<Core> &core) : AbstractDb(*new MainDbPrivate),
long long peerSipAddressId,
long long localSipAddressId,
int capabilities,
const tm &creationTime,
const string &subject
const tm &creationTime
) {
L_Q();
soci::session *session = dbSession.getBackendSession<soci::session>();
long long id = selectChatRoomId(peerSipAddressId, localSipAddressId);
if (id < 0) {
lInfo() << "Insert new chat room in database: (peer=" << peerSipAddressId <<
", local=" << localSipAddressId << ", capabilities=" << capabilities << ").";
*session << "INSERT INTO chat_room ("
" peer_sip_address_id, local_sip_address_id, creation_time, last_update_time, capabilities, subject"
") VALUES (:peerSipAddressId, :localSipAddressId, :creationTime, :lastUpdateTime, :capabilities, :subject)",
soci::use(peerSipAddressId), soci::use(localSipAddressId), soci::use(creationTime), soci::use(creationTime),
soci::use(capabilities), soci::use(subject);
return q->getLastInsertId();
}
if (id >= 0)
return id;
return id;
lInfo() << "Insert new chat room in database: (peer=" << peerSipAddressId <<
", local=" << localSipAddressId << ", capabilities=" << capabilities << ").";
*session << "INSERT INTO chat_room ("
" peer_sip_address_id, local_sip_address_id, creation_time, last_update_time, capabilities"
") VALUES (:peerSipAddressId, :localSipAddressId, :creationTime, :lastUpdateTime, :capabilities)",
soci::use(peerSipAddressId), soci::use(localSipAddressId), soci::use(creationTime), soci::use(creationTime),
soci::use(capabilities);
return q->getLastInsertId();
}
long long MainDbPrivate::insertChatRoom (
const ChatRoomId &chatRoomId,
int capabilities,
const tm &creationTime,
const string &subject
) {
return insertChatRoom (
insertSipAddress(chatRoomId.getPeerAddress().asString()),
insertSipAddress(chatRoomId.getLocalAddress().asString()),
capabilities,
creationTime,
subject
);
long long MainDbPrivate::insertChatRoom (const std::shared_ptr<ChatRoom> &chatRoom) {
L_Q();
soci::session *session = dbSession.getBackendSession<soci::session>();
const ChatRoomId &chatRoomId = chatRoom->getChatRoomId();
long long peerSipAddressId = selectSipAddressId(chatRoomId.getPeerAddress().asString());
long long localSipAddressId = selectSipAddressId(chatRoomId.getLocalAddress().asString());
long long id = selectChatRoomId(peerSipAddressId, localSipAddressId);
if (id >= 0) {
lWarning() << "Unable to insert chat room (it already exists): (peer=" << peerSipAddressId <<
", local=" << localSipAddressId << ").";
return id;
}
lInfo() << "Insert new chat room in database: (peer=" << peerSipAddressId <<
", local=" << localSipAddressId << ").";
tm creationTime = Utils::getTimeTAsTm(chatRoom->getCreationTime());
*session << "INSERT INTO chat_room ("
" peer_sip_address_id, local_sip_address_id, creation_time, last_update_time, capabilities, subject"
") VALUES (:peerSipAddressId, :localSipAddressId, :creationTime, :lastUpdateTime, :capabilities, :subject)",
soci::use(peerSipAddressId), soci::use(localSipAddressId), soci::use(creationTime), soci::use(creationTime),
soci::use(static_cast<int>(chatRoom->getCapabilities())), soci::use(chatRoom->getSubject());
id = q->getLastInsertId();
for (const auto &participant : chatRoom->getParticipants())
insertChatRoomParticipant(id, selectSipAddressId(participant->getAddress().asString()), participant->isAdmin());
return id;
}
void MainDbPrivate::insertChatRoomParticipant (long long chatRoomId, long long sipAddressId, bool isAdmin) {
......@@ -651,18 +668,37 @@ MainDb::MainDb (const shared_ptr<Core> &core) : AbstractDb(*new MainDbPrivate),
}
long long MainDbPrivate::insertConferenceParticipantEvent (const shared_ptr<EventLog> &eventLog) {
long long eventId = insertConferenceNotifiedEvent(eventLog);
long long chatRoomId;
long long eventId = insertConferenceNotifiedEvent(eventLog, &chatRoomId);
if (eventId < 0)
return -1;
shared_ptr<ConferenceParticipantEvent> participantEvent =
static_pointer_cast<ConferenceParticipantEvent>(eventLog);
long long participantAddressId = insertSipAddress(
static_pointer_cast<ConferenceParticipantEvent>(eventLog)->getParticipantAddress().asString()
participantEvent->getParticipantAddress().asString()
);
soci::session *session = dbSession.getBackendSession<soci::session>();
*session << "INSERT INTO conference_participant_event (event_id, participant_sip_address_id)"
" VALUES (:eventId, :participantAddressId)", soci::use(eventId), soci::use(participantAddressId);
bool isAdmin = eventLog->getType() == EventLog::Type::ConferenceParticipantSetAdmin;
switch (eventLog->getType()) {
case EventLog::Type::ConferenceParticipantAdded:
case EventLog::Type::ConferenceParticipantSetAdmin:
case EventLog::Type::ConferenceParticipantUnsetAdmin:
insertChatRoomParticipant(chatRoomId, participantAddressId, isAdmin);
break;
case EventLog::Type::ConferenceParticipantRemoved:
// TODO: Deal with remove.
default:
break;
}
return eventId;
}
......@@ -1537,7 +1573,7 @@ MainDb::MainDb (const shared_ptr<Core> &core) : AbstractDb(*new MainDbPrivate),
return chatRooms;
}
void MainDb::insertChatRoom (const ChatRoomId &chatRoomId, int capabilities, const string &subject) {
void MainDb::insertChatRoom (const shared_ptr<ChatRoom> &chatRoom) {
L_D();
if (!isConnected()) {
......@@ -1545,18 +1581,17 @@ MainDb::MainDb (const shared_ptr<Core> &core) : AbstractDb(*new MainDbPrivate),
return;
}
const ChatRoomId &chatRoomId = chatRoom->getChatRoomId();
DurationLogger durationLogger(
"Insert chat room: peer=" + chatRoomId.getPeerAddress().asString() +
", local=" + chatRoomId.getLocalAddress().asString() +
", capabilities=" + Utils::toString(capabilities) +
", subject=" + subject + ")."
"Insert chat room: (peer=" + chatRoomId.getPeerAddress().asString() +
", local=" + chatRoomId.getLocalAddress().asString() + ")."
);
L_BEGIN_LOG_EXCEPTION
soci::transaction tr(*d->dbSession.getBackendSession<soci::session>());
d->insertChatRoom(chatRoomId, capabilities, Utils::getTimeTAsTm(time(0)), subject);
d->insertChatRoom(chatRoom);
tr.commit();
......@@ -1711,8 +1746,7 @@ MainDb::MainDb (const shared_ptr<Core> &core) : AbstractDb(*new MainDbPrivate),
remoteSipAddressId,
localSipAddressId,
static_cast<int>(ChatRoom::Capabilities::Basic),
creationTime,
""
creationTime
);
*session << "INSERT INTO conference_event (event_id, chat_room_id)"
......
......@@ -100,7 +100,7 @@ public:
// ---------------------------------------------------------------------------
std::list<std::shared_ptr<ChatRoom>> getChatRooms () const;
void insertChatRoom (const ChatRoomId &chatRoomId, int capabilities, const std::string &subject);
void insertChatRoom (const std::shared_ptr<ChatRoom> &chatRoom);
void deleteChatRoom (const ChatRoomId &chatRoomId);
// ---------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment