(collID, userID int64, email string, confirmed bool)
| 3116 | } |
| 3117 | |
| 3118 | func (db *datastore) AddEmailSubscription(collID, userID int64, email string, confirmed bool) (*EmailSubscriber, error) { |
| 3119 | friendlyChars := "0123456789BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz" |
| 3120 | subID := id.GenerateRandomString(friendlyChars, 8) |
| 3121 | token := id.GenerateRandomString(friendlyChars, 16) |
| 3122 | emailVal := sql.NullString{ |
| 3123 | String: email, |
| 3124 | Valid: email != "", |
| 3125 | } |
| 3126 | userIDVal := sql.NullInt64{ |
| 3127 | Int64: userID, |
| 3128 | Valid: userID > 0, |
| 3129 | } |
| 3130 | |
| 3131 | _, err := db.Exec("INSERT INTO emailsubscribers (id, collection_id, user_id, email, subscribed, token, confirmed) VALUES (?, ?, ?, ?, "+db.now()+", ?, ?)", subID, collID, userIDVal, emailVal, token, confirmed) |
| 3132 | if err != nil { |
| 3133 | if mysqlErr, ok := err.(*mysql.MySQLError); ok { |
| 3134 | if mysqlErr.Number == mySQLErrDuplicateKey { |
| 3135 | // Duplicate, so just return existing subscriber information |
| 3136 | log.Info("Duplicate subscriber for email %s, user %d; returning existing subscriber", email, userID) |
| 3137 | return db.FetchEmailSubscriber(email, userID, collID) |
| 3138 | } |
| 3139 | } |
| 3140 | return nil, err |
| 3141 | } |
| 3142 | |
| 3143 | return &EmailSubscriber{ |
| 3144 | ID: subID, |
| 3145 | CollID: collID, |
| 3146 | UserID: userIDVal, |
| 3147 | Email: emailVal, |
| 3148 | Token: token, |
| 3149 | }, nil |
| 3150 | } |
| 3151 | |
| 3152 | func (db *datastore) IsEmailSubscriber(email string, userID, collID int64) bool { |
| 3153 | var dummy int |
no test coverage detected