Skip to content

Commit

Permalink
Fix telegram message layout
Browse files Browse the repository at this point in the history
  • Loading branch information
web-flow committed Dec 8, 2020
1 parent a5b0997 commit 693a0f1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.5.4] - 2020-12-08

### Fixed
- Fix telegram message layout.

## [0.5.3] - 2020-12-03

### Fixed
Expand Down
35 changes: 18 additions & 17 deletions service/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func (t *telegram) Serve(_ context.Context) error {
}

g.Go(func() error {
col := &collect{}
msgID, err := col.archive(t, update.Message.MessageID, urls)
col, msgID, err := archive(t, update.Message.MessageID, urls)
if err != nil {
logger.Error("Telegram: archiving failed, ", err)
return err
Expand All @@ -97,13 +96,12 @@ func (t *telegram) Serve(_ context.Context) error {
}

type collect struct {
Arc []string
Dst []map[string]string
Arc string
Dst map[string]string
}

func (c *collect) archive(t *telegram, msgid int, urls []string) (int, error) {
func archive(t *telegram, msgid int, urls []string) (col []*collect, id int, err error) {
logger.Debug("Telegram: archives start...")
p := *c

wg := sync.WaitGroup{}
var wbrc wayback.Broker = &wayback.Handle{URLs: urls, Opts: t.opts}
Expand All @@ -114,44 +112,47 @@ func (c *collect) archive(t *telegram, msgid int, urls []string) (int, error) {
wg.Add(1)
go func(slot string) {
defer wg.Done()
c := &collect{}
switch slot {
case config.SLOT_IA:
logger.Debug("Telegram: archiving slot: %s", slot)
p.Dst = append(p.Dst, wbrc.IA())
p.Arc = append(p.Arc, fmt.Sprintf("<a href='https://web.archive.org/'>%s</a>", config.SlotName(slot)))
c.Arc = fmt.Sprintf("<a href='https://web.archive.org/'>%s</a>", config.SlotName(slot))
c.Dst = wbrc.IA()
case config.SLOT_IS:
logger.Debug("Telegram: archiving slot: %s", slot)
p.Dst = append(p.Dst, wbrc.IS())
p.Arc = append(p.Arc, fmt.Sprintf("<a href='https://archive.today/'>%s</a>", config.SlotName(slot)))
c.Arc = fmt.Sprintf("<a href='https://archive.today/'>%s</a>", config.SlotName(slot))
c.Dst = wbrc.IS()
case config.SLOT_IP:
logger.Debug("Telegram: archiving slot: %s", slot)
p.Dst = append(p.Dst, wbrc.IP())
p.Arc = append(p.Arc, fmt.Sprintf("<a href='https://ipfs.github.io/public-gateway-checker/'>%s</a>", config.SlotName(slot)))
c.Arc = fmt.Sprintf("<a href='https://ipfs.github.io/public-gateway-checker/'>%s</a>", config.SlotName(slot))
c.Dst = wbrc.IP()
}
col = append(col, c)
}(slot)
}
wg.Wait()
*c = p

return msgid, nil
return col, msgid, nil
}

func render(vars *collect) string {
func render(vars []*collect) string {
var tmplBytes bytes.Buffer

const tmpl = `{{range $i, $name := .Arc}}<b>{{ $name }}</b>:
{{ range $url := index $.Dst $i -}}
const tmpl = `{{range $ := .}}<b>{{ $.Arc }}</b>:
{{ range $url := $.Dst -}}
• {{ $url }}
{{end}}
{{end}}`

tpl, err := template.New("message").Parse(tmpl)
if err != nil {
logger.Debug("Telegram: parse template failed, %v", err)
return ""
}

err = tpl.Execute(&tmplBytes, vars)
if err != nil {
logger.Debug("Telegram: execute template failed, %v", err)
return ""
}

Expand Down

0 comments on commit 693a0f1

Please sign in to comment.