Reformat more imports

This commit is contained in:
Timur Ismagilov 2024-09-07 22:53:22 +03:00
parent b3626ee546
commit 8466e2d613
10 changed files with 59 additions and 54 deletions

View File

@ -2,23 +2,23 @@ package shroom
import ( import (
"errors" "errors"
hyphae2 "github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/internal/user"
"github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/internal/user"
"github.com/bouncepaw/mycorrhiza/l18n" "github.com/bouncepaw/mycorrhiza/l18n"
) )
// TODO: get rid of this abomination // TODO: get rid of this abomination
func canFactory( func canFactory(
rejectLogger func(hyphae2.Hypha, *user.User, string), rejectLogger func(hyphae.Hypha, *user.User, string),
action string, action string,
dispatcher func(hyphae2.Hypha, *user.User, *l18n.Localizer) (string, string), dispatcher func(hyphae.Hypha, *user.User, *l18n.Localizer) (string, string),
noRightsMsg string, noRightsMsg string,
notExistsMsg string, notExistsMsg string,
mustExist bool, mustExist bool,
) func(*user.User, hyphae2.Hypha, *l18n.Localizer) error { ) func(*user.User, hyphae.Hypha, *l18n.Localizer) error {
return func(u *user.User, h hyphae2.Hypha, lc *l18n.Localizer) error { return func(u *user.User, h hyphae.Hypha, lc *l18n.Localizer) error {
if !u.CanProceed(action) { if !u.CanProceed(action) {
rejectLogger(h, u, "no rights") rejectLogger(h, u, "no rights")
return errors.New(noRightsMsg) return errors.New(noRightsMsg)
@ -26,7 +26,7 @@ func canFactory(
if mustExist { if mustExist {
switch h.(type) { switch h.(type) {
case *hyphae2.EmptyHypha: case *hyphae.EmptyHypha:
rejectLogger(h, u, "does not exist") rejectLogger(h, u, "does not exist")
return errors.New(notExistsMsg) return errors.New(notExistsMsg)
} }

View File

@ -2,29 +2,30 @@ package shroom
import ( import (
"fmt" "fmt"
"github.com/bouncepaw/mycorrhiza/history" "github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/internal/backlinks" "github.com/bouncepaw/mycorrhiza/internal/backlinks"
"github.com/bouncepaw/mycorrhiza/internal/categories" "github.com/bouncepaw/mycorrhiza/internal/categories"
hyphae2 "github.com/bouncepaw/mycorrhiza/internal/hyphae" "github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/internal/user" "github.com/bouncepaw/mycorrhiza/internal/user"
) )
// Delete deletes the hypha and makes a history record about that. // Delete deletes the hypha and makes a history record about that.
func Delete(u *user.User, h hyphae2.ExistingHypha) error { func Delete(u *user.User, h hyphae.ExistingHypha) error {
hop := history. hop := history.
Operation(history.TypeDeleteHypha). Operation(history.TypeDeleteHypha).
WithMsg(fmt.Sprintf("Delete %s", h.CanonicalName())). WithMsg(fmt.Sprintf("Delete %s", h.CanonicalName())).
WithUser(u) WithUser(u)
originalText, _ := hyphae2.FetchMycomarkupFile(h) originalText, _ := hyphae.FetchMycomarkupFile(h)
switch h := h.(type) { switch h := h.(type) {
case *hyphae2.MediaHypha: case *hyphae.MediaHypha:
if h.HasTextFile() { if h.HasTextFile() {
hop.WithFilesRemoved(h.MediaFilePath(), h.TextFilePath()) hop.WithFilesRemoved(h.MediaFilePath(), h.TextFilePath())
} else { } else {
hop.WithFilesRemoved(h.MediaFilePath()) hop.WithFilesRemoved(h.MediaFilePath())
} }
case *hyphae2.TextualHypha: case *hyphae.TextualHypha:
hop.WithFilesRemoved(h.TextFilePath()) hop.WithFilesRemoved(h.TextFilePath())
} }
if hop.Apply().HasErrors() { if hop.Apply().HasErrors() {
@ -32,6 +33,6 @@ func Delete(u *user.User, h hyphae2.ExistingHypha) error {
} }
backlinks.UpdateBacklinksAfterDelete(h, originalText) backlinks.UpdateBacklinksAfterDelete(h, originalText)
categories.RemoveHyphaFromAllCategories(h.CanonicalName()) categories.RemoveHyphaFromAllCategories(h.CanonicalName())
hyphae2.DeleteHypha(h) hyphae.DeleteHypha(h)
return nil return nil
} }

View File

@ -1,22 +1,24 @@
package shroom package shroom
import ( import (
"os"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/mycoopts"
"github.com/bouncepaw/mycorrhiza/web/viewutil"
"git.sr.ht/~bouncepaw/mycomarkup/v5" "git.sr.ht/~bouncepaw/mycomarkup/v5"
"git.sr.ht/~bouncepaw/mycomarkup/v5/blocks" "git.sr.ht/~bouncepaw/mycomarkup/v5/blocks"
"git.sr.ht/~bouncepaw/mycomarkup/v5/mycocontext" "git.sr.ht/~bouncepaw/mycomarkup/v5/mycocontext"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
hyphae2 "github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/mycoopts"
"github.com/bouncepaw/mycorrhiza/web/viewutil"
"os"
) )
// SetHeaderLinks initializes header links by reading the configured hypha, if there is any, or resorting to default values. // SetHeaderLinks initializes header links by reading the configured hypha, if there is any, or resorting to default values.
func SetHeaderLinks() { func SetHeaderLinks() {
switch userLinksHypha := hyphae2.ByName(cfg.HeaderLinksHypha).(type) { switch userLinksHypha := hyphae.ByName(cfg.HeaderLinksHypha).(type) {
case *hyphae2.EmptyHypha: case *hyphae.EmptyHypha:
setDefaultHeaderLinks() setDefaultHeaderLinks()
case hyphae2.ExistingHypha: case hyphae.ExistingHypha:
contents, err := os.ReadFile(userLinksHypha.TextFilePath()) contents, err := os.ReadFile(userLinksHypha.TextFilePath())
if err != nil || len(contents) == 0 { if err != nil || len(contents) == 0 {
setDefaultHeaderLinks() setDefaultHeaderLinks()

View File

@ -1,9 +1,10 @@
package shroom package shroom
import ( import (
"log"
"github.com/bouncepaw/mycorrhiza/internal/hyphae" "github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/internal/user" "github.com/bouncepaw/mycorrhiza/internal/user"
"log"
) )
func rejectRenameLog(h hyphae.Hypha, u *user.User, errmsg string) { func rejectRenameLog(h hyphae.Hypha, u *user.User, errmsg string) {

View File

@ -3,36 +3,36 @@ package shroom
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/bouncepaw/mycorrhiza/internal/backlinks"
"github.com/bouncepaw/mycorrhiza/internal/categories"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/bouncepaw/mycorrhiza/internal/files"
hyphae2 "github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/internal/user"
"path" "path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
"github.com/bouncepaw/mycorrhiza/history" "github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/internal/backlinks"
"github.com/bouncepaw/mycorrhiza/internal/categories"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/bouncepaw/mycorrhiza/internal/files"
"github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/internal/user"
"github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/util"
) )
// Rename renames the old hypha to the new name and makes a history record about that. Call if and only if the user has the permission to rename. // Rename renames the old hypha to the new name and makes a history record about that. Call if and only if the user has the permission to rename.
func Rename(oldHypha hyphae2.ExistingHypha, newName string, recursive bool, leaveRedirections bool, u *user.User) error { func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, leaveRedirections bool, u *user.User) error {
// * bouncepaw hates this function and related renaming functions // * bouncepaw hates this function and related renaming functions
if newName == "" { if newName == "" {
rejectRenameLog(oldHypha, u, "no new name given") rejectRenameLog(oldHypha, u, "no new name given")
return errors.New("ui.rename_noname_tip") return errors.New("ui.rename_noname_tip")
} }
if !hyphae2.IsValidName(newName) { if !hyphae.IsValidName(newName) {
rejectRenameLog(oldHypha, u, fmt.Sprintf("new name %s invalid", newName)) rejectRenameLog(oldHypha, u, fmt.Sprintf("new name %s invalid", newName))
return errors.New("ui.rename_badname_tip") // FIXME: There is a bug related to this. return errors.New("ui.rename_badname_tip") // FIXME: There is a bug related to this.
} }
switch targetHypha := hyphae2.ByName(newName); targetHypha.(type) { switch targetHypha := hyphae.ByName(newName); targetHypha.(type) {
case hyphae2.ExistingHypha: case hyphae.ExistingHypha:
if targetHypha.CanonicalName() == oldHypha.CanonicalName() { if targetHypha.CanonicalName() == oldHypha.CanonicalName() {
return nil return nil
} }
@ -81,7 +81,7 @@ func Rename(oldHypha hyphae2.ExistingHypha, newName string, recursive bool, leav
oldName = h.CanonicalName() oldName = h.CanonicalName()
newName = re.ReplaceAllString(oldName, newName) newName = re.ReplaceAllString(oldName, newName)
) )
hyphae2.RenameHyphaTo(h, newName, replaceName) hyphae.RenameHyphaTo(h, newName, replaceName)
backlinks.UpdateBacklinksAfterRename(h, oldName) backlinks.UpdateBacklinksAfterRename(h, oldName)
categories.RenameHyphaInAllCategories(oldName, newName) categories.RenameHyphaInAllCategories(oldName, newName)
if leaveRedirections { if leaveRedirections {
@ -104,12 +104,12 @@ const redirectionTemplate = `=> %[1]s | 👁️➡️ %[2]s
func leaveRedirection(oldName, newName string, hop *history.Op) error { func leaveRedirection(oldName, newName string, hop *history.Op) error {
var ( var (
text = fmt.Sprintf(redirectionTemplate, newName, util.BeautifulName(newName)) text = fmt.Sprintf(redirectionTemplate, newName, util.BeautifulName(newName))
emptyHypha = hyphae2.ByName(oldName) emptyHypha = hyphae.ByName(oldName)
) )
switch emptyHypha := emptyHypha.(type) { switch emptyHypha := emptyHypha.(type) {
case *hyphae2.EmptyHypha: case *hyphae.EmptyHypha:
h := hyphae2.ExtendEmptyToTextual(emptyHypha, filepath.Join(files.HyphaeDir(), oldName+".myco")) h := hyphae.ExtendEmptyToTextual(emptyHypha, filepath.Join(files.HyphaeDir(), oldName+".myco"))
hyphae2.Insert(h) hyphae.Insert(h)
categories.AddHyphaToCategory(oldName, cfg.RedirectionCategory) categories.AddHyphaToCategory(oldName, cfg.RedirectionCategory)
defer backlinks.UpdateBacklinksAfterEdit(h, "") defer backlinks.UpdateBacklinksAfterEdit(h, "")
return writeTextToDisk(h, []byte(text), hop) return writeTextToDisk(h, []byte(text), hop)
@ -118,15 +118,15 @@ func leaveRedirection(oldName, newName string, hop *history.Op) error {
} }
} }
func findHyphaeToRename(superhypha hyphae2.ExistingHypha, recursive bool) []hyphae2.ExistingHypha { func findHyphaeToRename(superhypha hyphae.ExistingHypha, recursive bool) []hyphae.ExistingHypha {
hyphaList := []hyphae2.ExistingHypha{superhypha} hyphaList := []hyphae.ExistingHypha{superhypha}
if recursive { if recursive {
hyphaList = append(hyphaList, hyphae2.Subhyphae(superhypha)...) hyphaList = append(hyphaList, hyphae.Subhyphae(superhypha)...)
} }
return hyphaList return hyphaList
} }
func renamingPairs(hyphaeToRename []hyphae2.ExistingHypha, replaceName func(string) string) (map[string]string, error) { func renamingPairs(hyphaeToRename []hyphae.ExistingHypha, replaceName func(string) string) (map[string]string, error) {
var ( var (
renameMap = make(map[string]string) renameMap = make(map[string]string)
newNames = make([]string, len(hyphaeToRename)) newNames = make([]string, len(hyphaeToRename))
@ -138,12 +138,12 @@ func renamingPairs(hyphaeToRename []hyphae2.ExistingHypha, replaceName func(stri
renameMap[h.TextFilePath()] = replaceName(h.TextFilePath()) renameMap[h.TextFilePath()] = replaceName(h.TextFilePath())
} }
switch h := h.(type) { switch h := h.(type) {
case *hyphae2.MediaHypha: case *hyphae.MediaHypha:
renameMap[h.MediaFilePath()] = replaceName(h.MediaFilePath()) renameMap[h.MediaFilePath()] = replaceName(h.MediaFilePath())
} }
h.Unlock() h.Unlock()
} }
if firstFailure, ok := hyphae2.AreFreeNames(newNames...); !ok { if firstFailure, ok := hyphae.AreFreeNames(newNames...); !ok {
return nil, errors.New("Hypha " + firstFailure + " already exists") return nil, errors.New("Hypha " + firstFailure + " already exists")
} }
return renameMap, nil return renameMap, nil

View File

@ -1,9 +1,9 @@
package shroom package shroom
import ( import (
"github.com/bouncepaw/mycorrhiza/internal/hyphae"
"strings" "strings"
"github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/util"
) )

View File

@ -2,14 +2,14 @@ package shroom
import ( import (
"fmt" "fmt"
hyphae2 "github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/internal/user"
"github.com/bouncepaw/mycorrhiza/history" "github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/internal/user"
) )
// RemoveMedia removes media from the media hypha and makes a history record about that. If it only had media, the hypha will be deleted. If it also had text, the hypha will become textual. // RemoveMedia removes media from the media hypha and makes a history record about that. If it only had media, the hypha will be deleted. If it also had text, the hypha will become textual.
func RemoveMedia(u *user.User, h *hyphae2.MediaHypha) error { func RemoveMedia(u *user.User, h *hyphae.MediaHypha) error {
hop := history. hop := history.
Operation(history.TypeRemoveMedia). Operation(history.TypeRemoveMedia).
WithFilesRemoved(h.MediaFilePath()). WithFilesRemoved(h.MediaFilePath()).
@ -24,9 +24,9 @@ func RemoveMedia(u *user.User, h *hyphae2.MediaHypha) error {
} }
if h.HasTextFile() { if h.HasTextFile() {
hyphae2.Insert(hyphae2.ShrinkMediaToTextual(h)) hyphae.Insert(hyphae.ShrinkMediaToTextual(h))
} else { } else {
hyphae2.DeleteHypha(h) hyphae.DeleteHypha(h)
} }
return nil return nil
} }

View File

@ -3,10 +3,10 @@ package user
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"log" "log"
"os" "os"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/bouncepaw/mycorrhiza/internal/files" "github.com/bouncepaw/mycorrhiza/internal/files"
"github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/util"
) )

View File

@ -6,16 +6,16 @@ import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"log" "log"
"net/http" "net/http"
"sort" "sort"
"strings" "strings"
"time" "time"
"golang.org/x/crypto/bcrypt" "github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/util"
"golang.org/x/crypto/bcrypt"
) )
// CanProceed returns `true` if the user in `rq` has enough rights to access `route`. // CanProceed returns `true` if the user in `rq` has enough rights to access `route`.

View File

@ -2,12 +2,13 @@ package user
import ( import (
"fmt" "fmt"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"net/http" "net/http"
"strings" "strings"
"sync" "sync"
"time" "time"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )