Initial users support

This commit is contained in:
video-prize-ranch
2022-01-27 20:41:10 -05:00
parent fdd66853f9
commit 8d21d1a576
15 changed files with 344 additions and 66 deletions

View File

@@ -1,16 +1,14 @@
package pages
import "github.com/gofiber/fiber/v2"
import (
"codeberg.org/video-prize-ranch/rimgo/utils"
"github.com/gofiber/fiber/v2"
)
func FrontpageHandler(c *fiber.Ctx) error {
c.Set("Cache-Control", "public,max-age=1800")
c.Set("X-Frame-Options", "DENY")
c.Set("Referrer-Policy", "no-referrer")
c.Set("X-Content-Type-Options", "nosniff")
c.Set("X-Robots-Tag", "noindex, noimageindex, nofollow")
c.Set("Strict-Transport-Security", "max-age=31557600")
c.Set("Permissions-Policy", "accelerometer=(), ambient-light-sensor=(), autoplay=(), battery=(), camera=(), cross-origin-isolated=(), display-capture=(), document-domain=(), encrypted-media=(), execution-while-not-rendered=(), execution-while-out-of-viewport=(), fullscreen=(), geolocation=(), gyroscope=(), interest-cohort=(), magnetometer=(), microphone=(), midi=(), navigation-override=(), payment=(), picture-in-picture=(), publickey-credentials-get=(), screen-wake-lock=(), sync-xhr=(), usb=(), web-share=(), xr-spatial-tracking=()")
c.Set("Content-Security-Policy", "default-src 'none'; style-src 'self'; script-src 'none'; img-src 'self'; font-src 'self'; block-all-mixed-content; manifest-src 'self'")
utils.SetHeaders(c)
c.Set("Cache-Control", "public,max-age=31557600")
c.Set("Content-Security-Policy", "default-src 'none'; style-src 'self'; img-src 'self'; font-src 'self'; block-all-mixed-content")
return c.Render("frontpage", fiber.Map{})
}

View File

@@ -3,18 +3,13 @@ package pages
import (
"codeberg.org/video-prize-ranch/rimgo/api"
"codeberg.org/video-prize-ranch/rimgo/types"
"codeberg.org/video-prize-ranch/rimgo/utils"
"github.com/gofiber/fiber/v2"
)
func HandleGallery(c *fiber.Ctx) error {
c.Set("Cache-Control", "public,max-age=604800")
c.Set("X-Frame-Options", "DENY")
c.Set("Referrer-Policy", "no-referrer")
c.Set("X-Content-Type-Options", "nosniff")
c.Set("X-Robots-Tag", "noindex, noimageindex, nofollow")
c.Set("Strict-Transport-Security", "max-age=31557600")
c.Set("Permissions-Policy", "accelerometer=(), ambient-light-sensor=(), autoplay=(), battery=(), camera=(), cross-origin-isolated=(), display-capture=(), document-domain=(), encrypted-media=(), execution-while-not-rendered=(), execution-while-out-of-viewport=(), fullscreen=(), geolocation=(), gyroscope=(), interest-cohort=(), magnetometer=(), microphone=(), midi=(), navigation-override=(), payment=(), picture-in-picture=(), publickey-credentials-get=(), screen-wake-lock=(), sync-xhr=(), usb=(), web-share=(), xr-spatial-tracking=()")
c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'self'; script-src 'none'; img-src 'self'; font-src 'self'; block-all-mixed-content; manifest-src 'self'")
utils.SetHeaders(c)
c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'self'; img-src 'self'; font-src 'self'; block-all-mixed-content")
album, err := api.FetchAlbum(c.Params("galleryID"))
if err != nil {
@@ -23,10 +18,13 @@ func HandleGallery(c *fiber.Ctx) error {
comments := []types.Comment{}
if album.SharedWithCommunity {
c.Set("Cache-Control", "public,max-age=604800")
comments, err = api.FetchComments(c.Params("galleryID"))
if err != nil {
return err
}
} else {
c.Set("Cache-Control", "public,max-age=31557600")
}
return c.Render("gallery", fiber.Map{

View File

@@ -1,27 +1,3 @@
export const handleUser = async (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
// https://imgur.com/user/MomBotNumber5
if (!CONFIG.use_api) {
return 'User page disabled. Rimgu administrator needs to enable API for this to work.';
}
const userID = request.params.userID;
const user = await fetchUserInfo(userID);
const posts = await fetchUserPosts(userID);
return h.view('posts', {
posts,
user,
pageTitle: CONFIG.page_title,
util,
});
};
export const handleUserCover = async (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
const userID = request.params.userID;
const result = await fetchMedia(`/user/${userID}/cover?maxwidth=2560`);
const response = h.response(result.rawBody)
.header('Content-Type', result.headers["content-type"] || `image/jpeg`);
return response;
};
export const handleTag = async (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
// https://imgur.com/t/funny
if (!CONFIG.use_api) {

View File

@@ -4,17 +4,35 @@ import (
"net/http"
"strconv"
"codeberg.org/video-prize-ranch/rimgo/utils"
"github.com/gofiber/fiber/v2"
)
func HandleMedia(c *fiber.Ctx) error {
res, err := http.Get("https://i.imgur.com/" + c.Params("baseName") + "." + c.Params("extension"))
c.Set("Cache-Control", "public,max-age=31557600")
return handleMedia(c, "https://i.imgur.com/" + c.Params("baseName") + "." + c.Params("extension"))
}
func HandleUserCover(c *fiber.Ctx) error {
c.Set("Cache-Control", "public,max-age=604800")
return handleMedia(c, "https://imgur.com/user/" + c.Params("userID") + "/cover?maxwidth=2560")
};
func HandleUserAvatar(c *fiber.Ctx) error {
c.Set("Cache-Control", "public,max-age=604800")
return handleMedia(c, "https://imgur.com/user/" + c.Params("userID") + "/avatar")
};
func handleMedia(c *fiber.Ctx, url string) error {
utils.SetHeaders(c)
c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'self'; img-src 'self'; font-src 'self'; block-all-mixed-content")
res, err := http.Get(url)
if err != nil {
return err
}
c.Set("Content-Type", res.Header.Get("Content-Type"));
contentLen, _ := strconv.Atoi(res.Header.Get("Content-Length"))
c.SendStream(res.Body, contentLen)
return nil
return c.SendStream(res.Body, contentLen)
}

42
pages/user.go Normal file
View File

@@ -0,0 +1,42 @@
package pages
import (
"sync"
"codeberg.org/video-prize-ranch/rimgo/api"
"codeberg.org/video-prize-ranch/rimgo/types"
"codeberg.org/video-prize-ranch/rimgo/utils"
"github.com/gofiber/fiber/v2"
)
func HandleUser(c *fiber.Ctx) error {
utils.SetHeaders(c)
c.Set("Cache-Control", "public,max-age=604800")
c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'unsafe-inline' 'self'; img-src 'self'; font-src 'self'; block-all-mixed-content")
wg := sync.WaitGroup{}
wg.Add(2)
user, err := types.User{}, error(nil)
go func() {
defer wg.Done()
user, err = api.FetchUser(c.Params("userID"))
}()
if err != nil {
return err
}
submissions, err := []types.Submission{}, error(nil)
go func() {
defer wg.Done()
submissions, err = api.FetchSubmissions(c.Params("userID"), "newest", "0")
}()
if err != nil {
return err
}
wg.Wait()
return c.Render("user", fiber.Map{
"user": user,
"submissions": submissions,
})
}