// Insert saves a conversation
func (m *ConversationType) Insert(siteID int64, profileID int64) (int, error) {
	status, err := m.Validate(siteID, profileID, false, false)
	if err != nil {
		return status, err
	}

	dupeKey := "dupe_" + h.MD5Sum(
		strconv.FormatInt(m.MicrocosmID, 10)+
			m.Title+
			strconv.FormatInt(m.Meta.CreatedByID, 10),
	)
	v, ok := c.GetInt64(dupeKey)
	if ok {
		m.ID = v
		return http.StatusOK, nil
	}

	status, err = m.insert(siteID, profileID)
	if status == http.StatusOK {
		// 5 minute dupe check
		c.SetInt64(dupeKey, m.ID, 60*5)
	}

	return status, err
}
Example #2
0
// MakeGravatarURL hashes the email and creates the Gravatar URL
func MakeGravatarURL(email string) string {
	return fmt.Sprintf(
		"%s%s?d=identicon",
		URLGravatar,
		h.MD5Sum(strings.ToLower(strings.Trim(email, " "))),
	)
}
Example #3
0
// Insert saves a huddle to the database
func (m *HuddleType) Insert(siteID int64) (int, error) {

	dupeKey := "dupe_" + h.MD5Sum(
		m.Title+
			strconv.FormatInt(m.Meta.CreatedByID, 10),
	)

	v, ok := c.GetInt64(dupeKey)
	if ok {
		m.ID = v
		return http.StatusOK, nil
	}

	status, err := m.insert(siteID)

	// 5 second dupe check just to catch people hitting submit multiple times
	c.SetInt64(dupeKey, m.ID, 5)

	return status, err
}
Example #4
0
// Insert saves an event
func (m *EventType) Insert(siteID int64, profileID int64) (int, error) {
	status, err := m.Validate(siteID, profileID, false)
	if err != nil {
		return status, err
	}

	var (
		when  string
		where string
	)

	if m.WhenNullable.Valid {
		when = m.WhenNullable.Time.String()
	}
	if m.WhereNullable.Valid {
		where = m.WhereNullable.String
	}

	dupeKey := "dupe_" + h.MD5Sum(
		strconv.FormatInt(m.MicrocosmID, 10)+
			m.Title+
			when+
			where+
			fmt.Sprintf("%b", m.Lat)+
			fmt.Sprintf("%b", m.Lon)+
			fmt.Sprintf("%b", m.North)+
			fmt.Sprintf("%b", m.East)+
			fmt.Sprintf("%b", m.South)+
			fmt.Sprintf("%b", m.West)+
			m.Status+
			fmt.Sprintf("%d", m.RSVPLimit)+
			strconv.FormatInt(m.Meta.CreatedByID, 10),
	)

	v, ok := c.GetInt64(dupeKey)
	if ok {
		m.ID = v
		return http.StatusOK, nil
	}

	tx, err := h.GetTransaction()
	if err != nil {
		return http.StatusInternalServerError, err
	}
	defer tx.Rollback()

	var insertID int64

	err = tx.QueryRow(`
INSERT INTO events (
    microcosm_id, title, created, created_by, "when",
    duration, "where", lat, lon, bounds_north,
    bounds_east, bounds_south, bounds_west, status, rsvp_limit,
    rsvp_spaces
) VALUES (
    $1, $2, $3, $4, $5,
    $6, $7, $8, $9, $10,
    $11, $12, $13, $14, $15,
    $16
) RETURNING event_id`,
		m.MicrocosmID,
		m.Title,
		m.Meta.Created,
		m.Meta.CreatedByID,
		m.WhenNullable,
		m.Duration,
		m.WhereNullable,
		m.Lat,
		m.Lon,
		m.North,
		m.East,
		m.South,
		m.West,
		m.Status,
		m.RSVPLimit,
		m.RSVPSpaces,
	).Scan(
		&insertID,
	)
	if err != nil {
		glog.Errorf(`Could not create event: %+v`, err)
		return http.StatusInternalServerError,
			fmt.Errorf("Error inserting data and returning ID: %+v", err)
	}
	m.ID = insertID

	err = IncrementMicrocosmItemCount(tx, m.MicrocosmID)
	if err != nil {
		return http.StatusInternalServerError, err
	}

	err = tx.Commit()
	if err != nil {
		glog.Errorf(`Could not commit event transaction: %+v`, err)
		return http.StatusInternalServerError,
			fmt.Errorf("Transaction failed: %v", err.Error())
	}

	// 5 minute dupe check
	c.SetInt64(dupeKey, m.ID, 60*5)

	PurgeCache(h.ItemTypes[h.ItemTypeEvent], m.ID)
	PurgeCache(h.ItemTypes[h.ItemTypeMicrocosm], m.MicrocosmID)

	return http.StatusOK, nil
}