Exemplo n.º 1
0
func NewGroup(db *periwinkle.Tx, name string, existence []int, read []int, post []int, join []int) *Group {
	if name == "" {
		programmerError("Group name can't be empty")
	}
	name = strings.ToLower(name)
	subscriptions := make([]Subscription, 0)
	o := Group{
		ID:                 name,
		ReadPublic:         read[0],
		ReadConfirmed:      read[1],
		ExistencePublic:    existence[0],
		ExistenceConfirmed: existence[1],
		PostPublic:         post[0],
		PostConfirmed:      post[1],
		PostMember:         post[2],
		JoinPublic:         join[0],
		JoinConfirmed:      join[1],
		JoinMember:         join[2],
		Subscriptions:      subscriptions,
	}
	if err := db.Create(&o).Error; err != nil {
		dbError(err)
	}
	return &o
}
Exemplo n.º 2
0
func (o Medium) dbSeed(db *periwinkle.Tx) locale.Error {
	errs := errorList{}
	errHelper(&errs, locale.UntranslatedError(db.Create(&Medium{"email"}).Error))
	errHelper(&errs, locale.UntranslatedError(db.Create(&Medium{"sms"}).Error))
	errHelper(&errs, locale.UntranslatedError(db.Create(&Medium{"mms"}).Error))
	if len(errs) > 0 {
		return errs
	}
	return nil
}
Exemplo n.º 3
0
func NewShortURL(db *periwinkle.Tx, u *url.URL) *ShortURL {
	o := ShortURL{
		ID:   RandomString(5),
		Dest: u.String(), // TODO: automatic marshalling
	}
	if err := db.Create(&o).Error; err != nil {
		dbError(err)
	}
	return &o
}
Exemplo n.º 4
0
func NewCaptcha(db *periwinkle.Tx) *Captcha {
	o := Captcha{
		ID:    captcha.New(),
		Value: string(captcha.RandomDigits(defaultLen)),
	}
	if err := db.Create(&o).Error; err != nil {
		dbError(err)
	}
	return &o
}
Exemplo n.º 5
0
func NewSubscription(db *periwinkle.Tx, addressID int64, groupID string, confirmed bool) Subscription {
	subscription := Subscription{
		AddressID: addressID,
		GroupID:   groupID,
		Confirmed: confirmed,
	}
	if err := db.Create(&subscription).Error; err != nil {
		dbError(err)
	}
	return subscription
}
Exemplo n.º 6
0
func NewUserAddress(db *periwinkle.Tx, userID string, medium string, address string, confirmed bool) UserAddress {
	userID = strings.ToLower(userID)
	o := UserAddress{
		UserID:    userID,
		Medium:    medium,
		Address:   address,
		Confirmed: confirmed,
	}
	if err := db.Create(&o).Error; err != nil {
		dbError(err)
	}
	return o
}
Exemplo n.º 7
0
func NewMessage(db *periwinkle.Tx, id string, group Group, unique maildir.Unique) Message {
	if id == "" {
		programmerError("Message ID can't be emtpy")
	}
	o := Message{
		ID:      id,
		GroupID: group.ID,
		Unique:  string(unique),
	}
	if err := db.Create(&o).Error; err != nil {
		dbError(err)
	}
	return o
}
Exemplo n.º 8
0
func NewSession(db *periwinkle.Tx, user *User, password string) *Session {
	if user == nil || !user.CheckPassword(password) {
		return nil
	}
	o := Session{
		ID:       RandomString(24),
		UserID:   user.ID,
		LastUsed: time.Now(),
	}
	if err := db.Create(&o).Error; err != nil {
		dbError(err)
	}
	return &o
}
Exemplo n.º 9
0
func NewUser(db *periwinkle.Tx, name string, password string, email string) User {
	if name == "" {
		programmerError("User name can't be empty")
	}
	name = strings.ToLower(name)
	o := User{
		ID:        name,
		FullName:  "",
		Addresses: []UserAddress{{Medium: "email", Address: email, Confirmed: false}},
	}
	o.SetPassword(password)
	if err := db.Create(&o).Error; err != nil {
		dbError(err)
	}
	return o
}
Exemplo n.º 10
0
func (o Group) dbSeed(db *periwinkle.Tx) locale.Error {
	existence := [2]int{2, 2}
	read := [2]int{2, 2}
	post := [3]int{1, 1, 1}
	join := [3]int{1, 1, 1}
	return locale.UntranslatedError(db.Create(&Group{
		ID:                 "test",
		ReadPublic:         read[0],
		ReadConfirmed:      read[1],
		ExistencePublic:    existence[0],
		ExistenceConfirmed: existence[1],
		PostPublic:         post[0],
		PostConfirmed:      post[1],
		PostMember:         post[2],
		JoinPublic:         join[0],
		JoinConfirmed:      join[1],
		JoinMember:         join[2],
		Subscriptions:      []Subscription{},
	}).Error)
}
Exemplo n.º 11
0
func AssignTwilioNumber(db *periwinkle.Tx, userid string, groupid string, twilioNum string) *TwilioPool {

	num := TwilioNumber{}
	err := db.Where(TwilioNumber{Number: twilioNum}).FirstOrCreate(&num).Error

	if err != nil {
		dbError(err)
	}

	o := TwilioPool{
		UserID:   userid,
		GroupID:  groupid,
		NumberID: num.ID,
	}

	if err := db.Create(&o).Error; err != nil {
		dbError(err)
	}

	return &o

}
Exemplo n.º 12
0
func Test(cfg *periwinkle.Cfg, db *periwinkle.Tx) {

	num := backend.TwilioNumber{
		Number: "+13346038139",
	}

	if err := db.Create(&num).Error; err != nil {
		panic(err)
	}

	user1 := backend.User{
		ID:        "alex",
		FullName:  "",
		Addresses: []backend.UserAddress{{Medium: "email", Address: "*****@*****.**", Confirmed: true}},
	}

	uerr := db.Create(&user1).Error
	if uerr != nil {
		periwinkle.LogErr(locale.UntranslatedError(uerr))
	}

	user2 := backend.User{
		ID:        "john",
		FullName:  "",
		Addresses: []backend.UserAddress{{Medium: "sms", Address: "+17656027006", Confirmed: true}, {Medium: "email", Address: "*****@*****.**", Confirmed: true}},
	}

	uerr = db.Create(&user2).Error
	if uerr != nil {
		periwinkle.LogErr(locale.UntranslatedError(uerr))
	}

	user3 := backend.User{
		ID:        "guntas",
		FullName:  "",
		Addresses: []backend.UserAddress{{Medium: "sms", Address: "+16166342620", Confirmed: true}},
	}

	uerr = db.Create(&user3).Error
	if uerr != nil {
		periwinkle.LogErr(locale.UntranslatedError(uerr))
	}
	existence := [2]int{2, 2}
	read := [2]int{2, 2}
	post := [3]int{1, 1, 1}
	join := [3]int{1, 1, 1}
	uerr = db.Create(&backend.Group{
		ID:                 "Purdue",
		ReadPublic:         read[0],
		ReadConfirmed:      read[1],
		ExistencePublic:    existence[0],
		ExistenceConfirmed: existence[1],
		PostPublic:         post[0],
		PostConfirmed:      post[1],
		PostMember:         post[2],
		JoinPublic:         join[0],
		JoinConfirmed:      join[1],
		JoinMember:         join[2],
		Subscriptions: []backend.Subscription{
			{AddressID: user1.Addresses[0].ID, Confirmed: true},
			{AddressID: user2.Addresses[0].ID, Confirmed: true},
			{AddressID: user2.Addresses[1].ID, Confirmed: true},
			{AddressID: user3.Addresses[0].ID, Confirmed: true},
		},
	}).Error
	if uerr != nil {
		periwinkle.LogErr(locale.UntranslatedError(uerr))
	}
}