Skip to content

Commit

Permalink
Fix IMessageEditCallback#retainFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Jul 19, 2022
1 parent 2eb1341 commit efe995d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public abstract class InteractionCallbackImpl<T> extends RestActionImpl<T> imple
{
protected final List<AttachedFile> files = new ArrayList<>();
protected final InteractionImpl interaction;
protected boolean isFileUpdate = false;

public InteractionCallbackImpl(InteractionImpl interaction)
{
Expand All @@ -54,30 +55,42 @@ public InteractionCallbackImpl(InteractionImpl interaction)
protected RequestBody finalizeData()
{
DataObject json = toData();
if (files.isEmpty())
return getRequestBody(json);

MultipartBody.Builder body = AttachedFile.createMultipartBody(files, null);
if (isFileUpdate || !files.isEmpty())
{
// Add the attachments array to the payload, as required since v10
DataObject data;
if (json.isNull("data"))
json.put("data", data = DataObject.empty());
else
data = json.getObject("data");

// Add the attachments array to the payload, as required since v10
DataObject data;
if (json.isNull("data"))
json.put("data", data = DataObject.empty());
else
data = json.getObject("data");
DataArray attachments;
if (data.isNull("attachments"))
data.put("attachments", attachments = DataArray.empty());
else
attachments = data.getArray("attachments");

DataArray attachments;
if (data.isNull("attachments"))
data.put("attachments", attachments = DataArray.empty());
else
attachments = data.getArray("attachments");
for (int i = 0; i < files.size(); i++)
attachments.add(files.get(i).toAttachmentData(i));
}

for (int i = 0; i < files.size(); i++)
attachments.add(files.get(i).toAttachmentData(i));
RequestBody body;
// Upload files using multipart request if applicable
if (files.stream().anyMatch(FileUpload.class::isInstance))
{
MultipartBody.Builder form = AttachedFile.createMultipartBody(files, null);
form.addFormDataPart("payload_json", json.toString());
body = form.build();
}
else
{
body = getRequestBody(json);
}

body.addFormDataPart("payload_json", json.toString());
isFileUpdate = false;
files.clear();
return body.build();
return body;
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public MessageEditCallbackActionImpl closeResources()

private boolean isEmpty()
{
return content == null && embeds == null && components == null && files.isEmpty();
return content == null && embeds == null && components == null && !isFileUpdate && files.isEmpty();
}

@Override
Expand Down Expand Up @@ -152,6 +152,7 @@ public MessageEditCallbackAction addFile(@Nonnull InputStream data, @Nonnull Str
name = "SPOILER_" + name;

files.add(FileUpload.fromData(data, name));
isFileUpdate = true;
return this;
}

Expand All @@ -167,6 +168,7 @@ public MessageEditCallbackAction retainFilesById(@Nonnull Collection<String> ids
ids.stream()
.map(AttachedFile::fromAttachment)
.forEach(this.files::add);
isFileUpdate = true;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public ReplyCallbackAction addFile(@Nonnull InputStream data, @Nonnull String na
name = "SPOILER_" + name;

files.add(FileUpload.fromData(data, name));
isFileUpdate = true;
return this;
}

Expand Down

0 comments on commit efe995d

Please sign in to comment.