Esempio n. 1
0
func (mr *MsgRepository) SaveDirectMsg(msg *domain.Msg) error {
	tx, err := mr.db.Begin()
	defer func() {
		if err != nil {
			if tx != nil {
				tx.Rollback()
			}
			log.Error(err)
		}
	}()

	if err != nil {
		return err
	}

	var atStr string
	if len(msg.AtUserIDs) > 0 {
		atStr = gjson.MarshalStr(msg.AtUserIDs)
	}

	content := gjson.MarshalStr(msg.Content)
	_, err = tx.Exec("insert into "+getUserMsgTable(msg.SenderID)+"("+userMsgFields+") values(?,?,?,?,?,?,?,?)",
		msg.SenderID, msg.ID, msg.LocalID, msg.SenderID, msg.ReceiverID, content, msg.CreatedAt, atStr)
	if err != nil {
		return err
	}

	_, err = tx.Exec("insert into "+getUserMsgTable(msg.ReceiverID)+"("+userMsgFields+") values(?,?,?,?,?,?,?,?)",
		msg.ReceiverID, msg.ID, msg.LocalID, msg.SenderID, msg.ReceiverID, content, msg.CreatedAt, atStr)
	if err != nil {
		return err
	}
	err = tx.Commit()
	return err
}
Esempio n. 2
0
func (mr *MsgRepository) GetMsgs(userID types.ID, sinceID types.ID, size int) ([]*domain.Msg, error) {
	log.Debug(userID, sinceID, size)
	tableName := getUserMsgTable(userID)
	msgs := make([]*domain.Msg, 0, size)
	var seqID int64
	var err error
	defer func() {
		if err != nil {
			msgs = msgs[0:0]
			log.Error("[GetUserMsgs]", tableName, sinceID, size, err)
		}

		if err == sql.ErrNoRows {
			err = nil
		}
	}()
	if sinceID > 0 {
		err = mr.db.QueryRow("select id from "+tableName+" where msg_id=?", sinceID).Scan(&seqID)
		if err == sql.ErrNoRows {
			err = mr.db.QueryRow("select id from "+tableName+" where msg_id>? order by id limit 1", sinceID).Scan(&seqID)
		}
		if err != nil {
			return nil, err
		}
	}

	var rows *sql.Rows
	query := "select " + msgFields + " from " + tableName + " where user_id=? and id>? order by id limit ?"
	rows, err = mr.db.Query(query, userID, seqID, size)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var cdata, atData []byte
	for rows.Next() {
		msg := &domain.Msg{}
		err = rows.Scan(&msg.ID, &msg.LocalID, &msg.SenderID, &msg.ReceiverID, &cdata, &msg.CreatedAt, &atData)
		if err != nil {
			break
		}
		gjson.Unmarshal(cdata, &msg.Content)
		if len(atData) > 0 {
			gjson.Unmarshal(atData, &msg.AtUserIDs)
		}
		msgs = append(msgs, msg)
	}

	if len(msgs) == 0 {
		log.Debug(query)
	}
	return msgs, nil
}
Esempio n. 3
0
func (mr *MsgRepository) SaveGroupMsg(msg *domain.Msg, memberIDs []types.ID) error {
	tx, err := mr.db.Begin()
	defer func() {
		if err != nil {
			if tx != nil {
				tx.Rollback()
			}
			log.Error(err)
		}
	}()

	if err != nil {
		return err
	}

	var atStr string
	if len(msg.AtUserIDs) > 0 {
		atStr = gjson.MarshalStr(msg.AtUserIDs)
	}

	content := gjson.MarshalStr(msg.Content)
	_, err = tx.Exec("insert into "+getUserMsgTable(msg.SenderID)+"("+userMsgFields+") values(?,?,?,?,?,?,?,?)",
		msg.SenderID, msg.ID, msg.LocalID, msg.SenderID, msg.ReceiverID, content, msg.CreatedAt, atStr)
	if err != nil {
		return err
	}

	_, err = tx.Exec("insert into "+getGroupMsgTable(msg.ReceiverID)+"("+msgFields+") values(?,?,?,?,?,?,?)",
		msg.ID, msg.LocalID, msg.SenderID, msg.ReceiverID, content, msg.CreatedAt, atStr)
	if err != nil {
		return err
	}

	//copy to each user's own table
	for _, uid := range memberIDs {
		_, err = tx.Exec("insert into "+getUserMsgTable(uid)+"("+userMsgFields+") values(?,?,?,?,?,?,?,?)",
			uid, msg.ID, msg.LocalID, msg.SenderID, msg.ReceiverID, content, msg.CreatedAt, atStr)
		if err != nil {
			return err
		}
	}

	err = tx.Commit()
	return err
}
Esempio n. 4
0
func Serve(name string, addr string, h RequestHandlerFunc) {
	creator := namedServerCreators[name]
	if creator == nil {
		panic("no server registered for name " + name)
	}

	s := creator()

	log.Info("start serving at", addr)
	conns := newUserConns()
	addrConns[addr] = conns
	err := s.Serve(addr, func(c Conn) {
		log.Info("new conn", c)
		for {
			req, err := c.Receive(time.Minute * 5)
			if err != nil {
				log.Error(getUserID(c), err)
				break
			}

			if req != nil {
				log.Debug("recv:", req)
				ctx := &ConnContext{}
				ctx.conns = conns
				ctx.conn = c
				h(ctx, req)
			}
		}
		conns.remove(c)
		c.Close()
		log.Info("close conn", c)
	})

	if err != nil {
		panic(err)
	}
}