Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: quoted message overriding contextInfo #493

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/main/java/it/auties/whatsapp/api/Whatsapp.java
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,7 @@ public CompletableFuture<? extends MessageInfo> sendNewsletterMessage(JidProvide
* @return a CompletableFuture
*/
public CompletableFuture<? extends MessageInfo> sendMessage(JidProvider chat, ContextualMessage<?> message, MessageInfo quotedMessage) {
var contextInfo = ContextInfo.of(quotedMessage);
message.setContextInfo(contextInfo);
message.contextInfo().ifPresentOrElse(contextInfo -> message.setContextInfo(ContextInfo.of(contextInfo, quotedMessage)), () -> message.setContextInfo(ContextInfo.of(quotedMessage)));
return sendMessage(chat, MessageContainer.of(message));
}

Expand All @@ -517,8 +516,7 @@ public CompletableFuture<? extends MessageInfo> sendMessage(JidProvider chat, Co
* @return a CompletableFuture
*/
public CompletableFuture<ChatMessageInfo> sendChatMessage(JidProvider chat, ContextualMessage<?> message, MessageInfo quotedMessage) {
var contextInfo = ContextInfo.of(quotedMessage);
message.setContextInfo(contextInfo);
message.contextInfo().ifPresentOrElse(contextInfo -> message.setContextInfo(ContextInfo.of(contextInfo, quotedMessage)), () -> message.setContextInfo(ContextInfo.of(quotedMessage)));
return sendChatMessage(chat, MessageContainer.of(message));
}

Expand All @@ -532,8 +530,7 @@ public CompletableFuture<ChatMessageInfo> sendChatMessage(JidProvider chat, Cont
* @return a CompletableFuture
*/
public CompletableFuture<NewsletterMessageInfo> sendNewsletterMessage(JidProvider chat, ContextualMessage<?> message, MessageInfo quotedMessage) {
var contextInfo = ContextInfo.of(quotedMessage);
message.setContextInfo(contextInfo);
message.contextInfo().ifPresentOrElse(contextInfo -> message.setContextInfo(ContextInfo.of(contextInfo, quotedMessage)), () -> message.setContextInfo(ContextInfo.of(quotedMessage)));
return sendNewsletterMessage(chat, MessageContainer.of(message));
}

Expand Down
20 changes: 19 additions & 1 deletion src/main/java/it/auties/whatsapp/model/info/ContextInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,24 @@ public static ContextInfo of(MessageInfo quotedMessage) {
.build();
}

public static ContextInfo of(ContextInfo contextInfo, MessageInfo quotedMessage) {
var newContext = of(quotedMessage);
var fields = contextInfo.getClass().getFields();
try {
for (var field : fields) {
field.setAccessible(true);
var value = field.get(contextInfo);
if (value != null) {
field.set(newContext, value);
}
}
return newContext;
} catch (IllegalAccessException e) {
System.err.println("Failed to merge context info");
return ContextInfo.of(quotedMessage);
}
}

public static ContextInfo empty() {
return new ContextInfoBuilder()
.mentions(new ArrayList<>())
Expand Down Expand Up @@ -401,4 +419,4 @@ public Optional<String> trustBannerType() {
public int trustBannerAction() {
return trustBannerAction;
}
}
}
Loading