2011-09-29 05:20:09 -04:00
|
|
|
/*
|
|
|
|
* libretroshare/src/services: p3HistoryMgr.cc
|
|
|
|
*
|
|
|
|
* RetroShare C++ .
|
|
|
|
*
|
|
|
|
* Copyright 2011 by Thunder.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-10-21 07:00:49 -04:00
|
|
|
#include <time.h>
|
|
|
|
|
2011-09-29 05:20:09 -04:00
|
|
|
#include "p3historymgr.h"
|
|
|
|
#include "serialiser/rshistoryitems.h"
|
|
|
|
#include "serialiser/rsconfigitems.h"
|
|
|
|
#include "retroshare/rsiface.h"
|
|
|
|
#include "retroshare/rspeers.h"
|
|
|
|
#include "serialiser/rsmsgitems.h"
|
2014-01-07 17:51:22 -05:00
|
|
|
#include "rsserver/p3face.h"
|
2011-10-01 09:12:28 -04:00
|
|
|
#include "util/rsstring.h"
|
2011-09-29 05:20:09 -04:00
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
// clean too old messages every 5 minutes
|
|
|
|
//
|
|
|
|
#define MSG_HISTORY_CLEANING_PERIOD 300
|
|
|
|
|
2011-09-29 05:20:09 -04:00
|
|
|
RsHistory *rsHistory = NULL;
|
|
|
|
|
|
|
|
p3HistoryMgr::p3HistoryMgr()
|
|
|
|
: p3Config(CONFIG_TYPE_HISTORY), mHistoryMtx("p3HistoryMgr")
|
|
|
|
{
|
|
|
|
nextMsgId = 1;
|
|
|
|
|
|
|
|
mPublicEnable = false;
|
|
|
|
mPrivateEnable = true;
|
2013-09-27 17:18:44 -04:00
|
|
|
mLobbyEnable = true;
|
2011-09-29 05:20:09 -04:00
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
mPublicSaveCount = 0;
|
|
|
|
mLobbySaveCount = 0;
|
2011-09-29 05:20:09 -04:00
|
|
|
mPrivateSaveCount = 0;
|
2013-09-27 17:18:44 -04:00
|
|
|
mLastCleanTime = 0 ;
|
|
|
|
|
|
|
|
mMaxStorageDurationSeconds = 10*86400 ; // store for 10 days at most.
|
2011-09-29 05:20:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p3HistoryMgr::~p3HistoryMgr()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/***** p3HistoryMgr *****/
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
void p3HistoryMgr::addMessage(bool incoming, const RsPeerId &chatPeerId, const RsPeerId &peerId, const RsChatMsgItem *chatItem)
|
2011-09-29 05:20:09 -04:00
|
|
|
{
|
2012-03-20 17:25:39 -04:00
|
|
|
uint32_t addMsgId = 0;
|
2011-09-29 05:20:09 -04:00
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
time_t now = time(NULL) ;
|
|
|
|
|
|
|
|
if(mLastCleanTime + MSG_HISTORY_CLEANING_PERIOD < now)
|
|
|
|
{
|
|
|
|
cleanOldMessages() ;
|
|
|
|
mLastCleanTime = now ;
|
|
|
|
}
|
|
|
|
|
2012-03-20 17:25:39 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mHistoryMtx); /********** STACK LOCKED MTX ******/
|
2011-09-29 05:20:09 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
if (mPublicEnable == false && chatPeerId.isNull()) {
|
2012-03-20 17:25:39 -04:00
|
|
|
// public chat not enabled
|
|
|
|
return;
|
|
|
|
}
|
2012-01-18 15:47:32 -05:00
|
|
|
|
2012-03-20 17:25:39 -04:00
|
|
|
const RsChatLobbyMsgItem *cli = dynamic_cast<const RsChatLobbyMsgItem*>(chatItem);
|
2011-10-01 09:12:28 -04:00
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
if (cli)
|
|
|
|
{
|
2014-03-17 16:56:06 -04:00
|
|
|
if (mLobbyEnable == false && !chatPeerId.isNull()) // lobby chat not enabled
|
2013-09-27 17:18:44 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-17 16:56:06 -04:00
|
|
|
if (mPrivateEnable == false && !chatPeerId.isNull()) // private chat not enabled
|
2013-04-21 12:43:49 -04:00
|
|
|
return;
|
2012-03-20 17:25:39 -04:00
|
|
|
}
|
2011-09-29 05:20:09 -04:00
|
|
|
|
2012-03-20 17:25:39 -04:00
|
|
|
RsHistoryMsgItem* item = new RsHistoryMsgItem;
|
|
|
|
item->chatPeerId = chatPeerId;
|
|
|
|
item->incoming = incoming;
|
|
|
|
item->peerId = peerId;
|
2014-03-17 16:56:06 -04:00
|
|
|
item->peerName = cli ? cli->nick : rsPeers->getPeerName(RsPeerId(item->peerId));
|
2012-03-20 17:25:39 -04:00
|
|
|
item->sendTime = chatItem->sendTime;
|
|
|
|
item->recvTime = chatItem->recvTime;
|
|
|
|
|
2013-04-21 12:43:49 -04:00
|
|
|
if (cli) {
|
|
|
|
// disable save to disc for chat lobbies until they are saved
|
|
|
|
item->saveToDisc = false;
|
|
|
|
}
|
|
|
|
|
2013-10-03 17:41:34 -04:00
|
|
|
item->message = chatItem->message ;
|
|
|
|
//librs::util::ConvertUtf16ToUtf8(chatItem->message, item->message);
|
2012-03-20 17:25:39 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::map<RsPeerId, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit = mMessages.find(item->chatPeerId);
|
2012-03-20 17:25:39 -04:00
|
|
|
if (mit != mMessages.end()) {
|
|
|
|
item->msgId = nextMsgId++;
|
|
|
|
mit->second.insert(std::make_pair(item->msgId, item));
|
|
|
|
addMsgId = item->msgId;
|
|
|
|
|
|
|
|
// check the limit
|
|
|
|
uint32_t limit;
|
2014-03-17 16:56:06 -04:00
|
|
|
if (chatPeerId.isNull())
|
2012-03-20 17:25:39 -04:00
|
|
|
limit = mPublicSaveCount;
|
2013-09-27 17:18:44 -04:00
|
|
|
else if (cli)
|
|
|
|
limit = mLobbySaveCount;
|
|
|
|
else
|
|
|
|
limit = mPrivateSaveCount;
|
2011-09-29 05:20:09 -04:00
|
|
|
|
2012-03-20 17:25:39 -04:00
|
|
|
if (limit) {
|
|
|
|
while (mit->second.size() > limit) {
|
|
|
|
delete(mit->second.begin()->second);
|
|
|
|
mit->second.erase(mit->second.begin());
|
|
|
|
}
|
|
|
|
}
|
2011-09-29 05:20:09 -04:00
|
|
|
} else {
|
2012-03-20 17:25:39 -04:00
|
|
|
std::map<uint32_t, RsHistoryMsgItem*> msgs;
|
|
|
|
item->msgId = nextMsgId++;
|
|
|
|
msgs.insert(std::make_pair(item->msgId, item));
|
|
|
|
mMessages.insert(std::make_pair(item->chatPeerId, msgs));
|
|
|
|
addMsgId = item->msgId;
|
2011-09-29 05:20:09 -04:00
|
|
|
|
2012-03-20 17:25:39 -04:00
|
|
|
// no need to check the limit
|
2011-09-29 05:20:09 -04:00
|
|
|
}
|
|
|
|
|
2012-03-20 17:25:39 -04:00
|
|
|
IndicateConfigChanged();
|
2011-09-29 05:20:09 -04:00
|
|
|
}
|
|
|
|
|
2012-03-20 17:25:39 -04:00
|
|
|
if (addMsgId) {
|
2014-01-07 17:51:22 -05:00
|
|
|
RsServer::notify()->notifyHistoryChanged(addMsgId, NOTIFY_TYPE_ADD);
|
2012-03-20 17:25:39 -04:00
|
|
|
}
|
2011-09-29 05:20:09 -04:00
|
|
|
}
|
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
void p3HistoryMgr::cleanOldMessages()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mHistoryMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
|
|
|
std::cerr << "****** cleaning old messages." << std::endl;
|
|
|
|
time_t now = time(NULL) ;
|
|
|
|
bool changed = false ;
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
for(std::map<RsPeerId, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit = mMessages.begin(); mit != mMessages.end();)
|
2013-09-27 17:18:44 -04:00
|
|
|
{
|
2013-09-29 09:31:15 -04:00
|
|
|
if (mMaxStorageDurationSeconds > 0)
|
|
|
|
{
|
|
|
|
for(std::map<uint32_t, RsHistoryMsgItem*>::iterator lit = mit->second.begin();lit!=mit->second.end();)
|
|
|
|
if(lit->second->recvTime + mMaxStorageDurationSeconds < now)
|
|
|
|
{
|
|
|
|
std::map<uint32_t, RsHistoryMsgItem*>::iterator lit2 = lit ;
|
|
|
|
++lit2 ;
|
|
|
|
|
|
|
|
std::cerr << " removing msg id " << lit->first << ", for peer id " << mit->first << std::endl;
|
|
|
|
delete lit->second ;
|
|
|
|
|
|
|
|
mit->second.erase(lit) ;
|
|
|
|
lit = lit2 ;
|
|
|
|
|
|
|
|
changed = true ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++lit ;
|
|
|
|
}
|
2013-09-27 17:18:44 -04:00
|
|
|
|
|
|
|
if(mit->second.empty())
|
|
|
|
{
|
2014-03-17 16:56:06 -04:00
|
|
|
std::map<RsPeerId, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit2 = mit ;
|
2013-09-27 17:18:44 -04:00
|
|
|
++mit2 ;
|
|
|
|
std::cerr << " removing peer id " << mit->first << ", since it has no messages" << std::endl;
|
|
|
|
mMessages.erase(mit) ;
|
|
|
|
mit = mit2 ;
|
|
|
|
|
|
|
|
changed = true ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++mit ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(changed)
|
|
|
|
IndicateConfigChanged() ;
|
|
|
|
}
|
|
|
|
|
2011-09-29 05:20:09 -04:00
|
|
|
/***** p3Config *****/
|
|
|
|
|
|
|
|
RsSerialiser* p3HistoryMgr::setupSerialiser()
|
|
|
|
{
|
|
|
|
RsSerialiser *rss = new RsSerialiser;
|
|
|
|
rss->addSerialType(new RsHistorySerialiser);
|
|
|
|
rss->addSerialType(new RsGeneralConfigSerialiser());
|
|
|
|
|
|
|
|
return rss;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3HistoryMgr::saveList(bool& cleanup, std::list<RsItem*>& saveData)
|
|
|
|
{
|
|
|
|
cleanup = false;
|
|
|
|
|
|
|
|
mHistoryMtx.lock(); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::map<RsPeerId, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit;
|
2011-09-29 05:20:09 -04:00
|
|
|
std::map<uint32_t, RsHistoryMsgItem*>::iterator lit;
|
|
|
|
for (mit = mMessages.begin(); mit != mMessages.end(); mit++) {
|
|
|
|
for (lit = mit->second.begin(); lit != mit->second.end(); lit++) {
|
2013-04-21 12:43:49 -04:00
|
|
|
if (lit->second->saveToDisc) {
|
|
|
|
saveData.push_back(lit->second);
|
|
|
|
}
|
2011-09-29 05:20:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RsConfigKeyValueSet *vitem = new RsConfigKeyValueSet;
|
|
|
|
|
|
|
|
RsTlvKeyValue kv;
|
|
|
|
kv.key = "PUBLIC_ENABLE";
|
|
|
|
kv.value = mPublicEnable ? "TRUE" : "FALSE";
|
|
|
|
vitem->tlvkvs.pairs.push_back(kv);
|
|
|
|
|
|
|
|
kv.key = "PRIVATE_ENABLE";
|
|
|
|
kv.value = mPrivateEnable ? "TRUE" : "FALSE";
|
|
|
|
vitem->tlvkvs.pairs.push_back(kv);
|
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
kv.key = "LOBBY_ENABLE";
|
|
|
|
kv.value = mLobbyEnable ? "TRUE" : "FALSE";
|
|
|
|
vitem->tlvkvs.pairs.push_back(kv);
|
|
|
|
|
|
|
|
kv.key = "MAX_STORAGE_TIME";
|
|
|
|
rs_sprintf(kv.value,"%d",mMaxStorageDurationSeconds) ;
|
|
|
|
vitem->tlvkvs.pairs.push_back(kv);
|
|
|
|
|
|
|
|
kv.key = "LOBBY_SAVECOUNT";
|
|
|
|
rs_sprintf(kv.value, "%lu", mLobbySaveCount);
|
|
|
|
vitem->tlvkvs.pairs.push_back(kv);
|
|
|
|
|
2011-09-29 05:20:09 -04:00
|
|
|
kv.key = "PUBLIC_SAVECOUNT";
|
2012-04-14 18:38:24 -04:00
|
|
|
rs_sprintf(kv.value, "%lu", mPublicSaveCount);
|
2011-09-29 05:20:09 -04:00
|
|
|
vitem->tlvkvs.pairs.push_back(kv);
|
|
|
|
|
|
|
|
kv.key = "PRIVATE_SAVECOUNT";
|
2012-04-14 18:38:24 -04:00
|
|
|
rs_sprintf(kv.value, "%lu", mPrivateSaveCount);
|
2011-09-29 05:20:09 -04:00
|
|
|
vitem->tlvkvs.pairs.push_back(kv);
|
|
|
|
|
|
|
|
saveData.push_back(vitem);
|
|
|
|
saveCleanupList.push_back(vitem);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void p3HistoryMgr::saveDone()
|
|
|
|
{
|
|
|
|
/* clean up the save List */
|
|
|
|
std::list<RsItem*>::iterator it;
|
|
|
|
for (it = saveCleanupList.begin(); it != saveCleanupList.end(); ++it) {
|
|
|
|
delete (*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
saveCleanupList.clear();
|
|
|
|
|
|
|
|
/* unlock mutex */
|
|
|
|
mHistoryMtx.unlock(); /****** MUTEX UNLOCKED *******/
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3HistoryMgr::loadList(std::list<RsItem*>& load)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mHistoryMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
|
|
|
RsHistoryMsgItem *msgItem;
|
|
|
|
std::list<RsItem*>::iterator it;
|
|
|
|
|
|
|
|
for (it = load.begin(); it != load.end(); it++) {
|
|
|
|
if (NULL != (msgItem = dynamic_cast<RsHistoryMsgItem*>(*it))) {
|
2013-09-27 17:18:44 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::map<RsPeerId, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit = mMessages.find(msgItem->chatPeerId);
|
2011-09-29 05:20:09 -04:00
|
|
|
msgItem->msgId = nextMsgId++;
|
2013-09-27 17:18:44 -04:00
|
|
|
|
|
|
|
std::cerr << "Loading msg history item: peer id=" << msgItem->chatPeerId << "), msg id =" << msgItem->msgId << std::endl;
|
|
|
|
|
2011-09-29 05:20:09 -04:00
|
|
|
if (mit != mMessages.end()) {
|
|
|
|
mit->second.insert(std::make_pair(msgItem->msgId, msgItem));
|
|
|
|
} else {
|
|
|
|
std::map<uint32_t, RsHistoryMsgItem*> msgs;
|
|
|
|
msgs.insert(std::make_pair(msgItem->msgId, msgItem));
|
|
|
|
mMessages.insert(std::make_pair(msgItem->chatPeerId, msgs));
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't delete the item !!
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsConfigKeyValueSet *rskv ;
|
|
|
|
if (NULL != (rskv = dynamic_cast<RsConfigKeyValueSet*>(*it))) {
|
|
|
|
for (std::list<RsTlvKeyValue>::const_iterator kit = rskv->tlvkvs.pairs.begin(); kit != rskv->tlvkvs.pairs.end(); kit++) {
|
|
|
|
if (kit->key == "PUBLIC_ENABLE") {
|
2011-09-29 05:52:01 -04:00
|
|
|
mPublicEnable = (kit->value == "TRUE") ? true : false;
|
2011-09-29 05:20:09 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kit->key == "PRIVATE_ENABLE") {
|
2011-09-29 05:52:01 -04:00
|
|
|
mPrivateEnable = (kit->value == "TRUE") ? true : false;
|
2011-09-29 05:20:09 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
if (kit->key == "LOBBY_ENABLE") {
|
|
|
|
mLobbyEnable = (kit->value == "TRUE") ? true : false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kit->key == "MAX_STORAGE_TIME") {
|
|
|
|
uint32_t val ;
|
|
|
|
if (sscanf(kit->value.c_str(), "%d", &val) == 1)
|
|
|
|
mMaxStorageDurationSeconds = val ;
|
|
|
|
|
|
|
|
std::cerr << "Loaded max storage time for history = " << val << " seconds" << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-09-29 05:20:09 -04:00
|
|
|
if (kit->key == "PUBLIC_SAVECOUNT") {
|
|
|
|
mPublicSaveCount = atoi(kit->value.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (kit->key == "PRIVATE_SAVECOUNT") {
|
|
|
|
mPrivateSaveCount = atoi(kit->value.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
2013-09-27 17:18:44 -04:00
|
|
|
if (kit->key == "LOBBY_SAVECOUNT") {
|
|
|
|
mLobbySaveCount = atoi(kit->value.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
2011-09-29 05:20:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
delete (*it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete unknown items
|
|
|
|
delete (*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***** p3History *****/
|
|
|
|
|
|
|
|
static void convertMsg(const RsHistoryMsgItem* item, HistoryMsg &msg)
|
|
|
|
{
|
|
|
|
msg.msgId = item->msgId;
|
|
|
|
msg.chatPeerId = item->chatPeerId;
|
|
|
|
msg.incoming = item->incoming;
|
|
|
|
msg.peerId = item->peerId;
|
|
|
|
msg.peerName = item->peerName;
|
|
|
|
msg.sendTime = item->sendTime;
|
|
|
|
msg.recvTime = item->recvTime;
|
|
|
|
msg.message = item->message;
|
|
|
|
}
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
bool p3HistoryMgr::getMessages(const RsPeerId &chatPeerId, std::list<HistoryMsg> &msgs, uint32_t loadCount)
|
2011-09-29 05:20:09 -04:00
|
|
|
{
|
|
|
|
msgs.clear();
|
|
|
|
|
|
|
|
RsStackMutex stack(mHistoryMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
std::cerr << "Getting history for peer " << chatPeerId << std::endl;
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
if (mPublicEnable == false && chatPeerId.isNull()) { // chatPeerId.empty() means it's public chat
|
2011-09-29 05:20:09 -04:00
|
|
|
// public chat not enabled
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
if (mPrivateEnable == false && chatPeerId.isNull() == false) // private chat not enabled
|
2013-09-27 17:18:44 -04:00
|
|
|
return false;
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
if (mLobbyEnable == false && chatPeerId.isNull() == false) // private chat not enabled
|
2011-09-29 05:20:09 -04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
uint32_t foundCount = 0;
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::map<RsPeerId, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit = mMessages.find(chatPeerId);
|
2013-09-27 17:18:44 -04:00
|
|
|
|
|
|
|
if (mit != mMessages.end())
|
|
|
|
{
|
2011-09-29 05:20:09 -04:00
|
|
|
std::map<uint32_t, RsHistoryMsgItem*>::reverse_iterator lit;
|
2013-09-27 17:18:44 -04:00
|
|
|
|
|
|
|
for (lit = mit->second.rbegin(); lit != mit->second.rend(); lit++)
|
|
|
|
{
|
2011-09-29 05:20:09 -04:00
|
|
|
HistoryMsg msg;
|
|
|
|
convertMsg(lit->second, msg);
|
|
|
|
msgs.insert(msgs.begin(), msg);
|
|
|
|
foundCount++;
|
|
|
|
if (loadCount && foundCount >= loadCount) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-27 17:18:44 -04:00
|
|
|
std::cerr << msgs.size() << " messages added." << std::endl;
|
2011-09-29 05:20:09 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3HistoryMgr::getMessage(uint32_t msgId, HistoryMsg &msg)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mHistoryMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::map<RsPeerId, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit;
|
2011-09-29 05:20:09 -04:00
|
|
|
for (mit = mMessages.begin(); mit != mMessages.end(); mit++) {
|
|
|
|
std::map<uint32_t, RsHistoryMsgItem*>::iterator lit = mit->second.find(msgId);
|
|
|
|
if (lit != mit->second.end()) {
|
|
|
|
convertMsg(lit->second, msg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
void p3HistoryMgr::clear(const RsPeerId &chatPeerId)
|
2011-09-29 05:20:09 -04:00
|
|
|
{
|
2012-03-20 17:25:39 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mHistoryMtx); /********** STACK LOCKED MTX ******/
|
2011-09-29 05:20:09 -04:00
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
std::cerr << "********** p3History::clear()called for peer id " << chatPeerId << std::endl;
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::map<RsPeerId, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit = mMessages.find(chatPeerId);
|
2012-03-20 17:25:39 -04:00
|
|
|
if (mit == mMessages.end()) {
|
|
|
|
return;
|
|
|
|
}
|
2011-09-29 05:20:09 -04:00
|
|
|
|
2012-03-20 17:25:39 -04:00
|
|
|
std::map<uint32_t, RsHistoryMsgItem*>::iterator lit;
|
|
|
|
for (lit = mit->second.begin(); lit != mit->second.end(); lit++) {
|
|
|
|
delete(lit->second);
|
|
|
|
}
|
|
|
|
mit->second.clear();
|
|
|
|
mMessages.erase(mit);
|
2011-09-29 05:20:09 -04:00
|
|
|
|
2012-03-20 17:25:39 -04:00
|
|
|
IndicateConfigChanged();
|
|
|
|
}
|
2011-09-29 05:20:09 -04:00
|
|
|
|
2014-01-07 17:51:22 -05:00
|
|
|
RsServer::notify()->notifyHistoryChanged(0, NOTIFY_TYPE_MOD);
|
2011-09-29 05:20:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void p3HistoryMgr::removeMessages(const std::list<uint32_t> &msgIds)
|
|
|
|
{
|
|
|
|
std::list<uint32_t> ids = msgIds;
|
|
|
|
std::list<uint32_t> removedIds;
|
|
|
|
std::list<uint32_t>::iterator iit;
|
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
std::cerr << "********** p3History::removeMessages called()" << std::endl;
|
2011-09-29 05:20:09 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mHistoryMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::map<RsPeerId, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit;
|
2011-09-29 05:20:09 -04:00
|
|
|
for (mit = mMessages.begin(); mit != mMessages.end(); ++mit) {
|
|
|
|
iit = ids.begin();
|
|
|
|
while (iit != ids.end()) {
|
|
|
|
std::map<uint32_t, RsHistoryMsgItem*>::iterator lit = mit->second.find(*iit);
|
|
|
|
if (lit != mit->second.end()) {
|
|
|
|
delete(lit->second);
|
|
|
|
mit->second.erase(lit);
|
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
std::cerr << "**** Removing " << mit->first << " msg id = " << lit->first << std::endl;
|
2011-09-29 05:20:09 -04:00
|
|
|
removedIds.push_back(*iit);
|
|
|
|
iit = ids.erase(iit);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++iit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ids.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (removedIds.empty() == false) {
|
|
|
|
IndicateConfigChanged();
|
|
|
|
|
|
|
|
for (iit = removedIds.begin(); iit != removedIds.end(); ++iit) {
|
2014-01-07 17:51:22 -05:00
|
|
|
RsServer::notify()->notifyHistoryChanged(*iit, NOTIFY_TYPE_DEL);
|
2011-09-29 05:20:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
bool p3HistoryMgr::getEnable(uint32_t chat_type)
|
2011-09-29 05:20:09 -04:00
|
|
|
{
|
2013-09-27 17:18:44 -04:00
|
|
|
switch(chat_type)
|
|
|
|
{
|
|
|
|
case RS_HISTORY_TYPE_PUBLIC : return mPublicEnable ;
|
|
|
|
case RS_HISTORY_TYPE_LOBBY : return mLobbyEnable ;
|
|
|
|
case RS_HISTORY_TYPE_PRIVATE: return mPrivateEnable ;
|
|
|
|
default:
|
|
|
|
std::cerr << "Unexpected value " << chat_type<< " in p3HistoryMgr::getEnable(): this is a bug." << std::endl;
|
|
|
|
return 0 ;
|
|
|
|
}
|
2011-09-29 05:20:09 -04:00
|
|
|
}
|
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
uint32_t p3HistoryMgr::getMaxStorageDuration()
|
|
|
|
{
|
|
|
|
return mMaxStorageDurationSeconds ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void p3HistoryMgr::setMaxStorageDuration(uint32_t seconds)
|
|
|
|
{
|
|
|
|
if(mMaxStorageDurationSeconds != seconds)
|
|
|
|
IndicateConfigChanged() ;
|
|
|
|
|
|
|
|
mMaxStorageDurationSeconds = seconds ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void p3HistoryMgr::setEnable(uint32_t chat_type, bool enable)
|
2011-09-29 05:20:09 -04:00
|
|
|
{
|
|
|
|
bool oldValue;
|
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
switch(chat_type)
|
|
|
|
{
|
|
|
|
case RS_HISTORY_TYPE_PUBLIC : oldValue = mPublicEnable ;
|
|
|
|
mPublicEnable = enable ;
|
|
|
|
break ;
|
|
|
|
|
|
|
|
case RS_HISTORY_TYPE_LOBBY : oldValue = mLobbyEnable ;
|
|
|
|
mLobbyEnable = enable;
|
|
|
|
break ;
|
|
|
|
|
|
|
|
case RS_HISTORY_TYPE_PRIVATE: oldValue = mPrivateEnable ;
|
|
|
|
mPrivateEnable = enable ;
|
|
|
|
break ;
|
2011-09-29 05:20:09 -04:00
|
|
|
}
|
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
if (oldValue != enable)
|
2011-09-29 05:20:09 -04:00
|
|
|
IndicateConfigChanged();
|
|
|
|
}
|
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
uint32_t p3HistoryMgr::getSaveCount(uint32_t chat_type)
|
2011-09-29 05:20:09 -04:00
|
|
|
{
|
2013-09-27 17:18:44 -04:00
|
|
|
switch(chat_type)
|
|
|
|
{
|
|
|
|
case RS_HISTORY_TYPE_PUBLIC : return mPublicSaveCount ;
|
|
|
|
case RS_HISTORY_TYPE_LOBBY : return mLobbySaveCount ;
|
|
|
|
case RS_HISTORY_TYPE_PRIVATE: return mPrivateSaveCount ;
|
|
|
|
default:
|
|
|
|
std::cerr << "Unexpected value " << chat_type<< " in p3HistoryMgr::getSaveCount(): this is a bug." << std::endl;
|
|
|
|
return 0 ;
|
|
|
|
}
|
2011-09-29 05:20:09 -04:00
|
|
|
}
|
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
void p3HistoryMgr::setSaveCount(uint32_t chat_type, uint32_t count)
|
2011-09-29 05:20:09 -04:00
|
|
|
{
|
|
|
|
uint32_t oldValue;
|
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
switch(chat_type)
|
|
|
|
{
|
|
|
|
case RS_HISTORY_TYPE_PUBLIC : oldValue = mPublicSaveCount ;
|
|
|
|
mPublicSaveCount = count ;
|
|
|
|
break ;
|
|
|
|
|
|
|
|
case RS_HISTORY_TYPE_LOBBY : oldValue = mLobbySaveCount ;
|
|
|
|
mLobbySaveCount = count;
|
|
|
|
break ;
|
|
|
|
|
|
|
|
case RS_HISTORY_TYPE_PRIVATE: oldValue = mPrivateSaveCount ;
|
|
|
|
mPrivateSaveCount = count ;
|
|
|
|
break ;
|
2011-09-29 05:20:09 -04:00
|
|
|
}
|
|
|
|
|
2013-09-27 17:18:44 -04:00
|
|
|
if (oldValue != count)
|
2011-09-29 05:20:09 -04:00
|
|
|
IndicateConfigChanged();
|
|
|
|
}
|