From d8699d46c08a142ffa67a64695f02d3ad951d7b1 Mon Sep 17 00:00:00 2001
From: Timur Ismagilov <bouncepaw2@ya.ru>
Date: Sat, 26 Feb 2022 09:42:54 +0300
Subject: [PATCH] Backlinks: Fix backlinks for media and empty hyphae

---
 hyphae/existing_hypha.go | 18 ++++++++++++++++++
 shroom/upload.go         |  7 ++-----
 user/user.go             |  4 ++++
 views/nav.qtpl           |  2 +-
 views/nav.qtpl.go        |  2 +-
 5 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/hyphae/existing_hypha.go b/hyphae/existing_hypha.go
index d75523a..3f230f9 100644
--- a/hyphae/existing_hypha.go
+++ b/hyphae/existing_hypha.go
@@ -2,6 +2,8 @@ package hyphae
 
 import (
 	"github.com/bouncepaw/mycorrhiza/util"
+	"os"
+	"path/filepath"
 )
 
 // ExistingHypha is not EmptyHypha. *MediaHypha and *TextualHypha implement this interface.
@@ -59,3 +61,19 @@ func Insert(h ExistingHypha) (madeNewRecord bool) {
 
 	return !recorded
 }
+
+func WriteToMycoFile(h ExistingHypha, data []byte) error {
+	if err := os.MkdirAll(filepath.Dir(h.TextFilePath()), 0777); err != nil {
+		return err
+	}
+	if err := os.WriteFile(h.TextFilePath(), data, 0666); err != nil {
+		return err
+	}
+	switch h := h.(type) {
+	case *MediaHypha:
+		if !h.HasTextFile() {
+			h.mycoFilePath = h.TextFilePath()
+		}
+	}
+	return nil
+}
diff --git a/shroom/upload.go b/shroom/upload.go
index 97f0e7e..04045f4 100644
--- a/shroom/upload.go
+++ b/shroom/upload.go
@@ -33,11 +33,7 @@ func historyMessageForTextUpload(h hyphae.Hypha, userMessage string) string {
 }
 
 func writeTextToDisk(h hyphae.ExistingHypha, data []byte, hop *history.Op) error {
-	if err := os.MkdirAll(filepath.Dir(h.TextFilePath()), 0777); err != nil {
-		return err
-	}
-
-	if err := os.WriteFile(h.TextFilePath(), data, 0666); err != nil {
+	if err := hyphae.WriteToMycoFile(h, data); err != nil {
 		return err
 	}
 	hop.WithFiles(h.TextFilePath())
@@ -92,6 +88,7 @@ func UploadText(h hyphae.Hypha, data []byte, userMessage string, u *user.User) e
 		}
 
 		hyphae.Insert(H)
+		backlinks.UpdateBacklinksAfterEdit(H, "")
 	case *hyphae.MediaHypha:
 		oldText, err := FetchTextFile(h)
 		if err != nil {
diff --git a/user/user.go b/user/user.go
index 26b0ffe..fe35bf1 100644
--- a/user/user.go
+++ b/user/user.go
@@ -31,6 +31,10 @@ type User struct {
 
 // Route — Right (more is more right)
 var minimalRights = map[string]int{
+	"text":                0,
+	"backlinks":           0,
+	"history":             0,
+	"attachment":          1,
 	"edit":                1,
 	"upload-binary":       1,
 	"upload-text":         1,
diff --git a/views/nav.qtpl b/views/nav.qtpl
index 5bf2897..592d34f 100644
--- a/views/nav.qtpl
+++ b/views/nav.qtpl
@@ -24,7 +24,7 @@
 	<ul class="hypha-info__list">
 		{%= hyphaInfoEntry(h, u, "history", lc.Get("ui.history_link")) %}
 		{%= hyphaInfoEntry(h, u, "rename", lc.Get("ui.rename_link")) %}
-		{%= hyphaInfoEntry(h, u, "delete-ask", lc.Get("ui.delete_link")) %}
+		{%= hyphaInfoEntry(h, u, "delete", lc.Get("ui.delete_link")) %}
 		{%= hyphaInfoEntry(h, u, "text", lc.Get("ui.text_link")) %}
 		{%= hyphaInfoEntry(h, u, "attachment", lc.Get("ui.attachment_link")) %}
 		{%= hyphaInfoEntry(h, u, "backlinks", lc.GetPlural("ui.backlinks_link", backs)) %}
diff --git a/views/nav.qtpl.go b/views/nav.qtpl.go
index 1bb70ef..5ce4d5c 100644
--- a/views/nav.qtpl.go
+++ b/views/nav.qtpl.go
@@ -127,7 +127,7 @@ func streamhyphaInfo(qw422016 *qt422016.Writer, rq *http.Request, h hyphae.Hypha
 	qw422016.N().S(`
 		`)
 //line views/nav.qtpl:27
-	streamhyphaInfoEntry(qw422016, h, u, "delete-ask", lc.Get("ui.delete_link"))
+	streamhyphaInfoEntry(qw422016, h, u, "delete", lc.Get("ui.delete_link"))
 //line views/nav.qtpl:27
 	qw422016.N().S(`
 		`)