Пример #1
0
func (f *Feed) UpdateStories(s gorp.SqlExecutor) error {
	m := &Member{}
	if err := sqlutil.SelectOneRelation(s, TableNameMember, f.MemberID, m); err != nil {
		return err
	}
	return FeedType(f.Type).GetStories(s, m, f)
}
Пример #2
0
func InviteHandler() httperr.Handler {
	return func(w http.ResponseWriter, r *http.Request) error {
		type inviteReq struct {
			MemberID int64
			Email    string
		}

		req := &inviteReq{}
		if err := json.NewDecoder(r.Body).Decode(req); err != nil {
			return httperr.New(http.StatusBadRequest, err.Error(), err)
		}

		dbmap, err := getDB()
		defer dbmap.Db.Close()
		if err != nil {
			return err
		}

		member := &model.Member{}
		if err := sqlutil.SelectOneRelation(dbmap, model.TableNameMember, req.MemberID, member); err != nil {
			return httperr.New(http.StatusBadRequest, "member not found", err)
		}
		member.SetPassword(model.NewAutoPassword())
		member.Email = req.Email
		if _, err := dbmap.Update(member); err != nil {
			return err
		}
		if err := member.Invite(req.Email); err != nil {
			return err
		}
		return json.NewEncoder(w).Encode(member)
	}
}
Пример #3
0
func (story *Story) PostInsert(s gorp.SqlExecutor) error {
	m := &Member{}
	if err := sqlutil.SelectOneRelation(s, TableNameMember, story.MemberID, m); err != nil {
		return err
	}

	images := append(story.ImagesSlice(), m.ImagesSlice()...)
	m.SetImages(images)

	hashtags := append(story.HashtagsSlice(), m.HashtagsSlice()...)
	m.SetHashtags(hashtags)

	s.Update(m)

	feed := &Feed{}
	if err := sqlutil.SelectOneRelation(s, TableNameFeed, story.FeedID, feed); err != nil {
		return err
	}
	if story.Timestamp > feed.LastRetrieved {
		feed.LastRetrieved = story.Timestamp
		s.Update(feed)
	}
	return nil
}
Пример #4
0
func (story *Story) PostGet(s gorp.SqlExecutor) error {
	story.Object = ObjectNameStory
	story.Links = story.LinksSlice()
	story.Images = story.ImagesSlice()
	story.Hashtags = story.HashtagsSlice()
	story.Location = story.LocationCoords()

	m := &Member{}
	if err := sqlutil.SelectOneRelation(s, TableNameMember, story.MemberID, m); err != nil {
		return err
	}

	story.MemberIcon = m.Icon
	story.CategoryIds = m.CategoryIds
	return nil
}
Пример #5
0
func CommunityHandler() httperr.Handler {
	return func(w http.ResponseWriter, r *http.Request) error {
		dbmap, err := getDB()
		defer dbmap.Db.Close()
		if err != nil {
			return err
		}

		community := &model.Community{}
		if err := sqlutil.SelectOneRelation(dbmap, model.TableNameCommunity, 1, community); err != nil {
			return err
		}

		return json.NewEncoder(w).Encode(community)
	}
}
Пример #6
0
func (f *Feed) PreInsert(s gorp.SqlExecutor) error {
	f.Created = milli.Timestamp(time.Now())
	f.Updated = milli.Timestamp(time.Now())

	icon := ""
	switch FeedType(f.Type) {
	case FeedTypeFacebook:
		// get user from facebook api
		session := facebookSession()
		route := fmt.Sprintf("/%s", f.Identifier)
		result, err := session.Api(route, facebook.GET, nil)
		if err != nil {
			return httperr.New(http.StatusBadRequest, "invalid facebook id", err)
		}
		// decode response
		user := &fbookUser{}
		if err := result.Decode(user); err != nil {
			return err
		}
		icon = user.Cover.Source
	case FeedTypeTwitter:
		api := twitterAPI()
		user, err := api.GetUsersShow(f.Identifier, url.Values{})
		if err != nil {
			return err
		}
		icon = user.ProfileImageURL
	}

	if icon != "" {
		member := &Member{}
		if err := sqlutil.SelectOneRelation(s, TableNameMember, f.MemberID, member); err != nil {
			return err
		}
		member.Icon = icon

		if _, err := s.Update(member); err != nil {
			return err
		}
	}

	return f.Validate()
}
Пример #7
0
func (story *Story) CalculateScore(s gorp.SqlExecutor) error {
	f := &Feed{}
	if err := sqlutil.SelectOneRelation(s, TableNameFeed, story.FeedID, f); err != nil {
		return err
	}

	score := 0.0
	if len(story.ImagesSlice()) > 0 {
		score += 10.0
	}
	if len(story.LinksSlice()) > 0 {
		score += 2.0
	}
	if story.Latitude != 0.0 {
		score += 10.0
	}

	switch FeedType(f.Type) {
	case FeedTypeFacebook:
		score += 3.0
	case FeedTypeTwitter:
		score += 3.0
	}

	// randomize score
	score += (10.0 * rand.Float64())

	// increase score for timely posts
	dur := milli.Time(story.Timestamp).Sub(milli.Time(f.LastRetrieved))
	durScore := float64(dur / time.Hour)
	if durScore > 12.0 {
		durScore = 12.0
	} else if durScore < 0.0 {
		durScore = 0.0
	}
	score += durScore

	story.Score += score

	return nil
}