Update note to self sending flow.

This commit is contained in:
Greyson Parrelli
2025-02-06 11:44:39 -05:00
parent 09447dda0f
commit e2b0567534
5 changed files with 14 additions and 202 deletions

View File

@@ -123,7 +123,6 @@ public final class JobManagerFactories {
put(AttachmentCopyJob.KEY, new AttachmentCopyJob.Factory());
put(AttachmentDownloadJob.KEY, new AttachmentDownloadJob.Factory());
put(AttachmentHashBackfillJob.KEY, new AttachmentHashBackfillJob.Factory());
put(MarkNoteToSelfAttachmentUploadedJob.KEY, new MarkNoteToSelfAttachmentUploadedJob.Factory());
put(AttachmentUploadJob.KEY, new AttachmentUploadJob.Factory());
put(AutomaticSessionResetJob.KEY, new AutomaticSessionResetJob.Factory());
put(AvatarGroupsV1DownloadJob.KEY, new AvatarGroupsV1DownloadJob.Factory());
@@ -379,6 +378,7 @@ public final class JobManagerFactories {
put("DonationReceiptRedemptionJob", new FailingJob.Factory());
put("SendGiftJob", new FailingJob.Factory());
put("InactiveGroupCheckMigrationJob", new PassingMigrationJob.Factory());
put("AttachmentMarkUploadedJob", new FailingJob.Factory());
}};
}

View File

@@ -1,97 +0,0 @@
package org.thoughtcrime.securesms.jobs;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.attachments.AttachmentId;
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
import org.thoughtcrime.securesms.database.AttachmentTable;
import org.thoughtcrime.securesms.database.SignalDatabase;
import org.thoughtcrime.securesms.jobmanager.JsonJobData;
import org.thoughtcrime.securesms.jobmanager.Job;
import java.util.concurrent.TimeUnit;
/**
* Marks a note to self attachment (that didn't need to be uploaded, because there's no linked devices) as being uploaded for UX purposes.
* Also generates a key/iv/digest that otherwise wouldn't exist due to the lack of upload.
*/
public final class MarkNoteToSelfAttachmentUploadedJob extends BaseJob {
public static final String KEY = "AttachmentMarkUploadedJob";
@SuppressWarnings("unused")
private static final String TAG = Log.tag(MarkNoteToSelfAttachmentUploadedJob.class);
private static final String KEY_ATTACHMENT_ID = "row_id";
private static final String KEY_MESSAGE_ID = "message_id";
private final AttachmentId attachmentId;
private final long messageId;
public MarkNoteToSelfAttachmentUploadedJob(long messageId, @NonNull AttachmentId attachmentId) {
this(new Parameters.Builder()
.setLifespan(TimeUnit.DAYS.toMillis(1))
.setMaxAttempts(Parameters.UNLIMITED)
.build(),
messageId,
attachmentId);
}
private MarkNoteToSelfAttachmentUploadedJob(@NonNull Parameters parameters, long messageId, @NonNull AttachmentId attachmentId) {
super(parameters);
this.attachmentId = attachmentId;
this.messageId = messageId;
}
@Override
public @Nullable byte[] serialize() {
return new JsonJobData.Builder().putLong(KEY_ATTACHMENT_ID, attachmentId.id)
.putLong(KEY_MESSAGE_ID, messageId)
.serialize();
}
@Override
public @NonNull String getFactoryKey() {
return KEY;
}
@Override
public void onRun() throws Exception {
DatabaseAttachment databaseAttachment = SignalDatabase.attachments().getAttachment(attachmentId);
if (databaseAttachment == null) {
throw new InvalidAttachmentException("Cannot find the specified attachment.");
}
SignalDatabase.attachments().markAttachmentUploaded(messageId, databaseAttachment);
SignalDatabase.attachments().createKeyIvDigestIfNecessary(databaseAttachment);
}
@Override
public void onFailure() {
}
@Override
protected boolean onShouldRetry(@NonNull Exception exception) {
return false;
}
private class InvalidAttachmentException extends Exception {
InvalidAttachmentException(String message) {
super(message);
}
}
public static final class Factory implements Job.Factory<MarkNoteToSelfAttachmentUploadedJob> {
@Override
public @NonNull MarkNoteToSelfAttachmentUploadedJob create(@NonNull Parameters parameters, @Nullable byte[] serializedData) {
JsonJobData data = JsonJobData.deserialize(serializedData);
return new MarkNoteToSelfAttachmentUploadedJob(parameters,
data.getLong(KEY_MESSAGE_ID),
new AttachmentId(data.getLong(KEY_ATTACHMENT_ID)));
}
}
}

View File

@@ -118,22 +118,7 @@ class MediaSelectionRepository(context: Context) {
StoryType.NONE
}
if (MessageSender.isLocalSelfSend(context, singleRecipient, SendType.SIGNAL)) {
Log.i(TAG, "Local self-send. Skipping pre-upload.")
emitter.onSuccess(
MediaSendActivityResult(
recipientId = singleRecipient!!.id,
nonUploadedMedia = updatedMedia,
body = trimmedBody,
messageSendType = sendType,
isViewOnce = isViewOnce,
mentions = trimmedMentions,
bodyRanges = trimmedBodyRanges,
storyType = StoryType.NONE,
scheduledTime = scheduledTime
)
)
} else if (scheduledTime != -1L && storyType == StoryType.NONE) {
if (scheduledTime != -1L && storyType == StoryType.NONE) {
Log.i(TAG, "Scheduled message. Skipping pre-upload.")
if (contacts.isEmpty()) {
emitter.onSuccess(
@@ -261,10 +246,6 @@ class MediaSelectionRepository(context: Context) {
uploadRepository.deleteAbandonedAttachments()
}
fun isLocalSelfSend(recipient: Recipient?): Boolean {
return MessageSender.isLocalSelfSend(context, recipient, SendType.SIGNAL)
}
@WorkerThread
private fun buildModelsToTransform(
selectedMedia: List<Media>,

View File

@@ -107,7 +107,7 @@ class MediaSelectionViewModel(
store.update {
it.copy(
isMeteredConnection = metered,
isPreUploadEnabled = shouldPreUpload(metered, it.recipient)
isPreUploadEnabled = shouldPreUpload(metered)
)
}
}
@@ -120,7 +120,7 @@ class MediaSelectionViewModel(
store.update(Recipient.live(recipientSearchKey.recipientId).liveData) { r, s ->
s.copy(
recipient = r,
isPreUploadEnabled = shouldPreUpload(s.isMeteredConnection, r)
isPreUploadEnabled = shouldPreUpload(s.isMeteredConnection)
)
}
}
@@ -449,8 +449,8 @@ class MediaSelectionViewModel(
repository.uploadRepository.cancelUpload(media)
}
private fun shouldPreUpload(metered: Boolean, recipient: Recipient?): Boolean {
return !metered && !repository.isLocalSelfSend(recipient)
private fun shouldPreUpload(metered: Boolean): Boolean {
return !metered
}
fun onSaveState(outState: Bundle) {

View File

@@ -50,7 +50,6 @@ import org.thoughtcrime.securesms.jobmanager.Job;
import org.thoughtcrime.securesms.jobmanager.JobManager;
import org.thoughtcrime.securesms.jobs.AttachmentCompressionJob;
import org.thoughtcrime.securesms.jobs.AttachmentCopyJob;
import org.thoughtcrime.securesms.jobs.MarkNoteToSelfAttachmentUploadedJob;
import org.thoughtcrime.securesms.jobs.AttachmentUploadJob;
import org.thoughtcrime.securesms.jobs.IndividualSendJob;
import org.thoughtcrime.securesms.jobs.ProfileKeySendJob;
@@ -59,17 +58,14 @@ import org.thoughtcrime.securesms.jobs.PushGroupSendJob;
import org.thoughtcrime.securesms.jobs.ReactionSendJob;
import org.thoughtcrime.securesms.jobs.RemoteDeleteSendJob;
import org.thoughtcrime.securesms.keyvalue.SignalStore;
import org.thoughtcrime.securesms.linkpreview.LinkPreview;
import org.thoughtcrime.securesms.mediasend.Media;
import org.thoughtcrime.securesms.mms.MmsException;
import org.thoughtcrime.securesms.mms.OutgoingMessage;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientId;
import org.thoughtcrime.securesms.recipients.RecipientUtil;
import org.thoughtcrime.securesms.service.ExpiringMessageManager;
import org.thoughtcrime.securesms.util.ParcelUtil;
import org.thoughtcrime.securesms.util.SignalLocalMetrics;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.signalservice.api.push.DistributionId;
import org.whispersystems.signalservice.api.util.Preconditions;
@@ -78,10 +74,8 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@@ -203,8 +197,8 @@ public class MessageSender {
recipient,
SendType.SIGNAL,
messageId,
jobDependencyIds,
false);
jobDependencyIds
);
}
onMessageSent();
@@ -240,7 +234,7 @@ public class MessageSender {
SignalLocalMetrics.IndividualMessageSend.onInsertedIntoDatabase(messageId, metricId);
}
sendMessageInternal(context, recipient, sendType, messageId, Collections.emptyList(), message.getScheduledDate() > 0);
sendMessageInternal(context, recipient, sendType, messageId, Collections.emptyList());
onMessageSent();
threadTable.update(allocatedThreadId, true, true);
@@ -282,7 +276,7 @@ public class MessageSender {
attachmentDatabase.updateMessageId(attachmentIds, messageId, message.getStoryType().isStory());
sendMessageInternal(context, recipient, SendType.SIGNAL, messageId, jobIds, false);
sendMessageInternal(context, recipient, SendType.SIGNAL, messageId, jobIds);
onMessageSent();
threadTable.update(allocatedThreadId, true, true);
@@ -398,9 +392,7 @@ public class MessageSender {
long messageId = messageIds.get(i);
Recipient recipient = messages.get(i).getThreadRecipient();
if (isLocalSelfSend(context, recipient, SendType.SIGNAL)) {
sendLocalMediaSelf(messageId);
} else if (recipient.isPushGroup()) {
if (recipient.isPushGroup()) {
jobManager.add(new PushGroupSendJob(messageId, recipient.getId(), Collections.emptySet(), true, false), messageDependsOnIds, recipient.getId().toQueueKey());
} else if (recipient.isDistributionList()) {
jobManager.add(new PushDistributionListSendJob(messageId, recipient.getId(), true, Collections.emptySet()), messageDependsOnIds, recipient.getId().toQueueKey());
@@ -415,9 +407,6 @@ public class MessageSender {
* be enqueued (like in the case of a local self-send).
*/
public static @Nullable PreUploadResult preUploadPushAttachment(@NonNull Context context, @NonNull Attachment attachment, @Nullable Recipient recipient, @NonNull Media media) {
if (isLocalSelfSend(context, recipient, SendType.SIGNAL)) {
return null;
}
Log.i(TAG, "Pre-uploading attachment for " + (recipient != null ? recipient.getId() : "null"));
try {
@@ -508,7 +497,7 @@ public class MessageSender {
sendType = SendType.SIGNAL;
}
sendMessageInternal(context, recipient, sendType, messageId, Collections.emptyList(), false);
sendMessageInternal(context, recipient, sendType, messageId, Collections.emptyList());
onMessageSent();
}
@@ -532,12 +521,9 @@ public class MessageSender {
Recipient recipient,
SendType sendType,
long messageId,
@NonNull Collection<String> uploadJobIds,
boolean isScheduledSend)
@NonNull Collection<String> uploadJobIds)
{
if (isLocalSelfSend(context, recipient, sendType) && !isScheduledSend && !SignalStore.backup().backsUpMedia()) {
sendLocalMediaSelf(messageId);
} else if (recipient.isPushGroup()) {
if (recipient.isPushGroup()) {
sendGroupPush(context, recipient, messageId, Collections.emptySet(), uploadJobIds);
} else if (recipient.isDistributionList()) {
sendDistributionList(context, recipient, messageId, Collections.emptySet(), uploadJobIds);
@@ -609,64 +595,6 @@ public class MessageSender {
}
}
public static boolean isLocalSelfSend(@NonNull Context context, @Nullable Recipient recipient, SendType sendType) {
return recipient != null &&
recipient.isSelf() &&
sendType == SendType.SIGNAL &&
SignalStore.account().isRegistered() &&
!SignalStore.account().hasLinkedDevices();
}
private static void sendLocalMediaSelf(long messageId) {
try {
ExpiringMessageManager expirationManager = AppDependencies.getExpiringMessageManager();
MessageTable mmsDatabase = SignalDatabase.messages();
OutgoingMessage message = mmsDatabase.getOutgoingMessage(messageId);
SyncMessageId syncId = new SyncMessageId(Recipient.self().getId(), message.getSentTimeMillis());
List<Attachment> attachments = new LinkedList<>();
attachments.addAll(message.getAttachments());
attachments.addAll(Stream.of(message.getLinkPreviews())
.map(LinkPreview::getThumbnail)
.filter(Optional::isPresent)
.map(Optional::get)
.toList());
attachments.addAll(Stream.of(message.getSharedContacts())
.map(Contact::getAvatar).withoutNulls()
.map(Contact.Avatar::getAttachment).withoutNulls()
.toList());
List<AttachmentCompressionJob> compressionJobs = Stream.of(attachments)
.map(a -> AttachmentCompressionJob.fromAttachment((DatabaseAttachment) a, false, -1))
.toList();
List<MarkNoteToSelfAttachmentUploadedJob> fakeUploadJobs = Stream.of(attachments)
.map(a -> new MarkNoteToSelfAttachmentUploadedJob(messageId, ((DatabaseAttachment) a).attachmentId))
.toList();
AppDependencies.getJobManager().startChain(compressionJobs)
.then(fakeUploadJobs)
.enqueue();
mmsDatabase.markAsSent(messageId, true);
mmsDatabase.markUnidentified(messageId, true);
mmsDatabase.incrementDeliveryReceiptCount(message.getSentTimeMillis(), Recipient.self().getId(), System.currentTimeMillis());
mmsDatabase.incrementReadReceiptCount(message.getSentTimeMillis(), Recipient.self().getId(), System.currentTimeMillis());
mmsDatabase.incrementViewedReceiptCount(message.getSentTimeMillis(), Recipient.self().getId(), System.currentTimeMillis());
if (message.getExpiresIn() > 0 && !message.isExpirationUpdate()) {
mmsDatabase.markExpireStarted(messageId);
expirationManager.scheduleDeletion(messageId, true, message.getExpiresIn());
}
} catch (NoSuchMessageException | MmsException e) {
Log.w(TAG, "Failed to update self-sent message.", e);
}
}
public static class PreUploadResult implements Parcelable {
private final Media media;
private final AttachmentId attachmentId;