Including composer custom fields on save draft

Recently, I started looking into a bug with the Events plugin where the events data shown on the composer is lost if the post is saved to drafts.

I tried to fix the issue and soon found out that there’s no API exposed by Discourse which allows us to do that.

I think this is an important feature from a UX standpoint with many potential use cases other than what I’m faced with.

For that, I am making a PR, which fixes this issue by introducing a new method in the Composer Model called serializeToDraft. This method will add those custom fields while saving the post to draft. Also, those fields will be set to the composer model when the draft is reopened.

@angus is helping me out on this PR by reviewing the code and suggesting the important improvements to be made.

I would like to know the thoughts of the Discourse team on this feature.

2 Likes

I agree we should have a clean mechanism for plugins to add and retrieve draft info associated with a post.

3 Likes

Me and @angus have put together a PR to address this here.
https://github.com/discourse/discourse/pull/8175

6 Likes

There are some things I dont understand: why is serializeToDraft part of the plugin api while serializeOnCreate and serializeToTopic are not? under what circumstance is serializeToDraft useful without the other two and vice versa? shouldnt there be a wrapper that does the serialization on both post/topic and also the draft?

3 Likes

Yes, agreed. You can go ahead and make a PR for those ones too.

I came across another issue: what about the saveDraft() method? somehow there needs to be an observer for the fields added with serializeToDraft() similar to this one:
https://github.com/discourse/discourse/blob/c01c937151526f5ab55ecfe7a5e8f84c9081f05b/app/assets/javascripts/discourse/controllers/composer.js.es6#L1073
otherwise the draft will not be sent to the endpoint if only the custom draft fields change.
so saveDraft() should also be part of the api or actually this should be abstracted away somehow.
I also found this dataChanged observer in the composermodel. I think its very odd, that we have two observers watching the same thing. The logic inside this observer would likely also need to be triggered for the custom draft fields. I also wonder which of the two observers will run first and what the implications are.
https://github.com/discourse/discourse/blob/eef21625c6b9dd589573bc1771c7075a12e0abc9/app/assets/javascripts/discourse/models/composer.js.es6#L1162

1 Like