2021-06-27 20:44:35 +00:00
package com.discord.stores ;
import c.d.b.a.a ;
import com.discord.api.message.MessageReference ;
import com.discord.models.domain.ModelMessageDelete ;
2021-07-03 23:36:06 +00:00
import com.discord.models.message.Message ;
2021-06-27 20:44:35 +00:00
import com.discord.stores.updates.ObservationDeck ;
import com.discord.stores.updates.ObservationDeckProvider ;
import d0.d0.f ;
import d0.t.g0 ;
import d0.t.h0 ;
import d0.t.m0 ;
import d0.t.n0 ;
import d0.t.o ;
import d0.t.u ;
import d0.z.d.m ;
import java.util.ArrayList ;
import java.util.Collection ;
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 kotlin.jvm.internal.DefaultConstructorMarker ;
import rx.Observable ;
/* compiled from: StoreMessageReplies.kt */
public final class StoreMessageReplies extends StoreV2 {
public static final Companion Companion = new Companion ( null ) ;
private static final Map < Long , MessageState > NO_RESULTS = h0 . emptyMap ( ) ;
private final HashMap < Long , Set < Long > > channelMap ;
private final Dispatcher dispatcher ;
private final ObservationDeck observationDeck ;
private Map < Long , ? extends Map < Long , ? extends MessageState > > repliedChannelMessagesCacheSnapshot ;
private final MessageCache < Long , MessageState > repliedMessagesCache ;
private Map < Long , ? extends MessageState > repliedMessagesCacheSnapshot ;
private final StoreMessages storeMessages ;
/* compiled from: StoreMessageReplies.kt */
public static final class Companion {
private Companion ( ) {
}
public /* synthetic */ Companion ( DefaultConstructorMarker defaultConstructorMarker ) {
this ( ) ;
}
}
/* compiled from: StoreMessageReplies.kt */
public static final class MessageCache < K , V > extends LinkedHashMap < K , V > {
@Override // java.util.LinkedHashMap, java.util.AbstractMap, java.util.Map, java.util.HashMap
public final /* bridge */ Set < Map . Entry < K , V > > entrySet ( ) {
return getEntries ( ) ;
}
public /* bridge */ Set getEntries ( ) {
return super . entrySet ( ) ;
}
public /* bridge */ Set getKeys ( ) {
return super . keySet ( ) ;
}
public /* bridge */ int getSize ( ) {
return super . size ( ) ;
}
public /* bridge */ Collection getValues ( ) {
return super . values ( ) ;
}
@Override // java.util.LinkedHashMap, java.util.AbstractMap, java.util.Map, java.util.HashMap
public final /* bridge */ Set < K > keySet ( ) {
return getKeys ( ) ;
}
@Override // java.util.LinkedHashMap
public boolean removeEldestEntry ( Map . Entry < K , V > entry ) {
return size ( ) > 1000 ;
}
@Override // java.util.AbstractMap, java.util.Map, java.util.HashMap
public final /* bridge */ int size ( ) {
return getSize ( ) ;
}
@Override // java.util.LinkedHashMap, java.util.AbstractMap, java.util.Map, java.util.HashMap
public final /* bridge */ Collection < V > values ( ) {
return getValues ( ) ;
}
}
/* compiled from: StoreMessageReplies.kt */
public static abstract class MessageState {
/* compiled from: StoreMessageReplies.kt */
public static final class Deleted extends MessageState {
public static final Deleted INSTANCE = new Deleted ( ) ;
private Deleted ( ) {
super ( null ) ;
}
}
/* compiled from: StoreMessageReplies.kt */
public static final class Loaded extends MessageState {
2021-06-27 20:53:42 +00:00
private final Message message ;
2021-06-27 20:44:35 +00:00
/* JADX INFO: super call moved to the top of the method (can break code semantics) */
2021-06-27 20:53:42 +00:00
public Loaded ( Message message ) {
2021-06-27 20:44:35 +00:00
super ( null ) ;
2021-06-27 20:53:42 +00:00
m . checkNotNullParameter ( message , " message " ) ;
this . message = message ;
2021-06-27 20:44:35 +00:00
}
2021-06-27 20:53:42 +00:00
public static /* synthetic */ Loaded copy$default ( Loaded loaded , Message message , int i , Object obj ) {
2021-06-27 20:44:35 +00:00
if ( ( i & 1 ) ! = 0 ) {
2021-06-27 20:53:42 +00:00
message = loaded . message ;
2021-06-27 20:44:35 +00:00
}
2021-06-27 20:53:42 +00:00
return loaded . copy ( message ) ;
2021-06-27 20:44:35 +00:00
}
2021-06-27 20:53:42 +00:00
public final Message component1 ( ) {
return this . message ;
2021-06-27 20:44:35 +00:00
}
2021-06-27 20:53:42 +00:00
public final Loaded copy ( Message message ) {
m . checkNotNullParameter ( message , " message " ) ;
return new Loaded ( message ) ;
2021-06-27 20:44:35 +00:00
}
public boolean equals ( Object obj ) {
if ( this ! = obj ) {
2021-06-27 20:53:42 +00:00
return ( obj instanceof Loaded ) & & m . areEqual ( this . message , ( ( Loaded ) obj ) . message ) ;
2021-06-27 20:44:35 +00:00
}
return true ;
}
2021-06-27 20:53:42 +00:00
public final Message getMessage ( ) {
return this . message ;
2021-06-27 20:44:35 +00:00
}
public int hashCode ( ) {
2021-06-27 20:53:42 +00:00
Message message = this . message ;
if ( message ! = null ) {
return message . hashCode ( ) ;
2021-06-27 20:44:35 +00:00
}
return 0 ;
}
public String toString ( ) {
2021-06-27 20:53:42 +00:00
StringBuilder L = a . L ( " Loaded(message= " ) ;
L . append ( this . message ) ;
2021-06-27 20:44:35 +00:00
L . append ( " ) " ) ;
return L . toString ( ) ;
}
}
/* compiled from: StoreMessageReplies.kt */
public static final class Unloaded extends MessageState {
public static final Unloaded INSTANCE = new Unloaded ( ) ;
private Unloaded ( ) {
super ( null ) ;
}
}
private MessageState ( ) {
}
public /* synthetic */ MessageState ( DefaultConstructorMarker defaultConstructorMarker ) {
this ( ) ;
}
}
public StoreMessageReplies ( Dispatcher dispatcher , StoreMessages storeMessages , ObservationDeck observationDeck ) {
m . checkNotNullParameter ( dispatcher , " dispatcher " ) ;
m . checkNotNullParameter ( storeMessages , " storeMessages " ) ;
m . checkNotNullParameter ( observationDeck , " observationDeck " ) ;
this . dispatcher = dispatcher ;
this . storeMessages = storeMessages ;
this . observationDeck = observationDeck ;
this . repliedMessagesCache = new MessageCache < > ( ) ;
this . channelMap = new HashMap < > ( ) ;
this . repliedChannelMessagesCacheSnapshot = h0 . emptyMap ( ) ;
this . repliedMessagesCacheSnapshot = h0 . emptyMap ( ) ;
}
/* JADX INFO: this call moved to the top of the method (can break code semantics) */
public /* synthetic */ StoreMessageReplies ( Dispatcher dispatcher , StoreMessages storeMessages , ObservationDeck observationDeck , int i , DefaultConstructorMarker defaultConstructorMarker ) {
this ( dispatcher , storeMessages , ( i & 4 ) ! = 0 ? ObservationDeckProvider . get ( ) : observationDeck ) ;
}
public static final /* synthetic */ Map access$getCachedChannelMessages ( StoreMessageReplies storeMessageReplies , long j ) {
return storeMessageReplies . getCachedChannelMessages ( j ) ;
}
@StoreThread
private final boolean deleteMessage ( long j , long j2 ) {
if ( ! this . repliedMessagesCache . containsKey ( Long . valueOf ( j ) ) ) {
return false ;
}
updateCache ( j , j2 , MessageState . Deleted . INSTANCE ) ;
return true ;
}
private final Map < Long , MessageState > getCachedChannelMessages ( long j ) {
Map < Long , MessageState > map = ( Map ) this . repliedChannelMessagesCacheSnapshot . get ( Long . valueOf ( j ) ) ;
return map ! = null ? map : NO_RESULTS ;
}
2021-07-03 23:36:06 +00:00
@StoreThread
private final boolean processMessage ( com . discord . api . message . Message message ) {
return processMessage ( new Message ( message ) ) ;
}
2021-06-27 20:44:35 +00:00
@StoreThread
2021-06-27 20:53:42 +00:00
private final boolean processMessage ( Message message ) {
2021-06-27 20:44:35 +00:00
boolean z2 ;
2021-07-03 23:36:06 +00:00
if ( this . repliedMessagesCache . containsKey ( Long . valueOf ( message . getId ( ) ) ) ) {
updateCache ( message . getId ( ) , message . getChannelId ( ) , new MessageState . Loaded ( message ) ) ;
2021-06-27 20:44:35 +00:00
z2 = true ;
} else {
z2 = false ;
}
2021-07-03 23:36:06 +00:00
if ( ! message . isEmbeddedMessageType ( ) ) {
2021-06-27 20:44:35 +00:00
return z2 ;
}
2021-07-03 23:36:06 +00:00
MessageReference messageReference = message . getMessageReference ( ) ;
if ( messageReference = = null ) {
2021-06-27 20:44:35 +00:00
return z2 ;
}
2021-07-03 23:36:06 +00:00
com . discord . api . message . Message referencedMessage = message . getReferencedMessage ( ) ;
Long a = messageReference . a ( ) ;
Long c2 = messageReference . c ( ) ;
if ( referencedMessage ! = null ) {
processReferencedMessage ( new Message ( referencedMessage ) ) ;
2021-06-27 20:44:35 +00:00
return true ;
} else if ( a = = null | | c2 = = null ) {
return true ;
} else {
2021-06-27 20:53:42 +00:00
Message message2 = this . storeMessages . getMessage ( a . longValue ( ) , c2 . longValue ( ) ) ;
if ( message2 ! = null ) {
processReferencedMessage ( message2 ) ;
2021-06-27 20:44:35 +00:00
return true ;
}
updateCache ( c2 . longValue ( ) , a . longValue ( ) , MessageState . Unloaded . INSTANCE ) ;
return true ;
}
}
2021-06-27 20:53:42 +00:00
private final void processReferencedMessage ( Message message ) {
2021-07-03 23:36:06 +00:00
updateCache ( message . getId ( ) , message . getChannelId ( ) , new MessageState . Loaded ( message ) ) ;
Integer type = message . getType ( ) ;
if ( type ! = null & & type . intValue ( ) = = 19 ) {
2021-06-27 20:53:42 +00:00
processMessage ( message ) ;
2021-06-27 20:44:35 +00:00
}
}
private final void snapShotAllMessages ( ) {
this . repliedMessagesCacheSnapshot = new HashMap ( this . repliedMessagesCache ) ;
}
@StoreThread
private final void snapShotChannelMessages ( ) {
HashMap < Long , Set < Long > > hashMap = this . channelMap ;
LinkedHashMap linkedHashMap = new LinkedHashMap ( g0 . mapCapacity ( hashMap . size ( ) ) ) ;
Iterator < T > it = hashMap . entrySet ( ) . iterator ( ) ;
while ( it . hasNext ( ) ) {
Map . Entry entry = ( Map . Entry ) it . next ( ) ;
Object key = entry . getKey ( ) ;
Set set = ( Set ) entry . getValue ( ) ;
LinkedHashMap linkedHashMap2 = new LinkedHashMap ( f . coerceAtLeast ( g0 . mapCapacity ( o . collectionSizeOrDefault ( set , 10 ) ) , 16 ) ) ;
for ( Object obj : set ) {
Object obj2 = ( MessageState ) this . repliedMessagesCache . get ( Long . valueOf ( ( ( Number ) obj ) . longValue ( ) ) ) ;
if ( obj2 = = null ) {
obj2 = MessageState . Unloaded . INSTANCE ;
}
linkedHashMap2 . put ( obj , obj2 ) ;
}
linkedHashMap . put ( key , linkedHashMap2 ) ;
}
this . repliedChannelMessagesCacheSnapshot = linkedHashMap ;
}
@StoreThread
private final void updateCache ( long j , long j2 , MessageState messageState ) {
this . repliedMessagesCache . put ( Long . valueOf ( j ) , messageState ) ;
HashMap < Long , Set < Long > > hashMap = this . channelMap ;
Long valueOf = Long . valueOf ( j2 ) ;
Set of = m0 . setOf ( Long . valueOf ( j ) ) ;
Set < Long > set = this . channelMap . get ( Long . valueOf ( j2 ) ) ;
if ( set = = null ) {
set = n0 . emptySet ( ) ;
}
hashMap . put ( valueOf , u . union ( of , set ) ) ;
}
/* JADX DEBUG: Type inference failed for r0v0. Raw type applied. Possible types: java.util.Map<java.lang.Long, ? extends com.discord.stores.StoreMessageReplies$MessageState>, java.util.Map<java.lang.Long, com.discord.stores.StoreMessageReplies$MessageState> */
public final Map < Long , MessageState > getAllMessageReferences ( ) {
return this . repliedMessagesCacheSnapshot ;
}
@StoreThread
2021-06-27 20:53:42 +00:00
public final void handleLoadMessages ( Collection < Message > collection ) {
2021-06-27 20:44:35 +00:00
m . checkNotNullParameter ( collection , " messages " ) ;
ArrayList arrayList = new ArrayList ( o . collectionSizeOrDefault ( collection , 10 ) ) ;
2021-06-27 20:53:42 +00:00
for ( Message message : collection ) {
arrayList . add ( Boolean . valueOf ( processMessage ( message ) ) ) ;
2021-06-27 20:44:35 +00:00
}
if ( arrayList . contains ( Boolean . TRUE ) ) {
markChanged ( ) ;
}
}
@StoreThread
2021-07-03 23:36:06 +00:00
public final void handleMessageCreate ( com . discord . api . message . Message message ) {
2021-06-27 20:53:42 +00:00
m . checkNotNullParameter ( message , " message " ) ;
if ( processMessage ( message ) ) {
2021-06-27 20:44:35 +00:00
markChanged ( ) ;
}
}
@StoreThread
public final void handleMessageDelete ( ModelMessageDelete modelMessageDelete ) {
boolean z2 ;
m . checkNotNullParameter ( modelMessageDelete , " messageDeleteBulk " ) ;
List < Long > messageIds = modelMessageDelete . getMessageIds ( ) ;
m . checkNotNullExpressionValue ( messageIds , " messageDeleteBulk.messageIds " ) ;
Iterator < T > it = messageIds . iterator ( ) ;
loop0 :
while ( true ) {
z2 = false ;
while ( true ) {
if ( ! it . hasNext ( ) ) {
break loop0 ;
}
Long l = ( Long ) it . next ( ) ;
m . checkNotNullExpressionValue ( l , " messageId " ) ;
if ( deleteMessage ( l . longValue ( ) , modelMessageDelete . getChannelId ( ) ) | | z2 ) {
z2 = true ;
}
}
}
if ( z2 ) {
markChanged ( ) ;
}
}
@StoreThread
2021-07-03 23:36:06 +00:00
public final void handleMessageUpdate ( com . discord . api . message . Message message ) {
2021-06-27 20:44:35 +00:00
MessageState messageState ;
2021-06-27 20:53:42 +00:00
m . checkNotNullParameter ( message , " message " ) ;
2021-07-03 23:36:06 +00:00
if ( this . repliedMessagesCache . containsKey ( Long . valueOf ( message . o ( ) ) ) & & ( messageState = this . repliedMessagesCache . get ( Long . valueOf ( message . o ( ) ) ) ) ! = null & & ( messageState instanceof MessageState . Loaded ) ) {
Message merge = ( ( MessageState . Loaded ) messageState ) . getMessage ( ) . merge ( message ) ;
updateCache ( merge . getId ( ) , merge . getChannelId ( ) , new MessageState . Loaded ( merge ) ) ;
2021-06-27 20:44:35 +00:00
markChanged ( ) ;
}
}
public final Observable < Map < Long , MessageState > > observeMessageReferencesForChannel ( long j ) {
2021-07-03 23:36:06 +00:00
Observable < Map < Long , MessageState > > r = ObservationDeck . connectRx$default ( this . observationDeck , new ObservationDeck . UpdateSource [ ] { this } , false , null , null , new StoreMessageReplies$observeMessageReferencesForChannel$1 ( this , j ) , 14 , null ) . r ( ) ;
m . checkNotNullExpressionValue ( r , " observationDeck \ n … .distinctUntilChanged() " ) ;
return r ;
2021-06-27 20:44:35 +00:00
}
@Override // com.discord.stores.StoreV2
public void snapshotData ( ) {
super . snapshotData ( ) ;
snapShotChannelMessages ( ) ;
snapShotAllMessages ( ) ;
}
}