package com.discord.stores; import android.app.Application; import android.content.Context; import androidx.core.app.FrameMetricsAggregator; import b.a.d.o; import b.a.e.d; import b.d.b.a.a; import b.i.a.f.e.o.f; import com.discord.api.channel.Channel; import com.discord.api.presence.ClientStatus; import com.discord.models.guild.Guild; import com.discord.models.message.Message; import com.discord.restapi.RestAPIParams; import com.discord.stores.StoreStream; import com.discord.utilities.SnowflakeUtils; import com.discord.utilities.analytics.AnalyticsTracker; import com.discord.utilities.fcm.NotificationClient; import com.discord.utilities.persister.Persister; import com.discord.utilities.rest.RestAPI; import com.discord.utilities.rx.ObservableExtensionsKt; import com.discord.utilities.time.Clock; import com.discord.widgets.notice.NoticePopupChannel; import d0.z.d.m; import java.util.LinkedHashMap; import java.util.Map; import java.util.concurrent.TimeUnit; import kotlin.jvm.functions.Function0; import kotlin.jvm.functions.Function1; import kotlin.jvm.internal.DefaultConstructorMarker; import rx.Observable; import s.a.k0; /* compiled from: StoreNotifications.kt */ /* loaded from: classes.dex */ public final class StoreNotifications extends Store { public static final Companion Companion = new Companion(null); private static final long INAPP_MESSAGE_WINDOW_MS = 10000; private String authToken; private final Clock clock; private Context context; private final Persister notificationSettings = new Persister<>("STORE_NOTIFICATIONS_SETTINGS_V2", new NotificationClient.SettingsV2(false, false, false, false, false, false, null, null, null, FrameMetricsAggregator.EVERY_DURATION, null)); private String pushToken; private String pushTokenPersisted; private final StoreStream stream; /* compiled from: StoreNotifications.kt */ /* loaded from: classes.dex */ public static final class Companion { private Companion() { } public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } } public StoreNotifications(Clock clock, StoreStream storeStream) { m.checkNotNullParameter(clock, "clock"); m.checkNotNullParameter(storeStream, "stream"); this.clock = clock; this.stream = storeStream; } public static final /* synthetic */ Context access$getContext$p(StoreNotifications storeNotifications) { return storeNotifications.context; } public static final /* synthetic */ String access$getPushToken$p(StoreNotifications storeNotifications) { return storeNotifications.pushToken; } public static final /* synthetic */ void access$handleRegistrationToken(StoreNotifications storeNotifications, String str) { storeNotifications.handleRegistrationToken(str); } public static final /* synthetic */ void access$handleRegistrationTokenPersisted(StoreNotifications storeNotifications, String str) { storeNotifications.handleRegistrationTokenPersisted(str); } public static final /* synthetic */ void access$setContext$p(StoreNotifications storeNotifications, Context context) { storeNotifications.context = context; } public static final /* synthetic */ void access$setPushToken$p(StoreNotifications storeNotifications, String str) { storeNotifications.pushToken = str; } private final void configureContextSetter(Application application) { application.registerActivityLifecycleCallbacks(new StoreNotifications$configureContextSetter$1(this)); } private final void configureNotificationClient() { NotificationClient.INSTANCE.setRegistrationIdReceived(new StoreNotifications$configureNotificationClient$1(this)); Observable settings = getSettings(); StoreStream.Companion companion = StoreStream.Companion; Observable j = Observable.j(Observable.h(settings, companion.getAuthentication().getAuthedToken$app_productionGoogleRelease(), companion.getUserSettingsSystem().observeSettings(false), ObservableExtensionsKt.leadingEdgeThrottle(companion.getPermissions().observePermissionsForAllChannels(), 1L, TimeUnit.SECONDS).F(StoreNotifications$configureNotificationClient$completedSettings$1.INSTANCE), StoreNotifications$configureNotificationClient$completedSettings$2.INSTANCE).q(), d.d.a(), StoreNotifications$configureNotificationClient$2.INSTANCE); m.checkNotNullExpressionValue(j, "Observable\n .comb… isBackgrounded\n }"); Observable q = ObservableExtensionsKt.computationBuffered(j).q(); m.checkNotNullExpressionValue(q, "Observable\n .comb… .distinctUntilChanged()"); ObservableExtensionsKt.appSubscribe$default(q, (Context) null, "nsClient", (Function1) null, StoreNotifications$configureNotificationClient$3.INSTANCE, (Function1) null, (Function0) null, (Function0) null, 117, (Object) null); } private final void displayPopup(Message message, Channel channel) { Context context; if ((message.getId() >>> 22) + SnowflakeUtils.DISCORD_EPOCH + 10000 > this.clock.currentTimeMillis() && (context = this.context) != null) { NoticePopupChannel noticePopupChannel = NoticePopupChannel.INSTANCE; StringBuilder R = a.R("{InAppNotif}#"); R.append(message.getChannelId()); noticePopupChannel.enqueue(context, R.toString(), message, new StoreNotifications$displayPopup$1(channel, message)); } } private final synchronized void handleRegistrationToken(String str) { this.pushToken = str; tryTokenPersist(); } private final synchronized void handleRegistrationTokenPersisted(String str) { this.pushTokenPersisted = str; } public static /* synthetic */ void setEnabledInApp$default(StoreNotifications storeNotifications, boolean z2, boolean z3, int i, Object obj) { if ((i & 2) != 0) { z3 = true; } storeNotifications.setEnabledInApp(z2, z3); } private final void tryTokenPersist() { String str = this.authToken; if (str == null) { this.pushTokenPersisted = null; } if (str != null && !m.areEqual(this.pushToken, this.pushTokenPersisted)) { ObservableExtensionsKt.computationBuffered(ObservableExtensionsKt.restSubscribeOn$default(RestAPI.Companion.getApi().userCreateDevice(new RestAPIParams.UserDevices(this.pushToken)), false, 1, null)).k(o.a.g(null, new StoreNotifications$tryTokenPersist$1(this), null)); } } public final synchronized String getPushToken() { return this.pushToken; } public final Observable getSettings() { return ObservableExtensionsKt.computationBuffered(this.notificationSettings.getObservable()); } public final synchronized void handleAuthToken(String str) { this.authToken = str; tryTokenPersist(); } @StoreThread public final void handleChannelSelected(long j) { f.H0(f.c(k0.a), null, null, new StoreNotifications$handleChannelSelected$1(j, null), 3, null); } /* JADX WARN: Removed duplicated region for block: B:31:0x00d2 */ /* JADX WARN: Removed duplicated region for block: B:43:? A[RETURN, SYNTHETIC] */ @StoreThread /* Code decompiled incorrectly, please refer to instructions dump. */ public final void handleMessageCreate(com.discord.api.message.Message message) { m.checkNotNullParameter(message, "message"); if (this.notificationSettings.get().isEnabledInApp() && this.stream.getPresences$app_productionGoogleRelease().getLocalPresence$app_productionGoogleRelease().getStatus() != ClientStatus.DND) { Map relationships = this.stream.getUserRelationships$app_productionGoogleRelease().getRelationships(); LinkedHashMap linkedHashMap = new LinkedHashMap(); for (Map.Entry entry : relationships.entrySet()) { if (entry.getValue().intValue() == 2) { linkedHashMap.put(entry.getKey(), entry.getValue()); } } if (message.g() != this.stream.getChannelsSelected$app_productionGoogleRelease().getId()) { Map> channelsByGuildInternal$app_productionGoogleRelease = this.stream.getChannels$app_productionGoogleRelease().getChannelsByGuildInternal$app_productionGoogleRelease(); Long m = message.m(); Map map = channelsByGuildInternal$app_productionGoogleRelease.get(Long.valueOf(m != null ? m.longValue() : 0L)); Channel channel = this.stream.getChannels$app_productionGoogleRelease().getThreadsByIdInternal$app_productionGoogleRelease().get(Long.valueOf(message.g())); if (channel == null) { if (map != null) { channel = map.get(Long.valueOf(message.g())); } else { channel = null; if (channel == null) { if (NotificationTextUtils.INSTANCE.shouldNotifyInAppPopup(this.stream.getUsers$app_productionGoogleRelease().getMeInternal$app_productionGoogleRelease(), message, channel, linkedHashMap, map != null ? map.get(Long.valueOf(channel.r())) : null, (Guild) a.u0(channel, this.stream.getGuilds$app_productionGoogleRelease().getGuildsInternal$app_productionGoogleRelease()), this.stream.getGuilds$app_productionGoogleRelease().getGuildMembersComputedInternal$app_productionGoogleRelease(), this.stream.getGuildSettings$app_productionGoogleRelease().getGuildSettingsInternal$app_productionGoogleRelease(), this.stream.getThreadsJoined$app_productionGoogleRelease().getAllJoinedThreadsInternal$app_productionGoogleRelease(), this.stream.getVoiceChannelSelected$app_productionGoogleRelease().getSelectedVoiceChannelId(), (Long) a.c(channel, this.stream.getPermissions$app_productionGoogleRelease().getPermissionsByChannel()))) { displayPopup(new Message(message), channel); return; } return; } return; } } if (channel == null) { } } } } public final void handlePreLogout() { Persister.set$default(this.notificationSettings, new NotificationClient.SettingsV2(false, false, false, false, false, false, null, null, null, FrameMetricsAggregator.EVERY_DURATION, null), false, 2, null); } public final void init(Application application) { m.checkNotNullParameter(application, "application"); super.init((Context) application); configureContextSetter(application); configureNotificationClient(); } public final void setEnabled(boolean z2) { this.notificationSettings.getAndSet(true, new StoreNotifications$setEnabled$1(z2)); AnalyticsTracker.INSTANCE.updateNotifications(z2); } public final void setEnabledInApp(boolean z2, boolean z3) { NotificationClient.SettingsV2 andSet = this.notificationSettings.getAndSet(true, new StoreNotifications$setEnabledInApp$oldValue$1(z2)); if (z3 && andSet.isEnabledInApp() != z2) { AnalyticsTracker.INSTANCE.updateNotificationsInApp(z2); } } public final void setNotificationLightDisabled(boolean z2) { this.notificationSettings.getAndSet(true, new StoreNotifications$setNotificationLightDisabled$1(z2)); } public final void setNotificationSoundDisabled(boolean z2) { this.notificationSettings.getAndSet(true, new StoreNotifications$setNotificationSoundDisabled$1(z2)); } public final void setNotificationsVibrateDisabled(boolean z2) { this.notificationSettings.getAndSet(true, new StoreNotifications$setNotificationsVibrateDisabled$1(z2)); } }