Can I create a plugin that checks users and grant access to group based on activity?

What do you mean by group query? Isn’ t get https://meta.discourse.org/groups/search.json what you want?