save()를 오버라이드할 때 modifyClass("model:composer")의 권장 대체 방법

I’m migrating away from the deprecated api.modifyClass("model:composer", ...).

My current override of Composer#save conditionally changes the save behavior, but otherwise needs to call super.save(opts).

I see the new addModelMethod / addModelCallback APIs, but I can’t find an example in core of using addModelMethod to override an existing method while retaining access to the original implementation.

Is addModelMethod intended for overriding existing model methods like this? If so, how should a plugin delegate to the original save() implementation?

Or is there another extension point that should be used for this Composer use case?

I guess it would depend on what you’re overriding the save method to do…

if you are looking to prevent a save based on some condition, you could use our composer-service-cannot-submit-post value transformer

if you’re doing something once the post is created, there’s api.onAppEvent("post:created")

if you’re adding a field there’s api.serializeOnCreate

do any of those fit what you’re looking for?

Thanks! I’m already using composer-service-cannot-submit-post — it handles the service-level check. But the composer model’s save() also checks cantSubmitPost, which includes missingReplyCharacters > 0, so an empty body is still blocked there.

onAppEvent("post:created") is too late because the post never gets created, and serializeOnCreate only adds fields to the request, so it doesn’t help with the body-length validation.

So the remaining issue is bypassing missingReplyCharacters / cantSubmitPost for the first post. Is there a supported extension point for this?

Here’s the current initializer for reference:

@david , 이 문제에 대해 가장 좋은 접근 방식에 대해 의견이 있으시면 말씀해 주시면 감사하겠습니다.

가장 좋은 방법은 Discourse 코어에 새로운 훅을 추가하여, 지원되는 방식으로 필요한 기능을 구현할 수 있도록 하는 것입니다. 다음과 같은 방식으로 어떨까요:

그렇게 되면 여러분의 플러그인은 다음과 같이 동작하게 됩니다.

api.registerValueTransformer(
  "composer-minimum-post-length",
  ({ value, context: { composer } }) => {
    if (
      siteSettings.discourse_optional_topic_body_enabled &&
      composer.topicFirstPost
    ) {
      return 0;
    }

    return value; // Keep core logic
  }
);

이 방식이 여러분의 사용 사례에 적합한지 알려주시면, PR을 초안에서 해제하고 리뷰 및 병합을 진행하겠습니다.

감사합니다, David! composer-minimum-post-length 트랜스포머를 테스트해 봤는데 정상적으로 작동합니다 — 본문이 비어 있는 주제도 성공적으로 생성됩니다.

한 가지 발견한 점이 있습니다: composer-editorvalidation에 하드코딩된 replyLength < 1 체크가 minimumPostLength를 사용하지 않기 때문에, 에디터에는 여전히 **“게시물은 비워둘 수 없습니다”**라는 메시지가 표시됩니다.

이 체크도 트랜스포머를 반영하거나, minimumPostLength0일 때 건너뛰도록 할 수 있을까요?

좋은 제안입니다. PR에 추가했습니다. :+1:

이제 완벽하게 작동합니다. 이 기능을 추가해 주셔서 정말 감사합니다, @david! :raising_hands: