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

Quoted message overriding contextInfo #492

Closed
wants to merge 9 commits into from
11 changes: 4 additions & 7 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 Expand Up @@ -583,7 +580,7 @@ public CompletableFuture<ChatMessageInfo> sendChatMessage(JidProvider recipient,
.status(MessageStatus.PENDING)
.senderJid(jidOrThrowError())
.key(key)
.message(message.withDeviceInfo(deviceInfo))
.message(recipient.toJid().hasServer(JidServer.GROUP) ? message : message.withDeviceInfo(deviceInfo))
.timestampSeconds(timestamp)
.broadcast(recipient.toJid().hasServer(JidServer.BROADCAST))
.build();
Expand Down
31 changes: 31 additions & 0 deletions 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,37 @@ public static ContextInfo of(MessageInfo quotedMessage) {
.build();
}

public static ContextInfo of(ContextInfo context, MessageInfo quotedMessage) {
return new ContextInfoBuilder()
.quotedMessageId(quotedMessage.id())
.quotedMessage(quotedMessage.message())
.quotedMessageChatJid(quotedMessage.parentJid())
.quotedMessageSenderJid(quotedMessage.senderJid())
.actionLink(context.actionLink().orElse(null))
.conversionData(context.conversionData().orElse(null))
.conversionSource(context.conversionSource().orElse(null))
.conversionDelaySeconds(context.conversionDelaySeconds())
.entryPointConversionApp(context.entryPointConversionApp().orElse(null))
.entryPointConversionSource(context.entryPointConversionSource().orElse(null))
.entryPointConversionDelaySeconds(context.entryPointConversionDelaySeconds())
.disappearingMode(context.disappearingMode().orElse(null))
.ephemeralExpiration(context.ephemeralExpiration())
.ephemeralSettingTimestamp(context.ephemeralSettingTimestamp())
.externalAdReply(context.externalAdReply().orElse(null))
.forwarded(context.forwarded())
.forwardingScore(context.forwardingScore())
.groupSubject(context.groupSubject().orElse(null))
.ephemeralSharedSecret(context.ephemeralSharedSecret().orElse(null))
.parentGroup(context.parentGroup().orElse(null))
.placeholderKey(context.placeholderKey().orElse(null))
.quotedAd(context.quotedAd().orElse(null))
.trustBannerAction(context.trustBannerAction())
.trustBannerType(context.trustBannerType().orElse(null))
.mentions(context.mentions())
.build();
}


public static ContextInfo empty() {
return new ContextInfoBuilder()
.mentions(new ArrayList<>())
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/it/auties/whatsapp/socket/MessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -1076,10 +1076,12 @@ private void saveMessage(ChatMessageInfo info, boolean notify) {
return;
}
if (info.message().hasCategory(MessageCategory.SERVER)) {
if (info.message().content() instanceof ProtocolMessage protocolMessage) {
if (!(info.message().content() instanceof ProtocolMessage protocolMessage)) return;
if (protocolMessage.protocolType() == null) return;
if (!protocolMessage.protocolType().equals(ProtocolMessage.Type.MESSAGE_EDIT)) {
handleProtocolMessage(info, protocolMessage);
return;
}
return;
}

var chat = info.chat()
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/it/auties/whatsapp/socket/SocketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,13 @@ public CompletableFuture<Void> sendReceipt(Jid jid, Jid participant, List<String
.put("t", Clock.nowMilliseconds(), () -> Objects.equals(type, "read") || Objects.equals(type, "read-self"))
.put("to", jid)
.put("type", type, Objects::nonNull);
if (Objects.equals(type, "sender") && jid.hasServer(JidServer.WHATSAPP)) {
attributes.put("recipient", jid);
attributes.put("to", participant);
if (Objects.equals(type, "sender")) {
if (jid.hasServer(JidServer.WHATSAPP)) {
attributes.put("recipient", jid);
attributes.put("to", participant);
} else if (jid.hasServer(JidServer.GROUP)) {
attributes.put("participant", participant);
}
}

var receipt = Node.of("receipt", attributes.toMap(), toMessagesNode(messages));
Expand Down
Loading