Exemplo n.º 1
0
// Returns true if the given text represents a valid @username
func UsernameIsValid(username string) bool {
	if username == "" {
		return false
	}

	extracted := extract.ExtractMentionedScreenNames(username)
	return len(extracted) == 1 && extracted[0].Text == username
}
Exemplo n.º 2
0
func post(user *User, status string) error {

	postId, err := redis.Int(conn.Do("INCR", "next_post_id"))
	if err != nil {
		return err
	}
	status = strings.Replace(status, "\n", " ", -1)
	_, err = conn.Do("HMSET", fmt.Sprintf("post:%d", postId), "user_id", user.Id, "time", time.Now().Unix(), "body", status)
	if err != nil {
		return err
	}
	followers, err := redis.Strings(conn.Do("ZRANGE", "followers:"+user.Id, 0, -1))
	if err != nil {
		return err
	}
	recipients := mapset.NewSet()
	for _, fId := range followers {
		recipients.Add(fId)
	}
	entities := extract.ExtractMentionedScreenNames(status)
	for _, e := range entities {
		username, _ := e.ScreenName()
		profile, err := profileByUsername(username)
		if err == nil {
			recipients.Add(profile.Id)
		}
	}
	recipients.Add(user.Id)
	for fId := range recipients.Iter() {
		str, ok := fId.(string)
		if ok {
			conn.Do("LPUSH", "posts:"+str, postId)
		}
	}
	_, err = conn.Do("LPUSH", "timeline", postId)
	if err != nil {
		return err
	}
	_, err = conn.Do("LTRIM", "timeline", 0, 1000)
	if err != nil {
		return err
	}
	return nil
}