-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package usr | ||
|
||
import ( | ||
"github.com/gotd/td/tg" | ||
"github.com/iyear/searchx/app/usr/run/internal/model" | ||
"github.com/iyear/searchx/app/usr/run/internal/util" | ||
"github.com/iyear/searchx/pkg/keygen" | ||
"github.com/iyear/searchx/pkg/models" | ||
"github.com/iyear/searchx/pkg/storage/search" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func index(sp *model.UsrScope, chatID int64, chatName string, msgID int, senderID int64, senderName string, text string, date int) error { | ||
sp.Log.Debugw("new message", "chatID", chatID, "chatName", chatName, "msgID", msgID, "senderID", senderID, "senderName", senderName, "text", text, "date", date) | ||
|
||
return sp.Storage.Search.Index([]*search.Item{{ | ||
ID: keygen.SearchMsgID(chatID, msgID), | ||
Data: &models.SearchMsg{ | ||
ID: strconv.Itoa(msgID), | ||
Chat: strconv.FormatInt(chatID, 10), | ||
Text: strings.ReplaceAll(text, "\n", " "), | ||
Sender: strconv.FormatInt(senderID, 10), | ||
SenderName: senderName, | ||
Date: strconv.Itoa(date), | ||
}, | ||
}}) | ||
} | ||
|
||
func indexMessage(sp *model.UsrScope, e tg.Entities, msg tg.MessageClass) error { | ||
m, ok := msg.(*tg.Message) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
// get chat info | ||
chatID := util.GetPeerID(m.PeerID) | ||
chatName := util.GetPeerName(m.PeerID, e) | ||
|
||
// get from info | ||
from, ok := m.GetFromID() // judge is from channel | ||
senderID, senderName := chatID, chatName | ||
if ok { | ||
senderID, senderName = util.GetPeerID(from), util.GetPeerName(from, e) | ||
} | ||
|
||
// get date | ||
date, ok := m.GetEditDate() | ||
if !ok { | ||
date = m.Date | ||
} | ||
|
||
return index(sp, chatID, chatName, m.ID, senderID, senderName, messageText(m), date) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package usr | ||
|
||
import ( | ||
"github.com/gotd/td/tg" | ||
"strings" | ||
) | ||
|
||
func messageText(msg *tg.Message) string { | ||
// TODO(iyear): index service messages? | ||
|
||
media, ok := msg.GetMedia() | ||
if ok { | ||
return strings.Join([]string{msg.Message, mediaText(media)}, " ") | ||
} | ||
|
||
return msg.Message | ||
} | ||
|
||
func mediaText(media tg.MessageMediaClass) string { | ||
switch m := media.(type) { | ||
case *tg.MessageMediaDocument: | ||
return mediaDocText(m) | ||
case *tg.MessageMediaWebPage: | ||
return mediaWebPageText(m) | ||
case *tg.MessageMediaVenue: | ||
return mediaVenueText(m) | ||
// case *tg.MessageMediaGame: // TODO(iyear): index media game? | ||
case *tg.MessageMediaInvoice: | ||
return mediaInvoiceText(m) | ||
} | ||
return "" | ||
} | ||
|
||
func mediaDocText(document *tg.MessageMediaDocument) string { | ||
doc, ok := document.Document.(*tg.Document) | ||
if !ok { | ||
return "" | ||
} | ||
|
||
attrs := make([]string, 0) | ||
text := "" | ||
|
||
for _, attr := range doc.Attributes { | ||
switch a := attr.(type) { | ||
case *tg.DocumentAttributeAudio: | ||
text = a.Title | ||
case *tg.DocumentAttributeFilename: | ||
text = a.FileName | ||
} | ||
attrs = append(attrs, text) | ||
} | ||
|
||
return strings.Join(attrs, " ") | ||
} | ||
|
||
func mediaWebPageText(webPage *tg.MessageMediaWebPage) string { | ||
switch wp := webPage.Webpage.(type) { | ||
case *tg.WebPage: | ||
// TODO(iyear): index further content | ||
return strings.Join([]string{wp.SiteName, wp.Title, wp.Description}, " ") | ||
} | ||
return "" | ||
} | ||
|
||
func mediaVenueText(venue *tg.MessageMediaVenue) string { | ||
return strings.Join([]string{venue.Title, venue.Address}, " ") | ||
} | ||
|
||
func mediaInvoiceText(invoice *tg.MessageMediaInvoice) string { | ||
return strings.Join([]string{invoice.Title, invoice.Description}, " ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package util | ||
|
||
import ( | ||
"github.com/gotd/td/tg" | ||
"github.com/iyear/searchx/pkg/utils" | ||
) | ||
|
||
func GetPeerID(peer tg.PeerClass) int64 { | ||
switch p := peer.(type) { | ||
case *tg.PeerUser: | ||
return p.UserID | ||
case *tg.PeerChat: | ||
return p.ChatID | ||
case *tg.PeerChannel: | ||
return p.ChannelID | ||
} | ||
return 0 | ||
} | ||
|
||
func GetPeerName(peer tg.PeerClass, e tg.Entities) string { | ||
switch p := peer.(type) { | ||
case *tg.PeerUser: | ||
u := e.Users[p.UserID] | ||
return utils.GetSenderName(u.FirstName, u.LastName) | ||
case *tg.PeerChat: | ||
return e.Chats[p.ChatID].Title | ||
case *tg.PeerChannel: | ||
return e.Channels[p.ChannelID].Title | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters