diff --git a/app/bot/query/query.go b/app/bot/query/query.go index 205417e..413b2a9 100644 --- a/app/bot/query/query.go +++ b/app/bot/query/query.go @@ -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{{ diff --git a/app/bot/run/internal/handler/private/search.go b/app/bot/run/internal/handler/private/search.go index 56be00f..42bde82 100644 --- a/app/bot/run/internal/handler/private/search.go +++ b/app/bot/run/internal/handler/private/search.go @@ -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" @@ -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, @@ -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), }) } diff --git a/app/bot/run/internal/handler/private/start.go b/app/bot/run/internal/handler/private/start.go index 93f1269..27b0bf2 100644 --- a/app/bot/run/internal/handler/private/start.go +++ b/app/bot/run/internal/handler/private/start.go @@ -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 { @@ -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), + })) } diff --git a/app/bot/run/internal/i18n/template.go b/app/bot/run/internal/i18n/template.go index ff16565..b22fe17 100644 --- a/app/bot/run/internal/i18n/template.go +++ b/app/bot/run/internal/i18n/template.go @@ -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"` diff --git a/app/bot/run/internal/model/template.go b/app/bot/run/internal/model/template.go index 34d6e21..5475bd9 100644 --- a/app/bot/run/internal/model/template.go +++ b/app/bot/run/internal/model/template.go @@ -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 } diff --git a/app/usr/run/handler.go b/app/usr/run/handler.go index 42e9156..8649b48 100644 --- a/app/usr/run/handler.go +++ b/app/usr/run/handler.go @@ -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" ) @@ -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? } diff --git a/app/usr/run/run.go b/app/usr/run/run.go index b11e503..b352d27 100644 --- a/app/usr/run/run.go +++ b/app/usr/run/run.go @@ -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{ diff --git a/config/bot/i18n/en.toml b/config/bot/i18n/en.toml index c3e3a2d..de97dae 100644 --- a/config/bot/i18n/en.toml +++ b/config/bot/i18n/en.toml @@ -17,12 +17,20 @@ keywords_too_long = "⚠ Keywords are too long, please shorten them" results = """ 👉 Search: {{ .Keyword }} 👈 {{ range .Results }} -{{ .Seq }}. {{ .ChatName }} @{{ .SenderName }} - {{ .Date }} +{{ .Seq }} {{ .ChatName }} @{{ .SenderName }} - {{ .Date }} | {{ .Content }} {{ end }} ⏳ Time: {{ .Took }}ms """ +view = """ +Message ID: {{ .MsgID }} +Chat: {{ .ChatName }} | {{ .ChatID }} +Sender: {{ .SenderName }} | {{ .SenderID }} +Time: {{ .Date }} + +{{ .Content }} +""" [text.settings] desc = "👇 Select your settings item" diff --git a/config/bot/i18n/zh-cn.toml b/config/bot/i18n/zh-cn.toml index 5755f58..7d81b63 100644 --- a/config/bot/i18n/zh-cn.toml +++ b/config/bot/i18n/zh-cn.toml @@ -18,12 +18,20 @@ keywords_too_long = "⚠ 关键词过长,请适当缩短关键词" results = """ 👉 搜索: {{ .Keyword }} 👈 {{ range .Results }} -{{ .Seq }}. {{ .ChatName }} @{{ .SenderName }} - {{ .Date }} +{{ .Seq }} {{ .ChatName }} @{{ .SenderName }} - {{ .Date }} | {{ .Content }} {{ end }} ⏳ 本次搜索花费时间: {{ .Took }}ms """ +view = """ +消息ID: {{ .MsgID }} +会话: {{ .ChatName }} | {{ .ChatID }} +发送者: {{ .SenderName }} | {{ .SenderID }} +时间: {{ .Date }} + +{{ .Content }} +""" [text.settings] desc = "👇 请选择你的设置项" diff --git a/pkg/storage/search.go b/pkg/storage/search.go index e9cc49b..c6fd099 100644 --- a/pkg/storage/search.go +++ b/pkg/storage/search.go @@ -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) { diff --git a/pkg/storage/search/bleve/search.go b/pkg/storage/search/bleve/search.go index b0a96a0..a035865 100644 --- a/pkg/storage/search/bleve/search.go +++ b/pkg/storage/search/bleve/search.go @@ -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{"*"} diff --git a/pkg/utils/string.go b/pkg/utils/string.go index f45c5c2..54eb405 100644 --- a/pkg/utils/string.go +++ b/pkg/utils/string.go @@ -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 +}