Plots2: [Discussion] Autocompletion in comment boxes

Created on 7 Jun 2021  路  12Comments  路  Source: publiclab/plots2

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

All 12 comments

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,

In the model

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

In the controller

def search_active_users
active_users = Users.search_recently_active_users
render json: active_users
end

Ensure you have registered the search_active_users action in routes.rb

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

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,

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?

@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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

first-timers[bot] picture first-timers[bot]  路  3Comments

milaaraujo picture milaaraujo  路  3Comments

first-timers[bot] picture first-timers[bot]  路  3Comments

divyabaid16 picture divyabaid16  路  3Comments

first-timers[bot] picture first-timers[bot]  路  3Comments