package com.discord.stores; import android.content.Context; import b.a.d.a0; import b.a.d.b0; import b.d.b.a.a; import com.discord.api.channel.Channel; import com.discord.api.channel.ChannelUtils; import com.discord.api.utcdatetime.UtcDateTime; import com.discord.models.application.Unread; import com.discord.models.domain.ModelNotificationSettings; import com.discord.stores.StoreMessageAck; import com.discord.stores.StoreStream; import com.discord.stores.StoreThreadsActiveJoined; import com.discord.utilities.SnowflakeUtils; import com.discord.utilities.message.MessageUtils; import com.discord.utilities.permissions.PermissionUtils; import com.discord.utilities.persister.Persister; import com.discord.utilities.rx.ObservableExtensionsKt; import com.discord.utilities.rx.ObservableWithLeadingEdgeThrottle; import com.discord.utilities.time.Clock; import d0.z.d.m; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import kotlin.Pair; import kotlin.jvm.functions.Function0; import kotlin.jvm.functions.Function1; import rx.Observable; import rx.subjects.BehaviorSubject; import rx.subjects.SerializedSubject; /* compiled from: StoreReadStates.kt */ /* loaded from: classes.dex */ public final class StoreReadStates extends Store { private final Clock clock; private final SerializedSubject, Set> unreadChannelIds = new SerializedSubject<>(BehaviorSubject.l0(new HashSet())); private final SerializedSubject, Set> unreadGuildIds = new SerializedSubject<>(BehaviorSubject.l0(new HashSet())); private final SerializedSubject markAsRead = new SerializedSubject<>(BehaviorSubject.l0(Boolean.FALSE)); private final Persister unreadMessageMarker = new Persister<>("UNREAD_MESSAGE_MARKER_V3", new Unread(null, 0, 3, null)); public StoreReadStates(Clock clock) { m.checkNotNullParameter(clock, "clock"); this.clock = clock; } public static final /* synthetic */ void access$clearMarker(StoreReadStates storeReadStates) { storeReadStates.clearMarker(); } public static final /* synthetic */ Pair access$computeUnreadIds(StoreReadStates storeReadStates, Map map, Map map2, Map map3, Map map4, Map map5, Map map6, Map map7, long j) { return storeReadStates.computeUnreadIds(map, map2, map3, map4, map5, map6, map7, j); } public static final /* synthetic */ SerializedSubject access$getMarkAsRead$p(StoreReadStates storeReadStates) { return storeReadStates.markAsRead; } public static final /* synthetic */ SerializedSubject access$getUnreadChannelIds$p(StoreReadStates storeReadStates) { return storeReadStates.unreadChannelIds; } public static final /* synthetic */ SerializedSubject access$getUnreadGuildIds$p(StoreReadStates storeReadStates) { return storeReadStates.unreadGuildIds; } public static final /* synthetic */ Persister access$getUnreadMessageMarker$p(StoreReadStates storeReadStates) { return storeReadStates.unreadMessageMarker; } public static final /* synthetic */ boolean access$wasMessageSentAfterTimestamp(StoreReadStates storeReadStates, UtcDateTime utcDateTime, Long l) { return storeReadStates.wasMessageSentAfterTimestamp(utcDateTime, l); } private final void clearMarker() { Observable Z = this.unreadMessageMarker.getObservable().G(StoreReadStates$clearMarker$1.INSTANCE).Z(1); m.checkNotNullExpressionValue(Z, "unreadMessageMarker\n …ount() }\n .take(1)"); ObservableExtensionsKt.appSubscribe$default(Z, (Context) null, "unreadMessageMarker", (Function1) null, new StoreReadStates$clearMarker$2(this), (Function1) null, (Function0) null, (Function0) null, 117, (Object) null); } private final void computeUnreadChannelIds() { StoreStream.Companion companion = StoreStream.Companion; Observable> observePermissionsForAllChannels = companion.getPermissions().observePermissionsForAllChannels(); Observable> observeGuildAndPrivateChannels = companion.getChannels().observeGuildAndPrivateChannels(); Observable> observeJoinedAt = companion.getGuilds().observeJoinedAt(); Observable> observeGuildSettings = companion.getUserGuildSettings().observeGuildSettings(); Observable> observeAll = companion.getMessageAck().observeAll(); Observable> observeRecentMessageIds = companion.getMessagesMostRecent().observeRecentMessageIds(); TimeUnit timeUnit = TimeUnit.SECONDS; Observable combineLatest = ObservableWithLeadingEdgeThrottle.combineLatest(observePermissionsForAllChannels, observeGuildAndPrivateChannels, observeJoinedAt, observeGuildSettings, observeAll, ObservableExtensionsKt.leadingEdgeThrottle(observeRecentMessageIds, 3L, timeUnit), companion.getThreadsActiveJoined().observeAllActiveJoinedThreadsById(), companion.getVoiceChannelSelected().observeSelectedVoiceChannelId(), new StoreReadStates$sam$rx_functions_Func8$0(new StoreReadStates$computeUnreadChannelIds$1(this)), 1L, timeUnit); m.checkNotNullExpressionValue(combineLatest, "ObservableWithLeadingEdg…imeUnit.SECONDS\n )"); Observable r = ObservableExtensionsKt.computationLatest(combineLatest).r(); m.checkNotNullExpressionValue(r, "ObservableWithLeadingEdg… .distinctUntilChanged()"); ObservableExtensionsKt.appSubscribe$default(r, (Context) null, "computeUnreadChannelIds", (Function1) null, new StoreReadStates$computeUnreadChannelIds$2(this), (Function1) null, (Function0) null, (Function0) null, 117, (Object) null); } private final Pair, Set> computeUnreadIds(Map map, Map map2, Map map3, Map map4, Map map5, Map map6, Map map7, long j) { long j2; HashSet hashSet = new HashSet(); HashSet hashSet2 = new HashSet(); for (Map.Entry entry : map6.entrySet()) { long longValue = entry.getKey().longValue(); long longValue2 = entry.getValue().longValue(); StoreThreadsActiveJoined.ActiveJoinedThread activeJoinedThread = map7.get(Long.valueOf(longValue)); Channel channel = map2.get(Long.valueOf(longValue)); if (channel == null) { StoreThreadsActiveJoined.ActiveJoinedThread activeJoinedThread2 = map7.get(Long.valueOf(longValue)); channel = activeJoinedThread2 != null ? activeJoinedThread2.getChannel() : null; } if (channel != null && !ChannelUtils.y(channel) && ((!ChannelUtils.E(channel) || activeJoinedThread != null) && PermissionUtils.INSTANCE.hasAccess(channel, map.get(Long.valueOf(longValue))) && !isChannelMuted(channel, map4, activeJoinedThread) && ((!ChannelUtils.G(channel) || channel.j() == j) && (!ChannelUtils.G(channel) || channel.j() != j || wasMessageSentAfterTimestamp(new UtcDateTime(StoreStream.Companion.getVoiceChannelSelected().getTimeSelectedMs()), Long.valueOf(longValue2)))))) { StoreMessageAck.Ack ack = map5.get(Long.valueOf(longValue)); if (ack != null || !ChannelUtils.E(channel)) { if (ack != null) { j2 = ack.getMessageId(); } else { Long l = (Long) a.u0(channel, map3); j2 = ((l != null ? l.longValue() : this.clock.currentTimeMillis()) - SnowflakeUtils.DISCORD_EPOCH) << 22; } if (MessageUtils.isNewer(Long.valueOf(j2), Long.valueOf(longValue2))) { hashSet.add(Long.valueOf(longValue)); Channel channel2 = map2.get(Long.valueOf(channel.t())); boolean isChannelMuted = isChannelMuted(channel2, map4, activeJoinedThread); if (!isChannelMuted && ChannelUtils.E(channel) && channel2 != null) { isChannelMuted = isChannelMuted(map2.get(Long.valueOf(channel2.t())), map4, activeJoinedThread); } if (!isChannelMuted) { hashSet2.add(Long.valueOf(channel.h())); } } } } } return new Pair<>(hashSet, hashSet2); } private final void computeUnreadMarker() { StoreReadStates$computeUnreadMarker$1 storeReadStates$computeUnreadMarker$1 = StoreReadStates$computeUnreadMarker$1.INSTANCE; Observable observeId = StoreStream.Companion.getChannelsSelected().observeId(); StoreReadStates$computeUnreadMarker$2 storeReadStates$computeUnreadMarker$2 = StoreReadStates$computeUnreadMarker$2.INSTANCE; Unread.Marker marker = new Unread.Marker(); StoreReadStates$computeUnreadMarker$3 storeReadStates$computeUnreadMarker$3 = StoreReadStates$computeUnreadMarker$3.INSTANCE; m.checkNotNullParameter(storeReadStates$computeUnreadMarker$2, "observableCondition"); m.checkNotNullParameter(storeReadStates$computeUnreadMarker$3, "defaultObservableFunc"); b0 b0Var = new b0(marker); m.checkNotNullParameter(storeReadStates$computeUnreadMarker$2, "observableCondition"); m.checkNotNullParameter(b0Var, "switchedObservableFunc"); m.checkNotNullParameter(storeReadStates$computeUnreadMarker$3, "defaultObservableFunc"); Observable Y = observeId.k(new a0(storeReadStates$computeUnreadMarker$2, b0Var, storeReadStates$computeUnreadMarker$3)).Y(new StoreReadStates$computeUnreadMarker$4(this)); m.checkNotNullExpressionValue(Y, "getChannelsSelected()\n …clearMarker() }\n }"); ObservableExtensionsKt.appSubscribe$default(Y, (Context) null, "unreadMessageMarker", (Function1) null, new StoreReadStates$computeUnreadMarker$5(this), (Function1) null, (Function0) null, (Function0) null, 117, (Object) null); } private final boolean isChannelMuted(Channel channel, Map map, StoreThreadsActiveJoined.ActiveJoinedThread activeJoinedThread) { if (channel == null) { return false; } if (ChannelUtils.E(channel) && activeJoinedThread != null) { return activeJoinedThread.getMuted(); } long j = channel.j(); ModelNotificationSettings modelNotificationSettings = (ModelNotificationSettings) a.u0(channel, map); ModelNotificationSettings.ChannelOverride channelOverride = modelNotificationSettings != null ? modelNotificationSettings.getChannelOverride(j) : null; return channelOverride != null && channelOverride.isMuted(); } public static /* synthetic */ Observable observeUnreadCountForChannel$default(StoreReadStates storeReadStates, long j, UtcDateTime utcDateTime, int i, Object obj) { if ((i & 2) != 0) { utcDateTime = null; } return storeReadStates.observeUnreadCountForChannel(j, utcDateTime); } private final boolean wasMessageSentAfterTimestamp(UtcDateTime utcDateTime, Long l) { return utcDateTime == null || (l != null && new UtcDateTime((l.longValue() >>> 22) + SnowflakeUtils.DISCORD_EPOCH).compareTo(utcDateTime) >= 0); } public final Observable getIsUnread(long j) { Observable r = getUnreadGuildIds().G(new StoreReadStates$getIsUnread$1(j)).r(); m.checkNotNullExpressionValue(r, "getUnreadGuildIds()\n … .distinctUntilChanged()"); return r; } public final Observable> getUnreadChannelIds() { return this.unreadChannelIds; } public final Observable> getUnreadGuildIds() { return this.unreadGuildIds; } public final Observable getUnreadMarker(long j) { Observable r = getUnreadMarkerForSelectedChannel().y(new StoreReadStates$getUnreadMarker$1(j)).r(); m.checkNotNullExpressionValue(r, "unreadMarkerForSelectedC… .distinctUntilChanged()"); return r; } public final Observable getUnreadMarkerForSelectedChannel() { Observable r = ObservableExtensionsKt.computationLatest(this.unreadMessageMarker.getObservable()).r(); m.checkNotNullExpressionValue(r, "unreadMessageMarker\n … .distinctUntilChanged()"); return r; } @Override // com.discord.stores.Store public void init(Context context) { m.checkNotNullParameter(context, "context"); super.init(context); computeUnreadChannelIds(); computeUnreadMarker(); } public final void markAsRead(Long l) { if (l != null) { SerializedSubject serializedSubject = this.markAsRead; serializedSubject.k.onNext(Boolean.TRUE); SerializedSubject serializedSubject2 = this.markAsRead; serializedSubject2.k.onNext(Boolean.FALSE); StoreStream.Companion.getMessageAck().ack(l.longValue(), false, false); } } public final Observable observeUnreadCountForChannel(long j, UtcDateTime utcDateTime) { StoreStream.Companion companion = StoreStream.Companion; Observable j2 = Observable.j(companion.getMessages().observeMessagesForChannel(j), companion.getMessageAck().observeAll().G(new StoreReadStates$observeUnreadCountForChannel$1(j)), new StoreReadStates$observeUnreadCountForChannel$2(this, utcDateTime)); m.checkNotNullExpressionValue(j2, "Observable.combineLatest…\n messageCount\n }"); return j2; } }