 
   return refs
 
+
+###*
+ * Returns the collection from RealtimeManager instance with given name.
+ * If collection is not created, this function will try to create a new
+ * collection with given `defaultType` and `defaultValue` parameters.
+ * If you are not gonna pass those values make sure that collection with the
+ * given name is present.
+ *
+ * Example:
+ *     # get collection syntax:
+ *     participants = getFromManager rtm, 'participants'
+ *     # get collection with default values in case that collection does not exist.
+ *     participants = getFromManager rtm, 'participants', 'list', []
+ *
+ * @param {RealtimeManager} manager
+ * @param {string} name
+ * @param {string} defaultType - for now just 'list' and 'map'
+ * @param {(object|Array)} - array for 'list', object for 'map'
+ * @return {object} collection
+###
 getFromManager = (manager, name, defaultType, defaultValue) ->
-  item   = manager.getFromModel name
-  item or= manager.create defaultType, name, defaultValue
-
-  return item
-
+
+  collection   = manager.getFromModel name
+  collection or= manager.create defaultType, name, defaultValue
+
+  return collection
+
+
+###*
+ * Updates logged in user's participant representation
+ * from collaborators collection from RealtimeManager instance.
+ *
+ * @param {RealtimeManager} manager
+ * @param {object} participants
+###
 registerCollaborationSessionId = (manager, participants) ->
+
   collaborators = manager.getCollaborators()
   for collaborator in collaborators when collaborator.isMe
     for user, index in participants.asArray() when user.nickname is getNick()

      newData.sessionId = collaborator.sessionId
      participants.remove index
      participants.insert index, newData

fetchParticipants = (channelId, callback) ->
  {socialapi} = kd.singletons
  socialapi.channel.listParticipants {channelId}, (err, participants) ->
    return callback err  if err
    idList = participants.map ({accountId}) -> accountId
    query  = socialApiId: $in: idList

    remote.api.JAccount.some query, {}
      .then (accounts) -> callback null, accounts

removeParticipantFromParticipantList = (references, nickname) ->
  { participants } = references
  return console.warn 'participants is not set'  unless participants

  for participant, index in participants.asArray()
    if participant.nickname is nickname
      participants.remove index
      break

removeParticipantFromMaps = (manager, nickname) ->
  myWatchMapName = "#{nickname}WatchMap"
  mySnapshotName = "#{nickname}Snapshot"

  manager.delete 'map', myWatchMapName
  manager.delete 'map', mySnapshotName

removeParticipantFromPermissions = (references, nickname) ->
  references.permissions.delete nickname

removeFromManager = (manager, references, nickname) ->
  removeParticipantFromParticipantList references, nickname
  removeParticipantFromMaps manager, nickname
  removeParticipantFromPermissions references, nickname

module.exports = {
  loadCollaborationFile


  return { targetUser, targetIndex }


###*
 * Returns an object with the necessary google drive maps and lists are set.
 *
 * TODO: `initialSnapshot` may be irrelevant here.
 *
 * @param {RealtimeManager} manager
 * @param {string} channelId
 * @param {object} initialSnapshot
###
getReferences = (manager, channelId, initialSnapshot) ->

  nickname          = getNick()
  watchMapName      = "#{nickname}WatchMap"
  snapshotName      = "#{nickname}Snapshot"
