Exemplo n.º 1
0
Arquivo: user.go Projeto: t7er/ohlala
func User_SaveMap(m map[string]interface{}) (sql.Result, error) {
	var db *goku.MysqlDB = GetDB()
	defer db.Close()
	m["email_lower"] = strings.ToLower(m["email"].(string))
	r, err := db.Insert("user", m)
	return r, err
}
Exemplo n.º 2
0
//收藏link
func SaveUserFavorite(f map[string]interface{}) error {
	var db *goku.MysqlDB = GetDB()
	defer db.Close()
	_, err := db.Insert("user_favorite_link", f)

	return err
}
Exemplo n.º 3
0
// 减少DB操作
// @t: 推送类型, 1:关注的用户, 2:关注的话题
func linkForUser_AddWithDb(db *goku.MysqlDB, userId, linkId int64, t int) error {
	m := map[string]interface{}{
		"user_id":     userId,
		"link_id":     linkId,
		"create_time": time.Now(),
	}
	if t == 1 {
		m["user_count"] = 1
	} else {
		m["topic_count"] = 1
	}

	_, err := db.Insert(LinkForUser_TableName(userId), m)
	if err != nil {
		if strings.Index(err.Error(), "Duplicate entry") > -1 {
			m := map[string]interface{}{}
			if t == 1 {
				m["user_count"] = 1
			} else {
				m["topic_count"] = 1
			}
			_, err = db.Update(LinkForUser_TableName(userId), m, "user_id=? and link_id=?", userId, linkId)
			if err != nil {
				goku.Logger().Errorln(err.Error())
			}
		} else {
			goku.Logger().Errorln(err.Error())
		}
	}
	return err
}
Exemplo n.º 4
0
// 保持topic到数据库,同时建立topic与link的关系表
// 如果topic已经存在,则直接建立与link的关联
// 全部成功则返回true
func Topic_SaveTopics(topics string, linkId int64) bool {
	if topics == "" {
		return true
	}
	var db *goku.MysqlDB = GetDB()
	defer db.Close()

	success := true
	topicList := strings.Split(topics, ",")
	for _, topic := range topicList {
		topicLower := strings.ToLower(topic)
		t := new(Topic)
		err := db.GetStruct(t, "`name_lower`=?", topic)
		if err != nil {
			goku.Logger().Logln(topic)
			goku.Logger().Errorln(err.Error())
			success = false
			continue
		}
		if t.Id < 1 {
			t.Name = topic
			t.NameLower = topicLower
			_, err = db.InsertStruct(t)
			if err != nil {
				goku.Logger().Errorln(err.Error())
				success = false
				continue
			}
		}
		if t.Id > 0 && linkId > 0 {
			_, err = db.Insert("topic_link", map[string]interface{}{"topic_id": t.Id, "link_id": linkId})
			if err != nil {
				goku.Logger().Errorln(err.Error())
				success = false
			} else {
				// 成功,更新话题的链接数量统计
				Topic_IncCount(db, t.Id, "link_count", 1)

				redisClient := GetRedis()
				defer redisClient.Quit()
				// 加入推送队列
				// 格式: pushtype,topicid,linkid,timestamp
				qv := fmt.Sprintf("%v,%v,%v,%v", LinkForUser_ByTopic, t.Id, linkId, time.Now().Unix())
				_, err = redisClient.Lpush(golink.KEY_LIST_PUSH_TO_USER, qv)
				if err != nil {
					goku.Logger().Errorln(err.Error())
				}
			}
		}
	}
	return success
}
Exemplo n.º 5
0
func (ur *UserRecovery) Save() (sql.Result, error) {
	var db *goku.MysqlDB = GetDB()
	defer db.Close()

	m := make(map[string]interface{})
	m["user_id"] = ur.UserId
	m["token"] = ur.Token
	m["active"] = ur.Active
	m["create_time"] = ur.CreateTime
	m["recovery_time"] = ur.RecoveryTime
	r, err := db.Insert("user_recovery", m)
	return r, err
}
Exemplo n.º 6
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
}
Exemplo n.º 7
0
// 添加一条推送评论到被评论的用户,
func CommentForUser_Add(userId int64, comment Comment) error {
	var db *goku.MysqlDB = GetDB()
	defer db.Close()

	m := map[string]interface{}{}

	m["user_id"] = userId
	m["comment_id"] = comment.Id
	m["link_id"] = comment.LinkId
	m["pcomment_id"] = comment.ParentId
	m["create_time"] = comment.CreateTime

	_, err := db.Insert(table_CommentForUser, m)
	if err != nil {
		goku.Logger().Errorln(err.Error())
	}
	return err
}
Exemplo n.º 8
0
// userId 关注 followId
func User_Follow(userId, followId int64) (bool, error) {
	if userId < 1 || followId < 1 {
		return false, errors.New("参数错误")
	}
	if userId == followId {
		return false, errors.New("不能关注自己")
	}
	var db *goku.MysqlDB = GetDB()
	defer db.Close()

	vals := map[string]interface{}{
		"user_id":     userId,
		"follow_id":   followId,
		"create_time": time.Now(),
	}
	r, err := db.Insert("user_follow", vals)
	if err != nil {
		if strings.Index(err.Error(), "Duplicate entry") > -1 {
			return false, errors.New("已经关注该用户")
		} else {
			goku.Logger().Errorln(err.Error())
			return false, err
		}
	}

	var afrow int64
	afrow, err = r.RowsAffected()
	if err != nil {
		goku.Logger().Errorln(err.Error())
		return false, err
	}

	if afrow > 0 {
		LinkForUser_FollowUser(userId, followId)
		// 更新粉丝数
		User_IncCount(db, userId, "friend_count", 1)
		// 更新关注数
		User_IncCount(db, followId, "follower_count", 1)
		// 通知有新粉丝
		Remind_Inc(followId, REMIND_FANS)
		return true, nil
	}
	return false, nil
}
Exemplo n.º 9
0
func (u *ThirdPartyUser) Save() (sql.Result, error) {
	var db *goku.MysqlDB = GetDB()
	defer db.Close()

	m := make(map[string]interface{})
	m["user_id"] = u.UserId
	m["third_party"] = u.ThirdParty
	m["third_party_user_id"] = u.ThirdPartyUserId
	m["third_party_email"] = u.ThirdPartyEmail
	m["access_token"] = u.AccessToken
	m["refresh_token"] = u.RefreshToken
	m["token_expire_time"] = u.TokenExpireTime
	m["create_time"] = u.CreateTime
	m["last_active_time"] = u.LastActiveTime
	m["avatar_url"] = u.AvatarUrl
	m["link"] = u.Link
	r, err := db.Insert("third_party_user", m)
	return r, err
}
Exemplo n.º 10
0
// 用户userId 关注 话题topicId
func Topic_Follow(userId, topicId int64) (bool, error) {
	if userId < 1 || topicId < 1 {
		return false, errors.New("参数错误")
	}
	var db *goku.MysqlDB = GetDB()
	defer db.Close()

	vals := map[string]interface{}{
		"user_id":     userId,
		"topic_id":    topicId,
		"create_time": time.Now(),
	}
	r, err := db.Insert("topic_follow", vals)
	if err != nil {
		if strings.Index(err.Error(), "Duplicate entry") > -1 {
			return false, errors.New("已经关注该话题")
		} else {
			goku.Logger().Errorln(err.Error())
			return false, err
		}
	}

	var afrow int64
	afrow, err = r.RowsAffected()
	if err != nil {
		goku.Logger().Errorln(err.Error())
		return false, err
	}

	if afrow > 0 {
		// 关注话题成功,将话题的链接推送给用户
		LinkForUser_FollowTopic(userId, topicId)
		// 更新用户关注话题的数量
		User_IncCount(db, userId, "ftopic_count", 1)
		// 更新话题的关注用户数
		Topic_IncCount(db, topicId, "follower_count", 1)
		return true, nil
	}
	return false, nil
}
Exemplo n.º 11
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
}
Exemplo n.º 12
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
}
Exemplo n.º 13
0
func SaveTodo(m map[string]interface{}) (sql.Result, error) {
	var db *goku.MysqlDB = GetDB()
	defer db.Close()
	r, err := db.Insert("todo", m)
	return r, err
}