Propagate labels

pull/182/head
M66B 4 years ago
parent 1488a37caa
commit 141d57ba18

@ -698,20 +698,25 @@ class Core {
String label = jargs.getString(0);
boolean set = jargs.getBoolean(1);
// Gmail does not push label changes
try {
Message imessage = ifolder.getMessageByUID(message.uid);
if (imessage == null)
throw new MessageRemovedException();
if (!(imessage instanceof GmailMessage))
throw new IllegalArgumentException("GmailMessage");
if (imessage instanceof GmailMessage)
((GmailMessage) imessage).setLabels(new String[]{label}, set);
// Gmail does not push label changes
message.setLabel(label, set);
} catch (MessagingException ex) {
Log.w(ex);
}
DB db = DB.getInstance(context);
db.message().setMessageLabels(message.id, DB.Converters.fromStringArray(message.labels));
List<EntityMessage> messages = db.message().getMessagesByMsgId(message.account, message.msgid);
if (messages != null)
for (EntityMessage m : messages) {
EntityFolder f = db.folder().getFolder(m.folder);
if (!label.equals(f.name) && m.setLabel(label, set)) {
Log.i("Set " + label + "=" + set + " id=" + m.id + " folder=" + f.name);
db.message().setMessageLabels(m.id, DB.Converters.fromStringArray(m.labels));
}
}
}
private static void onAdd(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPStore istore, IMAPFolder ifolder, State state) throws MessagingException, IOException {
@ -1018,13 +1023,39 @@ class Core {
ifolder.fetch(new Message[]{imessage}, fp);
EntityMessage message = synchronizeMessage(context, account, folder, istore, ifolder, imessage, false, download, rules, state);
if (download && message != null)
if (message != null) {
if (account.isGmail() && EntityFolder.USER.equals(folder.type))
try {
JSONArray jlabel = new JSONArray();
jlabel.put(0, folder.name);
jlabel.put(1, true);
onLabel(context, jlabel, folder, message, istore, ifolder, state);
} catch (Throwable ex1) {
Log.e(ex1);
}
if (download)
downloadMessage(context, account, folder, istore, ifolder, imessage, message.id, state);
}
} finally {
((IMAPMessage) imessage).invalidateHeaders();
}
} catch (MessageRemovedException ex) {
Log.i(ex);
if (account.isGmail() && EntityFolder.USER.equals(folder.type)) {
EntityMessage message = db.message().getMessageByUid(folder.id, uid);
if (message != null)
try {
JSONArray jlabel = new JSONArray();
jlabel.put(0, folder.name);
jlabel.put(1, false);
onLabel(context, jlabel, folder, message, istore, ifolder, state);
} catch (Throwable ex1) {
Log.e(ex1);
}
}
db.message().deleteMessage(folder.id, uid);
} finally {
int count = ifolder.getMessageCount();

@ -244,14 +244,28 @@ public class EntityMessage implements Serializable {
return "notification." + sender.getAddress().toLowerCase(Locale.ROOT);
}
void setLabel(String label, boolean set) {
boolean setLabel(String label, boolean set) {
List<String> list = new ArrayList<>();
if (labels != null)
list.addAll(Arrays.asList(labels));
list.remove(label);
if (set)
boolean changed = false;
if (set) {
if (!list.contains(label)) {
changed = true;
list.add(label);
}
} else {
if (list.contains(label)) {
changed = true;
list.remove(label);
}
}
if (changed)
labels = list.toArray(new String[0]);
return changed;
}
static File getFile(Context context, Long id) {

Loading…
Cancel
Save