2021-10-03 11:48:59 +03:00
|
|
|
package controllers
|
2021-08-26 20:23:55 +03:00
|
|
|
|
|
|
|
import (
|
2022-04-07 10:02:26 +03:00
|
|
|
"context"
|
2021-09-05 00:36:55 +03:00
|
|
|
"log"
|
2021-09-10 02:17:45 +03:00
|
|
|
"math"
|
2022-04-07 10:02:26 +03:00
|
|
|
"path/filepath"
|
2021-09-05 00:36:55 +03:00
|
|
|
"strconv"
|
2022-04-07 10:02:26 +03:00
|
|
|
"strings"
|
2021-08-26 20:23:55 +03:00
|
|
|
|
2021-10-03 11:48:59 +03:00
|
|
|
"github.com/dchest/captcha"
|
2022-04-06 11:45:39 +03:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2021-08-28 18:41:34 +03:00
|
|
|
|
2022-04-06 11:45:39 +03:00
|
|
|
"micrach/config"
|
2022-04-07 10:02:26 +03:00
|
|
|
"micrach/db"
|
2022-04-06 11:45:39 +03:00
|
|
|
"micrach/repositories"
|
2022-04-07 10:02:26 +03:00
|
|
|
"micrach/utils"
|
2021-08-26 20:23:55 +03:00
|
|
|
)
|
|
|
|
|
2022-04-06 11:45:39 +03:00
|
|
|
func GetThreads(c *fiber.Ctx) error {
|
|
|
|
pageString := c.Query("page", "1")
|
2021-09-10 02:17:45 +03:00
|
|
|
page, err := strconv.Atoi(pageString)
|
2021-08-30 12:09:27 +03:00
|
|
|
if err != nil {
|
2022-04-06 11:45:39 +03:00
|
|
|
return c.Status(fiber.StatusNotFound).Render("pages/404", nil)
|
2021-09-10 17:09:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if page <= 0 {
|
2022-04-06 11:45:39 +03:00
|
|
|
return c.Status(fiber.StatusNotFound).Render("pages/404", nil)
|
2021-08-30 12:09:27 +03:00
|
|
|
}
|
2021-09-10 17:09:15 +03:00
|
|
|
|
2021-09-10 02:17:45 +03:00
|
|
|
limit := 10
|
|
|
|
offset := limit * (page - 1)
|
2022-04-06 11:45:39 +03:00
|
|
|
threads, err := repositories.Posts.Get(limit, offset)
|
2021-09-10 02:17:45 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-06 11:45:39 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-09-10 02:17:45 +03:00
|
|
|
}
|
2022-04-06 11:45:39 +03:00
|
|
|
count, err := repositories.Posts.GetThreadsCount()
|
2021-09-10 02:17:45 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-06 11:45:39 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-09-10 02:17:45 +03:00
|
|
|
}
|
|
|
|
|
2021-09-10 17:09:15 +03:00
|
|
|
pagesCount := int(math.Ceil(float64(count) / 10))
|
2021-09-11 01:20:56 +03:00
|
|
|
if page > pagesCount && count != 0 {
|
2022-04-06 11:45:39 +03:00
|
|
|
return c.Status(fiber.StatusNotFound).Render("pages/404", nil)
|
2021-09-10 17:09:15 +03:00
|
|
|
}
|
2021-09-30 20:44:14 +03:00
|
|
|
|
2021-10-03 11:48:59 +03:00
|
|
|
captchaID := captcha.New()
|
2022-04-06 11:45:39 +03:00
|
|
|
htmlData := repositories.GetThreadsHtmlData{
|
2022-01-31 19:56:21 +03:00
|
|
|
Threads: threads,
|
2022-04-06 11:45:39 +03:00
|
|
|
Pagination: repositories.HtmlPaginationData{
|
2022-01-31 19:56:21 +03:00
|
|
|
PagesCount: pagesCount,
|
|
|
|
Page: page,
|
|
|
|
},
|
2022-04-06 11:45:39 +03:00
|
|
|
FormData: repositories.HtmlFormData{
|
2022-01-27 20:23:51 +03:00
|
|
|
CaptchaID: captchaID,
|
2022-04-06 11:45:39 +03:00
|
|
|
IsCaptchaActive: config.App.IsCaptchaActive,
|
2021-10-04 11:40:53 +03:00
|
|
|
},
|
|
|
|
}
|
2022-04-07 09:44:10 +03:00
|
|
|
return c.Render("pages/index", htmlData)
|
2021-08-26 20:23:55 +03:00
|
|
|
}
|
|
|
|
|
2022-04-07 09:44:10 +03:00
|
|
|
func GetThread(c *fiber.Ctx) error {
|
|
|
|
threadID, err := c.ParamsInt("threadID")
|
|
|
|
if err != nil {
|
|
|
|
return c.Status(fiber.StatusNotFound).Render("pages/404", nil)
|
|
|
|
}
|
|
|
|
thread, err := repositories.Posts.GetThreadByPostID(threadID)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
}
|
|
|
|
if thread == nil {
|
|
|
|
return c.Status(fiber.StatusNotFound).Render("pages/404", nil)
|
|
|
|
}
|
2022-04-06 11:45:39 +03:00
|
|
|
|
2022-04-07 09:44:10 +03:00
|
|
|
firstPost := thread[0]
|
|
|
|
captchaID := captcha.New()
|
|
|
|
htmlData := repositories.GetThreadHtmlData{
|
|
|
|
Thread: thread,
|
|
|
|
FormData: repositories.HtmlFormData{
|
|
|
|
FirstPostID: firstPost.ID,
|
|
|
|
CaptchaID: captchaID,
|
|
|
|
IsCaptchaActive: config.App.IsCaptchaActive,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return c.Render("pages/thread", htmlData)
|
|
|
|
}
|
2022-04-06 11:45:39 +03:00
|
|
|
|
2022-04-07 10:02:26 +03:00
|
|
|
func CreateThread(c *fiber.Ctx) error {
|
|
|
|
form, err := c.MultipartForm()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
}
|
2022-04-06 11:45:39 +03:00
|
|
|
|
2022-04-07 10:02:26 +03:00
|
|
|
// TODO: dat shit crashes if no fields in request
|
|
|
|
text := form.Value["text"][0]
|
|
|
|
title := form.Value["title"][0]
|
|
|
|
filesInRequest := form.File["files"]
|
|
|
|
validationErrorMessage := utils.ValidatePost(title, text, filesInRequest)
|
|
|
|
if validationErrorMessage != "" {
|
|
|
|
errorHtmlData := repositories.BadRequestHtmlData{
|
|
|
|
Message: validationErrorMessage,
|
|
|
|
}
|
|
|
|
return c.Status(fiber.StatusBadRequest).Render("pages/400", errorHtmlData)
|
|
|
|
}
|
2022-04-06 11:45:39 +03:00
|
|
|
|
2022-04-07 10:02:26 +03:00
|
|
|
if config.App.IsCaptchaActive {
|
|
|
|
captchaID := form.Value["captchaId"][0]
|
|
|
|
captchaString := form.Value["captcha"][0]
|
|
|
|
isCaptchaValid := captcha.VerifyString(captchaID, captchaString)
|
|
|
|
if !isCaptchaValid {
|
|
|
|
errorHtmlData := repositories.BadRequestHtmlData{
|
|
|
|
Message: repositories.InvalidCaptchaErrorMessage,
|
|
|
|
}
|
|
|
|
return c.Status(fiber.StatusBadRequest).Render("pages/400", errorHtmlData)
|
|
|
|
}
|
|
|
|
}
|
2022-04-06 11:45:39 +03:00
|
|
|
|
2022-04-07 10:02:26 +03:00
|
|
|
conn, err := db.Pool.Acquire(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
2022-04-06 11:45:39 +03:00
|
|
|
|
2022-04-07 10:02:26 +03:00
|
|
|
threadsCount, err := repositories.Posts.GetThreadsCount()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
}
|
2022-04-06 11:45:39 +03:00
|
|
|
|
2022-04-07 10:02:26 +03:00
|
|
|
if threadsCount >= config.App.ThreadsMaxCount {
|
|
|
|
oldestThreadUpdatedAt, err := repositories.Posts.GetOldestThreadUpdatedAt()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
}
|
|
|
|
err = repositories.Posts.ArchiveThreadsFrom(oldestThreadUpdatedAt)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
}
|
|
|
|
}
|
2022-04-06 11:45:39 +03:00
|
|
|
|
2022-04-07 10:02:26 +03:00
|
|
|
tx, err := conn.Begin(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
}
|
|
|
|
defer tx.Rollback(context.TODO())
|
2022-04-06 11:45:39 +03:00
|
|
|
|
2022-04-07 10:02:26 +03:00
|
|
|
post := repositories.Post{
|
|
|
|
IsParent: true,
|
|
|
|
Title: title,
|
|
|
|
Text: text,
|
|
|
|
IsSage: false,
|
|
|
|
}
|
|
|
|
threadID, err := repositories.Posts.CreateInTx(tx, post)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
}
|
2022-04-06 11:45:39 +03:00
|
|
|
|
2022-04-07 10:02:26 +03:00
|
|
|
err = utils.CreateThreadFolder(threadID)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
}
|
2022-04-06 11:45:39 +03:00
|
|
|
|
2022-04-07 10:02:26 +03:00
|
|
|
for _, fileInRequest := range filesInRequest {
|
|
|
|
file := repositories.File{
|
|
|
|
PostID: threadID,
|
|
|
|
Name: fileInRequest.Filename,
|
|
|
|
// image/jpeg -> jpeg
|
|
|
|
Ext: strings.Split(fileInRequest.Header["Content-Type"][0], "/")[1],
|
|
|
|
Size: int(fileInRequest.Size),
|
|
|
|
}
|
|
|
|
|
|
|
|
fileID, err := repositories.Files.CreateInTx(tx, file)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(
|
|
|
|
utils.UPLOADS_DIR_PATH,
|
|
|
|
strconv.Itoa(threadID),
|
|
|
|
"o",
|
|
|
|
strconv.Itoa(fileID)+"."+file.Ext,
|
|
|
|
)
|
|
|
|
err = c.SaveFile(fileInRequest, path)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
}
|
|
|
|
// creating thumbnail
|
|
|
|
thumbImg, err := utils.MakeImageThumbnail(path, file.Ext, threadID, fileID)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
}
|
|
|
|
// saving thumbnail
|
|
|
|
err = utils.SaveImageThumbnail(thumbImg, threadID, fileID, file.Ext)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
}
|
|
|
|
}
|
2022-04-06 11:45:39 +03:00
|
|
|
|
2022-04-07 10:02:26 +03:00
|
|
|
tx.Commit(context.TODO())
|
2022-04-06 11:45:39 +03:00
|
|
|
|
2022-04-07 10:02:26 +03:00
|
|
|
return c.Redirect("/"+strconv.Itoa(threadID), fiber.StatusFound)
|
|
|
|
}
|
2022-04-06 11:45:39 +03:00
|
|
|
|
|
|
|
// // Add new post in thread
|
|
|
|
// func UpdateThread(c *gin.Context) {
|
|
|
|
// threadIDString := c.Param("threadID")
|
|
|
|
// threadID, err := strconv.Atoi(threadIDString)
|
|
|
|
// if err != nil {
|
|
|
|
// c.HTML(http.StatusNotFound, "500.html", nil)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
// isArchived, err := Repositories.Posts.GetIfThreadIsArchived(threadID)
|
|
|
|
// if isArchived {
|
|
|
|
// errorHtmlData := Repositories.BadRequestHtmlData{
|
|
|
|
// Message: Repositories.ThreadIsArchivedErrorMessage,
|
|
|
|
// }
|
|
|
|
// c.HTML(http.StatusBadRequest, "400.html", errorHtmlData)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// if err != nil {
|
|
|
|
// log.Println("error:", err)
|
|
|
|
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
// form, err := c.MultipartForm()
|
|
|
|
// if err != nil {
|
|
|
|
// log.Println("error:", err)
|
|
|
|
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // TODO: dat shit crashes if no fields in request
|
|
|
|
// text := form.Value["text"][0]
|
|
|
|
// filesInRequest := form.File["files"]
|
|
|
|
// validationErrorMessage := Utils.ValidatePost("", text, filesInRequest)
|
|
|
|
// if validationErrorMessage != "" {
|
|
|
|
// errorHtmlData := Repositories.BadRequestHtmlData{
|
|
|
|
// Message: validationErrorMessage,
|
|
|
|
// }
|
|
|
|
// c.HTML(http.StatusBadRequest, "400.html", errorHtmlData)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if Config.App.IsCaptchaActive {
|
|
|
|
// captchaID := form.Value["captchaId"][0]
|
|
|
|
// captchaString := form.Value["captcha"][0]
|
|
|
|
// isCaptchaValid := captcha.VerifyString(captchaID, captchaString)
|
|
|
|
// if !isCaptchaValid {
|
|
|
|
// errorHtmlData := Repositories.BadRequestHtmlData{
|
|
|
|
// Message: Repositories.InvalidCaptchaErrorMessage,
|
|
|
|
// }
|
|
|
|
// c.HTML(http.StatusBadRequest, "400.html", errorHtmlData)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// isSageField := form.Value["sage"]
|
|
|
|
// var isSageString string
|
|
|
|
// if len(isSageField) != 0 {
|
|
|
|
// isSageString = isSageField[0]
|
|
|
|
// }
|
|
|
|
// isSage := isSageString == "on"
|
|
|
|
|
|
|
|
// conn, err := Db.Pool.Acquire(context.TODO())
|
|
|
|
// if err != nil {
|
|
|
|
// log.Println("error:", err)
|
|
|
|
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// defer conn.Release()
|
|
|
|
|
|
|
|
// tx, err := conn.Begin(context.TODO())
|
|
|
|
// if err != nil {
|
|
|
|
// log.Println("error:", err)
|
|
|
|
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// defer tx.Rollback(context.TODO())
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
// log.Println("error:", err)
|
|
|
|
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// post := Repositories.Post{
|
|
|
|
// IsParent: false,
|
|
|
|
// ParentID: &threadID,
|
|
|
|
// Title: "",
|
|
|
|
// Text: text,
|
|
|
|
// IsSage: isSage,
|
|
|
|
// }
|
|
|
|
// postID, err := Repositories.Posts.CreateInTx(tx, post)
|
|
|
|
// if err != nil {
|
|
|
|
// log.Println("error:", err)
|
|
|
|
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
// postsCountInThread, err := Repositories.Posts.GetThreadPostsCount(threadID)
|
|
|
|
// if err != nil {
|
|
|
|
// log.Println("error:", err)
|
|
|
|
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// isBumpLimit := postsCountInThread >= Config.App.ThreadBumpLimit
|
|
|
|
// isThreadBumped := !isBumpLimit && !isSage && !post.IsParent
|
|
|
|
// if isThreadBumped {
|
|
|
|
// err = Repositories.Posts.BumpThreadInTx(tx, threadID)
|
|
|
|
// if err != nil {
|
|
|
|
// log.Println("error:", err)
|
|
|
|
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// for _, fileInRequest := range filesInRequest {
|
|
|
|
// file := Repositories.File{
|
|
|
|
// PostID: postID,
|
|
|
|
// Name: fileInRequest.Filename,
|
|
|
|
// // image/jpeg -> jpeg
|
|
|
|
// Ext: strings.Split(fileInRequest.Header["Content-Type"][0], "/")[1],
|
|
|
|
// Size: int(fileInRequest.Size),
|
|
|
|
// }
|
|
|
|
|
|
|
|
// fileID, err := Repositories.Files.CreateInTx(tx, file)
|
|
|
|
// if err != nil {
|
|
|
|
// log.Println("error:", err)
|
|
|
|
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
// path := filepath.Join(
|
|
|
|
// Utils.UPLOADS_DIR_PATH,
|
|
|
|
// strconv.Itoa(threadID),
|
|
|
|
// "o",
|
|
|
|
// strconv.Itoa(fileID)+"."+file.Ext,
|
|
|
|
// )
|
|
|
|
// err = c.SaveUploadedFile(fileInRequest, path)
|
|
|
|
// if err != nil {
|
|
|
|
// log.Println("error:", err)
|
|
|
|
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// // creating thumbnail
|
|
|
|
// thumbImg, err := Utils.MakeImageThumbnail(path, file.Ext, threadID, fileID)
|
|
|
|
// if err != nil {
|
|
|
|
// log.Println("error:", err)
|
|
|
|
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// // saving thumbnail
|
|
|
|
// err = Utils.SaveImageThumbnail(thumbImg, threadID, fileID, file.Ext)
|
|
|
|
// if err != nil {
|
|
|
|
// log.Println("error:", err)
|
|
|
|
// c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// tx.Commit(context.TODO())
|
|
|
|
|
|
|
|
// c.Header("Refresh", "0")
|
|
|
|
// }
|