I've been looking through At.js docs https://github.com/ichord/At.js/wiki/Callbacks, https://github.com/ichord/At.js/wiki and https://github.com/publiclab/plots2/blob/main/app/assets/javascripts/atWhoAutoComplete.js. From what I understand I think the data key is what is supposed to hold the pre-fetched usernames
data: ["one", "two", "three"]
But instead of hardcoded names, the names are generated from recently active users on the site. I looked through the repo for the query that gets the recently active users on the site and I found this https://github.com/publiclab/plots2/blob/700fdf41a08d52bfd51d7774902dc9217ed225b5/app/controllers/users_controller.rb#L137-L142
I'm a little confused on how to display the results of that query in json format in https://github.com/publiclab/plots2/blob/700fdf41a08d52bfd51d7774902dc9217ed225b5/app/api/srch/search.rb
@jywarren @cesswairimu @RuthNjeri
I've been playing around with this a bit and I think I understand it. I created a new method in search_service.rb with the recently active query from above.
def search_recently_active_users(limit = 10, order = 'last_updated DESC')
User.select('rusers.*, MAX(node_revisions.status), MAX(node_revisions.timestamp) AS last_updated')
.joins("INNER JOIN `node_revisions` ON `node_revisions`.`uid` = `rusers`.`id` ")
.where("node_revisions.status = 1")
.group('rusers.id')
.order(order)
end
Now the issue is with the query string. The SearchCriteria class has a valid? method that checks if a query is present.
https://github.com/publiclab/plots2/blob/700fdf41a08d52bfd51d7774902dc9217ed225b5/app/services/search_criteria.rb#L20-L22
The recently_active_users query does not require a query string so it returns an error that says {"error":"query is missing"}. I'm not sure how to move forward.
@RuthNjeri
Hi Tilda, I think from the tests https://github.com/publiclab/plots2/blob/700fdf41a08d52bfd51d7774902dc9217ed225b5/test/unit/api/search_service_test.rb#L9-L11 the query is related to the params...
From the discussion we had yesterday, I think all you need to do is make the method you have written above search_recently_active_users an action in the user search controller, or in the relevant controller, then record it as a route in the routes.rb file.
The query can be included in the users.rb file, then accessed in the action within the controller and the results rendered in json.
For example,
def search_recently_active_users(limit = 10, order = 'last_updated DESC')
User.select('rusers.*, MAX(node_revisions.status), MAX(node_revisions.timestamp) AS last_updated')
.joins("INNER JOIN node_revisions ON node_revisions.uid = rusers.id ")
.where("node_revisions.status = 1")
.group('rusers.id')
.order(order)
end
def search_active_users
active_users = Users.search_recently_active_users
render json: active_users
end
get 'users/active' => 'controller_name#search_active_users'
Alternatively, you could write the method search_recently_active_users inside the services and call it similar to the search controller file https://github.com/publiclab/plots2/blob/700fdf41a08d52bfd51d7774902dc9217ed225b5/app/controllers/search_controller.rb#L36
Let me know if it makes sense
Also, I am not sure why the search_service tries to access the valid method from the search_criteria, my understanding from the use of the search_service shared above, is that it does not rely on the search_criteria
@RuthNjeri To execute a search this method is called https://github.com/publiclab/plots2/blob/700fdf41a08d52bfd51d7774902dc9217ed225b5/app/api/srch/search.rb#L101
https://github.com/publiclab/plots2/blob/700fdf41a08d52bfd51d7774902dc9217ed225b5/app/api/srch/search.rb#L399-L401
which in turn calls the valid? method
https://github.com/publiclab/plots2/blob/700fdf41a08d52bfd51d7774902dc9217ed225b5/app/api/srch/search.rb#L403
From the discussion we had yesterday, I think all you need to do is make the method you have written above
search_recently_active_usersan action in the user search controller, or in the relevant controller,
I think the controller would be user_controller.rb and the model would be user.rb
Thanks @RuthNjeri it makes perfect sense.
Just to note that I'm not 100% sure all the scenarios this API is used for, so it may be important to preserve the current behavior and switch to a different one only in the prese of a specific URL parameter. But I'm not quite sure! Can anyone think of another use? I guess regular user search on the sidebar of the search page maybe?
But I guess that's not going to use recently active, just general user search. So maybe we are OK.
@jywarren So, @RuthNjeri's suggestion is okay?
@RuthNjeri To execute a search this method is called
which in turn calls the
valid?method
https://github.com/publiclab/plots2/blob/700fdf41a08d52bfd51d7774902dc9217ed225b5/app/api/srch/search.rb#L403
Thanks @TildaDares, and how is the code above related to the search_service?
@jywarren maybe in the case for research notes, wikis, and questions?
@RuthNjeri I'm not sure how but maybe you'll understand it better https://github.com/publiclab/plots2/blob/main/doc/API.md#api-code
I think this all looks great and so does your PR - i'll make comments there! I think you were way ahead of what i noted in my comment. Great to see it!