DEV: add support for custom post message callbacks

Allows for cleaner subscription to changes by plugins, polls will move to
it.
This commit is contained in:
Sam 2018-05-24 12:10:22 +10:00
parent 2aad91d4a2
commit 11b544ef4e
2 changed files with 36 additions and 2 deletions

View File

@ -14,6 +14,16 @@ import { popupAjaxError } from 'discourse/lib/ajax-error';
import { spinnerHTML } from 'discourse/helpers/loading-spinner'; import { spinnerHTML } from 'discourse/helpers/loading-spinner';
import { userPath } from 'discourse/lib/url'; 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, { export default Ember.Controller.extend(BufferedContent, {
composer: Ember.inject.controller(), composer: Ember.inject.controller(),
application: Ember.inject.controller(), application: Ember.inject.controller(),
@ -935,9 +945,14 @@ export default Ember.Controller.extend(BufferedContent, {
break; break;
} }
default: { default: {
let callback = customPostMessageCallbacks[data.type];
if (callback) {
callback(this, data);
} else {
Em.Logger.warn("unknown topic bus message type", data); Em.Logger.warn("unknown topic bus message type", data);
} }
} }
}
if (topic.get('isPrivateMessage') && if (topic.get('isPrivateMessage') &&
this.currentUser && this.currentUser &&

View File

@ -25,9 +25,10 @@ import { modifySelectKit } from "select-kit/mixins/plugin-api";
import { addGTMPageChangedCallback } from 'discourse/lib/page-tracker'; import { addGTMPageChangedCallback } from 'discourse/lib/page-tracker';
import { registerCustomAvatarHelper } from 'discourse/helpers/user-avatar'; import { registerCustomAvatarHelper } from 'discourse/helpers/user-avatar';
import { disableNameSuppression } from 'discourse/widgets/poster-name'; 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 // 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 { class PluginApi {
constructor(version, container) { constructor(version, container) {
@ -426,6 +427,24 @@ class PluginApi {
disableNameSuppression(); 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 * Changes a setting associated with a widget. For example, if
* you wanted small avatars in the post stream: * you wanted small avatars in the post stream: