Esempio n. 1
0
// ByName fetches the channel by name, type_constant and group_name, it doesnt
// have the best name, but evolved to this situation :/
func (c *Channel) ByName(q *request.Query) (Channel, error) {
	var channel Channel

	if q.GroupName == "" {
		return channel, ErrGroupNameIsNotSet
	}

	if q.Type == "" {
		q.Type = Channel_TYPE_TOPIC
	}

	query := &bongo.Query{
		Selector: map[string]interface{}{
			"group_name":    q.GroupName,
			"type_constant": q.Type,
			"name":          q.Name,
		},
		Pagination: *bongo.NewPagination(q.Limit, q.Skip),
	}

	query.AddScope(RemoveTrollContent(c, q.ShowExempt))

	err := c.One(query)
	if err != nil && err != bongo.RecordNotFound {
		return channel, err
	}

	return *c, nil
}
Esempio n. 2
0
// BySlug fetchs channel message by its slug
// checks if message is in the channel or not
func (c *ChannelMessage) BySlug(query *request.Query) error {
	if query.Slug == "" {
		return ErrSlugIsNotSet
	}

	// fetch message itself
	q := &bongo.Query{
		Selector: map[string]interface{}{
			"slug": query.Slug,
		},
	}

	q.AddScope(RemoveTrollContent(
		c, query.ShowExempt,
	))

	if err := c.One(q); err != nil {
		return err
	}

	query.Type = Channel_TYPE_GROUP
	res, err := c.isInChannel(query, "public")
	if err != nil {
		return err
	}

	if res {
		return nil
	}

	query.Type = Channel_TYPE_ANNOUNCEMENT
	res, err = c.isInChannel(query, "changelog")
	if err != nil {
		return err
	}

	if !res {
		return bongo.RecordNotFound
	}

	return nil
}
Esempio n. 3
0
// ByParticipants fetches the channels by their respective participants
func (c *Channel) ByParticipants(participants []int64, q *request.Query) ([]Channel, error) {
	if q.GroupName == "" {
		return nil, ErrGroupNameIsNotSet
	}

	if len(participants) == 0 {
		return nil, ErrChannelParticipantIsNotSet
	}

	if q.Type == "" {
		q.Type = Channel_TYPE_PRIVATE_MESSAGE
	}

	var channelIds []int64

	cp := NewChannelParticipant()

	err := bongo.B.DB.
		Model(cp).
		Table(cp.BongoName()).
		Joins(
			`left join api.channel on
		 api.channel_participant.channel_id = api.channel.id`).
		Where(
			`api.channel_participant.account_id IN ( ? ) and
		 api.channel_participant.status_constant = ? and
		 api.channel.group_name = ? and
		 api.channel.deleted_at < '0001-01-02 00:00:00+00' and
		 api.channel.type_constant = ?`,
			participants,
			ChannelParticipant_STATUS_ACTIVE,
			q.GroupName,
			q.Type,
		).
		Group("channel_participant.channel_id, channel.created_at").
		Having("COUNT (channel_participant.channel_id) = ?", len(participants)).
		Order("channel.created_at").
		Limit(q.Limit).
		Offset(q.Skip).
		Pluck("api.channel_participant.channel_id", &channelIds).Error
	if err != nil {
		return nil, err
	}

	return c.FetchByIds(channelIds)
}
Esempio n. 4
0
func getUserChannelsQuery(q *request.Query) *gorm.DB {
	c := models.NewChannel()

	if q.Type == "" {
		q.Type = models.Channel_TYPE_PRIVATE_MESSAGE
	}

	return bongo.B.DB.
		Model(c).
		Table(c.BongoName()).
		Select("api.channel_participant.channel_id").
		Joins("left join api.channel_participant on api.channel_participant.channel_id = api.channel.id").
		Where("api.channel_participant.account_id = ? and "+
			"api.channel.group_name = ? and "+
			"api.channel.type_constant = ? and "+
			"api.channel_participant.status_constant = ?",
			q.AccountId,
			q.GroupName,
			q.Type,
			models.ChannelParticipant_STATUS_ACTIVE)
}
Esempio n. 5
0
func (c *Channel) Search(q *request.Query) ([]Channel, error) {
	if q.GroupName == "" {
		return nil, ErrGroupNameIsNotSet
	}

	if q.Type == "" {
		q.Type = Channel_TYPE_TOPIC
	}

	var channels []Channel

	bongoQuery := &bongo.Query{
		Selector: map[string]interface{}{
			"group_name":    q.GroupName,
			"type_constant": q.Type,
		},
		Pagination: *bongo.NewPagination(q.Limit, q.Skip),
	}

	bongoQuery.AddScope(RemoveTrollContent(c, q.ShowExempt))

	query := bongo.B.BuildQuery(c, bongoQuery)

	// use 'ilike' for case-insensitive search
	query = query.Where("name ilike ?", "%"+q.Name+"%")

	if err := bongo.CheckErr(
		query.Find(&channels),
	); err != nil {
		return nil, err
	}

	if channels == nil {
		return make([]Channel, 0), nil
	}

	return channels, nil
}