2026-02-26 11:23:47 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2026-03-26 10:16:50 +00:00
|
|
|
"log"
|
2026-02-26 11:23:47 +00:00
|
|
|
"net/url"
|
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
|
|
"emperror.dev/errors"
|
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
|
|
|
"github.com/gofiber/fiber/v3/middleware/session"
|
|
|
|
|
"lmika.dev/lmika/weiro/models"
|
|
|
|
|
"lmika.dev/lmika/weiro/services/sites"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var sitePath = regexp.MustCompile(`^/sites/([0-9]+)`)
|
|
|
|
|
|
|
|
|
|
type IndexHandler struct {
|
|
|
|
|
SiteService *sites.Service
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h IndexHandler) Index(c fiber.Ctx) error {
|
|
|
|
|
hasUserAndSites, err := h.SiteService.HasUsersAsSites(c.Context())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else if !hasUserAndSites {
|
|
|
|
|
return c.Redirect().To("/first-run")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user, hasUser := models.GetUser(c.Context())
|
|
|
|
|
if !hasUser {
|
|
|
|
|
return c.Redirect().To("/login")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if refUrl, rerr := url.Parse(c.Get("Referer")); rerr == nil {
|
|
|
|
|
if parts := sitePath.FindStringSubmatch(refUrl.Path); len(parts) == 2 {
|
|
|
|
|
return c.Redirect().To(fmt.Sprintf("/sites/%v/posts", parts[1]))
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-26 10:16:50 +00:00
|
|
|
|
|
|
|
|
sess := session.FromContext(c)
|
|
|
|
|
lastSiteID, ok := sess.Get("last_site_id").(int64)
|
|
|
|
|
log.Printf("last site id: %v", lastSiteID)
|
|
|
|
|
if ok {
|
|
|
|
|
return c.Redirect().To(fmt.Sprintf("/sites/%v/posts", lastSiteID))
|
|
|
|
|
}
|
2026-02-26 11:23:47 +00:00
|
|
|
|
|
|
|
|
site, err := h.SiteService.BestSite(c.Context(), user)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.Redirect().To(fmt.Sprintf("/sites/%v/posts", site.ID))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h IndexHandler) FirstRun(c fiber.Ctx) error {
|
|
|
|
|
hasUserAndSites, err := h.SiteService.HasUsersAsSites(c.Context())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else if hasUserAndSites {
|
|
|
|
|
return errors.New("you already have a site")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.Render("index/first-run", fiber.Map{}, "layouts/bare_with_scripts")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h IndexHandler) FirstRunSubmit(c fiber.Ctx) error {
|
|
|
|
|
var req sites.FirstRunRequest
|
|
|
|
|
if err := c.Bind().Body(&req); err != nil {
|
|
|
|
|
return errors.Wrap(err, "failed to parse first run request")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sess := session.FromContext(c)
|
|
|
|
|
if err := sess.Regenerate(); err != nil {
|
|
|
|
|
return c.Status(fiber.StatusInternalServerError).SendString("Failed to login")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user, site, err := h.SiteService.FirstRun(c.Context(), req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sess.Set("user_id", user.ID)
|
|
|
|
|
return c.Redirect().To(fmt.Sprintf("/sites/%v/posts", site.ID))
|
|
|
|
|
}
|