<commit_before><script lang="coffee">
import EventBus from '@/shared/services/event_bus'
import Records from '@/shared/services/records'
import Session from '@/shared/services/session'
import {map, debounce, without, filter, uniq, uniqBy, find} from 'lodash'
import RecipientsNotificationsCount from '@/components/common/recipients_notifications_count'

export default
  components: {
    RecipientsNotificationsCount
  }

  props:
    autofocus: Boolean
    label: String
    placeholder: String
    reset: Boolean
    model: Object
    excludedUserIds:
      type: Array
      default: -> [Session.user().id]
    initialRecipients:
      type: Array
      default: -> []

  data: ->
    query: ''
    suggestions: []
    recipients: @initialRecipients
    loading: false

  mounted: ->
    @fetchAndUpdateSuggestions()

  watch:
    'model.groupId': (groupId) ->
      @newRecipients(@initialRecipients)
      @fetchAndUpdateSuggestions()

    reset: ->
      @query = ''
      @recipients = @initialRecipients
      @fetchAndUpdateSuggestions()

    recipients: (val) ->
      @newRecipients(val)
      @$emit('new-recipients', val)
      @updateSuggestions()

    query: (q) ->
      @$emit('new-query', q)
      @fetchAndUpdateSuggestions()

  methods:
    fetchAndUpdateSuggestions: ->
      @fetchMemberships()
      @updateSuggestions()

    fetchMemberships: debounce ->
      if @query
        emails = uniq(@query.match(/[^\s:,;'"`<>]+?@[^\s:,;'"`<>]+\.[^\s:,;'"`<>]+/g) || [])
        return if emails.length

      @loading = true

      Records.memberships.fetch
        params:
          exclude_types: 'group'
          q: @query
          subgroups: 'all'
          per: 20
          group_id: @model.group().parentOrSelf().id
      .then =>
        @updateSuggestions()
      .finally =>
        @loading = false
    , 300

    newRecipients: (val) ->
      @model.recipientAudience = (find(val, (o) -> o.type == 'audience') || {}).id
      @model.recipientUserIds = map filter(val, (o) -> o.type == 'user'), 'id'
      @model.recipientEmails = map filter(val, (o) -> o.type == 'email'), 'name'

    findUsers: ->
      chain = Records.users.collection.chain()

      if @model.groupId
        chain = chain.find(id: {$in: @model.group().parentAndSelfMemberIds()})

      chain = chain.find(id: {$nin: @excludedUserIds})

      if @query
        chain = chain.find
          $or: [
            {name: {'$regex': ["^#{@query}", "i"]}}
            {username: {'$regex': ["^#{@query}", "i"]}}
            {name: {'$regex': [" #{@query}", "i"]}}
          ]

      chain.data()

    notificationsCount: ->
      sum(@recipients.map((r) => r.size || 1))

    remove: (item) ->
      @recipients = filter @recipients, (r) ->
        !(r.id == item.id && r.type == item.type)

    emailToRecipient: (email) ->
      id: email
      type: 'email'
      icon: 'mdi-email-outline'
      name: email

    updateSuggestions: ->
      if @query
        emails = uniq(@query.match(/[^\s:,;'"`<>]+?@[^\s:,;'"`<>]+\.[^\s:,;'"`<>]+/g) || [])

        # catch paste of multiple email addresses, or failure to press enter after an email address
        if emails.length > 1 or (emails.length == 1 && [',', ' '].includes(@query.slice(-1)))
          objs = uniqBy @recipients.concat(emails.map(@emailToRecipient)), 'id'
          @recipients = objs
          @suggestions = objs
          @query = ''
          return
        else if emails.length == 1
          @suggestions = @recipients.concat emails.map(@emailToRecipient)
          return

      members = @findUsers().map (u) ->
        id: u.id
        type: 'user'
        name: u.name
        user: u

      audiences = @audiences.map (a) ->
        id: a.id
        type: 'audience'
        icon: 'mdi-account-group'
        name: a.name
        size: a.size

      @suggestions = @recipients.concat(audiences).concat(members)

  computed:
    modelName: -> @model.constructor.singular

    audiences: ->
      ret = []
      canAnnounce = !!(@model.group().adminsInclude(Session.user()) || @model.group().membersCanAnnounce)
      console.log "audiences: recipients", @recipients
      if @recipients.length == 0
        if @model.groupId && canAnnounce
          ret.push
            id: 'group'
            name: @model.group().name
            size: @model.group().acceptedMembershipsCount
            icon: 'mdi-account-group'
        if @model.discussion() && @model.discussion().id && @model.discussion().membersCount > 1
          ret.push
            id: 'discussion_group'
            name: @$t('announcement.audiences.discussion_group')
            size: @model.discussion().membersCount
            icon: 'mdi-forum'

        if @model.poll
          if @model.poll().stancesCount > 1
            ret.push
              id: 'voters'
              name: @$t('announcement.audiences.voters')
              size: @model.poll().stancesCount
              icon: 'mdi-forum'
<commit_msg>remove log<commit_after><script lang="coffee">
import EventBus from '@/shared/services/event_bus'
import Records from '@/shared/services/records'
import Session from '@/shared/services/session'
import {map, debounce, without, filter, uniq, uniqBy, find} from 'lodash'
import RecipientsNotificationsCount from '@/components/common/recipients_notifications_count'

export default
  components: {
    RecipientsNotificationsCount
  }

  props:
    autofocus: Boolean
    label: String
    placeholder: String
    reset: Boolean
    model: Object
    excludedUserIds:
      type: Array
      default: -> [Session.user().id]
    initialRecipients:
      type: Array
      default: -> []

  data: ->
    query: ''
    suggestions: []
    recipients: @initialRecipients
    loading: false

  mounted: ->
    @fetchAndUpdateSuggestions()

  watch:
    'model.groupId': (groupId) ->
      @newRecipients(@initialRecipients)
      @fetchAndUpdateSuggestions()

    reset: ->
      @query = ''
      @recipients = @initialRecipients
      @fetchAndUpdateSuggestions()

    recipients: (val) ->
      @newRecipients(val)
      @$emit('new-recipients', val)
      @updateSuggestions()

    query: (q) ->
      @$emit('new-query', q)
      @fetchAndUpdateSuggestions()

  methods:
    fetchAndUpdateSuggestions: ->
      @fetchMemberships()
      @updateSuggestions()

    fetchMemberships: debounce ->
      if @query
        emails = uniq(@query.match(/[^\s:,;'"`<>]+?@[^\s:,;'"`<>]+\.[^\s:,;'"`<>]+/g) || [])
        return if emails.length

      @loading = true

      Records.memberships.fetch
        params:
          exclude_types: 'group'
          q: @query
          subgroups: 'all'
          per: 20
          group_id: @model.group().parentOrSelf().id
      .then =>
        @updateSuggestions()
      .finally =>
        @loading = false
    , 300

    newRecipients: (val) ->
      @model.recipientAudience = (find(val, (o) -> o.type == 'audience') || {}).id
      @model.recipientUserIds = map filter(val, (o) -> o.type == 'user'), 'id'
      @model.recipientEmails = map filter(val, (o) -> o.type == 'email'), 'name'

    findUsers: ->
      chain = Records.users.collection.chain()

      if @model.groupId
        chain = chain.find(id: {$in: @model.group().parentAndSelfMemberIds()})

      chain = chain.find(id: {$nin: @excludedUserIds})

      if @query
        chain = chain.find
          $or: [
            {name: {'$regex': ["^#{@query}", "i"]}}
            {username: {'$regex': ["^#{@query}", "i"]}}
            {name: {'$regex': [" #{@query}", "i"]}}
          ]

      chain.data()

    notificationsCount: ->
      sum(@recipients.map((r) => r.size || 1))

    remove: (item) ->
      @recipients = filter @recipients, (r) ->
        !(r.id == item.id && r.type == item.type)

    emailToRecipient: (email) ->
      id: email
      type: 'email'
      icon: 'mdi-email-outline'
      name: email

    updateSuggestions: ->
      if @query
        emails = uniq(@query.match(/[^\s:,;'"`<>]+?@[^\s:,;'"`<>]+\.[^\s:,;'"`<>]+/g) || [])

        # catch paste of multiple email addresses, or failure to press enter after an email address
        if emails.length > 1 or (emails.length == 1 && [',', ' '].includes(@query.slice(-1)))
          objs = uniqBy @recipients.concat(emails.map(@emailToRecipient)), 'id'
          @recipients = objs
          @suggestions = objs
          @query = ''
          return
        else if emails.length == 1
          @suggestions = @recipients.concat emails.map(@emailToRecipient)
          return

      members = @findUsers().map (u) ->
        id: u.id
        type: 'user'
        name: u.name
        user: u

      audiences = @audiences.map (a) ->
        id: a.id
        type: 'audience'
        icon: 'mdi-account-group'
        name: a.name
        size: a.size

      @suggestions = @recipients.concat(audiences).concat(members)

  computed:
    modelName: -> @model.constructor.singular

    audiences: ->
      ret = []
      canAnnounce = !!(@model.group().adminsInclude(Session.user()) || @model.group().membersCanAnnounce)
      if @recipients.length == 0
        if @model.groupId && canAnnounce
          ret.push
            id: 'group'
            name: @model.group().name
            size: @model.group().acceptedMembershipsCount
            icon: 'mdi-account-group'
        if @model.discussion() && @model.discussion().id && @model.discussion().membersCount > 1
          ret.push
            id: 'discussion_group'
            name: @$t('announcement.audiences.discussion_group')
            size: @model.discussion().membersCount
            icon: 'mdi-forum'

        if @model.poll
          if @model.poll().stancesCount > 1
            ret.push
              id: 'voters'
              name: @$t('announcement.audiences.voters')
              size: @model.poll().stancesCount
              icon: 'mdi-forum'

{'fraction_non_alphanumeric': 0.16070702966273873, 'fraction_numerical': 0.002641202763104429, 'mean_word_length': 2.2225859247135844, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 77, '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': '251479', 'n_tokens_mistral': 3218, 'n_tokens_neox': 3037, 'n_words': 633}