Skip to content

Commit

Permalink
Add map of people to publiclab.org/people publiclab#1940 (publiclab#2158
Browse files Browse the repository at this point in the history
)

* Added maps

* API made for people maps

* Added API url for People map

* Made helper function for People Map API

* Updated self.people_map

* Created _peopleLeaflet.html.erb

* Drupal_user removed

* Indentation done

* search Recent People functionality

* search Recent People helper
  • Loading branch information
sagarpreet-chadha authored and jywarren committed Feb 1, 2018
1 parent 6a31adb commit bd222da
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 11 deletions.
23 changes: 23 additions & 0 deletions app/api/srch/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ class Search < Grape::API
sresult
end

#API TO FETCH QRY RECENT CONTRIBUTORS
# Request URL should be /api/srch/peoplelocations?srchString=QRY[&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM]
#QRY should be a number
desc 'Perform a search to show x Recent People',
hidden: false,
is_array: false,
nickname: 'srchGetPeople'
params do
requires :srchString, type: String, documentation: { example: 'Spec' }
optional :seq, type: Integer, documentation: { example: 995 }
optional :showCount, type: Integer, documentation: { example: 3 }
optional :pageNum, type: Integer, documentation: { example: 0 }
end
get :peoplelocations do
sresult = DocList.new
unless params[:srchString].nil? || params[:srchString] == 0
sservice = SearchService.new
sresult = sservice.recentPeople(params[:srchString])
end
sparms = SearchRequest.fromRequest(params)
sresult.srchParams = sparms
sresult
end
# end endpoint definitions
end
end
Expand Down
15 changes: 5 additions & 10 deletions app/models/concerns/node_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,32 +227,27 @@ def self.notes_map_by_tag(body)
end
end

# in our interface, "users" are known as "people" because it's more human
# in our interface, "users" are known as "people" because it's more human
def self.people_map(body, _page = 1)
body.gsub(/[^\>`](\<p\>)?\[map\:people\:(\S+)\:(\S+)\]/) do |_tagname|
tagname = Regexp.last_match(2)
lat = Regexp.last_match(2)
lon = Regexp.last_match(3)
nids = nids || []
users = User.where(status: 1)
.includes(:user_tags)
.references(:user_tags)
.where('user_tags.value LIKE ?', 'lat:' + lat[0..lat.length - 2] + '%')

a = ActionController::Base.new()
output = a.render_to_string(template: "map/_leaflet",
output = a.render_to_string(template: "map/_peopleLeaflet",
layout: false,
locals: {
lat: lat,
lon: lon,
items: users,
lat: lat ,
lon: lon ,
people: true
}
)
output
end
end


# in our interface, "users" are known as "people" because it's more human
def self.people_grid(body, current_user = nil, _page = 1)
body.gsub(/[^\>`](\<p\>)?\[people\:(\S+)\]/) do |_tagname|
Expand Down
23 changes: 23 additions & 0 deletions app/services/search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,27 @@ def nearbyNodes(srchString)
sresult
end

#GET X number of latest people/contributors
# X = srchString
def recentPeople(srchString)
sresult = DocList.new
nodes = Node.all.order("changed DESC").limit(100).uniq
users = []
nodes.each do |node|
users << node.author.user
end
users = users.uniq
users.each do |user|
if user.has_power_tag("lat") && user.has_power_tag("lon")
blurred = false
if user.has_power_tag("location")
blurred = user.get_value_of_power_tag("location")
end
doc = DocResult.fromLocationSearch(user.id, 'people_coordinates', user.path , user.username , 0 , 0 , user.lat , user.lon , blurred)
sresult.addDoc(doc)
end
end
sresult
end

end
28 changes: 28 additions & 0 deletions app/views/map/_peopleLeaflet.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<% unique_id = rand(100) %>
<style>
#map<%= unique_id %> { width:100%; height:300px; margin: 0; position: relative;}
</style>

<div class="leaflet-map" id="map<%= unique_id %>"></div>
<% if defined? people %>
<p><i><small>
Share your own location on <a href='/profile'>your profile</a>.
</small></i></p>
<% end %>

<script>
var map<%= unique_id %> = L.map('map<%= unique_id %>').on('load', onMapLoad).setView([<%= lat %>,<%= lon %>], 2);
L.tileLayer("//a.tiles.mapbox.com/v3/jywarren.map-lmrwb2em/{z}/{x}/{y}.png").addTo(map<%= unique_id %>);
function onMapLoad(e){
$.getJSON("/api/srch/peoplelocations?srchString=100" , function(data){
if (!!data.items){
for (i = 0; i < data.items.length ; i++) {
var url = data.items[i].docUrl ;
var title = data.items[i].docTitle ;
L.marker([data.items[i].latitude , data.items[i].longitude]).addTo(map<%= unique_id %>).bindPopup("<a href=" + url + ">" + title + "</a>") ;
}
}
});
}
</script>

2 changes: 1 addition & 1 deletion app/views/users/list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</div>
<div class="col-md-9">
<h2><%= raw t('users.list.members_last_activity') %></h2>

<%= render :partial => "map/peopleLeaflet" , locals: {people: true , lat:17 , lon: 75} %>
<table class="table">
<tr>
<th class="visible-sm visible-xs"></th>
Expand Down
1 change: 1 addition & 0 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Per-model API endpoints are:
* Tags: https://publiclab.org/api/srch/tags?srchString=foo
* Notes: https://publiclab.org/api/srch/notes?srchString=foo
* Locations: https://publiclab.org/api/srch/locations?srchString=lat,lon
* PeopleLocations: https://publiclab.org/api/srch/peoplelocations?srchString=QRY

****

Expand Down
24 changes: 24 additions & 0 deletions test/functional/search_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,28 @@ def app

end

test 'search Recent People functionality' do
get '/api/srch/peoplelocations?srchString=100'
assert last_response.ok?

# Expected search pattern
pattern = {
srchParams: {
srchString: '100',
seq: nil,
}.ignore_extra_keys!
}.ignore_extra_keys!

matcher = JsonExpressions::Matcher.new(pattern)

json = JSON.parse(last_response.body)

assert_equal users(:bob).username, json['items'][0]['docTitle']
assert_equal "people_coordinates", json['items'][0]['docType']
assert_equal 1, json['items'][0]['docId']

assert matcher =~ json

end

end
11 changes: 11 additions & 0 deletions test/unit/helpers/search_helper_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
require 'test_helper'

class SearchHelperTest < ActionView::TestCase

test "search Recent People helper" do
sservice = SearchService.new
result = sservice.recentPeople(100)
assert_equal result.items.first.docType , "people_coordinates"
assert_equal result.items.first.docTitle , users(:bob).username
assert_equal result.items.first.docSummary , 0
assert_equal result.items.first.docScore , 0
assert_equal result.items.first.latitude , "41"
assert_equal result.items.first.longitude , "-90"
end
end

0 comments on commit bd222da

Please sign in to comment.