.class public final Ldev/maelstrom/ykit/enhancedblock/EnhancedBlockFilter;
.super Ljava/lang/Object;
.source "SourceFile"

# direct methods
.method public static final filterPost(Lcom/tumblr/rumblr/model/post/Post;)Lcom/tumblr/rumblr/model/post/Post;
    .locals 1

    # Try enhanced block filter
    invoke-static {p0}, Ldev/maelstrom/ykit/PostJsonAdapterShim;->filterPostEnhancedBlock(Lcom/tumblr/rumblr/model/post/Post;)Lcom/tumblr/rumblr/model/post/Post;
    move-result-object p0

    # If null, finish
    if-eqz p0, :returnNormal

    # Try filtered post filter

    # Check to see if it's enabled
    sget-boolean v0, Ldev/maelstrom/ykit/settings/YkitCurrentSettings;->hideFilteredPosts:Z
    if-eqz v0, :returnNormal

    # Get filtered field    
    invoke-virtual {p0}, Lcom/tumblr/rumblr/model/post/Post;->X()Lcom/tumblr/rumblr/model/post/asset/Filtered;
    move-result-object v0

    # If v0 is null, then continue as normal. Otherwise, return null
    if-eqz v0, :returnNormal

    const p0, 0x0

    :returnNormal
    return-object p0
.end method


.method private static final filterPostEnhancedBlock(Lcom/tumblr/rumblr/model/post/Post;)Lcom/tumblr/rumblr/model/post/Post;
    .locals 4

    # Check that EnhancedBlock is enabled, skip if otherwise
    sget-boolean v0, Ldev/maelstrom/ykit/settings/YkitCurrentSettings;->enhancedBlockEnabled:Z
    if-eqz v0, :returnNormal

    # Get the reblog trail
    invoke-virtual {p0}, Lcom/tumblr/rumblr/model/post/Post;->s0()Ljava/util/List; # getReblogTrail()
    move-result-object v1

    # Iterate over reblog trail
    invoke-interface {v1}, Ljava/util/Collection;->iterator()Ljava/util/Iterator;
    move-result-object v2

    :loop
    invoke-interface {v2}, Ljava/util/Iterator;->hasNext()Z
    move-result v0
    if-eqz v0, :loopEnd

    # Get next reblog trail
    invoke-interface {v2}, Ljava/util/Iterator;->next()Ljava/lang/Object;
    move-result-object v3
    check-cast v3, Lcom/tumblr/rumblr/model/post/ReblogTrail;

    # Get the blog
    invoke-virtual {v3}, Lcom/tumblr/rumblr/model/post/ReblogTrail;->c()Lcom/tumblr/rumblr/model/blog/ShortBlogInfoReblogTrail; # getShortBlogInfo()
    move-result-object v3

    # If the blog info is null, then skip
    if-eqz v3, :loop # continue

    # Get the blog's uuid
    invoke-virtual {v3}, Lcom/tumblr/rumblr/model/blog/ShortBlogInfoReblogTrail;->X()Ljava/lang/String; # getUniqueId()
    move-result-object v3

    # Check if the uuid is blocked
    invoke-static {v3}, Ldev/maelstrom/ykit/PostJsonAdapterShim;->isBlocked(Ljava/lang/String;)Z
    move-result v0

    # If it matches, then return null
    if-nez v0, :returnNull

    goto :loop
    :loopEnd
    ## End
    goto :returnNormal

    :returnNull
    const p0, 0x0 # Set timeline object to null

    :returnNormal
    return-object p0
.end method

# isBlocked(String uuid) -> boolean
.method private static final isBlocked(Ljava/lang/String;)Z
    .locals 2

    # Fetch blocklist if not already cached
    sget-object v1, Ldev/maelstrom/ykit/PostJsonAdapterShim;->cachedBlocks:Ljava/util/List;
    if-nez v1, :skipFetchBlockList

    invoke-static {}, Ldev/maelstrom/ykit/PostJsonAdapterShim;->fetchBlockList()Ljava/util/List;
    move-result-object v1

    :skipFetchBlockList

    invoke-interface {v1, p0}, Ljava/util/Collection;->contains(Ljava/lang/Object;)Z
    move-result v0
    
    return v0
.end method

# fetchBlockList() -> List<String uuid>
.method public static final fetchBlockList()Ljava/util/List;
    .locals 2

    # Get current blog name
    invoke-static {}, Ldev/maelstrom/ykit/CoreAppShim;->getCurrentBlogName()Ljava/lang/String;
    move-result-object v0

    # Append tumblr extension for host name
    const-string v1, ".tumblr.com"
    invoke-virtual {v0, v1}, Ljava/lang/String;->concat(Ljava/lang/String;)Ljava/lang/String;
    move-result-object v0

    # Fetch block list
    invoke-static {v0}, Ldev/maelstrom/ykit/PostJsonAdapterShim;->fetchBlockList(Ljava/lang/String;)Ljava/util/List;
    move-result-object v0

    return-object v0
.end method

# Called by fetchBlockList above and also BlogChangedListener
# fetchBlockList(String blogHostName) -> List<String uuid>
.method public static final fetchBlockList(Ljava/lang/String;)Ljava/util/List;
    .locals 4

    # Get TumblrService
    invoke-static {}, Ldev/maelstrom/ykit/CoreAppShim;->getTumblrService()Lcom/tumblr/rumblr/TumblrService;
    move-result-object v0

    # Build blocklist query
    #const-string v1, "jingle--balls.tumblr.com" # TODO:
    invoke-interface {v0, p0}, Lcom/tumblr/rumblr/TumblrService;->blocks(Ljava/lang/String;)Lretrofit2/Call;
    move-result-object v0

    # Execute
    invoke-interface {v0}, Lretrofit2/Call;->execute()Lretrofit2/Response;
    move-result-object v0

    # Get BlocksResponse
    invoke-virtual {v0}, Lretrofit2/Response;->body()Ljava/lang/Object;
    move-result-object v0
    check-cast v0, Lcom/tumblr/rumblr/response/ApiResponse;

    invoke-virtual {v0}, Lcom/tumblr/rumblr/response/ApiResponse;->getResponse()Ljava/lang/Object;
    move-result-object v0
    check-cast v0, Lcom/tumblr/rumblr/response/BlocksResponse;
    
    # Get list of blocked blogs
    invoke-virtual {v0}, Lcom/tumblr/rumblr/response/BlocksResponse;->getBlocks()Ljava/util/List;
    move-result-object v0

    # Iterate over each blog and extract its UUID, put that into a new list
    new-instance v1, Ljava/util/ArrayList;
    invoke-interface {v0}, Ljava/util/Collection;->size()I
    move-result v2
    invoke-direct {v1, v2}, Ljava/util/ArrayList;-><init>(I)V # Pre-allocate with enough items

    # Get the iterator
    invoke-interface {v0}, Ljava/util/Collection;->iterator()Ljava/util/Iterator;
    move-result-object v0

    :loop
    invoke-interface {v0}, Ljava/util/Iterator;->hasNext()Z
    move-result v2
    if-eqz v2, :loopEnd

    # Get next blocked blog
    invoke-interface {v0}, Ljava/util/Iterator;->next()Ljava/lang/Object;
    move-result-object v3
    check-cast v3, Lcom/tumblr/rumblr/model/blog/ShortBlogInfo;

    # Get the blog's unique id
    invoke-virtual {v3}, Lcom/tumblr/rumblr/model/blog/ShortBlogInfo;->X()Ljava/lang/String; # getUniqueId()
    move-result-object v3

    # Add it to the list
    invoke-interface {v1, v3}, Ljava/util/Collection;->add(Ljava/lang/Object;)Z

    goto :loop
    :loopEnd
    ## End

    # Cache results for further calls
    sput-object v1, Ldev/maelstrom/ykit/PostJsonAdapterShim;->cachedBlocks:Ljava/util/List;

    return-object v1
.end method

.method static constructor <clinit>()V
    .locals 2

    # Get Context
    invoke-static {}, Lcom/tumblr/CoreApp;->M()Landroid/content/Context;
    move-result-object v0

    # Create event listener
    new-instance v1, Ldev/maelstrom/ykit/PostJsonAdapterShim$BlogChangedListener;
    invoke-direct {v1}, Ldev/maelstrom/ykit/PostJsonAdapterShim$BlogChangedListener;-><init>()V

    # Add event listener
    invoke-static {v0, v1}, Lee0/p;->i(Landroid/content/Context;Landroid/content/BroadcastReceiver;)V # BlogChangedEvent.listen

    return-void
.end method