How to make a topic filter based on current user group and topic location

Hi,

I’m working on a plugin that filters all topics based on where the topic was posted from and what group the user viewing the topic is in. The use case here is having a support team in different countries handling questions coming out of their own locations.

I’m a newbie discourse plugin creator still learning how to do this. The topic location will be handled by the locations plugin here Locations Plugin

and I will add the users that need to have this filtered view to a group named something like NA_queue or EU_queue.

the plugin will check if the user is logged in and if the primary group is set. if it is, it will filter all topics to match the queue that the current user is a part of.

im thinking this might be done with something similar to the following code:
after_initialize do
require_dependency ‘topic_query’

  # if logged in and with primary group set

  # need to get user's primary group
  # need to get topic location

  TopicQuery.add_custom_filter(:location) do |results, topic_query|
    filter = "SOME SQL QUERY HERE"
    results = results.joins("{filter}")

    results
  end

Is this a good approach to my problem?
Also, does anyone have any suggestions on what the SQL would be or how to get those two commented pieces of information?

1 Like

I don’t have permissions to update my question so i’ll just post this here. Update:

I’m getting caught up in how would I pass the current user from the browser side js to my code running on the server side. There’s probably a guide out there that explains this but i have not stumbled onto it yet :frowning:

the following code works great as a filter on post location
after_initialize do

# gets current user passed in somehow from client side
def self.filter_on_user

  require_dependency 'topic_query'

  # if current user has a primary group, set the country code
  # for now, asume country code is US for usa
 countrycode = "us" 

  #filter on that country code for all topic queries on site for that user.
  TopicQuery.add_custom_filter(:location) do |results, topic_query|

    results = results.joins("JOIN topic_custom_fields tc ON
                             topics.id = tc.topic_id AND
                             tc.name = 'location' AND
                             tc.value LIKE '%countrycode\":\"#{countrycode}%'
                             ")
 results
  end
end

filter_on_user

end