Ensure avatars are not loaded multiple times

This commit is contained in:
Marvin W 2019-06-11 14:45:26 +02:00
parent 6c480b862e
commit c32990dff1
No known key found for this signature in database
GPG key ID: 072E9235DB996F2A
2 changed files with 11 additions and 1 deletions

View file

@ -23,6 +23,7 @@ public class AvatarManager : StreamInteractionModule, Object {
private HashMap<Jid, string> vcard_avatars = new HashMap<Jid, string>(Jid.hash_func, Jid.equals_func); private HashMap<Jid, string> vcard_avatars = new HashMap<Jid, string>(Jid.hash_func, Jid.equals_func);
private AvatarStorage avatar_storage = new AvatarStorage(get_storage_dir()); private AvatarStorage avatar_storage = new AvatarStorage(get_storage_dir());
private HashMap<string, Pixbuf> cached_pixbuf = new HashMap<string, Pixbuf>(); private HashMap<string, Pixbuf> cached_pixbuf = new HashMap<string, Pixbuf>();
private HashMap<string, Gee.List<SourceFuncWrapper>> pending_pixbuf = new HashMap<string, Gee.List<SourceFuncWrapper>>();
private const int MAX_PIXEL = 192; private const int MAX_PIXEL = 192;
public static void start(StreamInteractor stream_interactor, Database db) { public static void start(StreamInteractor stream_interactor, Database db) {
@ -50,12 +51,21 @@ public class AvatarManager : StreamInteractionModule, Object {
if (cached_pixbuf.has_key(hash)) { if (cached_pixbuf.has_key(hash)) {
return cached_pixbuf[hash]; return cached_pixbuf[hash];
} }
if (pending_pixbuf.has_key(hash)) {
pending_pixbuf[hash].add(new SourceFuncWrapper(get_avatar_by_hash.callback));
yield;
return cached_pixbuf[hash];
}
pending_pixbuf[hash] = new ArrayList<SourceFuncWrapper>();
Pixbuf? image = yield avatar_storage.get_image(hash); Pixbuf? image = yield avatar_storage.get_image(hash);
if (image != null) { if (image != null) {
cached_pixbuf[hash] = image; cached_pixbuf[hash] = image;
} else { } else {
db.avatar.delete().with(db.avatar.hash, "=", hash).perform(); db.avatar.delete().with(db.avatar.hash, "=", hash).perform();
} }
foreach (SourceFuncWrapper sfw in pending_pixbuf[hash]) {
sfw.sfun();
}
return image; return image;
} }

View file

@ -33,7 +33,7 @@ public class AvatarStorage : Xep.PixbufStorage, Object {
File file = File.new_for_path(Path.build_filename(folder, id)); File file = File.new_for_path(Path.build_filename(folder, id));
FileInputStream stream = yield file.read_async(); FileInputStream stream = yield file.read_async();
uint8 fbuf[100]; uint8 fbuf[1024];
size_t size; size_t size;
Checksum checksum = new Checksum (ChecksumType.SHA1); Checksum checksum = new Checksum (ChecksumType.SHA1);