コード例 #1
0
ファイル: account.go プロジェクト: shevilangle/sports
func userInfoHandler(request *http.Request, resp http.ResponseWriter,
	redis *models.RedisLogger, p Parameter) {
	user := &models.Account{}
	form := p.(getInfoForm)
	if find, err := user.FindByUserid(form.Userid); !find {
		if err == nil {
			err = errors.NewError(errors.NotExistsError)
		}
		writeResponse(request.RequestURI, resp, nil, err)
		return
	}

	info := convertUser(user, redis)
	//info.Pet = userPet(info.Props.Level)
	info.ScoreRank, _ = user.PropIndex("score", user.Props.Score)

	if uid := redis.OnlineUser(p.TokenId()); len(uid) > 0 {
		relation := redis.Relationship(uid, user.Id)
		switch relation {
		case models.RelFriend:
			info.Relation = "FRIENDS"
		case models.RelFollowing:
			info.Relation = "ATTENTION"
		case models.RelFollower:
			info.Relation = "FANS"
		case models.RelBlacklist:
			info.Relation = "DEFRIEND"
		}
	}

	writeResponse(request.RequestURI, resp, info, nil)
}
コード例 #2
0
ファイル: user.go プロジェクト: shevilangle/sports
func followHandler(request *http.Request, resp http.ResponseWriter,
	client *ApnClient, redis *models.RedisLogger, user *models.Account, p Parameter) {

	form := p.(relationshipForm)

	for _, peer := range form.Userids {
		if redis.Relationship(peer, user.Id) == models.RelBlacklist {
			writeResponse(request.RequestURI, resp, nil, errors.NewError(errors.DbError, "对方已屏蔽了你!"))
			return
		}
	}

	redis.SetRelationship(user.Id, form.Userids, models.RelFollowing, form.Follow)
	writeResponse(request.RequestURI, resp, map[string]interface{}{"ExpEffect": Awards{}}, nil)

	for _, userid := range form.Userids {
		u := &models.Account{}
		u.FindByUserid(userid)

		event := &models.Event{
			Type: models.EventMsg,
			Time: time.Now().Unix(),
			Data: models.EventData{
				Type: models.EventSub,
				Id:   user.Id + "-" + u.Id,
				From: user.Id,
				To:   u.Id,
				Body: []models.MsgBody{
					{Type: "nikename", Content: user.Nickname},
					{Type: "image", Content: user.Profile},
				},
			},
		}

		if form.Follow {
			/*
				if err := event.Save(); err == nil {
					redis.IncrEventCount(u.Id, event.Data.Type, 1)
				}
			*/
			event.Upsert()

			event.Data.Body = append(event.Data.Body,
				models.MsgBody{Type: "new_count", Content: "1"})
			redis.PubMsg(models.EventMsg, u.Id, event.Bytes())

			// apple push
			if u.Push {
				go sendApn(client, user.Nickname+"关注了你!", u.EventCount("", ""), u.Devs...)
			}
		} else {
			//count := u.DelEvent(models.EventSub, user.Id, user.Id, u.Id)
			//redis.IncrEventCount(u.Id, models.EventSub, -count)
			event.Delete()
		}
	}
}
コード例 #3
0
ファイル: account.go プロジェクト: shevilangle/sports
func recommendHandler(r *http.Request, w http.ResponseWriter,
	redis *models.RedisLogger, user *models.Account, p Parameter) {

	form := p.(recommendForm)

	excludes := redis.Friends(models.RelFollowing, user.Id)
	excludes = append(excludes, redis.Friends(models.RelBlacklist, user.Id)...)

	users, _ := user.Recommend(excludes)
	var list []*leaderboardResp
	for i, _ := range users {
		if users[i].Id == user.Id {
			continue
		}
		rel := redis.Relationship(user.Id, users[i].Id)
		if rel == models.RelFollowing || rel == models.RelFriend {
			continue
		}

		lb := &leaderboardResp{
			Userid:   users[i].Id,
			Score:    users[i].Props.Score,
			Level:    users[i].Level(),
			Profile:  users[i].Profile,
			Nickname: users[i].Nickname,
			Gender:   users[i].Gender,
			LastLog:  users[i].LastLogin.Unix(),
			Birth:    users[i].Birth,
			Location: users[i].Loc,
			Addr:     users[i].LocAddr,
			Phone:    users[i].Phone,
			Actor:    users[i].Actor,
		}
		lb.Distance, _ = redis.RecStats(users[i].Id)
		lb.Status = users[i].LatestArticle().Title
		list = append(list, lb)
	}

	respData := map[string]interface{}{
		"members_list":  list,
		"page_frist_id": form.Paging.First,
		"page_last_id":  form.Paging.Last,
	}
	writeResponse(r.RequestURI, w, respData, nil)
}
コード例 #4
0
ファイル: article.go プロジェクト: shevilangle/sports
func articleInfoHandler(request *http.Request, resp http.ResponseWriter,
	redis *models.RedisLogger, user *models.Account, p Parameter) {
	form := p.(articleInfoForm)
	article := &models.Article{}
	if find, err := article.FindById(form.Id); !find {
		if err == nil {
			err = errors.NewError(errors.NotExistsError)
		}
		writeResponse(request.RequestURI, resp, nil, err)
		return
	}

	event := &models.Event{}
	//event.Data.Type = models.EventThumb
	event.Data.Id = article.Id.Hex()
	event.Data.To = user.Id
	event.Clear() // clear all events about this article

	u := &models.Account{}
	u.FindByUserid(article.Author)
	author := convertUser(u, redis)
	jsonStruct := convertArticle(user, article, author)

	jsonStruct.Relation = redis.Relationship(user.Id, article.Author)
	switch jsonStruct.Relation {
	case models.RelFriend:
		jsonStruct.Relation = "FRIENDS"
	case models.RelFollowing:
		jsonStruct.Relation = "ATTENTION"
	case models.RelFollower:
		jsonStruct.Relation = "FANS"
	case models.RelBlacklist:
		jsonStruct.Relation = "DEFRIEND"
	}

	writeResponse(request.RequestURI, resp, jsonStruct, nil)
}
コード例 #5
0
ファイル: article.go プロジェクト: shevilangle/sports
func articleThumbHandler(request *http.Request, resp http.ResponseWriter,
	client *ApnClient, redis *models.RedisLogger, user *models.Account, p Parameter) {

	form := p.(articleThumbForm)
	article := &models.Article{}
	if find, err := article.FindById(form.Id); !find {
		e := errors.NewError(errors.NotExistsError, "文章不存在!")
		if err != nil {
			e = errors.NewError(errors.DbError)
		}
		writeResponse(request.RequestURI, resp, nil, e)
		return
	}

	if redis.Relationship(article.Author, user.Id) == models.RelBlacklist {
		writeResponse(request.RequestURI, resp, nil,
			errors.NewError(errors.AccessError, "对方屏蔽了你!"))
		return
	}

	if err := article.SetThumb(user.Id, form.Status); err != nil {
		writeResponse(request.RequestURI, resp, nil, err)
		return
	}

	awards := Awards{}
	if form.Status {
		awards = Awards{Score: 1, Wealth: 1 * models.Satoshi}
		GiveAwards(user, awards, redis)
	}
	writeResponse(request.RequestURI, resp, map[string]interface{}{"ExpEffect": awards}, nil)

	author := &models.Account{Id: article.Author}

	// ws push
	event := &models.Event{
		Type: models.EventArticle,
		Time: time.Now().Unix(),
		Data: models.EventData{
			Type: models.EventThumb,
			Id:   article.Id.Hex(),
			From: user.Id,
			To:   author.Id,
			Body: []models.MsgBody{
				{Type: "total_count", Content: strconv.Itoa(article.ThumbCount + 1)},
				{Type: "image", Content: article.Image},
			},
		},
	}

	if form.Status {
		author.FindByUserid(article.Author)
		/*
			if err := event.Save(); err == nil {
				redis.IncrEventCount(article.Author, event.Data.Type, 1)
			}
		*/
		event.Upsert()

		event.Data.Body = append(event.Data.Body,
			models.MsgBody{Type: "new_count",
				Content: strconv.Itoa(models.EventCount(event.Data.Type, event.Data.Id, event.Data.To))})
		redis.PubMsg(models.EventArticle, article.Author, event.Bytes())

		// apple push
		if author.Push {
			go sendApn(client, user.Nickname+"赞了你的主题!", author.EventCount("", ""), author.Devs...)
		}
	} else {
		//count := author.DelEvent(models.EventThumb, article.Id.Hex(), user.Id, author.Id)
		//redis.IncrEventCount(author.Id, models.EventThumb, -count)
		event.Delete()
	}
	//user.UpdateAction(ActThumb, nowDate())
}
コード例 #6
0
ファイル: article.go プロジェクト: shevilangle/sports
func newArticleHandler(request *http.Request, resp http.ResponseWriter,
	client *ApnClient, redis *models.RedisLogger, user *models.Account, p Parameter) {
	form := p.(newArticleForm)

	article := &models.Article{
		Author:   user.Id,
		Contents: form.Contents,
		PubTime:  time.Now(),
		Parent:   form.Parent,
		//Tags:     form.Tags,
		Loc:  form.Location,
		Type: form.Type,
	}
	article.Title, article.Images = articleCover(form.Contents)
	if len(article.Images) > 0 {
		article.Image = article.Images[0]
	}
	/*
		if len(article.Tags) == 0 {
			article.Tags = []string{"SPORT_LOG"}
		}
	*/

	awards := Awards{}
	parent := &models.Article{}
	if len(form.Parent) > 0 {
		if find, err := parent.FindById(form.Parent); !find {
			e := errors.NewError(errors.NotExistsError, "文章不存在!")
			if err != nil {
				e = errors.NewError(errors.DbError)
			}
			writeResponse(request.RequestURI, resp, nil, e)
			return
		}

		if redis.Relationship(parent.Author, user.Id) == models.RelBlacklist {
			writeResponse(request.RequestURI, resp, nil,
				errors.NewError(errors.AccessError, "对方屏蔽了你!"))
			return
		}

		awards = Awards{Literal: 1 + user.Level(), Score: 1 + user.Level()}
	} else {
		if user.Stat != nil && user.Stat.LastArticleTime < nowDate().Unix() {
			awards = Awards{Literal: 2 + user.Level(), Wealth: 2 * models.Satoshi, Score: 2 + user.Level()}
		}
		user.UpdateStat(models.StatLastArticleTime, time.Now().Unix())
	}

	if article.Type == models.ArticleRecord {
		article.Coaches = []string{user.Id}
	}

	if article.Type == models.ArticleCoach {
		if parent.Author != user.Id &&
			user.Actor != models.ActorCoach && user.Actor != models.ActorAdmin {
			writeResponse(request.RequestURI, resp, nil,
				errors.NewError(errors.AccessError))
			return
		}

		if err := article.Save(); err != nil {
			log.Println(err)
			writeResponse(request.RequestURI, resp, nil, err)
			return
		}

		// ws push
		event := &models.Event{
			Type: models.EventArticle,
			Time: time.Now().Unix(),
			Data: models.EventData{
				Type: models.EventCoach,
				Id:   parent.Id.Hex(),
				From: user.Id,
				//To:   parent.Author,
				Body: []models.MsgBody{
					{Type: "total_count", Content: strconv.Itoa(parent.CoachReviewCount + 1)},
					{Type: "image", Content: user.Profile},
				},
			},
		}

		for _, coach := range parent.Coaches {
			if coach == user.Id {
				continue
			}
			event.Data.To = coach
			event.Save()
			redis.PubMsg(models.EventArticle, coach, event.Bytes())
		}

		respData := map[string]interface{}{
			"ExpEffect": Awards{},
		}
		writeResponse(request.RequestURI, resp, respData, nil)
		return
	}

	if err := article.Save(); err != nil {
		log.Println(err)
		writeResponse(request.RequestURI, resp, nil, err)
		return
	}
	t := ""
	stat := models.StatArticles
	if len(form.Parent) > 0 {
		t = models.ArticleComment
		stat = models.StatComments
	}
	redis.AddPost(user.Id, t, 1)
	user.UpdateStat(stat, 1)

	if err := GiveAwards(user, awards, redis); err != nil {
		log.Println(err)
		writeResponse(request.RequestURI, resp, nil, errors.NewError(errors.DbError))
		return
	}

	// comment
	if len(form.Parent) > 0 && parent.Author != user.Id {
		//u := &models.User{Id: parent.Author}
		author := &models.Account{}
		author.FindByUserid(parent.Author)

		// ws push
		event := &models.Event{
			Type: models.EventArticle,
			Time: time.Now().Unix(),
			Data: models.EventData{
				Type: models.EventComment,
				Id:   parent.Id.Hex(),
				From: user.Id,
				To:   parent.Author,
				Body: []models.MsgBody{
					{Type: "total_count", Content: strconv.Itoa(parent.ReviewCount + 1)},
					{Type: "image", Content: parent.Image},
				},
			},
		}
		/*
			if err := event.Save(); err == nil {
				redis.IncrEventCount(parent.Author, event.Data.Type, 1)
			}
		*/
		event.Save()

		event.Data.Body = append(event.Data.Body,
			models.MsgBody{Type: "new_count",
				Content: strconv.Itoa(models.EventCount(event.Data.Type, event.Data.Id, event.Data.To))})
		redis.PubMsg(models.EventArticle, parent.Author, event.Bytes())
		// apple push
		if author.Push {
			go sendApn(client, user.Nickname+"评论了你的主题!", author.EventCount("", ""), author.Devs...)
		}
	}

	respData := map[string]interface{}{
		//"articles_without_content": convertArticle(article),
		"ExpEffect": awards,
	}
	writeResponse(request.RequestURI, resp, respData, nil)
}
コード例 #7
0
ファイル: wallet.go プロジェクト: shevilangle/sports
func txHandler(r *http.Request, w http.ResponseWriter,
	client *ApnClient, redis *models.RedisLogger, user *models.Account, p Parameter) {
	form := p.(txForm)

	if form.FromAddr == form.ToAddr {
		writeResponse(r.RequestURI, w, map[string]string{"txid": ""}, nil)
		return
	}

	if form.Value <= 0 {
		writeResponse(r.RequestURI, w, nil, errors.NewError(errors.AccessError, "无效的金额"))
		return
	}

	receiver := &models.Account{}
	if find, err := receiver.FindByWalletAddr(form.ToAddr); !find {
		e := errors.NewError(errors.NotFoundError, "无效的收款地址")
		if err != nil {
			e = errors.NewError(errors.DbError, "无效的钱包地址")
		}
		writeResponse(r.RequestURI, w, nil, e)
		return
	}

	if redis.Relationship(receiver.Id, user.Id) == models.RelBlacklist {
		writeResponse(r.RequestURI, w, nil,
			errors.NewError(errors.AccessError, "对方屏蔽了你!"))
		return
	}

	wal, err := getWallet(user.Wallet.Id, user.Wallet.Key)
	if err != nil {
		writeResponse(r.RequestURI, w, nil, errors.NewError(errors.DbError, "获取钱包失败"))
		return
	}

	outputs, amount, err := getUnspent(form.FromAddr, wal.Keys, form.Value)
	if err != nil {
		writeResponse(r.RequestURI, w, nil, errors.NewError(errors.DbError, "获取账户信息失败"))
		return
	}
	//log.Println("amount:", amount, "value:", form.Value)

	if form.Value > amount {
		writeResponse(r.RequestURI, w, nil, errors.NewError(errors.AccessError, "余额不足"))
		return
	}

	article := &models.Article{}
	if strings.ToLower(form.Type) == "reward" && len(form.Id) > 0 {
		if b, e := article.FindById(form.Id); !b {
			err := errors.NewError(errors.NotExistsError, "文章不存在!")
			if e != nil {
				err = errors.NewError(errors.DbError)
			}
			writeResponse(r.RequestURI, w, nil, err)
			return
		}
	}

	changeAddr := form.FromAddr
	if len(changeAddr) == 0 {
		changeAddr = wal.Keys[0].PubKey
	}
	rawtx, err := CreateRawTx2(outputs, amount, form.Value, form.ToAddr, changeAddr)
	if err != nil {
		writeResponse(r.RequestURI, w, nil, errors.NewError(errors.DbError))
		return
	}

	txid, err := sendRawTx(rawtx)
	if err != nil {
		writeResponse(r.RequestURI, w, nil, errors.NewError(errors.DbError))
		return
	}

	redis.Transaction(user.Id, receiver.Id, form.Value)
	// ws push
	event := &models.Event{
		Type: models.EventWallet,
		Time: time.Now().Unix(),
	}

	//log.Println("tx type:", strings.ToLower(form.Type))

	switch strings.ToLower(form.Type) {
	case "reward":

		article.Reward(user.Id, form.Value)

		event.Data = models.EventData{
			Type: models.EventReward,
			Id:   article.Id.Hex(),
			From: user.Id,
			To:   receiver.Id,
			Body: []models.MsgBody{
				{Type: "nikename", Content: user.Nickname},
				{Type: "image", Content: article.Image},
				{Type: "total_count", Content: strconv.FormatInt(article.TotalReward, 10)},
				//{Type: "new_count", Content: strconv.Itoa(models.EventCount(models.EventReward, article.Id.Hex()) + 1)},
			},
		}

	default:
		event.Data = models.EventData{
			Type: models.EventTx,
			Id:   user.Id + "-" + receiver.Id,
			From: user.Id,
			To:   receiver.Id,
			Body: []models.MsgBody{
				{Type: "nikename", Content: user.Nickname},
				{Type: "image", Content: user.Profile},
				{Type: "total_count", Content: strconv.FormatInt(form.Value, 10)},
			},
		}
	}

	if user.Id != receiver.Id {
		/*
			if err := event.Save(); err == nil {
				redis.IncrEventCount(receiver.Id, event.Data.Type, 1)
			}
		*/
		event.Save()
		event.Data.Body = append(event.Data.Body,
			models.MsgBody{Type: "new_count",
				Content: strconv.Itoa(models.EventCount(event.Data.Type, event.Data.Id, event.Data.To))})
		redis.PubMsg("wallet", receiver.Id, event.Bytes())
	}

	if receiver.Push {
		msg := user.Nickname + "通过转帐发送给你" +
			strconv.FormatFloat(float64(form.Value)/float64(models.Satoshi), 'f', 8, 64) + "个贝币"
		if event.Data.Type == models.EventReward {
			msg = user.Nickname + "给你的文章打赏了" +
				strconv.FormatFloat(float64(form.Value)/float64(models.Satoshi), 'f', 8, 64) + "个贝币"
		}
		go sendApn(client, msg, receiver.EventCount("", ""), receiver.Devs...)
	}

	writeResponse(r.RequestURI, w, map[string]string{"txid": txid}, nil)
}