Commit 5bfb0834 authored by Randall Leeds's avatar Randall Leeds

Clean up streamer and base URI usage

Factor out a socket factory into a service and send the client ID only
before the first message. Configure the client ID and this service in a
new module, h.socket. Fix the base URI to end in a slash and to be the
root URL of the server.
parent 5972dc76
......@@ -12,6 +12,7 @@ imports = [
'h.filters'
'h.session'
'h.services'
'h.socket'
]
......
imports = [
'bootstrap'
'h.helpers'
'h.socket'
'h.streamfilter'
]
......@@ -26,11 +27,11 @@ class App
this.$inject = [
'$element', '$filter', '$http', '$location', '$rootScope', '$scope', '$timeout',
'annotator', 'baseURI', 'session', 'streamfilter', 'viewFilter'
'annotator', 'session', 'socket', 'streamfilter', 'viewFilter'
]
constructor: (
$element, $filter, $http, $location, $rootScope, $scope, $timeout
annotator, baseURI, session, streamfilter, viewFilter
annotator, session, socket, streamfilter, viewFilter
) ->
{plugins, host, providers} = annotator
......@@ -412,15 +413,9 @@ class App
.addClause('/uri', 'one_of', Object.keys(annotator.plugins.Store.entities))
.getFilter()
streamerURI = baseURI.replace /\/\w+(\/?\??[^\/]*)\/?$/, '/__streamer__'
$scope.updater = new SockJS streamerURI
$scope.updater.onopen = =>
sockmsg =
filter: filter
clientID: annotator.clientID
#console.log sockmsg
$scope.updater.send JSON.stringify sockmsg
$scope.updater = socket()
$scope.updater.onopen = ->
$scope.updater.send(JSON.stringify({filter}))
$scope.updater.onclose = =>
$timeout $scope.initUpdater, 60000
......@@ -431,10 +426,6 @@ class App
return
data = msg.data.payload
action = msg.data.options.action
clientID = msg.data.options.clientID
if clientID is annotator.clientID
return
unless data instanceof Array then data = [data]
......@@ -648,15 +639,7 @@ class Annotation
if drafts.contains $scope.model
$scope.editing = true
# XXX: This should be done some other way since we should not assume
# the annotation share URL is in any particular path relation to the
# app base URL. It's time to start reflecting routes, I think. I'm
# just not sure how best to do that with pyramid traversal since there
# is not a pre-determined route map. One possibility would be to
# unify everything so that it's relative to the app URL.
prefix = baseURI.replace /\/\w+\/?$/, ''
$scope.shared_link = prefix + '/a/' + $scope.model.id
$scope.shared_link = "#{baseURI}a/#{$scope.model.id}"
$scope.$watch 'model.target', (targets) ->
return unless targets
......
......@@ -3,6 +3,7 @@ imports = [
'h.filters'
'h.directives'
'h.helpers'
'h.socket'
'h.streamfilter'
]
......@@ -20,16 +21,14 @@ get_quote = (annotation) ->
class Displayer
idTable : {}
this.$inject = ['$scope','$element','$timeout','baseURI', 'streamfilter']
constructor: ($scope, $element, $timeout, baseURI, streamfilter) ->
# Set streamer url
streamerURI = baseURI.replace /\/\w+(\/?\??[^\/]*)\/?$/, '/__streamer__'
# Generate client ID
buffer = new Array(16)
uuid.v4 null, buffer, 0
@clientID = uuid.unparse buffer
this.$inject = [
'$element', '$scope', '$timeout',
'socket', 'streamfilter'
]
constructor: (
$element, $scope, $timeout,
socket, streamfilter
) ->
$scope.root = document.init_annotation
$scope.annotation = $scope.root.annotation
$scope.annotations = [$scope.annotation]
......@@ -39,7 +38,7 @@ class Displayer
if $scope.annotation.references? then $scope.annotation.references.length else 0
$scope.full_deleted = false
@idTable[$scope.annotation.id] = $scope.annotation
$scope.filter =
filter =
streamfilter
.setPastDataNone()
.setMatchPolicyIncludeAny()
......@@ -57,14 +56,10 @@ class Displayer
to_change.replies = replies
to_change.reply_count = reply_count
$scope.open = =>
$scope.sock = new SockJS streamerURI
$scope.sock.onopen = =>
sockmsg =
filter: $scope.filter
clientID: @clientID
$scope.sock.send JSON.stringify sockmsg
$scope.open = ->
$scope.sock = socket()
$scope.sock.onopen = ->
$scope.sock.send(JSON.stringify({filter}))
$scope.sock.onclose = =>
$timeout $scope.open, 5000
......
......@@ -70,12 +70,6 @@ class Hypothesis extends Annotator
window.annotator = this
# Generate client ID
buffer = new Array(16)
uuid.v4 null, buffer, 0
@clientID = uuid.unparse buffer
$.ajaxSetup headers: "x-client-id": @clientID
@auth = session
@providers = []
@socialView =
......
......@@ -74,7 +74,7 @@ class SessionProvider
for name, options of ACTION_OPTION
actions[name] = angular.extend {}, options, @options
model = $resource(baseURI, {}, actions).load()
model = $resource("#{baseURI}app", {}, actions).load()
]
......
imports = [
'h.helpers'
]
clientID = ->
# Generate client ID
buffer = (new Array(16))
uuid.v4 null, buffer, 0
uuid.unparse buffer
run = ['clientID', (clientID) ->
$.ajaxSetup
headers:
"X-Client-Id": clientID
]
socket = ['baseURI', 'clientID', (baseURI, clientID) ->
-> new Socket(clientID, "#{baseURI}__streamer__")
]
class Socket extends SockJS
constructor: (clientID, args...)->
SockJS.apply(this, args)
send = this.send
this.send = (data) =>
# Set the client ID before the first message.
cid = JSON.stringify
messageType: 'client_id'
value: clientID
# Send the messages.
send.call(this, cid)
send.call(this, data)
# Restore the original send method.
this.send = send
angular.module('h.socket', imports, configure)
.factory('clientID', clientID)
.factory('socket', socket)
.run(run)
......@@ -3,6 +3,7 @@ imports = [
'h.directives'
'h.filters'
'h.helpers'
'h.socket'
'h.streamfilter'
]
......@@ -151,18 +152,16 @@ class StreamSearch
and_or: 'and'
operator: 'ge'
this.inject = ['$element', '$location', '$scope', '$timeout', 'baseURI','streamfilter']
this.inject = [
'$element', '$location', '$scope', '$timeout',
'baseURI', 'socket', 'streamfilter'
]
constructor: (
$element, $location, $scope, $timeout, baseURI, streamfilter
$element, $location, $scope, $timeout,
baseURI, socket, streamfilter
) ->
prefix = baseURI.replace /\/\w+(\/?\??[^\/]*)\/?$/, ''
$scope.empty = false
# Generate client ID
buffer = new Array(16)
uuid.v4 null, buffer, 0
@clientID = uuid.unparse buffer
$scope.sortAnnotations = (a, b) ->
a_upd = if a.updated? then new Date(a.updated) else new Date()
b_upd = if b.updated? then new Date(b.updated) else new Date()
......@@ -231,13 +230,9 @@ class StreamSearch
if $scope.sock? then $scope.sock.close()
$scope.annotations = new Array()
$scope.sock = new SockJS prefix + '/__streamer__'
$scope.sock.onopen = =>
sockmsg =
filter: filter
clientID: @clientID
$scope.sock.send JSON.stringify sockmsg
$scope.sock = socket()
$scope.sock.onopen = ->
$scope.sock.send(JSON.stringify({filter}))
$scope.sock.onclose = =>
# stream is closed
......@@ -264,7 +259,7 @@ class StreamSearch
for annotation in data
annotation.action = action
annotation.quote = get_quote annotation
annotation._share_link = prefix + '/a/' + annotation.id
annotation._share_link = "#{baseURI}a/#{annotation.id}"
if annotation in $scope.annotations then continue
......@@ -303,7 +298,6 @@ class StreamSearch
unless $scope.sock? then return
sockmsg =
messageType: 'more_hits'
clientID: @clientID
moreHits: number
$scope.sock.send JSON.stringify sockmsg
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment