Beispiel #1
0
// 保存link到数据库,如果成功,则返回link的id
func Link_SaveMap(m map[string]interface{}) int64 {
	var db *goku.MysqlDB = GetDB()
	defer db.Close()
	m["create_time"] = time.Now()
	//新增link默认投票1次,显示的时候默认减一
	m["vote_up"] = 0 //1
	m["reddit_score"] = utils.LinkSortAlgorithm(m["create_time"].(time.Time), int64(0), int64(0))
	m["context_md5"] = utils.MD5_16(strings.ToLower(m["context"].(string)))

	r, err := db.Insert("link", m)
	if err != nil {
		goku.Logger().Errorln(err.Error())
		return 0
	}
	var id int64
	id, err = r.LastInsertId()
	if err != nil {
		goku.Logger().Errorln(err.Error())
		return 0
	}

	if id > 0 {
		uid := m["user_id"].(int64)
		// 更新用户的链接计数
		IncCountById(db, "user", uid, "link_count", 1)
		// 直接推送给自己,自己必须看到
		LinkForUser_Add(uid, id, LinkForUser_ByUser)

		// 存入`tui_link_for_handle` 链接处理队列表
		db.Query("INSERT ignore INTO tui_link_for_handle(link_id,create_time,user_id,insert_time,data_type) VALUES (?, ?, ?, NOW(), ?)",
			id, m["create_time"].(time.Time), uid, 1)

		redisClient := GetRedis()
		defer redisClient.Quit()
		// 加入推送队列
		// 格式: pushtype,userid,linkid,timestamp
		qv := fmt.Sprintf("%v,%v,%v,%v", LinkForUser_ByUser, uid, id, time.Now().Unix())
		_, err = redisClient.Lpush(golink.KEY_LIST_PUSH_TO_USER, qv)
		if err != nil {
			goku.Logger().Errorln(err.Error())
			// return 0
		}

	}

	return id
}
Beispiel #2
0
// 保存评论到数据库,如果成功,则返回comment的id
func Comment_SaveMap(m map[string]interface{}) (int64, error) {
	var db *goku.MysqlDB = GetDB()
	defer db.Close()

	var err error
	// 评论的链接存不存在?
	linkId := m["link_id"].(int64)
	link, err := Link_GetById(linkId)
	if err != nil {
		return 0, err
	} else if link.Id < 1 {
		return int64(0), errors.New("评论的链接不存在")
	}

	// 检查父评论是否存在
	var pComment *Comment
	if id, ok := m["parent_id"].(int64); ok && id > 0 {
		pComment, err = Comment_GetById(id)
		if err != nil {
			goku.Logger().Errorln(err.Error())
			return int64(0), err
		}
		// 指定了父评论的id但是数据库中没有
		if pComment == nil {
			return int64(0), errors.New("指定的父评论不存在")
		}
	} else if !ok {
		m["parent_id"] = int64(0)
	}

	// 路径相关
	if pComment == nil {
		m["parent_id"] = int64(0)
		m["top_parent_id"] = int64(0)
		m["parent_path"] = "/"
		m["deep"] = 0
	} else {
		m["parent_id"] = pComment.Id
		if pComment.TopParentId == 0 {
			m["top_parent_id"] = pComment.Id
		} else {
			m["top_parent_id"] = pComment.TopParentId
		}
		m["parent_path"] = fmt.Sprintf("%v%v/", pComment.ParentPath, pComment.Id)
		m["deep"] = pComment.Deep + 1
	}

	m["status"] = 1
	m["create_time"] = time.Now()
	//新增comment默认投票1次,显示的时候默认减一
	m["vote_up"] = 0
	m["reddit_score"] = utils.LinkSortAlgorithm(m["create_time"].(time.Time), int64(0), int64(0))

	r, err := db.Insert(Table_Comment, m)
	if err != nil {
		goku.Logger().Errorln(err.Error())
		return 0, err
	}
	var id int64
	id, err = r.LastInsertId()
	if err != nil {
		goku.Logger().Errorln(err.Error())
		return 0, err
	}

	if id > 0 {
		// 更新Link的计数器
		IncCountById(db, Table_Link, linkId, "comment_count", 1)
		if pComment != nil {
			IncCountById(db, Table_Comment, pComment.Id, "children_count", 1)
		} else {
			IncCountById(db, Table_Link, linkId, "comment_root_count", 1)
		}

		// 通知评论用户
		userId := m["user_id"].(int64)
		toLinkUser := userId != link.UserId
		// 如果是回复,则推送给所回复评论的用户
		toPcommentUser := (pComment != nil && userId != pComment.UserId && pComment.UserId != link.UserId)
		if toLinkUser || toPcommentUser {

			comment := Comment{}
			comment.Id = id
			comment.UserId = userId
			comment.LinkId = linkId
			comment.ParentId = m["parent_id"].(int64)
			comment.CreateTime = m["create_time"].(time.Time)
			if toLinkUser {
				CommentForUser_Add(link.UserId, comment)
				Remind_Inc(link.UserId, REMIND_COMMENT)
			}
			if toPcommentUser {
				CommentForUser_Add(pComment.UserId, comment)
				Remind_Inc(pComment.UserId, REMIND_COMMENT)
			}
		}
	}

	return id, nil
}
Beispiel #3
0
func VoteLink(linkId int64, userId int64, score int, siteRunTime string) *Vote {

	var db *goku.MysqlDB = GetDB()
	defer db.Close()

	var update bool = false
	var vote *Vote = &Vote{0, 0, false, ""}
	checkRows, checkErr := db.Query("SELECT `user_id` FROM `link` WHERE `id` = ? LIMIT 0,1", linkId)
	if checkErr != nil {
		goku.Logger().Errorln(checkErr.Error())
		vote.Errors = "数据库错误"
	} else if checkRows.Next() == false {
		vote.Errors = "参数错误"
	} else {
		var luid int64
		checkRows.Scan(&luid)
		if luid == userId {
			vote.Errors = "不可以对自己投票"
		}
	}
	if vote.Errors != "" {
		return vote
	}

	rows, err := db.Query("SELECT score FROM `link_support_record` WHERE `link_id` = ? AND `user_id` = ? LIMIT 0,1", linkId, userId)
	if err == nil {

		var upVote int64
		var downVote int64
		var createTime time.Time
		if rows.Next() { //投过票的情况
			var scoreTemp int
			rows.Scan(&scoreTemp)

			//已投了支持,再投反对
			if scoreTemp == 1 && score == -1 {

				update = true
				rows, err = db.Query("SELECT vote_up,vote_down,create_time FROM `link` WHERE id=?", linkId)
				rows.Next()
				rows.Scan(&upVote, &downVote, &createTime)
				upVote = upVote - 1
				downVote = downVote + 1
				score := utils.LinkSortAlgorithm(createTime, upVote, downVote)
				dispute_score := utils.DisputeLinkSortAlgorithm(upVote, downVote)
				_, err = db.Query("UPDATE `link` SET vote_up=?,vote_down=?,reddit_score=?,dispute_score=? WHERE id=?;", upVote, downVote, score, dispute_score, linkId)
				if err != nil {
					goku.Logger().Errorln(err)
				}
				_, err = db.Query("UPDATE `link_support_record` SET score=-1,vote_time=NOW() WHERE `link_id` = ? AND `user_id` = ?", linkId, userId)
				if err != nil {
					goku.Logger().Errorln(err)
				}

				//已投了反对,再投支持
			} else if scoreTemp == -1 && score == 1 {

				update = true
				rows, err = db.Query("SELECT vote_up,vote_down,create_time FROM `link` WHERE id=?", linkId)
				rows.Next()
				rows.Scan(&upVote, &downVote, &createTime)
				upVote = upVote + 1
				downVote = downVote - 1
				score := utils.LinkSortAlgorithm(createTime, upVote, downVote)
				dispute_score := utils.DisputeLinkSortAlgorithm(upVote, downVote)
				_, err = db.Query("UPDATE `link` SET vote_up=?,vote_down=?,reddit_score=?,dispute_score=? WHERE id=?;", upVote, downVote, score, dispute_score, linkId)
				if err != nil {
					goku.Logger().Errorln(err)
				}
				_, err = db.Query("UPDATE `link_support_record` SET score=1,vote_time=NOW() WHERE `link_id` = ? AND `user_id` = ?", linkId, userId)
				if err != nil {
					goku.Logger().Errorln(err)
				}

			} else {
				vote.Errors = "您已对此投票"
			}

		} else { //没投过票的情况

			update = true
			rows, err = db.Query("SELECT vote_up,vote_down,create_time FROM `link` WHERE id=?", linkId)
			rows.Next()
			rows.Scan(&upVote, &downVote, &createTime)
			if score == 1 {

				upVote = upVote + 1
				score := utils.LinkSortAlgorithm(createTime, upVote, downVote)
				dispute_score := utils.DisputeLinkSortAlgorithm(upVote, downVote)
				_, err = db.Query("UPDATE `link` SET vote_up=?,reddit_score=?,dispute_score=? WHERE id=?;", upVote, score, dispute_score, linkId)
				if err != nil {
					goku.Logger().Errorln(err)
				}

			} else {

				downVote = downVote + 1
				score := utils.LinkSortAlgorithm(createTime, upVote, downVote)
				dispute_score := utils.DisputeLinkSortAlgorithm(upVote, downVote)
				_, err = db.Query("UPDATE `link` SET vote_down=?,reddit_score=?,dispute_score=? WHERE `id`=?;", downVote, score, dispute_score, linkId)
				if err != nil {
					goku.Logger().Errorln(err)
				}

			}
			_, err = db.Query("INSERT INTO `link_support_record` (link_id, user_id, score,vote_time) VALUES (?, ?, ?, NOW())", linkId, userId, score)
			if err != nil {
				goku.Logger().Errorln(err)
			}
		}

		if update {
			vote.Id = linkId
			vote.VoteNum = upVote - downVote
			vote.Success = true
			// 存入`tui_link_for_handle` 链接处理队列表
			_, err = db.Query("INSERT ignore INTO tui_link_for_handle(link_id,create_time,user_id,insert_time,data_type) VALUES (?, ?, ?, NOW(), ?)",
				linkId, createTime, userId, 2)
			if err != nil {
				goku.Logger().Errorln(err)
			}
		}
	}
	if err != nil {
		vote.Success = false
		goku.Logger().Errorln(err.Error())
		vote.Errors = "数据库错误"
	}

	return vote
}