summaryrefslogtreecommitdiffstats
path: root/api4/openGraph.go
diff options
context:
space:
mode:
authorCarlos Tadeu Panato Junior <ctadeu@gmail.com>2017-05-10 12:44:18 +0200
committerGeorge Goldberg <george@gberg.me>2017-05-10 11:44:18 +0100
commit16581ae431ffeae97db18eb8672232505a7ce3c0 (patch)
treeb87317febf6f2ee4588fa3c8b48ed7a1cf81c048 /api4/openGraph.go
parentb9f4ced52b65d147b57d7028f6df4935a6aea8f9 (diff)
downloadchat-16581ae431ffeae97db18eb8672232505a7ce3c0.tar.gz
chat-16581ae431ffeae97db18eb8672232505a7ce3c0.tar.bz2
chat-16581ae431ffeae97db18eb8672232505a7ce3c0.zip
implement open graph metadata for apiV4 (#6343)
Diffstat (limited to 'api4/openGraph.go')
-rw-r--r--api4/openGraph.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/api4/openGraph.go b/api4/openGraph.go
new file mode 100644
index 000000000..e9e998474
--- /dev/null
+++ b/api4/openGraph.go
@@ -0,0 +1,56 @@
+// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package api4
+
+import (
+ "net/http"
+
+ l4g "github.com/alecthomas/log4go"
+ "github.com/mattermost/platform/app"
+ "github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/utils"
+)
+
+const OPEN_GRAPH_METADATA_CACHE_SIZE = 10000
+
+var openGraphDataCache = utils.NewLru(OPEN_GRAPH_METADATA_CACHE_SIZE)
+
+func InitOpenGraph() {
+ l4g.Debug(utils.T("api.opengraph.init.debug"))
+
+ BaseRoutes.OpenGraph.Handle("", ApiSessionRequired(getOpenGraphMetadata)).Methods("POST")
+}
+
+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)
+
+ url := ""
+ ok := false
+ if url, ok = props["url"].(string); len(url) == 0 || !ok {
+ c.SetInvalidParam("url")
+ return
+ }
+
+ ogJSONGeneric, ok := openGraphDataCache.Get(url)
+ if ok {
+ w.Write(ogJSONGeneric.([]byte))
+ return
+ }
+
+ og := app.GetOpenGraphMetadata(url)
+
+ ogJSON, err := og.ToJSON()
+ openGraphDataCache.AddWithExpiresInSecs(props["url"], ogJSON, 3600) // Cache would expire after 1 hour
+ if err != nil {
+ w.Write([]byte(`{"url": ""}`))
+ return
+ }
+
+ w.Write(ogJSON)
+}