// lists followed channels of an account func ListChannels(u *url.URL, h http.Header, _ interface{}, c *models.Context) (int, http.Header, interface{}, error) { accountId, err := request.GetURIInt64(u, "id") if err != nil { return response.NewBadRequest(err) } if !c.IsLoggedIn() { return response.NewBadRequest(models.ErrNotLoggedIn) } if accountId != c.Client.Account.Id { return response.NewBadRequest(models.ErrAccessDenied) } query := request.GetQuery(u) query = c.OverrideQuery(query) if query.Type == "" { query.Type = models.Channel_TYPE_TOPIC } a := &models.Account{Id: accountId} channels, err := a.FetchChannels(query) if err != nil { return response.NewBadRequest(err) } cc := models.NewChannelContainers() cc.PopulateWith(channels, query.AccountId).AddUnreadCount(query.AccountId) return response.HandleResultAndError(cc, cc.Err()) }
// ByParticipants finds private message channels by their participants func ByParticipants(u *url.URL, h http.Header, _ interface{}, context *models.Context) (int, http.Header, interface{}, error) { // only logged in users if !context.IsLoggedIn() { return response.NewBadRequest(models.ErrNotLoggedIn) } query := request.GetQuery(u) query = context.OverrideQuery(query) participantsStr, ok := u.Query()["id"] if !ok { return response.NewBadRequest(errors.New("participants not set")) } if len(participantsStr) == 0 { return response.NewBadRequest(errors.New("at least one participant is required")) } unify := make(map[string]interface{}) // add current account to participants list unify[strconv.FormatInt(context.Client.Account.Id, 10)] = struct{}{} // remove duplicates from participants for i := range participantsStr { unify[participantsStr[i]] = struct{}{} } participants := make([]int64, 0) // convert strings to int64 for participantStr := range unify { i, err := strconv.ParseInt(participantStr, 10, 64) if err != nil { return response.NewBadRequest(err) } participants = append(participants, i) } channels, err := models.NewChannel().ByParticipants(participants, query) if err != nil { if err == bongo.RecordNotFound { return response.NewNotFound() } } cc := models.NewChannelContainers(). PopulateWith(channels, context.Client.Account.Id). AddLastMessage(context.Client.Account.Id). AddUnreadCount(context.Client.Account.Id) return response.HandleResultAndError(cc, cc.Err()) }
func handleChannelListResponse(channelList []models.Channel, q *request.Query) (int, http.Header, interface{}, error) { cc := models.NewChannelContainers() if err := cc.Fetch(channelList, q); err != nil { return response.NewBadRequest(err) } cc.AddIsParticipant(q.AccountId) // TODO this should be in the channel cache by default cc.AddLastMessage(q.AccountId) return response.HandleResultAndError(cc, cc.Err()) }
func FetchAccountChannels(token string) (*models.ChannelContainers, error) { cc := models.NewChannelContainers() res, err := marshallAndSendRequestWithAuth("GET", "/account/channels", cc, token) if err != nil { return nil, err } if err := json.Unmarshal(res, cc); err != nil { return nil, err } return cc, nil }
func buildContainer(channelList []models.Channel, q *request.Query) (*models.ChannelContainers, error) { cc := models.NewChannelContainers() if err := cc.Fetch(channelList, q); err != nil { return cc, err } cc.AddIsParticipant(q.AccountId) // TODO this should be in the channel cache by default cc.AddLastMessage(q.AccountId) cc.AddUnreadCount(q.AccountId) return cc, cc.Err() }
func ListGroupChannels(u *url.URL, h http.Header, _ interface{}, c *models.Context) (int, http.Header, interface{}, error) { if !c.IsLoggedIn() { return response.NewBadRequest(models.ErrNotLoggedIn) } cp := models.NewChannelParticipant() cids, err := cp.FetchAllParticipatedChannelIdsInGroup(c.Client.Account.Id, c.GroupName) if err != nil { return response.NewBadRequest(err) } channels, err := models.NewChannel().FetchByIds(cids) if err != nil { return response.NewBadRequest(err) } cc := models.NewChannelContainers() cc.PopulateWith(channels, c.Client.Account.Id) return response.HandleResultAndError(cc, cc.Err()) }