#4257 Added functionality to create previews for post links using open graph data from those links. (#4890)
Этот коммит содержится в:
коммит произвёл
enahum
родитель
fefe4b70d9
Коммит
3aaf71fdea
29
api/post.go
29
api/post.go
@@ -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)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user