Skip to content

Commit

Permalink
feat(bot): view full result
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Aug 14, 2022
1 parent 60cc994 commit c1da9e0
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/bot/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Query(driver string, searchOptions map[string]string, query string, pn, ps
}

// todo(iyear): add sortBy options
results := _search.Search(query, &search.Options{
results := _search.Search(query, search.Options{
From: pn * ps,
Size: ps,
SortBy: []search.OptionSortByItem{{
Expand Down
7 changes: 5 additions & 2 deletions app/bot/run/internal/handler/private/search.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package private

import (
"encoding/base64"
"github.com/iyear/searchx/app/bot/run/internal/config"
"github.com/iyear/searchx/app/bot/run/internal/model"
"github.com/iyear/searchx/app/bot/run/internal/util"
"github.com/iyear/searchx/pkg/keygen"
"github.com/iyear/searchx/pkg/models"
"github.com/iyear/searchx/pkg/storage/search"
"github.com/iyear/searchx/pkg/utils"
Expand Down Expand Up @@ -46,7 +48,7 @@ func Search(c tele.Context) error {
prevBtn.Data = searchSetData(keyword, pn-1, order)

// 每次多查一个判断 total%ps==0 的情况
searchResults := sp.Storage.Search.Search(keyword, &search.Options{
searchResults := sp.Storage.Search.Search(keyword, search.Options{
From: pn * ps,
Size: ps + 1,
SortBy: config.SearchOrders[order].SortBy,
Expand Down Expand Up @@ -99,12 +101,13 @@ func Search(c tele.Context) error {

results = append(results, &model.TSearchResult{
Seq: pn*ps + i + 1,
ViewLink: utils.GetDeepLink(c.Bot().Me.Username, base64.URLEncoding.EncodeToString([]byte(keygen.New(msg.Chat, msg.ID)))),
SenderName: html.EscapeString(strings.TrimSpace(sender)),
SenderLink: "tg://user?id=" + msg.Sender,
ChatName: html.EscapeString(utils.SubString(msg.ChatName, config.ChatNameMax)),
Date: utils.MustGetDate(msg.Date).Format("2006.01.02"),
Content: html.EscapeString(strings.Join(append(contents, ""), "...")),
Link: util.GetMsgLink(msg.Chat, msg.ID),
GoLink: util.GetMsgLink(msg.Chat, msg.ID),
})
}

Expand Down
36 changes: 35 additions & 1 deletion app/bot/run/internal/handler/private/start.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package private

import (
"encoding/base64"
"github.com/iyear/searchx/app/bot/run/internal/config"
"github.com/iyear/searchx/app/bot/run/internal/model"
"github.com/iyear/searchx/app/bot/run/internal/util"
"github.com/iyear/searchx/global"
"github.com/iyear/searchx/pkg/models"
"github.com/iyear/searchx/pkg/utils"
"github.com/mitchellh/mapstructure"
tele "gopkg.in/telebot.v3"
"html"
)

func Start(c tele.Context) error {
Expand All @@ -29,5 +34,34 @@ func Start(c tele.Context) error {
})
}

return nil
return messageView(c)
}

func messageView(c tele.Context) error {
sp := util.GetScope(c)

id, err := base64.URLEncoding.DecodeString(c.Message().Payload)
if err != nil {
return err
}

result, err := sp.Storage.Search.Get(string(id))
if err != nil {
return err
}

msg := models.SearchMsg{}
if err = mapstructure.Decode(result.Fields, &msg); err != nil {
return err
}

return c.EditOrSend(sp.Template.Text.Search.View.T(&model.TSearchView{
MsgID: msg.ID,
ChatID: msg.Chat,
ChatName: msg.ChatName,
SenderID: msg.Sender,
SenderName: msg.SenderName,
Date: utils.MustGetDate(msg.Date).Format("2006-01-02 15:04:05"),
Content: html.UnescapeString(msg.Text),
}))
}
1 change: 1 addition & 0 deletions app/bot/run/internal/i18n/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type TemplateText struct {
Search struct {
KeywordsTooLong i18n.Text `mapstructure:"keywords_too_long"`
Results i18n.Text `mapstructure:"results"`
View i18n.Text `mapstructure:"view"`
} `mapstructure:"search"`
AddedToGroup struct {
Fail i18n.Text `mapstructure:"fail"`
Expand Down
13 changes: 12 additions & 1 deletion app/bot/run/internal/model/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@ type TSearchResults struct {

type TSearchResult struct {
Seq int
ViewLink string
SenderName string
SenderLink string
ChatName string
Date string
Content string
Link string
GoLink string
}

type TSearchView struct {
MsgID string
ChatID string
ChatName string
SenderID string
SenderName string
Date string
Content string
}
6 changes: 6 additions & 0 deletions app/usr/run/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package run

import (
"context"
"github.com/google/martian/log"
"github.com/gotd/td/tg"
"github.com/iyear/searchx/app/usr/run/internal/handler/usr"
)
Expand All @@ -11,5 +13,9 @@ func handleUsr(dispatcher *tg.UpdateDispatcher) {
dispatcher.OnNewScheduledMessage(usr.OnNewScheduledMessage)
dispatcher.OnNewChannelMessage(usr.OnNewChannelMessage)
dispatcher.OnEditChannelMessage(usr.OnEditChannelMessage)
dispatcher.OnChannelTooLong(func(ctx context.Context, e tg.Entities, update *tg.UpdateChannelTooLong) error {
log.Infof("channel too long. id: %d, pts: %d", update.ChannelID, update.Pts)
return nil
})
// TODO(iyear): handler delete event?
}
3 changes: 3 additions & 0 deletions app/usr/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ func Run(ctx context.Context, cfg string, _login bool) error {
Logger: zap.NewNop(),
Storage: sto.NewState(kv),
AccessHasher: sto.NewAccessHasher(kv),
OnChannelTooLong: func(channelID int64) {
slog.Errorw("channel is too long", "channelID", channelID)
},
})

c := telegram.NewClient(config.C.Account.ID, config.C.Account.Hash, telegram.Options{
Expand Down
10 changes: 9 additions & 1 deletion config/bot/i18n/en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ keywords_too_long = "⚠ Keywords are too long, please shorten them"
results = """
👉 Search: <b>{{ .Keyword }}</b> 👈
{{ range .Results }}
<b>{{ .Seq }}.</b> <code>{{ .ChatName }}</code> @<code>{{ .SenderName }}</code> - <code>{{ .Date }}</code> <a href="{{ .Link }}">→</a>
<a href="{{ .ViewLink }}">{{ .Seq }}</a> <code>{{ .ChatName }}</code> @<code>{{ .SenderName }}</code> - <code>{{ .Date }}</code> <a href="{{ .GoLink }}">→</a>
| {{ .Content }}
{{ end }}
⏳ Time: <code>{{ .Took }}ms</code>
"""
view = """
Message ID: <code>{{ .MsgID }}</code>
Chat: <code>{{ .ChatName }}</code> | <code>{{ .ChatID }}</code>
Sender: <code>{{ .SenderName }}</code> | <code>{{ .SenderID }}</code>
Time: <code>{{ .Date }}</code>
{{ .Content }}
"""

[text.settings]
desc = "👇 Select your settings item"
Expand Down
10 changes: 9 additions & 1 deletion config/bot/i18n/zh-cn.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ keywords_too_long = "⚠ 关键词过长,请适当缩短关键词"
results = """
👉 搜索: <b>{{ .Keyword }}</b> 👈
{{ range .Results }}
<b>{{ .Seq }}.</b> <code>{{ .ChatName }}</code> @<code>{{ .SenderName }}</code> - <code>{{ .Date }}</code> <a href="{{ .Link }}">→</a>
<a href="{{ .ViewLink }}">{{ .Seq }}</a> <code>{{ .ChatName }}</code> @<code>{{ .SenderName }}</code> - <code>{{ .Date }}</code> <a href="{{ .GoLink }}">→</a>
| {{ .Content }}
{{ end }}
⏳ 本次搜索花费时间: <code>{{ .Took }}ms</code>
"""
view = """
消息ID: <code>{{ .MsgID }}</code>
会话: <code>{{ .ChatName }}</code> | <code>{{ .ChatID }}</code>
发送者: <code>{{ .SenderName }}</code> | <code>{{ .SenderID }}</code>
时间: <code>{{ .Date }}</code>
{{ .Content }}
"""

[text.settings]
desc = "👇 请选择你的设置项"
Expand Down
3 changes: 2 additions & 1 deletion pkg/storage/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (

type Search interface {
Index(items []*search.Item) error
Search(query string, options *search.Options) []*search.Result
Search(query string, options search.Options) []*search.Result
Get(id string) (*search.Result, error)
}

func NewSearch(name string, options map[string]interface{}) (Search, error) {
Expand Down
24 changes: 18 additions & 6 deletions pkg/storage/search/bleve/search.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
package bleve

import (
"fmt"
"github.com/blevesearch/bleve/v2"
"github.com/iyear/searchx/pkg/storage/search"
)

func (b *Bleve) Search(query string, options *search.Options) []*search.Result {
func (b *Bleve) Get(id string) (*search.Result, error) {
s := bleve.NewSearchRequest(bleve.NewDocIDQuery([]string{id}))
results := b.searchReq(s, search.Options{})

if len(results) != 1 {
return nil, fmt.Errorf("get doc failed, id: %s", id)
}

return results[0], nil
}

func (b *Bleve) Search(query string, options search.Options) []*search.Result {
// try query string query
// if not have results, try wildcard query
search := bleve.NewSearchRequestOptions(bleve.NewQueryStringQuery(query), options.Size, options.From, false)
results := b.searchReq(search, options)
s := bleve.NewSearchRequestOptions(bleve.NewQueryStringQuery(query), options.Size, options.From, false)
results := b.searchReq(s, options)
if len(results) > 0 {
return results
}

search = bleve.NewSearchRequestOptions(bleve.NewWildcardQuery("*"+query+"*"), options.Size, options.Size, false)
return b.searchReq(search, options)
s = bleve.NewSearchRequestOptions(bleve.NewWildcardQuery("*"+query+"*"), options.Size, options.Size, false)
return b.searchReq(s, options)
}

func (b *Bleve) searchReq(req *bleve.SearchRequest, options *search.Options) []*search.Result {
func (b *Bleve) searchReq(req *bleve.SearchRequest, options search.Options) []*search.Result {
req.IncludeLocations = true
req.Fields = []string{"*"}

Expand Down
4 changes: 4 additions & 0 deletions pkg/utils/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ func SubString(s string, l int) string {
}
return ss
}

func GetDeepLink(bot string, code string) string {
return "https://t.me/" + bot + "?start=" + code
}

0 comments on commit c1da9e0

Please sign in to comment.