Handle 428 rate limiting.

This commit is contained in:
Greyson Parrelli
2021-05-05 12:49:18 -04:00
parent 02d060ca0a
commit 31e1c6f7aa
60 changed files with 1235 additions and 57 deletions

View File

@@ -23,6 +23,7 @@ import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.Ringtone;
@@ -131,7 +132,7 @@ public class DefaultMessageNotifier implements MessageNotifier {
}
@Override
public void notifyMessageDeliveryFailed(Context context, Recipient recipient, long threadId) {
public void notifyMessageDeliveryFailed(@NonNull Context context, @NonNull Recipient recipient, long threadId) {
if (visibleThread == threadId) {
sendInThreadNotification(context, recipient);
} else {
@@ -145,6 +146,15 @@ public class DefaultMessageNotifier implements MessageNotifier {
}
}
@Override
public void notifyProofRequired(@NonNull Context context, @NonNull Recipient recipient, long threadId) {
if (visibleThread == threadId) {
sendInThreadNotification(context, recipient);
} else {
Log.w(TAG, "[Proof Required] Not notifying on old notifier.");
}
}
@Override
public void cancelDelayedNotifications() {
executor.cancel();

View File

@@ -16,7 +16,8 @@ public interface MessageNotifier {
long getVisibleThread();
void clearVisibleThread();
void setLastDesktopActivityTimestamp(long timestamp);
void notifyMessageDeliveryFailed(Context context, Recipient recipient, long threadId);
void notifyMessageDeliveryFailed(@NonNull Context context, @NonNull Recipient recipient, long threadId);
void notifyProofRequired(@NonNull Context context, @NonNull Recipient recipient, long threadId);
void cancelDelayedNotifications();
void updateNotification(@NonNull Context context);
void updateNotification(@NonNull Context context, long threadId);

View File

@@ -53,10 +53,15 @@ public class OptimizedMessageNotifier implements MessageNotifier {
}
@Override
public void notifyMessageDeliveryFailed(Context context, Recipient recipient, long threadId) {
public void notifyMessageDeliveryFailed(@NonNull Context context, @NonNull Recipient recipient, long threadId) {
getNotifier().notifyMessageDeliveryFailed(context, recipient, threadId);
}
@Override
public void notifyProofRequired(@NonNull Context context, @NonNull Recipient recipient, long threadId) {
getNotifier().notifyProofRequired(context, recipient, threadId);
}
@Override
public void cancelDelayedNotifications() {
getNotifier().cancelDelayedNotifications();

View File

@@ -76,6 +76,10 @@ class MessageNotifierV2(context: Application) : MessageNotifier {
NotificationFactory.notifyMessageDeliveryFailed(context, recipient, threadId, visibleThread)
}
override fun notifyProofRequired(context: Context, recipient: Recipient, threadId: Long) {
NotificationFactory.notifyProofRequired(context, recipient, threadId, visibleThread)
}
override fun cancelDelayedNotifications() {
executor.cancel()
}

View File

@@ -315,6 +315,33 @@ object NotificationFactory {
NotificationManagerCompat.from(context).safelyNotify(context, recipient, threadId.toInt(), builder.build())
}
fun notifyProofRequired(context: Context, recipient: Recipient, threadId: Long, visibleThread: Long) {
if (threadId == visibleThread) {
notifyInThread(context, recipient, 0)
return
}
val intent: Intent = ConversationIntents.createBuilder(context, recipient.id, threadId)
.build()
.makeUniqueToPreventMerging()
val builder: NotificationBuilder = NotificationBuilder.create(context)
builder.apply {
setSmallIcon(R.drawable.ic_notification)
setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.ic_info_outline))
setContentTitle(context.getString(R.string.MessageNotifier_message_delivery_paused))
setContentText(context.getString(R.string.MessageNotifier_verify_to_continue_messaging_on_signal))
setContentIntent(PendingIntent.getActivity(context, 0, intent, 0))
setOnlyAlertOnce(true)
setAutoCancel(true)
setAlarms(recipient)
setChannelId(NotificationChannels.FAILURES)
}
NotificationManagerCompat.from(context).safelyNotify(context, recipient, threadId.toInt(), builder.build())
}
private fun NotificationManagerCompat.safelyNotify(context: Context, threadRecipient: Recipient?, notificationId: Int, notification: Notification) {
try {
notify(notificationId, notification)