Cache messages also when you leave room but do not disconnect

This commit is contained in:
HanzZ 2012-12-21 11:20:05 +01:00
parent 92b1f98bce
commit 8761e976ae
7 changed files with 105 additions and 4 deletions

View file

@ -117,6 +117,9 @@ void MyIrcSession::on_quit(IrcMessage *message) {
for(AutoJoinMap::iterator it = m_autoJoin.begin(); it != m_autoJoin.end(); it++) {
bool op = 0;
std::string nickname = TO_UTF8(m->sender().name());
if (!hasIRCBuddy(it->second->getChannel(), nickname)) {
continue;
}
op = correctNickname(nickname);
removeIRCBuddy(it->second->getChannel(), nickname);
LOG4CXX_INFO(logger, user << ": " << nickname << " quit " << it->second->getChannel() + suffix);

View file

@ -140,7 +140,7 @@ class Conversation {
void sendParticipants(const Swift::JID &to);
void sendCachedMessages(const Swift::JID &to);
void sendCachedMessages(const Swift::JID &to = Swift::JID());
private:
Swift::Presence::ref generatePresence(const std::string &nick, int flag, int status, const std::string &statusMessage, const std::string &newname = "");

View file

@ -59,6 +59,8 @@ class ConversationManager {
/// \return Conversation or NULL.
Conversation *getConversation(const std::string &name);
void sendCachedChatMessages();
/// Adds new Conversation to the manager.
/// \param conv Conversation.

View file

@ -127,7 +127,20 @@ void Conversation::handleMessage(boost::shared_ptr<Swift::Message> &message, con
message->setFrom(Swift::JID(m_room, m_conversationManager->getComponent()->getJID().toBare(), n));
}
}
m_conversationManager->getComponent()->getStanzaChannel()->sendMessage(message);
if (m_conversationManager->getComponent()->inServerMode() && m_conversationManager->getUser()->shouldCacheMessages()) {
boost::posix_time::ptime timestamp = boost::posix_time::second_clock::universal_time();
boost::shared_ptr<Swift::Delay> delay(boost::make_shared<Swift::Delay>());
delay->setStamp(timestamp);
message->addPayload(delay);
m_cachedMessages.push_back(message);
if (m_cachedMessages.size() > 100) {
m_cachedMessages.pop_front();
}
}
else {
m_conversationManager->getComponent()->getStanzaChannel()->sendMessage(message);
}
}
else {
std::string legacyName = m_legacyName;
@ -142,7 +155,7 @@ void Conversation::handleMessage(boost::shared_ptr<Swift::Message> &message, con
message->setFrom(Swift::JID(legacyName, m_conversationManager->getComponent()->getJID().toBare(), n));
if (m_conversationManager->getUser()->shouldCacheMessages()) {
if (m_jids.empty()) {
boost::posix_time::ptime timestamp = boost::posix_time::second_clock::universal_time();
boost::shared_ptr<Swift::Delay> delay(boost::make_shared<Swift::Delay>());
delay->setStamp(timestamp);
@ -176,7 +189,12 @@ void Conversation::sendParticipants(const Swift::JID &to) {
void Conversation::sendCachedMessages(const Swift::JID &to) {
for (std::list<boost::shared_ptr<Swift::Message> >::const_iterator it = m_cachedMessages.begin(); it != m_cachedMessages.end(); it++) {
(*it)->setTo(to);
if (to.isValid()) {
(*it)->setTo(to);
}
else {
(*it)->setTo(m_jid.toBare());
}
m_conversationManager->getComponent()->getStanzaChannel()->sendMessage(*it);
}
m_cachedMessages.clear();

View file

@ -42,6 +42,15 @@ ConversationManager::~ConversationManager() {
deleteAllConversations();
}
void ConversationManager::sendCachedChatMessages() {
for (std::map<std::string, Conversation *>::const_iterator it = m_convs.begin(); it != m_convs.end(); it++) {
if ((*it).second->isMUC()) {
continue;
}
(*it).second->sendCachedMessages();
}
}
void ConversationManager::deleteAllConversations() {
while(!m_convs.empty()) {
LOG4CXX_INFO(logger, m_user->getJID().toString() << ": Removing conversation " << (*m_convs.begin()).first);

View file

@ -27,6 +27,7 @@ class ConversationManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTe
CPPUNIT_TEST(handleNormalMessagesHeadline);
CPPUNIT_TEST(handleGroupchatMessages);
CPPUNIT_TEST(handleGroupchatMessagesBouncer);
CPPUNIT_TEST(handleGroupchatMessagesBouncerLeave);
CPPUNIT_TEST(handleGroupchatMessagesTwoResources);
CPPUNIT_TEST(handleChatstateMessages);
CPPUNIT_TEST(handleSubjectMessages);
@ -355,6 +356,71 @@ class ConversationManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTe
}
void handleGroupchatMessagesBouncerLeave() {
User *user = userManager->getUser("user@localhost");
user->addUserSetting("stay_connected", "1");
TestingConversation *conv = new TestingConversation(user->getConversationManager(), "#room", true);
user->getConversationManager()->addConversation(conv);
conv->onMessageToSend.connect(boost::bind(&ConversationManagerTest::handleMessageReceived, this, _1, _2));
conv->setNickname("nickname");
conv->addJID("user@localhost/resource");
CPPUNIT_ASSERT(!user->shouldCacheMessages());
Swift::Presence::ref response3 = Swift::Presence::create();
response3->setType(Swift::Presence::Unavailable);
response3->setTo("#room@localhost/hanzz");
response3->setFrom("user@localhost/resource");
Swift::MUCPayload *payload3 = new Swift::MUCPayload();
payload3->setPassword("password");
response3->addPayload(boost::shared_ptr<Swift::Payload>(payload3));
injectPresence(response3);
loop->processEvents();
CPPUNIT_ASSERT(!user->shouldCacheMessages());
// reset resources should not touch this resource
user->getConversationManager()->resetResources();
boost::shared_ptr<Swift::Message> msg(new Swift::Message());
msg->setBody("hi there!");
conv->handleMessage(msg, "anotheruser");
boost::shared_ptr<Swift::Message> msg2(new Swift::Message());
msg2->setBody("hi there2!");
conv->handleMessage(msg2, "anotheruser");
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
userRegistry->isValidUserPassword(Swift::JID("user@localhost/resource"), serverFromClientSession.get(), Swift::createSafeByteArray("password"));
userRegistry->onPasswordValid(Swift::JID("user@localhost/resource"));
loop->processEvents();
Swift::Presence::ref response = Swift::Presence::create();
response->setTo("#room@localhost/hanzz");
response->setFrom("user@localhost/resource");
Swift::MUCPayload *payload = new Swift::MUCPayload();
payload->setPassword("password");
response->addPayload(boost::shared_ptr<Swift::Payload>(payload));
injectPresence(response);
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(4, (int) received.size());
CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[2])));
CPPUNIT_ASSERT_EQUAL(std::string("hi there!"), dynamic_cast<Swift::Message *>(getStanza(received[2]))->getBody());
CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Message *>(getStanza(received[2]))->getTo().toString());
CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Message *>(getStanza(received[2]))->getFrom().toString());
CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[3])));
CPPUNIT_ASSERT_EQUAL(std::string("hi there2!"), dynamic_cast<Swift::Message *>(getStanza(received[3]))->getBody());
CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Message *>(getStanza(received[3]))->getTo().toString());
CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Message *>(getStanza(received[3]))->getFrom().toString());
}
void handleGroupchatMessagesTwoResources() {
connectSecondResource();
received2.clear();

View file

@ -186,6 +186,9 @@ void User::setConnected(bool connected) {
}
void User::setCacheMessages(bool cacheMessages) {
if (m_component->inServerMode() && !m_cacheMessages && cacheMessages) {
m_conversationManager->sendCachedChatMessages();
}
m_cacheMessages = cacheMessages;
}