コード例 #1
0
ファイル: main.go プロジェクト: blabber/cmg
func getConferenceData(id string) (*conferenceData, error) {
	c, err := backend.GetConference(id)
	if err != nil {
		return nil, err
	}

	data := &conferenceData{
		baseData: baseData{
			*host,
			*port,
		},
		Slug:     c.Slug,
		Title:    c.Title,
		MainItem: mainItem,
	}

	for _, e := range c.Events {
		id := e.Url[strings.LastIndex(e.Url, "/")+1:]
		selector := fmt.Sprintf("%s%s", eventPrefix, id)
		data.Items = append(data.Items, newMenuItem(typeDir, e.Title, selector))
	}

	if len(data.Items) == 0 {
		return nil, fmt.Errorf(`unknown conference: "%s"`, id)
	}

	sort.Sort(menuItemByTitle(data.Items))

	return data, nil
}
コード例 #2
0
ファイル: main.go プロジェクト: blabber/cmg
func getCategoryData(category string) (*categoryData, error) {
	cs, err := backend.GetConferences()
	if err != nil {
		return nil, err
	}

	ic := make(chan menuItem)

	go func() {
		var wg sync.WaitGroup

		for _, c := range cs {
			cSlug := ensureTrailingSlash(c.Slug)
			categorySlash := ensureTrailingSlash(category)

			if strings.HasPrefix(cSlug, categorySlash) || category == "" {
				trailingString := strings.TrimPrefix(cSlug, categorySlash)
				trailingElements := strings.Split(trailingString, "/")
				nextElement := trailingElements[0]

				var checkSlug string
				if category == "" {
					checkSlug = nextElement
				} else {
					checkSlug = fmt.Sprintf("%s/%s", category, nextElement)
				}

				if c.Slug == checkSlug {
					wg.Add(1)
					go func(c backend.Conference) {
						defer wg.Done()

						id := c.Url[strings.LastIndex(c.Url, "/")+1:]

						bc, err := backend.GetConference(id)
						if err != nil {
							log.Print(err)
						}

						if bc.Events != nil && len(bc.Events) > 0 {
							ic <- newMenuItem(typeDir, c.Title,
								fmt.Sprintf("%s%s", conferencePrefix, id))
						} else {
							log.Printf(`skipping empty conference "%s", %s`,
								c.Title, id)
						}
					}(c)
				} else {
					var selector string
					if category == "" {
						selector = fmt.Sprintf("%s%s", categoryPrefix, nextElement)
					} else {
						selector = fmt.Sprintf("%s%s/%s", categoryPrefix, category,
							nextElement)
					}
					ic <- newMenuItem(typeDir, nextElement, selector)
				}
			}
		}

		wg.Wait()
		close(ic)
	}()

	itemSet := make(map[menuItem]bool)
	for i := range ic {
		itemSet[i] = true
	}

	if len(itemSet) == 0 {
		return nil, fmt.Errorf(`unknown category: "%s"`, category)
	}

	var items []menuItem
	for i := range itemSet {
		items = append(items, i)
	}

	sort.Sort(menuItemByTitle(items))

	data := &categoryData{
		baseData{
			*host,
			*port,
		},
		category,
		items,
	}

	return data, nil
}