all repos — telebonk @ 35e839782be9df360365efb527770615965184c0

reposter from honk to telegram

improve comments and compact if-statements
la-ninpre leobrekalini@gmail.com
Mon, 17 Oct 2022 12:41:20 +0300
commit

35e839782be9df360365efb527770615965184c0

parent

ca7eca93e8e9ff2f8f448b34de61471eae48fc70

1 files changed, 31 insertions(+), 33 deletions(-)

jump to
M main.gomain.go

@@ -52,28 +52,28 @@ }

var config = &Config{} -// A Honk is a honk's honk object with most fields omitted. +// A Honk is a post from honk. type Honk struct { - ID int - What string // to filter out honkbacks - Oondle string - Oonker string - XID string - RID string - Date time.Time - Precis string // danger zone and all that - Noise string - Onts []string // to filter out #notg - Donks []*Donk + ID int // unique id of a post + What string // type of an action (post, repost, reply) + Oondle string // e-mail style handle of the original author of a post + Oonker string // url of the original author of a post + XID string // url of a post, also unique + RID string // url of a post that current post is replying to + Date time.Time // datetime of a post + Precis string // post summary + Noise string // contents of a post + Onts []string // a slice of tags in a post + Donks []*Donk // a slice of attachments to a post - messID int - replyToID int + messID int // telegram message_id + replyToID int // telegram message_id of a message to reply to } // A Donk stores metadata for media files. type Donk struct { URL string - Media string + Media string // mime-type of an attachment Desc string }

@@ -128,12 +128,12 @@ }

return honkSend } -// save records Honk to a honkMap +// save records a Honk to a honkMap func (h *Honk) save(t *TelegramResponse) { honkMap[h.XID] = honkInfo{h: h, t: t} } -// forget removes Honk from a honkMap +// forget removes a Honk from a honkMap func (h *Honk) forget() { delete(honkMap, h.XID) }

@@ -154,14 +154,14 @@ ChatID string `json:"chat_id"`

MessageID int `json:"message_id,omitempty"` ReplyToMessageID int `json:"reply_to_message_id,omitempty"` - // Donks - Document string `json:"document,omitempty"` - Photo string `json:"photo,omitempty"` - Caption string `json:"caption,omitempty"` - kind messKind // messHonk, messDonkPht or messDonkDoc + Document string `json:"document,omitempty"` + Photo string `json:"photo,omitempty"` + Caption string `json:"caption,omitempty"` + + kind messKind } -// A TelegramResponse is for handling possible errors with Telegram API. +// A TelegramResponse is a response from Telegram API. type TelegramResponse struct { Ok bool Description string

@@ -304,6 +304,7 @@ }

return nil } +// Telegram Bot API methods const ( tgGetMe = "getMe" tgSendMessage = "sendMessage"

@@ -340,7 +341,7 @@ return nil, err

} honks := honkJunk["honks"] - // honk.ID monotonically increases, so it can be used to sort them + // honk.ID monotonically increases, so it can be used to sort honks sort.Slice(honks, func(i, j int) bool { return honks[i].ID < honks[j].ID }) return honks, nil

@@ -362,7 +363,7 @@ )

const emptyNoise = "<p></p>\n" -// calmNoise erases htm tags that are not supported by Telegram. +// calmNoise erases and rewrites html tags that are not supported by Telegram. func calmNoise(noise string) string { // TODO: check length of a honk

@@ -392,12 +393,11 @@ flag.StringVar(&config.HonkURL, "honk_url", "", "URL of a Honk instance")

flag.Parse() - err := config.Check() - if err != nil { + if err := config.Check(); err != nil { log.Fatal("config:", err) // fail } - err = checkTgAPI() - if err != nil { + + if err := checkTgAPI(); err != nil { log.Fatal(err) // fail } }

@@ -428,8 +428,7 @@ switch action {

case honkIgnore: continue case honkSend, honkEdit: - err := honk.Check() - if err != nil { + if err := honk.Check(); err != nil { log.Print(err) // error continue }

@@ -445,8 +444,7 @@ }

// remember only the first mess' response honk.save(resp) for _, mess := range messes[1:] { - _, err := mess.Send() - if err != nil { + if _, err := mess.Send(); err != nil { log.Print(err) // error continue HonkLoop }