dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 144a34b9a53610d195dcdf55d578fb39c68ab85d
parent 08e46093abd637b81d7d9fa38b5b531fbaa0175b
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Thu,  8 Jun 2023 13:44:47 -0700

Keep syntax highlight preference in memory for future use

Diffstat:
Acmd/dkf/migrations/139.sql | 4++++
Mpkg/database/tableUsers.go | 1+
Mpkg/web/handlers/handlers.go | 2+-
Mpkg/web/handlers/streamModals/codeModal.go | 71+++++++++++++++++++++++++++++++++++++++++------------------------------
Mpkg/web/handlers/streamModals/manager.go | 4++--
Mpkg/web/handlers/streamModals/streamModal.go | 2+-
6 files changed, 50 insertions(+), 34 deletions(-)

diff --git a/cmd/dkf/migrations/139.sql b/cmd/dkf/migrations/139.sql @@ -0,0 +1,4 @@ +-- +migrate Up +ALTER TABLE users ADD COLUMN syntax_highlight_code VARCHAR(20) NOT NULL DEFAULT ''; + +-- +migrate Down diff --git a/pkg/database/tableUsers.go b/pkg/database/tableUsers.go @@ -117,6 +117,7 @@ type User struct { GeneralMessagesCount int64 AFK bool UseStream bool + SyntaxHighlightCode string HighlightOwnMessages bool `gorm:"-"` } diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -5255,7 +5255,7 @@ Loop: return nil } - if modalsManager.Handle(topic, csrf, msgTyp, send) { + if modalsManager.Handle(authUser, topic, csrf, msgTyp, send) { c.Response().Flush() continue } diff --git a/pkg/web/handlers/streamModals/codeModal.go b/pkg/web/handlers/streamModals/codeModal.go @@ -1,10 +1,12 @@ package streamModals import ( + "bytes" "dkforest/pkg/database" dutils "dkforest/pkg/database/utils" "dkforest/pkg/utils" "dkforest/pkg/web/handlers/interceptors/command" + "html/template" "strconv" "strings" ) @@ -33,9 +35,9 @@ func (m *CodeModal) Css() string { return getCss() } -func (m *CodeModal) Handle(topic, csrf string, msgTyp database.ChatMessageType, send func(string)) bool { +func (m *CodeModal) Handle(authUser *database.User, topic, csrf string, msgTyp database.ChatMessageType, send func(string)) bool { if topic == m.topics[0] { - send(getCodeModalHTML(m.idx, m.room.Name, csrf, msgTyp)) + send(getCodeModalHTML(m.idx, m.room.Name, csrf, msgTyp, authUser.SyntaxHighlightCode)) return true } else if topic == m.topics[1] { @@ -59,43 +61,49 @@ func getCss() string { .code-modal .controls { position: absolute; left: 10px; right: 10px; bottom: 5px; }`, "\n"), " ") } -func getCodeModalHTML(codeModalIdx int, roomName, csrf string, msgTyp database.ChatMessageType) string { - out := `<div class="code-modal code-modal-` + strconv.Itoa(codeModalIdx) + `"> +func getCodeModalHTML(codeModalIdx int, roomName, csrf string, msgTyp database.ChatMessageType, syntaxHighlightCode string) string { + htmlTmpl := `<div class="code-modal code-modal-` + strconv.Itoa(codeModalIdx) + `"> <form method="post" target="iframe1" action="/api/v1/chat/top-bar/` + roomName + `"> - <input type="hidden" name="csrf" value="` + csrf + `" />` - if msgTyp.IsMod { - out += `<input type="hidden" name="isMod" value="1" />` - } - if msgTyp.ToUserUsername != nil { - out += `<input type="hidden" name="pm" value="` + msgTyp.ToUserUsername.String() + `" />` - } - out += ` + <input type="hidden" name="csrf" value="` + csrf + `" /> + {{ if .IsMod }} + <input type="hidden" name="isMod" value="1" /> + {{ end }} + {{ if .ToUserUsername }} + <input type="hidden" name="pm" value="{{ .ToUserUsername }}" /> + {{ end }} <input type="hidden" name="sender" value="codeModal" /> <div class=wrapper> <textarea name="message" placeholder="Paste your code here..."></textarea> </div> <div class="controls"> <select name="lang"> - <option value="">Raw text</option> - <option value="go">Golang</option> - <option value="rs">Rust</option> - <option value="cpp">C++</option> - <option value="c">C</option> - <option value="py">Python</option> - <option value="js">Javascript</option> - <option value="php">PHP</option> - <option value="css">CSS</option> - <option value="sql">SQL</option> - <option value="c#">C#</option> - <option value="rb">Ruby</option> - <option value="html">HTML</option> - <option value="bash">Bash</option> + <option value=""{{ if eq .SyntaxHighlightCode "" }} selected{{ end }}>Raw text</option> + <option value="go"{{ if eq .SyntaxHighlightCode "go" }} selected{{ end }}>Golang</option> + <option value="rs"{{ if eq .SyntaxHighlightCode "rs" }} selected{{ end }}>Rust</option> + <option value="cpp"{{ if eq .SyntaxHighlightCode "cpp" }} selected{{ end }}>C++</option> + <option value="c"{{ if eq .SyntaxHighlightCode "c" }} selected{{ end }}>C</option> + <option value="py"{{ if eq .SyntaxHighlightCode "py" }} selected{{ end }}>Python</option> + <option value="js"{{ if eq .SyntaxHighlightCode "js" }} selected{{ end }}>Javascript</option> + <option value="php"{{ if eq .SyntaxHighlightCode "php" }} selected{{ end }}>PHP</option> + <option value="css"{{ if eq .SyntaxHighlightCode "css" }} selected{{ end }}>CSS</option> + <option value="sql"{{ if eq .SyntaxHighlightCode "sql" }} selected{{ end }}>SQL</option> + <option value="c#"{{ if eq .SyntaxHighlightCode "C#" }} selected{{ end }}>C#</option> + <option value="rb"{{ if eq .SyntaxHighlightCode "rb" }} selected{{ end }}>Ruby</option> + <option value="html"{{ if eq .SyntaxHighlightCode "html" }} selected{{ end }}>HTML</option> + <option value="bash"{{ if eq .SyntaxHighlightCode "bash" }} selected{{ end }}>Bash</option> </select> <button type="submit">send</button> </div> </form> </div>` - return out + data := map[string]any{ + "IsMod": msgTyp.IsMod, + "ToUserUsername": msgTyp.ToUserUsername, + "SyntaxHighlightCode": syntaxHighlightCode, + } + var buf bytes.Buffer + _ = utils.Must(template.New("").Parse(htmlTmpl)).Execute(&buf, data) + return buf.String() } func (_ CodeModal) InterceptMsg(cmd *command.Command) { @@ -110,6 +118,12 @@ func (_ CodeModal) InterceptMsg(cmd *command.Command) { CodeModal{}.Hide(cmd.AuthUser.ID, cmd.Room.ID) + if !utils.InArr(lang, []string{"go", "c", "cpp", "py", "js", "php", "css", "sql", "rs", "c#", "rb", "html", "bash"}) { + lang = "" + } + cmd.AuthUser.SyntaxHighlightCode = lang + cmd.AuthUser.DoSave(cmd.DB) + cmd.ModMsg = isMod if pm != "" { toUser, err := cmd.DB.GetUserByUsername(database.Username(pm)) @@ -130,9 +144,6 @@ func (_ CodeModal) InterceptMsg(cmd *command.Command) { return } - if !utils.InArr(lang, []string{"go", "c", "cpp", "py", "js", "php", "css", "sql", "rs", "c#", "rb", "html", "bash"}) { - lang = "" - } cmd.OrigMessage = codeFenceWrap(lang, cmd.OrigMessage) cmd.Message = codeFenceWrap(lang, cmd.Message) } diff --git a/pkg/web/handlers/streamModals/manager.go b/pkg/web/handlers/streamModals/manager.go @@ -36,10 +36,10 @@ func (m *ModalsManager) Topics() (out []string) { } // Handle returns after the first modal that handle a specific topic -func (m *ModalsManager) Handle(topic, csrf string, msgTyp database.ChatMessageType, send func(string)) bool { +func (m *ModalsManager) Handle(authUser *database.User, topic, csrf string, msgTyp database.ChatMessageType, send func(string)) bool { for _, modal := range m.modals { if utils.InArr(topic, modal.Topics()) { - if modal.Handle(topic, csrf, msgTyp, send) { + if modal.Handle(authUser, topic, csrf, msgTyp, send) { return true } } diff --git a/pkg/web/handlers/streamModals/streamModal.go b/pkg/web/handlers/streamModals/streamModal.go @@ -8,7 +8,7 @@ type IStreamModal interface { // Topics returns all the topics the modal is interested in Topics() []string // Handle a stream message - Handle(topic, csrf string, msgTyp database.ChatMessageType, send func(string)) bool + Handle(authUser *database.User, topic, csrf string, msgTyp database.ChatMessageType, send func(string)) bool // Implement interceptor Show(database.UserID, database.RoomID, database.ChatMessageType) Hide(database.UserID, database.RoomID)