Repository navigation
Expand file tree
/
Copy pathUpdatedMessage.java
More file actions
65 lines (54 loc) · 2.1 KB
/
Copy pathUpdatedMessage.java
File metadata and controls
65 lines (54 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package dev.getchat.sdk;
import org.jspecify.annotations.Nullable;
/**
* The result of {@link GetChatClient#updateMessage}. The backend always reports whether
* the edit changed anything ({@link #isUpdated()}), but only echoes the stored
* message when the caller asked for it via
* {@link UpdateMessageOptions.Builder#returnResource(boolean)} (which sends
* {@code Prefer: return=representation}); otherwise {@link #message()} is
* {@code null}. Both signals — plus the raw envelope — are kept so the return type
* is honest about what came back.
*
* <p>A lazy view over the response {@link JsonValue}, equal to another instance when
* their underlying JSON is equal.
*/
public final class UpdatedMessage {
private final JsonValue raw;
private UpdatedMessage(JsonValue raw) {
this.raw = raw;
}
/** Wrap an update-message response envelope as a typed result. */
public static UpdatedMessage of(JsonValue raw) {
return new UpdatedMessage(raw);
}
/** The whole response envelope, for fields without a typed accessor. */
public JsonValue raw() {
return raw;
}
/** Whether the edit actually changed the message. */
public boolean isUpdated() {
return raw.get("is_updated").asBoolean(false);
}
/**
* The updated message, or {@code null} when it was not requested (no
* {@code returnResource(true)} / {@code Prefer: return=representation}).
*/
public @Nullable Message message() {
JsonValue m = raw.get("message");
return m.isObject() ? Message.of(m) : null;
}
/** Equal when the underlying JSON is equal. */
@Override
public boolean equals(@Nullable Object o) {
return this == o || (o instanceof UpdatedMessage other && raw.equals(other.raw));
}
@Override
public int hashCode() {
return raw.hashCode();
}
/** Compact summary for logs, e.g. {@code UpdatedMessage{isUpdated=true, hasMessage=false}}. */
@Override
public String toString() {
return "UpdatedMessage{isUpdated=" + isUpdated() + ", hasMessage=" + (message() != null) + "}";
}
}