Example #1
0
func (grp *Group) GetSubscriberIDs(db *periwinkle.Tx) []string {
	subscriptions := grp.GetSubscriptions(db)
	addressIDs := make([]int64, len(subscriptions))
	for i, sub := range subscriptions {
		addressIDs[i] = sub.AddressID
	}
	var addresses []UserAddress
	if len(addressIDs) > 0 {
		if err := db.Where("id IN (?)", addressIDs).Find(&addresses).Error; err != nil {
			dbError(err)
		}
	} else {
		addresses = []UserAddress{}
	}
	userIDSet := map[string]bool{}
	for _, address := range addresses {
		userIDSet[address.UserID] = true
	}
	userIDs := make([]string, len(userIDSet))
	i := 0
	for userID := range userIDSet {
		userIDs[i] = userID
		i++
	}
	return userIDs
}
Example #2
0
func (u *User) populate(db *periwinkle.Tx) {
	db.Where(`user_id = ?`, u.ID).Model(UserAddress{}).Find(&u.Addresses)
	addressIDs := make([]int64, len(u.Addresses))
	for i, address := range u.Addresses {
		addressIDs[i] = address.ID
	}
}
Example #3
0
func IsAdmin(db *periwinkle.Tx, userID string, group Group) bool {
	subscriptions := group.GetSubscriptions(db)
	addressIDs := make([]int64, len(subscriptions))
	for i, subscription := range subscriptions {
		addressIDs[i] = subscription.AddressID
	}
	var addresses []UserAddress
	if len(addressIDs) > 0 {
		if result := db.Where("id IN (?)", addressIDs).Find(&addresses); result.Error != nil {
			if !result.RecordNotFound() {
				panic("cant find any subscriptions corresponding user address")
			}
		}
	} else {
		// no subscriptions so user cannot possibly be subscribed
		return false
	}
	for _, address := range addresses {
		if address.UserID == userID && address.Medium == "admin" {
			return true
		}
	}
	// could not find user in subscribed user addresses, therefore, he/she isn't subscribed
	return false
}
Example #4
0
func GetGroupsByMember(db *periwinkle.Tx, user User) []Group {
	subscriptions := user.GetSubscriptions(db)
	var groupIDs []string
	for _, sub := range subscriptions {
		groupIDs = append(groupIDs, sub.GroupID)
	}
	var groups []Group
	if len(groupIDs) > 0 {
		if err := db.Where("id IN (?)", groupIDs).Find(&groups).Error; err != nil {
			dbError(err)
		}
	} else {
		groups = make([]Group, 0)
	}
	groupsByID := make(map[string]Group)
	for _, group := range groups {
		groupsByID[group.ID] = group
	}

	groupset := map[string]Group{}
	for _, sub := range subscriptions {
		// only add group if user is confirmed member or
		// if group allows non confirmed members to see that it exists
		if sub.Confirmed || groupsByID[sub.GroupID].ExistenceConfirmed == 2 {
			groupset[sub.GroupID] = groupsByID[sub.GroupID]
		}
	}
	var finalgroups []Group
	for _, group := range groupset {
		finalgroups = append(finalgroups, group)
	}

	return finalgroups
}
Example #5
0
func (addr *UserAddress) GetSubscriptions(db *periwinkle.Tx) []Subscription {
	var subscriptions []Subscription
	if err := db.Where("address_id = ?", addr.ID).Find(&subscriptions).Error; err != nil {
		dbError(err)
	}
	return subscriptions
}
Example #6
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
}
Example #7
0
func GetAllGroups(db *periwinkle.Tx) []Group {
	var o []Group
	if result := db.Find(&o); result.Error != nil {
		if result.RecordNotFound() {
			return nil
		}
		dbError(result.Error)
	}
	return o
}
Example #8
0
func GetShortURLByID(db *periwinkle.Tx, id string) *ShortURL {
	var o ShortURL
	if result := db.First(&o, "id = ?", id); result.Error != nil {
		if result.RecordNotFound() {
			return nil
		}
		dbError(result.Error)
	}
	return &o
}
Example #9
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
}
Example #10
0
func GetTwilioSMSMessageBySID(db *periwinkle.Tx, sid string) *TwilioSMSMessage {
	var o TwilioSMSMessage
	if result := db.First(&o, "message_s_id = ?", sid); result.Error != nil {
		if result.RecordNotFound() {
			return nil
		}
		dbError(result.Error)
	}
	return &o
}
Example #11
0
func GetCaptchaByID(db *periwinkle.Tx, id string) *Captcha {
	var o Captcha
	if result := db.First(&o, "id = ?", id); result.Error != nil {
		if result.RecordNotFound() {
			return nil
		}
		dbError(result.Error)
	}
	return &o
}
Example #12
0
func (grp *Group) GetSubscriptions(db *periwinkle.Tx) []Subscription {
	var subscriptions []Subscription
	if result := db.Where("group_id = ?", grp.ID).Find(&subscriptions); result.Error != nil {
		if result.RecordNotFound() {
			return []Subscription{}
		}
		dbError(result.Error)
	}
	return subscriptions
}
Example #13
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
}
Example #14
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
}
Example #15
0
func GetMessageByID(db *periwinkle.Tx, id string) *Message {
	var o Message
	if result := db.First(&o, "id = ?", id); result.Error != nil {
		if result.RecordNotFound() {
			return nil
		}
		dbError(result.Error)
	}
	return &o
}
Example #16
0
func GetAddressesByUserAndMedium(db *periwinkle.Tx, userID string, medium string) []UserAddress {
	userID = strings.ToLower(userID)
	var o []UserAddress
	if result := db.Where("user_id=? AND medium=?", userID, medium).Find(&o); result.Error != nil {
		if result.RecordNotFound() {
			return nil
		}
		dbError(result.Error)
	}
	return o
}
Example #17
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
}
Example #18
0
func GetAllUsedTwilioNumbers(db *periwinkle.Tx) (ret []TwilioNumber) {
	var twilioNum []TwilioNumber
	if result := db.Find(&twilioNum); result.Error != nil {
		if result.RecordNotFound() {
			return nil
		}
		dbError(result.Error)
	}
	return twilioNum

}
Example #19
0
func GetTwilioPoolByUserID(db *periwinkle.Tx, userid string) []TwilioPool {

	var o []TwilioPool
	if result := db.Where("user_id = ?", userid).Find(&o); result.Error != nil {
		if result.RecordNotFound() {
			return nil
		}
		dbError(result.Error)
	}
	return o
}
Example #20
0
func DbDrop(db *periwinkle.Tx) locale.Error {
	errs := errorList{}
	for i := range tables {
		table := tables[len(tables)-1-i]
		errHelper(&errs, locale.UntranslatedError(db.DropTable(table).Error))
	}
	if len(errs) > 0 {
		return errs
	}
	return nil
}
Example #21
0
func GetUserByAddress(db *periwinkle.Tx, medium string, address string) *User {
	var o User
	result := db.Joins("INNER JOIN user_addresses ON user_addresses.user_id=users.id").Where("user_addresses.medium=? and user_addresses.address=?", medium, address).Find(&o)
	if result.Error != nil {
		if result.RecordNotFound() {
			return nil
		}
		dbError(result.Error)
	}
	o.populate(db)
	return &o
}
Example #22
0
func GetUserByID(db *periwinkle.Tx, id string) *User {
	id = strings.ToLower(id)
	var o User
	if result := db.First(&o, "id = ?", id); result.Error != nil {
		if result.RecordNotFound() {
			return nil
		}
		dbError(result.Error)
	}
	o.populate(db)
	return &o
}
Example #23
0
func GetGroupByID(db *periwinkle.Tx, id string) *Group {
	id = strings.ToLower(id)
	var o Group
	if result := db.First(&o, "id = ?", id); result.Error != nil {
		if result.RecordNotFound() {
			return nil
		}
		dbError(result.Error)
	}
	db.Model(&o).Related(&o.Subscriptions)
	return &o
}
Example #24
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
}
Example #25
0
func GetTwilioNumberByID(db *periwinkle.Tx, id int64) *TwilioNumber {

	var twilio_num TwilioNumber

	if result := db.Where("id = ?", id).First(&twilio_num); result.Error != nil {
		if result.RecordNotFound() {
			return nil
		}
		dbError(result.Error)
	}

	return &twilio_num
}
Example #26
0
func UseCaptcha(db *periwinkle.Tx, id, token string) bool {
	o := GetCaptchaByID(db, id)
	if o == nil {
		panic("Captcha " + id + " does not exist.")
	}
	if strings.Compare(token, "true") == 0 {
		// destroy captcha
		db.Delete(&o)
		return true
	}
	// destroy captcha
	db.Delete(&o)
	return false
}
Example #27
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
}
Example #28
0
func (o *Group) Save(db *periwinkle.Tx) {
	if o.Subscriptions != nil {
		var oldSubscriptions []Subscription
		db.Model(o).Related(&oldSubscriptions)

		for _, oldsub := range oldSubscriptions {
			match := false

			for _, newsub := range o.Subscriptions {
				if newsub.AddressID == oldsub.AddressID {
					match = true
					break
				}
			}
			if !match {
				var o UserAddress
				db.First(&o, "id = ?", oldsub.AddressID)
				if o.Medium != "noop" && o.Medium != "admin" {
					if err := db.Where("address_id = ? AND group_id = ?", oldsub.AddressID, oldsub.GroupID).Delete(Subscription{}).Error; err != nil {
						dbError(err)
					}
				}
			}
		}

	}

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

}
Example #29
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
}
Example #30
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
}