From e87f5c6cf912a4f650a056ee042f19268963177d Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Mon, 6 Mar 2017 21:13:12 -0500 Subject: [PATCH] Add system console switch for enabling link previews (#5663) --- api/post.go | 5 ++ api/post_test.go | 11 ++++ config/config.json | 1 + i18n/en.json | 4 ++ model/config.go | 6 ++ utils/config.go | 1 + webapp/actions/global_actions.jsx | 4 ++ .../admin_console/admin_sidebar.jsx | 10 +++ .../admin_console/link_previews_settings.jsx | 66 +++++++++++++++++++ .../user_settings/user_settings_advanced.jsx | 17 +++-- webapp/i18n/en.json | 4 ++ webapp/routes/route_admin_console.jsx | 5 ++ 12 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 webapp/components/admin_console/link_previews_settings.jsx diff --git a/api/post.go b/api/post.go index 9c22dc5eef..4e7801e2b8 100644 --- a/api/post.go +++ b/api/post.go @@ -452,6 +452,11 @@ func getFileInfosForPost(c *Context, w http.ResponseWriter, r *http.Request) { } func getOpenGraphMetadata(c *Context, w http.ResponseWriter, r *http.Request) { + if !*utils.Cfg.ServiceSettings.EnableLinkPreviews { + c.Err = model.NewAppError("getOpenGraphMetadata", "api.post.link_preview_disabled.app_error", nil, "", http.StatusNotImplemented) + return + } + props := model.StringInterfaceFromJson(r.Body) ogJSONGeneric, ok := openGraphDataCache.Get(props["url"]) diff --git a/api/post_test.go b/api/post_test.go index b93b5b6a62..39ef2f6c08 100644 --- a/api/post_test.go +++ b/api/post_test.go @@ -1307,6 +1307,12 @@ func TestGetOpenGraphMetadata(t *testing.T) { th := Setup().InitBasic() Client := th.BasicClient + enableLinkPreviews := *utils.Cfg.ServiceSettings.EnableLinkPreviews + defer func() { + *utils.Cfg.ServiceSettings.EnableLinkPreviews = enableLinkPreviews + }() + *utils.Cfg.ServiceSettings.EnableLinkPreviews = true + ogDataCacheMissCount := 0 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -1355,4 +1361,9 @@ func TestGetOpenGraphMetadata(t *testing.T) { )) } } + + *utils.Cfg.ServiceSettings.EnableLinkPreviews = false + if _, err := Client.DoApiPost("/get_opengraph_metadata", "{\"url\":\"/og-data/\"}"); err == nil || err.StatusCode != http.StatusNotImplemented { + t.Fatal("should have failed with 501 - disabled link previews") + } } diff --git a/config/config.json b/config/config.json index 3c4ed9bf39..18594f60dd 100644 --- a/config/config.json +++ b/config/config.json @@ -19,6 +19,7 @@ "EnableOnlyAdminIntegrations": true, "EnablePostUsernameOverride": false, "EnablePostIconOverride": false, + "EnableLinkPreviews": false, "EnableTesting": false, "EnableDeveloper": false, "EnableSecurityFixAlert": true, diff --git a/i18n/en.json b/i18n/en.json index 156d6d8b28..14b8383da6 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -3,6 +3,10 @@ "id": "April", "translation": "April" }, + { + "id": "api.post.link_preview_disabled.app_error", + "translation": "Link previews have been disabled by the system administrator." + }, { "id": "August", "translation": "August" diff --git a/model/config.go b/model/config.go index 37c1dae36c..86693b57ee 100644 --- a/model/config.go +++ b/model/config.go @@ -129,6 +129,7 @@ type ServiceSettings struct { EnableOnlyAdminIntegrations *bool EnablePostUsernameOverride bool EnablePostIconOverride bool + EnableLinkPreviews *bool EnableTesting bool EnableDeveloper *bool EnableSecurityFixAlert *bool @@ -495,6 +496,11 @@ func (o *Config) SetDefaults() { *o.ServiceSettings.SiteURL = SERVICE_SETTINGS_DEFAULT_SITE_URL } + if o.ServiceSettings.EnableLinkPreviews == nil { + o.ServiceSettings.EnableLinkPreviews = new(bool) + *o.ServiceSettings.EnableLinkPreviews = false + } + if o.ServiceSettings.EnableDeveloper == nil { o.ServiceSettings.EnableDeveloper = new(bool) *o.ServiceSettings.EnableDeveloper = false diff --git a/utils/config.go b/utils/config.go index fd1597132a..2996c241c1 100644 --- a/utils/config.go +++ b/utils/config.go @@ -267,6 +267,7 @@ func getClientConfig(c *model.Config) map[string]string { props["EnableOnlyAdminIntegrations"] = strconv.FormatBool(*c.ServiceSettings.EnableOnlyAdminIntegrations) props["EnablePostUsernameOverride"] = strconv.FormatBool(c.ServiceSettings.EnablePostUsernameOverride) props["EnablePostIconOverride"] = strconv.FormatBool(c.ServiceSettings.EnablePostIconOverride) + props["EnableLinkPreviews"] = strconv.FormatBool(*c.ServiceSettings.EnableLinkPreviews) props["EnableTesting"] = strconv.FormatBool(c.ServiceSettings.EnableTesting) props["EnableDeveloper"] = strconv.FormatBool(*c.ServiceSettings.EnableDeveloper) props["EnableDiagnostics"] = strconv.FormatBool(*c.LogSettings.EnableDiagnostics) diff --git a/webapp/actions/global_actions.jsx b/webapp/actions/global_actions.jsx index c4b29a2314..e22c942948 100644 --- a/webapp/actions/global_actions.jsx +++ b/webapp/actions/global_actions.jsx @@ -623,6 +623,10 @@ export function redirectUserToDefaultTeam() { requestOpenGraphMetadata.openGraphMetadataOnGoingRequests = {}; // Format: {: true} export function requestOpenGraphMetadata(url) { + if (global.mm_config.EnableLinkPreviews !== 'true') { + return; + } + const onself = requestOpenGraphMetadata; if (!onself.openGraphMetadataOnGoingRequests[url]) { diff --git a/webapp/components/admin_console/admin_sidebar.jsx b/webapp/components/admin_console/admin_sidebar.jsx index c53836ef65..2f299bdeb4 100644 --- a/webapp/components/admin_console/admin_sidebar.jsx +++ b/webapp/components/admin_console/admin_sidebar.jsx @@ -689,6 +689,16 @@ export default class AdminSidebar extends React.Component { } /> + + + } + /> + + + ); + } + + renderSettings() { + return ( + + + } + helpText={ + + } + value={this.state.enableLinkPreviews} + onChange={this.handleChange} + /> + + ); + } +} diff --git a/webapp/components/user_settings/user_settings_advanced.jsx b/webapp/components/user_settings/user_settings_advanced.jsx index 6e3d488e1e..3459af8b3c 100644 --- a/webapp/components/user_settings/user_settings_advanced.jsx +++ b/webapp/components/user_settings/user_settings_advanced.jsx @@ -53,14 +53,19 @@ export default class AdvancedSettingsDisplay extends React.Component { ) }; + const webrtcEnabled = global.mm_config.EnableWebrtc === 'true'; + const linkPreviewsEnabled = global.mm_config.EnableLinkPreviews === 'true'; + + if (!webrtcEnabled) { + preReleaseFeaturesKeys = preReleaseFeaturesKeys.filter((f) => f !== 'WEBRTC_PREVIEW'); + } + + if (!linkPreviewsEnabled) { + preReleaseFeaturesKeys = preReleaseFeaturesKeys.filter((f) => f !== 'EMBED_PREVIEW'); + } + let enabledFeatures = 0; for (const [name, value] of advancedSettings) { - const webrtcEnabled = global.mm_config.EnableWebrtc === 'true'; - - if (!webrtcEnabled) { - preReleaseFeaturesKeys = preReleaseFeaturesKeys.filter((f) => f !== 'WEBRTC_PREVIEW'); - } - for (const key of preReleaseFeaturesKeys) { const feature = PreReleaseFeatures[key]; diff --git a/webapp/i18n/en.json b/webapp/i18n/en.json index cf04ccc0e0..53f7a7f3eb 100644 --- a/webapp/i18n/en.json +++ b/webapp/i18n/en.json @@ -1,4 +1,8 @@ { + "admin.customization.enableLinkPreviewsDesc": "Enable users to display a preview of website content below the message, if available. When true, website previews can be enabled from Account Settings > Advanced > Preview pre-release features.", + "admin.customization.enableLinkPreviewsTitle": "Enable Link Previews:", + "admin.customization.linkPreviews": "Link Previews", + "admin.sidebar.linkPreviews": "Link Previews", "multiselect.go": "Go", "multiselect.instructions": "Use up/down arrows to navigate and enter to select", "multiselect.placeholder": "Search and add members", diff --git a/webapp/routes/route_admin_console.jsx b/webapp/routes/route_admin_console.jsx index 5b0f5d28e0..f983af9f5c 100644 --- a/webapp/routes/route_admin_console.jsx +++ b/webapp/routes/route_admin_console.jsx @@ -35,6 +35,7 @@ import StorageSettings from 'components/admin_console/storage_settings.jsx'; import ImageSettings from 'components/admin_console/image_settings.jsx'; import CustomBrandSettings from 'components/admin_console/custom_brand_settings.jsx'; import CustomEmojiSettings from 'components/admin_console/custom_emoji_settings.jsx'; +import LinkPreviewsSettings from 'components/admin_console/link_previews_settings.jsx'; import LegalAndSupportSettings from 'components/admin_console/legal_and_support_settings.jsx'; import NativeAppLinkSettings from 'components/admin_console/native_app_link_settings.jsx'; import ComplianceSettings from 'components/admin_console/compliance_settings.jsx'; @@ -180,6 +181,10 @@ export default ( path='custom_emoji' component={CustomEmojiSettings} /> +