2021-11-24 06:30:19 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2021-12-14 06:41:05 +00:00
|
|
|
"encoding/xml"
|
2021-11-28 14:31:51 +00:00
|
|
|
"fmt"
|
2021-11-25 05:51:40 +00:00
|
|
|
"github.com/fogleman/gg"
|
|
|
|
|
"github.com/golang/freetype"
|
|
|
|
|
"github.com/golang/freetype/truetype"
|
2021-11-24 06:30:19 +00:00
|
|
|
"github.com/gorilla/mux"
|
2021-11-25 05:51:40 +00:00
|
|
|
"golang.org/x/image/font"
|
|
|
|
|
"golang.org/x/image/font/gofont/goregular"
|
|
|
|
|
"golang.org/x/image/math/fixed"
|
2021-11-26 22:36:48 +00:00
|
|
|
"html/template"
|
2021-11-25 05:51:40 +00:00
|
|
|
"image"
|
|
|
|
|
"image/color"
|
|
|
|
|
"image/draw"
|
2021-12-06 06:14:20 +00:00
|
|
|
"status/model"
|
2021-11-26 22:36:48 +00:00
|
|
|
"strconv"
|
2021-11-28 14:31:51 +00:00
|
|
|
"time"
|
2021-11-26 22:36:48 +00:00
|
|
|
|
2021-11-25 05:51:40 +00:00
|
|
|
//"image/png"
|
2021-11-24 06:30:19 +00:00
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
|
2021-11-28 07:40:11 +00:00
|
|
|
func (h *Handler) showManageView(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
logged, err := h.sess.Get(r)
|
|
|
|
|
if err != nil {
|
2021-12-10 17:56:41 +00:00
|
|
|
unauthorized(w, r)
|
2021-11-28 07:40:11 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var page int64 = 0
|
|
|
|
|
if val, ok := r.URL.Query()["page"]; ok && len(val[0]) == 1 {
|
|
|
|
|
page, _ = strconv.ParseInt(val[0], 10, 64)
|
|
|
|
|
}
|
|
|
|
|
statuses, showMore, err := h.storage.StatusByUsername(logged, 20, page)
|
|
|
|
|
if err != nil {
|
|
|
|
|
serverError(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
session, err := h.sess.Store.Get(r, "status")
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
flash := ""
|
|
|
|
|
if flashes := session.Flashes(); len(flashes) > 0 {
|
|
|
|
|
flash = flashes[0].(string)
|
|
|
|
|
}
|
|
|
|
|
session.Save(r, w)
|
|
|
|
|
h.renderLayout(w, "manage", map[string]interface{}{
|
|
|
|
|
"statuses": statuses,
|
|
|
|
|
"showMore": showMore,
|
|
|
|
|
"page": page,
|
|
|
|
|
"flash": flash,
|
|
|
|
|
"next_page": page + 1,
|
|
|
|
|
"prev_page": page - 1,
|
|
|
|
|
}, logged)
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 06:30:19 +00:00
|
|
|
func (h *Handler) showUserView(w http.ResponseWriter, r *http.Request) {
|
2021-11-26 22:43:14 +00:00
|
|
|
logged, _ := h.sess.Get(r)
|
2021-11-26 22:36:48 +00:00
|
|
|
var page int64 = 0
|
|
|
|
|
if val, ok := r.URL.Query()["page"]; ok && len(val[0]) == 1 {
|
|
|
|
|
page, _ = strconv.ParseInt(val[0], 10, 64)
|
|
|
|
|
}
|
2021-11-25 06:37:30 +00:00
|
|
|
username := mux.Vars(r)["user"]
|
|
|
|
|
user, err := h.storage.UserByName(username)
|
|
|
|
|
if err != nil {
|
2021-11-24 06:30:19 +00:00
|
|
|
notFound(w)
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-11-26 22:36:48 +00:00
|
|
|
statuses, showMore, err := h.storage.StatusByUsername(mux.Vars(r)["user"], 20, page)
|
2021-11-24 06:30:19 +00:00
|
|
|
if err != nil {
|
|
|
|
|
serverError(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-12-14 06:41:05 +00:00
|
|
|
face := ""
|
|
|
|
|
if len(statuses) > 0 {
|
|
|
|
|
face = statuses[0].Face
|
|
|
|
|
}
|
2021-11-24 06:30:19 +00:00
|
|
|
h.renderLayout(w, "user", map[string]interface{}{
|
2021-11-26 22:36:48 +00:00
|
|
|
"user": username,
|
|
|
|
|
"statuses": statuses,
|
2021-12-14 06:41:05 +00:00
|
|
|
"face": face,
|
2021-11-26 22:36:48 +00:00
|
|
|
"homepage": user.Homepage,
|
|
|
|
|
"about": template.HTML(user.About),
|
|
|
|
|
"picture": user.Picture,
|
2021-11-28 14:31:51 +00:00
|
|
|
"email": user.Email,
|
2021-11-26 22:36:48 +00:00
|
|
|
"showMore": showMore,
|
|
|
|
|
"page": page,
|
|
|
|
|
"next_page": page + 1,
|
|
|
|
|
"prev_page": page - 1,
|
2021-11-26 22:43:14 +00:00
|
|
|
}, logged)
|
2021-11-24 06:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type statusjson struct {
|
|
|
|
|
Author string `json:"author"`
|
|
|
|
|
Content string `json:"content"`
|
2021-12-04 15:22:30 +00:00
|
|
|
Face string `json:"face"`
|
2021-11-25 05:51:40 +00:00
|
|
|
TimeAgo string `json:"timeAgo"`
|
2021-11-24 06:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
2021-12-06 06:14:20 +00:00
|
|
|
func (h *Handler) showUserStatusJSONView(w http.ResponseWriter, r *http.Request) {
|
2021-11-24 06:30:19 +00:00
|
|
|
user := mux.Vars(r)["user"]
|
|
|
|
|
if !h.storage.UserExists(user) {
|
|
|
|
|
notFound(w)
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-11-26 22:36:48 +00:00
|
|
|
statuses, _, err := h.storage.StatusByUsername(mux.Vars(r)["user"], 1, 0)
|
2021-11-24 06:30:19 +00:00
|
|
|
if err != nil {
|
|
|
|
|
serverError(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
var res statusjson
|
|
|
|
|
if len(statuses) > 0 {
|
|
|
|
|
res.Author = statuses[0].User
|
|
|
|
|
res.Content = statuses[0].Content
|
2021-12-04 15:22:30 +00:00
|
|
|
res.Face = statuses[0].Face
|
2021-11-24 06:30:19 +00:00
|
|
|
res.TimeAgo = statuses[0].TimeAgo()
|
|
|
|
|
}
|
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
|
}
|
2021-11-25 05:51:40 +00:00
|
|
|
|
2021-12-06 06:14:20 +00:00
|
|
|
func (h *Handler) showUserStatusView(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
statuses, _, err := h.storage.StatusByUsername(mux.Vars(r)["user"], 1, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
serverError(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var status model.Status
|
|
|
|
|
if len(statuses) > 0 {
|
|
|
|
|
status = statuses[0]
|
|
|
|
|
}
|
|
|
|
|
h.view("status").Execute(w, map[string]interface{}{"status": status})
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 05:51:40 +00:00
|
|
|
func drawText(canvas *image.RGBA, text string) error {
|
|
|
|
|
var (
|
|
|
|
|
fgColor image.Image
|
|
|
|
|
fontFace *truetype.Font
|
|
|
|
|
err error
|
|
|
|
|
fontSize = 12.0
|
|
|
|
|
)
|
|
|
|
|
fgColor = image.White
|
|
|
|
|
fontFace, err = freetype.ParseFont(goregular.TTF)
|
|
|
|
|
fontDrawer := &font.Drawer{
|
|
|
|
|
Dst: canvas,
|
|
|
|
|
Src: fgColor,
|
|
|
|
|
Face: truetype.NewFace(fontFace, &truetype.Options{
|
|
|
|
|
Size: fontSize,
|
|
|
|
|
Hinting: font.HintingFull,
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
//textBounds, _ := fontDrawer.BoundString(text)
|
|
|
|
|
xPosition := fixed.I(20)
|
|
|
|
|
//textHeight := textBounds.Max.Y - textBounds.Min.Y
|
|
|
|
|
yPosition := fixed.I(20)
|
|
|
|
|
fontDrawer.Dot = fixed.Point26_6{
|
|
|
|
|
X: xPosition,
|
|
|
|
|
Y: yPosition,
|
|
|
|
|
}
|
|
|
|
|
fontDrawer.DrawString(text)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createAvatar(text string) *image.RGBA {
|
|
|
|
|
bgColor := color.RGBA{
|
|
|
|
|
R: 255,
|
|
|
|
|
G: 20,
|
|
|
|
|
B: 100,
|
|
|
|
|
A: 255,
|
|
|
|
|
}
|
|
|
|
|
background := image.NewRGBA(image.Rect(0, 0, 200, 100))
|
|
|
|
|
draw.Draw(background, background.Bounds(), &image.Uniform{C: bgColor},
|
|
|
|
|
image.Point{}, draw.Src)
|
|
|
|
|
drawText(background, text)
|
|
|
|
|
return background
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-06 06:14:20 +00:00
|
|
|
func (h *Handler) showUserStatusBadgeView(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
user := mux.Vars(r)["user"]
|
|
|
|
|
if !h.storage.UserExists(user) {
|
|
|
|
|
notFound(w)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
statuses, _, err := h.storage.StatusByUsername(mux.Vars(r)["user"], 1, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
serverError(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var username string
|
|
|
|
|
if len(statuses) > 0 {
|
|
|
|
|
username = statuses[0].User
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
background := "#f0ffff"
|
|
|
|
|
|
|
|
|
|
dc := gg.NewContext(88, 31)
|
|
|
|
|
dc.SetHexColor(background)
|
|
|
|
|
dc.Clear()
|
|
|
|
|
dc.SetHexColor("#191970")
|
|
|
|
|
font, err := truetype.Parse(goregular.TTF)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic("")
|
|
|
|
|
}
|
|
|
|
|
face := truetype.NewFace(font, &truetype.Options{
|
|
|
|
|
Size: 12,
|
|
|
|
|
})
|
|
|
|
|
dc.SetFontFace(face)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
}
|
|
|
|
|
//dc.DrawString(username, 10, 14)
|
|
|
|
|
dc.DrawStringAnchored(username, 88/2, 8, 0.5, 0.5)
|
|
|
|
|
dc.DrawStringAnchored("status.cafe", 88/2, 20, 0.5, 0.5)
|
|
|
|
|
dc.EncodePNG(w)
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 05:51:40 +00:00
|
|
|
func (h *Handler) showUserStatusImageView(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
user := mux.Vars(r)["user"]
|
|
|
|
|
if !h.storage.UserExists(user) {
|
|
|
|
|
notFound(w)
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-11-26 22:36:48 +00:00
|
|
|
statuses, _, err := h.storage.StatusByUsername(mux.Vars(r)["user"], 1, 0)
|
2021-11-25 05:51:40 +00:00
|
|
|
if err != nil {
|
|
|
|
|
serverError(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
//w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
|
//w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
var text string
|
|
|
|
|
if len(statuses) > 0 {
|
|
|
|
|
text = statuses[0].Content
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
background := "#ffffff"
|
|
|
|
|
foreground := "#000000"
|
|
|
|
|
|
|
|
|
|
if c := r.URL.Query().Get("background"); c != "" {
|
|
|
|
|
background = "#" + c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c := r.URL.Query().Get("foreground"); c != "" {
|
|
|
|
|
foreground = "#" + c
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 21:38:08 +00:00
|
|
|
width := 125
|
|
|
|
|
dc := gg.NewContext(width, 125)
|
2021-11-25 05:51:40 +00:00
|
|
|
dc.SetHexColor(background)
|
|
|
|
|
dc.Clear()
|
|
|
|
|
dc.SetHexColor(foreground)
|
2021-11-28 21:38:08 +00:00
|
|
|
font, err := truetype.Parse(goregular.TTF)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic("")
|
|
|
|
|
}
|
|
|
|
|
face := truetype.NewFace(font, &truetype.Options{
|
|
|
|
|
Size: 9,
|
|
|
|
|
})
|
|
|
|
|
dc.SetFontFace(face)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
}
|
|
|
|
|
dc.DrawStringWrapped(text, 8, 4, 0, 0, float64(width-(2*8)), 1.5, gg.AlignLeft)
|
2021-11-25 05:51:40 +00:00
|
|
|
dc.EncodePNG(w)
|
|
|
|
|
//dc.SavePNG("out.png")
|
|
|
|
|
|
|
|
|
|
//avatar := createAvatar(text)
|
|
|
|
|
//png.Encode(w, avatar)
|
|
|
|
|
}
|
2021-11-28 14:31:51 +00:00
|
|
|
|
2021-12-10 07:33:45 +00:00
|
|
|
func truncate(s string, max int) string {
|
|
|
|
|
if len(s) > max {
|
|
|
|
|
return s[:max] + "..."
|
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 14:31:51 +00:00
|
|
|
func (h *Handler) showAtomView(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
username := mux.Vars(r)["user"]
|
|
|
|
|
user, err := h.storage.UserByName(username)
|
|
|
|
|
if err != nil {
|
|
|
|
|
notFound(w)
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-12-14 06:41:05 +00:00
|
|
|
feed := Feed{
|
|
|
|
|
Title: user.Name,
|
|
|
|
|
ID: fmt.Sprintf("https://status.cafe/users/%s/", user.Name),
|
|
|
|
|
Author: &Person{
|
|
|
|
|
Name: user.Name,
|
|
|
|
|
URI: fmt.Sprintf("https://status.cafe/users/%s", user.Name),
|
|
|
|
|
},
|
|
|
|
|
Updated: Time(time.Now()),
|
|
|
|
|
Link: []Link{
|
|
|
|
|
{
|
|
|
|
|
Rel: "self",
|
|
|
|
|
Href: fmt.Sprintf("https://status.cafe/users/%s.atom", user.Name),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Rel: "alternate",
|
|
|
|
|
Href: fmt.Sprintf("https://status.cafe/users/%s", user.Name),
|
|
|
|
|
Type: "text/html",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Icon: user.Picture,
|
|
|
|
|
Logo: user.Picture,
|
2021-11-28 14:31:51 +00:00
|
|
|
}
|
|
|
|
|
statuses, _, err := h.storage.StatusByUsername(user.Name, 20, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
serverError(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for _, status := range statuses {
|
2021-12-14 06:41:05 +00:00
|
|
|
feed.Entry = append(feed.Entry, createAtomEntryFromStatus(status))
|
2021-11-28 14:31:51 +00:00
|
|
|
}
|
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
|
w.Header().Set("Content-Type", "application/atom+xml")
|
2021-12-14 06:41:05 +00:00
|
|
|
var data []byte
|
|
|
|
|
data, err = xml.MarshalIndent(&feed, "", " ")
|
|
|
|
|
if err != nil {
|
|
|
|
|
serverError(w, err)
|
|
|
|
|
}
|
|
|
|
|
w.Write([]byte(xml.Header + string(data)))
|
2021-11-28 14:31:51 +00:00
|
|
|
}
|