commit 96745a8e63122a5445b4be746379a014f5187868
parent e2fd9642813e299bf22707c5778afbbd9c4b95e4
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 8 Jun 2023 00:07:05 -0700
separate stream modal concept
Diffstat:
5 files changed, 110 insertions(+), 57 deletions(-)
diff --git a/pkg/web/handlers/api/v1/codeModalInterceptor.go b/pkg/web/handlers/api/v1/codeModalInterceptor.go
@@ -17,7 +17,7 @@ func (i CodeModalInterceptor) InterceptMsg(cmd *Command) {
return
}
- database.MsgPubSub.Pub("code_hide_"+cmd.authUser.ID.String()+"_"+cmd.room.ID.String(), database.ChatMessageType{})
+ database.MsgPubSub.Pub("modal_code_hide_"+cmd.authUser.ID.String()+"_"+cmd.room.ID.String(), database.ChatMessageType{})
cmd.modMsg = isMod
if pm != "" {
diff --git a/pkg/web/handlers/api/v1/slashInterceptor.go b/pkg/web/handlers/api/v1/slashInterceptor.go
@@ -1788,7 +1788,8 @@ func handleCodeCmd(c *Command) (handled bool) {
toUserUsername := c.toUser.Username
payload.ToUserUsername = &toUserUsername
}
- database.MsgPubSub.Pub("code_show_"+c.authUser.ID.String()+"_"+c.room.ID.String(), payload)
+ // ModalManager.Pub("code", payload)
+ database.MsgPubSub.Pub("modal_code_show_"+c.authUser.ID.String()+"_"+c.room.ID.String(), payload)
return true
}
return
diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go
@@ -11,6 +11,7 @@ import (
"dkforest/pkg/pubsub"
"dkforest/pkg/utils/crypto"
v1 "dkforest/pkg/web/handlers/api/v1"
+ "dkforest/pkg/web/handlers/streamModals"
"encoding/base64"
"encoding/csv"
"encoding/hex"
@@ -5146,18 +5147,17 @@ func ChatStreamMessagesHandler(c echo.Context) error {
send("<div>" + v1.RenderMessages(authUser, data, csrf, nullUsername) + "</div>")
c.Response().Flush()
+ codeModal := streamModals.NewCodeModal(authUser, room)
+
// Create a subscriber and topics to listen to
- selfCodeShowTopic := "code_show_" + authUser.ID.String() + "_" + room.ID.String()
- selfCodeHideTopic := "code_hide_" + authUser.ID.String() + "_" + room.ID.String()
selfRefreshTopic := "refresh_" + string(authUser.Username)
readMarkerTopic := "readmarker_" + authUser.ID.String()
authorizedTopics := []string{
"refresh",
- selfCodeShowTopic,
- selfCodeHideTopic,
selfRefreshTopic,
readMarkerTopic,
"room_" + room.ID.String()}
+ authorizedTopics = append(authorizedTopics, codeModal.Topics()...)
sub := database.MsgPubSub.Subscribe(authorizedTopics)
defer sub.Close()
@@ -5201,8 +5201,6 @@ func ChatStreamMessagesHandler(c echo.Context) error {
// We need to change the css class in order for the css to never actually complete the animation and stay "green".
var indicatorSelector bool
- var codeModalIdx int
-
Loop:
for {
select {
@@ -5252,16 +5250,11 @@ Loop:
return nil
}
- if topic == selfCodeShowTopic {
- send(getCodeModalHTML(codeModalIdx, roomName, csrf, msgTyp))
- c.Response().Flush()
- continue
- }
- if topic == selfCodeHideTopic {
- send(`<style>.code-modal-` + strconv.Itoa(codeModalIdx) + `{display:none;}</style>`)
- codeModalIdx++
- c.Response().Flush()
- continue
+ if utils.InArr(topic, codeModal.Topics()) {
+ if codeModal.Handle(topic, csrf, msgTyp, send) {
+ c.Response().Flush()
+ continue
+ }
}
if msgTyp.Typ == database.DeleteMsg {
@@ -5361,45 +5354,6 @@ Loop:
return nil
}
-func getCodeModalHTML(codeModalIdx int, roomName, csrf string, msgTyp database.ChatMessageType) string {
- out := `<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="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>
- </select>
- <button type="submit">send</button>
- </div>
-</form>
-</div>`
- return out
-}
-
func ChatCodeHandler(c echo.Context) error {
messageUUID := c.Param("messageUUID")
idx, err := strconv.Atoi(c.Param("idx"))
diff --git a/pkg/web/handlers/streamModals/codeModal.go b/pkg/web/handlers/streamModals/codeModal.go
@@ -0,0 +1,73 @@
+package streamModals
+
+import (
+ "dkforest/pkg/database"
+ "strconv"
+)
+
+type CodeModal struct {
+ StreamModal
+}
+
+func NewCodeModal(authUser *database.User, room database.ChatRoom) *CodeModal {
+ name := "code"
+ m := &CodeModal{StreamModal{authUser: authUser, room: room, name: name}}
+ showTopic := "modal_" + name + "_show_" + m.authUser.ID.String() + "_" + m.room.ID.String()
+ hideTopic := "modal_" + name + "_hide_" + m.authUser.ID.String() + "_" + m.room.ID.String()
+ m.topics = append(m.topics, showTopic)
+ m.topics = append(m.topics, hideTopic)
+ return m
+}
+
+func (m *CodeModal) Handle(topic, csrf string, msgTyp database.ChatMessageType, send func(string)) bool {
+ if topic == m.topics[0] {
+ send(getCodeModalHTML(m.idx, m.room.Name, csrf, msgTyp))
+ return true
+
+ } else if topic == m.topics[1] {
+ send(`<style>.code-modal-` + strconv.Itoa(m.idx) + `{display:none;}</style>`)
+ m.idx++
+ return true
+ }
+
+ return false
+}
+
+func getCodeModalHTML(codeModalIdx int, roomName, csrf string, msgTyp database.ChatMessageType) string {
+ out := `<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="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>
+ </select>
+ <button type="submit">send</button>
+ </div>
+</form>
+</div>`
+ return out
+}
diff --git a/pkg/web/handlers/streamModals/streamModal.go b/pkg/web/handlers/streamModals/streamModal.go
@@ -0,0 +1,25 @@
+package streamModals
+
+import (
+ "dkforest/pkg/database"
+)
+
+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
+ // Implement interceptor
+}
+
+type StreamModal struct {
+ topics []string
+ idx int
+ name string
+ authUser *database.User
+ room database.ChatRoom
+}
+
+func (m *StreamModal) Topics() []string {
+ return m.topics
+}