func TestChannelMessageFetchMessageCount(t *testing.T) { tests.WithRunner(t, func(r *runner.Runner) { Convey("while fetching message count since the given time", t, func() { channel, accounts := CreateChannelWithParticipants() cm1 := CreateMessage(channel.Id, accounts[0].Id, ChannelMessage_TYPE_PRIVATE_MESSAGE) CreateMessage(channel.Id, accounts[1].Id, ChannelMessage_TYPE_PRIVATE_MESSAGE) CreateMessage(channel.Id, accounts[0].Id, ChannelMessage_TYPE_PRIVATE_MESSAGE) CreateMessage(channel.Id, accounts[0].Id, ChannelMessage_TYPE_PRIVATE_MESSAGE) query := request.NewQuery() query.Type = ChannelMessage_TYPE_PRIVATE_MESSAGE query.GroupChannelId = channel.Id query.From = cm1.CreatedAt Convey("first user should see 4 messages when their account is not excluded", func() { c := NewChannelMessage() count, err := c.FetchTotalMessageCount(query) So(err, ShouldBeNil) So(count, ShouldEqual, 4) }) Convey("second user should see 3 messages then their account is excluded", func() { c := NewChannelMessage() query.ExcludeField("AccountId", accounts[1].Id) count, err := c.FetchTotalMessageCount(query) So(err, ShouldBeNil) So(count, ShouldEqual, 3) }) }) }) }
func FetchPostCount(u *url.URL, h http.Header, _ interface{}, context *models.Context) (int, http.Header, interface{}, error) { query := request.GetQuery(u) query = context.OverrideQuery(query) accountId, err := request.GetId(u) if err != nil { return response.NewBadRequest(err) } c, err := models.Cache.Channel.ByGroupName(query.GroupName) if err != nil { return response.NewBadRequest(err) } // fetch user post count in koding channel q := request.NewQuery() q.AccountId = accountId q.Type = models.ChannelMessage_TYPE_POST q.GroupChannelId = c.Id cm := models.NewChannelMessage() count, err := cm.FetchTotalMessageCount(q) if err != nil { return response.NewBadRequest(err) } res := new(models.CountResponse) res.TotalCount = count return response.NewOK(res) }
func TestChannelMessageFetchLatestMessages(t *testing.T) { tests.WithRunner(t, func(r *runner.Runner) { Convey("while fetching latest messages of a channel with three participants and four messages", t, func() { channel, accounts := CreateChannelWithParticipants() cm1 := CreateMessage(channel.Id, accounts[0].Id, ChannelMessage_TYPE_PRIVATE_MESSAGE) cm2 := CreateMessage(channel.Id, accounts[1].Id, ChannelMessage_TYPE_PRIVATE_MESSAGE) cm3 := CreateMessage(channel.Id, accounts[0].Id, ChannelMessage_TYPE_PRIVATE_MESSAGE) cm4 := CreateMessage(channel.Id, accounts[0].Id, ChannelMessage_TYPE_PRIVATE_MESSAGE) query := request.NewQuery() query.Limit = 3 query.AddSortField("CreatedAt", request.ORDER_DESC) query.Type = ChannelMessage_TYPE_PRIVATE_MESSAGE Convey("first user should see last three messages when no parameters are set", func() { c := NewChannelMessage() cms, err := c.FetchMessagesByChannelId(channel.Id, query) So(err, ShouldBeNil) So(len(cms), ShouldEqual, 3) So(cms[2].Id, ShouldEqual, cm2.Id) }) Convey("first user should see one message when their account id is excluded", func() { query.ExcludeField("AccountId", cm1.AccountId) c := NewChannelMessage() cms, err := c.FetchMessagesByChannelId(channel.Id, query) So(err, ShouldBeNil) So(len(cms), ShouldEqual, 1) So(cms[0].Id, ShouldEqual, cm2.Id) }) Convey("second user should see two messages starting with their own message", func() { c := NewChannelMessage() query.ExcludeField("AccountId", cm2.AccountId) query.From = cm2.CreatedAt cms, err := c.FetchMessagesByChannelId(channel.Id, query) So(err, ShouldBeNil) So(len(cms), ShouldEqual, 2) So(cms[0].Id, ShouldEqual, cm4.Id) So(cms[1].Id, ShouldEqual, cm3.Id) }) Convey("third user should see three messages when all parameters are set", func() { c := NewChannelMessage() query.ExcludeField("AccountId", accounts[2].Id) query.From = cm1.CreatedAt cms, err := c.FetchMessagesByChannelId(channel.Id, query) So(err, ShouldBeNil) So(len(cms), ShouldEqual, 3) So(cms[0].Id, ShouldEqual, cm4.Id) So(cms[1].Id, ShouldEqual, cm3.Id) So(cms[2].Id, ShouldEqual, cm2.Id) }) }) }) }
func CacheForChannel(id int64) (string, error) { query := request.NewQuery().SetDefaults() cc, err := BuildChannelContainer(id, query) d, err := json.Marshal(cc) if err != nil { return "", err } return string(d), nil }
func CacheForChannelMessage(id int64) (string, error) { query := request.NewQuery().SetDefaults() // no need to add IsInteracted data into cache query.AddIsInteracted = false cmc, err := BuildChannelMessageContainer(id, query) d, err := json.Marshal(cmc) if err != nil { return "", err } return string(d), err }
func New(log logging.Logger, client algoliasearch.Client, indexSuffix string) *Controller { // TODO later on listen channel_participant_added event and remove this koding channel fetch c := models.NewChannel() q := request.NewQuery() q.GroupName = "koding" q.Name = "public" q.Type = models.Channel_TYPE_GROUP channel, err := c.ByName(q) if err != nil { log.Error("Could not fetch koding channel: %s:", err) } var channelId string if channel.Id != 0 { channelId = strconv.FormatInt(channel.Id, 10) } controller := &Controller{ log: log, client: client, indexes: &IndexSet{ IndexTopics: &IndexSetItem{ Index: client.InitIndex(IndexTopics + indexSuffix), Settings: &Settings{ // empty slice means all properties will be searchable AttributesToIndex: []string{}, }, }, IndexAccounts: &IndexSetItem{ Index: client.InitIndex(IndexAccounts + indexSuffix), Settings: &Settings{ AttributesToIndex: []string{ "nick", "email", "firstName", "lastName", "_tags", }, UnretrievableAttributes: []string{"email"}, }, }, IndexMessages: &IndexSetItem{ Index: client.InitIndex(IndexMessages + indexSuffix), Settings: &Settings{ AttributesToIndex: []string{}, }, }, }, kodingChannelId: channelId, } return controller }
func (m *MessageReply) ListAll() ([]ChannelMessage, error) { query := request.NewQuery() query.Limit = 0 query.Skip = 0 return m.fetchMessages(query) }
func TestChannelByParticipants(t *testing.T) { tests.WithRunner(t, func(r *runner.Runner) { appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() Convey("while fetching channels by their participants", t, func() { _, _, groupName := CreateRandomGroupDataWithChecks() Convey("group name should be set in query", func() { q := request.NewQuery() q.GroupName = "" c := NewChannel() _, err := c.ByParticipants([]int64{}, q) So(err, ShouldNotBeNil) So(err, ShouldEqual, ErrGroupNameIsNotSet) }) Convey("at least one participant should be passed", func() { q := request.NewQuery() q.GroupName = groupName c := NewChannel() _, err := c.ByParticipants([]int64{}, q) So(err, ShouldNotBeNil) So(err, ShouldEqual, ErrChannelParticipantIsNotSet) }) acc1 := CreateAccountWithTest() acc2 := CreateAccountWithTest() acc3 := CreateAccountWithTest() tc1 := CreateTypedGroupedChannelWithTest( acc1.Id, Channel_TYPE_TOPIC, groupName, ) AddParticipantsWithTest(tc1.Id, acc1.Id, acc2.Id, acc3.Id) tc2 := CreateTypedGroupedChannelWithTest( acc1.Id, Channel_TYPE_TOPIC, groupName, ) AddParticipantsWithTest(tc2.Id, acc1.Id, acc2.Id, acc3.Id) Convey("ordering should be working by channel created_at", func() { q := request.NewQuery() q.GroupName = groupName q.Type = Channel_TYPE_TOPIC c := NewChannel() channels, err := c.ByParticipants([]int64{acc1.Id, acc2.Id, acc3.Id}, q) So(err, ShouldBeNil) So(len(channels), ShouldEqual, 2) So(channels[0].Id, ShouldEqual, tc1.Id) So(channels[1].Id, ShouldEqual, tc2.Id) }) Convey("skip options should be working", func() { q := request.NewQuery() q.GroupName = groupName q.Type = Channel_TYPE_TOPIC q.Skip = 1 c := NewChannel() channels, err := c.ByParticipants([]int64{acc1.Id, acc2.Id, acc3.Id}, q) So(err, ShouldBeNil) So(len(channels), ShouldEqual, 1) So(channels[0].Id, ShouldEqual, tc2.Id) }) Convey("limit options should be working", func() { q := request.NewQuery() q.GroupName = groupName q.Type = Channel_TYPE_TOPIC q.Limit = 1 c := NewChannel() channels, err := c.ByParticipants([]int64{acc1.Id, acc2.Id, acc3.Id}, q) So(err, ShouldBeNil) So(len(channels), ShouldEqual, 1) So(channels[0].Id, ShouldEqual, tc1.Id) }) Convey("type option should be working", func() { pc1 := CreateTypedGroupedChannelWithTest( acc1.Id, Channel_TYPE_PRIVATE_MESSAGE, groupName, ) AddParticipantsWithTest(pc1.Id, acc1.Id, acc2.Id, acc3.Id) q := request.NewQuery() q.GroupName = groupName q.Type = Channel_TYPE_PRIVATE_MESSAGE c := NewChannel() channels, err := c.ByParticipants([]int64{acc1.Id, acc2.Id, acc3.Id}, q) So(err, ShouldBeNil) So(len(channels), ShouldEqual, 1) So(channels[0].Id, ShouldEqual, pc1.Id) }) Convey("all channels should be active", func() { // delete the second channel So(tc2.Delete(), ShouldBeNil) q := request.NewQuery() q.GroupName = groupName q.Type = Channel_TYPE_TOPIC c := NewChannel() channels, err := c.ByParticipants([]int64{acc1.Id, acc2.Id, acc3.Id}, q) So(err, ShouldBeNil) So(len(channels), ShouldEqual, 1) So(channels[0].Id, ShouldEqual, tc1.Id) }) Convey("all members should be active", func() { // delete the second participant from second channel So(tc2.RemoveParticipant(acc2.Id), ShouldBeNil) q := request.NewQuery() q.GroupName = groupName q.Type = Channel_TYPE_TOPIC c := NewChannel() channels, err := c.ByParticipants([]int64{acc1.Id, acc2.Id, acc3.Id}, q) So(err, ShouldBeNil) So(len(channels), ShouldEqual, 1) So(channels[0].Id, ShouldEqual, tc1.Id) }) Convey("if the members are also in other groups", func() { Convey("group context should be working", func() { _, _, groupName := CreateRandomGroupDataWithChecks() tc1 := CreateTypedGroupedChannelWithTest( acc1.Id, Channel_TYPE_TOPIC, groupName, ) AddParticipantsWithTest(tc1.Id, acc1.Id, acc2.Id, acc3.Id) tc2 := CreateTypedGroupedChannelWithTest( acc1.Id, Channel_TYPE_TOPIC, groupName, ) AddParticipantsWithTest(tc2.Id, acc1.Id, acc2.Id, acc3.Id) q := request.NewQuery() q.GroupName = groupName q.Type = Channel_TYPE_TOPIC c := NewChannel() channels, err := c.ByParticipants([]int64{acc1.Id, acc2.Id, acc3.Id}, q) So(err, ShouldBeNil) So(len(channels), ShouldEqual, 2) So(channels[0].GroupName, ShouldEqual, groupName) }) }) }) }) }
func TestPresenceDailyFetchActiveAccounts(t *testing.T) { tests.WithRunner(t, func(r *runner.Runner) { Convey("With given presence data", t, func() { acc1, _, groupName := CreateRandomGroupDataWithChecks() gr, err := modelhelper.GetGroup(groupName) tests.ResultedWithNoErrorCheck(gr, err) err = modelhelper.MakeAdmin(bson.ObjectIdHex(acc1.OldId), gr.Id) So(err, ShouldBeNil) p1 := &PresenceDaily{ AccountId: acc1.Id, GroupName: groupName, CreatedAt: time.Now().UTC(), } So(p1.Create(), ShouldBeNil) ses, err := modelhelper.FetchOrCreateSession(acc1.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) for i := 0; i < 5; i++ { // create accounts acc2 := CreateAccountInBothDbsWithCheck() // add them to presence p1 := &PresenceDaily{ AccountId: acc2.Id, GroupName: groupName, CreatedAt: time.Now().UTC(), } So(p1.Create(), ShouldBeNil) } Convey("FetchActiveAccounts should not work properly if query is nil", func() { query := request.NewQuery() c1, err := (&PresenceDaily{}).FetchActiveAccounts(query) So(err, ShouldNotBeNil) So(c1, ShouldBeNil) Convey("FetchActiveAccounts should work properly", func() { query := request.NewQuery().SetDefaults() query.GroupName = groupName c1, err := (&PresenceDaily{}).FetchActiveAccounts(query) So(err, ShouldBeNil) So(c1, ShouldNotBeNil) }) Convey("Pagination skip should work properly", func() { query := request.NewQuery().SetDefaults() query.GroupName = groupName query.Skip = 20 c1, err := (&PresenceDaily{}).FetchActiveAccounts(query) So(err, ShouldNotBeNil) So(err, ShouldEqual, bongo.RecordNotFound) So(c1, ShouldBeNil) Convey("Pagination limit should work properly", func() { query := request.NewQuery().SetDefaults() query.GroupName = groupName query.Limit = 4 c1, err := (&PresenceDaily{}).FetchActiveAccounts(query) So(err, ShouldBeNil) So(c1, ShouldNotBeNil) So(len(c1.Accounts), ShouldEqual, 4) }) }) }) }) }) }