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":"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", "name":"org.whispersystems.signalservice.internal.push.SignalServiceProtos$Envelope",
"fields":[ "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.TypingAction;
import org.asamk.signal.manager.api.UnregisteredRecipientException; import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.manager.api.UpdateGroup; 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.api.UserStatus;
import org.asamk.signal.manager.groups.GroupId; import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupInviteLinkUrl; import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
@ -67,15 +68,10 @@ public interface Manager extends Closeable {
void updateConfiguration(Configuration configuration) throws IOException, NotMasterDeviceException; void updateConfiguration(Configuration configuration) throws IOException, NotMasterDeviceException;
/** /**
* @param givenName if null, the previous givenName will be kept * Update the user's profile.
* @param familyName if null, the previous familyName will be kept * If a field is null, the previous value 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),
*/ */
void setProfile( void updateProfile(UpdateProfile updateProfile) throws IOException;
String givenName, String familyName, String about, String aboutEmoji, Optional<File> avatar
) throws IOException;
void unregister() 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.TypingAction;
import org.asamk.signal.manager.api.UnregisteredRecipientException; import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.manager.api.UpdateGroup; 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.api.UserStatus;
import org.asamk.signal.manager.config.ServiceEnvironmentConfig; import org.asamk.signal.manager.config.ServiceEnvironmentConfig;
import org.asamk.signal.manager.groups.GroupId; import org.asamk.signal.manager.groups.GroupId;
@ -261,10 +262,15 @@ class ManagerImpl implements Manager {
} }
@Override @Override
public void setProfile( public void updateProfile(UpdateProfile updateProfile) throws IOException {
String givenName, final String familyName, String about, String aboutEmoji, Optional<File> avatar context.getProfileHelper()
) throws IOException { .setProfile(updateProfile.getGivenName(),
context.getProfileHelper().setProfile(givenName, familyName, about, aboutEmoji, avatar); updateProfile.getFamilyName(),
updateProfile.getAbout(),
updateProfile.getAboutEmoji(),
updateProfile.isDeleteAvatar()
? Optional.empty()
: updateProfile.getAvatar() == null ? null : Optional.of(updateProfile.getAvatar()));
context.getSyncHelper().sendSyncFetchProfileMessage(); 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.CaptchaRequiredException;
import org.asamk.signal.manager.api.IncorrectPinException; import org.asamk.signal.manager.api.IncorrectPinException;
import org.asamk.signal.manager.api.PinLockedException; 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.ServiceConfig;
import org.asamk.signal.manager.config.ServiceEnvironmentConfig; import org.asamk.signal.manager.config.ServiceEnvironmentConfig;
import org.asamk.signal.manager.helper.AccountFileUpdater; 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 // Set an initial empty profile so user can be added to groups
try { try {
m.setProfile(null, null, null, null, null); m.updateProfile(UpdateProfile.newBuilder().build());
} catch (NoClassDefFoundError e) { } catch (NoClassDefFoundError e) {
logger.warn("Failed to set default profile: {}", e.getMessage()); 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.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.UpdateProfile;
import org.asamk.signal.output.OutputWriter; import org.asamk.signal.output.OutputWriter;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Optional;
public class UpdateProfileCommand implements JsonRpcLocalCommand { public class UpdateProfileCommand implements JsonRpcLocalCommand {
@ -44,12 +44,17 @@ public class UpdateProfileCommand implements JsonRpcLocalCommand {
var avatarPath = ns.getString("avatar"); var avatarPath = ns.getString("avatar");
boolean removeAvatar = Boolean.TRUE.equals(ns.getBoolean("remove-avatar")); boolean removeAvatar = Boolean.TRUE.equals(ns.getBoolean("remove-avatar"));
Optional<File> avatarFile = removeAvatar File avatarFile = removeAvatar || avatarPath == null ? null : new File(avatarPath);
? Optional.empty()
: avatarPath == null ? null : Optional.of(new File(avatarPath));
try { 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) { } catch (IOException e) {
throw new IOErrorException("Update profile error: " + e.getMessage(), 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.StickerPackUrl;
import org.asamk.signal.manager.api.TypingAction; import org.asamk.signal.manager.api.TypingAction;
import org.asamk.signal.manager.api.UpdateGroup; 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.api.UserStatus;
import org.asamk.signal.manager.groups.GroupId; import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupInviteLinkUrl; import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
@ -138,19 +139,13 @@ public class DbusManagerImpl implements Manager {
} }
@Override @Override
public void setProfile( public void updateProfile(UpdateProfile updateProfile) throws IOException {
final String givenName, signal.updateProfile(emptyIfNull(updateProfile.getGivenName()),
final String familyName, emptyIfNull(updateProfile.getFamilyName()),
final String about, emptyIfNull(updateProfile.getAbout()),
final String aboutEmoji, emptyIfNull(updateProfile.getAboutEmoji()),
final Optional<File> avatar updateProfile.getAvatar() == null ? "" : updateProfile.getAvatar().getPath(),
) throws IOException { updateProfile.isDeleteAvatar());
signal.updateProfile(emptyIfNull(givenName),
emptyIfNull(familyName),
emptyIfNull(about),
emptyIfNull(aboutEmoji),
avatar == null ? "" : avatar.map(File::getPath).orElse(""),
avatar != null && avatar.isEmpty());
} }
@Override @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.TypingAction;
import org.asamk.signal.manager.api.UnregisteredRecipientException; import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.manager.api.UpdateGroup; 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.api.UserStatus;
import org.asamk.signal.manager.groups.GroupId; import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupInviteLinkUrl; import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
@ -662,10 +663,15 @@ public class DbusSignalImpl implements Signal {
about = nullIfEmpty(about); about = nullIfEmpty(about);
aboutEmoji = nullIfEmpty(aboutEmoji); aboutEmoji = nullIfEmpty(aboutEmoji);
avatarPath = nullIfEmpty(avatarPath); avatarPath = nullIfEmpty(avatarPath);
Optional<File> avatarFile = removeAvatar File avatarFile = removeAvatar || avatarPath == null ? null : new File(avatarPath);
? Optional.empty() m.updateProfile(UpdateProfile.newBuilder()
: avatarPath == null ? null : Optional.of(new File(avatarPath)); .withGivenName(givenName)
m.setProfile(givenName, familyName, about, aboutEmoji, avatarFile); .withFamilyName(familyName)
.withAbout(about)
.withAboutEmoji(aboutEmoji)
.withAvatar(avatarFile)
.withDeleteAvatar(removeAvatar)
.build());
} catch (IOException e) { } catch (IOException e) {
throw new Error.Failure(e.getMessage()); throw new Error.Failure(e.getMessage());
} }