Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
akurilov committed Apr 8, 2024
1 parent ba76021 commit 7cf66d1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
18 changes: 9 additions & 9 deletions service/chats/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (r *reader) runOnce() (err error) {
}
if err == nil {
defer readerAwk.Close()
err = r.deliverEventsReadLoop(ctx, readerAwk, subDescr)
err = r.deliverEventsReadLoop(ctx, readerAwk, r.subId, subDescr)
}
switch {
case errors.Is(err, subscriptions.ErrNotFound):
Expand Down Expand Up @@ -215,10 +215,10 @@ func (r *reader) checkExpiration(groupIdCtx context.Context) {
func (r *reader) deliverEventsReadLoop(
ctx context.Context,
readerAwk model.AckReader[[]*pb.CloudEvent],
subDescr string,
subId, subDescr string,
) (err error) {
for !r.stop {
err = r.deliverEventsRead(ctx, readerAwk, subDescr)
err = r.deliverEventsRead(ctx, readerAwk, subId, subDescr)
if err != nil {
break
}
Expand All @@ -229,15 +229,15 @@ func (r *reader) deliverEventsReadLoop(
func (r *reader) deliverEventsRead(
ctx context.Context,
readerAwk model.AckReader[[]*pb.CloudEvent],
subDescr string,
subId, subDescr string,
) (err error) {
var evts []*pb.CloudEvent
evts, err = readerAwk.Read()
switch status.Code(err) {
case codes.OK:
var countAck uint32
if len(evts) > 0 {
countAck, err = r.deliverEvents(evts, subDescr)
countAck, err = r.deliverEvents(evts, subId, subDescr)
}
_ = readerAwk.Ack(countAck)
if err != nil {
Expand All @@ -258,10 +258,10 @@ func (r *reader) deliverEventsRead(
return err
}

func (r *reader) deliverEvents(evts []*pb.CloudEvent, subDescr string) (countAck uint32, err error) {
func (r *reader) deliverEvents(evts []*pb.CloudEvent, subId, subDescr string) (countAck uint32, err error) {
for _, evt := range evts {
r.rl.Take()
tgMsg := r.format.Convert(evt, subDescr, messages.FormatModeHtml)
tgMsg := r.format.Convert(evt, subId, subDescr, messages.FormatModeHtml)
err = r.tgCtx.Send(tgMsg, telebot.ModeHTML)
if err != nil {
switch err.(type) {
Expand All @@ -275,7 +275,7 @@ func (r *reader) deliverEvents(evts []*pb.CloudEvent, subDescr string) (countAck
return
}
fmt.Printf("Failed to send message %+v to chat %d in HTML mode, cause: %s (%s)\n", tgMsg, r.tgCtx.Chat().ID, err, reflect.TypeOf(err))
tgMsg = r.format.Convert(evt, subDescr, messages.FormatModePlain)
tgMsg = r.format.Convert(evt, subId, subDescr, messages.FormatModePlain)
err = r.tgCtx.Send(tgMsg) // fallback: try to re-send as a plain text
}
}
Expand All @@ -284,7 +284,7 @@ func (r *reader) deliverEvents(evts []*pb.CloudEvent, subDescr string) (countAck
case telebot.FloodError:
default:
fmt.Printf("Failed to send message %+v in plain text mode, cause: %s\n", tgMsg, err)
tgMsg = r.format.Convert(evt, subDescr, messages.FormatModeRaw)
tgMsg = r.format.Convert(evt, subId, subDescr, messages.FormatModeRaw)
err = r.tgCtx.Send(tgMsg) // fallback: try to re-send as a raw text w/o file attachments
}
}
Expand Down
22 changes: 13 additions & 9 deletions service/messages/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var htmlStripTags = bluemonday.
StrictPolicy().
AddSpaceWhenStrippingTag(true)

func (f Format) Convert(evt *pb.CloudEvent, subDescr string, mode FormatMode) (tgMsg any) {
func (f Format) Convert(evt *pb.CloudEvent, subId, subDescr string, mode FormatMode) (tgMsg any) {
fileTypeAttr, fileTypeFound := evt.Attributes[attrKeyFileType]
if fileTypeFound && mode != FormatModeRaw {
ft := FileType(fileTypeAttr.GetCeInteger())
Expand All @@ -42,27 +42,27 @@ func (f Format) Convert(evt *pb.CloudEvent, subDescr string, mode FormatMode) (t
tgMsg = &telebot.Audio{
File: file,
Duration: int(evt.Attributes[attrKeyFileMediaDuration].GetCeInteger()),
Caption: f.convert(evt, subDescr, mode, false, false),
Caption: f.convert(evt, subId, subDescr, mode, false, false),
}
case FileTypeDocument:
tgMsg = &telebot.Document{
File: file,
Caption: f.convert(evt, subDescr, mode, false, false),
Caption: f.convert(evt, subId, subDescr, mode, false, false),
}
case FileTypeImage:
tgMsg = &telebot.Photo{
File: file,
Width: int(evt.Attributes[attrKeyFileImgWidth].GetCeInteger()),
Height: int(evt.Attributes[attrKeyFileImgHeight].GetCeInteger()),
Caption: f.convert(evt, subDescr, mode, false, false),
Caption: f.convert(evt, subId, subDescr, mode, false, false),
}
case FileTypeVideo:
tgMsg = &telebot.Video{
File: file,
Width: int(evt.Attributes[attrKeyFileImgWidth].GetCeInteger()),
Height: int(evt.Attributes[attrKeyFileImgHeight].GetCeInteger()),
Duration: int(evt.Attributes[attrKeyFileMediaDuration].GetCeInteger()),
Caption: f.convert(evt, subDescr, mode, false, false),
Caption: f.convert(evt, subId, subDescr, mode, false, false),
}
}
} else {
Expand All @@ -71,15 +71,15 @@ func (f Format) Convert(evt *pb.CloudEvent, subDescr string, mode FormatMode) (t
case true:
// no need to truncate for telegram when message is from telegram
// no need to convert any other attributes except text and footer
tgMsg = f.convert(evt, subDescr, mode, false, true)
tgMsg = f.convert(evt, subId, subDescr, mode, false, true)
default:
tgMsg = f.convert(evt, subDescr, mode, true, true)
tgMsg = f.convert(evt, subId, subDescr, mode, true, true)
}
}
return
}

func (f Format) convert(evt *pb.CloudEvent, subDescr string, mode FormatMode, trunc, attrs bool) (txt string) {
func (f Format) convert(evt *pb.CloudEvent, subId, subDescr string, mode FormatMode, trunc, attrs bool) (txt string) {
if attrs {
txt += f.convertHeaderAttrs(evt, mode, trunc)
}
Expand Down Expand Up @@ -117,7 +117,11 @@ func (f Format) convert(evt *pb.CloudEvent, subDescr string, mode FormatMode, tr
if txt == "" && attrNameFound {
txt = fmt.Sprintf("%s\n\n", attrName.GetCeString())
}
txt += fmt.Sprintf("Subscription: %s\n\nsource: %s\n", subDescr, evt.Source)
subDetailsLink := "https://awakari.com/sub-details.html?id=" + subId
if mode == FormatModeHtml {
subDetailsLink = "<a href=\"" + subDetailsLink + "\">" + subDescr + "</a>"
}
txt += fmt.Sprintf("Subscription: %s\n\nsource: %s\n", subDetailsLink, evt.Source)
var attrsTxt string
if attrs {
attrsTxt = f.convertExtraAttrs(evt, mode, trunc)
Expand Down

0 comments on commit 7cf66d1

Please sign in to comment.