-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoveHandlers.go
More file actions
108 lines (104 loc) · 3.16 KB
/
moveHandlers.go
File metadata and controls
108 lines (104 loc) · 3.16 KB
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
package gobookmarks
import (
"fmt"
"github.com/gorilla/sessions"
"golang.org/x/oauth2"
"net/http"
"strconv"
)
func MoveTabAction(w http.ResponseWriter, r *http.Request) error {
from, _ := strconv.Atoi(r.URL.Query().Get("from"))
to, _ := strconv.Atoi(r.URL.Query().Get("to"))
session := r.Context().Value(ContextValues("session")).(*sessions.Session)
githubUser, _ := session.Values["GithubUser"].(*User)
token, _ := session.Values["Token"].(*oauth2.Token)
login := ""
if githubUser != nil {
login = githubUser.Login
}
ref := r.URL.Query().Get("ref")
if ref == "" {
ref = "refs/heads/main"
}
bookmarks, sha, err := GetBookmarks(r.Context(), login, ref, token)
if err != nil {
return fmt.Errorf("GetBookmarks: %w", err)
}
list := ParseBookmarks(bookmarks)
list.MoveTab(from, to)
if err := UpdateBookmarks(r.Context(), login, token, ref, "main", list.String(), sha); err != nil {
return fmt.Errorf("updateBookmarks: %w", err)
}
return nil
}
func MovePageAction(w http.ResponseWriter, r *http.Request) error {
from, _ := strconv.Atoi(r.URL.Query().Get("from"))
to, _ := strconv.Atoi(r.URL.Query().Get("to"))
tabIdx := TabFromRequest(r)
session := r.Context().Value(ContextValues("session")).(*sessions.Session)
githubUser, _ := session.Values["GithubUser"].(*User)
token, _ := session.Values["Token"].(*oauth2.Token)
login := ""
if githubUser != nil {
login = githubUser.Login
}
ref := r.URL.Query().Get("ref")
if ref == "" {
ref = "refs/heads/main"
}
bookmarks, sha, err := GetBookmarks(r.Context(), login, ref, token)
if err != nil {
return fmt.Errorf("GetBookmarks: %w", err)
}
list := ParseBookmarks(bookmarks)
if tabIdx >= 0 && tabIdx < len(list) {
list[tabIdx].MovePage(from, to)
}
if err := UpdateBookmarks(r.Context(), login, token, ref, "main", list.String(), sha); err != nil {
return fmt.Errorf("updateBookmarks: %w", err)
}
return nil
}
func MoveEntryAction(w http.ResponseWriter, r *http.Request) error {
from, _ := strconv.Atoi(r.URL.Query().Get("from"))
to, _ := strconv.Atoi(r.URL.Query().Get("to"))
catIdx, _ := strconv.Atoi(r.URL.Query().Get("category"))
tabIdx := TabFromRequest(r)
pageIdx, _ := strconv.Atoi(r.URL.Query().Get("page"))
session := r.Context().Value(ContextValues("session")).(*sessions.Session)
githubUser, _ := session.Values["GithubUser"].(*User)
token, _ := session.Values["Token"].(*oauth2.Token)
login := ""
if githubUser != nil {
login = githubUser.Login
}
ref := r.URL.Query().Get("ref")
if ref == "" {
ref = "refs/heads/main"
}
bookmarks, sha, err := GetBookmarks(r.Context(), login, ref, token)
if err != nil {
return fmt.Errorf("GetBookmarks: %w", err)
}
list := ParseBookmarks(bookmarks)
if tabIdx >= 0 && tabIdx < len(list) {
t := list[tabIdx]
if pageIdx >= 0 && pageIdx < len(t.Pages) {
page := t.Pages[pageIdx]
for _, blk := range page.Blocks {
for _, col := range blk.Columns {
for _, c := range col.Categories {
if c.Index == catIdx {
c.MoveEntry(from, to)
break
}
}
}
}
}
}
if err := UpdateBookmarks(r.Context(), login, token, ref, "main", list.String(), sha); err != nil {
return fmt.Errorf("updateBookmarks: %w", err)
}
return nil
}