Exemplo n.º 1
0
func CreateUser(user models.User) (int, createResponse) {
	user.Id = uuid.NewV4().String()
	user.Password = Hash(user.Password)
	result, err := db.UserTable.Insert(user).RunWrite(db.Session)
	// For some ungodly strange reason rethink thinks returning errors on multiple insertions are better in the
	// results, than in the actual err variable. Documented here: https://github.com/rethinkdb/rethinkdb/issues/899
	if err != nil || result.Errors > 0 {
		firstError := errors.New(result.FirstError)
		if r.IsConflictErr(err) || r.IsConflictErr(firstError) {
			return http.StatusConflict, createResponse{userAlreadyExists, nil}
		}
		panic(err)
	}
	return http.StatusCreated, createResponse{userCreated, &user}
}
Exemplo n.º 2
0
// TopicCreateP2P given two users creates a p2p topic
func (a *RethinkDbAdapter) TopicCreateP2P(initiator, invited *t.Subscription) error {
	initiator.Id = initiator.Topic + ":" + initiator.User
	// Don't care if the initiator changes own subscription
	_, err := rdb.DB(a.dbName).Table("subscriptions").Insert(initiator, rdb.InsertOpts{Conflict: "replace"}).
		RunWrite(a.conn)
	if err != nil {
		return err
	}

	// Ensure this is a new subscription. If one already exist, don't overwrite it
	invited.Id = invited.Topic + ":" + invited.User
	_, err = rdb.DB(a.dbName).Table("subscriptions").Insert(invited, rdb.InsertOpts{Conflict: "error"}).
		RunWrite(a.conn)
	if err != nil {
		// Is this a duplicate subscription? If so, ifnore it. Otherwise it's a genuine DB error
		if !rdb.IsConflictErr(err) {
			return err
		}
	}

	topic := &t.Topic{
		ObjHeader: t.ObjHeader{Id: initiator.Topic},
		Access:    t.DefaultAccess{Auth: t.ModeP2P, Anon: t.ModeBanned}}
	topic.ObjHeader.MergeTimes(&initiator.ObjHeader)
	return a.TopicCreate(topic)
}
Exemplo n.º 3
0
// Add user's authentication record
func (a *RethinkDbAdapter) AddAuthRecord(uid t.Uid, unique string, secret []byte, expires time.Time) (error, bool) {
	_, err := rdb.DB(a.dbName).Table("auth").Insert(
		map[string]interface{}{
			"unique":  unique,
			"userid":  uid.String(),
			"secret":  secret,
			"expires": expires}).RunWrite(a.conn)
	if err != nil {
		if rdb.IsConflictErr(err) {
			return errors.New("duplicate credential"), true
		}
		return err, false
	}
	return nil, false
}