Add toot info page and improve linking between pages

This commit is contained in:
Romain de Laage 2021-03-04 10:13:38 +01:00
parent ce86ff0386
commit abd5b21817
Signed by: rdelaage
GPG Key ID: 534845FADDF0C329
3 changed files with 96 additions and 6 deletions

19
main.go
View File

@ -20,10 +20,12 @@ package main
import "log"
type Blog struct {
Id string `json:"id"`
Content string `json:"content"`
Date string `json:"created_at"`
Author Account `json:"account"`
Id string `json:"id"`
Content string `json:"content"`
Date string `json:"created_at"`
Author Account `json:"account"`
Tags []Tag `json:"tags"`
Mentions []Mention `json:"mentions"`
}
type Config struct {
@ -47,6 +49,15 @@ type Thread struct {
Descendants []Blog `json:"descendants"`
}
type Mention struct {
Id string `json:"id"`
Name string `json:"acct"`
}
type Tag struct {
Name string `json:"name"`
}
func main() {
config := getConfig()

View File

@ -103,6 +103,8 @@ func handleConn(conn *tls.Conn, baseURL, title, home_message string) {
// home
if path == "" || path == "/" {
log.Println("Received request for home page")
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n# %s\n\n%s\n\n=> /tag Search for a tag\n=> /about About MastoGem", title, home_message)
if err != nil {
log.Println("send error: %s", err)
@ -163,6 +165,9 @@ This capsule use %s Mastodon instance.
# AGPLv3 License
=> http://www.gnu.org/licenses/agpl-3.0.txt AGPLv3 License text`
log.Println("Received request for about page")
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n" + page, baseURL)
if err != nil {
log.Println("send: %s", err)
@ -178,7 +183,26 @@ This capsule use %s Mastodon instance.
return
}
log.Println("Received request for tag " + query)
printTag(conn, baseURL, query)
} /* toot */ else if strings.HasPrefix(path, "/toot/") {
path = path[6:]
_, err = strconv.ParseUint(path, 10, 64)
if err != nil {
log.Println("invalid request: %s", err)
_, err = fmt.Fprintf(conn, "59 Can't parse request\r\n")
if err != nil {
log.Println("send error: %s", err)
return
}
return
}
log.Println("Received request for toot " + path)
printToot(conn, baseURL, path)
} else {
_, err = fmt.Fprintf(conn, "59 Invalid request\r\n")
if err != nil {
@ -222,6 +246,61 @@ func printProfile(conn *tls.Conn, baseURL, profileID string) {
}
}
func printToot(conn *tls.Conn, baseURL, tootID string) {
toot, err := getToot(baseURL, tootID)
if err != nil {
_, err = fmt.Fprintf(conn, "40 Remote mastodon instance failed\r\n")
if err != nil {
log.Println("handleConn: %s", err)
return
}
return
}
// Print header
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n")
if err != nil {
log.Println("handleConn: %s", err)
return
}
// Print toot
_, err = fmt.Fprintf(conn, "# Toot\n\n%s\n=> /thread/%s View the thread\n=> /profile/%s More toots from %s\n", formatBlog(toot), toot.Id, toot.Author.Id, toot.Author.Name)
if err != nil {
log.Println("handleConn: %s", err)
return
}
// print mentions
_, err = fmt.Fprintf(conn, "\n# Mentions\n")
if err != nil {
log.Println("handleConn: %s", err)
return
}
for _, mention := range toot.Mentions {
_, err = fmt.Fprintf(conn, "\n=> /profile/%s View %s profile", mention.Id, mention.Name)
if err != nil {
log.Println("handleConn: %s", err)
return
}
}
// print tags
_, err = fmt.Fprintf(conn, "\n# Tags\n")
if err != nil {
log.Println("handleConn: %s", err)
return
}
for _, tag := range toot.Tags {
_, err = fmt.Fprintf(conn, "\n=> /tag?%s View %s tag", url.QueryEscape(tag.Name), tag.Name)
if err != nil {
log.Println("handleConn: %s", err)
return
}
}
}
func printThread(conn *tls.Conn, baseURL, tootID string) {
originalToot, err := getToot(baseURL, tootID)
if err != nil {
@ -312,7 +391,7 @@ func printTag(conn *tls.Conn, baseURL, tag string) {
// Print toots
for _, toot := range toots {
_, err = fmt.Fprintf(conn, "\n%s\n=> /profile/%s More toots from %s\n", formatBlog(toot), toot.Author.Id, toot.Author.Name)
_, err = fmt.Fprintf(conn, "\n%s\n=> /profile/%s More toots from %s\n=> /thread/%s View the thread\n", formatBlog(toot), toot.Author.Id, toot.Author.Name, toot.Id)
if err != nil {
log.Println("handleConn: %s", err)
return

View File

@ -99,5 +99,5 @@ func formatBlog(toot Blog) string {
author = toot.Author.DisplayName
}
return "### Written by " + author + " on " + toot.Date[0:10] + " at " + toot.Date[11:16] + "\n" + content
return "### Written by " + author + " on " + toot.Date[0:10] + " at " + toot.Date[11:16] + "\n" + content + "\n=> /toot/" + toot.Id + " More informations about this toot"
}