Browse Source

Make hooks a bit more modular

master
Jan-Lukas Else 2 months ago
parent
commit
8332d7ee43
6 changed files with 71 additions and 31 deletions
  1. +18
    -12
      activityPub.go
  2. +17
    -11
      hooks.go
  3. +5
    -6
      main.go
  4. +4
    -0
      postsFuncs.go
  5. +19
    -2
      telegram.go
  6. +8
    -0
      webmention.go

+ 18
- 12
activityPub.go View File

@ -29,6 +29,24 @@ var (
)
func initActivityPub() error {
if !appConfig.ActivityPub.Enabled {
return nil
}
// Add hooks
postHooks[postPostHook] = append(postHooks[postPostHook], func(p *post) {
if p.isPublishedSectionPost() {
p.apPost()
}
})
postHooks[postUpdateHook] = append(postHooks[postUpdateHook], func(p *post) {
if p.isPublishedSectionPost() {
p.apUpdate()
}
})
postHooks[postDeleteHook] = append(postHooks[postDeleteHook], func(p *post) {
p.apDelete()
})
// Read key and prepare signing
pkfile, err := ioutil.ReadFile(appConfig.ActivityPub.KeyPath)
if err != nil {
return err
@ -266,9 +284,6 @@ func apRemoveFollower(blog, follower string) error {
}
func (p *post) apPost() {
if !appConfig.ActivityPub.Enabled {
return
}
n := p.toASNote()
createActivity := make(map[string]interface{})
createActivity["@context"] = asContext
@ -286,9 +301,6 @@ func (p *post) apPost() {
}
func (p *post) apUpdate() {
if !appConfig.ActivityPub.Enabled {
return
}
n := p.toASNote()
updateActivity := make(map[string]interface{})
updateActivity["@context"] = asContext
@ -301,9 +313,6 @@ func (p *post) apUpdate() {
}
func (p *post) apAnnounce() {
if !appConfig.ActivityPub.Enabled {
return
}
announceActivity := make(map[string]interface{})
announceActivity["@context"] = asContext
announceActivity["actor"] = appConfig.Blogs[p.Blog].apIri()
@ -315,9 +324,6 @@ func (p *post) apAnnounce() {
}
func (p *post) apDelete() {
if !appConfig.ActivityPub.Enabled {
return
}
deleteActivity := make(map[string]interface{})
deleteActivity["@context"] = asContext
deleteActivity["actor"] = appConfig.Blogs[p.Blog].apIri()


+ 17
- 11
hooks.go View File

@ -17,6 +17,16 @@ func preStartHooks() {
}
}
type postHookType string
const (
postPostHook postHookType = "post"
postUpdateHook postHookType = "update"
postDeleteHook postHookType = "delete"
)
var postHooks = map[postHookType][]func(*post){}
func (p *post) postPostHooks() {
for _, cmdTmplString := range appConfig.Hooks.PostPost {
go func(p *post, cmdTmplString string) {
@ -26,12 +36,9 @@ func (p *post) postPostHooks() {
})
}(p, cmdTmplString)
}
if p.Published != "" && p.Section != "" {
// It's a published post
go p.apPost()
go p.tgPost()
for _, f := range postHooks[postPostHook] {
go f(p)
}
go p.sendWebmentions()
}
func (p *post) postUpdateHooks() {
@ -43,11 +50,9 @@ func (p *post) postUpdateHooks() {
})
}(p, cmdTmplString)
}
if p.Published != "" && p.Section != "" {
// It's a published post
go p.apUpdate()
for _, f := range postHooks[postUpdateHook] {
go f(p)
}
go p.sendWebmentions()
}
func (p *post) postDeleteHooks() {
@ -59,8 +64,9 @@ func (p *post) postDeleteHooks() {
})
}(p, cmdTmplString)
}
go p.apDelete()
go p.sendWebmentions()
for _, f := range postHooks[postDeleteHook] {
go f(p)
}
}
type hookTemplateData struct {


+ 5
- 6
main.go View File

@ -49,13 +49,12 @@ func main() {
log.Fatal(err)
return
}
if appConfig.ActivityPub.Enabled {
err = initActivityPub()
if err != nil {
log.Fatal(err)
return
}
err = initActivityPub()
if err != nil {
log.Fatal(err)
return
}
initTelegram()
initWebmention()
initCache()
initNodeInfo()


+ 4
- 0
postsFuncs.go View File

@ -77,3 +77,7 @@ func (p *post) translations() []*post {
}
return translations
}
func (p *post) isPublishedSectionPost() bool {
return p.Published != "" && p.Section != ""
}

+ 19
- 2
telegram.go View File

@ -9,8 +9,25 @@ import (
const telegramBaseURL = "https://api.telegram.org/bot"
func initTelegram() {
enable := false
for _, b := range appConfig.Blogs {
if tg := b.Telegram; tg != nil && tg.Enabled && tg.BotToken != "" && tg.ChatID != "" {
enable = true
}
}
if enable {
postHooks[postPostHook] = append(postHooks[postPostHook], func(p *post) {
if p.isPublishedSectionPost() {
p.tgPost()
}
})
}
}
func (p *post) tgPost() {
if appConfig.Blogs[p.Blog].Telegram == nil || !appConfig.Blogs[p.Blog].Telegram.Enabled {
tg := appConfig.Blogs[p.Blog].Telegram
if tg == nil || !tg.Enabled || tg.BotToken == "" || tg.ChatID == "" {
return
}
var message bytes.Buffer
@ -19,7 +36,7 @@ func (p *post) tgPost() {
message.WriteString("\n\n")
}
message.WriteString(p.fullURL())
sendTelegramMessage(message.String(), appConfig.Blogs[p.Blog].Telegram.BotToken, appConfig.Blogs[p.Blog].Telegram.ChatID)
sendTelegramMessage(message.String(), tg.BotToken, tg.ChatID)
}
func sendTelegramMessage(text, bottoken, chatID string) error {


+ 8
- 0
webmention.go View File

@ -33,6 +33,14 @@ type mention struct {
}
func initWebmention() {
// Add hooks
hookFunc := func(p *post) {
p.sendWebmentions()
}
postHooks[postPostHook] = append(postHooks[postPostHook], hookFunc)
postHooks[postUpdateHook] = append(postHooks[postUpdateHook], hookFunc)
postHooks[postDeleteHook] = append(postHooks[postDeleteHook], hookFunc)
// Start verifier
startWebmentionVerifier()
}


Loading…
Cancel
Save