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:
parent
d0bc74ec55
commit
da6d6e5d43
File diff suppressed because one or more lines are too long
@ -356,6 +356,7 @@ func handlePageRequest(c *gin.Context) {
|
||||
"Debounce": debounceTime,
|
||||
"DiaryMode": diaryMode,
|
||||
"Date": time.Now().Format("2006-01-02"),
|
||||
"UnixTime": time.Now().Unix(),
|
||||
})
|
||||
}
|
||||
|
||||
@ -415,6 +416,7 @@ func handlePageUpdate(c *gin.Context) {
|
||||
type QueryJSON struct {
|
||||
Page string `json:"page"`
|
||||
NewText string `json:"new_text"`
|
||||
FetchedAt int64 `json:"fetched_at"`
|
||||
IsEncrypted bool `json:"is_encrypted"`
|
||||
IsPrimed bool `json:"is_primed"`
|
||||
Meta string `json:"meta"`
|
||||
@ -442,6 +444,8 @@ func handlePageUpdate(c *gin.Context) {
|
||||
message = "Locked, must unlock first"
|
||||
} else if p.IsEncrypted {
|
||||
message = "Encrypted, must decrypt first"
|
||||
} else if json.FetchedAt > 0 && p.LastEditUnixTime() > json.FetchedAt {
|
||||
message = "Refusing to overwrite others work"
|
||||
} else {
|
||||
p.Meta = json.Meta
|
||||
p.Update(json.NewText)
|
||||
@ -455,7 +459,7 @@ func handlePageUpdate(c *gin.Context) {
|
||||
message = "Saved"
|
||||
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) {
|
||||
|
8
page.go
8
page.go
@ -27,6 +27,14 @@ type Page struct {
|
||||
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) {
|
||||
p = new(Page)
|
||||
p.Name = name
|
||||
|
@ -182,18 +182,21 @@ body#pad textarea {
|
||||
upload();
|
||||
}, {{ .Debounce }}));
|
||||
|
||||
var lastFetch = {{ .UnixTime }};
|
||||
function upload() {
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: '/update',
|
||||
data: JSON.stringify({
|
||||
new_text: $('#userInput').val(),
|
||||
page: "{{ .Page }}"
|
||||
page: "{{ .Page }}",
|
||||
fetched_at: lastFetch,
|
||||
}),
|
||||
success: function(data) {
|
||||
$('#saveEditButton').removeClass()
|
||||
if (data.success == true) {
|
||||
$('#saveEditButton').addClass("success");
|
||||
lastFetch = data.unix_time;
|
||||
} else {
|
||||
$('#saveEditButton').addClass("failure");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user