示例#1
0
文件: bsimdb.go 项目: quant67/bsim
/*
	select distinct(card.id), card.body, user.name
	from user, card, user_card
	where
	card.id = user_card.cardid and user_card.fromuserid=user.id
	and (user_card.fromuserid=20 or user_Card.touserid=20) and card.id>0
	order by card.id
*/
func PingMsgReply(req bsimjson.Bsimjson) (ret []bsimjson.Bsimjson, err error) {
	//func FindUserBySession(ss string) (id int64, name string, email string, password string, err error) {
	meuid, _, _, _, err := FindUserBySession(req.Who)
	rows, err := db.Query(`select distinct(card.id), card.body, user.name, user_card.sendtime
	from user, card, user_card
	where
	card.id = user_card.cardid and user_card.fromuserid=user.id
	and (user_card.fromuserid=? or user_Card.touserid=?) and card.id>?
	order by card.id`,
		meuid, meuid, req.Cardid)
	if err != nil {
		return
	}

	for rows.Next() {
		var tm bsimjson.Bsimjson
		var cardbody, fromname, sendTime string
		var cardid int64
		if err := rows.Scan(&cardid, &cardbody, &fromname, &sendTime); err != nil {
			log.Fatal(err)
			continue
		}
		tm.Type = "send"
		tm.Who = fromname
		tm.Msg = cardbody
		tm.Cardid = cardid
		tm.DTime = sendTime
		ret = append(ret, tm)
	}
	return ret, err
}
示例#2
0
文件: main.go 项目: quant67/bsim
func pingReply(ws *websocket.Conn, req bsimjson.Bsimjson) {
	if req.Msg == "start" {
		var ret bsimjson.Bsimjson
		_, mname, _, _, _ := bsimdb.FindUserBySession(req.Who)
		ret.Type = "ping"
		ret.Who = mname
		var friends []string
		friends, _ = bsimdb.FindUsersFriends(mname)
		ret.ToWho = friends
		ret.Msg = "friends"
		s, _ := bsimjson.Bsimjson2json(ret)
		if err := websocket.Message.Send(ws, s); err != nil {
			fmt.Println("Can't not send to client")
		}
	} else if req.Msg == "msg" || req.Msg == "msgAll" {
		ret, err := bsimdb.PingMsgReply(req)
		if err != nil {
			log.Fatal(err)
			return
		}
		if req.Msg == "msg" && len(ret) > 50 {
			ret = ret[len(ret)-50:]
		}
		for i := 0; i < len(ret); i++ {
			if ma, _ := regexp.MatchString("(?i:\\<script[\\S\\s]+?\\</script\\>)", ret[i].Msg); ma {
				ret[i].Msg = `<div class="alert alert-warning" role="alert"> <strong>Sorry.</strong> We modify this massage a little bit for safty.</div>` + html.EscapeString(ret[i].Msg)
			}
			ret[i].Msg = string(blackfriday.MarkdownCommon([]byte(ret[i].Msg)))

			s, _ := bsimjson.Bsimjson2json(ret[i])
			if err := websocket.Message.Send(ws, s); err != nil {
				fmt.Println("Can't not send to client")
			}
		}
	}
}