Fix message deletion & connectivity bug

This commit is contained in:
Niels Andriesse 2020-01-17 10:06:32 +11:00
parent 362256dc94
commit ec2691a918
3 changed files with 49 additions and 39 deletions

View File

@ -510,7 +510,6 @@ public class ConversationFragment extends Fragment
builder.setMessage(getActivity().getResources().getQuantityString(R.plurals.ConversationFragment_this_will_permanently_delete_all_n_selected_messages, messagesCount, messagesCount)); builder.setMessage(getActivity().getResources().getQuantityString(R.plurals.ConversationFragment_this_will_permanently_delete_all_n_selected_messages, messagesCount, messagesCount));
builder.setCancelable(true); builder.setCancelable(true);
// Loki - The delete option is only visible to the user in a public chat
LokiPublicChat publicChat = DatabaseFactory.getLokiThreadDatabase(getContext()).getPublicChat(threadId); LokiPublicChat publicChat = DatabaseFactory.getLokiThreadDatabase(getContext()).getPublicChat(threadId);
builder.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() { builder.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
@ -522,42 +521,52 @@ public class ConversationFragment extends Fragment
{ {
@Override @Override
protected Void doInBackground(MessageRecord... messageRecords) { protected Void doInBackground(MessageRecord... messageRecords) {
ArrayList<Long> serverIDs = new ArrayList<>(); if (publicChat != null) {
ArrayList<Long> ignoredMessages = new ArrayList<>(); ArrayList<Long> serverIDs = new ArrayList<>();
ArrayList<Long> failedMessages = new ArrayList<>(); ArrayList<Long> ignoredMessages = new ArrayList<>();
boolean isSentByUser = true; ArrayList<Long> failedMessages = new ArrayList<>();
LokiPublicChatAPI publicChatAPI = ApplicationContext.getInstance(getContext()).getLokiPublicChatAPI(); boolean isSentByUser = true;
for (MessageRecord messageRecord : messageRecords) { LokiPublicChatAPI publicChatAPI = ApplicationContext.getInstance(getContext()).getLokiPublicChatAPI();
isSentByUser = isSentByUser && messageRecord.isOutgoing(); for (MessageRecord messageRecord : messageRecords) {
Long serverID = DatabaseFactory.getLokiMessageDatabase(getContext()).getServerID(messageRecord.id); isSentByUser = isSentByUser && messageRecord.isOutgoing();
if (serverID != null) { Long serverID = DatabaseFactory.getLokiMessageDatabase(getContext()).getServerID(messageRecord.id);
serverIDs.add(serverID); if (serverID != null) {
} else { serverIDs.add(serverID);
ignoredMessages.add(messageRecord.getId()); } else {
} ignoredMessages.add(messageRecord.getId());
}
if (publicChat != null && publicChatAPI != null) {
publicChatAPI
.deleteMessages(serverIDs, publicChat.getChannel(), publicChat.getServer(), isSentByUser)
.success(l -> {
for (MessageRecord messageRecord : messageRecords) {
Long serverID = DatabaseFactory.getLokiMessageDatabase(getContext()).getServerID(messageRecord.id);
if (l.contains(serverID)) {
if (messageRecord.isMms()) {
DatabaseFactory.getMmsDatabase(getActivity()).delete(messageRecord.getId());
} else {
DatabaseFactory.getSmsDatabase(getActivity()).deleteMessage(messageRecord.getId());
}
} else if (!ignoredMessages.contains(serverID)) {
failedMessages.add(messageRecord.getId());
Log.d("Loki", "Failed to delete message: " + messageRecord.getId() + ".");
}
} }
return null; }
}). fail(e -> { if (publicChat != null && publicChatAPI != null) {
Log.d("Loki", "Couldn't delete message due to error: " + e.toString() + "."); publicChatAPI
return null; .deleteMessages(serverIDs, publicChat.getChannel(), publicChat.getServer(), isSentByUser)
}); .success(l -> {
for (MessageRecord messageRecord : messageRecords) {
Long serverID = DatabaseFactory.getLokiMessageDatabase(getContext()).getServerID(messageRecord.id);
if (l.contains(serverID)) {
if (messageRecord.isMms()) {
DatabaseFactory.getMmsDatabase(getActivity()).delete(messageRecord.getId());
} else {
DatabaseFactory.getSmsDatabase(getActivity()).deleteMessage(messageRecord.getId());
}
} else if (!ignoredMessages.contains(serverID)) {
failedMessages.add(messageRecord.getId());
Log.d("Loki", "Failed to delete message: " + messageRecord.getId() + ".");
}
}
return null;
}). fail(e -> {
Log.d("Loki", "Couldn't delete message due to error: " + e.toString() + ".");
return null;
});
}
} else {
for (MessageRecord messageRecord : messageRecords) {
if (messageRecord.isMms()) {
DatabaseFactory.getMmsDatabase(getActivity()).delete(messageRecord.getId());
} else {
DatabaseFactory.getSmsDatabase(getActivity()).deleteMessage(messageRecord.getId());
}
}
} }
return null; return null;
} }

View File

@ -40,11 +40,12 @@ class DisplayNameActivity : BaseActionBarActivity() {
TextSecurePreferences.setProfileName(this, displayName) TextSecurePreferences.setProfileName(this, displayName)
TextSecurePreferences.setHasSeenWelcomeScreen(this, true) TextSecurePreferences.setHasSeenWelcomeScreen(this, true)
TextSecurePreferences.setPromptedPushRegistration(this, true) TextSecurePreferences.setPromptedPushRegistration(this, true)
val application = ApplicationContext.getInstance(this)
application.setUpStorageAPIIfNeeded()
application.setUpP2PAPI()
val publicChatAPI = ApplicationContext.getInstance(this).lokiPublicChatAPI val publicChatAPI = ApplicationContext.getInstance(this).lokiPublicChatAPI
if (publicChatAPI != null) { if (publicChatAPI != null) {
// TODO: This won't be necessary anymore when we don't auto-join the Loki Public Chat anymore // TODO: This won't be necessary anymore when we don't auto-join the Loki Public Chat anymore
val application = ApplicationContext.getInstance(this)
application.setUpStorageAPIIfNeeded()
application.createDefaultPublicChatsIfNeeded() application.createDefaultPublicChatsIfNeeded()
val servers = DatabaseFactory.getLokiThreadDatabase(this).getAllPublicChatServers() val servers = DatabaseFactory.getLokiThreadDatabase(this).getAllPublicChatServers()
servers.forEach { publicChatAPI.setDisplayName(displayName, it) } servers.forEach { publicChatAPI.setDisplayName(displayName, it) }

View File

@ -80,7 +80,7 @@ class JoinPublicChatActivity : PassphraseRequiredActionBarActivity(), ScanQRCode
finish() finish()
}.failUi { }.failUi {
hideLoader() hideLoader()
Toast.makeText(this, "Couldn't Join Channel", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Couldn't join channel", Toast.LENGTH_SHORT).show()
} }
} }
// endregion // endregion