From 318623577ca497bb5153938f5b5d00ac45d3ab73 Mon Sep 17 00:00:00 2001 From: o98k-ok Date: Fri, 30 Dec 2022 18:23:02 +0800 Subject: [PATCH] [feat]: order by view ts and support config session Signed-off-by: o98k-ok --- doc/feishu.go | 28 ++++++++++++++++++++-------- go.mod | 2 +- go.sum | 4 ++-- main.go | 18 +++++++++++++++--- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/doc/feishu.go b/doc/feishu.go index d55003d..5f40dfb 100644 --- a/doc/feishu.go +++ b/doc/feishu.go @@ -12,10 +12,11 @@ import ( ) type Lark struct { - Offset int - Count int - URL string - Client *http.Client + Offset int + Count int + URL string + Client *http.Client + Session *string } func NewLark() *Lark { @@ -33,6 +34,11 @@ func (l *Lark) WithPage(offset, count int) *Lark { return l } +func (l *Lark) CustomSession(session string) *Lark { + l.Session = &session + return l +} + type Artitle struct { Title string `json:"title"` Preview string `json:"preview"` @@ -59,12 +65,18 @@ type ArtitleResp struct { func (l Lark) Query(query string) ([]Entity, error) { offset, count := strconv.FormatInt(int64(l.Offset), 10), strconv.FormatInt(int64(l.Count), 10) - sessions := cookie.NewKooky("feishu.cn", "session").Filter() - if len(sessions) == 0 { - return nil, errors.New("no fuch session") + var session cookie.CookieItem + if l.Session == nil || len(*l.Session) == 0 { + sessions := cookie.NewKooky("feishu.cn", "session").Filter() + if len(sessions) == 0 { + return nil, errors.New("no fuch session") + } + session = sessions[0] + } else { + session.Name = "session" + session.Value = *l.Session } - session := sessions[0] resp, err := np.NewRequest(l.Client, l.URL). AddParam("query", query).AddParam("offset", offset).AddParam("count", count). AddHeader("cookie", fmt.Sprintf("%s=%s;", session.Name, session.Value)).Do() diff --git a/go.mod b/go.mod index 1704d61..22c7f37 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/o98k-ok/aggregation go 1.18 require ( - github.com/o98k-ok/lazy v0.2.17 + github.com/o98k-ok/lazy v0.2.18 github.com/zellyn/kooky v0.0.0-20221025221128-3e66d684c4db ) diff --git a/go.sum b/go.sum index 4f1c36b..faede15 100644 --- a/go.sum +++ b/go.sum @@ -34,8 +34,8 @@ github.com/keybase/go-keychain v0.0.0-20220408132150-ad3b4a8fd4a7 h1:ttxQhWhqiYE github.com/keybase/go-keychain v0.0.0-20220408132150-ad3b4a8fd4a7/go.mod h1:enrU/ug069Om7vWxuFE6nikLI2BZNwevMiGSo43Kt5w= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= -github.com/o98k-ok/lazy v0.2.17 h1:U4+KVuY1Wk0jWT23dRIWMENq3+ZXTx537/S5MMBSX34= -github.com/o98k-ok/lazy v0.2.17/go.mod h1:bFqS0Mpk2K3JZvBkb3O8QSHXJ/U65nNmXrn1EJlVCtA= +github.com/o98k-ok/lazy v0.2.18 h1:G7FhhupyD21rsCf9rHUT94yKQrmPWI2umN6ZI9EWzBc= +github.com/o98k-ok/lazy v0.2.18/go.mod h1:bFqS0Mpk2K3JZvBkb3O8QSHXJ/U65nNmXrn1EJlVCtA= github.com/o98k-ok/pcurl v0.0.0-20220411095241-e8aed39adb6d h1:kGyMItE5ShkTYvmgq1VaEIwckR+C3t7N6NGvqhqnNCk= github.com/o98k-ok/pcurl v0.0.0-20220411095241-e8aed39adb6d/go.mod h1:/jSLI/84xMBQHFJZ4CKmEIfRaf3YXVdEH2ZXQ51yzq4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/main.go b/main.go index 6ccf2fb..32a7e63 100644 --- a/main.go +++ b/main.go @@ -37,9 +37,15 @@ func intro(entity doc.Entity) string { func entry() { cli := alfred.NewApp("lark doc search plugin") cli.Bind("query", func(s []string) { - params := strings.Join(s, " ") + env, err := alfred.GetFlowEnv() + if err != nil { + alfred.ErrItems("alfred get envs failed", err).Show() + return + } - entities, err := doc.NewLark().Query(params) + cli := doc.NewLark().CustomSession(env.GetAsString("session", "")).WithPage(0, env.GetAsInt("count", 9)) + params := strings.Join(s, " ") + entities, err := cli.Query(params) if err != nil { alfred.ErrItems("query lark failed", err) return @@ -47,8 +53,14 @@ func entry() { msg := alfred.NewItems() for _, entity := range entities { - msg.Append(alfred.NewItem(title(entity), intro(entity), entity.Url)) + item := alfred.NewItem(title(entity), intro(entity), entity.Url) + item.Extra = entity.ViewTS + msg.Append(item) } + + msg.Order(func(l, r *alfred.Item) bool { + return l.Extra.(uint32) > r.Extra.(uint32) + }) msg.Show() })