     user, qualified_user, kind, repo, qualified_repo
   }
 
-# Return our user representation in hubot's brainz
-user_data_key = (user) ->
-  'github-notify-' + user
-
 # Add a notification to the user settings, checking that all other notifications of the same kind
 # don't overlap with the new one (highest priority, same exact notification, etc.)
 add_notification = (robot, params) ->
-  # the user key in the database
-  user_key = user_data_key params.qualified_user
-
-  # get all notifications of user
-  notifications = robot.brain.get(user_key) or []
+  # get all specific notifications of user
+  userSettings = _.find(robot.brain.get(GITHUB_NOTIFY_PRE + params.kind), {userName: params.qualified_user}) or {}
+  userNotifications = userSettings.userNotifications or []
+  userInfo = userSettings.info or {}
 
   # if a global notification is already contained, throw an error
-  if _.find(notifications, {kind: params.kind, repo: true})?
+  if userNotifications.indexOf(true) >= 0
     throw new Error("You are already set to be notified for #{params.kind} in every repo.")
 
   # if the notification is already contained in the collection, throw an error
-  if _.find(notifications, {kind: params.kind, repo: params.qualified_repo})?
+  if userNotifications.indexOf(params.qualified_repo) >= 0
     throw new Error("You are already set to be notified for #{params.kind} in #{params.qualified_repo}.")
 
   # if the notification is for every repo, remove all the notifications of the same kind
   if not params.qualified_repo?
-    _.remove(notifications, {kind: params.kind})
+    userNotifications = []
 
   # add the new notification
-  notifications.push {
-    kind: params.kind,
-    repo: not params.repo? or params.qualified_repo,
-    user: params.user
+  userNotifications.push not params.repo? or params.qualified_repo
+  # update the user information
+  userInfo = params.user
+  # build the new userSettings
+  newUserSettings = {
+    userName: params.qualified_user
+    userNotifications: userNotifications,
+    userInfo: userInfo
   }
-  console.log notifications
-
-  # update the user notifications
-  robot.brain.remove user_key
-  robot.brain.set user_key, notifications
+
+  newSettings = robot.brain.get(GITHUB_NOTIFY_PRE + params.kind) or []
+  _.remove(newSettings, {userName: params.qualified_user})
+  newSettings.push(newUserSettings)
+
+  # update the user info
+  robot.brain.set GITHUB_NOTIFY_PRE + params.kind, newSettings
 
 # Remove a notification from the user settings, if it exists, otherwise it throws an appropriate Error
 remove_notification = (robot, params) ->
-  # the user key in the database
-  user_key = user_data_key params.qualified_user
-
-  # get all notifications of user
-  notifications = robot.brain.get(user_key) or []
+  # get all specific notifications of user
+  userSettings = _.find(robot.brain.get(GITHUB_NOTIFY_PRE + params.kind), {userName: params.qualified_user}) or {}
+  userNotifications = userSettings.userNotifications or []
+  userInfo = userSettings.info or {}
 
   # if the notification is for every repo, remove all the notifications of the same kind
   if not params.qualified_repo?
-    deleted = _.remove(notifications, {kind: params.kind})
+    userNotifications.length = 0
     throw new Error("You have no notifications set up for #{params.kind}.") if deleted?.length <= 0
   else
-    deleted = _.remove(notifications, {kind: params.kind, repo: params.qualified_repo})
+    deleted = _.remove(userNotifications, params.qualified_repo)
     throw new Error("You have no notifications set up for #{params.kind} in #{params.qualified_repo}.") if deleted?.length <= 0
 
-  console.log notifications
-
-  # update the user notifications
-  robot.brain.remove user_key
-  robot.brain.set user_key, notifications
+  # build the new userSettings
+  newUserSettings = {
+    userName: params.qualified_user
+    userNotifications: userNotifications,
+    userInfo: userInfo
+  }
+
+  newSettings = robot.brain.get(GITHUB_NOTIFY_PRE + params.kind) or []
+  _.remove(newSettings, {userName: params.qualified_user})
+  newSettings.push(newUserSettings)
+
+  # update the user info
+  robot.brain.set GITHUB_NOTIFY_PRE + params.kind, newSettings
+
+# Handle a generic comment, it can be the first of the issue (the issue itself) or any next comment
+on_commented_issue = (robot, comment, repository, users) ->
+  userInfos = []
+  # find all users that want to be notified of mentions
+  for user in users
+    userName = user.userName
+    # match the repository first, as it usually is the easier check
+    match_repo = user.userNotifications.indexOf(true) >= 0 or user.userNotifications.indexOf(repository.full_name) >= 0
+    # after matching repo, also match for a mention
+    if match_repo is true and comment.body.indexOf('@' + userName) >= 0
+      userInfos.push user.userInfo
+  # return the array
+  userInfos
+
+# Handle a generic comment, it can be the first of the issue (the issue itself) or any next comment
+on_assigned_issue = (robot, assignee, repository, users) ->
+  userInfos = []
+  # find all users that want to be notified of mentions
+  for user in users
+    userName = user.userName
+    # match the repository first, as it usually is the easier check
+    match_repo = user.userNotifications.indexOf(true) >= 0 or user.userNotifications.indexOf(repository.full_name) >= 0
+    # after matching repo, also match for a mention
+    if match_repo is true and assignee.login is userName
+      userInfos.push user.userInfo
+  # return the array
+  userInfos
 
 module.exports = (robot) ->
 

  # handle new comments and new issue assignments
  robot.router.post '/hubot/gh-notify', (req, res) ->
    payload = req.body
    console.log payload
    # discriminate the payload according to the action type
    res.send 'OK'


  notifications\s*                  # 'notifications'
  (in\s\S+)?\s*                     # Optional 'in <user/repo>'
///i

GITHUB_NOTIFY_PRE = 'github-notify-'

# Resolve user name to a potential GitHub username by using, in order:
#   - GitHub credentials as handled by the github-credentials.coffee script
