Esempio n. 1
0
// 添加好友关系
func handleUserRelationAdd(resp http.ResponseWriter, req *http.Request) {
	if req.Method == "POST" {
		receiver_category_id := req.FormValue("receiver_category_id")
		buddy_request_id := req.FormValue("buddy_request_id")
		buddyrequest, _ := model.GetBuddyRequestById(buddy_request_id)
		if buddyrequest != nil {
			receiver := buddyrequest.Receiver
			sender := buddyrequest.Sender
			sender_category_id := buddyrequest.SenderCategoryId
			//开启事务
			tx, _ := model.Database.Begin()
			//修改好友请求记录中接受人的好友分组ID
			_, err := model.UpdateBuddyRequestReceiverCategoryId(tx, buddy_request_id, receiver_category_id)
			//添加请求人好友关系数据
			_, err = model.AddFriendRelation(tx, receiver, sender_category_id)
			//添加接收人好友关系数据
			_, err = model.AddFriendRelation(tx, sender, receiver_category_id)
			//修改好友请求记录中状态
			_, err = model.UpdateBuddyRequestStatus(tx, buddy_request_id, "1")

			if err != nil {
				tx.Rollback()
				resp.Write(common.NewIMResponseSimple(100, err.Error(), "").Encode())
				return
			} else {
				tx.Commit()
				//判断请求者是不是在线 在线就把接受者推送给请求者
				conn, _ := model.GetConnByUserId(sender)
				if conn != nil { //在线
					user, _ := model.GetUserById(receiver)
					data := make(map[string]interface{})
					data["category_id"] = sender_category_id
					data["user"] = user
					ClientMaps[conn.Key].PutOut(common.NewIMResponseData(util.SetData("user", data), common.ADD_BUDDY))
				}
				conn, _ = model.GetConnByUserId(receiver)
				if conn != nil {
					user, _ := model.GetUserById(sender)
					data := make(map[string]interface{})
					data["category_id"] = receiver_category_id
					data["user"] = user
					ClientMaps[conn.Key].PutOut(common.NewIMResponseData(util.SetData("user", data), common.ADD_BUDDY))
				}
				resp.Write(common.NewIMResponseSimple(0, "好友关系建立成功", "").Encode())
				return
			}

		} else {
			resp.Write(common.NewIMResponseSimple(104, "该好友请求不存在", "").Encode())
		}

	} else {
		resp.Write(common.NewIMResponseSimple(404, "Not Found: "+req.Method, "").Encode())
	}
}
Esempio n. 2
0
func handleUserRelationRefuse(resp http.ResponseWriter, req *http.Request) {
	if req.Method == "POST" {
		buddy_request_id := req.FormValue("buddy_request_id")
		if buddy_request_id != "" {
			tx, _ := model.Database.Begin()
			//修改好友请求记录中状态
			_, err := model.UpdateBuddyRequestStatus(tx, buddy_request_id, "2")
			if err != nil {
				tx.Rollback()
				resp.Write(common.NewIMResponseSimple(100, err.Error(), "").Encode())
				return
			} else {
				tx.Commit()
				resp.Write(common.NewIMResponseSimple(0, "已经拒绝该好友请求成功", "").Encode())
				return
			}
		} else {
			resp.Write(common.NewIMResponseSimple(109, "该好友请求不合法", "").Encode())
		}

	} else {
		resp.Write(common.NewIMResponseSimple(404, "Not Found: "+req.Method, "").Encode())
	}
}