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 UpsertIdentity(i *Identity) error {
	photos := json.MarshalStr(i.Photos)
	_, err := gsql.Exec(_db, `insert into identities(account_id, type, number, photos, created_at) values(?,?,?,?,?)
	on duplicate key update type=?, number=?, photos=?, created_at=?`, i.AccountID, i.Type, i.Number, photos,
		i.CreatedAt, i.Type, i.Number, photos, i.CreatedAt)
	return err
}
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
}