Tag based task aggregator

This query works great, with @Siferiax 's suggested or-join. I’d love some help getting it to do a few additional things, if possible.

  1. Amend it so that it doesn’t use the current page, but some other input (for now, explicitly naming the tag. Later, perhaps the :parent-block content of the query).

  2. Check whether the input tag/page is one of a list of master task categories (e.g., “work”, “home”, “shopping”).

  3. Aggregate the tasks
    a. If tag is a valid master task category, aggregate the tasks as in the original query
    b. Otherwise, get all tasks that would not appear in the aggregated task queries of “work”, “home”, “shopping”

Item 1 is no problem.

Item 2 I think I can figure out by getting the child blocks of a “master task categories” page. Something like:

[?h :block/name "master task categories"]
[?i :block/content "Index"]
[?child :block/parent ?i]
[?child :block/refs ?categories] 

although I’m unclear on how to compare the input tag with the ?categories variable.

And Item 3b has got me stumped. I’ve tried all kinds of not and not-join combinations in place of the or-join, but I can’t get it working correctly.

Here’s the current code, which at the moment just covers item 1:

#+BEGIN_QUERY
; see https://discuss.logseq.com/t/tag-based-task-aggregator/21378/2
{:title "tasks related to current page"
  :query [:find (pull ?b [*])
    :in $ ?inputpage
      :where
      [task ?b #{"NOW" "LATER" "DOING" "TODO"}]
      [?p :block/name ?inputpage]
      (or-join [?p ?b]
        [?b :block/path-refs ?p]
        (and 
          [?page :block/tags ?p] 
          [?b :block/path-refs ?page]
        )
      )
  ]
  :result-transform (fn [result] (sort-by (fn [r] (get r :block/content)) result))
  :group-by-page? false
  :breadcrumb-show? false
  :inputs ["work"]
}
#+END_QUERY

Any suggestions would be appreciated!