<commit_before>kd                = require 'kd'

whoami            = require '../util/whoami'
kookies           = require 'kookies'
getGroup          = require '../util/getGroup'
getGroupStatus    = require '../util/getGroupStatus'
showError         = require '../util/showError'

remote            = require('../remote')
globals           = require 'globals'
GroupData         = require './groupdata'
remote_extensions = require 'app/remote-extensions'

module.exports = class GroupsController extends kd.Controller

  constructor: (options = {}, data) ->

    super options, data

    @isReady = no

    mainController    = kd.getSingleton 'mainController'
    { entryPoint }      = globals.config
    @groups           = {}
    @currentGroupData = new GroupData
    @currentGroupData.setGroup globals.currentGroup

    mainController.ready =>
      { slug } = entryPoint  if entryPoint?.type is 'group'
      @changeGroup slug

    @ready =>
      @on 'GroupDestroyed', ->
        # delete client id cookie, which is used for session authentication
        kookies.expire 'clientId'
        # send user to home page
        global.location.href = '/'

      @on 'InstanceChanged', (data) ->
        remote_extensions.updateInstance data?.contents

      @on 'InstanceDeleted', (data) ->
        remote_extensions.removeInstance data?.contents


  getCurrentGroup: ->
    FIXME = 'FIXME: array should never be passed'
    throw FIXME  if Array.isArray @currentGroupData.data
    return @currentGroupData.data


  getCurrentGroupStatus: -> getGroupStatus @getCurrentGroup()


  currentGroupHasStack: ->

    { stackTemplates } = @getCurrentGroup()

    return stackTemplates?.length > 0


  currentGroupIsNew: -> not @getCurrentGroup().stackTemplates


  filterXssAndForwardEvents: (target, events) ->
    events.forEach (event) =>
      target.on event, (rest...) =>
        rest = remote.revive rest
        @emit event, rest...

  openSocialGroupChannel: (group, callback = -> ) ->
    { realtime, socialapi } = kd.singletons

    socialapi.channel.byId { id: group.socialApiChannelId }, (err, channel) =>
      return callback err  if err

      socialapi.registerAndOpenChannel group, channel, (err, registeredChan) =>
        return callback err  if err

        realtimeChan = registeredChan?.delegate

        return callback 'realtime chan is not set'  unless realtimeChan

        @filterXssAndForwardEvents realtimeChan, [
          'StackTemplateChanged'
          'GroupStackTemplateRemoved'
          'InstanceChanged'
          'InstanceDeleted'
          'GroupDestroyed'
          'GroupJoined'
          'GroupLeft'
          'StackAdminMessageCreated'
          'SharedStackTemplateAccessLevel'
        ]

        callback null

  changeGroup: (groupName = 'koding', callback = kd.noop) ->

    return callback()  if @currentGroupName is groupName

    @currentGroupName   = groupName

    remote.cacheable groupName, (err, models) =>
      if err then callback err
      else if models?
        [group] = models
        if group.bongo_.constructorName isnt 'JGroup'
          @changeGroup 'koding'<commit_msg>groupscontroller: listen `MembershipRoleChanged` event<commit_after>kd                = require 'kd'

whoami            = require '../util/whoami'
kookies           = require 'kookies'
getGroup          = require '../util/getGroup'
getGroupStatus    = require '../util/getGroupStatus'
showError         = require '../util/showError'

remote            = require('../remote')
globals           = require 'globals'
GroupData         = require './groupdata'
remote_extensions = require 'app/remote-extensions'

module.exports = class GroupsController extends kd.Controller

  constructor: (options = {}, data) ->

    super options, data

    @isReady = no

    mainController    = kd.getSingleton 'mainController'
    { entryPoint }      = globals.config
    @groups           = {}
    @currentGroupData = new GroupData
    @currentGroupData.setGroup globals.currentGroup

    mainController.ready =>
      { slug } = entryPoint  if entryPoint?.type is 'group'
      @changeGroup slug

    @ready =>
      @on 'GroupDestroyed', ->
        # delete client id cookie, which is used for session authentication
        kookies.expire 'clientId'
        # send user to home page
        global.location.href = '/'

      @on 'InstanceChanged', (data) ->
        remote_extensions.updateInstance data?.contents

      @on 'InstanceDeleted', (data) ->
        remote_extensions.removeInstance data?.contents


  getCurrentGroup: ->
    FIXME = 'FIXME: array should never be passed'
    throw FIXME  if Array.isArray @currentGroupData.data
    return @currentGroupData.data


  getCurrentGroupStatus: -> getGroupStatus @getCurrentGroup()


  currentGroupHasStack: ->

    { stackTemplates } = @getCurrentGroup()

    return stackTemplates?.length > 0


  currentGroupIsNew: -> not @getCurrentGroup().stackTemplates


  filterXssAndForwardEvents: (target, events) ->
    events.forEach (event) =>
      target.on event, (rest...) =>
        rest = remote.revive rest
        @emit event, rest...

  openSocialGroupChannel: (group, callback = -> ) ->
    { realtime, socialapi } = kd.singletons

    socialapi.channel.byId { id: group.socialApiChannelId }, (err, channel) =>
      return callback err  if err

      socialapi.registerAndOpenChannel group, channel, (err, registeredChan) =>
        return callback err  if err

        realtimeChan = registeredChan?.delegate

        return callback 'realtime chan is not set'  unless realtimeChan

        @filterXssAndForwardEvents realtimeChan, [
          'StackTemplateChanged'
          'GroupStackTemplateRemoved'
          'InstanceChanged'
          'InstanceDeleted'
          'GroupDestroyed'
          'GroupJoined'
          'MembershipRoleChanged'
          'GroupLeft'
          'StackAdminMessageCreated'
          'SharedStackTemplateAccessLevel'
        ]

        callback null

  changeGroup: (groupName = 'koding', callback = kd.noop) ->

    return callback()  if @currentGroupName is groupName

    @currentGroupName   = groupName

    remote.cacheable groupName, (err, models) =>
      if err then callback err
      else if models?
        [group] = models
        if group.bongo_.constructorName isnt 'JGroup'
          @changeGroup 'koding'
{'fraction_non_alphanumeric': 0.09644830307813733, 'fraction_numerical': 0.00031570639305445933, 'mean_word_length': 2.5416433761878143, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, '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': '5995805', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1676, 'n_words': 437}