SkunkyArt/post/comments.go

138 lines
3.6 KiB
Go

package post
import (
"encoding/json"
"fmt"
"skunkyart/util"
"strconv"
"strings"
)
type Comments struct {
Total int
Thread []struct {
CommentId, ParentId, Replies, Likes int
Posted util.Time
IsAuthorHighlited bool
TextContent struct {
Html struct {
Markup string
}
}
User struct {
Username string
IsBanned bool
}
}
}
// json
func js(c string, cc int, id []string, t int) (*string, bool, bool) {
var result string
var j struct {
Cursor string
PrevOffset int
HasMore, HasLess bool
}
for x := 0; x < cc; x++ {
/*
1: comments of the post;
4: comments on userpage
*/
p := "dashared/comments/thread?typeid=" + strconv.Itoa(t) + "&itemid=" + id[len(id)-1] + "&maxdepth=1000&order=newest"
result = util.Puppy(p + "&limit=50&cursor=" + strings.ReplaceAll(c, "+", `%2B`))
json.Unmarshal([]byte(result), &j)
if !j.HasMore {
cc = 0
}
c = j.Cursor
}
return &result, j.HasMore, j.HasLess
}
// comments!!
func GetComments(uri, cursor string, page, typ int, id []string) *string {
var comments Comments
rawjson, next, prev := js(cursor, page, id, typ)
err := json.Unmarshal([]byte(*rawjson), &comments)
util.Err(err)
// обработка жирсона
var htm string
for _, b := range comments.Thread {
var msgbody string
// если свич находит фигурную скобку, то парсит "по-новому", не находит - "по-старому" (в сообщении нет жсона).
switch b.TextContent.Html.Markup[0:1] {
case "{":
var comments struct {
Blocks []struct {
Text string
// EntityMap []struct {
// Data struct {
// Src, Title, Type string
// }
// }
}
}
err := json.Unmarshal([]byte(b.TextContent.Html.Markup), &comments)
util.Err(err)
for _, a := range comments.Blocks {
msgbody = a.Text
// for _, a := range a.EntityMap {
// fmt.Println(a)
// println("<img src=\"" + a.Data.Src + "\">" + a.Data.Title + "</img>")
// msgbody += "<img src=\"" + a.Data.Src + "\">" + a.Data.Title + "</img>"
// }
}
default:
msgbody = b.TextContent.Html.Markup
}
// удаляем говно
msgbody = strings.ReplaceAll(msgbody, "https://www.deviantart.com/users/outgoing?", "")
// генерируем HTML-куски
var (
isauthor string
reply string
)
if b.IsAuthorHighlited {
isauthor = "author"
}
var space string
if b.ParentId != 0 {
space = "margin-left: 2%"
reply = "<b>In reply to <a href=\"#" + strconv.Itoa(b.ParentId) + "\">message</a></b>:"
}
msgbody = util.Parse(msgbody)
htm += fmt.Sprintf("<div class=\"msg\" style=\"%[10]s\"><p id=\"%[2]d\"><img src=\""+util.Conf.Base_uri+"avatar/%[1]s\" width=\"30px\" height=\"30px\"/> <a href=\""+util.Conf.Base_uri+"user/%[1]s\"><b class=\"%[6]s %[5]t\">%[1]s</b></a> %[4]s %[9]s<p>%[3]s<p>👍: %[7]d ⏩: %[8]d</p></div>\n", b.User.Username, b.CommentId, msgbody, b.Posted.UTC().String(), b.User.IsBanned, isauthor, b.Likes, b.Replies, reply, space)
}
// если комментариев больше, чем 0, то отображаем их, иначе пишем, что комментов нет.
if comments.Total > 0 {
htm = "<h2 id=\"comments\">Comments (" + strconv.Itoa(comments.Total) + ")</h2>" + htm
uri = uri[strings.LastIndex(uri, "/")+1:]
if prev {
htm += "<a href=\"" + uri + "?p=" + strconv.Itoa(page-2) + "#comments\"><-- Back</a> "
}
if next {
htm += "<a href=\"" + uri + "?p=" + strconv.Itoa(page) + "#comments\">Next --></a>"
}
} else {
htm = "<p>There is no comments.</p>"
}
return &htm
}