publiclab / mapknitter

Upload your own aerial images, position (rubbersheet) them in a web interface over existing map data, and share via web or composite and export for print.
http://mapknitter.org
GNU General Public License v3.0
267 stars 210 forks source link

Calling API to retrieve user profile location map from PublicLab.org #626

Open rarrunategu1 opened 5 years ago

rarrunategu1 commented 5 years ago

What happened just before the problem occurred

I'm going through PublicLab's API documentation in order to pull a user profile "location map" to display on their mapknitter profile. Looking at the documentation, I can't seem to find what will help achieve that. I'm not returning any profile information when I search for a username that exists. Am I missing something? Will the API not return profile information? Any help is greatly appreciated. Even in the web interface ("https://publiclab.org/api/docs"), I get the same response which you'll see below: image

Relevant URLs

https://github.com/publiclab/plots2/blob/master/doc/API.md

PublicLab.org username

parker_rose

Browser, version, and operating system

For bug reports, fill out the above template; for feature requests, you can delete the template.

jywarren commented 5 years ago

Hi! Hoping perhaps @milaaraujo @stefannibrasil @cesswairimu could offer input on API, and @sagarpreet-chadha on display in a leaflet map? And @SidharthBansal I think we can skip direct referring to a PL resource via ruby because if we have the username and uid we can just point a JavaScript function directly at the remote Json from the API and display it in a map, but just pinging you about mk/PL links! Thanks all!

jywarren commented 5 years ago

It's possible we may need to make a single profile API endpoint but I thought we should be able to get it the way our people map works!

sagarpreet-chadha commented 5 years ago

Hey, So if the location is shared on publiclab.org , do you want to show the map automatically on mapknitter profile page ?

Currently i think there is no API that gives the location of one user via username or uid . We have a /peoples API though that gives the location of all users (we can after calling this API , search for xyz person's location if it exists) .

@milaaraujo , @cesswairimu and @stefannibrasil have done some great job in recent past to update the API , hopefully they will provide better insights regarding this .

Also i think it would be great if we can provide a "ADD LOCATION" button on mapknitter if the location of a user does not exists , that will send POST request to publiclab API (via LBL model) . So in a way it would be a 2 way tunnel between mk and plots2 .

Regarding showing on map , we can simply use AJAX call to the API and show dynamically a marker using the conventional Leaflet syntax .

I hope it helps , Thank you :)

rarrunategu1 commented 5 years ago

Thank you @sagarpreet-chadha. That's very helpful information. I will look into the /peoples API.

rarrunategu1 commented 5 years ago

@sagarpreet-chadha where can I find the /peoples API? I can't find it when I search through PublicLab.

sagarpreet-chadha commented 5 years ago

Okay so it seems we have depreciated the /people API that gave all users location information . We now use this : https://github.com/publiclab/plots2/blob/439d3ce4feed3fdef5323b679ed2b04c8bc93179/app/views/map/_peopleLeaflet.html.erb#L59

As you can see we have to provide two diagonally opposite coordinates in URL which forms a bounding rectangle and all locations within that rectangle are returned . (We can provide diagonally opposite coordinates of the map universe , but it seems like an inefficient way , what do you think ?)

I guess let's make a new API in publiclab that simply returns one users API .Later we can make that POST request API to add location as well .

What do you think @rarrunategu1 , @jywarren ?

jywarren commented 5 years ago

https://publiclab.org/api/srch/profiles?query=warren should return my username since it's an exact match but it doesn't. If we can ensure the API does return exact matches we can use this endpoint to get lat/Lon for sure. How's that?

jywarren commented 5 years ago

@rarrunategu1 have you added your location on PublicLab.org? If you do I hope your lat/Lon would appear with such a query.

jywarren commented 5 years ago

We may have to modify/improve this function to ensure perfect matches are included!

https://github.com/publiclab/plots2/blob/439d3ce4feed3fdef5323b679ed2b04c8bc93179/app/services/search_service.rb#L233

rarrunategu1 commented 5 years ago

@jywarren I do have my location added which shows with the map on my profile and the following below my photo: image

jywarren commented 5 years ago

OK, then indeed we have a bug where it's not showing the location for this API endpoint. We'll have to solve this in addition to ensuring it returns exact matches for the query!

https://publiclab.org/api/srch/profiles?query=parker_rose

Let's break this into 2 issues then - one for fixing the inclusion of API, and the other for ensuring exact matches are included.

This is the line where the query for users is happening in the API:

https://github.com/publiclab/plots2/blob/439d3ce4feed3fdef5323b679ed2b04c8bc93179/app/services/search_service.rb#L239

However, that leads to a model method in user.rb here:

https://github.com/publiclab/plots2/blob/439d3ce4feed3fdef5323b679ed2b04c8bc93179/app/models/user.rb#L362-L368

I think we could modify each of those perhaps like this, using the OR method of ActiveRecord:

User.where('MATCH(bio, username) AGAINST(? IN BOOLEAN MODE)', query + '*')
  .or(User.where(username: query))

What do you think?

For debugging why the location is no longer in there, I think we would look in this file:

https://github.com/publiclab/plots2/blob/439d3ce4feed3fdef5323b679ed2b04c8bc93179/app/api/srch/search.rb

I notice that on these API endpoints we explicitly provide location:

https://github.com/publiclab/plots2/blob/439d3ce4feed3fdef5323b679ed2b04c8bc93179/app/api/srch/search.rb#L343-L345

However, here we don't:

https://github.com/publiclab/plots2/blob/439d3ce4feed3fdef5323b679ed2b04c8bc93179/app/api/srch/search.rb#L103-L105

So, maybe we need to? I think this could be tested out locally to confirm if adding such fields like we do in the first example (latitude longitude and blurred) makes the API work properly locally. @sagarpreet-chadha you are probably most familiar with this code... does what I'm saying make sense? Thank you!!!

jywarren commented 5 years ago

Oh! Noting that this reliably returns my profile: https://publiclab.org/api/srch/profiles?query=warren&sort_by=recent&field=username

Not sure why that's different but maybe it's at least a workaround to the first issue above.

jywarren commented 5 years ago

(obscure, maybe not helpful, but here's the conditions that extra parameter adds to the query: https://github.com/publiclab/plots2/blob/e201b3b8e7b7bb4a56f7e926a7f6e8bd8b5a7dba/app/services/search_service.rb#L46-L50)

rarrunategu1 commented 5 years ago

@jywarren , your insight is super helpful. It makes total sense. I'm going to give it a try.

rarrunategu1 commented 5 years ago

@sagarpreet-chadha what do you use for testing the API locally? I looked through the README documentation but I couldn't find information how to test the API. I am currently using Postman but as I'm researching on testing api's with ruby I ran into rspec which is a gem installed in the application which can be used for testing as well. What do you suggest for testing them in your local environment?

jywarren commented 5 years ago

Hi @rarrunategui1 - i forgot, it's a totally separate app, but you've gotten plots2 running already I think? If so, you can boot it up, and the API is just a set of routes in that app. Like, https://localhost:3000/api/srch/profiles?query=warren

Make sense?

On Wed, May 22, 2019 at 1:49 PM rarrunategu1 notifications@github.com wrote:

@sagarpreet-chadha https://github.com/sagarpreet-chadha what do you use for testing the API locally? I looked through the README documentation but I couldn't find information how to test the API. I am currently using Postman but as I'm researching on testing api's with ruby I ran into rspec which is a gem installed in the application which can be used for testing as well. What do you suggest for testing them in your local environment?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/publiclab/mapknitter/issues/626?email_source=notifications&email_token=AAAF6JYRBFXIMJIQJU6H4SDPWWBQHA5CNFSM4HOM3362YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODV7ZULI#issuecomment-494901805, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAF6J73MXPPSB6X6PUDNB3PWWBQHANCNFSM4HOM336Q .

rarrunategu1 commented 5 years ago

Oh! That's much easier. I was returning the same in Postman...but one less program is nice.

rarrunategu1 commented 5 years ago

@jywarren I've added the code to the search.rb and tested it. It returns the lat and lon perfectly :) I'll do a pull request on seperate branches for each issue so I'll turn this one in now and then make the changes for the exact matches on it's own branch.

jywarren commented 5 years ago

Awesome!!!! Great work!!!

On Wed, May 22, 2019, 6:18 PM rarrunategu1 notifications@github.com wrote:

@jywarren https://github.com/jywarren I've added the code to the search.rb and tested it. It returns the lat and lon perfectly :) I'll do a pull request on seperate branches for each issue so I'll turn this one in now and then make the changes for the exact matches on it's own branch.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/publiclab/mapknitter/issues/626?email_source=notifications&email_token=AAAF6J6E2QUB6NQUJWNME7LPWXA7LA5CNFSM4HOM3362YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWAQLEQ#issuecomment-494994834, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAF6J65UDB37WAW6HU3R43PWXA7LANCNFSM4HOM336Q .