Пример #1
0
// Tests are done.
func (a *Account) FetchOrCreate() error {
	if err := ValidateAccount(a); err != nil {
		return err
	}

	selector := map[string]interface{}{
		"old_id": a.OldId,
	}

	err := a.One(bongo.NewQS(selector))
	// if we dont get any error
	// it means we found the record in our db
	if err == nil {
		return nil
	}

	// first check if the err is not found err
	if err == bongo.RecordNotFound {
		if a.Nick == "" {
			return ErrNickIsNotSet
		}

		if err := a.Create(); err != nil {
			return err
		}

		return nil
	}

	return err
}
Пример #2
0
func (c *ChannelParticipant) checkAccountStatus(accountId int64) (bool, error) {
	if accountId == 0 {
		return false, nil
	}

	if c.ChannelId == 0 {
		return false, ErrChannelIdIsNotSet
	}

	if c.StatusConstant == "" {
		c.StatusConstant = ChannelParticipant_STATUS_ACTIVE
	}

	selector := map[string]interface{}{
		"channel_id":      c.ChannelId,
		"account_id":      accountId,
		"status_constant": c.StatusConstant,
	}

	err := c.One(bongo.NewQS(selector))
	if err == nil {
		return true, nil
	}

	if err == bongo.RecordNotFound {
		return false, nil
	}

	return false, err
}
Пример #3
0
func (a *Account) ByToken(token string) error {
	if token == "" {
		return ErrIdIsNotSet
	}
	selector := map[string]interface{}{
		"token": token,
	}

	return a.One(bongo.NewQS(selector))
}
Пример #4
0
func (a *Account) ByOldId(oldId string) error {
	if oldId == "" {
		return ErrOldIdIsNotSet
	}

	selector := map[string]interface{}{
		"old_id": oldId,
	}

	return a.One(bongo.NewQS(selector))
}
Пример #5
0
// Tests are done.
func (a *Account) ByNick(nick string) error {
	if nick == "" {
		return ErrNickIsNotSet
	}

	selector := map[string]interface{}{
		"nick": nick,
	}

	return a.One(bongo.NewQS(selector))
}
Пример #6
0
func (c *ChannelParticipant) fetchParticipant(selector map[string]interface{}) error {
	if c.ChannelId == 0 {
		return ErrChannelIdIsNotSet
	}

	if c.AccountId == 0 {
		return ErrAccountIdIsNotSet
	}

	// TODO do we need to add isExempt scope here?
	return c.One(bongo.NewQS(selector))
}
Пример #7
0
// Create creates a channel in db
// some fields of the channel must be filled (should not be empty)
//
// Tests are done..
func (c *Channel) Create() error {
	if c.Name == "" || c.GroupName == "" || c.TypeConstant == "" || c.CreatorId == 0 {
		return fmt.Errorf("Validation failed %s - %s - %s - %d", c.Name, c.GroupName, c.TypeConstant, c.CreatorId)
	}

	// golang returns -1 if item not in the string
	if strings.Index(c.Name, " ") > -1 {
		return fmt.Errorf("Channel name %q has empty space in it", c.Name)
	}

	// if channel type is not group or following try to create it
	if c.TypeConstant != Channel_TYPE_GROUP && c.TypeConstant != Channel_TYPE_FOLLOWERS {
		return bongo.B.Create(c)
	}

	// selectors helps database to create what we need
	var selector map[string]interface{}

	switch c.TypeConstant {
	case Channel_TYPE_GROUP:
		selector = map[string]interface{}{
			"group_name":    c.GroupName,
			"type_constant": c.TypeConstant,
		}
	case Channel_TYPE_DEFAULT:
		selector = map[string]interface{}{
			"group_name":    c.GroupName,
			"type_constant": c.TypeConstant,
		}
	case Channel_TYPE_FOLLOWERS:
		selector = map[string]interface{}{
			"creator_id":    c.CreatorId,
			"type_constant": c.TypeConstant,
		}
	}

	// if err is nil
	// it means we already have that channel
	err := c.One(bongo.NewQS(selector))
	if err == nil {
		// this means, we already have the channel in the db, it is safe to return
		return nil
	}

	// if error is not NotFound, return it
	if err != bongo.RecordNotFound {
		return err
	}

	// if we couldnt find the record in the db, then create it
	return bongo.B.Create(c)
}
Пример #8
0
func setChangeLogChannel(log logging.Logger, group *kodingmodels.Group) {

	c := models.NewChannel()
	selector := map[string]interface{}{
		"type_constant": models.Channel_TYPE_ANNOUNCEMENT,
		"group_name":    models.Channel_KODING_NAME,
	}

	// if err is nil
	// it means we already have that channel
	err := c.One(bongo.NewQS(selector))
	if err != nil && err != bongo.RecordNotFound {
		log.Error("err while fetching changelog channel:", err.Error())
		return
	}

	if err == bongo.RecordNotFound {
		log.Error("postgres changelog couldn't found, creating it")

		acc, err := createChannelOwner(group)
		if err != nil {
			log.Error(err.Error())
			return
		}

		c.Name = "changelog"
		c.CreatorId = acc.Id
		c.GroupName = models.Channel_KODING_NAME
		c.TypeConstant = models.Channel_TYPE_ANNOUNCEMENT
		c.PrivacyConstant = models.Channel_PRIVACY_PRIVATE
		if err := c.Create(); err != nil {
			log.Error("err while creating the koding channel:", err.Error())
			return
		}
	}

	socialApiAnnouncementChannelId := strconv.FormatInt(c.Id, 10)
	if group.SocialApiAnnouncementChannelId == socialApiAnnouncementChannelId {
		log.Info("mongo and postgres socialApiAnnouncementChannel ids are same")
		return
	}

	log.Debug("mongo and postgres socialApiAnnouncementChannel ids are different, fixing it")
	if err := updateGroupPartially(group.Id, "socialApiAnnouncementChannelId", strconv.FormatInt(c.Id, 10)); err != nil {
		log.Error("err while udpating socialApiAnnouncementChannelId:", err.Error())
		return
	}
}
Пример #9
0
func (m *MessageReply) Delete() error {
	selector := map[string]interface{}{
		"message_id": m.MessageId,
		"reply_id":   m.ReplyId,
	}

	if err := m.One(bongo.NewQS(selector)); err != nil {
		return err
	}

	err := bongo.B.Delete(m)
	if err != nil {
		return err
	}

	return nil
}
Пример #10
0
// FetchChannel fetchs the channel of the account
//
// Channel_TYPE_GROUP as parameter returns error , in the tests!!!!
// TO-DO, other types dont return error
//
// Tests are done
func (a *Account) FetchChannel(channelType string) (*Channel, error) {
	if a.Id == 0 {
		return nil, ErrAccountIdIsNotSet
	}

	c := NewChannel()
	selector := map[string]interface{}{
		"creator_id":    a.Id,
		"type_constant": channelType,
	}

	if err := c.One(bongo.NewQS(selector)); err != nil {
		return nil, err
	}

	return c, nil
}
Пример #11
0
// FetchMessageList fetchs the messages in the channel
//
// has full test suit
func (c *Channel) FetchMessageList(messageId int64) (*ChannelMessageList, error) {
	if c.Id == 0 {
		return nil, ErrChannelIdIsNotSet
	}

	if messageId == 0 {
		return nil, ErrMessageIdIsNotSet
	}

	cml := NewChannelMessageList()
	selector := map[string]interface{}{
		"channel_id": c.Id,
		"message_id": messageId,
	}

	return cml, cml.One(bongo.NewQS(selector))
}
Пример #12
0
func setPublicChannel(log logging.Logger, group *kodingmodels.Group) {
	c := models.NewChannel()
	selector := map[string]interface{}{
		"type_constant": models.Channel_TYPE_GROUP,
		"group_name":    models.Channel_KODING_NAME,
	}

	err := c.One(bongo.NewQS(selector))
	if err != nil && err != bongo.RecordNotFound {
		log.Error("err while fetching koding channel:", err.Error())
		return
	}

	if err == bongo.RecordNotFound {
		log.Debug("postgres group couldn't found, creating it")

		acc, err := createChannelOwner(group)
		if err != nil {
			log.Error(err.Error())
			return
		}

		c.Name = "public"
		c.CreatorId = acc.Id
		c.GroupName = models.Channel_KODING_NAME
		c.TypeConstant = models.Channel_TYPE_GROUP
		c.PrivacyConstant = models.Channel_PRIVACY_PUBLIC
		if err := c.Create(); err != nil {
			log.Error("err while creating the koding channel: %s", err.Error())
			return
		}
	}

	socialApiId := strconv.FormatInt(c.Id, 10)
	if group.SocialApiChannelId == socialApiId {
		log.Debug("mongo and postgres socialApiChannelId ids are same")
		return
	}

	log.Debug("mongo and postgres socialApiChannelId ids are different, fixing it")
	if err := updateGroupPartially(group.Id, "socialApiChannelId", socialApiId); err != nil {
		log.Error("err while udpating socialApiChannelId: %s", err.Error())
		return
	}
}
Пример #13
0
// todo fix slug generation with the slugifiable.coffee implementation
func Slugify(message *ChannelMessage) (*ChannelMessage, error) {

	if message.TypeConstant != ChannelMessage_TYPE_POST {
		return message, nil
	}

	// we want hypen between our words
	slug.Replacement = '-'
	res := NewChannelMessage()

	suggestedSlug := slug.Clean(message.Body)
	if len(suggestedSlug) > 80 {
		suggestedSlug = suggestedSlug[:79]
	}

	query := map[string]interface{}{
		"slug": suggestedSlug,
	}

	rand.Seed(time.Now().UnixNano())

	for tryCount := 0; tryCount < 10; tryCount++ {
		if err := res.One(bongo.NewQS(query)); err != nil {
			// if we got error from db, it means it couldnt find the
			// data, so we can return here
			if err != bongo.RecordNotFound {
				return nil, err
			}
			message.Slug = suggestedSlug
			return message, nil
		}
		// iterate with the new slug
		// this is not the best strategy to generate slug
		// but also not the worst
		suggestedSlug = suggestedSlug + "-" + strconv.Itoa(rand.Intn(1000000000))
		query["slug"] = suggestedSlug
	}

	return nil, fmt.Errorf("couldnt generate unique slug:%s", message.Slug)
}