From 0c03ccb01e1ee96d48582960fed451836f9fc637 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 7 Mar 2017 17:12:31 -0500 Subject: [PATCH] FEATURE: allow plugins to transform, the transformed post This allows plugins to amend posts prior to rendering. --- .../discourse/lib/plugin-api.js.es6 | 21 ++++++++++++++++++- .../discourse/widgets/post-stream.js.es6 | 17 +++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 index 5ff39ada3a3..0b3e85739be 100644 --- a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 +++ b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 @@ -17,9 +17,10 @@ import { addDiscoveryQueryParam } from 'discourse/controllers/discovery-sortable import { addTagsHtmlCallback } from 'discourse/lib/render-tags'; import { addUserMenuGlyph } from 'discourse/widgets/user-menu'; import { addPostClassesCallback } from 'discourse/widgets/post'; +import { addPostTransformCallback } from 'discourse/widgets/post-stream'; // If you add any methods to the API ensure you bump up this number -const PLUGIN_API_VERSION = '0.8.4'; +const PLUGIN_API_VERSION = '0.8.5'; class PluginApi { constructor(version, container) { @@ -438,6 +439,24 @@ class PluginApi { addPostClassesCallback(callback) { addPostClassesCallback(callback); } + + /** + * + * Adds a callback to be executed on the "transformed" post that is passed to the post + * widget. + * + * This allows you to apply transformations on the actual post that is about to be rendered. + * + * Example: + * + * addPostTransformCallback((t)=>{ + * // post number 7 is overrated, don't show it ever + * if (t.post_number === 7) { t.cooked = ""; } + * }) + */ + addPostTransformCallback(callback) { + addPostTransformCallback(callback); + } } let _pluginv01; diff --git a/app/assets/javascripts/discourse/widgets/post-stream.js.es6 b/app/assets/javascripts/discourse/widgets/post-stream.js.es6 index 9488cf7686a..edc75be8f69 100644 --- a/app/assets/javascripts/discourse/widgets/post-stream.js.es6 +++ b/app/assets/javascripts/discourse/widgets/post-stream.js.es6 @@ -3,6 +3,21 @@ import transformPost from 'discourse/lib/transform-post'; import { Placeholder } from 'discourse/lib/posts-with-placeholders'; import { addWidgetCleanCallback } from 'discourse/components/mount-widget'; +let transformCallbacks = null; +function postTransformCallbacks(transformed) { + if (transformCallbacks === null) { + return; + } + + for(let i=0; i < transformCallbacks.length; i++) { + transformCallbacks[i].call(this, transformed); + } +} +export function addPostTransformCallback(callback){ + transformCallbacks = transformCallbacks || []; + transformCallbacks.push(callback); +}; + const CLOAKING_ENABLED = !window.inTestEnv; const DAY = 1000 * 60 * 60 * 24; @@ -96,6 +111,8 @@ export default createWidget('post-stream', { transformed.height = _heights[post.id]; transformed.cloaked = _cloaked[post.id]; + postTransformCallbacks(transformed); + if (transformed.isSmallAction) { result.push(this.attach('post-small-action', transformed, { model: post })); } else {