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 sorting of WhatsApp messages (#2337 complement) #2352

Merged
merged 1 commit into from
Oct 27, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ private List<Message> extractMessages(Connection conn, WAContact remote, boolean
}
}

Collections.sort(messages);
Message.sort(messages);
}

//Find quote messages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected List<Chat> extractChatList() throws WAExtractorException {
extractCalls(conn, idToChat);

for (Chat c : list) {
Collections.sort(c.getMessages());
Message.sort(c.getMessages());
if (c.isGroupChat()) {
setGroupMembers(c, conn, SELECT_GROUP_MEMBERS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ private void mergeUndeletedMessages(Chat chat, Map<String, Message> messagesMap,
}
}

Collections.sort(chat.getMessages());
Message.sort(chat.getMessages());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,4 +691,57 @@ public static enum MessageQuotedType {
public int compareTo(Message o) {
return Long.compare(getSortId(), o.getSortId());
}

public static void sort(List<Message> messages) {
Collections.sort(messages);

// Check if there are messages with sortId != 0 AND sortId == 0
boolean zero = false;
boolean notZero = false;
for (Message m : messages) {
if (m.sortId == 0) {
zero = true;
} else {
notZero = true;
}
if (zero && notZero) {
break;
}
}

// If both are present, merge these two groups
if (zero && notZero) {
List<Message> l0 = new ArrayList<Message>();
List<Message> l1 = new ArrayList<Message>();
for (Message m : messages) {
if (m.sortId == 0) {
l0.add(m);
} else {
l1.add(m);
}
}
messages.clear();
int idx0 = 0;
int idx1 = 0;
while (idx0 < l0.size() || idx1 < l1.size()) {
if (idx0 == l0.size()) {
messages.add(l1.get(idx1++));
} else if (idx1 == l1.size()) {
messages.add(l0.get(idx0++));
} else {
Message m0 = l0.get(idx0);
Message m1 = l1.get(idx1);
long t0 = m0.timeStamp == null ? m0.id : m0.timeStamp.getTime();
long t1 = m1.timeStamp == null ? m1.id : m1.timeStamp.getTime();
if (t0 < t1) {
messages.add(m0);
idx0++;
} else {
messages.add(m1);
idx1++;
}
}
}
}
}
}
Loading