Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discussion] Autocompletion in comment boxes #9755

Closed
TildaDares opened this issue Jun 7, 2021 · 12 comments · Fixed by #9773
Closed

[Discussion] Autocompletion in comment boxes #9755

TildaDares opened this issue Jun 7, 2021 · 12 comments · Fixed by #9773

Comments

@TildaDares
Copy link
Member

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

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_string)
.page(params[:page])

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

@TildaDares
Copy link
Member Author

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.

def valid?
(!query.nil? && query != 0) || !coordinates.nil?
end

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

@RuthNjeri
Copy link
Contributor

Hi Tilda, I think from the tests

search_criteria = SearchCriteria.new(params)
result = SearchService.new.search_profiles(search_criteria)
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

@tag_profiles = SearchService.new.find_users(params[:query], 15, 'tag').paginate(page: params[:page], per_page: 20)

Let me know if it makes sense

@RuthNjeri
Copy link
Contributor

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

@TildaDares
Copy link
Member Author

@RuthNjeri To execute a search this method is called

results = Search.execute(:profiles, params)

def self.execute(endpoint, params)
search_type = endpoint
search_criteria = SearchCriteria.new(params)

which in turn calls the valid? method

if search_criteria.valid?

@TildaDares
Copy link
Member Author

TildaDares commented Jun 9, 2021

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.

@jywarren
Copy link
Member

jywarren commented Jun 9, 2021

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?

@jywarren
Copy link
Member

jywarren commented Jun 9, 2021

But I guess that's not going to use recently active, just general user search. So maybe we are OK.

@TildaDares
Copy link
Member Author

@jywarren So, @RuthNjeri's suggestion is okay?

@RuthNjeri
Copy link
Contributor

@RuthNjeri To execute a search this method is called

results = Search.execute(:profiles, params)

def self.execute(endpoint, params)
search_type = endpoint
search_criteria = SearchCriteria.new(params)

which in turn calls the valid? method

if search_criteria.valid?

Thanks @TildaDares, and how is the code above related to the search_service?

@RuthNjeri
Copy link
Contributor

@jywarren maybe in the case for research notes, wikis, and questions?

@TildaDares
Copy link
Member Author

@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

@jywarren
Copy link
Member

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants