Skip to content

Commit

Permalink
fix #1774: makes old WA queries more robust against different versions
Browse files Browse the repository at this point in the history
  • Loading branch information
lfcnassif committed Aug 11, 2023
1 parent b4548a7 commit 46f2cad
Showing 1 changed file with 33 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,20 +342,7 @@ private List<Message> extractMessages(Connection conn, WAContact remote, boolean
Set<MessageWrapperForDuplicateRemoval> activeMessages = new HashSet<>();
Map<Long, Message> activeMessageIds = new HashMap<>();

String query;
if (!hasMediaDurationCol) {
query = SELECT_MESSAGES_NO_MEDIA_DURATION;
} else if (!hasMediaCaptionCol) {
query = SELECT_MESSAGES_NO_MEDIA_CAPTION;
} else if (!hasForwardedCol) {
query = SELECT_MESSAGES_NO_FORWARDED;
} else if (hasThumbTable) {
query = SELECT_MESSAGES_THUMBS_TABLE;
} else if (hasEditVersionCol) {
query = SELECT_MESSAGES_NO_THUMBS_TABLE;
} else {
query = SELECT_MESSAGES_NO_EDIT_VERSION;
}
String query = getMessagesQuery(hasThumbTable, hasEditVersionCol, hasMediaDurationCol, hasMediaCaptionCol, hasForwardedCol);

try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setFetchSize(1000);
Expand Down Expand Up @@ -654,63 +641,55 @@ protected Message.MessageType decodeMessageType(int messageType, int status, Int
private static final String SELECT_CHAT_LIST_NO_SUBJECT = "SELECT _id as id,key_remote_jid AS contact, " //$NON-NLS-1$
+ " null as subject, 1230768000000 as creation FROM chat_list"; //$NON-NLS-1$

private static String getMessagesQuery(boolean hasThumbTable, boolean hasEditVersion, boolean hasMediaDuration, boolean hasMediaCaption, boolean hasForwarded) {
String query;
if (hasThumbTable) {
query = SELECT_MESSAGES_THUMBS_TABLE;
} else {
query = SELECT_MESSAGES_NO_THUMBS_TABLE;
}
if (!hasEditVersion) {
query = query.replace("edit_version, ", "");
}
if (!hasMediaDuration) {
query = query.replace("media_duration, ", "");
}
if (!hasMediaCaption) {
query = query.replace("media_caption as mediaCaption, ", "null as mediaCaption, ");
}
if (!hasForwarded) {
query = query.replace("(forwarded & 1) as forwarded, ", "");
}
return query;
}

/*
* Filtragem por status de mensagem (status): -1 - mensagens de sistema 0 -
* mensagens 1 - ? 4 - mensagens 5 - mensagens 6 - ligacao / audio 7 - mensagens
* 8 - audio enviado 10 - audio recebido 12 - mensagens 13 - mensagens
*/
private static final String SELECT_MESSAGES_NO_MEDIA_DURATION = "SELECT _id AS id, key_remote_jid " //$NON-NLS-1$
+ "as remoteId, remote_resource AS remoteResource, status, data, " //$NON-NLS-1$
+ "key_from_me as fromMe, timestamp, media_url as mediaUrl, " //$NON-NLS-1$
+ "media_mime_type as mediaMime, media_size as mediaSize, media_name as mediaName, " //$NON-NLS-1$
+ "media_wa_type as messageType, null as thumbData, latitude, longitude, " //$NON-NLS-1$
+ "NULL as forwarded, " //$NON-NLS-1$
+ "NULL as mediaCaption, media_hash as mediaHash, raw_data as rawData FROM " //$NON-NLS-1$
+ "messages WHERE remoteId=? and status!=-1 ORDER BY timestamp"; //$NON-NLS-1$

private static final String SELECT_MESSAGES_NO_MEDIA_CAPTION = "SELECT _id AS id, key_remote_jid " //$NON-NLS-1$
+ "as remoteId, remote_resource AS remoteResource, status, data, " //$NON-NLS-1$
+ "key_from_me as fromMe, timestamp, media_url as mediaUrl, " //$NON-NLS-1$
+ "media_mime_type as mediaMime, media_size as mediaSize, media_name as mediaName, " //$NON-NLS-1$
+ "media_wa_type as messageType, null as thumbData, latitude, longitude, media_duration, " //$NON-NLS-1$
+ "NULL as forwarded, " //$NON-NLS-1$
+ "NULL as mediaCaption, media_hash as mediaHash, raw_data as rawData FROM " //$NON-NLS-1$
+ "messages WHERE remoteId=? and status!=-1 ORDER BY timestamp"; //$NON-NLS-1$

private static final String SELECT_MESSAGES_NO_FORWARDED = "SELECT _id AS id, key_remote_jid " //$NON-NLS-1$
+ "as remoteId, remote_resource AS remoteResource, status, data, " //$NON-NLS-1$
+ "key_from_me as fromMe, timestamp, media_url as mediaUrl, " //$NON-NLS-1$
+ "media_mime_type as mediaMime, media_size as mediaSize, media_name as mediaName, " //$NON-NLS-1$
+ "media_wa_type as messageType, null as thumbData, latitude, longitude, media_duration, " //$NON-NLS-1$
+ "NULL as forwarded, " //$NON-NLS-1$
+ "media_caption as mediaCaption, media_hash as mediaHash, raw_data as rawData FROM " //$NON-NLS-1$
+ "messages WHERE remoteId=? and status!=-1 ORDER BY timestamp"; //$NON-NLS-1$

private static final String SELECT_MESSAGES_NO_THUMBS_TABLE = "SELECT _id AS id, key_remote_jid " //$NON-NLS-1$
+ "as remoteId, remote_resource AS remoteResource, status, data, " //$NON-NLS-1$
+ "key_from_me as fromMe, timestamp, media_url as mediaUrl, " //$NON-NLS-1$
+ "media_mime_type as mediaMime, media_size as mediaSize, media_name as mediaName, " //$NON-NLS-1$
+ "media_wa_type as messageType, null as thumbData, edit_version, latitude, longitude, media_duration, " //$NON-NLS-1$
+ "(forwarded & 1) as forwarded, " //$NON-NLS-1$
+ "media_caption as mediaCaption, media_hash as mediaHash, raw_data as rawData FROM " //$NON-NLS-1$
+ "messages WHERE remoteId=? and status!=-1 ORDER BY timestamp"; //$NON-NLS-1$

private static final String SELECT_MESSAGES_NO_EDIT_VERSION = "SELECT _id AS id, key_remote_jid " //$NON-NLS-1$
+ "as remoteId, remote_resource AS remoteResource, status, data, " //$NON-NLS-1$
+ "key_from_me as fromMe, timestamp, media_url as mediaUrl, " //$NON-NLS-1$
+ "media_mime_type as mediaMime, media_size as mediaSize, media_name as mediaName, " //$NON-NLS-1$
+ "media_wa_type as messageType, null as thumbData, latitude, longitude, media_duration, " //$NON-NLS-1$
+ "media_wa_type as messageType, null as thumbData, latitude, longitude, " //$NON-NLS-1$
+ "edit_version, " //$NON-NLS-1$
+ "media_duration, " //$NON-NLS-1$
+ "media_caption as mediaCaption, " //$NON-NLS-1$
+ "(forwarded & 1) as forwarded, " //$NON-NLS-1$
+ "media_caption as mediaCaption, media_hash as mediaHash, raw_data as rawData FROM " //$NON-NLS-1$
+ "media_hash as mediaHash, raw_data as rawData FROM " //$NON-NLS-1$
+ "messages WHERE remoteId=? and status!=-1 ORDER BY timestamp"; //$NON-NLS-1$

private static final String SELECT_MESSAGES_THUMBS_TABLE = "SELECT _id AS id, messages.key_remote_jid " //$NON-NLS-1$
+ "as remoteId, remote_resource AS remoteResource, status, data, " //$NON-NLS-1$
+ "messages.key_from_me as fromMe, messages.timestamp as timestamp, media_url as mediaUrl, " //$NON-NLS-1$
+ "media_mime_type as mediaMime, media_size as mediaSize, media_name as mediaName, " //$NON-NLS-1$
+ "media_wa_type as messageType, raw_data as rawData, edit_version, latitude, longitude, media_duration, " //$NON-NLS-1$
+ "media_wa_type as messageType, raw_data as rawData, latitude, longitude, " //$NON-NLS-1$
+ "edit_version, " //$NON-NLS-1$
+ "media_duration, " //$NON-NLS-1$
+ "media_caption as mediaCaption, " //$NON-NLS-1$
+ "(forwarded & 1) as forwarded, " //$NON-NLS-1$
+ "media_caption as mediaCaption, media_hash as mediaHash, thumbnail as thumbData FROM " //$NON-NLS-1$
+ "media_hash as mediaHash, thumbnail as thumbData FROM " //$NON-NLS-1$
+ "messages LEFT JOIN message_thumbnails ON (messages.key_id = message_thumbnails.key_id " //$NON-NLS-1$
+ "AND messages.key_remote_jid = message_thumbnails.key_remote_jid " //$NON-NLS-1$
+ "AND messages.key_from_me = message_thumbnails.key_from_me) " //$NON-NLS-1$
Expand Down

0 comments on commit 46f2cad

Please sign in to comment.