2018-08-02 13:33:06 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
|
|
/*
|
2018-08-14 05:53:24 +00:00
|
|
|
This file is part of FairEmail.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-14 05:53:24 +00:00
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
2018-08-02 13:33:06 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
2018-10-29 10:46:49 +00:00
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
2018-08-02 13:33:06 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2018-10-29 10:46:49 +00:00
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2021-01-01 08:56:36 +01:00
|
|
|
Copyright 2018-2021 by Marcel Bokhorst (M66B)
|
2018-08-02 13:33:06 +00:00
|
|
|
*/
|
|
|
|
|
|
2019-01-04 18:37:56 +00:00
|
|
|
import android.content.Context;
|
2019-01-24 07:13:54 +00:00
|
|
|
import android.content.SharedPreferences;
|
2019-09-27 14:14:04 +02:00
|
|
|
import android.text.TextUtils;
|
2019-01-04 18:37:56 +00:00
|
|
|
|
2019-04-17 20:21:44 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
import androidx.room.Entity;
|
|
|
|
|
import androidx.room.ForeignKey;
|
|
|
|
|
import androidx.room.Index;
|
|
|
|
|
import androidx.room.PrimaryKey;
|
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
import org.json.JSONArray;
|
2018-12-10 18:44:45 +01:00
|
|
|
import org.json.JSONException;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2019-03-14 07:45:13 +00:00
|
|
|
import java.io.File;
|
2019-01-04 18:37:56 +00:00
|
|
|
import java.io.IOException;
|
2020-01-28 14:35:49 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Collections;
|
2018-08-12 10:51:09 +00:00
|
|
|
import java.util.Date;
|
2019-09-06 16:45:37 +02:00
|
|
|
import java.util.HashMap;
|
2020-01-28 14:35:49 +01:00
|
|
|
import java.util.List;
|
2019-09-06 16:45:37 +02:00
|
|
|
import java.util.Map;
|
2019-02-26 11:05:21 +01:00
|
|
|
import java.util.Objects;
|
2018-08-06 11:05:14 +00:00
|
|
|
|
2018-08-08 06:55:47 +00:00
|
|
|
import static androidx.room.ForeignKey.CASCADE;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
|
@Entity(
|
|
|
|
|
tableName = EntityOperation.TABLE_NAME,
|
|
|
|
|
foreignKeys = {
|
2018-08-09 20:45:42 +00:00
|
|
|
@ForeignKey(childColumns = "folder", entity = EntityFolder.class, parentColumns = "id", onDelete = CASCADE),
|
2018-08-08 06:55:47 +00:00
|
|
|
@ForeignKey(childColumns = "message", entity = EntityMessage.class, parentColumns = "id", onDelete = CASCADE)
|
2018-08-02 13:33:06 +00:00
|
|
|
},
|
|
|
|
|
indices = {
|
2019-05-11 21:30:09 +02:00
|
|
|
@Index(value = {"account"}),
|
2018-08-10 09:45:36 +00:00
|
|
|
@Index(value = {"folder"}),
|
2019-03-03 12:59:58 +00:00
|
|
|
@Index(value = {"message"}),
|
2019-05-11 21:30:09 +02:00
|
|
|
@Index(value = {"name"}),
|
|
|
|
|
@Index(value = {"state"})
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
public class EntityOperation {
|
|
|
|
|
static final String TABLE_NAME = "operation";
|
|
|
|
|
|
|
|
|
|
@PrimaryKey(autoGenerate = true)
|
|
|
|
|
public Long id;
|
2019-03-12 15:54:25 +00:00
|
|
|
public Long account; // performance
|
2018-08-02 13:33:06 +00:00
|
|
|
@NonNull
|
2018-08-09 20:45:42 +00:00
|
|
|
public Long folder;
|
2018-08-02 13:33:06 +00:00
|
|
|
public Long message;
|
|
|
|
|
@NonNull
|
|
|
|
|
public String name;
|
2018-08-11 08:28:54 +00:00
|
|
|
@NonNull
|
2018-08-02 13:33:06 +00:00
|
|
|
public String args;
|
2018-08-12 10:51:09 +00:00
|
|
|
@NonNull
|
|
|
|
|
public Long created;
|
2020-02-11 17:46:04 +01:00
|
|
|
@NonNull
|
|
|
|
|
public int tries = 0;
|
2019-05-11 21:04:27 +02:00
|
|
|
public String state;
|
2018-12-01 15:13:57 +01:00
|
|
|
public String error;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-12-09 15:49:43 +01:00
|
|
|
static final String ADD = "add";
|
|
|
|
|
static final String MOVE = "move";
|
2019-03-02 14:32:19 +00:00
|
|
|
static final String COPY = "copy";
|
2019-09-17 13:38:23 +02:00
|
|
|
static final String FETCH = "fetch";
|
2019-09-17 16:01:29 +02:00
|
|
|
static final String DELETE = "delete";
|
2018-12-09 15:49:43 +01:00
|
|
|
static final String SEEN = "seen";
|
|
|
|
|
static final String ANSWERED = "answered";
|
|
|
|
|
static final String FLAG = "flag";
|
|
|
|
|
static final String KEYWORD = "keyword";
|
2020-06-25 09:14:05 +02:00
|
|
|
static final String LABEL = "label"; // Gmail
|
2018-12-09 15:49:43 +01:00
|
|
|
static final String HEADERS = "headers";
|
2019-01-16 17:37:45 +00:00
|
|
|
static final String RAW = "raw";
|
2018-12-09 15:49:43 +01:00
|
|
|
static final String BODY = "body";
|
|
|
|
|
static final String ATTACHMENT = "attachment";
|
|
|
|
|
static final String SYNC = "sync";
|
2019-04-25 18:47:52 +02:00
|
|
|
static final String SUBSCRIBE = "subscribe";
|
2019-09-17 13:38:23 +02:00
|
|
|
static final String SEND = "send";
|
2019-09-17 14:22:18 +02:00
|
|
|
static final String EXISTS = "exists";
|
2020-07-13 16:34:55 +02:00
|
|
|
static final String RULE = "rule";
|
2020-07-26 19:58:13 +02:00
|
|
|
static final String PURGE = "purge";
|
2021-02-12 12:51:34 +01:00
|
|
|
static final String EXPUNGE = "expunge";
|
2018-08-06 11:05:14 +00:00
|
|
|
|
2020-11-06 08:09:26 +01:00
|
|
|
private static final int MAX_FETCH = 100; // operations
|
|
|
|
|
|
2019-05-18 08:49:20 +02:00
|
|
|
static void queue(Context context, EntityMessage message, String name, Object... values) {
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
2018-12-10 18:44:45 +01:00
|
|
|
try {
|
2019-11-03 15:25:13 +01:00
|
|
|
JSONArray jargs = new JSONArray();
|
|
|
|
|
for (Object value : values)
|
|
|
|
|
jargs.put(value);
|
|
|
|
|
|
2018-12-10 18:44:45 +01:00
|
|
|
if (SEEN.equals(name)) {
|
2019-09-10 18:05:27 +02:00
|
|
|
boolean seen = jargs.getBoolean(0);
|
2019-06-23 10:18:43 +02:00
|
|
|
boolean ignore = jargs.optBoolean(1, true);
|
2020-06-18 14:22:25 +02:00
|
|
|
for (EntityMessage similar : db.message().getMessagesBySimilarity(message.account, message.id, message.msgid))
|
|
|
|
|
if (message.ui_seen != seen || message.ui_ignored != ignore) {
|
|
|
|
|
db.message().setMessageUiSeen(similar.id, seen);
|
|
|
|
|
db.message().setMessageUiIgnored(similar.id, ignore);
|
|
|
|
|
queue(context, similar.account, similar.folder, similar.id, name, jargs);
|
|
|
|
|
}
|
2019-11-03 15:25:13 +01:00
|
|
|
return;
|
2018-12-10 18:44:45 +01:00
|
|
|
|
2019-05-15 11:10:47 +02:00
|
|
|
} else if (FLAG.equals(name)) {
|
|
|
|
|
boolean flagged = jargs.getBoolean(0);
|
|
|
|
|
Integer color = (jargs.length() > 1 && !jargs.isNull(1) ? jargs.getInt(1) : null);
|
2020-06-18 14:22:25 +02:00
|
|
|
for (EntityMessage similar : db.message().getMessagesBySimilarity(message.account, message.id, message.msgid))
|
|
|
|
|
if (message.ui_flagged != flagged || !Objects.equals(message.color, color)) {
|
|
|
|
|
db.message().setMessageUiFlagged(similar.id, flagged, flagged ? color : null);
|
|
|
|
|
queue(context, similar.account, similar.folder, similar.id, name, jargs);
|
|
|
|
|
}
|
2020-02-20 15:50:26 +01:00
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
boolean auto_important = prefs.getBoolean("auto_important", false);
|
2020-04-14 16:22:31 +02:00
|
|
|
if (auto_important)
|
|
|
|
|
db.message().setMessageImportance(message.id, flagged ? 2 : null);
|
2020-02-20 15:50:26 +01:00
|
|
|
|
2019-11-03 15:25:13 +01:00
|
|
|
return;
|
2018-12-10 18:44:45 +01:00
|
|
|
|
2019-11-03 15:25:13 +01:00
|
|
|
} else if (ANSWERED.equals(name)) {
|
2019-11-06 17:36:22 +01:00
|
|
|
for (EntityMessage similar : db.message().getMessagesBySimilarity(message.account, message.id, message.msgid)) {
|
2018-12-10 18:44:45 +01:00
|
|
|
db.message().setMessageUiAnswered(similar.id, jargs.getBoolean(0));
|
2019-11-03 15:25:13 +01:00
|
|
|
queue(context, similar.account, similar.folder, similar.id, name, jargs);
|
|
|
|
|
}
|
|
|
|
|
return;
|
2018-12-10 18:44:45 +01:00
|
|
|
|
2020-01-28 14:35:49 +01:00
|
|
|
} else if (KEYWORD.equals(name)) {
|
|
|
|
|
String keyword = jargs.getString(0);
|
|
|
|
|
boolean set = jargs.getBoolean(1);
|
|
|
|
|
|
|
|
|
|
List<String> keywords = new ArrayList<>(Arrays.asList(message.keywords));
|
|
|
|
|
while (keywords.remove(keyword))
|
|
|
|
|
;
|
|
|
|
|
if (set)
|
|
|
|
|
keywords.add(keyword);
|
|
|
|
|
Collections.sort(keywords);
|
|
|
|
|
|
2020-11-15 08:10:16 +01:00
|
|
|
message.keywords = keywords.toArray(new String[0]);
|
|
|
|
|
db.message().setMessageKeywords(message.id, DB.Converters.fromStringArray(message.keywords));
|
2020-01-28 14:35:49 +01:00
|
|
|
|
2020-06-27 08:16:34 +02:00
|
|
|
} else if (LABEL.equals(name)) {
|
|
|
|
|
String label = jargs.getString(0);
|
|
|
|
|
boolean set = jargs.getBoolean(1);
|
|
|
|
|
|
|
|
|
|
if (message.setLabel(label, set))
|
|
|
|
|
db.message().setMessageLabels(message.id, DB.Converters.fromStringArray(message.labels));
|
|
|
|
|
|
2019-11-03 15:25:13 +01:00
|
|
|
} else if (MOVE.equals(name)) {
|
2021-01-04 09:27:27 +01:00
|
|
|
// Parameters in:
|
|
|
|
|
// 0: target folder
|
|
|
|
|
// 1: mark seen
|
|
|
|
|
// 2: auto classified
|
|
|
|
|
|
|
|
|
|
// Parameters out:
|
2019-06-14 08:31:37 +02:00
|
|
|
// 0: target folder
|
2019-10-10 17:09:56 +02:00
|
|
|
// 1: mark seen
|
2019-09-28 16:16:55 +02:00
|
|
|
// 2: temporary message
|
2019-10-10 17:09:56 +02:00
|
|
|
// 3: remove flag
|
2019-01-24 07:13:54 +00:00
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
boolean autoread = prefs.getBoolean("autoread", false);
|
2019-10-01 17:31:58 +02:00
|
|
|
boolean autounflag = prefs.getBoolean("autounflag", false);
|
2020-02-03 19:25:46 +01:00
|
|
|
boolean reset_importance = prefs.getBoolean("reset_importance", false);
|
2019-01-24 07:13:54 +00:00
|
|
|
|
2021-01-03 17:21:44 +01:00
|
|
|
if (jargs.opt(1) != null) {
|
|
|
|
|
// rules, classify
|
2020-01-14 11:34:26 +01:00
|
|
|
autoread = jargs.getBoolean(1);
|
2021-01-03 17:21:44 +01:00
|
|
|
autounflag = false;
|
|
|
|
|
}
|
2021-01-04 09:27:27 +01:00
|
|
|
|
2021-01-10 17:02:27 +01:00
|
|
|
boolean auto_classified = false;
|
2021-01-04 09:27:27 +01:00
|
|
|
if (jargs.opt(2) != null) {
|
|
|
|
|
auto_classified = jargs.getBoolean(2);
|
|
|
|
|
jargs.remove(2);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 17:09:56 +02:00
|
|
|
jargs.put(1, autoread);
|
|
|
|
|
jargs.put(3, autounflag);
|
2019-10-02 09:58:54 +02:00
|
|
|
|
2018-12-14 20:15:07 +01:00
|
|
|
EntityFolder source = db.folder().getFolder(message.folder);
|
2019-01-04 18:37:56 +00:00
|
|
|
EntityFolder target = db.folder().getFolder(jargs.getLong(0));
|
2020-04-05 12:41:00 +02:00
|
|
|
if (source == null || target == null || source.id.equals(target.id))
|
2019-01-28 16:57:30 +00:00
|
|
|
return;
|
2019-01-04 18:37:56 +00:00
|
|
|
|
2020-12-08 16:06:52 +01:00
|
|
|
EntityLog.log(context, "Move message=" + message.id +
|
|
|
|
|
"@" + new Date(message.received) +
|
|
|
|
|
":" + message.subject +
|
2019-08-20 16:21:06 +02:00
|
|
|
" source=" + source.id + ":" + source.name + "" +
|
|
|
|
|
" target=" + target.id + ":" + target.name +
|
2020-02-12 08:30:53 +01:00
|
|
|
" auto read=" + autoread + " flag=" + autounflag + " importance=" + reset_importance);
|
2019-08-20 16:21:06 +02:00
|
|
|
|
2020-02-03 19:25:46 +01:00
|
|
|
if (autoread || autounflag || reset_importance)
|
2019-11-06 17:36:22 +01:00
|
|
|
for (EntityMessage similar : db.message().getMessagesBySimilarity(message.account, message.id, message.msgid)) {
|
2019-10-01 18:00:06 +02:00
|
|
|
if (autoread)
|
|
|
|
|
db.message().setMessageUiSeen(similar.id, true);
|
|
|
|
|
if (autounflag)
|
|
|
|
|
db.message().setMessageUiFlagged(similar.id, false, null);
|
2020-02-03 19:25:46 +01:00
|
|
|
if (reset_importance)
|
|
|
|
|
db.message().setMessageImportance(similar.id, null);
|
2019-10-01 18:00:06 +02:00
|
|
|
}
|
2019-09-17 11:08:17 +02:00
|
|
|
|
2020-03-20 13:49:32 +01:00
|
|
|
EntityAccount account = db.account().getAccount(message.account);
|
2020-04-26 18:02:18 +02:00
|
|
|
if ((account != null && !account.isGmail()) ||
|
2020-03-20 13:49:32 +01:00
|
|
|
!EntityFolder.ARCHIVE.equals(source.type) ||
|
2019-07-25 10:14:32 +02:00
|
|
|
EntityFolder.TRASH.equals(target.type) || EntityFolder.JUNK.equals(target.type))
|
2021-02-11 09:51:48 +01:00
|
|
|
if (!message.ui_deleted)
|
|
|
|
|
db.message().setMessageUiHide(message.id, true);
|
2018-12-10 18:44:45 +01:00
|
|
|
|
2020-05-11 09:12:29 +02:00
|
|
|
if (account != null && account.isGmail() &&
|
2020-05-11 21:52:22 +02:00
|
|
|
EntityFolder.ARCHIVE.equals(source.type) &&
|
|
|
|
|
!(EntityFolder.TRASH.equals(target.type) || EntityFolder.JUNK.equals(target.type)))
|
2020-05-11 09:12:29 +02:00
|
|
|
name = COPY;
|
|
|
|
|
|
2019-07-25 10:13:05 +02:00
|
|
|
if (message.ui_snoozed != null &&
|
|
|
|
|
(EntityFolder.ARCHIVE.equals(target.type) || EntityFolder.TRASH.equals(target.type))) {
|
|
|
|
|
message.ui_snoozed = null;
|
|
|
|
|
EntityMessage.snooze(context, message.id, null);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-25 16:17:59 +02:00
|
|
|
if (EntityFolder.JUNK.equals(source.type) && EntityFolder.INBOX.equals(target.type)) {
|
|
|
|
|
List<EntityRule> rules = db.rule().getRules(target.id);
|
|
|
|
|
for (EntityRule rule : rules)
|
|
|
|
|
if (rule.isBlockingSender(message, source))
|
|
|
|
|
db.rule().deleteRule(rule.id);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-04 18:37:56 +00:00
|
|
|
// Create copy without uid in target folder
|
2019-01-12 09:18:37 +00:00
|
|
|
// Message with same msgid can be in archive
|
2019-01-12 08:06:36 +00:00
|
|
|
if (message.uid != null &&
|
2019-09-27 14:14:04 +02:00
|
|
|
!TextUtils.isEmpty(message.msgid) &&
|
2019-01-08 07:13:44 +00:00
|
|
|
db.message().countMessageByMsgId(target.id, message.msgid) == 0) {
|
2019-03-14 07:45:13 +00:00
|
|
|
File msource = message.getFile(context);
|
|
|
|
|
|
2019-03-07 08:45:10 +00:00
|
|
|
// Copy message to target folder
|
2021-04-28 08:15:48 +02:00
|
|
|
long _id = message.id;
|
|
|
|
|
Long _identity = message.identity;
|
|
|
|
|
long _uid = message.uid;
|
|
|
|
|
Boolean _raw = message.raw;
|
|
|
|
|
Long _stored = message.stored;
|
|
|
|
|
int _notifying = message.notifying;
|
|
|
|
|
boolean _fts = message.fts;
|
2021-04-28 08:12:27 +02:00
|
|
|
boolean _auto_classified = message.auto_classified;
|
2021-04-28 08:15:48 +02:00
|
|
|
Integer _importance = message.importance;
|
|
|
|
|
boolean _seen = message.seen;
|
|
|
|
|
boolean _flagged = message.flagged;
|
|
|
|
|
boolean _ui_seen = message.ui_seen;
|
|
|
|
|
boolean _ui_flagged = message.ui_flagged;
|
|
|
|
|
Boolean _ui_hide = message.ui_hide;
|
|
|
|
|
boolean _ui_browsed = message.ui_browsed;
|
|
|
|
|
Integer _color = message.color;
|
|
|
|
|
String _error = message.error;
|
2019-06-13 17:53:56 +02:00
|
|
|
|
2019-01-04 18:37:56 +00:00
|
|
|
message.id = null;
|
2019-01-23 18:52:52 +00:00
|
|
|
message.account = target.account;
|
2019-01-04 18:37:56 +00:00
|
|
|
message.folder = target.id;
|
2019-06-13 17:53:56 +02:00
|
|
|
message.identity = null;
|
2019-01-23 18:52:52 +00:00
|
|
|
message.uid = null;
|
2020-02-06 20:12:38 +01:00
|
|
|
message.raw = null;
|
2020-11-07 14:41:20 +01:00
|
|
|
message.stored = new Date().getTime();
|
2019-09-27 12:08:25 +02:00
|
|
|
message.notifying = 0;
|
2020-01-15 10:29:16 +01:00
|
|
|
message.fts = false;
|
2021-01-04 09:27:27 +01:00
|
|
|
message.auto_classified = auto_classified;
|
2020-02-03 19:25:46 +01:00
|
|
|
if (reset_importance)
|
|
|
|
|
message.importance = null;
|
2019-01-25 12:45:37 +00:00
|
|
|
if (autoread) {
|
|
|
|
|
message.seen = true;
|
|
|
|
|
message.ui_seen = true;
|
|
|
|
|
}
|
2020-02-03 19:25:46 +01:00
|
|
|
if (autounflag) {
|
|
|
|
|
message.flagged = false;
|
|
|
|
|
message.ui_flagged = false;
|
2020-02-05 09:59:09 +01:00
|
|
|
message.color = null;
|
2020-02-03 19:25:46 +01:00
|
|
|
}
|
2021-04-17 12:22:27 +02:00
|
|
|
message.ui_hide = false;
|
2019-03-18 09:41:46 +00:00
|
|
|
message.ui_browsed = false;
|
2019-06-20 08:39:37 +02:00
|
|
|
message.error = null;
|
2019-03-14 07:45:13 +00:00
|
|
|
message.id = db.message().insertMessage(message);
|
2020-11-07 14:41:20 +01:00
|
|
|
|
2019-03-14 07:45:13 +00:00
|
|
|
File mtarget = message.getFile(context);
|
2019-06-14 08:31:37 +02:00
|
|
|
long tmpid = message.id;
|
2019-09-28 16:16:55 +02:00
|
|
|
jargs.put(2, tmpid);
|
2019-03-07 08:45:10 +00:00
|
|
|
|
2021-04-28 08:15:48 +02:00
|
|
|
message.id = _id;
|
2019-01-23 18:52:52 +00:00
|
|
|
message.account = source.account;
|
2019-01-04 18:37:56 +00:00
|
|
|
message.folder = source.id;
|
2021-04-28 08:15:48 +02:00
|
|
|
message.identity = _identity;
|
|
|
|
|
message.uid = _uid;
|
|
|
|
|
message.raw = _raw;
|
|
|
|
|
message.stored = _stored;
|
|
|
|
|
message.notifying = _notifying;
|
|
|
|
|
message.fts = _fts;
|
2021-04-28 08:12:27 +02:00
|
|
|
message.auto_classified = _auto_classified;
|
2021-04-28 08:15:48 +02:00
|
|
|
message.importance = _importance;
|
|
|
|
|
message.seen = _seen;
|
|
|
|
|
message.flagged = _flagged;
|
|
|
|
|
message.ui_seen = _ui_seen;
|
|
|
|
|
message.ui_flagged = _ui_flagged;
|
|
|
|
|
message.ui_hide = _ui_hide;
|
|
|
|
|
message.ui_browsed = _ui_browsed;
|
|
|
|
|
message.color = _color;
|
|
|
|
|
message.error = _error;
|
2019-01-19 18:13:48 +00:00
|
|
|
|
2019-01-04 18:37:56 +00:00
|
|
|
if (message.content)
|
|
|
|
|
try {
|
2019-03-14 07:45:13 +00:00
|
|
|
Helper.copy(msource, mtarget);
|
2019-01-04 18:37:56 +00:00
|
|
|
} catch (IOException ex) {
|
|
|
|
|
Log.e(ex);
|
2020-03-26 15:25:44 +01:00
|
|
|
db.message().resetMessageContent(tmpid);
|
2019-01-04 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-14 07:18:42 +00:00
|
|
|
EntityAttachment.copy(context, message.id, tmpid);
|
2021-04-04 10:06:41 +02:00
|
|
|
|
|
|
|
|
if (message.ui_snoozed != null)
|
|
|
|
|
EntityMessage.snooze(context, tmpid, message.ui_snoozed);
|
2019-01-04 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 08:31:37 +02:00
|
|
|
// Cross account move
|
2020-02-06 20:08:34 +01:00
|
|
|
if (source.account.equals(target.account))
|
|
|
|
|
queue(context, message.account, source.id, message.id, name, jargs);
|
|
|
|
|
else {
|
|
|
|
|
if (message.raw != null && message.raw)
|
|
|
|
|
queue(context, target.account, target.id, message.id, ADD, jargs);
|
|
|
|
|
else
|
|
|
|
|
queue(context, source.account, source.id, message.id, RAW, jargs);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-03 15:25:13 +01:00
|
|
|
return;
|
2020-04-27 17:41:49 +02:00
|
|
|
} else if (DELETE.equals(name)) {
|
2021-02-11 09:51:48 +01:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
boolean perform_expunge = prefs.getBoolean("perform_expunge", true);
|
|
|
|
|
if (perform_expunge)
|
|
|
|
|
db.message().setMessageUiHide(message.id, true);
|
2021-02-11 10:41:07 +01:00
|
|
|
else {
|
|
|
|
|
message.ui_deleted = !message.ui_deleted;
|
|
|
|
|
db.message().setMessageUiDeleted(message.id, message.ui_deleted);
|
|
|
|
|
}
|
2020-04-27 17:41:49 +02:00
|
|
|
/*
|
|
|
|
|
if (message.hash != null) {
|
|
|
|
|
List<EntityMessage> sames = db.message().getMessagesByHash(message.account, message.hash);
|
|
|
|
|
for (EntityMessage same : sames) {
|
|
|
|
|
db.message().setMessageUiHide(same.id, true);
|
|
|
|
|
queue(context, same.account, same.folder, same.id, name, jargs);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
} else if (ATTACHMENT.equals(name))
|
2020-01-04 20:02:29 +01:00
|
|
|
db.attachment().setProgress(jargs.getLong(0), 0);
|
2019-10-05 12:42:04 +02:00
|
|
|
|
2019-11-03 15:25:13 +01:00
|
|
|
queue(context, message.account, message.folder, message.id, name, jargs);
|
|
|
|
|
|
2018-12-10 18:44:45 +01:00
|
|
|
} catch (JSONException ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(ex);
|
2018-12-10 18:44:45 +01:00
|
|
|
}
|
2019-11-03 15:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
2020-11-09 21:29:14 +01:00
|
|
|
static void queue(Context context, EntityFolder folder, String name, Object... values) {
|
|
|
|
|
JSONArray jargs = new JSONArray();
|
|
|
|
|
for (Object value : values)
|
|
|
|
|
jargs.put(value);
|
2019-09-06 16:45:37 +02:00
|
|
|
|
2020-11-09 21:29:14 +01:00
|
|
|
queue(context, folder.account, folder.id, null, name, jargs);
|
2018-08-06 11:05:14 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2020-11-09 21:29:14 +01:00
|
|
|
private static void queue(Context context, Long account, long folder, Long message, String name, JSONArray jargs) {
|
2019-09-17 13:38:23 +02:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
2020-11-09 21:29:14 +01:00
|
|
|
if (FETCH.equals(name)) {
|
|
|
|
|
int count = db.operation().getOperationCount(folder, name);
|
|
|
|
|
if (count >= MAX_FETCH) {
|
|
|
|
|
Log.i("Replacing fetch by sync folder=" + folder + " args=" + jargs + " count=" + count);
|
|
|
|
|
sync(context, folder, false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-17 13:38:23 +02:00
|
|
|
|
|
|
|
|
EntityOperation op = new EntityOperation();
|
2020-11-09 21:29:14 +01:00
|
|
|
op.account = account;
|
|
|
|
|
op.folder = folder;
|
|
|
|
|
op.message = message;
|
2019-09-17 13:38:23 +02:00
|
|
|
op.name = name;
|
|
|
|
|
op.args = jargs.toString();
|
|
|
|
|
op.created = new Date().getTime();
|
|
|
|
|
op.id = db.operation().insertOperation(op);
|
|
|
|
|
|
|
|
|
|
Log.i("Queued op=" + op.id + "/" + op.name +
|
|
|
|
|
" folder=" + op.folder + " msg=" + op.message +
|
|
|
|
|
" args=" + op.args);
|
|
|
|
|
|
|
|
|
|
Map<String, String> crumb = new HashMap<>();
|
|
|
|
|
crumb.put("name", op.name);
|
|
|
|
|
crumb.put("args", op.args);
|
|
|
|
|
crumb.put("folder", op.account + ":" + op.folder);
|
|
|
|
|
if (op.message != null)
|
|
|
|
|
crumb.put("message", Long.toString(op.message));
|
|
|
|
|
crumb.put("free", Integer.toString(Log.getFreeMemMb()));
|
|
|
|
|
Log.breadcrumb("queued", crumb);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-02 07:46:53 +00:00
|
|
|
static void sync(Context context, long fid, boolean foreground) {
|
2020-12-02 16:58:32 +01:00
|
|
|
sync(context, fid, foreground, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void sync(Context context, long fid, boolean foreground, boolean force) {
|
2019-03-02 07:46:53 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
2019-03-05 10:34:00 +00:00
|
|
|
EntityFolder folder = db.folder().getFolder(fid);
|
2019-05-23 19:14:24 +02:00
|
|
|
if (folder == null)
|
|
|
|
|
return;
|
2019-03-02 17:46:49 +00:00
|
|
|
|
2020-12-02 16:58:32 +01:00
|
|
|
if (force)
|
|
|
|
|
db.operation().deleteOperation(fid, SYNC);
|
|
|
|
|
|
2019-12-07 15:14:35 +01:00
|
|
|
// TODO: replace sync parameters?
|
2020-12-02 16:58:32 +01:00
|
|
|
if (db.operation().getOperationCount(fid, SYNC) == 0) {
|
2019-03-02 07:46:53 +00:00
|
|
|
EntityOperation operation = new EntityOperation();
|
2019-03-12 15:54:25 +00:00
|
|
|
operation.account = folder.account;
|
2019-03-02 07:46:53 +00:00
|
|
|
operation.folder = folder.id;
|
|
|
|
|
operation.message = null;
|
|
|
|
|
operation.name = SYNC;
|
2020-12-02 16:58:32 +01:00
|
|
|
operation.args = folder.getSyncArgs(force).toString();
|
2019-03-02 07:46:53 +00:00
|
|
|
operation.created = new Date().getTime();
|
|
|
|
|
operation.id = db.operation().insertOperation(operation);
|
|
|
|
|
|
2019-03-05 10:34:00 +00:00
|
|
|
Log.i("Queued sync folder=" + folder);
|
2019-05-22 08:02:37 +02:00
|
|
|
}
|
2019-03-02 07:46:53 +00:00
|
|
|
|
2020-05-04 16:22:18 +02:00
|
|
|
if (foreground && folder.sync_state == null) // Show spinner
|
2020-01-30 13:23:30 +01:00
|
|
|
db.folder().setFolderSyncState(fid, "requested");
|
|
|
|
|
|
2019-05-23 19:14:24 +02:00
|
|
|
if (folder.account == null) // Outbox
|
2019-03-05 10:34:00 +00:00
|
|
|
ServiceSend.start(context);
|
2019-03-02 07:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-25 18:47:52 +02:00
|
|
|
static void subscribe(Context context, long fid, boolean subscribe) {
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
|
|
EntityFolder folder = db.folder().getFolder(fid);
|
|
|
|
|
|
|
|
|
|
JSONArray jargs = new JSONArray();
|
|
|
|
|
jargs.put(subscribe);
|
|
|
|
|
|
|
|
|
|
EntityOperation operation = new EntityOperation();
|
|
|
|
|
operation.account = folder.account;
|
|
|
|
|
operation.folder = folder.id;
|
|
|
|
|
operation.message = null;
|
|
|
|
|
operation.name = SUBSCRIBE;
|
|
|
|
|
operation.args = jargs.toString();
|
|
|
|
|
operation.created = new Date().getTime();
|
|
|
|
|
operation.id = db.operation().insertOperation(operation);
|
|
|
|
|
|
|
|
|
|
Log.i("Queued subscribe=" + subscribe + " folder=" + folder);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 16:45:30 +01:00
|
|
|
void cleanup(Context context, boolean fetch) {
|
2019-12-07 20:32:58 +01:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
2020-12-08 16:06:52 +01:00
|
|
|
EntityLog.log(context, "Cleanup op=" + id + "/" + name + " folder=" + folder + " message=" + message);
|
|
|
|
|
|
2021-01-31 20:51:05 +01:00
|
|
|
if (message != null) {
|
2019-12-07 20:32:58 +01:00
|
|
|
db.message().setMessageUiHide(message, false);
|
|
|
|
|
|
2021-05-23 08:08:30 +02:00
|
|
|
if (SEEN.equals(name)) {
|
2021-01-31 20:51:05 +01:00
|
|
|
EntityMessage m = db.message().getMessage(message);
|
|
|
|
|
if (m != null)
|
|
|
|
|
db.message().setMessageUiSeen(m.id, m.seen);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-23 08:08:30 +02:00
|
|
|
if (FLAG.equals(name)) {
|
2021-01-31 20:51:05 +01:00
|
|
|
EntityMessage m = db.message().getMessage(message);
|
|
|
|
|
if (m != null)
|
|
|
|
|
db.message().setMessageUiFlagged(m.id, m.flagged, m.color);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-23 08:08:30 +02:00
|
|
|
if (MOVE.equals(name) ||
|
|
|
|
|
ADD.equals(name) ||
|
|
|
|
|
RAW.equals(name))
|
2019-12-07 20:32:58 +01:00
|
|
|
try {
|
|
|
|
|
JSONArray jargs = new JSONArray(args);
|
|
|
|
|
long tmpid = jargs.optLong(2, -1);
|
2020-10-12 18:23:06 +02:00
|
|
|
if (tmpid < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
db.message().deleteMessage(tmpid);
|
2019-12-07 20:32:58 +01:00
|
|
|
} catch (JSONException ex) {
|
|
|
|
|
Log.e(ex);
|
|
|
|
|
}
|
2020-02-08 12:26:49 +01:00
|
|
|
|
2021-05-23 08:08:30 +02:00
|
|
|
if (EXISTS.equals(name)) {
|
2021-05-22 17:07:09 +02:00
|
|
|
EntityMessage m = db.message().getMessage(message);
|
|
|
|
|
if (m != null)
|
2021-05-23 08:08:30 +02:00
|
|
|
queue(context, m, ADD);
|
2021-05-22 17:07:09 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-23 08:08:30 +02:00
|
|
|
if (ATTACHMENT.equals(name))
|
2020-10-12 16:38:24 +02:00
|
|
|
try {
|
|
|
|
|
JSONArray jargs = new JSONArray(args);
|
2021-04-30 19:45:20 +02:00
|
|
|
long id = jargs.getLong(0);
|
|
|
|
|
db.attachment().setProgress(id, null);
|
|
|
|
|
db.attachment().setError(id, error);
|
2020-10-12 16:38:24 +02:00
|
|
|
return;
|
|
|
|
|
} catch (JSONException ex) {
|
|
|
|
|
Log.e(ex);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-23 08:08:30 +02:00
|
|
|
if (SYNC.equals(name))
|
2020-02-08 12:49:12 +01:00
|
|
|
db.folder().setFolderSyncState(folder, null);
|
|
|
|
|
|
2020-11-05 16:45:30 +01:00
|
|
|
if (fetch && message != null) {
|
2020-02-08 12:26:49 +01:00
|
|
|
EntityMessage m = db.message().getMessage(message);
|
|
|
|
|
if (m == null || m.uid == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
EntityFolder f = db.folder().getFolder(folder);
|
|
|
|
|
if (f == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-05-23 08:08:30 +02:00
|
|
|
if (FETCH.equals(name))
|
|
|
|
|
sync(context, f.id, false);
|
2020-12-03 11:32:22 +01:00
|
|
|
else
|
|
|
|
|
queue(context, f, FETCH, m.uid);
|
2020-02-08 12:26:49 +01:00
|
|
|
}
|
2019-12-07 20:32:58 +01:00
|
|
|
}
|
|
|
|
|
|
2018-08-11 08:28:54 +00:00
|
|
|
@Override
|
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
|
if (obj instanceof EntityOperation) {
|
|
|
|
|
EntityOperation other = (EntityOperation) obj;
|
|
|
|
|
return (this.folder.equals(other.folder) &&
|
2019-02-26 11:05:21 +01:00
|
|
|
Objects.equals(this.message, other.message) &&
|
2018-08-11 08:28:54 +00:00
|
|
|
this.name.equals(other.name) &&
|
2018-12-01 14:02:27 +01:00
|
|
|
this.args.equals(other.args) &&
|
2018-12-01 15:13:57 +01:00
|
|
|
this.created.equals(other.created) &&
|
2019-05-11 21:04:27 +02:00
|
|
|
Objects.equals(this.state, other.state) &&
|
2019-02-26 11:05:21 +01:00
|
|
|
Objects.equals(this.error, other.error));
|
2018-08-11 08:28:54 +00:00
|
|
|
} else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-06-26 10:59:32 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return Long.toString(id);
|
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|