Example #1
0
func (tw *Tweets) POST(c *kocha.Context) error {
	if _, err := exec.Command(os.Args[0], "check").Output(); err != nil {
		return c.RenderError(500, err, nil)
	}

	return tw.GET(c)
}
Example #2
0
func (us *Users) POST(c *kocha.Context) error {
	updateUsers := c.Request.Form["users[]"]
	if len(updateUsers) > 0 {
		var users []model.User
		dbInst := db.Get("default")
		err := dbInst.Select(&users, dbInst.Where("id").In(updateUsers))
		if err == nil {
			action := c.Params.Get("action")

			var ids []string
			for _, user := range users {
				ids = append(ids, strconv.FormatInt(user.Id, 10))
			}

			_, err = dbInst.DB().Exec(`
				UPDATE users SET enable = ?, updated_at = CURRENT_TIMESTAMP
				WHERE id IN (`+strings.Join(ids, ",")+`)
			`, action == "enable")
		}

		if err != nil {
			return c.RenderError(500, err, nil)
		}
	}

	return us.GET(c)
}
Example #3
0
func (m *AuthMiddleware) Process(app *kocha.Application, c *kocha.Context, next func() error) (err error) {
	if kocha.Getenv("RETWITTER_AUTH", "0") == "1" && !checkAuth(c.Request) {
		c.Response.StatusCode = http.StatusUnauthorized
		c.Response.Header().Set("WWW-Authenticate", `Basic realm="Input ID and Password"`)
		return c.RenderError(http.StatusUnauthorized, nil, nil)
	}
	return next()
}
Example #4
0
func (au *Auth) GET(c *kocha.Context) error {
	client := twitter.GetClient()
	callbackUrl := kocha.Getenv("RETWITTER_TWITTER_CALLBACK", "")
	requestToken, url, err := client.GetRequestTokenAndUrl(callbackUrl)
	if err != nil {
		return c.RenderError(500, err, nil)
	}

	c.Session.Set("requestToken.Token", requestToken.Token)
	c.Session.Set("requestToken.Secret", requestToken.Secret)
	return c.Redirect(url, false)
}
Example #5
0
func (fe *Newsong) GET(c *kocha.Context) error {
	data := c.Data.(map[string]interface{})
	selectedVocaloids := data["SelectedVocaloids"].([]*model.Vocaloid)
	p, err := strconv.Atoi(c.Params.Get("p"))
	if err != nil {
		p = 1
	}
	offset := int(math.Max(itemsPerPage, 0) * math.Max(float64(p-1), 0))
	songs, err := model.Songs.FindByVocaloid(selectedVocaloids, offset, itemsPerPage)
	if err != nil {
		return c.RenderError(500, err, nil)
	}
	if len(selectedVocaloids) == 0 {
		selectedVocaloids = append(selectedVocaloids, &model.Vocaloid{Key: "vocaloid", Name: "VOCALOID"})
	}
	latestUpdate, err := model.Songs.LatestUpdate()
	if err != nil {
		return c.RenderError(500, err, nil)
	}
	feed := &feeds.Feed{
		Title:       "VOCALOID新曲 - ぼかにゅー",
		Link:        &feeds.Link{Href: c.Request.URL.String(), Rel: "alternate"},
		Description: selectedVocaloids[0].Name + "の新曲",
		Author:      &feeds.Author{Name: "ぼかにゅー"},
		Updated:     latestUpdate,
	}
	var buf bytes.Buffer
	for _, v := range songs {
		buf.Reset()
		if err := rssDescription.Execute(&buf, v); err != nil {
			return c.RenderError(500, err, nil)
		}
		feed.Add(&feeds.Item{
			Title:       v.Title,
			Link:        &feeds.Link{Href: "http://www.nicovideo.jp/watch/" + v.VideoId, Rel: "alternate"},
			Description: buf.String(),
			Updated:     v.PostTime,
		})
	}
	var xml string
	if c.Params.Get("rss") == "2.0" {
		c.Response.ContentType = "application/rss+xml"
		xml, err = feed.ToRss()
	} else {
		c.Response.ContentType = "application/atom+xml"
		xml, err = feed.ToAtom()
	}
	if err != nil {
		return c.RenderError(500, err, nil)
	}
	return c.RenderText(xml)
}
Example #6
0
func (au *AuthCallback) GET(c *kocha.Context) error {
	oauthVerifier := c.Params.Get("oauth_verifier")
	requestToken := au.GetRequestToken(c)
	if oauthVerifier != "" && requestToken != nil {
		client := twitter.GetClient()
		accessToken, err := client.AuthorizeToken(requestToken, oauthVerifier)
		if err == nil {
			err = model.RegisterUser(accessToken)
		}

		if err != nil {
			return c.RenderError(500, err, nil)
		}
	}

	return c.Redirect("/users", false)
}
Example #7
0
func (us *Users) GET(c *kocha.Context) error {
	var n int
	var users []model.User

	num, _ := GetPerItem(c)
	currentPage, _ := GetPageNum(c)
	page := currentPage
	if page > 0 {
		page--
	}

	dbInst := db.Get("default")
	condition := dbInst.OrderBy("id", genmai.DESC).Limit(num).Offset(page * num)
	err := dbInst.Select(&users, condition)
	if err == nil {
		if page > 0 && len(users) == 0 {
			c.Redirect("/users", false)
		}
		err = dbInst.Select(&n, dbInst.Count(), dbInst.From(&model.User{}))
	}

	if err != nil {
		c.RenderError(500, err, nil)
	}

	pagination, _ := simpagin.New(currentPage, n, num, DEFAULT_PER_PAGE)
	return c.Render(map[string]interface{}{
		"ControllerName": "Users",
		"users":          users,
		"totalPage":      int(math.Ceil(float64(n) / float64(num))),
		"pagination":     pagination,
	})
}
Example #8
0
func (qu *Queues) POST(c *kocha.Context) error {
	status := c.Params.Get("action")
	if status == "exec" {
		if _, err := exec.Command(os.Args[0], "exec").Output(); err != nil {
			return c.RenderError(500, err, nil)
		}

		return qu.GET(c)
	}

	if status != "ready" && status != "canceled" {
		return qu.GET(c)
	}

	updateQueues := c.Request.Form["queues[]"]
	if len(updateQueues) > 0 {
		var queues []model.Queue
		dbInst := db.Get("default")
		err := dbInst.Select(&queues, dbInst.Where("id").In(updateQueues))
		if err == nil {
			var ids []string
			for _, queue := range queues {
				ids = append(ids, strconv.FormatInt(queue.Id, 10))
			}

			_, err = dbInst.DB().Exec(`
				UPDATE queues SET status = ?, updated_at = CURRENT_TIMESTAMP
				WHERE id IN (`+strings.Join(ids, ",")+`)
			`, status)
		}

		if err != nil {
			return c.RenderError(500, err, nil)
		}
	}

	return qu.GET(c)
}
Example #9
0
func (qu *Queues) GET(c *kocha.Context) error {
	var n int
	var queues []Queue

	num, _ := GetPerItem(c)
	currentPage, _ := GetPageNum(c)
	page := currentPage
	if page > 0 {
		page--
	}

	dbInst := db.Get("default")
	rows, err := dbInst.DB().Query(`
		SELECT queues.id, status, screen_name, tweet_id,
		action, execute_time, fail_count, queues.created_at, queues.updated_at
		FROM queues JOIN users ON user_id = users.id
		ORDER BY id DESC LIMIT ? OFFSET ?
	`, num, page*num)

	if err == nil {
		defer rows.Close()
		for rows.Next() {
			var queue Queue
			if err := rows.Scan(
				&queue.Id,
				&queue.Status,
				&queue.ScreenName,
				&queue.TweetId,
				&queue.Action,
				&queue.ExecuteTime,
				&queue.FailCount,
				&queue.CreatedAt,
				&queue.UpdatedAt,
			); err != nil {
				panic(err)
			}
			queues = append(queues, queue)
		}

		if page > 0 && len(queues) == 0 {
			c.Redirect("/queues", false)
		}
		err = dbInst.Select(&n, dbInst.Count(), dbInst.From(&model.Queue{}))
	}

	if err != nil {
		c.RenderError(500, err, nil)
	}

	pagination, _ := simpagin.New(currentPage, n, num, DEFAULT_PER_PAGE)
	return c.Render(map[string]interface{}{
		"ControllerName": "Queues",
		"queues":         queues,
		"totalPage":      int(math.Ceil(float64(n) / float64(num))),
		"pagination":     pagination,
	})
}
Example #10
0
func (m *Vocaloid) Process(app *kocha.Application, c *kocha.Context, next func() error) error {
	vocaloids, err := model.AllVocaloids()
	if err != nil {
		return err
	}
	selectedVocaloids := m.selectedVocaloids(c, vocaloids)
	keys := make([]string, len(selectedVocaloids))
	for i, v := range selectedVocaloids {
		keys[i] = v.Key
	}
	c.Data = map[string]interface{}{
		"Vocaloids":         vocaloids,
		"SelectedVocaloids": selectedVocaloids,
		"SelectedKeys":      keys,
	}
	return next()
}
Example #11
0
func (co *Contact) POST(c *kocha.Context) error {
	c.Response.ContentType = "application/json"
	if !c.Request.IsXHR() {
		return c.RenderError(404, nil, nil)
	}
	text := c.Params.Get("text")
	if text == "" {
		return c.RenderError(400, nil, map[string]interface{}{
			"success": 0,
			"errors":  []string{"内容が入力されていません。"},
		})
	}
	c.App.Event.Trigger("vocanew:send_contact_mail", text)
	return c.RenderJSON(map[string]interface{}{
		"success": 1,
	})
}
Example #12
0
func (r *Root) GET(c *kocha.Context) error {
	data := c.Data.(map[string]interface{})
	selectedVocaloids := data["SelectedVocaloids"].([]*model.Vocaloid)
	count, err := model.Songs.Count(selectedVocaloids...)
	if err != nil {
		return c.RenderError(500, err, nil)
	}
	p, err := strconv.Atoi(c.Params.Get("p"))
	if err != nil {
		p = 1
	}
	offset := int(math.Max(itemsPerPage, 0) * math.Max(float64(p-1), 0))
	songs, err := model.Songs.FindByVocaloid(selectedVocaloids, offset, itemsPerPage)
	if err != nil {
		return c.RenderError(500, err, nil)
	}
	if len(selectedVocaloids) == 0 {
		selectedVocaloids = append(selectedVocaloids, &model.Vocaloid{Key: "vocaloid", Name: "VOCALOID"})
		data["SelectedVocaloids"] = selectedVocaloids
	}
	paginationLength := int(count / itemsPerPage)
	if count%itemsPerPage > 0 {
		paginationLength++
	}
	plength := paginationLength
	if plength < 1 {
		plength = 1
	}
	if plength > maxPaginationLength {
		plength = maxPaginationLength
	}
	paginator := pagination.NewPaginator(count, itemsPerPage, plength, paginationThreshold)
	page := paginator.Page(p)
	pages := paginator.Pages(p)
	return c.Render(map[string]interface{}{
		"Songs":            songs,
		"SelectedVocaloid": selectedVocaloids[0],
		"Pages":            pages,
		"Page":             page,
	})
}
Example #13
0
func (r *Root) GET(c *kocha.Context) error {
	return c.Render(map[string]interface{}{
		"ControllerName": "Root",
	})
}
Example #14
0
func (r *Root) GET(c *kocha.Context) error {
	return c.Redirect("/users", false)
}
Example #15
0
func (ab *About) GET(c *kocha.Context) error {
	return c.Render(nil)
}
Example #16
0
File: root.go Project: naoina/kocha
func (ro *Root) GET(c *kocha.Context) error {
	return c.Render(nil)
}