Skip to content

Commit

Permalink
feat!(prj): add view message and modify search msg indexing model
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Aug 15, 2022
1 parent faabe6f commit cb66747
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 73 deletions.
27 changes: 16 additions & 11 deletions app/bot/run/internal/handler/channel/on_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,28 @@ import (
"github.com/iyear/searchx/pkg/storage/search"
"github.com/iyear/searchx/pkg/utils"
tele "gopkg.in/telebot.v3"
"strconv"
"time"
)

func Index(c tele.Context) error {
m := c.Message()

msg := &models.SearchMsg{
ID: m.ID,
Chat: m.Chat.ID,
ChatName: m.Chat.Title,
Text: c.Text(),
Sender: m.SenderChat.ID,
SenderName: utils.Telegram.GetSenderName(m.SenderChat.FirstName, m.SenderChat.LastName),
Date: time.Now().Unix(),
}
data, err := msg.Encode()
if err != nil {
return err
}

return util.GetScope(c).Storage.Search.Index([]*search.Item{{
ID: keygen.SearchMsgID(m.Chat.ID, m.ID),
Data: &models.SearchMsg{
ID: strconv.Itoa(m.ID),
Chat: strconv.FormatInt(m.Chat.ID, 10),
ChatName: m.Chat.Title,
Text: c.Text(),
Sender: m.SenderChat.Recipient(),
SenderName: utils.Telegram.GetSenderName(m.SenderChat.FirstName, m.SenderChat.LastName),
Date: strconv.FormatInt(time.Now().Unix(), 10),
}},
ID: keygen.SearchMsgID(m.Chat.ID, m.ID),
Data: data},
})
}
33 changes: 17 additions & 16 deletions app/bot/run/internal/handler/group/on_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/iyear/searchx/pkg/storage/search"
"github.com/iyear/searchx/pkg/utils"
tele "gopkg.in/telebot.v3"
"strconv"
"strings"
)

Expand Down Expand Up @@ -47,21 +46,23 @@ func index(c tele.Context, text string) error {
date = msg.Unixtime
}

m := &models.SearchMsg{
ID: msg.ID,
Chat: msg.Chat.ID,
ChatName: msg.Chat.Title,
Text: strings.ReplaceAll(text, "\n", " "),
Sender: msg.Sender.ID,
SenderName: utils.Telegram.GetSenderName(msg.Sender.FirstName, msg.Sender.LastName),
Date: date,
}

data, err := m.Encode()
if err != nil {
return err
}

return util.GetScope(c).Storage.Search.Index([]*search.Item{{
ID: keygen.SearchMsgID(msg.Chat.ID, msg.ID),
Data: &models.SearchMsg{
ID: strconv.Itoa(msg.ID),
Chat: msg.Chat.Recipient(),
ChatName: msg.Chat.Title,
Text: strings.ReplaceAll(text, "\n", " "),
Sender: msg.Sender.Recipient(),
SenderName: utils.Telegram.GetSenderName(msg.Sender.FirstName, msg.Sender.LastName),
Date: strconv.FormatInt(date, 10),
},
ID: keygen.SearchMsgID(msg.Chat.ID, msg.ID),
Data: data,
}})
}

func OnUserJoined(c tele.Context) error {
// u := c.ChatMember().NewChatMember.User
return nil
}
10 changes: 5 additions & 5 deletions app/bot/run/internal/handler/private/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func Search(c tele.Context) error {
msg := models.SearchMsg{}
for i := 0; i < num; i++ {
result := searchResults[i]
if err := mapstructure.Decode(result.Fields, &msg); err != nil {
if err := mapstructure.WeakDecode(result.Fields, &msg); err != nil {
return err
}

Expand All @@ -96,16 +96,16 @@ func Search(c tele.Context) error {

sender := utils.String.RuneSubString(msg.SenderName, config.SenderNameMax)
if sender == "" {
sender = msg.Sender
sender = strconv.FormatInt(msg.Sender, 10)
}

results = append(results, &model.TSearchResult{
Seq: pn*ps + i + 1,
ViewLink: utils.Telegram.GetDeepLink(c.Bot().Me.Username, base64.URLEncoding.EncodeToString([]byte(keygen.New(msg.Chat, msg.ID)))),
ViewLink: utils.Telegram.GetDeepLink(c.Bot().Me.Username, base64.URLEncoding.EncodeToString([]byte(keygen.SearchMsgID(msg.Chat, msg.ID)))),
SenderName: html.EscapeString(strings.TrimSpace(sender)),
SenderLink: "tg://user?id=" + msg.Sender,
SenderLink: "tg://user?id=" + strconv.FormatInt(msg.Sender, 10),
ChatName: html.EscapeString(utils.String.RuneSubString(msg.ChatName, config.ChatNameMax)),
Date: utils.String.MustGetDate(msg.Date).Format("2006.01.02"),
Date: time.Unix(msg.Date, 0).Format("2006.01.02"),
Content: html.EscapeString(strings.Join(append(contents, ""), "...")),
GoLink: util.GetMsgLink(msg.Chat, msg.ID),
})
Expand Down
6 changes: 3 additions & 3 deletions app/bot/run/internal/handler/private/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"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"
"time"
)

func Start(c tele.Context) error {
Expand Down Expand Up @@ -51,7 +51,7 @@ func messageView(c tele.Context) error {
}

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

Expand All @@ -61,7 +61,7 @@ func messageView(c tele.Context) error {
ChatName: msg.ChatName,
SenderID: msg.Sender,
SenderName: msg.SenderName,
Date: utils.String.MustGetDate(msg.Date).Format("2006-01-02 15:04:05"),
Date: time.Unix(msg.Date, 0).Format("2006-01-02 15:04:05"),
Content: html.UnescapeString(msg.Text),
}))
}
6 changes: 3 additions & 3 deletions app/bot/run/internal/model/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ type TSearchResult struct {
}

type TSearchView struct {
MsgID string
ChatID string
MsgID int
ChatID int64
ChatName string
SenderID string
SenderID int64
SenderName string
Date string
Content string
Expand Down
7 changes: 2 additions & 5 deletions app/bot/run/internal/util/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package util
import (
"fmt"
tele "gopkg.in/telebot.v3"
"strconv"
)

func appendBack(back tele.InlineButton, opts ...interface{}) []interface{} {
Expand Down Expand Up @@ -42,8 +41,6 @@ func EditOrSendWithBack(c tele.Context, what interface{}, opts ...interface{}) e
return doWithBack(c, c.EditOrSend, what, opts...)
}

func GetMsgLink(chat string, msg string) string {
// super group and channel
c, _ := strconv.ParseInt(chat, 10, 64)
return fmt.Sprintf("https://t.me/c/%d/%s", (-c)-1e12, msg)
func GetMsgLink(chat int64, msg int) string {
return fmt.Sprintf("https://t.me/c/%d/%d", (-chat)-1e12, msg)
}
38 changes: 26 additions & 12 deletions app/bot/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,35 @@ func index(src string, chatID int64, chatName string, _search storage.Search) er
continue
}

sender := strings.TrimPrefix(msg.FromID, "user")
sender = strings.TrimPrefix(sender, "channel")
senderStr := strings.TrimPrefix(msg.FromID, "user")
senderStr = strings.TrimPrefix(senderStr, "channel")
sender, err := strconv.ParseInt(senderStr, 10, 64)
if err != nil {
return err
}

_time, err := strconv.ParseInt(msg.Time, 10, 64)
if err != nil {
return err
}

if text != "" {
m := &models.SearchMsg{
ID: msg.ID,
Chat: chatID,
ChatName: chatName,
Text: strings.ReplaceAll(text, "\n", " "),
Sender: sender,
SenderName: msg.From,
Date: _time,
}
data, err := m.Encode()
if err != nil {
return err
}
items = append(items, &search.Item{
ID: keygen.SearchMsgID(chatID, msg.ID),
Data: &models.SearchMsg{
ID: strconv.Itoa(msg.ID),
Chat: strconv.FormatInt(chatID, 10),
ChatName: chatName,
Text: strings.ReplaceAll(text, "\n", " "),
Sender: sender,
SenderName: msg.From,
Date: msg.Time,
},
ID: keygen.SearchMsgID(chatID, msg.ID),
Data: data,
})
}

Expand Down
26 changes: 15 additions & 11 deletions app/usr/run/internal/handler/usr/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/iyear/searchx/pkg/keygen"
"github.com/iyear/searchx/pkg/models"
"github.com/iyear/searchx/pkg/storage/search"
"strconv"
"strings"
)

Expand All @@ -21,17 +20,22 @@ func index(sp *model.UsrScope, chatID int64, chatName string, msgID int, senderI

sp.Log.Debugw("new message", "chatID", chatID, "chatName", chatName, "msgID", msgID, "senderID", senderID, "senderName", senderName, "text", text, "date", date)

msg := &models.SearchMsg{
ID: msgID,
Chat: chatID,
ChatName: chatName,
Text: strings.ReplaceAll(text, "\n", " "),
Sender: senderID,
SenderName: senderName,
Date: int64(date),
}
data, err := msg.Encode()
if err != nil {
return err
}
return sp.Storage.Search.Index([]*search.Item{{
ID: keygen.SearchMsgID(chatID, msgID),
Data: &models.SearchMsg{
ID: strconv.Itoa(msgID),
Chat: strconv.FormatInt(chatID, 10),
ChatName: chatName,
Text: strings.ReplaceAll(text, "\n", " "),
Sender: strconv.FormatInt(senderID, 10),
SenderName: senderName,
Date: strconv.Itoa(date),
},
ID: keygen.SearchMsgID(chatID, msgID),
Data: data,
}})
}

Expand Down
32 changes: 25 additions & 7 deletions pkg/models/search.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
package models

import (
"encoding/json"
)

type SearchMsg struct {
ID string `json:"id" mapstructure:"id"` // message id
Chat string `json:"chat" mapstructure:"chat"` // chat id
ChatName string `json:"chat_name" mapstructure:"chat_name"` // chat name
Text string `json:"text" mapstructure:"text"` // text content
Sender string `json:"sender" mapstructure:"sender"` // sender id
SenderName string `json:"sender_name" mapstructure:"sender_name"` // sender name
Date string `json:"date" mapstructure:"date"` // unix timestamp
ID int `json:"id,string" mapstructure:"id" index:"id"` // message id
Chat int64 `json:"chat,string" mapstructure:"chat" index:"chat"` // chat id
ChatName string `json:"chat_name" mapstructure:"chat_name" index:"chat_name"` // chat name
Text string `json:"text" mapstructure:"text" index:"text"` // text content
Sender int64 `json:"sender,string" mapstructure:"sender" index:"sender"` // sender id
SenderName string `json:"sender_name" mapstructure:"sender_name" index:"sender_name"` // sender name
Date int64 `json:"date,string" mapstructure:"date" index:"date"` // unix timestamp
}

func (m *SearchMsg) Encode() (map[string]string, error) {
b, err := json.Marshal(m)
if err != nil {
return nil, err
}

mm := make(map[string]string)
if err = json.Unmarshal(b, &mm); err != nil {
return nil, err
}

return mm, nil
}
1 change: 1 addition & 0 deletions pkg/storage/search/bleve/bleve.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func New(options map[string]interface{}) (*Bleve, error) {
}

mapping.DefaultAnalyzer = jieba
mapping.DefaultMapping.StructTagKey = "index"

var index bleve.Index

Expand Down

0 comments on commit cb66747

Please sign in to comment.