Skip to content

Commit

Permalink
InterfaceMessages.DeleteByDBID deletes all messages in IMAP folder. h…
Browse files Browse the repository at this point in the history
  • Loading branch information
hmailserver-dev committed Feb 19, 2017
1 parent 155bc87 commit c670cc1
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 63 deletions.
80 changes: 40 additions & 40 deletions hmailserver/source/Server/COM/InterfaceMessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ STDMETHODIMP InterfaceMessages::get_Count(long *pVal)
}
}

void
InterfaceMessages::Attach(std::shared_ptr<HM::Messages> pMessages)
void
InterfaceMessages::Attach(std::shared_ptr<HM::Messages> pMessages)
{
messages_ = pMessages;
messages_ = pMessages;
}

STDMETHODIMP InterfaceMessages::get_Item(long Index, IInterfaceMessage **pVal)
Expand All @@ -44,9 +44,9 @@ STDMETHODIMP InterfaceMessages::get_Item(long Index, IInterfaceMessage **pVal)

CComObject<InterfaceMessage>* pInterfaceMessage = new CComObject<InterfaceMessage>();
pInterfaceMessage->SetAuthentication(authentication_);

std::shared_ptr<HM::Message> pMsg = messages_->GetItem(Index);

if (pMsg)
{
pInterfaceMessage->AttachItem(pMsg);
Expand All @@ -55,9 +55,9 @@ STDMETHODIMP InterfaceMessages::get_Item(long Index, IInterfaceMessage **pVal)
}
else
{
return DISP_E_BADINDEX;
}
return DISP_E_BADINDEX;
}

return S_OK;
}
catch (...)
Expand All @@ -74,12 +74,12 @@ STDMETHODIMP InterfaceMessages::get_ItemByDBID(hyper DBID, IInterfaceMessage **p
return GetAccessDenied();

//

CComObject<InterfaceMessage>* pInterfaceMessage = new CComObject<InterfaceMessage>();
pInterfaceMessage->SetAuthentication(authentication_);

std::shared_ptr<HM::Message> pMsg = messages_->GetItemByDBID(DBID);

if (pMsg)
{
pInterfaceMessage->AttachItem(pMsg);
Expand All @@ -88,9 +88,9 @@ STDMETHODIMP InterfaceMessages::get_ItemByDBID(hyper DBID, IInterfaceMessage **p
}
else
{
return DISP_E_BADINDEX;
return DISP_E_BADINDEX;
}

return S_OK;
}
catch (...)
Expand All @@ -106,29 +106,29 @@ STDMETHODIMP InterfaceMessages::Add(IInterfaceMessage **pVal)
if (!messages_)
return GetAccessDenied();

__int64 iAccountID = messages_->GetAccountID();
__int64 iFolderID = messages_->GetFolderID();
__int64 iAccountID = messages_->GetAccountID();
__int64 iFolderID = messages_->GetFolderID();

if (iFolderID == -1)
{
// We can't add a new message when we're not browsing the
// message cache.
return DISP_E_BADINDEX;
}

std::shared_ptr<HM::Message> pNewMsg = std::shared_ptr<HM::Message>(new HM::Message);
pNewMsg->SetAccountID(iAccountID);
pNewMsg->SetFolderID(iFolderID);
pNewMsg->SetState(HM::Message::Created);

CComObject<InterfaceMessage>* pInterfaceMessage = new CComObject<InterfaceMessage>();

pInterfaceMessage->AttachParent(messages_, false);
pInterfaceMessage->SetAuthentication(authentication_);
pInterfaceMessage->AttachItem(pNewMsg);
pInterfaceMessage->AddRef();
*pVal = pInterfaceMessage;

return S_OK;
}
catch (...)
Expand All @@ -144,53 +144,53 @@ STDMETHODIMP InterfaceMessages::DeleteByDBID(hyper lDBID)
if (!messages_)
return GetAccessDenied();

std::shared_ptr<HM::Message> pMsg = messages_->GetItemByDBID(lDBID);
std::shared_ptr<HM::Message> pMsg = messages_->GetItemByDBID(lDBID);

if (!pMsg)
{
// No such message exists
return DISP_E_BADINDEX;
}

// Mark the message for deletion.
if (!messages_->DeleteMessageByDBID(lDBID))
return DISP_E_BADINDEX;


// Expunge the mailbox. Will cause the message to be
// deleted from disk and database.
std::function<bool(int, std::shared_ptr<HM::Message>)> filter = [](int index, std::shared_ptr<HM::Message> message)
std::function<bool(int, std::shared_ptr<HM::Message>)> filter = [lDBID](int index, std::shared_ptr<HM::Message> message)
{
if (message->GetID() == lDBID)
{
return true;
};
}

return false;
};

messages_->DeleteMessages(filter);

auto messages = HM::MessagesContainer::Instance()->GetMessages(messages_->GetAccountID(), messages_->GetFolderID());
messages->DeleteMessages(filter);

// If we're aren't browsing in the message cache already,
// we need to delete the message from the cache now. This
// is needed if the messages are accessed using the
// Account.Messages property. When messages are accessed
// via Account.IMAPFolders, we access the message cache
// directly, and when we don't need to remove message here.
// (Since it was done like 10 lines up).

__int64 iFolderID = messages_->GetFolderID();
if (iFolderID == -1)
{
// We're not browsing the message cache.
__int64 iAccountID = messages_->GetAccountID();


std::vector<__int64> affectedMessages;
affectedMessages.push_back(lDBID);

// Notify clients that the message has been dropped.
std::shared_ptr<HM::ChangeNotification> notification =
std::shared_ptr<HM::ChangeNotification> notification =
std::shared_ptr<HM::ChangeNotification>(new HM::ChangeNotification(messages_->GetAccountID(), messages_->GetFolderID(), HM::ChangeNotification::NotificationMessageDeleted, affectedMessages));

HM::Application::Instance()->GetNotificationServer()->SendNotification(notification);
}

return S_OK;
}
catch (...)
Expand All @@ -199,7 +199,7 @@ STDMETHODIMP InterfaceMessages::DeleteByDBID(hyper lDBID)
}
}

STDMETHODIMP
STDMETHODIMP
InterfaceMessages::Clear()
{
try
Expand Down
21 changes: 0 additions & 21 deletions hmailserver/source/Server/Common/BO/Messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,27 +240,6 @@ namespace HM
}
}

bool
Messages::DeleteMessageByDBID(__int64 ID)
//---------------------------------------------------------------------------()
// DESCRIPTION:
// Deletes a message from the collection with the given database identifier.
//---------------------------------------------------------------------------()
{
boost::lock_guard<boost::recursive_mutex> guard(_mutex);

for(std::shared_ptr<Message> pCurMsg : vecObjects)
{
if (pCurMsg->GetID() == ID)
{
pCurMsg->SetFlagDeleted(true);
return true;
}
}

return false;
}

void
Messages::RemoveRecentFlags()
{
Expand Down
2 changes: 0 additions & 2 deletions hmailserver/source/Server/Common/BO/Messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ namespace HM

void Refresh(bool update_recent_flags);

bool DeleteMessageByDBID(__int64 ID);

void AddToCollection(std::shared_ptr<DALRecordset> pRS);

void Remove(__int64 iDBID);
Expand Down
91 changes: 91 additions & 0 deletions hmailserver/test/RegressionTests/API/Messages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) 2010 Martin Knafve / hMailServer.com.
// http://www.hmailserver.com

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Mail;
using System.Reflection;
using System.Text;
using NUnit.Framework;
using RegressionTests.Infrastructure;
using RegressionTests.Shared;
using hMailServer;
using Attachment = System.Net.Mail.Attachment;

namespace RegressionTests.API
{
[TestFixture]
public class Messages : TestFixtureBase
{
[Test]
public void DeletingSingleMessageShouldNotAffectOtherMessages()
{
var account = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "[email protected]", "test");

// Add 3 messages to the inbox
var imapClientSimulator = new ImapClientSimulator();
imapClientSimulator.ConnectAndLogon("[email protected]", "test");
for (int i = 0; i < 3; i++)
imapClientSimulator.SendSingleCommandWithLiteral("A01 APPEND INBOX {4}", "ABCD");
imapClientSimulator.Disconnect();

var messages = account.Messages;

var firstId = messages[0].ID;
var secondId = messages[1].ID;
var thirdId = messages[2].ID;

messages.DeleteByDBID(secondId);

Assert.AreEqual(2, messages.Count);
Assert.AreEqual(firstId, messages[0].ID);
Assert.AreEqual(thirdId, messages[1].ID);

imapClientSimulator.ConnectAndLogon("[email protected]", "test");
var imapMessageCount = imapClientSimulator.GetMessageCount("Inbox");
Assert.AreEqual(imapMessageCount, messages.Count);

}

[Test]
public void DeletingMultipleMessagesShouldSucceed()
{
var account = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "[email protected]", "test");

var imapClientSimulator = new ImapClientSimulator();
imapClientSimulator.ConnectAndLogon("[email protected]", "test");
for (int i = 0; i < 10; i++)
imapClientSimulator.SendSingleCommandWithLiteral("A01 APPEND INBOX {4}", "ABCD");
imapClientSimulator.Disconnect();

var messages = account.Messages;

var messagesToDelete = new List<long>();
var messagesToSave = new List<long>();

for (int i = 0; i < 10; i++)
{
if (i <= 3)
messagesToDelete.Add(messages[i].ID);
else
messagesToSave.Add(messages[i].ID);
}

// Delete the 4 messages
foreach (var messageToDelete in messagesToDelete)
messages.DeleteByDBID(messageToDelete);

// Should be 6 remaining
Assert.AreEqual(6, messages.Count);

// Validate that the 6 remaining are the expected ones
foreach (var messageToSave in messagesToSave)
{
var message = messages.get_ItemByDBID(messageToSave);
Assert.IsNotNull(message);
}
}

}
}
1 change: 1 addition & 0 deletions hmailserver/test/RegressionTests/RegressionTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="API\Messages.cs" />
<Compile Include="IMAP\ConcurrentConnections.cs" />
<Compile Include="Infrastructure\AccountProperties.cs" />
<Compile Include="Infrastructure\AccountServices.cs" />
Expand Down

0 comments on commit c670cc1

Please sign in to comment.