Refactor manager update profile method

This commit is contained in:
AsamK 2022-05-21 09:29:58 +02:00
parent 7587a60387
commit 3666531f8b
8 changed files with 160 additions and 35 deletions

View file

@ -2754,6 +2754,14 @@
{"name":"stickerId_"}
]
},
{
"name":"org.whispersystems.signalservice.internal.push.SignalServiceProtos$DataMessage$StoryContext",
"fields":[
{"name":"authorUuid_"},
{"name":"bitField0_"},
{"name":"sentTimestamp_"}
]
},
{
"name":"org.whispersystems.signalservice.internal.push.SignalServiceProtos$Envelope",
"fields":[

View file

@ -21,6 +21,7 @@ import org.asamk.signal.manager.api.StickerPackUrl;
import org.asamk.signal.manager.api.TypingAction;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.manager.api.UpdateGroup;
import org.asamk.signal.manager.api.UpdateProfile;
import org.asamk.signal.manager.api.UserStatus;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
@ -67,15 +68,10 @@ public interface Manager extends Closeable {
void updateConfiguration(Configuration configuration) throws IOException, NotMasterDeviceException;
/**
* @param givenName if null, the previous givenName will be kept
* @param familyName if null, the previous familyName will be kept
* @param about if null, the previous about text will be kept
* @param aboutEmoji if null, the previous about emoji will be kept
* @param avatar if avatar is null the image from the local avatar store is used (if present),
* Update the user's profile.
* If a field is null, the previous value will be kept.
*/
void setProfile(
String givenName, String familyName, String about, String aboutEmoji, Optional<File> avatar
) throws IOException;
void updateProfile(UpdateProfile updateProfile) throws IOException;
void unregister() throws IOException;

View file

@ -38,6 +38,7 @@ import org.asamk.signal.manager.api.StickerPackUrl;
import org.asamk.signal.manager.api.TypingAction;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.manager.api.UpdateGroup;
import org.asamk.signal.manager.api.UpdateProfile;
import org.asamk.signal.manager.api.UserStatus;
import org.asamk.signal.manager.config.ServiceEnvironmentConfig;
import org.asamk.signal.manager.groups.GroupId;
@ -261,10 +262,15 @@ class ManagerImpl implements Manager {
}
@Override
public void setProfile(
String givenName, final String familyName, String about, String aboutEmoji, Optional<File> avatar
) throws IOException {
context.getProfileHelper().setProfile(givenName, familyName, about, aboutEmoji, avatar);
public void updateProfile(UpdateProfile updateProfile) throws IOException {
context.getProfileHelper()
.setProfile(updateProfile.getGivenName(),
updateProfile.getFamilyName(),
updateProfile.getAbout(),
updateProfile.getAboutEmoji(),
updateProfile.isDeleteAvatar()
? Optional.empty()
: updateProfile.getAvatar() == null ? null : Optional.of(updateProfile.getAvatar()));
context.getSyncHelper().sendSyncFetchProfileMessage();
}

View file

@ -19,6 +19,7 @@ package org.asamk.signal.manager;
import org.asamk.signal.manager.api.CaptchaRequiredException;
import org.asamk.signal.manager.api.IncorrectPinException;
import org.asamk.signal.manager.api.PinLockedException;
import org.asamk.signal.manager.api.UpdateProfile;
import org.asamk.signal.manager.config.ServiceConfig;
import org.asamk.signal.manager.config.ServiceEnvironmentConfig;
import org.asamk.signal.manager.helper.AccountFileUpdater;
@ -139,7 +140,7 @@ class RegistrationManagerImpl implements RegistrationManager {
}
// Set an initial empty profile so user can be added to groups
try {
m.setProfile(null, null, null, null, null);
m.updateProfile(UpdateProfile.newBuilder().build());
} catch (NoClassDefFoundError e) {
logger.warn("Failed to set default profile: {}", e.getMessage());
}

View file

@ -0,0 +1,108 @@
package org.asamk.signal.manager.api;
import java.io.File;
public class UpdateProfile {
private final String givenName;
private final String familyName;
private final String about;
private final String aboutEmoji;
private final File avatar;
private final boolean deleteAvatar;
private UpdateProfile(final Builder builder) {
givenName = builder.givenName;
familyName = builder.familyName;
about = builder.about;
aboutEmoji = builder.aboutEmoji;
avatar = builder.avatar;
deleteAvatar = builder.deleteAvatar;
}
public static Builder newBuilder() {
return new Builder();
}
public static Builder newBuilder(final UpdateProfile copy) {
Builder builder = new Builder();
builder.givenName = copy.getGivenName();
builder.familyName = copy.getFamilyName();
builder.about = copy.getAbout();
builder.aboutEmoji = copy.getAboutEmoji();
builder.avatar = copy.getAvatar();
builder.deleteAvatar = copy.isDeleteAvatar();
return builder;
}
public String getGivenName() {
return givenName;
}
public String getFamilyName() {
return familyName;
}
public String getAbout() {
return about;
}
public String getAboutEmoji() {
return aboutEmoji;
}
public File getAvatar() {
return avatar;
}
public boolean isDeleteAvatar() {
return deleteAvatar;
}
public static final class Builder {
private String givenName;
private String familyName;
private String about;
private String aboutEmoji;
private File avatar;
private boolean deleteAvatar;
private Builder() {
}
public Builder withGivenName(final String val) {
givenName = val;
return this;
}
public Builder withFamilyName(final String val) {
familyName = val;
return this;
}
public Builder withAbout(final String val) {
about = val;
return this;
}
public Builder withAboutEmoji(final String val) {
aboutEmoji = val;
return this;
}
public Builder withAvatar(final File val) {
avatar = val;
return this;
}
public Builder withDeleteAvatar(final boolean val) {
deleteAvatar = val;
return this;
}
public UpdateProfile build() {
return new UpdateProfile(this);
}
}
}

View file

@ -7,11 +7,11 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.UpdateProfile;
import org.asamk.signal.output.OutputWriter;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
public class UpdateProfileCommand implements JsonRpcLocalCommand {
@ -44,12 +44,17 @@ public class UpdateProfileCommand implements JsonRpcLocalCommand {
var avatarPath = ns.getString("avatar");
boolean removeAvatar = Boolean.TRUE.equals(ns.getBoolean("remove-avatar"));
Optional<File> avatarFile = removeAvatar
? Optional.empty()
: avatarPath == null ? null : Optional.of(new File(avatarPath));
File avatarFile = removeAvatar || avatarPath == null ? null : new File(avatarPath);
try {
m.setProfile(givenName, familyName, about, aboutEmoji, avatarFile);
m.updateProfile(UpdateProfile.newBuilder()
.withGivenName(givenName)
.withFamilyName(familyName)
.withAbout(about)
.withAboutEmoji(aboutEmoji)
.withAvatar(avatarFile)
.withDeleteAvatar(removeAvatar)
.build());
} catch (IOException e) {
throw new IOErrorException("Update profile error: " + e.getMessage(), e);
}

View file

@ -22,6 +22,7 @@ import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackUrl;
import org.asamk.signal.manager.api.TypingAction;
import org.asamk.signal.manager.api.UpdateGroup;
import org.asamk.signal.manager.api.UpdateProfile;
import org.asamk.signal.manager.api.UserStatus;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
@ -138,19 +139,13 @@ public class DbusManagerImpl implements Manager {
}
@Override
public void setProfile(
final String givenName,
final String familyName,
final String about,
final String aboutEmoji,
final Optional<File> avatar
) throws IOException {
signal.updateProfile(emptyIfNull(givenName),
emptyIfNull(familyName),
emptyIfNull(about),
emptyIfNull(aboutEmoji),
avatar == null ? "" : avatar.map(File::getPath).orElse(""),
avatar != null && avatar.isEmpty());
public void updateProfile(UpdateProfile updateProfile) throws IOException {
signal.updateProfile(emptyIfNull(updateProfile.getGivenName()),
emptyIfNull(updateProfile.getFamilyName()),
emptyIfNull(updateProfile.getAbout()),
emptyIfNull(updateProfile.getAboutEmoji()),
updateProfile.getAvatar() == null ? "" : updateProfile.getAvatar().getPath(),
updateProfile.isDeleteAvatar());
}
@Override

View file

@ -17,6 +17,7 @@ import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.manager.api.TypingAction;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.manager.api.UpdateGroup;
import org.asamk.signal.manager.api.UpdateProfile;
import org.asamk.signal.manager.api.UserStatus;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
@ -662,10 +663,15 @@ public class DbusSignalImpl implements Signal {
about = nullIfEmpty(about);
aboutEmoji = nullIfEmpty(aboutEmoji);
avatarPath = nullIfEmpty(avatarPath);
Optional<File> avatarFile = removeAvatar
? Optional.empty()
: avatarPath == null ? null : Optional.of(new File(avatarPath));
m.setProfile(givenName, familyName, about, aboutEmoji, avatarFile);
File avatarFile = removeAvatar || avatarPath == null ? null : new File(avatarPath);
m.updateProfile(UpdateProfile.newBuilder()
.withGivenName(givenName)
.withFamilyName(familyName)
.withAbout(about)
.withAboutEmoji(aboutEmoji)
.withAvatar(avatarFile)
.withDeleteAvatar(removeAvatar)
.build());
} catch (IOException e) {
throw new Error.Failure(e.getMessage());
}