Example #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"] = 1
	m["reddit_score"] = utils.RedditSortAlgorithm(m["create_time"].(time.Time), int64(1), 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
}
Example #2
0
// 保存评论到数据库,如果成功,则返回comment的id
func Comment_SaveMap(m map[string]interface{}) (int64, error) {
	var db *goku.MysqlDB = GetDB()
	defer db.Close()

	// TODO: 链接评论的链接存不存在?

	// 检查父评论是否存在
	var pComment *Comment
	var err error
	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("指定的父评论不存在")
		}
	}

	// 路径相关
	if pComment == nil {
		m["parent_id"] = 0
		m["top_parent_id"] = 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"] = 1
	m["reddit_score"] = utils.RedditSortAlgorithm(m["create_time"].(time.Time), int64(1), 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, m["link_id"].(int64), "comment_count", 1)
		if pComment != nil {
			IncCountById(db, Table_Comment, pComment.Id, "children_count", 1)
		} else {
			IncCountById(db, Table_Link, m["link_id"].(int64), "comment_root_count", 1)
		}
	}

	return id, nil
}
Example #3
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"] = 1
    m["reddit_score"] = utils.RedditSortAlgorithm(m["create_time"].(time.Time), int64(1), 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
}