示例#1
0
文件: og.go 项目: mattwkelly/rell
// Create a new Object from query string data.
func NewFromValues(context *context.Context, values url.Values) (*Object, error) {
	object := &Object{context: context}
	for key, values := range values {
		if strings.Contains(key, ":") {
			for _, value := range values {
				object.AddPair(key, value)
			}
		}
	}

	if object.shouldGenerate("og:url") {
		copiedValues := copyValues(values)
		copiedValues.Del("og:type")
		copiedValues.Del("og:title")
		url := url.URL{
			Scheme:   context.Scheme,
			Host:     context.Host,
			Path:     "/og/" + object.Type() + "/" + object.Title(),
			RawQuery: sortedEncode(copiedValues),
		}
		object.AddPair("og:url", url.String())
	}

	ogType := object.Type()
	isGlobalOGType := !strings.Contains(ogType, ":")
	isOwnedOGType := strings.HasPrefix(ogType, context.AppNamespace()+":")
	if object.shouldGenerate("fb:app_id") && (isGlobalOGType || isOwnedOGType) {
		object.AddPair("fb:app_id", strconv.FormatUint(context.AppID, 10))
	}

	err := object.generateDefaults()
	if err != nil {
		return nil, err
	}
	return object, nil
}