Merge pull request #35 from sharelatex/cmg-anonymous-display-name

Nameless logged in user labelled as anonymous
This commit is contained in:
Chrystal Maria Griffiths 2019-02-15 14:53:18 +00:00 committed by GitHub
commit 0b88a63444
2 changed files with 41 additions and 14 deletions

View file

@ -52,6 +52,10 @@ module.exports = WebsocketController =
metrics.inc "editor.leave-project"
Utils.getClientAttributes client, ["project_id", "user_id"], (error, {project_id, user_id}) ->
return callback(error) if error?
logger.log {project_id, user_id, client_id: client.id}, "client leaving project"
WebsocketLoadBalancer.emitToRoom project_id, "clientTracking.clientDisconnected", client.id
# bail out if the client had not managed to authenticate or join
# the project. Prevents downstream errors in docupdater from
# flushProjectToMongoAndDelete with null project_id.
@ -61,9 +65,6 @@ module.exports = WebsocketController =
if not project_id?
logger.log {user_id: user_id, client_id: client.id}, "client leaving, not in project"
return callback()
logger.log {project_id, user_id, client_id: client.id}, "client leaving project"
WebsocketLoadBalancer.emitToRoom project_id, "clientTracking.clientDisconnected", client.id
# We can do this in the background
ConnectedUsersManager.markUserAsDisconnected project_id, client.id, (err) ->
@ -129,7 +130,6 @@ module.exports = WebsocketController =
# after the initial joinDoc since we know they are already authorised.
## AuthorizationManager.removeAccessToDoc client, doc_id
callback()
updateClientPosition: (client, cursorData, callback = (error) ->) ->
metrics.inc "editor.update-client-position", 0.1
Utils.getClientAttributes client, [
@ -137,7 +137,7 @@ module.exports = WebsocketController =
], (error, {project_id, first_name, last_name, email, user_id}) ->
return callback(error) if error?
logger.log {user_id, project_id, client_id: client.id, cursorData: cursorData}, "updating client position"
AuthorizationManager.assertClientCanViewProjectAndDoc client, cursorData.doc_id, (error) ->
if error?
logger.warn {err: error, client_id: client.id, project_id, user_id}, "silently ignoring unauthorized updateClientPosition. Client likely hasn't called joinProject yet."
@ -145,13 +145,19 @@ module.exports = WebsocketController =
cursorData.id = client.id
cursorData.user_id = user_id if user_id?
cursorData.email = email if email?
if first_name or last_name
# Don't store anonymous users in redis to avoid influx
if !user_id or user_id == 'anonymous-user'
cursorData.name = ""
callback()
else
cursorData.name = if first_name && last_name
"#{first_name} #{last_name}"
else if first_name
first_name
else if last_name
last_name
else
""
ConnectedUsersManager.updateUserPosition(project_id, client.id, {
first_name: first_name,
last_name: last_name,
@ -162,9 +168,6 @@ module.exports = WebsocketController =
column: cursorData.column,
doc_id: cursorData.doc_id
}, callback)
else
cursorData.name = "Anonymous"
callback()
WebsocketLoadBalancer.emitToRoom(project_id, "clientTracking.clientUpdated", cursorData)
getConnectedUsers: (client, callback = (error, users) ->) ->
@ -221,4 +224,4 @@ module.exports = WebsocketController =
for op in update.op
if !op.c?
return false
return true
return true

View file

@ -426,7 +426,7 @@ describe 'WebsocketController', ->
doc_id: @doc_id
}).should.equal true
done()
it "should increment the update-client-position metric at 0.1 frequency", ->
@metrics.inc.calledWith("editor.update-client-position", 0.1).should.equal true
@ -509,6 +509,30 @@ describe 'WebsocketController', ->
it "should increment the update-client-position metric at 0.1 frequency", ->
@metrics.inc.calledWith("editor.update-client-position", 0.1).should.equal true
describe "with a logged in user who has no names set", ->
beforeEach ->
@clientParams = {
project_id: @project_id
first_name: undefined
last_name: undefined
email: @email = "joe@example.com"
user_id: @user_id = "user-id-123"
}
@client.get = (param, callback) => callback null, @clientParams[param]
@WebsocketController.updateClientPosition @client, @update
it "should send the update to the project name with no name", ->
@WebsocketLoadBalancer.emitToRoom
.calledWith(@project_id, "clientTracking.clientUpdated", {
doc_id: @doc_id,
id: @client.id,
user_id: @user_id,
name: "",
row: @row,
column: @column,
email: @email
})
.should.equal true
describe "with an anonymous user", ->
@ -519,17 +543,17 @@ describe 'WebsocketController', ->
@client.get = (param, callback) => callback null, @clientParams[param]
@WebsocketController.updateClientPosition @client, @update
it "should send the update to the project room with an anonymous name", ->
it "should send the update to the project room with no name", ->
@WebsocketLoadBalancer.emitToRoom
.calledWith(@project_id, "clientTracking.clientUpdated", {
doc_id: @doc_id,
id: @client.id
name: "Anonymous"
name: ""
row: @row
column: @column
})
.should.equal true
it "should not send cursor data to the connected user manager", (done)->
@ConnectedUsersManager.updateUserPosition.called.should.equal false
done()