josephg / ShareJS

Collaborative editing in any app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it possible to store user (id/name) metadata in ShareJS 0.6.2?

mizzao opened this issue · comments

I've been trying to figure out if it's possible to get an app-generated user ID to be stored in the meta field of the ops collection in ShareJS 0.6.2, and have been digging through the code. It appears as if this is not possible but I'd be happy corrected if someone who has more experience with this can let me know.

  • When a user connects/authenticates, I assign agent.name with their userId.
  • If they create a new document, this userId appears with creator.
  • Ops by users seem to completely ignore this name. The only thing that shows up in meta is the pretty useless source field which is a randomly generated ID for each socket. It helps to distinguish users but makes it near impossible to tell who's who, especially when switching between documents.

Is there an easy way to just get the name field into the meta object as well?

Thanks!

For ShareJS 0.6.2, I've done this by monkey-patching the UserAgent's submitOp function in https://github.com/mizzao/meteor-sharejs/blob/master/sharejs-meteor-auth.coffee#L52, on the first connection of any user:

UserAgent.submitOp = (docName, opData, callback) ->
    opData.meta ||= {}
    opData.meta.userId = @name # <- added this line
    opData.meta.source = @sessionId
    dupIfSource = opData.dupIfSource or []

    # If ops and meta get coalesced, they should be separated here.
    if opData.op
      @doAuth {docName, op:opData.op, v:opData.v, meta:opData.meta, dupIfSource}, 'submit op', callback, =>
        model.applyOp docName, opData, callback
    else
      @doAuth {docName, meta:opData.meta}, 'submit meta', callback, =>
        model.applyMetaOp docName, opData, callback

This has a distinct bad code smell, so while ShareJS 0.7 is in development, I would strongly encourage a good way to modularize the way metadata is stored, so that we can associate it with userIDs and other information in whatever app it's being integrated into.