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: receipts not sending on groups + Fix: dead sessions #489

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions src/main/java/it/auties/whatsapp/model/jid/Jid.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public record Jid(String user, JidServer server, Integer device, Integer agent)
*/
public Jid(String user, JidServer server, Integer device, Integer agent) {
this.user = user != null && user.startsWith("+") ? user.substring(1) : user;
this.server = server;
this.server = agent != null && agent == 1 ? JidServer.LID : server;
this.device = device;
this.agent = agent;
}
Expand Down Expand Up @@ -189,6 +189,10 @@ public Jid withServer(JidServer server) {
return new Jid(user(), server, device, agent);
}

public Jid withAgent(Integer agent) {
return new Jid(user(), server, device, agent);
}

/**
* Converts this jid to a user jid
*
Expand Down Expand Up @@ -274,4 +278,4 @@ public boolean hasAgent() {
public int hashCode() {
return Objects.hashCode(toString());
}
}
}
18 changes: 14 additions & 4 deletions src/main/java/it/auties/whatsapp/socket/SocketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -715,15 +715,22 @@ public CompletableFuture<Void> sendReceipt(Jid jid, Jid participant, List<String
if (messages.isEmpty()) {
return CompletableFuture.completedFuture(null);
}

jid = jid.withAgent(null);
if (participant != null) {
participant = participant.withAgent(null);
}

var attributes = Attributes.of()
.put("id", messages.getFirst())
.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);
if (jid.hasServer(JidServer.WHATSAPP)) {
attributes.put("to", participant);
attributes.put("recipient", jid, Objects.equals(type, "sender"));
} else if (jid.hasServer(JidServer.GROUP)) {
attributes.put("participant", participant);
}

var receipt = Node.of("receipt", attributes.toMap(), toMessagesNode(messages));
Expand All @@ -742,15 +749,18 @@ private List<Node> toMessagesNode(List<String> messages) {

protected CompletableFuture<Void> sendMessageAck(Jid from, Node node) {
var attrs = node.attributes();
var to = from.withAgent(null);
var participant = attrs.getNullableString("participant");
var recipient = attrs.getNullableString("recipient");
var type = attrs.getOptionalString("type")
.filter(entry -> !Objects.equals(entry, "message"))
.orElse(null);
var attributes = Attributes.of()
.put("id", node.id())
.put("to", from)
.put("class", node.description())
.put("participant", attrs.getNullableString("participant"), Objects::nonNull)
.put("recipient", attrs.getNullableString("recipient"), Objects::nonNull)
.put("participant", Jid.of(participant).withAgent(null), Objects.nonNull(participant))
.put("recipient", Jid.of(recipient).withAgent(null), Objects.nonNull(recipient))
.put("type", type, Objects::nonNull)
.toMap();
return sendNodeWithNoResponse(Node.of("ack", attributes));
Expand Down
Loading