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

action cable setup #805

Merged
merged 41 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
9b0487e
WIP action cable setup
ViditChitkara Jul 4, 2019
3b7b2bf
basic action cable setup complete
ViditChitkara Jul 6, 2019
10d42f4
minor change
ViditChitkara Jul 6, 2019
c7af515
minor changes
ViditChitkara Jul 10, 2019
826745f
few changes
ViditChitkara Jul 14, 2019
6e8e33b
initial working functionality complete
ViditChitkara Jul 18, 2019
53d8aad
Merge branch 'development' of https://github.com/publiclab/mapknitter…
alaxalves Jul 30, 2019
193f553
Refactoring code
alaxalves Jul 30, 2019
454cf2f
Merge branch 'development' of https://github.com/publiclab/mapknitter…
alaxalves Aug 4, 2019
852d5f9
Adding Foreman gem
alaxalves Aug 4, 2019
8fd235d
Scheduling Puma and Passenger servers
alaxalves Aug 4, 2019
a158e77
WIP action cable setup
ViditChitkara Jul 4, 2019
35aa34f
basic action cable setup complete
ViditChitkara Jul 6, 2019
2610522
minor change
ViditChitkara Jul 6, 2019
4040bc3
minor changes
ViditChitkara Jul 10, 2019
e231418
few changes
ViditChitkara Jul 14, 2019
55baec4
initial working functionality complete
ViditChitkara Jul 18, 2019
4904217
Refactoring code
alaxalves Jul 30, 2019
53084a1
Adding Foreman gem
alaxalves Aug 4, 2019
add9f16
Scheduling Puma and Passenger servers
alaxalves Aug 4, 2019
87deccd
few minor fix
ViditChitkara Aug 4, 2019
2432805
added a few tests
ViditChitkara Aug 6, 2019
2f9f6eb
Merge branch 'action-cable-integration' of https://github.com/publicl…
alaxalves Aug 6, 2019
a389a97
Merge branch 'development' of https://github.com/publiclab/mapknitter…
alaxalves Aug 6, 2019
54b5ef7
Refactoring connection module
alaxalves Aug 6, 2019
fdcd38a
Merge branch 'development' of https://github.com/publiclab/mapknitter…
alaxalves Aug 6, 2019
9191cf6
Using strong params in requests
alaxalves Aug 6, 2019
d0bdd43
Merge branch 'development' of https://github.com/publiclab/mapknitter…
alaxalves Aug 6, 2019
855089a
Merge branch 'development' of https://github.com/publiclab/mapknitter…
alaxalves Aug 6, 2019
d07cd13
Merge branch 'development' of https://github.com/publiclab/mapknitter…
alaxalves Aug 14, 2019
f271a84
added documentation
ViditChitkara Aug 14, 2019
fe95fc8
added more docs
ViditChitkara Aug 14, 2019
53bd5ab
added tests
ViditChitkara Aug 14, 2019
a522eaa
Merge branch 'development' of https://github.com/publiclab/mapknitter…
alaxalves Aug 16, 2019
3b2274a
Merge branch 'action-cable-integration' of https://github.com/publicl…
alaxalves Aug 16, 2019
ac8e40c
Using puma as dependency and correct image controller
alaxalves Aug 16, 2019
f002ad0
added a few tests
ViditChitkara Aug 18, 2019
1464157
a few changes
ViditChitkara Aug 18, 2019
b033cc5
remove unnecessary render
ViditChitkara Aug 19, 2019
e568fbe
few test fixes
ViditChitkara Aug 20, 2019
7779a96
Merge branch 'development' of https://github.com/publiclab/mapknitter…
alaxalves Aug 20, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ group :development, :test do
gem 'byebug', '~> 11.0.1', platforms: [:mri, :mingw, :x64_mingw]
gem 'faker', '~> 1.9.3'
gem 'pry-rails', '~> 0.3.9'
gem 'puma'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The puma server is required by action cable for it's working.

end

group :development do
Expand Down
2 changes: 2 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@
//= require annotations-legacy.js
//= require glfx-js/dist/glfx.js
//= require webgl-distort/dist/webgl-distort.js
//= require cable.js
//= require_tree .
11 changes: 11 additions & 0 deletions app/assets/javascripts/cable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
//= require action_cable
//= require_self
//= require_tree ./channels

(function() {
this.App || (this.App = {});

App.cable = ActionCable.createConsumer();

}).call(this);
22 changes: 22 additions & 0 deletions app/assets/javascripts/channels/concurrent_editing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
App.concurrent_editing = App.cable.subscriptions.create("ConcurrentEditingChannel", {
connected: function() {
// Called when the subscription is ready for use on the server
console.log("Connected");
},

disconnected: function() {
// Called when the subscription has been terminated by the server
console.log("bye");
},

received: function(data) {
// Called when there's incoming data on the websocket for this channel
window.mapKnitter.synchronizeData(data.changes);
},

speak: function(changes) {
return this.perform("sync", {
changes: changes
});
}
});
25 changes: 25 additions & 0 deletions app/assets/javascripts/mapknitter/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,28 @@ MapKnitter.Map = MapKnitter.Class.extend({
if (this.editing._mode != "lock") e.stopPropagation()
},

synchronizeData: function(warpables) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updation corresponding to each change will happen with the help of this function. This function gets all the necessary information related to the warpables on the map (which are broadcasted my the server on the concurrent_editing_channel)

var layers = [];
map.eachLayer(function(l) {layers.push(l)});
alaxalves marked this conversation as resolved.
Show resolved Hide resolved
layers = layers.filter(image => (image._url!=undefined || image._url!=null));
warpables.forEach(function(warpable) {
corners = [];
warpable.nodes.forEach(function(node) {
corners.push(L.latLng(node.lat, node.lon));
});

x = corners[2];
y = corners [3];
corners [2] = y;
corners [3] = x;

console.log(corners);

layer = layers.filter(l => l._url==warpable.srcmedium)[0];
layer.setCorners(corners);
});
},

saveImageIfChanged: function () {
var img = this
// check if image state has changed at all before saving!
Expand Down Expand Up @@ -499,6 +521,9 @@ MapKnitter.Map = MapKnitter.Class.extend({
beforeSend: function (e) {
$('.mk-save').removeClass('fa-check-circle fa-times-circle fa-green fa-red').addClass('fa-spinner fa-spin')
},
success: function(data) {
App.concurrent_editing.speak(data);
},
complete: function (e) {
$('.mk-save').removeClass('fa-spinner fa-spin').addClass('fa-check-circle fa-green')
},
Expand Down
4 changes: 4 additions & 0 deletions app/channels/application_cable/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module ApplicationCable
class Channel < ActionCable::Channel::Base
end
end
18 changes: 18 additions & 0 deletions app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user

def connect
self.current_user = find_verified_user
end

private
def find_verified_user
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This handles the authentication part.

if verified_user = User.find_by(id: cookies.signed["user_id"])
return verified_user
else
return nil
end
end
end
end
13 changes: 13 additions & 0 deletions app/channels/concurrent_editing_channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ConcurrentEditingChannel < ApplicationCable::Channel
def subscribed
stream_from "concurrent_editing_channel"
end

def unsubscribed
# Any cleanup needed when channel is unsubscribed
end

def sync changes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever changes are made, this method is called and it broadcasts the updated data to the channel.

ActionCable.server.broadcast 'concurrent_editing_channel', changes
end
end
4 changes: 3 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def current_user
user_id = session[:user_id]
if user_id
begin
@user = User.find(user_id)
u = User.find(user_id)
cookies.signed["user_id"] = u.id
@user = u
rescue StandardError
@user = nil
end
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ def update
@warpable.locked = params[:locked]
@warpable.cm_per_pixel = @warpable.get_cm_per_pixel
@warpable.save
render html: 'success'
data = @warpable.map.fetch_map_data
render json: data
end

def destroy
Expand Down
5 changes: 5 additions & 0 deletions app/models/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,9 @@ def add_tag(tagname, user)
tagname = tagname.downcase
tags.create(name: tagname, user_id: user.id, map_id: id) unless has_tag(tagname)
end

def fetch_map_data
data = warpables
return data.to_json
end
end
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<link rel="shortcut icon" href="/images/mapknitter-255.png">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
<%= stylesheet_link_tag 'application' %>
<%= action_cable_meta_tag %>
<%= javascript_include_tag 'application' %>
</head>
<body>
Expand Down
3 changes: 3 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,7 @@
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker

config.action_cable.url = "ws://localhost:3000/cable"
config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/]
end
2 changes: 2 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,6 @@

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/]
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

root to: 'front_ui#index'

mount ActionCable.server => '/cable'

get 'front-page', to: 'front_ui#index'
get 'mappers', to: 'front_ui#nearby_mappers'
get 'about', to: 'front_ui#about'
Expand Down