Skip to content

Commit

Permalink
feat(usr): add usr handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Aug 11, 2022
1 parent ccc8c29 commit fd77de2
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/usr/run/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ import (

func handleUsr(dispatcher *tg.UpdateDispatcher) {
dispatcher.OnNewMessage(usr.OnNewMessage)
dispatcher.OnEditMessage(usr.OnEditMessage)
dispatcher.OnNewScheduledMessage(usr.OnNewScheduledMessage)
dispatcher.OnNewChannelMessage(usr.OnNewChannelMessage)
dispatcher.OnEditChannelMessage(usr.OnEditChannelMessage)
// TODO(iyear): handler delete event?
}
54 changes: 54 additions & 0 deletions app/usr/run/internal/handler/usr/index.go
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)
}
71 changes: 71 additions & 0 deletions app/usr/run/internal/handler/usr/message.go
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}, " ")
}
19 changes: 18 additions & 1 deletion app/usr/run/internal/handler/usr/on_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,25 @@ package usr
import (
"context"
"github.com/gotd/td/tg"
"github.com/iyear/searchx/app/usr/run/internal/util"
)

func OnNewMessage(ctx context.Context, e tg.Entities, update *tg.UpdateNewMessage) error {
return nil
return indexMessage(util.GetUsrScope(ctx), e, update.Message)
}

func OnEditMessage(ctx context.Context, e tg.Entities, update *tg.UpdateEditMessage) error {
return indexMessage(util.GetUsrScope(ctx), e, update.Message)
}

func OnNewScheduledMessage(ctx context.Context, e tg.Entities, update *tg.UpdateNewScheduledMessage) error {
return indexMessage(util.GetUsrScope(ctx), e, update.Message)
}

func OnNewChannelMessage(ctx context.Context, e tg.Entities, update *tg.UpdateNewChannelMessage) error {
return indexMessage(util.GetUsrScope(ctx), e, update.Message)
}

func OnEditChannelMessage(ctx context.Context, e tg.Entities, update *tg.UpdateEditChannelMessage) error {
return indexMessage(util.GetUsrScope(ctx), e, update.Message)
}
31 changes: 31 additions & 0 deletions app/usr/run/internal/util/telegram.go
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 ""
}
2 changes: 2 additions & 0 deletions app/usr/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ func Run(ctx context.Context, cfg string, _login bool) error {

// init usr
dispatcher := tg.NewUpdateDispatcher()
handleUsr(&dispatcher)

gaps := updates.New(updates.Config{
Handler: dispatcher,
Logger: zap.NewNop(),
Expand Down

0 comments on commit fd77de2

Please sign in to comment.