package com.discord.stores; import a0.a.a.b; import android.content.Context; import c.d.b.a.a; import com.airbnb.lottie.parser.AnimatableValueParser; import com.discord.api.channel.Channel; import com.discord.api.guild.Guild; import com.discord.api.guildmember.GuildMember; import com.discord.api.message.Message; import com.discord.api.user.User; import com.discord.api.utcdatetime.UtcDateTime; import com.discord.models.domain.ModelPayload; import com.discord.models.domain.ModelReadState; import com.discord.stores.StoreThreadsActiveJoined; import com.discord.stores.updates.ObservationDeck; import com.discord.utilities.message.MessageUtils; import com.discord.utilities.persister.Persister; import com.discord.utilities.rest.RestAPI; import com.discord.utilities.rx.ObservableExtensionsKt; import com.discord.utilities.threads.ThreadUtils; import d0.d0.f; import d0.o; import d0.t.n0; import d0.t.o0; import d0.z.d.m; import j0.l.e.j; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import kotlin.NoWhenBranchMatchedException; import kotlin.Pair; import kotlin.Unit; import kotlin.jvm.functions.Function0; import kotlin.jvm.functions.Function1; import kotlin.jvm.internal.DefaultConstructorMarker; import rx.Observable; import rx.Subscription; /* compiled from: StoreMessageAck.kt */ public final class StoreMessageAck extends StoreV2 { private final Map acks = new HashMap(this.acksSnapshot); private final Persister> acksPersister; private Map acksSnapshot; private final Dispatcher dispatcher; private final ObservationDeck observationDeck; private final RestAPI restAPI; private final StoreStream stream; private Subscription threadSyncSubscription; /* compiled from: StoreMessageAck.kt */ public static final class Ack { private final boolean isLockedAck; private final long messageId; private final boolean viewed; public Ack(long j, boolean z2, boolean z3) { this.messageId = j; this.viewed = z2; this.isLockedAck = z3; } /* JADX INFO: this call moved to the top of the method (can break code semantics) */ public Ack(ModelReadState modelReadState, boolean z2, boolean z3) { this(modelReadState != null ? modelReadState.getLastMessageId() : 0, z2, z3); } public static /* synthetic */ Ack copy$default(Ack ack, long j, boolean z2, boolean z3, int i, Object obj) { if ((i & 1) != 0) { j = ack.messageId; } if ((i & 2) != 0) { z2 = ack.viewed; } if ((i & 4) != 0) { z3 = ack.isLockedAck; } return ack.copy(j, z2, z3); } public final long component1() { return this.messageId; } public final boolean component2() { return this.viewed; } public final boolean component3() { return this.isLockedAck; } public final Ack copy(long j, boolean z2, boolean z3) { return new Ack(j, z2, z3); } public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof Ack)) { return false; } Ack ack = (Ack) obj; return this.messageId == ack.messageId && this.viewed == ack.viewed && this.isLockedAck == ack.isLockedAck; } public final long getMessageId() { return this.messageId; } public final boolean getViewed() { return this.viewed; } public int hashCode() { int a = b.a(this.messageId) * 31; boolean z2 = this.viewed; int i = 1; if (z2) { z2 = true; } int i2 = z2 ? 1 : 0; int i3 = z2 ? 1 : 0; int i4 = z2 ? 1 : 0; int i5 = (a + i2) * 31; boolean z3 = this.isLockedAck; if (!z3) { i = z3 ? 1 : 0; } return i5 + i; } public final boolean isLockedAck() { return this.isLockedAck; } public String toString() { StringBuilder L = a.L("Ack(messageId="); L.append(this.messageId); L.append(", viewed="); L.append(this.viewed); L.append(", isLockedAck="); return a.G(L, this.isLockedAck, ")"); } } /* compiled from: StoreMessageAck.kt */ public static final class PendingAck { public static final Companion Companion = new Companion(null); private static final PendingAck EMPTY = new PendingAck(0, new Ack(0L, false, false)); private final Ack ack; private final long channelId; /* compiled from: StoreMessageAck.kt */ public static final class Companion { private Companion() { } public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } public final PendingAck getEMPTY() { return PendingAck.access$getEMPTY$cp(); } } public PendingAck(long j, Ack ack) { m.checkNotNullParameter(ack, "ack"); this.channelId = j; this.ack = ack; } public static final /* synthetic */ PendingAck access$getEMPTY$cp() { return EMPTY; } public static /* synthetic */ PendingAck copy$default(PendingAck pendingAck, long j, Ack ack, int i, Object obj) { if ((i & 1) != 0) { j = pendingAck.channelId; } if ((i & 2) != 0) { ack = pendingAck.ack; } return pendingAck.copy(j, ack); } public final long component1() { return this.channelId; } public final Ack component2() { return this.ack; } public final PendingAck copy(long j, Ack ack) { m.checkNotNullParameter(ack, "ack"); return new PendingAck(j, ack); } public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof PendingAck)) { return false; } PendingAck pendingAck = (PendingAck) obj; return this.channelId == pendingAck.channelId && m.areEqual(this.ack, pendingAck.ack); } public final Ack getAck() { return this.ack; } public final long getChannelId() { return this.channelId; } public int hashCode() { int a = b.a(this.channelId) * 31; Ack ack = this.ack; return a + (ack != null ? ack.hashCode() : 0); } public String toString() { StringBuilder L = a.L("PendingAck(channelId="); L.append(this.channelId); L.append(", ack="); L.append(this.ack); L.append(")"); return L.toString(); } } /* compiled from: StoreMessageAck.kt */ public static abstract class ThreadState { /* compiled from: StoreMessageAck.kt */ public static final class NotThread extends ThreadState { public static final NotThread INSTANCE = new NotThread(); private NotThread() { super(null); } } /* compiled from: StoreMessageAck.kt */ public static final class Thread extends ThreadState { private final boolean isActiveJoined; public Thread(boolean z2) { super(null); this.isActiveJoined = z2; } public static /* synthetic */ Thread copy$default(Thread thread, boolean z2, int i, Object obj) { if ((i & 1) != 0) { z2 = thread.isActiveJoined; } return thread.copy(z2); } public final boolean component1() { return this.isActiveJoined; } public final Thread copy(boolean z2) { return new Thread(z2); } public boolean equals(Object obj) { if (this != obj) { return (obj instanceof Thread) && this.isActiveJoined == ((Thread) obj).isActiveJoined; } return true; } public int hashCode() { boolean z2 = this.isActiveJoined; if (z2) { return 1; } return z2 ? 1 : 0; } public final boolean isActiveJoined() { return this.isActiveJoined; } public String toString() { return a.G(a.L("Thread(isActiveJoined="), this.isActiveJoined, ")"); } } private ThreadState() { } public /* synthetic */ ThreadState(DefaultConstructorMarker defaultConstructorMarker) { this(); } } public StoreMessageAck(StoreStream storeStream, ObservationDeck observationDeck, Dispatcher dispatcher, RestAPI restAPI) { m.checkNotNullParameter(storeStream, "stream"); m.checkNotNullParameter(observationDeck, "observationDeck"); m.checkNotNullParameter(dispatcher, "dispatcher"); m.checkNotNullParameter(restAPI, "restAPI"); this.stream = storeStream; this.observationDeck = observationDeck; this.dispatcher = dispatcher; this.restAPI = restAPI; Persister> persister = new Persister<>("MOST_RECENT_ACKS_V3", new HashMap()); this.acksPersister = persister; this.acksSnapshot = persister.get(); } public static final /* synthetic */ Dispatcher access$getDispatcher$p(StoreMessageAck storeMessageAck) { return storeMessageAck.dispatcher; } public static final /* synthetic */ RestAPI access$getRestAPI$p(StoreMessageAck storeMessageAck) { return storeMessageAck.restAPI; } public static final /* synthetic */ StoreStream access$getStream$p(StoreMessageAck storeMessageAck) { return storeMessageAck.stream; } public static final /* synthetic */ Subscription access$getThreadSyncSubscription$p(StoreMessageAck storeMessageAck) { return storeMessageAck.threadSyncSubscription; } public static final /* synthetic */ void access$internalAck(StoreMessageAck storeMessageAck, Channel channel, boolean z2, boolean z3) { storeMessageAck.internalAck(channel, z2, z3); } public static final /* synthetic */ Observable access$observeThreadState(StoreMessageAck storeMessageAck, long j) { return storeMessageAck.observeThreadState(j); } public static final /* synthetic */ void access$postPendingAck(StoreMessageAck storeMessageAck, Observable observable, int i) { storeMessageAck.postPendingAck(observable, i); } public static final /* synthetic */ void access$setThreadSyncSubscription$p(StoreMessageAck storeMessageAck, Subscription subscription) { storeMessageAck.threadSyncSubscription = subscription; } public static final /* synthetic */ boolean access$shouldAck(StoreMessageAck storeMessageAck, ThreadState threadState) { return storeMessageAck.shouldAck(threadState); } public static final /* synthetic */ void access$updateAcks(StoreMessageAck storeMessageAck, long j, Ack ack) { storeMessageAck.updateAcks(j, ack); } public static final /* synthetic */ void access$updateThreadAcks(StoreMessageAck storeMessageAck, Set set, Set set2) { storeMessageAck.updateThreadAcks(set, set2); } public static /* synthetic */ void ack$default(StoreMessageAck storeMessageAck, long j, boolean z2, boolean z3, int i, Object obj) { if ((i & 4) != 0) { z3 = false; } storeMessageAck.ack(j, z2, z3); } /* JADX DEBUG: Multi-variable search result rejected for r0v0, resolved type: com.discord.stores.StoreMessageAck */ /* JADX WARN: Multi-variable type inference failed */ public static /* synthetic */ void ackGuild$default(StoreMessageAck storeMessageAck, Context context, long j, Function0 function0, int i, Object obj) { if ((i & 4) != 0) { function0 = StoreMessageAck$ackGuild$1.INSTANCE; } storeMessageAck.ackGuild(context, j, function0); } private final Observable getPendingAck(Observable observable, boolean z2, boolean z3) { Observable Y = observable.Y(new StoreMessageAck$getPendingAck$2(this, z2, new StoreMessageAck$getPendingAck$1(z3))); m.checkNotNullExpressionValue(Y, "switchMap { channelId ->…gAck.EMPTY)\n }\n }"); return Y; } @StoreThread private final ThreadState getThreadStateInternal(long j) { return !this.stream.getChannels$app_productionBetaRelease().getThreadsByIdInternal$app_productionBetaRelease().containsKey(Long.valueOf(j)) ? ThreadState.NotThread.INSTANCE : new ThreadState.Thread(this.stream.getThreadsActiveJoined$app_productionBetaRelease().getActiveJoinedThreadsInternal$app_productionBetaRelease().containsKey(Long.valueOf(j))); } private final void internalAck(Channel channel, boolean z2, boolean z3) { j jVar = new j(Long.valueOf(channel.h())); m.checkNotNullExpressionValue(jVar, "Observable.just(channel.id)"); postPendingAck$default(this, ObservableExtensionsKt.takeSingleUntilTimeout$default(getPendingAck(jVar, z2, z3), 0, false, 1, null), 0, 1, null); } private final Observable, Set>> observeActiveJoinedThreadIdsWithPrevious() { Observable, Set>> P = this.stream.getThreadsActiveJoined$app_productionBetaRelease().observeAllActiveJoinedThreadsById().F(StoreMessageAck$observeActiveJoinedThreadIdsWithPrevious$1.INSTANCE).r().P(new Pair(n0.emptySet(), n0.emptySet()), StoreMessageAck$observeActiveJoinedThreadIdsWithPrevious$2.INSTANCE); m.checkNotNullExpressionValue(P, "stream\n .threadsA…oinedThreadIds)\n }"); return P; } private final Observable observeThreadState(long j) { Observable r = this.stream.getChannels$app_productionBetaRelease().observeChannel(j).Y(new StoreMessageAck$observeThreadState$1(this, j)).r(); m.checkNotNullExpressionValue(r, "stream\n .channe… .distinctUntilChanged()"); return r; } private final void postPendingAck(Observable observable, int i) { StoreMessageAck$postPendingAck$1 storeMessageAck$postPendingAck$1 = new StoreMessageAck$postPendingAck$1(this, i); Observable x2 = observable.x(StoreMessageAck$postPendingAck$2.INSTANCE); m.checkNotNullExpressionValue(x2, "filter { it != PendingAck.EMPTY }"); ObservableExtensionsKt.appSubscribe$default(ObservableExtensionsKt.computationLatest(x2), observable.getClass(), (Context) null, (Function1) null, (Function1) null, (Function0) null, (Function0) null, new StoreMessageAck$postPendingAck$3(storeMessageAck$postPendingAck$1), 62, (Object) null); } public static /* synthetic */ void postPendingAck$default(StoreMessageAck storeMessageAck, Observable observable, int i, int i2, Object obj) { if ((i2 & 1) != 0) { i = 0; } storeMessageAck.postPendingAck(observable, i); } @StoreThread private final void pruneAcks() { if (!(!this.stream.getGuilds$app_productionBetaRelease().getUnavailableGuildsInternal$app_productionBetaRelease().isEmpty())) { Map channelsByIdInternal$app_productionBetaRelease = this.stream.getChannels$app_productionBetaRelease().getChannelsByIdInternal$app_productionBetaRelease(); Map activeJoinedThreadsInternal$app_productionBetaRelease = this.stream.getThreadsActiveJoined$app_productionBetaRelease().getActiveJoinedThreadsInternal$app_productionBetaRelease(); Iterator it = this.acks.keySet().iterator(); while (it.hasNext()) { long longValue = it.next().longValue(); if (!channelsByIdInternal$app_productionBetaRelease.containsKey(Long.valueOf(longValue)) && !activeJoinedThreadsInternal$app_productionBetaRelease.containsKey(Long.valueOf(longValue))) { it.remove(); markChanged(); } } } } private final boolean shouldAck(ThreadState threadState) { if (threadState instanceof ThreadState.NotThread) { return true; } if (threadState instanceof ThreadState.Thread) { return ((ThreadState.Thread) threadState).isActiveJoined(); } throw new NoWhenBranchMatchedException(); } @StoreThread private final void updateAcks(long j, Ack ack) { if (shouldAck(getThreadStateInternal(j))) { Ack ack2 = this.acks.get(Long.valueOf(j)); if (ack2 == null || MessageUtils.compareMessages(Long.valueOf(ack2.getMessageId()), Long.valueOf(ack.getMessageId())) != 0 || (ack.isLockedAck() && !ack2.isLockedAck())) { this.acks.put(Long.valueOf(j), ack); markChanged(); } } } @StoreThread private final void updateThreadAcks(Set set, Set set2) { StoreThreadsActiveJoined.ActiveJoinedThread activeJoinedThread; if (this.stream.getGuilds$app_productionBetaRelease().getUnavailableGuildsInternal$app_productionBetaRelease().isEmpty()) { for (Number number : o0.minus((Set) set, (Iterable) set2)) { long longValue = number.longValue(); if (this.acks.containsKey(Long.valueOf(longValue)) && this.acks.remove(Long.valueOf(longValue)) != null) { markChanged(); } } } Map guildsJoinedAtInternal$app_productionBetaRelease = this.stream.getGuilds$app_productionBetaRelease().getGuildsJoinedAtInternal$app_productionBetaRelease(); Map activeJoinedThreadsInternal$app_productionBetaRelease = this.stream.getThreadsActiveJoined$app_productionBetaRelease().getActiveJoinedThreadsInternal$app_productionBetaRelease(); for (Number number2 : o0.minus((Set) set2, (Iterable) set)) { long longValue2 = number2.longValue(); if (!this.acks.containsKey(Long.valueOf(longValue2)) && (activeJoinedThread = activeJoinedThreadsInternal$app_productionBetaRelease.get(Long.valueOf(longValue2))) != null) { this.acks.put(Long.valueOf(longValue2), new Ack((ThreadUtils.getThreadAckMessageTimestamp$default(ThreadUtils.INSTANCE, activeJoinedThread.getChannel(), guildsJoinedAtInternal$app_productionBetaRelease.get(Long.valueOf(activeJoinedThread.getChannel().f())), Long.valueOf(activeJoinedThread.getJoinTimestamp().f()), null, 4, null) - 1420070400000L) << 22, false, false)); markChanged(); } } } public final void ack(long j, boolean z2, boolean z3) { this.dispatcher.schedule(new StoreMessageAck$ack$1(this, j, z2, z3)); } public final void ackGuild(Context context, long j, Function0 function0) { m.checkNotNullParameter(function0, "onSuccess"); ObservableExtensionsKt.appSubscribe$default(ObservableExtensionsKt.restSubscribeOn$default(this.restAPI.ackGuild(j), false, 1, null), context, "REST: ackGuild", (Function1) null, new StoreMessageAck$ackGuild$2(function0), (Function1) null, (Function0) null, (Function0) null, 116, (Object) null); } public final Map getAll() { return this.acksSnapshot; } @StoreThread public final Map getAllInternal() { return this.acks; } public final Ack getForChannel(long j) { return getAll().get(Long.valueOf(j)); } @StoreThread public final void handleChannelSelected() { for (Map.Entry entry : this.acks.entrySet()) { this.acks.put(Long.valueOf(entry.getKey().longValue()), Ack.copy$default(entry.getValue(), 0, false, false, 3, null)); } markChanged(); } /* JADX WARNING: Removed duplicated region for block: B:59:0x00cf A[SYNTHETIC] */ /* JADX WARNING: Removed duplicated region for block: B:62:0x007a A[SYNTHETIC] */ @StoreThread public final void handleConnectionOpen(ModelPayload modelPayload) { long j; List g; Object obj; UtcDateTime d; boolean z2; m.checkNotNullParameter(modelPayload, "payload"); ModelPayload.VersionedReadStates readState = modelPayload.getReadState(); m.checkNotNullExpressionValue(readState, "payload.readState"); if (!readState.isPartial()) { this.acks.clear(); } Map map = this.acks; ModelPayload.VersionedReadStates readState2 = modelPayload.getReadState(); m.checkNotNullExpressionValue(readState2, "payload.readState"); List entries = readState2.getEntries(); m.checkNotNullExpressionValue(entries, "payload.readState.entries"); LinkedHashMap linkedHashMap = new LinkedHashMap(a.I(entries, 10, 16)); for (ModelReadState modelReadState : entries) { Pair pair = o.to(Long.valueOf(modelReadState.getChannelId()), new Ack(modelReadState, false, false)); linkedHashMap.put(pair.getFirst(), pair.getSecond()); } map.putAll(linkedHashMap); List guilds = modelPayload.getGuilds(); m.checkNotNullExpressionValue(guilds, "payload.guilds"); for (Guild guild : guilds) { List s2 = guild.s(); if (s2 != null) { Iterator it = s2.iterator(); while (true) { if (!it.hasNext()) { obj = null; break; } obj = it.next(); if (((GuildMember) obj).j().i() == modelPayload.getMe().i()) { z2 = true; continue; } else { z2 = false; continue; } if (z2) { break; } } GuildMember guildMember = (GuildMember) obj; if (!(guildMember == null || (d = guildMember.d()) == null)) { j = d.f(); g = guild.g(); if (g == null) { ArrayList arrayList = new ArrayList(); for (Object obj2 : g) { Channel channel = (Channel) obj2; if (AnimatableValueParser.k1(channel) && !this.acks.containsKey(Long.valueOf(channel.h()))) { arrayList.add(obj2); } } ArrayList arrayList2 = new ArrayList(); for (Object obj3 : arrayList) { if ((((Channel) obj3).i() >>> 22) + 1420070400000L < j) { arrayList2.add(obj3); } } for (Channel channel2 : arrayList2) { this.acks.put(Long.valueOf(channel2.h()), new Ack(channel2.i(), false, false)); } } } } j = 0; g = guild.g(); if (g == null) { } } pruneAcks(); Subscription subscription = this.threadSyncSubscription; if (subscription != null) { subscription.unsubscribe(); } ObservableExtensionsKt.appSubscribe$default(ObservableExtensionsKt.computationLatest(observeActiveJoinedThreadIdsWithPrevious()), StoreMessageAck.class, (Context) null, new StoreMessageAck$handleConnectionOpen$3(this), (Function1) null, (Function0) null, (Function0) null, new StoreMessageAck$handleConnectionOpen$4(this), 58, (Object) null); markChanged(); } @StoreThread public final void handleGuildCreate() { pruneAcks(); } @StoreThread public final void handleMessageAck(ModelReadState modelReadState) { m.checkNotNullParameter(modelReadState, "readState"); updateAcks(modelReadState.getChannelId(), new Ack(modelReadState, false, false)); } @StoreThread public final void handleMessageCreate(Message message) { m.checkNotNullParameter(message, "message"); User e = message.e(); if (e != null && e.i() == this.stream.getUsers$app_productionBetaRelease().getMeInternal$app_productionBetaRelease().getId()) { updateAcks(message.g(), new Ack(message.o(), false, false)); } } @StoreThread public final void handlePreLogout() { Subscription subscription = this.threadSyncSubscription; if (subscription != null) { subscription.unsubscribe(); } this.threadSyncSubscription = null; } @Override // com.discord.stores.Store public void init(Context context) { m.checkNotNullParameter(context, "context"); super.init(context); Observable Y = StoreStream.Companion.getChannelsSelected().observeId().r().Y(new StoreMessageAck$init$1(this)); m.checkNotNullExpressionValue(Y, "StoreStream\n .get… 0L\n }\n }"); Observable o = ObservableExtensionsKt.computationLatest(Y).o(500, TimeUnit.MILLISECONDS); m.checkNotNullExpressionValue(o, "StoreStream\n .get…0, TimeUnit.MILLISECONDS)"); postPendingAck$default(this, getPendingAck(o, false, false), 0, 1, null); } public final void markUnread(long j, long j2) { Observable j3 = Observable.j(this.stream.getMessages$app_productionBetaRelease().observeMessagesForChannel(j).x(StoreMessageAck$markUnread$1.INSTANCE), observeThreadState(j), StoreMessageAck$markUnread$2.INSTANCE); m.checkNotNullExpressionValue(j3, "Observable\n .comb…lMessages, threadState) }"); ObservableExtensionsKt.appSubscribe$default(ObservableExtensionsKt.takeSingleUntilTimeout(j3, 10, false), StoreMessageAck.class, (Context) null, (Function1) null, (Function1) null, (Function0) null, (Function0) null, new StoreMessageAck$markUnread$3(this, j2, j), 62, (Object) null); } public final Observable> observeAll() { Observable> r = ObservationDeck.connectRx$default(this.observationDeck, new ObservationDeck.UpdateSource[]{this}, false, null, null, new StoreMessageAck$observeAll$1(this), 14, null).r(); m.checkNotNullExpressionValue(r, "observationDeck.connectR… .distinctUntilChanged()"); return r; } public final Observable observeForChannel(long j) { Observable r = ObservationDeck.connectRx$default(this.observationDeck, new ObservationDeck.UpdateSource[]{this}, false, null, null, new StoreMessageAck$observeForChannel$1(this, j), 14, null).r(); m.checkNotNullExpressionValue(r, "observationDeck.connectR… .distinctUntilChanged()"); return r; } @Override // com.discord.stores.StoreV2 public void snapshotData() { super.snapshotData(); HashMap hashMap = new HashMap(f.coerceAtLeast(this.acks.size(), 1), 0.75f); hashMap.putAll(this.acks); this.acksSnapshot = hashMap; Persister.set$default(this.acksPersister, hashMap, false, 2, null); } }