main-db-p.h 7.71 KB
Newer Older
1 2
/*
 * main-db-p.h
3
 * Copyright (C) 2010-2018 Belledonne Communications SARL
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

20 21
#ifndef _L_MAIN_DB_P_H_
#define _L_MAIN_DB_P_H_
22

23 24
#include <unordered_map>

25 26
#include "linphone/utils/utils.h"

27
#include "abstract/abstract-db-p.h"
28
#include "containers/lru-cache.h"
29 30 31 32 33 34 35 36 37 38
#include "event-log/event-log.h"
#include "main-db.h"

// =============================================================================

LINPHONE_BEGIN_NAMESPACE

class Content;

class MainDbPrivate : public AbstractDbPrivate {
39
public:
40
	mutable std::unordered_map<long long, std::weak_ptr<EventLog>> storageIdToEvent;
41
	mutable std::unordered_map<long long, std::weak_ptr<ChatMessage>> storageIdToChatMessage;
42

43
private:
44 45 46 47
	// ---------------------------------------------------------------------------
	// Misc helpers.
	// ---------------------------------------------------------------------------

48
	std::shared_ptr<AbstractChatRoom> findChatRoom (const ConferenceId &conferenceId) const;
49

50 51 52 53
	// ---------------------------------------------------------------------------
	// Low level API.
	// ---------------------------------------------------------------------------

54
	long long insertSipAddress (const std::string &sipAddress);
55
	void insertContent (long long chatMessageId, const Content &content);
56
	long long insertContentType (const std::string &contentType);
57
	long long insertOrUpdateImportedBasicChatRoom (
58 59
		long long peerSipAddressId,
		long long localSipAddressId,
60
		const tm &creationTime
61
	);
62
	long long insertChatRoom (const std::shared_ptr<AbstractChatRoom> &chatRoom, unsigned int notifyId = 0);
63
	long long insertChatRoomParticipant (long long chatRoomId, long long participantSipAddressId, bool isAdmin);
64
	void insertChatRoomParticipantDevice (long long participantId, long long participantDeviceSipAddressId);
65
	void insertChatMessageParticipant (long long chatMessageId, long long sipAddressId, int state, time_t stateChangeTime);
66

67 68
	long long selectSipAddressId (const std::string &sipAddress) const;
	long long selectChatRoomId (long long peerSipAddressId, long long localSipAddressId) const;
69
	long long selectChatRoomId (const ConferenceId &conferenceId) const;
70
	long long selectChatRoomParticipantId (long long chatRoomId, long long participantSipAddressId) const;
71
	long long selectOneToOneChatRoomId (long long sipAddressIdA, long long sipAddressIdB, bool encrypted) const;
72

73
	void deleteContents (long long chatMessageId);
74
	void deleteChatRoomParticipant (long long chatRoomId, long long participantSipAddressId);
75
	void deleteChatRoomParticipantDevice (long long participantId, long long participantDeviceSipAddressId);
76

77 78 79
	// ---------------------------------------------------------------------------
	// Events API.
	// ---------------------------------------------------------------------------
80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	long long getConferenceEventIdFromRow (const soci::row &row) const {
		return dbSession.resolveId(row, -1);
	}

	time_t getConferenceEventCreationTimeFromRow (const soci::row &row) const {
		return Utils::getTmAsTimeT(row.get<tm>(2));
	}

	unsigned int getConferenceEventNotifyIdFromRow (const soci::row &row) const {
		L_Q();
		return q->getBackend() == MainDb::Backend::Mysql
			? row.get<unsigned int>(10, 0)
			: static_cast<unsigned int>(row.get<int>(10, 0));
	}

96
	std::shared_ptr<EventLog> selectGenericConferenceEvent (
97 98
		const std::shared_ptr<AbstractChatRoom> &chatRoom,
		const soci::row &row
99 100
	) const;

101
	std::shared_ptr<EventLog> selectConferenceInfoEvent (
102
		const ConferenceId &conferenceId,
103
		const soci::row &row
104 105
	) const;

106
	std::shared_ptr<EventLog> selectConferenceEvent (
107
		const ConferenceId &conferenceId,
108
		EventLog::Type type,
109
		const soci::row &row
110 111
	) const;

112
	std::shared_ptr<EventLog> selectConferenceCallEvent (
113
		const ConferenceId &conferenceId,
114
		EventLog::Type type,
115
		const soci::row &row
116 117
	) const;

118
	std::shared_ptr<EventLog> selectConferenceChatMessageEvent (
119
		const std::shared_ptr<AbstractChatRoom> &chatRoom,
120
		EventLog::Type type,
121
		const soci::row &row
122 123
	) const;

124
	std::shared_ptr<EventLog> selectConferenceParticipantEvent (
125
		const ConferenceId &conferenceId,
126
		EventLog::Type type,
127
		const soci::row &row
128 129 130
	) const;

	std::shared_ptr<EventLog> selectConferenceParticipantDeviceEvent (
131
		const ConferenceId &conferenceId,
132
		EventLog::Type type,
133
		const soci::row &row
134 135
	) const;

136 137 138 139 140 141
	std::shared_ptr<EventLog> selectConferenceSecurityEvent (
		const ConferenceId &conferenceId,
		EventLog::Type type,
		const soci::row &row
	) const;

142
	std::shared_ptr<EventLog> selectConferenceSubjectEvent (
143
		const ConferenceId &conferenceId,
144
		EventLog::Type type,
145
		const soci::row &row
146
	) const;
Ronan's avatar
Ronan committed
147

148
	long long insertEvent (const std::shared_ptr<EventLog> &eventLog);
149
	long long insertConferenceEvent (const std::shared_ptr<EventLog> &eventLog, long long *chatRoomId = nullptr);
150 151
	long long insertConferenceCallEvent (const std::shared_ptr<EventLog> &eventLog);
	long long insertConferenceChatMessageEvent (const std::shared_ptr<EventLog> &eventLog);
152
	void updateConferenceChatMessageEvent(const std::shared_ptr<EventLog> &eventLog);
153 154
	long long insertConferenceNotifiedEvent (const std::shared_ptr<EventLog> &eventLog, long long *chatRoomId = nullptr);
	long long insertConferenceParticipantEvent (const std::shared_ptr<EventLog> &eventLog, long long *chatRoomId = nullptr);
155 156
	long long insertConferenceParticipantDeviceEvent (const std::shared_ptr<EventLog> &eventLog);
	long long insertConferenceSubjectEvent (const std::shared_ptr<EventLog> &eventLog);
157
	long long insertConferenceSecurityEvent (const std::shared_ptr<EventLog> &eventLog);
158

159 160 161 162 163 164 165
	void setChatMessageParticipantState (
		const std::shared_ptr<EventLog> &eventLog,
		const IdentityAddress &participantAddress,
		ChatMessage::State state,
		time_t stateChangeTime
	);

166 167 168 169
	// ---------------------------------------------------------------------------
	// Cache API.
	// ---------------------------------------------------------------------------

170 171 172 173 174 175
	void cache (const std::shared_ptr<EventLog> &eventLog, long long storageId) const;
	void cache (const std::shared_ptr<ChatMessage> &chatMessage, long long storageId) const;

	std::shared_ptr<EventLog> getEventFromCache (long long storageId) const;
	std::shared_ptr<ChatMessage> getChatMessageFromCache (long long storageId) const;

176
	void invalidConferenceEventsFromQuery (const std::string &query, long long chatRoomId);
177

178 179 180 181 182 183
	// ---------------------------------------------------------------------------
	// Versions.
	// ---------------------------------------------------------------------------

	unsigned int getModuleVersion (const std::string &name);
	void updateModuleVersion (const std::string &name, unsigned int version);
184
	void updateSchema ();
185

186 187 188 189 190 191 192
	// ---------------------------------------------------------------------------
	// Import.
	// ---------------------------------------------------------------------------

	void importLegacyFriends (DbSession &inDbSession);
	void importLegacyHistory (DbSession &inDbSession);

193 194
	// ---------------------------------------------------------------------------

195
	mutable LruCache<ConferenceId, int> unreadChatMessageCountCache;
196

197 198 199 200 201
	L_DECLARE_PUBLIC(MainDb);
};

LINPHONE_END_NAMESPACE

202
#endif // ifndef _L_MAIN_DB_P_H_