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 }
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 } }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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) } }
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 }
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 }