Beispiel #1
0
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)
}
Beispiel #2
0
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())
}
Beispiel #3
0
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)
}