Remove Message.getFolder()

This commit is contained in:
cketti 2020-01-08 22:50:26 +01:00
parent 0d303c8740
commit 13bc16caf8
13 changed files with 46 additions and 128 deletions

View file

@ -5,6 +5,7 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Objects;
import android.content.ContentValues;
import android.database.Cursor;
@ -13,11 +14,9 @@ import androidx.annotation.VisibleForTesting;
import com.fsck.k9.Account;
import com.fsck.k9.K9;
import com.fsck.k9.core.BuildConfig;
import com.fsck.k9.controller.MessageReference;
import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.Folder;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.internet.AddressHeaderBuilder;
import com.fsck.k9.mail.internet.MimeMessage;
@ -42,13 +41,10 @@ public class LocalMessage extends MimeMessage {
private String mimeType;
private PreviewType previewType;
private boolean headerNeedsUpdating = false;
private LocalFolder mFolder;
private LocalMessage(LocalStore localStore) {
this.localStore = localStore;
}
LocalMessage(LocalStore localStore, String uid, Folder folder) {
LocalMessage(LocalStore localStore, String uid, LocalFolder folder) {
this.localStore = localStore;
this.mUid = uid;
this.mFolder = folder;
@ -402,9 +398,8 @@ public class LocalMessage extends MimeMessage {
return messageReference;
}
@Override
public LocalFolder getFolder() {
return (LocalFolder) super.getFolder();
return mFolder;
}
public String getUri() {
@ -456,20 +451,22 @@ public class LocalMessage extends MimeMessage {
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
// distinguish by account uuid, in addition to what Message.equals() does above
String thisAccountUuid = getAccountUuid();
String thatAccountUuid = ((LocalMessage) o).getAccountUuid();
return thisAccountUuid != null ? thisAccountUuid.equals(thatAccountUuid) : thatAccountUuid == null;
LocalMessage other = (LocalMessage) o;
return Objects.equals(mUid, other.mUid) &&
Objects.equals(mFolder, other.mFolder) &&
Objects.equals(getAccountUuid(), other.getAccountUuid());
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (getAccountUuid() != null ? getAccountUuid().hashCode() : 0);
final int MULTIPLIER = 31;
int result = 1;
String accountUuid = getAccountUuid();
result = MULTIPLIER * result + (accountUuid != null ? accountUuid.hashCode() : 0);
result = MULTIPLIER * result + (mFolder != null ? mFolder.hashCode() : 0);
result = MULTIPLIER * result + mUid.hashCode();
return result;
}

View file

@ -1,15 +1,15 @@
package com.fsck.k9.notification
import com.fsck.k9.Account
import com.fsck.k9.mail.Message
import com.fsck.k9.mailstore.LocalFolder
import com.fsck.k9.mailstore.LocalMessage
interface NotificationStrategy {
fun shouldNotifyForMessage(
account: Account,
localFolder: LocalFolder,
message: Message,
message: LocalMessage,
isOldMessage: Boolean
): Boolean
}

View file

@ -4,15 +4,15 @@ import com.fsck.k9.Account
import com.fsck.k9.K9
import com.fsck.k9.helper.Contacts
import com.fsck.k9.mail.Flag
import com.fsck.k9.mail.Message
import com.fsck.k9.mailstore.LocalFolder
import com.fsck.k9.mailstore.LocalMessage
class K9NotificationStrategy(val contacts: Contacts) : NotificationStrategy {
override fun shouldNotifyForMessage(
account: Account,
localFolder: LocalFolder,
message: Message,
message: LocalMessage,
isOldMessage: Boolean
): Boolean {

View file

@ -26,8 +26,6 @@ public abstract class Message implements Part, Body {
private Date mInternalDate;
protected Folder mFolder;
public boolean olderThan(Date earliestDate) {
if (earliestDate == null) {
return false;
@ -39,26 +37,6 @@ public abstract class Message implements Part, Body {
return myDate != null && myDate.before(earliestDate);
}
@Override
public boolean equals(Object o) {
if (o == null || !(o instanceof Message)) {
return false;
}
Message other = (Message)o;
return (getUid().equals(other.getUid())
&& getFolder().getServerId().equals(other.getFolder().getServerId()));
}
@Override
public int hashCode() {
final int MULTIPLIER = 31;
int result = 1;
result = MULTIPLIER * result + (mFolder != null ? mFolder.getServerId().hashCode() : 0);
result = MULTIPLIER * result + mUid.hashCode();
return result;
}
public String getUid() {
return mUid;
}
@ -67,10 +45,6 @@ public abstract class Message implements Part, Body {
this.mUid = uid;
}
public Folder getFolder() {
return mFolder;
}
public abstract String getSubject();
public abstract void setSubject(String subject);

View file

@ -457,7 +457,7 @@ public class ImapFolder extends Folder<ImapMessage> {
@Override
public ImapMessage getMessage(String uid) throws MessagingException {
return new ImapMessage(uid, this);
return new ImapMessage(uid);
}
@Override
@ -587,7 +587,7 @@ public class ImapFolder extends Folder<ImapMessage> {
listener.messageStarted(uid, i, count);
}
ImapMessage message = new ImapMessage(uid, this);
ImapMessage message = new ImapMessage(uid);
messages.add(message);
if (listener != null) {
@ -817,19 +817,19 @@ public class ImapFolder extends Folder<ImapMessage> {
for (int i = 0, count = flags.size(); i < count; i++) {
String flag = flags.getString(i);
if (flag.equalsIgnoreCase("\\Deleted")) {
message.setFlagInternal(Flag.DELETED, true);
message.setFlag(Flag.DELETED, true);
} else if (flag.equalsIgnoreCase("\\Answered")) {
message.setFlagInternal(Flag.ANSWERED, true);
message.setFlag(Flag.ANSWERED, true);
} else if (flag.equalsIgnoreCase("\\Seen")) {
message.setFlagInternal(Flag.SEEN, true);
message.setFlag(Flag.SEEN, true);
} else if (flag.equalsIgnoreCase("\\Flagged")) {
message.setFlagInternal(Flag.FLAGGED, true);
message.setFlag(Flag.FLAGGED, true);
} else if (flag.equalsIgnoreCase("$Forwarded")) {
message.setFlagInternal(Flag.FORWARDED, true);
message.setFlag(Flag.FORWARDED, true);
/* a message contains FORWARDED FLAG -> so we can also create them */
store.getPermanentFlagsIndex().add(Flag.FORWARDED);
} else if (flag.equalsIgnoreCase("\\Draft")){
message.setFlagInternal(Flag.DRAFT, true);
message.setFlag(Flag.DRAFT, true);
}
}
}

View file

@ -587,7 +587,7 @@ class ImapFolderPusher extends ImapFolder {
List<Message> messages = new ArrayList<>();
for (long uid = startUid; uid <= newUid; uid++) {
ImapMessage message = new ImapMessage(Long.toString(uid), ImapFolderPusher.this);
ImapMessage message = new ImapMessage(Long.toString(uid));
messages.add(message);
}
@ -626,10 +626,10 @@ class ImapFolderPusher extends ImapFolder {
}
for (String uid : removeUids) {
ImapMessage message = new ImapMessage(uid, ImapFolderPusher.this);
ImapMessage message = new ImapMessage(uid);
try {
message.setFlagInternal(Flag.DELETED, true);
message.setFlag(Flag.DELETED, true);
} catch (MessagingException me) {
Timber.e("Unable to set DELETED flag on message %s", message.getUid());
}
@ -662,7 +662,7 @@ class ImapFolderPusher extends ImapFolder {
List<Message> messages = new ArrayList<>(count);
for (long uid = startUid; uid < uidNext; uid++) {
ImapMessage message = new ImapMessage(Long.toString(uid), ImapFolderPusher.this);
ImapMessage message = new ImapMessage(Long.toString(uid));
messages.add(message);
}

View file

@ -1,31 +1,15 @@
package com.fsck.k9.mail.store.imap;
import java.util.Collections;
import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.Folder;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.internet.MimeMessage;
public class ImapMessage extends MimeMessage {
ImapMessage(String uid, Folder folder) {
ImapMessage(String uid) {
this.mUid = uid;
this.mFolder = folder;
}
public void setSize(int size) {
this.mSize = size;
}
public void setFlagInternal(Flag flag, boolean set) throws MessagingException {
super.setFlag(flag, set);
}
@Override
public void setFlag(Flag flag, boolean set) throws MessagingException {
super.setFlag(flag, set);
mFolder.setFlags(Collections.singletonList(this), Collections.singleton(flag), set);
}
}

View file

@ -851,7 +851,7 @@ public class ImapFolderTest {
folder.fetch(messages, fetchProfile, null);
ImapMessage imapMessage = messages.get(0);
verify(imapMessage).setFlagInternal(Flag.SEEN, true);
verify(imapMessage).setFlag(Flag.SEEN, true);
}
@Test
@ -1043,13 +1043,12 @@ public class ImapFolderTest {
}
@Test
public void getMessageByUid_returnsNewImapMessageWithUidInFolder() throws Exception {
public void getMessageByUid_returnsNewImapMessageWithUid() throws Exception {
ImapFolder folder = createFolder("Folder");
ImapMessage message = folder.getMessage("uid");
assertEquals("uid", message.getUid());
assertEquals(folder, message.getFolder());
}
private Part createPlainTextPart(String serverExtra) {

View file

@ -137,7 +137,7 @@ public class Pop3Folder extends Folder<Pop3Message> {
public Pop3Message getMessage(String uid) throws MessagingException {
Pop3Message message = uidToMsgMap.get(uid);
if (message == null) {
message = new Pop3Message(uid, this);
message = new Pop3Message(uid);
}
return message;
}
@ -214,7 +214,7 @@ public class Pop3Folder extends Folder<Pop3Message> {
return;
}
String msgUid = uidParts[2];
message = new Pop3Message(msgUid, this);
message = new Pop3Message(msgUid);
indexMessage(msgNum, message);
}
}
@ -255,7 +255,7 @@ public class Pop3Folder extends Folder<Pop3Message> {
if (msgNum >= start && msgNum <= end) {
Pop3Message message = msgNumToMsgMap.get(msgNum);
if (message == null) {
message = new Pop3Message(msgUid, this);
message = new Pop3Message(msgUid);
indexMessage(msgNum, message);
}
}
@ -302,7 +302,7 @@ public class Pop3Folder extends Folder<Pop3Message> {
Pop3Message message = uidToMsgMap.get(msgUid);
if (message == null) {
message = new Pop3Message(msgUid, this);
message = new Pop3Message(msgUid);
}
indexMessage(msgNum, message);
}

View file

@ -1,27 +1,16 @@
package com.fsck.k9.mail.store.pop3;
import java.util.Collections;
import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.internet.MimeMessage;
public class Pop3Message extends MimeMessage {
Pop3Message(String uid, Pop3Folder folder) {
Pop3Message(String uid) {
mUid = uid;
mFolder = folder;
mSize = -1;
}
public void setSize(int size) {
mSize = size;
}
@Override
public void setFlag(Flag flag, boolean set) throws MessagingException {
super.setFlag(flag, set);
mFolder.setFlags(Collections.singletonList(this), Collections.singleton(flag), set);
}
}

View file

@ -487,7 +487,7 @@ public class WebDavFolder extends Folder<WebDavMessage> {
WebDavMessage wdMessage = (WebDavMessage) messages.get(i);
try {
wdMessage.setFlagInternal(Flag.SEEN, uidToReadStatus.get(wdMessage.getUid()));
wdMessage.setFlag(Flag.SEEN, uidToReadStatus.get(wdMessage.getUid()));
} catch (NullPointerException e) {
Timber.v(e, "Under some weird circumstances, " +
"setting the read status when syncing from webdav threw an NPE. Skipping.");
@ -550,7 +550,7 @@ public class WebDavFolder extends Folder<WebDavMessage> {
ParsedMessageEnvelope envelope = envelopes.get(message.getUid());
if (envelope != null) {
message.setNewHeaders(envelope);
message.setFlagInternal(Flag.SEEN, envelope.getReadStatus());
message.setFlag(Flag.SEEN, envelope.getReadStatus());
} else {
Timber.e("Asked to get metadata for a non-existent message: %s", message.getUid());
}

View file

@ -1,15 +1,13 @@
package com.fsck.k9.mail.store.webdav;
import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.Folder;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.internet.MimeMessage;
import timber.log.Timber;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import timber.log.Timber;
import static com.fsck.k9.mail.helper.UrlEncodingHelper.decodeUtf8;
import static com.fsck.k9.mail.helper.UrlEncodingHelper.encodeUtf8;
@ -17,10 +15,11 @@ import static com.fsck.k9.mail.helper.UrlEncodingHelper.encodeUtf8;
* A WebDav Message
*/
public class WebDavMessage extends MimeMessage {
private final WebDavFolder mFolder;
private String mUrl = "";
WebDavMessage(String uid, Folder folder) {
WebDavMessage(String uid, WebDavFolder folder) {
this.mUid = uid;
this.mFolder = folder;
}
@ -74,10 +73,6 @@ public class WebDavMessage extends MimeMessage {
this.mSize = size;
}
public void setFlagInternal(Flag flag, boolean set) throws MessagingException {
super.setFlag(flag, set);
}
public void setNewHeaders(ParsedMessageEnvelope envelope) throws MessagingException {
String[] headers = envelope.getHeaderList();
Map<String, String> messageHeaders = envelope.getMessageHeaders();
@ -94,10 +89,4 @@ public class WebDavMessage extends MimeMessage {
}
}
}
@Override
public void setFlag(Flag flag, boolean set) throws MessagingException {
super.setFlag(flag, set);
mFolder.setFlags(Collections.singletonList(this), Collections.singleton(flag), set);
}
}

View file

@ -1,17 +1,14 @@
package com.fsck.k9.mail.store.webdav;
import java.util.Collections;
import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.MessagingException;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class WebDavMessageTest {
@ -19,10 +16,6 @@ public class WebDavMessageTest {
private WebDavMessage message;
@Mock
private WebDavFolder mockFolder;
@Mock
private WebDavStore mockStore;
@Mock
private WebDavFolder mockTrashFolder;
@Before
public void before() {
@ -55,11 +48,4 @@ public class WebDavMessageTest {
message.setNewHeaders(parsedMessageEnvelope);
assertEquals(1024, message.getSize());
}
@Test
public void setFlag_asks_folder_to_set_flag() throws MessagingException {
message.setFlag(Flag.FLAGGED, true);
verify(mockFolder).setFlags(Collections.singletonList(message),
Collections.singleton(Flag.FLAGGED), true);
}
}