回复旧帖时添加确认框

(大部分内容从 Creating a component to show last-chance warning when replying to old topics 复制粘贴而来)

我正在运营一个社区,该社区过去在“挖坟”帖子方面存在一些问题。

熟悉 warn_reviving_old_topic_age(并且我们确实已设置它),但似乎许多用户没有看到警报。我们也不想设置全局帖子计时器,因为确实存在复兴旧帖子有用的情况。

我们之前的解决方案是基于此组件阻止在“你确定吗”对话框中发帖:

但该组件被标记为损坏(事实上,我们的版本也已损坏)。根据我最初支持请求中的建议,我开始研究 unformatted code detector,它非常有帮助,但我正在努力完成。

目前,我有一个类似这样的东西:

import { withPluginApi } from "discourse/lib/plugin-api";
import { inject as service } from "@ember/service";

export default {
  name: "discourse-necro-warnings",
  initialize() {
    withPluginApi("0.8.8", (api) => {
      api.modifyClass("service:composer", (Superclass) =>
        class extends Superclass {
          save(...args) {
            let lastPostedAt = moment(this.model?.topic?.last_posted_at);
            let now = moment();
            let diff = now - lastPostedAt;
            console.log(diff);
            let d = moment.duration(settings.death_timer);

            if (diff >= d && !skipWarning) {
              // TODO: i18n
              let diffH = moment.duration(diff).humanize();
              let confirmationMessage = `The last post in this thread was ${diffH} ago. Are you sure you want to bump this thread?`;
              this.dialog.yesNoConfirm({
                message: confirmationMessage,
                didConfirm: () => {
                  super.save(...args);
                },
              });
            } else {
              super.save(...args);
            }
          }
        }
      );
    });
  },
};

但它似乎不起作用。添加一些调试日志可以确认 this.model?.topic?.last_posted_at 确实是未定义的,而且我也不知道确认对话框是否有效。

任何建议都将不胜感激!我认为只要获得主题当前置顶日期的 ISO 字符串(如果有的话)就足以让我取得进展。

3 个赞