<commit_before>class intertwinkles.User extends Backbone.Model
  idAttribute: "id"

#
# User authentication state
#
intertwinkles.user = new intertwinkles.User()
intertwinkles.users = null  # map of intertwinkles user_id to user data
intertwinkles.groups = null # list of groups
if INITIAL_DATA.groups?
  intertwinkles.groups = INITIAL_DATA.groups
if INITIAL_DATA.users?
  intertwinkles.users = INITIAL_DATA.users
  user = _.find intertwinkles.users, (e) -> e.email == INITIAL_DATA.email
  if user? then intertwinkles.user.set(user)

#
# Persona handlers
#

intertwinkles.request_logout = ->
  frame = $("#auth_frame")[0].contentWindow
  frame.postMessage {action: 'intertwinkles_logout'}, INTERTWINKLES_API_URL

intertwinkles.onlogin = (assertion) ->
  if window.INTERTWINKLES_AUTH_LOGOUT?
    return intertwinkles.request_logout()

  if not intertwinkles.socket?
    console.log "onlogin awaiting socket"
    return setTimeout(->
      intertwinkles.onlogin(assertion)
    , 100)

  console.log "onlogin"
  finish = ->
    intertwinkles.user.trigger("login")
    if window.INTERTWINKLES_AUTH_REDIRECT?
      window.location.href = INTERTWINKLES_AUTH_REDIRECT

  handle = (data) ->
    old_user = intertwinkles.user?.get("email")
    if not data.error? and data.email
      intertwinkles.users = data.users
      intertwinkles.groups = data.groups
      user = _.find intertwinkles.users, (e) -> e.email == data.email
      if user?
        if data.message == "NEW_ACCOUNT" or user.name == ""
          intertwinkles.user.set(user, silent: true)
        else
          intertwinkles.user.set(user)
      else
        intertwinkles.user.clear()

      if data.message == "NEW_ACCOUNT" or user?.name == ""
        profile_editor = new intertwinkles.EditNewProfile()
        $("body").append(profile_editor.el)
        profile_editor.render()
        profile_editor.on "done", finish
      else if old_user != intertwinkles.user.get("email")
        flash "info", "Welcome, #{intertwinkles.user.get("name")}"
        finish()

    if data.error?
      intertwinkles.request_logout()
      flash "error", data.error or "Error signing in."

  intertwinkles.socket.once "login", handle
  intertwinkles.socket.send "verify", {callback: "login", assertion: assertion}

intertwinkles.onlogout = (count) ->
  intertwinkles.users = null
  intertwinkles.groups = null
  if not intertwinkles.socket?
    if count > 15
      return alert("Socket connection failed.")
    count ?= 0
    console.log "onlogout awaiting socket #{count}..."
    return setTimeout( ->
      intertwinkles.onlogout(count + 1)
    , 100)
  console.log "onlogout"
  intertwinkles.socket.once "logout", ->
    reload = intertwinkles.is_authenticated()
    intertwinkles.user.clear()
    intertwinkles.user.trigger("logout")
    if reload or window.INTERTWINKLES_AUTH_LOGOUT
      flash "info", "Signed out."
      window.location.pathname = "/"
  intertwinkles.socket.send "logout", {callback: "logout"}

onmessage = (event) ->
  if event.origin == INTERTWINKLES_API_URL
    switch event.data.action
      when 'onlogin' then intertwinkles.onlogin(event.data.assertion)
      when 'onlogout' then intertwinkles.onlogout()
window.addEventListener('message', onmessage, false)

intertwinkles.is_authenticated = -> return intertwinkles.user.get("email")?

intertwinkles.auth_frame_template = _.template("""<iframe id='auth_frame'
  src='#{INTERTWINKLES_API_URL}/static/auth_frame.html'
  style='border: none; overflow: hidden;' width=97 height=29></iframe>""")

<commit_msg>Increase timeout for socket in onlogout handler<commit_after>class intertwinkles.User extends Backbone.Model
  idAttribute: "id"

#
# User authentication state
#
intertwinkles.user = new intertwinkles.User()
intertwinkles.users = null  # map of intertwinkles user_id to user data
intertwinkles.groups = null # list of groups
if INITIAL_DATA.groups?
  intertwinkles.groups = INITIAL_DATA.groups
if INITIAL_DATA.users?
  intertwinkles.users = INITIAL_DATA.users
  user = _.find intertwinkles.users, (e) -> e.email == INITIAL_DATA.email
  if user? then intertwinkles.user.set(user)

#
# Persona handlers
#

intertwinkles.request_logout = ->
  frame = $("#auth_frame")[0].contentWindow
  frame.postMessage {action: 'intertwinkles_logout'}, INTERTWINKLES_API_URL

intertwinkles.onlogin = (assertion) ->
  if window.INTERTWINKLES_AUTH_LOGOUT?
    return intertwinkles.request_logout()

  if not intertwinkles.socket?
    console.log "onlogin awaiting socket"
    return setTimeout(->
      intertwinkles.onlogin(assertion)
    , 100)

  console.log "onlogin"
  finish = ->
    intertwinkles.user.trigger("login")
    if window.INTERTWINKLES_AUTH_REDIRECT?
      window.location.href = INTERTWINKLES_AUTH_REDIRECT

  handle = (data) ->
    old_user = intertwinkles.user?.get("email")
    if not data.error? and data.email
      intertwinkles.users = data.users
      intertwinkles.groups = data.groups
      user = _.find intertwinkles.users, (e) -> e.email == data.email
      if user?
        if data.message == "NEW_ACCOUNT" or user.name == ""
          intertwinkles.user.set(user, silent: true)
        else
          intertwinkles.user.set(user)
      else
        intertwinkles.user.clear()

      if data.message == "NEW_ACCOUNT" or user?.name == ""
        profile_editor = new intertwinkles.EditNewProfile()
        $("body").append(profile_editor.el)
        profile_editor.render()
        profile_editor.on "done", finish
      else if old_user != intertwinkles.user.get("email")
        flash "info", "Welcome, #{intertwinkles.user.get("name")}"
        finish()

    if data.error?
      intertwinkles.request_logout()
      flash "error", data.error or "Error signing in."

  intertwinkles.socket.once "login", handle
  intertwinkles.socket.send "verify", {callback: "login", assertion: assertion}

intertwinkles.onlogout = (count) ->
  intertwinkles.users = null
  intertwinkles.groups = null
  if not intertwinkles.socket?
    if count > 200
      return alert("Socket connection failed.")
    count ?= 0
    console.log "onlogout awaiting socket #{count}..."
    return setTimeout( ->
      intertwinkles.onlogout(count + 1)
    , 100)
  console.log "onlogout"
  intertwinkles.socket.once "logout", ->
    reload = intertwinkles.is_authenticated()
    intertwinkles.user.clear()
    intertwinkles.user.trigger("logout")
    if reload or window.INTERTWINKLES_AUTH_LOGOUT
      flash "info", "Signed out."
      window.location.pathname = "/"
  intertwinkles.socket.send "logout", {callback: "logout"}

onmessage = (event) ->
  if event.origin == INTERTWINKLES_API_URL
    switch event.data.action
      when 'onlogin' then intertwinkles.onlogin(event.data.assertion)
      when 'onlogout' then intertwinkles.onlogout()
window.addEventListener('message', onmessage, false)

intertwinkles.is_authenticated = -> return intertwinkles.user.get("email")?

intertwinkles.auth_frame_template = _.template("""<iframe id='auth_frame'
  src='#{INTERTWINKLES_API_URL}/static/auth_frame.html'
  style='border: none; overflow: hidden;' width=97 height=29></iframe>""")


{'fraction_non_alphanumeric': 0.11832652486265671, 'fraction_numerical': 0.004366812227074236, 'mean_word_length': 3.7113470471134704, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '968498', 'n_tokens_mistral': 2392, 'n_tokens_neox': 2202, 'n_words': 470}