// 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
}
示例#2
0
// GetProfileID fetches a profileID given a userID
func GetProfileID(siteID int64, userID int64) (int64, int, error) {

	if siteID == 0 || userID == 0 {
		return 0, http.StatusOK, nil
	}

	// Get from cache if it's available
	//
	// This map of siteId+userId = profileId is never expected to change, so
	// this cache key is unique and does not conform to the cache flushing
	// mechanism
	mcKey := fmt.Sprintf("s%d_u%d", siteID, userID)
	if val, ok := c.GetInt64(mcKey); ok {
		return val, http.StatusOK, nil
	}

	var profileID int64
	db, err := h.GetConnection()
	if err != nil {
		glog.Error(err)
		return profileID, http.StatusInternalServerError, err
	}

	err = db.QueryRow(`--GetProfileId
SELECT profile_id
  FROM profiles
 WHERE site_id = $1
   AND user_id = $2`,
		siteID,
		userID,
	).Scan(
		&profileID,
	)
	if err == sql.ErrNoRows {
		glog.Warning(err)
		return profileID, http.StatusNotFound,
			fmt.Errorf(
				"Profile for site (%d) and user (%d) not found.",
				siteID,
				userID,
			)

	} else if err != nil {
		glog.Error(err)
		return profileID, http.StatusInternalServerError,
			fmt.Errorf("Database query failed: %v", err.Error())
	}

	c.SetInt64(mcKey, profileID, mcTTL)

	return profileID, http.StatusOK, nil
}
示例#3
0
// GetUnreadHuddleCount fetches the current unread huddle count
func (m *ProfileType) GetUnreadHuddleCount() (int, error) {

	// Get from cache if it's available
	mcKey := fmt.Sprintf(mcProfileKeys[c.CacheCounts], m.ID)
	if i, ok := c.GetInt64(mcKey); ok {

		m.Meta.Stats = append(
			m.Meta.Stats,
			h.StatType{Metric: "unreadHuddles", Value: i},
		)

		return http.StatusOK, nil
	}

	db, err := h.GetConnection()
	if err != nil {
		return http.StatusInternalServerError, err
	}

	var unreadHuddles int64
	err = db.QueryRow(`--GetUnreadHuddleCount
SELECT unread_huddles
  FROM profiles
 WHERE profile_id = $1`,
		m.ID,
	).Scan(
		&unreadHuddles,
	)
	if err != nil {
		return http.StatusInternalServerError,
			fmt.Errorf("Error fetching row: %v", err.Error())
	}

	m.Meta.Stats = append(
		m.Meta.Stats,
		h.StatType{Metric: "unreadHuddles", Value: unreadHuddles},
	)

	c.SetInt64(mcKey, unreadHuddles, mcTTL)

	return http.StatusOK, nil
}
示例#4
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
}
示例#5
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
}