#4257 Added functionality to create previews for post links using open graph data from those links. (#4890)

Этот коммит содержится в:
Debanshu Kundu
2017-01-20 23:11:13 +05:30
коммит произвёл enahum
родитель fefe4b70d9
Коммит 3aaf71fdea
26 изменённых файлов: 1241 добавлений и 534 удалений

Просмотреть файл

@@ -8,6 +8,7 @@ import (
"strconv"
l4g "github.com/alecthomas/log4go"
"github.com/dyatlov/go-opengraph/opengraph"
"github.com/gorilla/mux"
"github.com/mattermost/platform/app"
"github.com/mattermost/platform/model"
@@ -18,6 +19,8 @@ import (
func InitPost() {
l4g.Debug(utils.T("api.post.init.debug"))
BaseRoutes.ApiRoot.Handle("/get_opengraph_metadata", ApiUserRequired(getOpenGraphMetadata)).Methods("POST")
BaseRoutes.NeedTeam.Handle("/posts/search", ApiUserRequiredActivity(searchPosts, true)).Methods("POST")
BaseRoutes.NeedTeam.Handle("/posts/flagged/{offset:[0-9]+}/{limit:[0-9]+}", ApiUserRequired(getFlaggedPosts)).Methods("GET")
BaseRoutes.NeedTeam.Handle("/posts/{post_id}", ApiUserRequired(getPostById)).Methods("GET")
@@ -649,3 +652,29 @@ func getFileInfosForPost(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(model.FileInfosToJson(infos)))
}
}
func getOpenGraphMetadata(c *Context, w http.ResponseWriter, r *http.Request) {
props := model.StringInterfaceFromJson(r.Body)
og := opengraph.NewOpenGraph()
res, err := http.Get(props["url"].(string))
if err != nil {
writeOpenGraphToResponse(w, og)
return
}
if err := og.ProcessHTML(res.Body); err != nil {
writeOpenGraphToResponse(w, og)
return
}
writeOpenGraphToResponse(w, og)
}
func writeOpenGraphToResponse(w http.ResponseWriter, og *opengraph.OpenGraph) {
ogJson, err := og.ToJSON()
if err != nil {
w.Write([]byte(`{"url": ""}`))
}
w.Write(ogJson)
}

Просмотреть файл

@@ -5,6 +5,7 @@ package api
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
@@ -1298,3 +1299,33 @@ func TestGetPermalinkTmp(t *testing.T) {
t.Fatal("should not be empty")
}
}
func TestGetOpenGraphMetadata(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
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]string){{"path": "/og-data/", "title": "Test Title"}, {"path": "/no-og-data/", "title": ""}} {
res, err := Client.DoApiPost("/get_opengraph_metadata", fmt.Sprintf("{\"url\":\"%s\"}", ts.URL+data["path"]))
if err != nil {
t.Fatal(err)
}
ogData := model.StringInterfaceFromJson(res.Body)
if strings.Compare(ogData["title"].(string), data["title"]) != 0 {
t.Fatal(fmt.Sprintf("OG data title mismatch for path \"%s\". Expected title: \"%s\". Actual title: \"%s\"", data["path"], data["title"], ogData["title"]))
}
}
}