Browse Source

Refactor and (hopefully) fix WebMentions

James Mills 1 week ago
parent
commit
0409f892b5
Signed by: prologic GPG Key ID: AC4C014F1440EBD6
  1. 25
      internal/post_handler.go
  2. 46
      internal/server.go

25
internal/post_handler.go

@ -166,24 +166,19 @@ func (s *Server) PostHandler() httprouter.Handle {
// WebMentions ...
// TODO: Use a queue here instead?
// TODO: Fix Webmentions
// TODO: https://git.mills.io/yarnsocial/yarn/issues/438
// TODO: https://git.mills.io/yarnsocial/yarn/issues/515
/*
if _, err := s.tasks.Dispatch(NewFuncTask(func() error {
for _, m := range twt.Mentions() {
twter := m.Twter()
if !isLocalURL(twter.RequestURI) {
if err := WebMention(twter.RequestURI, URLForTwt(s.config.BaseURL, twt.Hash())); err != nil {
log.WithError(err).Warnf("error sending webmention to %s", twter.RequestURI)
}
if _, err := s.tasks.Dispatch(NewFuncTask(func() error {
for _, m := range twt.Mentions() {
twter := m.Twter()
if !s.config.IsLocalURL(twter.URI) {
if err := WebMention(twter.URI, URLForTwt(s.config.BaseURL, twt.Hash())); err != nil {
log.WithError(err).Warnf("error sending webmention to %s", twter.URI)
}
}
return nil
})); err != nil {
log.WithError(err).Warn("error submitting task for webmentions")
}
*/
return nil
})); err != nil {
log.WithError(err).Warn("error submitting task for webmentions")
}
http.Redirect(w, r, RedirectRefererURL(r, s.config, "/"), http.StatusFound)
}

46
internal/server.go

@ -397,23 +397,57 @@ func (s *Server) processWebMention(source, target *url.URL, sourceData *microfor
return authorName, sourceFeed, nil
}
_, err := GetUserFromURL(s.config, s.db, target.String())
user, err := GetUserFromURL(s.config, s.db, target.String())
if err != nil {
log.WithError(err).WithField("target", target.String()).Warn("unable to get used from webmention target")
return err
}
authorName, sourceFeed, err := parseSourceData(sourceData)
authorNick, sourceFeed, err := parseSourceData(sourceData)
if err != nil {
log.WithError(err).Warnf("error parsing mf2 source data from %s", source)
return err
}
adminUser, err := s.db.GetUser(s.config.AdminUser)
if err != nil {
log.WithError(err).Warn("error loading admin user object")
return err
}
supportFeed, err := s.db.GetFeed(supportSpecialUser)
if err != nil {
log.WithError(err).Warn("error loading support feed object")
return err
}
// TODO: Implement some kind of in-app notifications?
if authorName != "" && sourceFeed != "" {
// TODO: Do something with the @-mention
// TODO: Make this configurable?
mentionText := fmt.Sprintf(
"👋 Hello @<%s %s>, ",
user.Username, s.config.URLForUser(user.Username),
)
if authorNick != "" && sourceFeed != "" {
mentionText += fmt.Sprintf(
"you were mentioned on %s by @<%s %s>",
source.String(), authorNick, sourceFeed,
)
} else {
// TODO: Do something with the WebMention
mentionText += fmt.Sprintf(
"you were mentioned on %s",
source.String(),
)
}
mentionText = CleanTwt(mentionText)
mentionTwt, err := s.AppendTwt(adminUser, supportFeed, mentionText)
if err != nil {
log.WithError(err).Warnf("error posting mention for %s", user.Username)
return err
}
s.cache.InjectFeed(s.config.URLForUser(supportFeed.Name), mentionTwt)
s.cache.DeleteUserViews(user)
return nil
}

Loading…
Cancel
Save