implement open graph metadata for apiV4 (#6343)
Этот коммит содержится в:
коммит произвёл
George Goldberg
родитель
b9f4ced52b
Коммит
16581ae431
@@ -69,6 +69,8 @@ type Routes struct {
|
|||||||
OAuthApps *mux.Router // 'api/v4/oauth/apps'
|
OAuthApps *mux.Router // 'api/v4/oauth/apps'
|
||||||
OAuthApp *mux.Router // 'api/v4/oauth/apps/{app_id:[A-Za-z0-9]+}'
|
OAuthApp *mux.Router // 'api/v4/oauth/apps/{app_id:[A-Za-z0-9]+}'
|
||||||
|
|
||||||
|
OpenGraph *mux.Router // 'api/v4/opengraph'
|
||||||
|
|
||||||
SAML *mux.Router // 'api/v4/saml'
|
SAML *mux.Router // 'api/v4/saml'
|
||||||
Compliance *mux.Router // 'api/v4/compliance'
|
Compliance *mux.Router // 'api/v4/compliance'
|
||||||
Cluster *mux.Router // 'api/v4/cluster'
|
Cluster *mux.Router // 'api/v4/cluster'
|
||||||
@@ -174,6 +176,8 @@ func InitApi(full bool) {
|
|||||||
|
|
||||||
BaseRoutes.Webrtc = BaseRoutes.ApiRoot.PathPrefix("/webrtc").Subrouter()
|
BaseRoutes.Webrtc = BaseRoutes.ApiRoot.PathPrefix("/webrtc").Subrouter()
|
||||||
|
|
||||||
|
BaseRoutes.OpenGraph = BaseRoutes.ApiRoot.PathPrefix("/opengraph").Subrouter()
|
||||||
|
|
||||||
InitUser()
|
InitUser()
|
||||||
InitTeam()
|
InitTeam()
|
||||||
InitChannel()
|
InitChannel()
|
||||||
@@ -194,6 +198,7 @@ func InitApi(full bool) {
|
|||||||
InitOAuth()
|
InitOAuth()
|
||||||
InitReaction()
|
InitReaction()
|
||||||
InitWebrtc()
|
InitWebrtc()
|
||||||
|
InitOpenGraph()
|
||||||
|
|
||||||
app.Srv.Router.Handle("/api/v4/{anything:.*}", http.HandlerFunc(Handle404))
|
app.Srv.Router.Handle("/api/v4/{anything:.*}", http.HandlerFunc(Handle404))
|
||||||
|
|
||||||
|
|||||||
56
api4/openGraph.go
Обычный файл
56
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)
|
||||||
|
}
|
||||||
73
api4/openGraph_test.go
Обычный файл
73
api4/openGraph_test.go
Обычный файл
@@ -0,0 +1,73 @@
|
|||||||
|
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package api4
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mattermost/platform/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetOpenGraphMetadata(t *testing.T) {
|
||||||
|
th := Setup().InitBasic()
|
||||||
|
Client := th.Client
|
||||||
|
|
||||||
|
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) {
|
||||||
|
ogDataCacheMissCount++
|
||||||
|
|
||||||
|
if r.URL.Path == "/og-data/" {
|
||||||
|
fmt.Fprintln(w, `
|
||||||
|
<html><head><meta property="og:type" content="article" />
|
||||||
|
<meta property="og:title" content="Test Title" />
|
||||||
|
<meta property="og:url" content="http://example.com/" />
|
||||||
|
</head><body></body></html>
|
||||||
|
`)
|
||||||
|
} else if r.URL.Path == "/no-og-data/" {
|
||||||
|
fmt.Fprintln(w, `<html><head></head><body></body></html>`)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
for _, data := range [](map[string]interface{}){
|
||||||
|
{"path": "/og-data/", "title": "Test Title", "cacheMissCount": 1},
|
||||||
|
{"path": "/no-og-data/", "title": "", "cacheMissCount": 2},
|
||||||
|
|
||||||
|
// Data should be cached for following
|
||||||
|
{"path": "/og-data/", "title": "Test Title", "cacheMissCount": 2},
|
||||||
|
{"path": "/no-og-data/", "title": "", "cacheMissCount": 2},
|
||||||
|
} {
|
||||||
|
|
||||||
|
openGraph, resp := Client.OpenGraph(ts.URL + data["path"].(string))
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
if strings.Compare(openGraph["title"], data["title"].(string)) != 0 {
|
||||||
|
t.Fatal(fmt.Sprintf(
|
||||||
|
"OG data title mismatch for path \"%s\". Expected title: \"%s\". Actual title: \"%s\"",
|
||||||
|
data["path"].(string), data["title"].(string), openGraph["title"],
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
if ogDataCacheMissCount != data["cacheMissCount"].(int) {
|
||||||
|
t.Fatal(fmt.Sprintf(
|
||||||
|
"Cache miss count didn't match. Expected value %d. Actual value %d.",
|
||||||
|
data["cacheMissCount"].(int), ogDataCacheMissCount,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*utils.Cfg.ServiceSettings.EnableLinkPreviews = false
|
||||||
|
_, resp := Client.OpenGraph(ts.URL + "/og-data/")
|
||||||
|
CheckNotImplementedStatus(t, resp)
|
||||||
|
}
|
||||||
@@ -1699,6 +1699,10 @@
|
|||||||
"id": "api.reaction.init.debug",
|
"id": "api.reaction.init.debug",
|
||||||
"translation": "Initializing reactions api routes"
|
"translation": "Initializing reactions api routes"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"id": "api.opengraph.init.debug",
|
||||||
|
"translation": "Initializing open graph protocol api routes"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"id": "api.reaction.list_reactions.mismatched_channel_id.app_error",
|
"id": "api.reaction.list_reactions.mismatched_channel_id.app_error",
|
||||||
"translation": "Failed to get reactions because channel ID does not match post ID in the URL"
|
"translation": "Failed to get reactions because channel ID does not match post ID in the URL"
|
||||||
|
|||||||
@@ -250,6 +250,10 @@ func (c *Client4) GetOAuthAppRoute(appId string) string {
|
|||||||
return fmt.Sprintf("/oauth/apps/%v", appId)
|
return fmt.Sprintf("/oauth/apps/%v", appId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client4) GetOpenGraphRoute() string {
|
||||||
|
return fmt.Sprintf("/opengraph")
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client4) DoApiGet(url string, etag string) (*http.Response, *AppError) {
|
func (c *Client4) DoApiGet(url string, etag string) (*http.Response, *AppError) {
|
||||||
return c.DoApiRequest(http.MethodGet, c.ApiUrl+url, "", etag)
|
return c.DoApiRequest(http.MethodGet, c.ApiUrl+url, "", etag)
|
||||||
}
|
}
|
||||||
@@ -2575,3 +2579,18 @@ func (c *Client4) DeleteReaction(reaction *Reaction) (bool, *Response) {
|
|||||||
return CheckStatusOK(r), BuildResponse(r)
|
return CheckStatusOK(r), BuildResponse(r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Open Graph Metadata Section
|
||||||
|
|
||||||
|
// OpenGraph return the open graph metadata for a particular url if the site have the metadata
|
||||||
|
func (c *Client4) OpenGraph(url string) (map[string]string, *Response) {
|
||||||
|
requestBody := make(map[string]string)
|
||||||
|
requestBody["url"] = url
|
||||||
|
|
||||||
|
if r, err := c.DoApiPost(c.GetOpenGraphRoute(), MapToJson(requestBody)); err != nil {
|
||||||
|
return nil, &Response{StatusCode: r.StatusCode, Error: err}
|
||||||
|
} else {
|
||||||
|
defer closeBody(r)
|
||||||
|
return MapFromJson(r.Body), BuildResponse(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user