Пример #1
0
// Create inserts a new record in the database using params, and returns the newly created id
func Create(params map[string]string) (int64, error) {

	// Remove params not in AllowedParams
	params = model.CleanParams(params, AllowedParams())

	// Check params for invalid values
	err := validateParams(params)
	if err != nil {
		return 0, err
	}

	// Check name is unique - no duplicate names allowed
	count, err := Query().Where("name=?", params["name"]).Count()
	if err != nil {
		return 0, err
	}

	if count > 0 {
		return 0, router.InternalError(err, "User name taken", "A username with this email already exists, sorry.")
	}

	// Update date params
	params["created_at"] = query.TimeString(time.Now().UTC())
	params["updated_at"] = query.TimeString(time.Now().UTC())

	return Query().Insert(params)
}
Пример #2
0
// Create inserts a new record in the database using params, and returns the newly created id
func Create(params map[string]string, fh *multipart.FileHeader) (int64, error) {

	// Remove params not in AllowedParams
	params = model.CleanParams(params, AllowedParamsCreate())

	// Check params for invalid values
	err := validateParams(params)
	if err != nil {
		return 0, err
	}

	// Update date params
	params["created_at"] = query.TimeString(time.Now().UTC())
	params["updated_at"] = query.TimeString(time.Now().UTC())

	// Create the file
	id, err := Query().Insert(params)

	if fh != nil && id != 0 {

		// Now retrieve and save the file representation
		f, err := Find(id)
		if err != nil {
			return id, err
		}

		// Save files to disk using the passed in file data (if any).
		err = f.saveFile(fh)
		if err != nil {
			return id, err
		}
	}

	return id, err
}
Пример #3
0
// Create inserts a new user
func Create(params map[string]string) (int64, error) {

	params = model.CleanParams(params, AllowedParams())

	err := validateParams(params)
	if err != nil {
		return 0, err
	}

	// Check that this user email is not already in use
	if len(params["email"]) > 0 {
		// Try to fetch a user by this email from the db - we don't allow duplicates
		count, err := Query().Where("email=?", params["email"]).Count()
		if err != nil {
			return 0, err
		}

		if count > 0 {
			return 0, errors.New("A username with this email already exists, sorry.")
		}

	}

	// Update/add some params by default
	params["created_at"] = query.TimeString(time.Now().UTC())
	params["updated_at"] = query.TimeString(time.Now().UTC())

	return Query().Insert(params)
}
Пример #4
0
// Create inserts a new record in the database using params, and returns the newly created id
func Create(params map[string]string) (int64, error) {

	// Check params for invalid values
	err := validateParams(params, true)
	if err != nil {
		return 0, err
	}

	// Update date params
	params["created_at"] = query.TimeString(time.Now().UTC())
	params["updated_at"] = query.TimeString(time.Now().UTC())

	return Query().Insert(params)
}
Пример #5
0
// Insert a new tag
func Create(params map[string]string) (int64, error) {

	// Remove params not in AllowedParams
	params = model.CleanParams(params, AllowedParams())

	err := validateParams(params)
	if err != nil {
		return 0, err
	}

	// Update/add some params by default
	params["created_at"] = query.TimeString(time.Now().UTC())
	params["updated_at"] = query.TimeString(time.Now().UTC())

	return Query().Insert(params)
}
Пример #6
0
// DailyEmail sends a daily email to subscribed users with top stories - change this to WeeklyEmail
// before putting it into production
// We should probably only do this for kenny at present
func DailyEmail(context schedule.Context) {
	context.Log("Sending daily email")

	// First fetch our stories over 5 points
	q := stories.Popular()

	// Must be within 7 days
	q.Where("created_at > current_timestamp - interval '7 day'")

	// Order by rank
	q.Order("rank desc, points desc, id desc")

	// Don't fetch stories that have already been mailed
	q.Where("newsletter_at IS NULL")

	// Fetch the stories
	topStories, err := stories.FindAll(q)
	if err != nil {
		context.Logf("#error getting top story tweet %s", err)
		return
	}

	if len(topStories) == 0 {
		context.Logf("#warn no stories found for newsletter")
		return
	}

	// Now fetch our recipient (initially just Kenny as this is in testing)
	recipient, err := users.Find(1)
	if err != nil {
		context.Logf("#error getting email reciipents %s", err)
		return
	}

	var jobStories []*stories.Story

	// Email recipients the stories in question - we should perhaps save in db so that we can
	// have an issue number and always reproduce the digests?
	mailContext := map[string]interface{}{
		"stories": topStories,
		"jobs":    jobStories,
	}
	err = mail.SendOne(recipient.Email, "Go News Digest", "users/views/mail/digest.html.got", mailContext)
	if err != nil {
		context.Logf("#error sending email %s", err)
		return
	}

	// Record that these stories have been mailed in db
	params := map[string]string{"newsletter_at": query.TimeString(time.Now().UTC())}
	err = q.Order("").UpdateAll(params)
	if err != nil {
		context.Logf("#error updating top story newsletter_at %s", err)
		return
	}

}
Пример #7
0
// Update this user
func (m *User) Update(params map[string]string) error {

	err := validateParams(params)
	if err != nil {
		return err
	}

	// Make sure updated_at is set to the current time
	params["updated_at"] = query.TimeString(time.Now().UTC())

	return Query().Where("id=?", m.Id).Update(params)
}
Пример #8
0
// TweetTopStory tweets the top story
func TweetTopStory(context schedule.Context) {
	context.Log("Sending top story tweet")

	// Get the top story which has not been tweeted yet, newer than 1 day (we don't look at older stories)
	q := stories.Popular().Limit(1).Order("rank desc, points desc, id desc")

	// Don't fetch old stories - at some point soon this can come down to 1 day
	// as all older stories will have been tweeted
	// For now omit this as we have a backlog of old unposted stories
	// q.Where("created_at > current_timestamp - interval '60 days'")

	// Don't fetch stories that have already been tweeted
	q.Where("tweeted_at IS NULL")

	// Fetch the stories
	results, err := stories.FindAll(q)
	if err != nil {
		context.Logf("#error getting top story tweet %s", err)
		return
	}

	if len(results) > 0 {
		story := results[0]

		// Link to the primary url for this type of story
		url := story.PrimaryURL()

		if strings.HasPrefix(url, "/") {
			url = "https://golangnews.com" + url
		}

		tweet := fmt.Sprintf("%s #golang %s", story.Name, url)

		context.Logf("#info sending tweet:%s", tweet)

		_, err := twitter.Tweet(tweet)
		if err != nil {
			context.Logf("#error tweeting top story %s", err)
			return
		}

		// Record that this story has been tweeted in db
		params := map[string]string{"tweeted_at": query.TimeString(time.Now().UTC())}
		err = story.Update(params)
		if err != nil {
			context.Logf("#error updating top story tweet %s", err)
			return
		}
	} else {
		context.Logf("#warn no top story found for tweet")
	}

}
Пример #9
0
// Update sets the record in the database from params
func (m *Story) Update(params map[string]string) error {

	// Check params for invalid values, but only if passed in
	err := validateParams(params, false)
	if err != nil {
		return err
	}

	// Update date params
	params["updated_at"] = query.TimeString(time.Now().UTC())

	return Query().Where("id=?", m.Id).Update(params)
}
Пример #10
0
// Update sets the record in the database from params
func (m *Comment) Update(params map[string]string) error {

	// Check params for invalid values
	err := validateParams(params)
	if err != nil {
		return err
	}

	// Update date params
	params["updated_at"] = query.TimeString(time.Now().UTC())

	return Query().Where("id=?", m.Id).Update(params)
}
Пример #11
0
// Create inserts a new image record in the database and returns the id
func Create(params map[string]string, fh *multipart.FileHeader) (int64, error) {

	// Remove params not in AllowedParams
	params = model.CleanParams(params, AllowedParams())

	err := validateParams(params)
	if err != nil {
		return 0, err
	}

	// Update/add some params by default
	params["created_at"] = query.TimeString(time.Now().UTC())
	params["updated_at"] = query.TimeString(time.Now().UTC())

	id, err := Query().Insert(params)

	if fh != nil && id != 0 {
		// Retreive the form image data by opening the referenced tmp file
		f, err := fh.Open()
		if err != nil {
			return id, err
		}

		// Now retrieve the image concerned, and save the file representations
		image, err := Find(id)
		if err != nil {
			return id, err
		}

		// Save files to disk using the passed in file data (if any)
		err = image.SaveImageRepresentations(f)
		if err != nil {
			return id, err
		}
	}

	return id, err
}
Пример #12
0
// Update this user
func (m *User) Update(params map[string]string) error {

	// Remove params not in AllowedParams
	params = model.CleanParams(params, AllowedParams())

	err := validateParams(params)
	if err != nil {
		return err
	}

	// Make sure updated_at is set to the current time
	params["updated_at"] = query.TimeString(time.Now().UTC())

	return Query().Where("id=?", m.Id).Update(params)
}
Пример #13
0
// Update sets the record in the database from params
func (m *User) Update(params map[string]string) error {

	// Remove params not in AllowedParams
	params = model.CleanParams(params, AllowedParams())

	// Check params for invalid values
	err := validateParams(params)
	if err != nil {
		return err
	}

	// Update date params
	params["updated_at"] = query.TimeString(time.Now().UTC())

	return Query().Where("id=?", m.Id).Update(params)
}
Пример #14
0
// TweetTopStory tweets the top story
func TweetTopStory(context schedule.Context) {
	context.Log("Sending top story tweet")

	// Get the top story which has not been tweeted yet, newer than 1 day (we don't look at older stories)
	q := stories.Popular().Limit(1).Order("rank desc, points desc, id desc")

	// Don't fetch old stories
	q.Where("created_at > current_timestamp - interval '1 day'")

	// Don't fetch stories that have already been tweeted
	q.Where("tweeted_at IS NULL")

	// Fetch the stories
	results, err := stories.FindAll(q)
	if err != nil {
		context.Logf("#error getting top story tweet %s", err)
		return
	}

	if len(results) > 0 {
		story := results[0]
		// TWEET
		tweet := fmt.Sprintf("%s #golang %s", story.Name, story.Url)

		_, err := twitter.Tweet(tweet)
		if err != nil {
			context.Logf("#error tweeting top story %s", err)
			return
		}

		// Record that this story has been tweeted in db
		params := map[string]string{"tweeted_at": query.TimeString(time.Now().UTC())}
		err = story.Update(params)
		if err != nil {
			context.Logf("#error updating top story tweet %s", err)
			return
		}
	} else {
		context.Logf("#warn no top story found for tweet")
	}

}
Пример #15
0
// TweetStory tweets the given story
func TweetStory(context schedule.Context, story *stories.Story) {

	// Base url from config
	baseURL := context.Config("root_url")

	// Link to the primary url for this type of story
	url := story.PrimaryURL()

	// Check for relative urls
	if strings.HasPrefix(url, "/") {
		url = baseURL + url
	}

	tweet := fmt.Sprintf("%s #golang %s", story.Name, url)

	// If the tweet will be too long for twitter, use GN url
	if len(tweet) > 140 {
		tweet = fmt.Sprintf("%s #golang %s", story.Name, baseURL+story.URLShow())
	}

	context.Logf("#info sending tweet:%s", tweet)

	_, err := twitter.Tweet(tweet)
	if err != nil {
		context.Logf("#error tweeting top story %s", err)
		return
	}

	// Record that this story has been tweeted in db
	params := map[string]string{"tweeted_at": query.TimeString(time.Now().UTC())}
	err = story.Update(params)
	if err != nil {
		context.Logf("#error updating top story tweet %s", err)
		return
	}

}
Пример #16
0
// Update this tag
func (m *Tag) Update(params map[string]string) error {

	// Remove params not in AllowedParams
	params = model.CleanParams(params, AllowedParams())

	err := validateParams(params)
	if err != nil {
		return err
	}

	// Make sure updated_at is set to the current time
	params["updated_at"] = query.TimeString(time.Now().UTC())

	// Always regenerate dotted ids - we fetch all tags first to avoid db calls
	q := Query().Select("select id,parent_id from tags").Order("id asc")
	tagsList, err := FindAll(q)
	if err == nil {
		params["dotted_ids"] = m.CalculateDottedIds(tagsList)
	} else {
		return err
	}

	return Query().Where("id=?", m.Id).Update(params)
}