<commit_before>import { withPluginApi } from 'discourse/lib/plugin-api';
import PostMenuComponent from 'discourse/components/post-menu';
import { Button } from 'discourse/components/post-menu';
import { showSelector } from "discourse/lib/emoji/emoji-toolbar";
import TopicRoute from 'discourse/routes/topic';

function priorToApi(container)
{
  PostMenuComponent.registerButton(function(visibleButtons) {
    if (!Discourse.User.current() || !this.siteSettings.retort_enabled) { return }
    return visibleButtons.splice(0, 0, new Button('retort', 'retort.title', 'smile-o'))
  })

  PostMenuComponent.reopen({
    clickRetort: function(post) {
      const self = this
      showSelector({
        container: self.container,
        onSelect: function(retort) {
          Discourse.ajax('/retorts/' + self.get('post.id') + '.json', {
            type: 'POST',
            data: { retort: retort }
          })
          return false
        }
      })
    }
  })
}

function initializePlugin(api)
{

  const container    = api.container
  const siteSettings = container.lookup('site-settings:main');

  TopicRoute.on("setupTopicController", function(event) {
    const controller = event.controller
    controller.messageBus.subscribe('/retort/topics/' + controller.model.id, function(retort) {
      var post = _.find(controller.get('postsToRender.posts'), function(p) { return p.id == retort.post_id })
      var existing = _.findIndex(post.retorts, function(r) { return r.retort == retort.retort })

      if (existing == -1) {
        post.retorts.addObject(retort)
      } else if (retort.usernames.length > 0) {
        post.retorts[existing] = retort
      } else {
        post.retorts.splice(existing, 1)
      }
      post.setProperties({ retorts: post.retorts })
      Discourse.Retort.widgets[post.id].scheduleRerender()
    })
  })

  api.includePostAttributes('retorts');

  api.decorateWidget('post-contents:after-cooked', dec => {
    const post = dec.getModel();
    Discourse.Retort = Discourse.Retort || { widgets: {} }
    Discourse.Retort.widgets[post.id] = dec.widget

    if (post.retorts.length === 0) { return; }

    var sentenceFor = function(retort) {
      switch(retort.usernames.length) {
        case 1:  return `${retort.usernames[0]} reacted with :${retort.retort}:`<commit_msg>Initialize empty retorts<commit_after>import { withPluginApi } from 'discourse/lib/plugin-api';
import PostMenuComponent from 'discourse/components/post-menu';
import { Button } from 'discourse/components/post-menu';
import { showSelector } from "discourse/lib/emoji/emoji-toolbar";
import TopicRoute from 'discourse/routes/topic';

function priorToApi(container)
{
  PostMenuComponent.registerButton(function(visibleButtons) {
    if (!Discourse.User.current() || !this.siteSettings.retort_enabled) { return }
    return visibleButtons.splice(0, 0, new Button('retort', 'retort.title', 'smile-o'))
  })

  PostMenuComponent.reopen({
    clickRetort: function(post) {
      const self = this
      showSelector({
        container: self.container,
        onSelect: function(retort) {
          Discourse.ajax('/retorts/' + self.get('post.id') + '.json', {
            type: 'POST',
            data: { retort: retort }
          })
          return false
        }
      })
    }
  })
}

function initializePlugin(api)
{

  const container    = api.container
  const siteSettings = container.lookup('site-settings:main');

  TopicRoute.on("setupTopicController", function(event) {
    const controller = event.controller
    controller.messageBus.subscribe('/retort/topics/' + controller.model.id, function(retort) {
      var post = _.find(controller.get('postsToRender.posts'), function(p) { return p.id == retort.post_id })
      var existing = _.findIndex(post.retorts, function(r) { return r.retort == retort.retort })

      if (existing == -1) {
        post.retorts.addObject(retort)
      } else if (retort.usernames.length > 0) {
        post.retorts[existing] = retort
      } else {
        post.retorts.splice(existing, 1)
      }
      post.setProperties({ retorts: post.retorts })
      Discourse.Retort.widgets[post.id].scheduleRerender()
    })
  })

  api.includePostAttributes('retorts');

  api.decorateWidget('post-contents:after-cooked', dec => {
    const post = dec.getModel();
    Discourse.Retort = Discourse.Retort || { widgets: {} }
    Discourse.Retort.widgets[post.id] = dec.widget

    if (!post.retorts) { post.setProperties({ retorts: [] }) }
    if (post.retorts.length === 0) { return; }

    var sentenceFor = function(retort) {
      switch(retort.usernames.length) {
        case 1:  return `${retort.usernames[0]} reacted with :${retort.retort}:`
{'fraction_non_alphanumeric': 0.1417624521072797, 'fraction_numerical': 0.0034057045551298426, 'mean_word_length': 3.267938237965486, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1692350', 'n_tokens_mistral': 1477, 'n_tokens_neox': 1449, 'n_words': 261}