Skip to content

A platform for community discussion. Free, open, simple.

DEV: add support for custom post message callbacks

Sam11b544ef4ec5
diff --git a/app/assets/javascripts/discourse/controllers/topic.js.es6 b/app/assets/javascripts/discourse/controllers/topic.js.es6
index 46448d84471..7a4bc0535d2 100644
--- a/app/assets/javascripts/discourse/controllers/topic.js.es6
+++ b/app/assets/javascripts/discourse/controllers/topic.js.es6
@@ -14,6 +14,16 @@ import { popupAjaxError } from 'discourse/lib/ajax-error';
import { spinnerHTML } from 'discourse/helpers/loading-spinner';
import { userPath } from 'discourse/lib/url';
+const customPostMessageCallbacks = {};
+
+export function registerCustomPostMessageCallback(type, callback) {
+ if (customPostMessageCallbacks[type]) {
+ throw `Error ${type} is an already registered post message!`;
+ }
+
+ customPostMessageCallbacks[type] = callback;
+}
+
export default Ember.Controller.extend(BufferedContent, {
composer: Ember.inject.controller(),
application: Ember.inject.controller(),
@@ -935,7 +945,12 @@ export default Ember.Controller.extend(BufferedContent, {
break;
}
default: {
- Em.Logger.warn("unknown topic bus message type", data);
+ let callback = customPostMessageCallbacks[data.type];
+ if (callback) {
+ callback(this, data);
+ } else {
+ Em.Logger.warn("unknown topic bus message type", data);
+ }
}
}
diff --git a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 b/app/assets/javascripts/discourse/lib/plugin-api.js.es6
index ce784807708..3222c2ddd26 100644
--- a/app/assets/javascripts/discourse/lib/plugin-api.js.es6
+++ b/app/assets/javascripts/discourse/lib/plugin-api.js.es6
@@ -25,9 +25,10 @@ import { modifySelectKit } from "select-kit/mixins/plugin-api";
import { addGTMPageChangedCallback } from 'discourse/lib/page-tracker';
import { registerCustomAvatarHelper } from 'discourse/helpers/user-avatar';
import { disableNameSuppression } from 'discourse/widgets/poster-name';
+import { registerCustomPostMessageCallback as registerCustomPostMessageCallback1 } from 'discourse/controllers/topic';
// If you add any methods to the API ensure you bump up this number
-const PLUGIN_API_VERSION = '0.8.21';
+const PLUGIN_API_VERSION = '0.8.22';
class PluginApi {
constructor(version, container) {
@@ -426,6 +427,24 @@ class PluginApi {
disableNameSuppression();
}
+ /**
+ * Registers a callback that will be invoked when the server calls
+ * Post#publish_change_to_clients! please ensure your type does not
+ * match acted,revised,rebaked,recovered, created,move_to_inbox or archived
+ *
+ * callback will be called with topicController and Message
+ *
+ * Example:
+ *
+ * api.registerCustomPostMessageCallback("applied_color", (topicController, message) => {
+ * let stream = topicController.get("model.postStream");
+ * // etc
+ * });
+ */
+ registerCustomPostMessageCallback(type, callback) {
+ registerCustomPostMessageCallback1(type, callback);
+ }
+
/**
* Changes a setting associated with a widget. For example, if
* you wanted small avatars in the post stream: