mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Split createGroup out of updateGroup method
This commit is contained in:
parent
dd0effc10c
commit
4ebacd0e1f
3 changed files with 133 additions and 108 deletions
|
@ -782,34 +782,34 @@ public class Manager implements Closeable {
|
||||||
return sendMessage(messageBuilder, g.getMembersWithout(account.getSelfRecipientId()));
|
return sendMessage(messageBuilder, g.getMembersWithout(account.getSelfRecipientId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pair<GroupId, List<SendMessageResult>> updateGroup(
|
public Pair<GroupId, List<SendMessageResult>> createGroup(
|
||||||
GroupId groupId, String name, String description, List<String> members, File avatarFile
|
String name, List<String> members, File avatarFile
|
||||||
) throws IOException, GroupNotFoundException, AttachmentInvalidException, InvalidNumberException, NotAGroupMemberException {
|
) throws IOException, AttachmentInvalidException, InvalidNumberException {
|
||||||
final var membersRecipientIds = members == null ? null : getSignalServiceAddresses(members);
|
return createGroup(name, members == null ? null : getSignalServiceAddresses(members), avatarFile);
|
||||||
if (membersRecipientIds != null) {
|
}
|
||||||
membersRecipientIds.remove(account.getSelfRecipientId());
|
|
||||||
}
|
private Pair<GroupId, List<SendMessageResult>> createGroup(
|
||||||
return sendUpdateGroupMessage(groupId, name, description, membersRecipientIds, avatarFile);
|
String name, Set<RecipientId> members, File avatarFile
|
||||||
|
) throws IOException, AttachmentInvalidException {
|
||||||
|
final var selfRecipientId = account.getSelfRecipientId();
|
||||||
|
if (members != null && members.contains(selfRecipientId)) {
|
||||||
|
members = new HashSet<>(members);
|
||||||
|
members.remove(selfRecipientId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Pair<GroupId, List<SendMessageResult>> sendUpdateGroupMessage(
|
|
||||||
GroupId groupId, String name, String description, Set<RecipientId> members, File avatarFile
|
|
||||||
) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException {
|
|
||||||
GroupInfo g;
|
|
||||||
SignalServiceDataMessage.Builder messageBuilder;
|
|
||||||
if (groupId == null) {
|
|
||||||
// Create new group
|
|
||||||
var gv2Pair = groupHelper.createGroupV2(name == null ? "" : name,
|
var gv2Pair = groupHelper.createGroupV2(name == null ? "" : name,
|
||||||
members == null ? Set.of() : members,
|
members == null ? Set.of() : members,
|
||||||
avatarFile);
|
avatarFile);
|
||||||
|
|
||||||
|
SignalServiceDataMessage.Builder messageBuilder;
|
||||||
if (gv2Pair == null) {
|
if (gv2Pair == null) {
|
||||||
|
// Failed to create v2 group, creating v1 group instead
|
||||||
var gv1 = new GroupInfoV1(GroupIdV1.createRandom());
|
var gv1 = new GroupInfoV1(GroupIdV1.createRandom());
|
||||||
gv1.addMembers(List.of(account.getSelfRecipientId()));
|
gv1.addMembers(List.of(selfRecipientId));
|
||||||
updateGroupV1(gv1, name, members, avatarFile);
|
final var result = updateGroupV1(gv1, name, members, avatarFile);
|
||||||
messageBuilder = getGroupUpdateMessageBuilder(gv1);
|
return new Pair<>(gv1.getGroupId(), result.second());
|
||||||
g = gv1;
|
}
|
||||||
} else {
|
|
||||||
// TODO set description as well
|
|
||||||
final var gv2 = gv2Pair.first();
|
final var gv2 = gv2Pair.first();
|
||||||
final var decryptedGroup = gv2Pair.second();
|
final var decryptedGroup = gv2Pair.second();
|
||||||
|
|
||||||
|
@ -819,59 +819,46 @@ public class Manager implements Closeable {
|
||||||
outputStream -> IOUtils.copyFileToStream(avatarFile, outputStream));
|
outputStream -> IOUtils.copyFileToStream(avatarFile, outputStream));
|
||||||
}
|
}
|
||||||
messageBuilder = getGroupUpdateMessageBuilder(gv2, null);
|
messageBuilder = getGroupUpdateMessageBuilder(gv2, null);
|
||||||
g = gv2;
|
account.getGroupStore().updateGroup(gv2);
|
||||||
|
|
||||||
|
final var result = sendMessage(messageBuilder, gv2.getMembersIncludingPendingWithout(selfRecipientId));
|
||||||
|
return new Pair<>(gv2.getGroupId(), result.second());
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
|
public Pair<Long, List<SendMessageResult>> updateGroup(
|
||||||
|
GroupId groupId, String name, String description, List<String> members, File avatarFile
|
||||||
|
) throws IOException, GroupNotFoundException, AttachmentInvalidException, InvalidNumberException, NotAGroupMemberException {
|
||||||
|
return updateGroup(groupId,
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
members == null ? null : getSignalServiceAddresses(members),
|
||||||
|
avatarFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Pair<Long, List<SendMessageResult>> updateGroup(
|
||||||
|
GroupId groupId, String name, String description, Set<RecipientId> members, File avatarFile
|
||||||
|
) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException {
|
||||||
var group = getGroupForUpdating(groupId);
|
var group = getGroupForUpdating(groupId);
|
||||||
|
|
||||||
if (group instanceof GroupInfoV2) {
|
if (group instanceof GroupInfoV2) {
|
||||||
final var groupInfoV2 = (GroupInfoV2) group;
|
return updateGroupV2((GroupInfoV2) group, name, description, members, avatarFile);
|
||||||
|
|
||||||
Pair<Long, List<SendMessageResult>> result = null;
|
|
||||||
if (groupInfoV2.isPendingMember(account.getSelfRecipientId())) {
|
|
||||||
var groupGroupChangePair = groupHelper.acceptInvite(groupInfoV2);
|
|
||||||
result = sendUpdateGroupMessage(groupInfoV2,
|
|
||||||
groupGroupChangePair.first(),
|
|
||||||
groupGroupChangePair.second());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (members != null) {
|
return updateGroupV1((GroupInfoV1) group, name, members, avatarFile);
|
||||||
final var newMembers = new HashSet<>(members);
|
|
||||||
newMembers.removeAll(group.getMembers());
|
|
||||||
if (newMembers.size() > 0) {
|
|
||||||
var groupGroupChangePair = groupHelper.updateGroupV2(groupInfoV2, newMembers);
|
|
||||||
result = sendUpdateGroupMessage(groupInfoV2,
|
|
||||||
groupGroupChangePair.first(),
|
|
||||||
groupGroupChangePair.second());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (result == null || name != null || description != null || avatarFile != null) {
|
|
||||||
var groupGroupChangePair = groupHelper.updateGroupV2(groupInfoV2, name, description, avatarFile);
|
|
||||||
if (avatarFile != null) {
|
|
||||||
avatarStore.storeGroupAvatar(groupInfoV2.getGroupId(),
|
|
||||||
outputStream -> IOUtils.copyFileToStream(avatarFile, outputStream));
|
|
||||||
}
|
|
||||||
result = sendUpdateGroupMessage(groupInfoV2,
|
|
||||||
groupGroupChangePair.first(),
|
|
||||||
groupGroupChangePair.second());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Pair<>(group.getGroupId(), result.second());
|
private Pair<Long, List<SendMessageResult>> updateGroupV1(
|
||||||
} else {
|
final GroupInfoV1 gv1, final String name, final Set<RecipientId> members, final File avatarFile
|
||||||
var gv1 = (GroupInfoV1) group;
|
) throws IOException, AttachmentInvalidException {
|
||||||
updateGroupV1(gv1, name, members, avatarFile);
|
updateGroupV1Details(gv1, name, members, avatarFile);
|
||||||
messageBuilder = getGroupUpdateMessageBuilder(gv1);
|
var messageBuilder = getGroupUpdateMessageBuilder(gv1);
|
||||||
g = gv1;
|
|
||||||
}
|
account.getGroupStore().updateGroup(gv1);
|
||||||
|
|
||||||
|
return sendMessage(messageBuilder, gv1.getMembersIncludingPendingWithout(account.getSelfRecipientId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
account.getGroupStore().updateGroup(g);
|
private void updateGroupV1Details(
|
||||||
|
|
||||||
final var result = sendMessage(messageBuilder,
|
|
||||||
g.getMembersIncludingPendingWithout(account.getSelfRecipientId()));
|
|
||||||
return new Pair<>(g.getGroupId(), result.second());
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateGroupV1(
|
|
||||||
final GroupInfoV1 g, final String name, final Collection<RecipientId> members, final File avatarFile
|
final GroupInfoV1 g, final String name, final Collection<RecipientId> members, final File avatarFile
|
||||||
) throws IOException {
|
) throws IOException {
|
||||||
if (name != null) {
|
if (name != null) {
|
||||||
|
@ -909,13 +896,40 @@ public class Manager implements Closeable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pair<GroupId, List<SendMessageResult>> joinGroup(
|
private Pair<Long, List<SendMessageResult>> updateGroupV2(
|
||||||
GroupInviteLinkUrl inviteLinkUrl
|
final GroupInfoV2 group,
|
||||||
) throws IOException, GroupLinkNotActiveException {
|
final String name,
|
||||||
return sendJoinGroupMessage(inviteLinkUrl);
|
final String description,
|
||||||
|
final Set<RecipientId> members,
|
||||||
|
final File avatarFile
|
||||||
|
) throws IOException {
|
||||||
|
Pair<Long, List<SendMessageResult>> result = null;
|
||||||
|
if (group.isPendingMember(account.getSelfRecipientId())) {
|
||||||
|
var groupGroupChangePair = groupHelper.acceptInvite(group);
|
||||||
|
result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Pair<GroupId, List<SendMessageResult>> sendJoinGroupMessage(
|
if (members != null) {
|
||||||
|
final var newMembers = new HashSet<>(members);
|
||||||
|
newMembers.removeAll(group.getMembers());
|
||||||
|
if (newMembers.size() > 0) {
|
||||||
|
var groupGroupChangePair = groupHelper.updateGroupV2(group, newMembers);
|
||||||
|
result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (result == null || name != null || description != null || avatarFile != null) {
|
||||||
|
var groupGroupChangePair = groupHelper.updateGroupV2(group, name, description, avatarFile);
|
||||||
|
if (avatarFile != null) {
|
||||||
|
avatarStore.storeGroupAvatar(group.getGroupId(),
|
||||||
|
outputStream -> IOUtils.copyFileToStream(avatarFile, outputStream));
|
||||||
|
}
|
||||||
|
result = sendUpdateGroupV2Message(group, groupGroupChangePair.first(), groupGroupChangePair.second());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pair<GroupId, List<SendMessageResult>> joinGroup(
|
||||||
GroupInviteLinkUrl inviteLinkUrl
|
GroupInviteLinkUrl inviteLinkUrl
|
||||||
) throws IOException, GroupLinkNotActiveException {
|
) throws IOException, GroupLinkNotActiveException {
|
||||||
final var groupJoinInfo = groupHelper.getDecryptedGroupJoinInfo(inviteLinkUrl.getGroupMasterKey(),
|
final var groupJoinInfo = groupHelper.getDecryptedGroupJoinInfo(inviteLinkUrl.getGroupMasterKey(),
|
||||||
|
@ -932,11 +946,20 @@ public class Manager implements Closeable {
|
||||||
return new Pair<>(group.getGroupId(), List.of());
|
return new Pair<>(group.getGroupId(), List.of());
|
||||||
}
|
}
|
||||||
|
|
||||||
final var result = sendUpdateGroupMessage(group, group.getGroup(), groupChange);
|
final var result = sendUpdateGroupV2Message(group, group.getGroup(), groupChange);
|
||||||
|
|
||||||
return new Pair<>(group.getGroupId(), result.second());
|
return new Pair<>(group.getGroupId(), result.second());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Pair<Long, List<SendMessageResult>> sendUpdateGroupV2Message(
|
||||||
|
GroupInfoV2 group, DecryptedGroup newDecryptedGroup, GroupChange groupChange
|
||||||
|
) throws IOException {
|
||||||
|
group.setGroup(newDecryptedGroup, this::resolveRecipient);
|
||||||
|
final var messageBuilder = getGroupUpdateMessageBuilder(group, groupChange.toByteArray());
|
||||||
|
account.getGroupStore().updateGroup(group);
|
||||||
|
return sendMessage(messageBuilder, group.getMembersIncludingPendingWithout(account.getSelfRecipientId()));
|
||||||
|
}
|
||||||
|
|
||||||
private static int currentTimeDays() {
|
private static int currentTimeDays() {
|
||||||
return (int) TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis());
|
return (int) TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis());
|
||||||
}
|
}
|
||||||
|
@ -959,15 +982,6 @@ public class Manager implements Closeable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Pair<Long, List<SendMessageResult>> sendUpdateGroupMessage(
|
|
||||||
GroupInfoV2 group, DecryptedGroup newDecryptedGroup, GroupChange groupChange
|
|
||||||
) throws IOException {
|
|
||||||
group.setGroup(newDecryptedGroup, this::resolveRecipient);
|
|
||||||
final var messageBuilder = getGroupUpdateMessageBuilder(group, groupChange.toByteArray());
|
|
||||||
account.getGroupStore().updateGroup(group);
|
|
||||||
return sendMessage(messageBuilder, group.getMembersIncludingPendingWithout(account.getSelfRecipientId()));
|
|
||||||
}
|
|
||||||
|
|
||||||
Pair<Long, List<SendMessageResult>> sendGroupInfoMessage(
|
Pair<Long, List<SendMessageResult>> sendGroupInfoMessage(
|
||||||
GroupIdV1 groupId, SignalServiceAddress recipient
|
GroupIdV1 groupId, SignalServiceAddress recipient
|
||||||
) throws IOException, NotAGroupMemberException, GroupNotFoundException, AttachmentInvalidException {
|
) throws IOException, NotAGroupMemberException, GroupNotFoundException, AttachmentInvalidException {
|
||||||
|
|
|
@ -62,15 +62,20 @@ public class UpdateGroupCommand implements DbusCommand, LocalCommand {
|
||||||
var groupAvatar = ns.getString("avatar");
|
var groupAvatar = ns.getString("avatar");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (groupId == null) {
|
||||||
|
var results = m.createGroup(groupName,
|
||||||
|
groupMembers,
|
||||||
|
groupAvatar == null ? null : new File(groupAvatar));
|
||||||
|
ErrorUtils.handleTimestampAndSendMessageResults(writer, 0, results.second());
|
||||||
|
final var newGroupId = results.first();
|
||||||
|
writer.println("Created new group: \"{}\"", newGroupId.toBase64());
|
||||||
|
} else {
|
||||||
var results = m.updateGroup(groupId,
|
var results = m.updateGroup(groupId,
|
||||||
groupName,
|
groupName,
|
||||||
groupDescription,
|
groupDescription,
|
||||||
groupMembers,
|
groupMembers,
|
||||||
groupAvatar == null ? null : new File(groupAvatar));
|
groupAvatar == null ? null : new File(groupAvatar));
|
||||||
ErrorUtils.handleTimestampAndSendMessageResults(writer, 0, results.second());
|
ErrorUtils.handleTimestampAndSendMessageResults(writer, results.first(), results.second());
|
||||||
final var newGroupId = results.first();
|
|
||||||
if (groupId == null) {
|
|
||||||
writer.println("Created new group: \"{}\"", newGroupId.toBase64());
|
|
||||||
}
|
}
|
||||||
} catch (AttachmentInvalidException e) {
|
} catch (AttachmentInvalidException e) {
|
||||||
throw new UserErrorException("Failed to add avatar attachment for group\": " + e.getMessage());
|
throw new UserErrorException("Failed to add avatar attachment for group\": " + e.getMessage());
|
||||||
|
|
|
@ -335,13 +335,19 @@ public class DbusSignalImpl implements Signal {
|
||||||
if (avatar.isEmpty()) {
|
if (avatar.isEmpty()) {
|
||||||
avatar = null;
|
avatar = null;
|
||||||
}
|
}
|
||||||
final var results = m.updateGroup(groupId == null ? null : GroupId.unknownVersion(groupId),
|
if (groupId == null) {
|
||||||
|
final var results = m.createGroup(name, members, avatar == null ? null : new File(avatar));
|
||||||
|
checkSendMessageResults(0, results.second());
|
||||||
|
return results.first().serialize();
|
||||||
|
} else {
|
||||||
|
final var results = m.updateGroup(GroupId.unknownVersion(groupId),
|
||||||
name,
|
name,
|
||||||
null,
|
null,
|
||||||
members,
|
members,
|
||||||
avatar == null ? null : new File(avatar));
|
avatar == null ? null : new File(avatar));
|
||||||
checkSendMessageResults(0, results.second());
|
checkSendMessageResults(results.first(), results.second());
|
||||||
return results.first().serialize();
|
return groupId;
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new Error.Failure(e.getMessage());
|
throw new Error.Failure(e.getMessage());
|
||||||
} catch (GroupNotFoundException | NotAGroupMemberException e) {
|
} catch (GroupNotFoundException | NotAGroupMemberException e) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue