Skip to content

Commit

Permalink
notes pub/sub refs #45073
Browse files Browse the repository at this point in the history
  • Loading branch information
skv-headless committed Apr 18, 2014
1 parent 8ab396a commit 9652afc
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 166 deletions.
6 changes: 3 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ gem "js-routes"
gem 'react-rails', '~> 0.8.0.0'
gem "activerecord-import", "~> 0.4.1"

gem "private_pub"
gem 'thin'

group :development do
gem "annotate", "~> 2.6.0.beta2"
gem "letter_opener"
Expand All @@ -204,9 +207,6 @@ group :development do

# Docs generator
gem "sdoc"

# thin instead webrick
gem 'thin'
end

group :undev do
Expand Down
23 changes: 23 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ GEM
colored (1.2)
colorize (0.5.8)
connection_pool (1.2.0)
cookiejar (0.3.2)
coveralls (0.7.0)
multi_json (~> 1.3)
rest-client
Expand Down Expand Up @@ -192,6 +193,14 @@ GEM
elasticsearch-transport (0.4.8)
faraday
multi_json
em-http-request (1.0.3)
addressable (>= 2.2.3)
cookiejar
em-socksify
eventmachine (>= 1.0.0.beta.4)
http_parser.rb (>= 0.5.3)
em-socksify (0.3.0)
eventmachine (>= 1.0.0.beta.4)
email_spec (1.5.0)
launchy (~> 2.1)
mail (~> 2.2)
Expand All @@ -216,6 +225,17 @@ GEM
multipart-post (~> 1.2.0)
faraday_middleware (0.9.0)
faraday (>= 0.7.4, < 0.9)
faye (1.0.1)
cookiejar (>= 0.3.0)
em-http-request (>= 0.3.0)
eventmachine (>= 0.12.0)
faye-websocket (>= 0.7.0)
multi_json (>= 1.0.0)
rack (>= 1.0.0)
websocket-driver (>= 0.3.0)
faye-websocket (0.7.0)
eventmachine (>= 0.12.0)
websocket-driver (>= 0.3.0)
ffaker (1.22.1)
ffi (1.9.3)
fog (1.3.1)
Expand Down Expand Up @@ -412,6 +432,8 @@ GEM
websocket-driver (>= 0.2.0)
polyglot (0.3.4)
posix-spawn (0.3.8)
private_pub (1.0.3)
faye
protected_attributes (1.0.5)
activemodel (>= 4.0.1, < 5.0)
pry (0.9.12.4)
Expand Down Expand Up @@ -735,6 +757,7 @@ DEPENDENCIES
omniauth-twitter
pg
poltergeist (~> 1.4.1)
private_pub
protected_attributes
pry
pry-rails
Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#= require underscore.string.min
#= require nprogress
#= require nprogress-turbolinks
#= require private_pub
#= require_directory ./api
#= require_tree .

Expand Down
51 changes: 11 additions & 40 deletions app/assets/javascripts/notes.js.coffee
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
class Notes
@interval: null
constructor: (@channel, @note_ids) ->
@subscribe(@channel)

constructor: (notes_url, note_ids) ->
@notes_url = notes_url
@notes_url = gon.relative_url_root + @notes_url if gon.relative_url_root?
@note_ids = note_ids
@initRefresh()
@setupMainTargetNoteForm()
@cleanBinding()
@addBinding()

addBinding: ->
# add note to UI after creation
$(document).on "ajax:success", ".js-main-target-form", @addNote
$(document).on "ajax:success", ".js-discussion-note-form", @addDiscussionNote

# change note in UI after update
# change note in UI after update
$(document).on "ajax:success", "form.edit_note", @updateNote

# Edit note link
Expand Down Expand Up @@ -63,25 +55,13 @@ class Notes
$(document).off "click", ".js-discussion-reply-button"
$(document).off "click", ".js-add-diff-note-button"


initRefresh: ->
clearInterval(Notes.interval)
Notes.interval = setInterval =>
@refresh()
, 15000

refresh: ->
@getContent()

getContent: ->
$.ajax
url: @notes_url
dataType: "json"
success: (data) =>
notes = data.notes
$.each notes, (i, note) =>
@renderNote(note)

subscribe: (channel) ->
PrivatePub.subscribe(channel, (note, channel) =>
if note.line_code
@renderDiscussionNote(note)
else
@addNote(note)
)

###
Render note in main comments area.
Expand Down Expand Up @@ -230,24 +210,15 @@ class Notes
GitLab.GfmAutoComplete.setup()
form.show()


###
Called in response to the new note form being submitted
Adds new note to list.
###
addNote: (xhr, note, status) =>
addNote: (note) =>
@renderNote(note)
@updateVotes()

###
Called in response to the new note form being submitted
Adds new note to list.
###
addDiscussionNote: (xhr, note, status) =>
@renderDiscussionNote(note)

###
Called in response to the edit note form being submitted
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/projects/notes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def index
def create
@note = ProjectsService.new(current_user, project, params).notes.create

channel = Gitlab::NoteHelper.channel(@note.noteable)
PrivatePub.publish_to(channel, note_json(@note))

respond_to do |format|
format.json { render_note_json(@note) }
format.html { redirect_to :back }
Expand Down Expand Up @@ -85,9 +88,14 @@ def note_to_discussion_html(note)
end

def render_note_json(note)
render json: {
render json: note_json(note)
end

def note_json(note)
{
id: note.id,
discussion_id: note.discussion_id,
line_code: note.line_code,
html: note_to_html(note),
discussion_html: note_to_discussion_html(note)
}
Expand Down
4 changes: 3 additions & 1 deletion app/views/projects/notes/_notes_with_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
- if can? current_user, :write_note, @project
= render "projects/notes/form"

- channel = Gitlab::NoteHelper.channel(@noteable)
= subscribe_to channel
:javascript
new Notes("#{project_notes_path(target_id: @noteable.id, target_type: @noteable.class.name.underscore)}", #{@notes.map(&:id).to_json})
new Notes("#{channel}", #{@notes.map(&:id).to_json})
10 changes: 10 additions & 0 deletions config/private_pub.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
development:
server: "http://localhost:9292/faye"
secret_token: "secret"
test:
server: "http://localhost:9292/faye"
secret_token: "secret"
production:
server: "http://localhost:9292/faye"
secret_token: "4c9dd55ee8462476486d8579f759afc5696825d16d09860bed4a476733661ba7"
signature_expiration: 3600 # one hour
Loading

0 comments on commit 9652afc

Please sign in to comment.