Пример #1
0
func handleExport(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	u := user.Current(c)
	if u == nil {
		return
	}

	marks, err := bookmarks.ByTags(c, []string{})
	if err != nil {
		http.Error(w, err.String(), http.StatusInternalServerError)
		return
	}

	// TODO: export view
	output(c, w, "export", map[string]interface{}{
		"count":     len(marks),
		"bookmarks": marks,
	})
}
Пример #2
0
func handleIndex(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	u := user.Current(c)
	if u == nil {
		output(c, w, "welcome")
		return
	}

	// Extract query information in form of ?q=multiple,tags+search+string
	fullQuery := r.FormValue("q")
	queryParts := strings.SplitN(fullQuery, " ", 2)
	tagString := queryParts[0]
	query := ""
	if len(queryParts) > 1 {
		query = queryParts[1]
	}

	// Get tag array (from tagString or default)
	var tags []string
	if tagString == "" && query == "" {
		tags = []string{"-follow"}
	} else {
		tags = strings.Split(tagString, ",")
	}

	// Follow mode or just listing?
	followMode := true
	if has, i := bookmarks.ContainsTag(tags, "-follow"); has {
		followMode = false
		tags = append(tags[:i], tags[i+1:]...)
	}

	// If tag "hidden" is not passed, hide all "hidden" tags
	if has, _ := bookmarks.ContainsTag(tags, "hidden"); !has {
		tags = append(tags, "-hidden")
	}

	// Fetch bookmarks with tags
	marks, err := bookmarks.ByTags(c, tags)
	if err != nil {
		http.Error(w, err.String(), http.StatusInternalServerError)
		return
	}

	// If no bookmarks with these tags are found, use "default" tag with query
	// as fallback, e.g. for search engine link
	if len(marks) == 0 {
		query = fullQuery
		tagString = "default"
		marks, err = bookmarks.ByTags(c, []string{"default"})
	}
	if err != nil {
		http.Error(w, err.String(), http.StatusInternalServerError)
		return
	}

	// Search query passed? Format URLs
	if query != "" {
		for i := 0; i < len(marks); i++ {
			marks[i].URL = strings.Replace(marks[i].URL, "%s", query, -1)
		}
	}

	// Navigate directly if a single bookmark was found
	if followMode && len(marks) == 1 {
		w.Header().Set("Location", marks[0].URL)
		w.WriteHeader(http.StatusFound)
		return
	}

	// Create title
	title := pluralize("Bookmark", len(marks), true)
	if tagString != "" {
		title += " tagged with '" + tagString + "'"
	}
	if query != "" {
		if tagString == "" {
			title += " with"
		} else {
			title += " and"
		}
		title += " query '" + query + "'"
	}

	output(c, w, "index", map[string]interface{}{
		"count":     len(marks),
		"title":     title,
		"query":     fullQuery,
		"tagString": tagString,
		"bookmarks": marks,
	})
}