- added history parameters for chat lobbies

- added max storage time for items in chat history. Avoids filling up the history with chat from volatile peers.
- worked out the layout in config->ChatPage in a more compact design and added help
- set the max size of chat messages to 6000 bytes instead of 2000 which was a bit too low
- default max number of lobby messages saved in each lobby is 20. Avoids flooding for peers who havn't configured it yet.



git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@6770 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2013-09-27 21:18:44 +00:00
parent f25f46dffb
commit e21d869006
11 changed files with 544 additions and 218 deletions

View file

@ -31,6 +31,10 @@
#include "serialiser/rsmsgitems.h"
#include "util/rsstring.h"
// clean too old messages every 5 minutes
//
#define MSG_HISTORY_CLEANING_PERIOD 300
RsHistory *rsHistory = NULL;
p3HistoryMgr::p3HistoryMgr()
@ -40,9 +44,14 @@ p3HistoryMgr::p3HistoryMgr()
mPublicEnable = false;
mPrivateEnable = true;
mLobbyEnable = true;
mPublicSaveCount = 0;
mPublicSaveCount = 0;
mLobbySaveCount = 0;
mPrivateSaveCount = 0;
mLastCleanTime = 0 ;
mMaxStorageDurationSeconds = 10*86400 ; // store for 10 days at most.
}
p3HistoryMgr::~p3HistoryMgr()
@ -55,6 +64,14 @@ void p3HistoryMgr::addMessage(bool incoming, const std::string &chatPeerId, cons
{
uint32_t addMsgId = 0;
time_t now = time(NULL) ;
if(mLastCleanTime + MSG_HISTORY_CLEANING_PERIOD < now)
{
cleanOldMessages() ;
mLastCleanTime = now ;
}
{
RsStackMutex stack(mHistoryMtx); /********** STACK LOCKED MTX ******/
@ -65,13 +82,15 @@ void p3HistoryMgr::addMessage(bool incoming, const std::string &chatPeerId, cons
const RsChatLobbyMsgItem *cli = dynamic_cast<const RsChatLobbyMsgItem*>(chatItem);
if (cli) {
// there is currently no setting for chat lobbies
} else {
if (mPrivateEnable == false && chatPeerId.empty() == false) {
// private chat not enabled
if (cli)
{
if (mLobbyEnable == false && !chatPeerId.empty()) // lobby chat not enabled
return;
}
else
{
if (mPrivateEnable == false && !chatPeerId.empty()) // private chat not enabled
return;
}
}
RsHistoryMsgItem* item = new RsHistoryMsgItem;
@ -97,16 +116,12 @@ void p3HistoryMgr::addMessage(bool incoming, const std::string &chatPeerId, cons
// check the limit
uint32_t limit;
if (chatPeerId.empty()) {
if (chatPeerId.empty())
limit = mPublicSaveCount;
} else {
if (cli) {
// there is currently no setting for chat lobbies
limit = 0;
} else {
limit = mPrivateSaveCount;
}
}
else if (cli)
limit = mLobbySaveCount;
else
limit = mPrivateSaveCount;
if (limit) {
while (mit->second.size() > limit) {
@ -132,6 +147,51 @@ void p3HistoryMgr::addMessage(bool incoming, const std::string &chatPeerId, cons
}
}
void p3HistoryMgr::cleanOldMessages()
{
RsStackMutex stack(mHistoryMtx); /********** STACK LOCKED MTX ******/
std::cerr << "****** cleaning old messages." << std::endl;
time_t now = time(NULL) ;
bool changed = false ;
for(std::map<std::string, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit = mMessages.begin(); mit != mMessages.end();)
{
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 ;
if(mit->second.empty())
{
std::map<std::string, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit2 = mit ;
++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() ;
}
/***** p3Config *****/
RsSerialiser* p3HistoryMgr::setupSerialiser()
@ -170,6 +230,18 @@ bool p3HistoryMgr::saveList(bool& cleanup, std::list<RsItem*>& saveData)
kv.value = mPrivateEnable ? "TRUE" : "FALSE";
vitem->tlvkvs.pairs.push_back(kv);
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);
kv.key = "PUBLIC_SAVECOUNT";
rs_sprintf(kv.value, "%lu", mPublicSaveCount);
vitem->tlvkvs.pairs.push_back(kv);
@ -207,8 +279,12 @@ bool p3HistoryMgr::loadList(std::list<RsItem*>& load)
for (it = load.begin(); it != load.end(); it++) {
if (NULL != (msgItem = dynamic_cast<RsHistoryMsgItem*>(*it))) {
std::map<std::string, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit = mMessages.find(msgItem->chatPeerId);
msgItem->msgId = nextMsgId++;
std::cerr << "Loading msg history item: peer id=" << msgItem->chatPeerId << "), msg id =" << msgItem->msgId << std::endl;
if (mit != mMessages.end()) {
mit->second.insert(std::make_pair(msgItem->msgId, msgItem));
} else {
@ -235,6 +311,20 @@ bool p3HistoryMgr::loadList(std::list<RsItem*>& load)
continue;
}
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;
}
if (kit->key == "PUBLIC_SAVECOUNT") {
mPublicSaveCount = atoi(kit->value.c_str());
continue;
@ -243,6 +333,10 @@ bool p3HistoryMgr::loadList(std::list<RsItem*>& load)
mPrivateSaveCount = atoi(kit->value.c_str());
continue;
}
if (kit->key == "LOBBY_SAVECOUNT") {
mLobbySaveCount = atoi(kit->value.c_str());
continue;
}
}
delete (*it);
@ -270,40 +364,35 @@ static void convertMsg(const RsHistoryMsgItem* item, HistoryMsg &msg)
msg.message = item->message;
}
//static void convertMsg(const HistoryMsg &msg, RsHistoryMsgItem* item)
//{
// item->msgId = msg.msgId;
// item->chatPeerId = msg.chatPeerId;
// item->incoming = msg.incoming;
// item->peerId = msg.peerId;
// item->peerName = msg.peerName;
// item->sendTime = msg.sendTime;
// item->recvTime = msg.recvTime;
// item->message = msg.message;
//}
bool p3HistoryMgr::getMessages(const std::string &chatPeerId, std::list<HistoryMsg> &msgs, uint32_t loadCount)
{
msgs.clear();
RsStackMutex stack(mHistoryMtx); /********** STACK LOCKED MTX ******/
if (mPublicEnable == false && chatPeerId.empty()) {
std::cerr << "Getting history for peer " << chatPeerId << std::endl;
if (mPublicEnable == false && chatPeerId.empty()) { // chatPeerId.empty() means it's public chat
// public chat not enabled
return false;
}
if (mPrivateEnable == false && chatPeerId.empty() == false) {
// private chat not enabled
if (mPrivateEnable == false && chatPeerId.empty() == false) // private chat not enabled
return false;
if (mLobbyEnable == false && chatPeerId.empty() == false) // private chat not enabled
return false;
}
uint32_t foundCount = 0;
std::map<std::string, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit = mMessages.find(chatPeerId);
if (mit != mMessages.end()) {
if (mit != mMessages.end())
{
std::map<uint32_t, RsHistoryMsgItem*>::reverse_iterator lit;
for (lit = mit->second.rbegin(); lit != mit->second.rend(); lit++) {
for (lit = mit->second.rbegin(); lit != mit->second.rend(); lit++)
{
HistoryMsg msg;
convertMsg(lit->second, msg);
msgs.insert(msgs.begin(), msg);
@ -313,6 +402,7 @@ bool p3HistoryMgr::getMessages(const std::string &chatPeerId, std::list<HistoryM
}
}
}
std::cerr << msgs.size() << " messages added." << std::endl;
return true;
}
@ -338,6 +428,8 @@ void p3HistoryMgr::clear(const std::string &chatPeerId)
{
RsStackMutex stack(mHistoryMtx); /********** STACK LOCKED MTX ******/
std::cerr << "********** p3History::clear()called for peer id " << chatPeerId << std::endl;
std::map<std::string, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit = mMessages.find(chatPeerId);
if (mit == mMessages.end()) {
return;
@ -362,6 +454,7 @@ void p3HistoryMgr::removeMessages(const std::list<uint32_t> &msgIds)
std::list<uint32_t> removedIds;
std::list<uint32_t>::iterator iit;
std::cerr << "********** p3History::removeMessages called()" << std::endl;
{
RsStackMutex stack(mHistoryMtx); /********** STACK LOCKED MTX ******/
@ -374,6 +467,7 @@ void p3HistoryMgr::removeMessages(const std::list<uint32_t> &msgIds)
delete(lit->second);
mit->second.erase(lit);
std::cerr << "**** Removing " << mit->first << " msg id = " << lit->first << std::endl;
removedIds.push_back(*iit);
iit = ids.erase(iit);
@ -398,46 +492,88 @@ void p3HistoryMgr::removeMessages(const std::list<uint32_t> &msgIds)
}
}
bool p3HistoryMgr::getEnable(bool ofPublic)
bool p3HistoryMgr::getEnable(uint32_t chat_type)
{
return ofPublic ? mPublicEnable : mPrivateEnable;
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 ;
}
}
void p3HistoryMgr::setEnable(bool forPublic, bool enable)
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)
{
bool oldValue;
if (forPublic) {
oldValue = mPublicEnable;
mPublicEnable = enable;
} else {
oldValue = mPrivateEnable;
mPrivateEnable = enable;
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 ;
}
if (oldValue != enable) {
if (oldValue != enable)
IndicateConfigChanged();
}
uint32_t p3HistoryMgr::getSaveCount(uint32_t chat_type)
{
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 ;
}
}
uint32_t p3HistoryMgr::getSaveCount(bool ofPublic)
{
return ofPublic ? mPublicSaveCount : mPrivateSaveCount;
}
void p3HistoryMgr::setSaveCount(bool forPublic, uint32_t count)
void p3HistoryMgr::setSaveCount(uint32_t chat_type, uint32_t count)
{
uint32_t oldValue;
if (forPublic) {
oldValue = mPublicSaveCount;
mPublicSaveCount = count;
} else {
oldValue = mPrivateSaveCount;
mPrivateSaveCount = count;
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 ;
}
if (oldValue != count) {
if (oldValue != count)
IndicateConfigChanged();
}
}

View file

@ -56,10 +56,13 @@ public:
bool getMessage(uint32_t msgId, HistoryMsg &msg);
void clear(const std::string &chatPeerId);
void removeMessages(const std::list<uint32_t> &msgIds);
bool getEnable(bool ofPublic);
void setEnable(bool forPublic, bool enable);
uint32_t getSaveCount(bool ofPublic);
void setSaveCount(bool forPublic, uint32_t count);
virtual bool getEnable(uint32_t chat_type);
virtual void setEnable(uint32_t chat_type, bool enable);
virtual uint32_t getSaveCount(uint32_t chat_type);
virtual void setSaveCount(uint32_t chat_type, uint32_t count);
virtual void setMaxStorageDuration(uint32_t seconds) ;
virtual uint32_t getMaxStorageDuration() ;
/********* p3config ************/
@ -72,12 +75,22 @@ private:
uint32_t nextMsgId;
std::map<std::string, std::map<uint32_t, RsHistoryMsgItem*> > mMessages;
// Removes messages stored for more than mMaxMsgStorageDurationSeconds seconds.
// This avoids the stored list to grow crazy with time.
//
void cleanOldMessages() ;
bool mPublicEnable;
bool mLobbyEnable;
bool mPrivateEnable;
uint32_t mPublicSaveCount;
uint32_t mLobbySaveCount;
uint32_t mPrivateSaveCount;
uint32_t mMaxStorageDurationSeconds ;
time_t mLastCleanTime ;
std::list<RsItem*> saveCleanupList; /* TEMPORARY LIST WHEN SAVING */
RsMutex mHistoryMtx;

View file

@ -38,6 +38,10 @@ extern RsHistory *rsHistory;
/*!
* data object used for message history
*/
static const uint32_t RS_HISTORY_TYPE_PUBLIC = 0 ;
static const uint32_t RS_HISTORY_TYPE_PRIVATE = 1 ;
static const uint32_t RS_HISTORY_TYPE_LOBBY = 2 ;
class HistoryMsg
{
public:
@ -72,12 +76,14 @@ public:
virtual void removeMessages(const std::list<uint32_t> &msgIds) = 0;
virtual void clear(const std::string &chatPeerId) = 0;
virtual bool getEnable(bool ofPublic) = 0;
virtual void setEnable(bool forPublic, bool enable) = 0;
virtual bool getEnable(uint32_t chat_type) = 0;
virtual void setEnable(uint32_t chat_type, bool enable) = 0;
virtual uint32_t getMaxStorageDuration() = 0 ;
virtual void setMaxStorageDuration(uint32_t seconds) = 0 ;
// 0 = no limit, >0 count of saved messages
virtual uint32_t getSaveCount(bool ofPublic) = 0;
virtual void setSaveCount(bool forPublic, uint32_t count) = 0;
virtual uint32_t getSaveCount(uint32_t chat_type) = 0;
virtual void setSaveCount(uint32_t chat_type, uint32_t count) = 0;
};
#endif

View file

@ -36,6 +36,14 @@ p3History::~p3History()
{
}
void p3History::setMaxStorageDuration(uint32_t seconds)
{
mHistoryMgr->setMaxStorageDuration(seconds) ;
}
uint32_t p3History::getMaxStorageDuration()
{
return mHistoryMgr->getMaxStorageDuration() ;
}
bool p3History::getMessages(const std::string &chatPeerId, std::list<HistoryMsg> &msgs, const uint32_t loadCount)
{
return mHistoryMgr->getMessages(chatPeerId, msgs, loadCount);
@ -56,22 +64,22 @@ void p3History::clear(const std::string &chatPeerId)
mHistoryMgr->clear(chatPeerId);
}
bool p3History::getEnable(bool ofPublic)
bool p3History::getEnable(uint32_t chat_type)
{
return mHistoryMgr->getEnable(ofPublic);
return mHistoryMgr->getEnable(chat_type);
}
void p3History::setEnable(bool forPublic, bool enable)
void p3History::setEnable(uint32_t chat_type, bool enable)
{
mHistoryMgr->setEnable(forPublic, enable);
mHistoryMgr->setEnable(chat_type, enable);
}
uint32_t p3History::getSaveCount(bool ofPublic)
uint32_t p3History::getSaveCount(uint32_t chat_type)
{
return mHistoryMgr->getSaveCount(ofPublic);
return mHistoryMgr->getSaveCount(chat_type);
}
void p3History::setSaveCount(bool forPublic, uint32_t count)
void p3History::setSaveCount(uint32_t chat_type, uint32_t count)
{
mHistoryMgr->setSaveCount(forPublic, count);
mHistoryMgr->setSaveCount(chat_type, count);
}

View file

@ -45,10 +45,12 @@ public:
virtual bool getMessage(uint32_t msgId, HistoryMsg &msg);
virtual void removeMessages(const std::list<uint32_t> &msgIds);
virtual void clear(const std::string &chatPeerId);
virtual bool getEnable(bool ofPublic);
virtual void setEnable(bool forPublic, bool enable);
virtual uint32_t getSaveCount(bool ofPublic);
virtual void setSaveCount(bool forPublic, uint32_t count);
virtual bool getEnable(uint32_t chat_type);
virtual void setEnable(uint32_t chat_type, bool enable);
virtual uint32_t getSaveCount(uint32_t chat_type);
virtual void setSaveCount(uint32_t chat_type, uint32_t count);
virtual void setMaxStorageDuration(uint32_t seconds) ;
virtual uint32_t getMaxStorageDuration() ;
private:
p3HistoryMgr* mHistoryMgr;

View file

@ -1151,12 +1151,12 @@ void p3ChatService::handleRecvChatAvatarItem(RsChatAvatarItem *ca)
bool p3ChatService::checkForMessageSecurity(RsChatMsgItem *ci)
{
// Remove too big messages
if (ci->message.length() > 2000 && (ci->chatFlags & RS_CHAT_FLAG_LOBBY))
if (ci->message.length() > 6000 && (ci->chatFlags & RS_CHAT_FLAG_LOBBY))
{
wchar_t tmp[300];
mbstowcs(tmp, rsPeers->getPeerName(ci->PeerId()).c_str(), 299);
ci->message = std::wstring(L"**** Security warning: Message bigger than 2000 characters, coming from id ") + tmp + L", dropped. ****";
ci->message = std::wstring(L"**** Security warning: Message bigger than 6000 characters, forwarded to you by ") + tmp + L", dropped. ****";
return false;
}
@ -2892,7 +2892,7 @@ void p3ChatService::unsubscribeChatLobby(const ChatLobbyId& id)
// remove history
mHistoryMgr->clear(it->second.virtual_peer_id);
//mHistoryMgr->clear(it->second.virtual_peer_id);
// remove lobby information