Add/update contacts in transaction

pull/156/head
M66B 6 years ago
parent be8c5a99e3
commit 102838c007

@ -1484,10 +1484,10 @@ class Core {
} }
} }
private static void updateContactInfo(Context context, EntityFolder folder, EntityMessage message) { private static void updateContactInfo(Context context, final EntityFolder folder, final EntityMessage message) {
DB db = DB.getInstance(context); final DB db = DB.getInstance(context);
int type = (folder.isOutgoing() ? EntityContact.TYPE_TO : EntityContact.TYPE_FROM); final int type = (folder.isOutgoing() ? EntityContact.TYPE_TO : EntityContact.TYPE_FROM);
Address[] recipients = (type == EntityContact.TYPE_TO Address[] recipients = (type == EntityContact.TYPE_TO
? message.to ? message.to
: (message.reply != null ? message.reply : message.from)); : (message.reply != null ? message.reply : message.from));
@ -1512,31 +1512,36 @@ class Core {
if (recipients != null) { if (recipients != null) {
for (Address recipient : recipients) { for (Address recipient : recipients) {
String email = ((InternetAddress) recipient).getAddress(); final String email = ((InternetAddress) recipient).getAddress();
String name = ((InternetAddress) recipient).getPersonal(); final String name = ((InternetAddress) recipient).getPersonal();
Uri avatar = ContactInfo.getLookupUri(context, new Address[]{recipient}); final Uri avatar = ContactInfo.getLookupUri(context, new Address[]{recipient});
EntityContact contact = db.contact().getContact(folder.account, type, email); db.runInTransaction(new Runnable() {
if (contact == null) { @Override
contact = new EntityContact(); public void run() {
contact.account = folder.account; EntityContact contact = db.contact().getContact(folder.account, type, email);
contact.type = type; if (contact == null) {
contact.email = email; contact = new EntityContact();
contact.name = name; contact.account = folder.account;
contact.avatar = (avatar == null ? null : avatar.toString()); contact.type = type;
contact.times_contacted = 1; contact.email = email;
contact.first_contacted = message.received; contact.name = name;
contact.last_contacted = message.received; contact.avatar = (avatar == null ? null : avatar.toString());
contact.id = db.contact().insertContact(contact); contact.times_contacted = 1;
Log.i("Inserted contact=" + contact + " type=" + type); contact.first_contacted = message.received;
} else { contact.last_contacted = message.received;
contact.name = name; contact.id = db.contact().insertContact(contact);
contact.avatar = (avatar == null ? null : avatar.toString()); Log.i("Inserted contact=" + contact + " type=" + type);
contact.times_contacted++; } else {
contact.first_contacted = Math.min(contact.first_contacted, message.received); contact.name = name;
contact.last_contacted = message.received; contact.avatar = (avatar == null ? null : avatar.toString());
db.contact().updateContact(contact); contact.times_contacted++;
Log.i("Updated contact=" + contact + " type=" + type); contact.first_contacted = Math.min(contact.first_contacted, message.received);
} contact.last_contacted = message.received;
db.contact().updateContact(contact);
Log.i("Updated contact=" + contact + " type=" + type);
}
}
});
} }
} }
} }

Loading…
Cancel
Save