Beispiel #1
0
Datei: user.go Projekt: sangba/im
func (u *User) Registry(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	user := r.FormValue("user")
	pwd := r.FormValue("pwd")

	if user == "" || pwd == "" {
		Error(w, "user or pwd error.")
		return
	} else {
		mongo := store.NewMongo()
		collection := mongo.DB("im").C("user")

		cnt, err := collection.Find(bson.M{"user": user}).Count()
		if err != nil {
			glog.Info("registry:" + err.Error())
		}
		if cnt != 0 {
			Error(w, "user exist.")
			return
		}

		uid := bson.NewObjectId().Hex()

		err = collection.Insert(&User{uid, user, pwd})
		if err != nil {
			glog.Info("registry:" + err.Error())
		}
		Success(w, nil)
	}
}
Beispiel #2
0
Datei: user.go Projekt: sangba/im
func (u *User) DelFri(w http.ResponseWriter, r *http.Request) {

	r.ParseForm()

	uid := r.FormValue("uid")
	fid := r.FormValue("fid")

	if uid == "" || fid == "" {
		Error(w, "params error.")
		return
	}

	mongo := store.NewMongo()
	collection := mongo.DB("im").C("friend")
	_, err := collection.RemoveAll(bson.M{"ids": bson.M{"$all": []string{uid, fid}}})
	if err != nil {
		glog.Info(err.Error())
		return
	}

	if err != nil {
		Error(w, "delfri failed.")
	} else {
		Success(w, nil)
	}
}
Beispiel #3
0
Datei: user.go Projekt: sangba/im
func (u *User) AddFri(w http.ResponseWriter, r *http.Request) {
	//验证是否已经重复添加好友
	r.ParseForm()

	uid := r.FormValue("uid")
	fid := r.FormValue("fid")

	if uid == "" || fid == "" {
		Error(w, "params error.")
		return
	}

	t := time.Now().Unix()

	mongo := store.NewMongo()
	collection := mongo.DB("im").C("friend")
	cnt, err := collection.Find(bson.M{"ids": bson.M{"$all": []string{uid, fid}}}).Count()
	if err != nil {
		fmt.Println(err.Error())
		glog.Info(err.Error())
		return
	}
	fmt.Println(cnt)
	if cnt != 0 {
		Error(w, "already fri.")
		return
	}

	err = collection.Insert(&Friend{[]string{uid, fid}, 1, t, t})
	if err != nil {
		Error(w, "addfri failed.")
	} else {
		Success(w, nil)
	}
}
Beispiel #4
0
Datei: user.go Projekt: sangba/im
func (u *User) MyFri(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()

	uid := r.FormValue("uid")

	var fris []Friend
	mongo := store.NewMongo()
	mongo.DB("im").C("friend").Find(bson.M{"ids": bson.M{"$in": []string{uid}}}).All(&fris)

	Success(w, fris)
}
Beispiel #5
0
Datei: user.go Projekt: sangba/im
func (u *User) Info(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()

	uid := r.FormValue("uid")
	if uid == "" {
		Error(w, "uid")
		return
	}

	var info User

	mongo := store.NewMongo()
	collection := mongo.DB("im").C("user")
	collection.Find(bson.M{"uid": uid}).One(&info)

	Success(w, info)
}
Beispiel #6
0
func SendP2pMsg(client_pool *store.ClientPool, fromid, toid, msg string) error {
	conn := client_pool.Get(toid)

	//mongo
	mongo := store.NewMongo()
	collection := mongo.DB("im").C("msg")

	if conn == nil { //离线
		collection.Insert(&PMsg{fromid, toid, 0, msg})
	} else { //在线
		pmsg := PMsg{fromid, toid, 1, msg}
		collection.Insert(&pmsg)

		b, _ := json.Marshal(Pm{fromid, toid, msg})
		conn.Write(b)
	}
	return nil
}
Beispiel #7
0
Datei: user.go Projekt: sangba/im
func (u *User) Login(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()

	user := r.FormValue("user")
	pwd := r.FormValue("pwd")
	if user == "" || pwd == "" {
		Error(w, "user or pwd error.")
		return
	}

	mongo := store.NewMongo()
	collection := mongo.DB("im").C("user")

	var info User
	err := collection.Find(bson.M{"user": user}).One(&info)
	if err != nil {
		glog.Info("login: " + err.Error())
	}
	if info.Pwd != pwd {
		Error(w, "pwd error.")
		return
	}
	Success(w, map[string]string{"token": info.Uid})
}
Beispiel #8
0
func SendGrpMsg(client_pool *store.ClientPool, fromid, gid, msg string) error {
	mongo := store.NewMongo()
	mongo.DB("im").C("grp_msg").Insert(&GMsg{gid, fromid, 1, msg})

	return nil
}