Cache profiles for 24h before retrieving them again

This commit is contained in:
AsamK 2020-09-10 14:20:16 +02:00
parent 0f3aa22519
commit b94c1e50e6
6 changed files with 168 additions and 4 deletions

View file

@ -22,6 +22,8 @@ import org.asamk.signal.storage.SignalAccount;
import org.asamk.signal.storage.contacts.ContactInfo;
import org.asamk.signal.storage.groups.GroupInfo;
import org.asamk.signal.storage.groups.JsonGroupStore;
import org.asamk.signal.storage.profiles.SignalProfile;
import org.asamk.signal.storage.profiles.SignalProfileEntry;
import org.asamk.signal.storage.protocol.JsonIdentityKeyStore;
import org.asamk.signal.util.IOUtils;
import org.asamk.signal.util.Util;
@ -442,6 +444,18 @@ public class Manager implements Closeable {
}
private SignalProfile getRecipientProfile(SignalServiceAddress address, Optional<UnidentifiedAccess> unidentifiedAccess, ProfileKey profileKey) throws IOException {
SignalProfileEntry profileEntry = account.getProfileStore().getProfile(address);
long now = new Date().getTime();
// Profiles are cache for 24h before retrieving them again
if (profileEntry == null || profileEntry.getProfile() == null || now - profileEntry.getLastUpdateTimestamp() > 24 * 60 * 60 * 1000) {
SignalProfile profile = retrieveRecipientProfile(address, unidentifiedAccess, profileKey);
profileEntry = new SignalProfileEntry(profileKey, now, profile);
account.getProfileStore().updateProfile(address, profileEntry);
}
return profileEntry.getProfile();
}
private SignalProfile retrieveRecipientProfile(SignalServiceAddress address, Optional<UnidentifiedAccess> unidentifiedAccess, ProfileKey profileKey) throws IOException {
final SignalServiceProfile encryptedProfile = getEncryptedRecipientProfile(address, unidentifiedAccess);
File avatarFile = null;

View file

@ -1,65 +0,0 @@
package org.asamk.signal.manager;
import org.whispersystems.signalservice.api.profiles.SignalServiceProfile;
import java.io.File;
public class SignalProfile {
private final String identityKey;
private final String name;
private final File avatarFile;
private final String unidentifiedAccess;
private final boolean unrestrictedUnidentifiedAccess;
private final SignalServiceProfile.Capabilities capabilities;
public SignalProfile(final String identityKey, final String name, final File avatarFile, final String unidentifiedAccess, final boolean unrestrictedUnidentifiedAccess, final SignalServiceProfile.Capabilities capabilities) {
this.identityKey = identityKey;
this.name = name;
this.avatarFile = avatarFile;
this.unidentifiedAccess = unidentifiedAccess;
this.unrestrictedUnidentifiedAccess = unrestrictedUnidentifiedAccess;
this.capabilities = capabilities;
}
public String getIdentityKey() {
return identityKey;
}
public String getName() {
return name;
}
public File getAvatarFile() {
return avatarFile;
}
public String getUnidentifiedAccess() {
return unidentifiedAccess;
}
public boolean isUnrestrictedUnidentifiedAccess() {
return unrestrictedUnidentifiedAccess;
}
public SignalServiceProfile.Capabilities getCapabilities() {
return capabilities;
}
@Override
public String toString() {
return "SignalProfile{" +
"identityKey='" + identityKey + '\'' +
", name='" + name + '\'' +
", avatarFile=" + avatarFile +
", unidentifiedAccess='" + unidentifiedAccess + '\'' +
", unrestrictedUnidentifiedAccess=" + unrestrictedUnidentifiedAccess +
", capabilities=" + capabilities +
'}';
}
}