-
Notifications
You must be signed in to change notification settings - Fork 26
/
editorWebsocket.go
185 lines (174 loc) · 4.49 KB
/
editorWebsocket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"context"
"errors"
"io"
"net/http"
"time"
ws "github.com/coder/websocket"
"github.com/google/uuid"
"go.goblog.app/app/pkgs/bodylimit"
"go.goblog.app/app/pkgs/contenttype"
"go.goblog.app/app/pkgs/htmlbuilder"
)
func (a *goBlog) serveEditorWebsocket(w http.ResponseWriter, r *http.Request) {
enablePreview := r.URL.Query().Get("preview") == "1"
enableSync := r.URL.Query().Get("sync") == "1"
if !enablePreview && !enableSync {
w.WriteHeader(http.StatusBadRequest)
return
}
// Get blog
blog, bc := a.getBlog(r)
// Open websocket connection
c, err := ws.Accept(w, r, &ws.AcceptOptions{CompressionMode: ws.CompressionContextTakeover})
if err != nil {
return
}
c.SetReadLimit(10 * bodylimit.MB)
defer c.Close(ws.StatusNormalClosure, "")
// Store connection to be able to send updates
var connectionId string
if enableSync {
connectionId = uuid.NewString()
bc.esws.Store(connectionId, c)
defer bc.esws.Delete(connectionId)
}
// Set cancel context
ctx, cancel := context.WithTimeout(r.Context(), time.Hour*6)
defer cancel()
// Send initial content
if enableSync {
initialState, err := a.getEditorStateFromDatabase(ctx, blog)
if err != nil {
return
}
if initialState != nil {
// Send initial state
if err := a.sendEditorState(ctx, c, initialState); err != nil {
return
}
// Send preview
if enablePreview {
if err := a.sendEditorPreview(ctx, c, blog, initialState); err != nil {
return
}
}
}
} else if !enableSync && enablePreview {
// Trigger editor to send content to generate the preview
w, err := c.Writer(ctx, ws.MessageText)
if err != nil {
return
}
if _, err := io.WriteString(w, "triggerpreview"); err != nil {
_ = w.Close()
return
}
if err := w.Close(); err != nil {
return
}
}
// Listen for new messages
for {
// Retrieve content
mt, message, err := c.Reader(ctx)
if err != nil {
break
}
if mt != ws.MessageText {
continue
}
messageBytes, err := io.ReadAll(message)
if err != nil {
break
}
// Save editor state
// and send editor state to other connections
if enableSync {
bc.esm.Lock()
a.updateEditorStateInDatabase(ctx, blog, messageBytes)
bc.esm.Unlock()
a.sendNewEditorStateToAllConnections(ctx, bc, connectionId, messageBytes)
}
// Create preview
if enablePreview {
if err := a.sendEditorPreview(ctx, c, blog, messageBytes); err != nil {
break
}
}
}
}
// SYNC
func (a *goBlog) sendNewEditorStateToAllConnections(ctx context.Context, bc *configBlog, origin string, state []byte) {
bc.esws.Range(func(key, value any) bool {
if key == origin {
return true
}
c, ok := value.(*ws.Conn)
if !ok {
return true
}
if err := a.sendEditorState(ctx, c, state); err != nil {
bc.esws.Delete(key)
return true
}
return true
})
}
func (*goBlog) sendEditorState(ctx context.Context, c *ws.Conn, state []byte) error {
w, err := c.Writer(ctx, ws.MessageText)
if err != nil {
return err
}
if _, err := io.WriteString(w, "sync:"); err != nil {
return errors.Join(err, w.Close())
}
if _, err := w.Write(state); err != nil {
return errors.Join(err, w.Close())
}
return w.Close()
}
const editorStateCacheKey = "editorstate_"
func (a *goBlog) updateEditorStateInDatabase(ctx context.Context, blog string, state []byte) {
_ = a.db.cachePersistentlyContext(ctx, editorStateCacheKey+blog, state)
}
func (a *goBlog) getEditorStateFromDatabase(ctx context.Context, blog string) ([]byte, error) {
return a.db.retrievePersistentCacheContext(ctx, editorStateCacheKey+blog)
}
// PREVIEW
func (a *goBlog) sendEditorPreview(ctx context.Context, c *ws.Conn, blog string, md []byte) error {
// Get writer
w, err := c.Writer(ctx, ws.MessageText)
if err != nil {
return err
}
if _, err := io.WriteString(w, "preview:"); err != nil {
return errors.Join(err, w.Close())
}
// Create preview post
p := &post{
Blog: blog,
Content: string(md),
}
if err := a.extractParamsFromContent(p); err != nil {
_, werr := io.WriteString(w, err.Error())
return errors.Join(werr, w.Close())
}
if err := a.checkPost(p, true); err != nil {
_, werr := io.WriteString(w, err.Error())
return errors.Join(werr, w.Close())
}
if t := p.Title(); t != "" {
p.RenderedTitle = a.renderMdTitle(t)
}
// Render post (using post's blog config)
pr, pw := io.Pipe()
go func() {
a.renderEditorPreview(htmlbuilder.NewHtmlBuilder(pw), a.getBlogFromPost(p), p)
_ = pw.Close()
}()
_ = a.min.Get().Minify(contenttype.HTMLUTF8, w, pr)
_ = pr.Close()
return w.Close()
}