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 = [ ...@@ -12,6 +12,7 @@ imports = [
'h.filters' 'h.filters'
'h.session' 'h.session'
'h.services' 'h.services'
'h.socket'
] ]
......
imports = [ imports = [
'bootstrap' 'bootstrap'
'h.helpers' 'h.helpers'
'h.socket'
'h.streamfilter' 'h.streamfilter'
] ]
...@@ -26,11 +27,11 @@ class App ...@@ -26,11 +27,11 @@ class App
this.$inject = [ this.$inject = [
'$element', '$filter', '$http', '$location', '$rootScope', '$scope', '$timeout', '$element', '$filter', '$http', '$location', '$rootScope', '$scope', '$timeout',
'annotator', 'baseURI', 'session', 'streamfilter', 'viewFilter' 'annotator', 'session', 'socket', 'streamfilter', 'viewFilter'
] ]
constructor: ( constructor: (
$element, $filter, $http, $location, $rootScope, $scope, $timeout $element, $filter, $http, $location, $rootScope, $scope, $timeout
annotator, baseURI, session, streamfilter, viewFilter annotator, session, socket, streamfilter, viewFilter
) -> ) ->
{plugins, host, providers} = annotator {plugins, host, providers} = annotator
...@@ -412,15 +413,9 @@ class App ...@@ -412,15 +413,9 @@ class App
.addClause('/uri', 'one_of', Object.keys(annotator.plugins.Store.entities)) .addClause('/uri', 'one_of', Object.keys(annotator.plugins.Store.entities))
.getFilter() .getFilter()
streamerURI = baseURI.replace /\/\w+(\/?\??[^\/]*)\/?$/, '/__streamer__' $scope.updater = socket()
$scope.updater = new SockJS streamerURI $scope.updater.onopen = ->
$scope.updater.send(JSON.stringify({filter}))
$scope.updater.onopen = =>
sockmsg =
filter: filter
clientID: annotator.clientID
#console.log sockmsg
$scope.updater.send JSON.stringify sockmsg
$scope.updater.onclose = => $scope.updater.onclose = =>
$timeout $scope.initUpdater, 60000 $timeout $scope.initUpdater, 60000
...@@ -431,10 +426,6 @@ class App ...@@ -431,10 +426,6 @@ class App
return return
data = msg.data.payload data = msg.data.payload
action = msg.data.options.action action = msg.data.options.action
clientID = msg.data.options.clientID
if clientID is annotator.clientID
return
unless data instanceof Array then data = [data] unless data instanceof Array then data = [data]
...@@ -648,15 +639,7 @@ class Annotation ...@@ -648,15 +639,7 @@ class Annotation
if drafts.contains $scope.model if drafts.contains $scope.model
$scope.editing = true $scope.editing = true
$scope.shared_link = "#{baseURI}a/#{$scope.model.id}"
# 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.$watch 'model.target', (targets) -> $scope.$watch 'model.target', (targets) ->
return unless targets return unless targets
......
...@@ -3,6 +3,7 @@ imports = [ ...@@ -3,6 +3,7 @@ imports = [
'h.filters' 'h.filters'
'h.directives' 'h.directives'
'h.helpers' 'h.helpers'
'h.socket'
'h.streamfilter' 'h.streamfilter'
] ]
...@@ -20,16 +21,14 @@ get_quote = (annotation) -> ...@@ -20,16 +21,14 @@ get_quote = (annotation) ->
class Displayer class Displayer
idTable : {} idTable : {}
this.$inject = ['$scope','$element','$timeout','baseURI', 'streamfilter'] this.$inject = [
constructor: ($scope, $element, $timeout, baseURI, streamfilter) -> '$element', '$scope', '$timeout',
# Set streamer url 'socket', 'streamfilter'
streamerURI = baseURI.replace /\/\w+(\/?\??[^\/]*)\/?$/, '/__streamer__' ]
constructor: (
# Generate client ID $element, $scope, $timeout,
buffer = new Array(16) socket, streamfilter
uuid.v4 null, buffer, 0 ) ->
@clientID = uuid.unparse buffer
$scope.root = document.init_annotation $scope.root = document.init_annotation
$scope.annotation = $scope.root.annotation $scope.annotation = $scope.root.annotation
$scope.annotations = [$scope.annotation] $scope.annotations = [$scope.annotation]
...@@ -39,7 +38,7 @@ class Displayer ...@@ -39,7 +38,7 @@ class Displayer
if $scope.annotation.references? then $scope.annotation.references.length else 0 if $scope.annotation.references? then $scope.annotation.references.length else 0
$scope.full_deleted = false $scope.full_deleted = false
@idTable[$scope.annotation.id] = $scope.annotation @idTable[$scope.annotation.id] = $scope.annotation
$scope.filter = filter =
streamfilter streamfilter
.setPastDataNone() .setPastDataNone()
.setMatchPolicyIncludeAny() .setMatchPolicyIncludeAny()
...@@ -57,14 +56,10 @@ class Displayer ...@@ -57,14 +56,10 @@ class Displayer
to_change.replies = replies to_change.replies = replies
to_change.reply_count = reply_count to_change.reply_count = reply_count
$scope.open = => $scope.open = ->
$scope.sock = new SockJS streamerURI $scope.sock = socket()
$scope.sock.onopen = ->
$scope.sock.onopen = => $scope.sock.send(JSON.stringify({filter}))
sockmsg =
filter: $scope.filter
clientID: @clientID
$scope.sock.send JSON.stringify sockmsg
$scope.sock.onclose = => $scope.sock.onclose = =>
$timeout $scope.open, 5000 $timeout $scope.open, 5000
......
...@@ -70,12 +70,6 @@ class Hypothesis extends Annotator ...@@ -70,12 +70,6 @@ class Hypothesis extends Annotator
window.annotator = this 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 @auth = session
@providers = [] @providers = []
@socialView = @socialView =
......
...@@ -74,7 +74,7 @@ class SessionProvider ...@@ -74,7 +74,7 @@ class SessionProvider
for name, options of ACTION_OPTION for name, options of ACTION_OPTION
actions[name] = angular.extend {}, options, @options 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 = [ ...@@ -3,6 +3,7 @@ imports = [
'h.directives' 'h.directives'
'h.filters' 'h.filters'
'h.helpers' 'h.helpers'
'h.socket'
'h.streamfilter' 'h.streamfilter'
] ]
...@@ -151,18 +152,16 @@ class StreamSearch ...@@ -151,18 +152,16 @@ class StreamSearch
and_or: 'and' and_or: 'and'
operator: 'ge' operator: 'ge'
this.inject = ['$element', '$location', '$scope', '$timeout', 'baseURI','streamfilter'] this.inject = [
'$element', '$location', '$scope', '$timeout',
'baseURI', 'socket', 'streamfilter'
]
constructor: ( constructor: (
$element, $location, $scope, $timeout, baseURI, streamfilter $element, $location, $scope, $timeout,
baseURI, socket, streamfilter
) -> ) ->
prefix = baseURI.replace /\/\w+(\/?\??[^\/]*)\/?$/, ''
$scope.empty = false $scope.empty = false
# Generate client ID
buffer = new Array(16)
uuid.v4 null, buffer, 0
@clientID = uuid.unparse buffer
$scope.sortAnnotations = (a, b) -> $scope.sortAnnotations = (a, b) ->
a_upd = if a.updated? then new Date(a.updated) else new Date() 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() b_upd = if b.updated? then new Date(b.updated) else new Date()
...@@ -231,13 +230,9 @@ class StreamSearch ...@@ -231,13 +230,9 @@ class StreamSearch
if $scope.sock? then $scope.sock.close() if $scope.sock? then $scope.sock.close()
$scope.annotations = new Array() $scope.annotations = new Array()
$scope.sock = new SockJS prefix + '/__streamer__' $scope.sock = socket()
$scope.sock.onopen = ->
$scope.sock.onopen = => $scope.sock.send(JSON.stringify({filter}))
sockmsg =
filter: filter
clientID: @clientID
$scope.sock.send JSON.stringify sockmsg
$scope.sock.onclose = => $scope.sock.onclose = =>
# stream is closed # stream is closed
...@@ -264,7 +259,7 @@ class StreamSearch ...@@ -264,7 +259,7 @@ class StreamSearch
for annotation in data for annotation in data
annotation.action = action annotation.action = action
annotation.quote = get_quote annotation 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 if annotation in $scope.annotations then continue
...@@ -303,7 +298,6 @@ class StreamSearch ...@@ -303,7 +298,6 @@ class StreamSearch
unless $scope.sock? then return unless $scope.sock? then return
sockmsg = sockmsg =
messageType: 'more_hits' messageType: 'more_hits'
clientID: @clientID
moreHits: number moreHits: number
$scope.sock.send JSON.stringify sockmsg $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