1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Avoid overwriting others work (fix #107)

This commit is contained in:
Daniel Heath 2018-01-31 09:33:23 +11:00
parent d0bc74ec55
commit da6d6e5d43
4 changed files with 19 additions and 4 deletions

File diff suppressed because one or more lines are too long

View File

@ -356,6 +356,7 @@ func handlePageRequest(c *gin.Context) {
"Debounce": debounceTime, "Debounce": debounceTime,
"DiaryMode": diaryMode, "DiaryMode": diaryMode,
"Date": time.Now().Format("2006-01-02"), "Date": time.Now().Format("2006-01-02"),
"UnixTime": time.Now().Unix(),
}) })
} }
@ -415,6 +416,7 @@ func handlePageUpdate(c *gin.Context) {
type QueryJSON struct { type QueryJSON struct {
Page string `json:"page"` Page string `json:"page"`
NewText string `json:"new_text"` NewText string `json:"new_text"`
FetchedAt int64 `json:"fetched_at"`
IsEncrypted bool `json:"is_encrypted"` IsEncrypted bool `json:"is_encrypted"`
IsPrimed bool `json:"is_primed"` IsPrimed bool `json:"is_primed"`
Meta string `json:"meta"` Meta string `json:"meta"`
@ -442,6 +444,8 @@ func handlePageUpdate(c *gin.Context) {
message = "Locked, must unlock first" message = "Locked, must unlock first"
} else if p.IsEncrypted { } else if p.IsEncrypted {
message = "Encrypted, must decrypt first" message = "Encrypted, must decrypt first"
} else if json.FetchedAt > 0 && p.LastEditUnixTime() > json.FetchedAt {
message = "Refusing to overwrite others work"
} else { } else {
p.Meta = json.Meta p.Meta = json.Meta
p.Update(json.NewText) p.Update(json.NewText)
@ -455,7 +459,7 @@ func handlePageUpdate(c *gin.Context) {
message = "Saved" message = "Saved"
success = true success = true
} }
c.JSON(http.StatusOK, gin.H{"success": success, "message": message}) c.JSON(http.StatusOK, gin.H{"success": success, "message": message, "unix_time": time.Now().Unix()})
} }
func handlePrime(c *gin.Context) { func handlePrime(c *gin.Context) {

View File

@ -27,6 +27,14 @@ type Page struct {
IsPublished bool IsPublished bool
} }
func (p Page) LastEditTime() time.Time {
return time.Unix(p.LastEditUnixTime(), 0)
}
func (p Page) LastEditUnixTime() int64 {
return p.Text.LastEditTime() / 1000000000
}
func Open(name string) (p *Page) { func Open(name string) (p *Page) {
p = new(Page) p = new(Page)
p.Name = name p.Name = name

View File

@ -182,18 +182,21 @@ body#pad textarea {
upload(); upload();
}, {{ .Debounce }})); }, {{ .Debounce }}));
var lastFetch = {{ .UnixTime }};
function upload() { function upload() {
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
url: '/update', url: '/update',
data: JSON.stringify({ data: JSON.stringify({
new_text: $('#userInput').val(), new_text: $('#userInput').val(),
page: "{{ .Page }}" page: "{{ .Page }}",
fetched_at: lastFetch,
}), }),
success: function(data) { success: function(data) {
$('#saveEditButton').removeClass() $('#saveEditButton').removeClass()
if (data.success == true) { if (data.success == true) {
$('#saveEditButton').addClass("success"); $('#saveEditButton').addClass("success");
lastFetch = data.unix_time;
} else { } else {
$('#saveEditButton').addClass("failure"); $('#saveEditButton').addClass("failure");
} }