That fuck shit the fascists are using
1package org.tm.archive.util;
2
3
4import android.content.Context;
5import android.text.TextUtils;
6
7import androidx.annotation.NonNull;
8import androidx.annotation.Nullable;
9import androidx.annotation.WorkerThread;
10
11import org.signal.core.util.logging.Log;
12import org.tm.archive.attachments.AttachmentId;
13import org.tm.archive.attachments.DatabaseAttachment;
14import org.tm.archive.database.NoSuchMessageException;
15import org.tm.archive.database.SignalDatabase;
16import org.tm.archive.database.model.MessageRecord;
17import org.tm.archive.jobmanager.impl.NotInCallConstraint;
18import org.tm.archive.recipients.Recipient;
19
20import java.util.Collections;
21import java.util.Set;
22
23public class AttachmentUtil {
24
25 private static final String TAG = Log.tag(AttachmentUtil.class);
26
27 @WorkerThread
28 public static boolean isAutoDownloadPermitted(@NonNull Context context, @Nullable DatabaseAttachment attachment) {
29 if (attachment == null) {
30 Log.w(TAG, "attachment was null, returning vacuous true");
31 return true;
32 }
33
34 if (!isFromTrustedConversation(context, attachment)) {
35 Log.w(TAG, "Not allowing download due to untrusted conversation");
36 return false;
37 }
38
39 Set<String> allowedTypes = getAllowedAutoDownloadTypes(context);
40 String contentType = attachment.contentType;
41
42 if (attachment.voiceNote ||
43 (MediaUtil.isAudio(attachment) && TextUtils.isEmpty(attachment.fileName)) ||
44 MediaUtil.isLongTextType(attachment.contentType) ||
45 attachment.isSticker())
46 {
47 return true;
48 } else if (attachment.videoGif) {
49 boolean allowed = NotInCallConstraint.isNotInConnectedCall() && allowedTypes.contains("image");
50 if (!allowed) {
51 Log.w(TAG, "Not auto downloading. inCall: " + NotInCallConstraint.isNotInConnectedCall() + " allowedType: " + allowedTypes.contains("image"));
52 }
53 return allowed;
54 } else if (isNonDocumentType(contentType)) {
55 boolean allowed = NotInCallConstraint.isNotInConnectedCall() && allowedTypes.contains(MediaUtil.getDiscreteMimeType(contentType));
56 if (!allowed) {
57 Log.w(TAG, "Not auto downloading. inCall: " + NotInCallConstraint.isNotInConnectedCall() + " allowedType: " + allowedTypes.contains(MediaUtil.getDiscreteMimeType(contentType)));
58 }
59 return allowed;
60 } else {
61 boolean allowed = NotInCallConstraint.isNotInConnectedCall() && allowedTypes.contains("documents");
62 if (!allowed) {
63 Log.w(TAG, "Not auto downloading. inCall: " + NotInCallConstraint.isNotInConnectedCall() + " allowedType: " + allowedTypes.contains("documents"));
64 }
65 return allowed;
66 }
67 }
68
69 /**
70 * Deletes the specified attachment. If its the only attachment for its linked message, the entire
71 * message is deleted.
72 */
73 @WorkerThread
74 public static void deleteAttachment(@NonNull Context context,
75 @NonNull DatabaseAttachment attachment)
76 {
77 AttachmentId attachmentId = attachment.attachmentId;
78 long mmsId = attachment.mmsId;
79 int attachmentCount = SignalDatabase.attachments()
80 .getAttachmentsForMessage(mmsId)
81 .size();
82
83 if (attachmentCount <= 1) {
84 SignalDatabase.messages().deleteMessage(mmsId);
85 } else {
86 SignalDatabase.attachments().deleteAttachment(attachmentId);
87 }
88 }
89
90 private static boolean isNonDocumentType(String contentType) {
91 return
92 MediaUtil.isImageType(contentType) ||
93 MediaUtil.isVideoType(contentType) ||
94 MediaUtil.isAudioType(contentType);
95 }
96
97 private static @NonNull Set<String> getAllowedAutoDownloadTypes(@NonNull Context context) {
98 if (NetworkUtil.isConnectedWifi(context)) return TextSecurePreferences.getWifiMediaDownloadAllowed(context);
99 else if (NetworkUtil.isConnectedRoaming(context)) return TextSecurePreferences.getRoamingMediaDownloadAllowed(context);
100 else if (NetworkUtil.isConnectedMobile(context)) return TextSecurePreferences.getMobileMediaDownloadAllowed(context);
101 else return Collections.emptySet();
102 }
103
104 @WorkerThread
105 private static boolean isFromTrustedConversation(@NonNull Context context, @NonNull DatabaseAttachment attachment) {
106 try {
107 MessageRecord message = SignalDatabase.messages().getMessageRecord(attachment.mmsId);
108
109 Recipient fromRecipient = message.getFromRecipient();
110 Recipient toRecipient = SignalDatabase.threads().getRecipientForThreadId(message.getThreadId());
111
112 if (toRecipient != null && toRecipient.isGroup()) {
113 return toRecipient.isProfileSharing() || isTrustedIndividual(fromRecipient, message);
114 } else {
115 return isTrustedIndividual(fromRecipient, message);
116 }
117 } catch (NoSuchMessageException e) {
118 Log.w(TAG, "Message could not be found! Assuming not a trusted contact.");
119 return false;
120 }
121 }
122
123 private static boolean isTrustedIndividual(@NonNull Recipient recipient, @NonNull MessageRecord message) {
124 return recipient.isSystemContact() ||
125 recipient.isProfileSharing() ||
126 message.isOutgoing() ||
127 recipient.isSelf() ||
128 recipient.isReleaseNotes();
129 }
130 }