owl-blogs/cmd/owl/webmention.go

100 lines
2.2 KiB
Go
Raw Normal View History

2022-09-06 17:47:15 +00:00
package main
import (
"h4kor/owl-blogs"
2022-09-10 12:16:22 +00:00
"sync"
2022-09-06 17:47:15 +00:00
"github.com/spf13/cobra"
)
var postId string
2022-09-06 17:47:15 +00:00
func init() {
rootCmd.AddCommand(webmentionCmd)
webmentionCmd.Flags().StringVar(
&postId, "post", "",
"specify the post to send webmentions for. Otherwise, all posts will be checked.",
)
2022-09-06 17:47:15 +00:00
}
var webmentionCmd = &cobra.Command{
Use: "webmention",
Short: "Send webmentions for posts, optionally for a specific user",
Long: `Send webmentions for posts, optionally for a specific user`,
Run: func(cmd *cobra.Command, args []string) {
repo, err := owl.OpenRepository(repoPath)
if err != nil {
println("Error opening repository: ", err.Error())
return
}
var users []owl.User
if user == "" {
// send webmentions for all users
users, err = repo.Users()
if err != nil {
println("Error getting users: ", err.Error())
return
}
} else {
// send webmentions for a specific user
user, err := repo.GetUser(user)
users = append(users, user)
if err != nil {
println("Error getting user: ", err.Error())
return
}
}
2022-12-05 20:09:23 +00:00
processPost := func(user owl.User, post owl.Post) error {
println("Webmentions for post: ", post.Title())
err := post.ScanForLinks()
2022-09-06 17:47:15 +00:00
if err != nil {
println("Error scanning post for links: ", err.Error())
return err
2022-09-06 17:47:15 +00:00
}
webmentions := post.OutgoingWebmentions()
println("Found ", len(webmentions), " links")
2022-09-10 12:16:22 +00:00
wg := sync.WaitGroup{}
wg.Add(len(webmentions))
for _, webmention := range webmentions {
2022-09-10 12:16:22 +00:00
go func(webmention owl.WebmentionOut) {
defer wg.Done()
2022-09-10 12:20:07 +00:00
sendErr := post.SendWebmention(webmention)
if sendErr != nil {
println("Error sending webmentions: ", sendErr.Error())
2022-09-10 12:16:22 +00:00
} else {
println("Webmention sent to ", webmention.Target)
}
}(webmention)
}
2022-09-10 12:17:00 +00:00
wg.Wait()
return nil
}
2022-09-06 17:47:15 +00:00
for _, user := range users {
if postId != "" {
// send webmentions for a specific post
post, err := user.GetPost(postId)
if err != nil {
println("Error getting post: ", err.Error())
return
2022-09-06 17:47:15 +00:00
}
2022-10-13 19:03:16 +00:00
processPost(user, post)
2022-09-08 19:28:05 +00:00
return
}
posts, err := user.PublishedPosts()
if err != nil {
println("Error getting posts: ", err.Error())
}
for _, post := range posts {
processPost(user, post)
2022-09-06 17:47:15 +00:00
}
}
},
}