Пример #1
0
func (c *CmdSearch) showJSONResults(results []keybase1.UserSummary) error {
	output := jsonw.NewArray(len(results))
	for userIndex, user := range results {
		userBlob := jsonw.NewDictionary()
		userBlob.SetKey("username", jsonw.NewString(user.Username))
		for _, social := range user.Proofs.Social {
			userBlob.SetKey(social.ProofType, jsonw.NewString(social.ProofName))
		}

		if len(user.Proofs.Web) > 0 {
			var webProofs []string
			for _, webProof := range user.Proofs.Web {
				for _, protocol := range webProof.Protocols {
					webProofs = append(webProofs, libkb.MakeURI(protocol, webProof.Hostname))
				}
			}
			websites := jsonw.NewArray(len(webProofs))
			for i, wp := range webProofs {
				websites.SetIndex(i, jsonw.NewString(wp))
			}
			userBlob.SetKey("websites", websites)
		}

		output.SetIndex(userIndex, userBlob)
	}
	GlobUI.Println(output.MarshalPretty())
	return nil
}
Пример #2
0
func (e *SearchEngine) Run(ctx *Context) error {
	APIArgs := libkb.HTTPArgs{
		"q": libkb.S{Val: e.query},
	}
	if e.numWanted > 0 {
		APIArgs["num_wanted"] = libkb.I{Val: e.numWanted}
	}
	res, err := e.G().API.Get(libkb.APIArg{
		Endpoint: "user/autocomplete",
		Args:     APIArgs,
	})
	if err != nil {
		return err
	}
	length, err := res.Body.AtKey("completions").Len()
	if err != nil {
		return fmt.Errorf("Failed to get completions from server.")
	}
	allCompletions := res.Body.AtKey("completions")
	for i := 0; i < length; i++ {
		componentKeys, err := allCompletions.AtIndex(i).AtKey("components").Keys()
		if err != nil {
			return fmt.Errorf("Failed to get completion components from server.")
		}
		completion := allCompletions.AtIndex(i)
		components := completion.AtKey("components")
		searchComponents := []keybase1.SearchComponent{}

		add := func(key string, val string, score float64) {
			searchComponents = append(searchComponents, keybase1.SearchComponent{
				Key:   key,
				Value: val,
				Score: score,
			})
		}

		for _, key := range componentKeys {
			if key == "websites" {
				n, err := components.AtKey(key).Len()
				if err != nil {
					return err
				}
				for i := 0; i < n; i++ {
					obj := components.AtKey(key).AtIndex(i)
					val, err := obj.AtKey("val").GetString()
					if err != nil {
						return err
					}
					score, err := obj.AtKey("score").GetFloat()
					if err != nil {
						return err
					}
					protocol, err := obj.AtKey("protocol").GetString()
					if err != nil {
						return err
					}
					if protocol == "" {
						return err
					}
					add(key, libkb.MakeURI(protocol, val), score)
				}

			} else {
				val, err := components.AtKey(key).AtKey("val").GetString()
				if err != nil {
					return err
				}
				score, err := components.AtKey(key).AtKey("score").GetFloat()
				if err != nil {
					return err
				}
				add(key, val, score)
			}
		}
		username, err := components.AtKey("username").AtKey("val").GetString()
		if err != nil {
			return err
		}
		uidString, err := completion.AtKey("uid").GetString()
		if err != nil {
			return err
		}
		uid, err := libkb.UIDFromHex(uidString)
		if err != nil {
			return err
		}
		totalScore, err := completion.AtKey("total_score").GetFloat()
		if err != nil {
			return err
		}
		e.results = append(e.results, keybase1.SearchResult{
			Uid:        uid,
			Username:   username,
			Components: searchComponents,
			Score:      totalScore,
		})
	}
	return nil
}