Skip to content

Commit

Permalink
Add attachment flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolaj-skrzypczak authored and felldo committed Jan 17, 2024
1 parent 971ea44 commit 0767ea7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.EnumSet;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -100,11 +101,18 @@ default boolean isImage() {
/**
* Gets the base64 encoded string of the waveform of the audio file
* Only present if the message is of type {@link MessageFlag#IS_VOICE_MESSAGE}.
*
*
* @return The base64 encoded string of the waveform of the audio file.
*/
Optional<String> getWaveFormBase64();

/**
* Gets the flags of the attachment.
*
* @return The flags of the attachment.
*/
EnumSet<AttachmentFlag> getFlags();

/**
* Downloads the attachment as an input stream.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.javacord.api.entity;

public enum AttachmentFlag {
/**
* This attachment has been edited using the remix feature on mobile.
*/
IS_REMIX(1 << 2),

/**
* The unknown message flag type.
*/
UNKNOWN(-1);

private final int id;

/**
* Creates a new attachment flags type.
*
* @param id attachment flags id.
*/
AttachmentFlag(final int id) {
this.id = id;
}

/**
* Gets the id of the attachment flags type.
*
* @return The id of the attachment flags type.
*/
public int getId() {
return this.id;
}

/**
* Gets the attashment flags type by its id.
*
* @param id The id of the attachment flag type.
* @return The attachment flag type with the given id or {@link AttachmentFlag#UNKNOWN} if unknown id.
*/
public static AttachmentFlag getFlagTypeById(final int id) {
for (final AttachmentFlag value : values()) {
if (value.getId() == id) {
return value;
}
}
return UNKNOWN;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.apache.logging.log4j.Logger;
import org.javacord.api.DiscordApi;
import org.javacord.api.entity.Attachment;
import org.javacord.api.entity.AttachmentFlag;
import org.javacord.api.entity.DiscordEntity;
import org.javacord.core.util.FileContainer;
import org.javacord.core.util.logging.LoggerUtil;
Expand All @@ -16,6 +17,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Base64;
import java.util.EnumSet;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -76,7 +78,7 @@ public class AttachmentImpl implements Attachment {
* Whether this attachment is ephemeral.
*/
private final Boolean ephemeral;

/**
* The duration of the audio file.
*/
Expand All @@ -87,6 +89,11 @@ public class AttachmentImpl implements Attachment {
*/
private final String waveform;

/**
* Attachment flags computed from the bitfield.
*/
private final EnumSet<AttachmentFlag> attachmentFlags = EnumSet.noneOf(AttachmentFlag.class);

/**
* Creates a new attachment.
*
Expand All @@ -106,6 +113,14 @@ public AttachmentImpl(DiscordApi api, final JsonNode data) {
ephemeral = data.hasNonNull("ephemeral") ? data.get("ephemeral").asBoolean() : null;
durationSeconds = data.hasNonNull("duration_secs") ? data.get("duration_secs").doubleValue() : null;
waveform = data.hasNonNull("waveform") ? data.get("waveform").asText() : null;
if (data.has("flags")) {
int flags = data.get("flags").asInt();
for (AttachmentFlag flag : AttachmentFlag.values()) {
if ((flag.getId() & flags) == flag.getId()) {
attachmentFlags.add(flag);
}
}
}
}

@Override
Expand Down Expand Up @@ -183,6 +198,11 @@ public Optional<String> getWaveFormBase64() {
return Optional.ofNullable(waveform);
}

@Override
public EnumSet<AttachmentFlag> getFlags() {
return attachmentFlags;
}

@Override
public InputStream asInputStream() throws IOException {
return new FileContainer(getUrl()).asInputStream(getApi());
Expand Down

0 comments on commit 0767ea7

Please sign in to comment.