-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6587173
commit 44eab3e
Showing
6 changed files
with
175 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package inbox | ||
|
||
import ( | ||
"strings" | ||
"time" | ||
|
||
"github.com/jprobinson/eazye" | ||
"github.com/microcosm-cc/bluemonday" | ||
) | ||
|
||
type ReaderInfo struct { | ||
Email string | ||
GitHub string | ||
GitHubSet bool | ||
ReportInterval time.Duration | ||
} | ||
|
||
func ParseBody(email eazye.Email, policy bluemonday.Policy) (*ReaderInfo, error) { | ||
r := &ReaderInfo{ | ||
Email: policy.Sanitize(email.From.Address), | ||
} | ||
|
||
bodyLines := strings.Split(string(email.Text), "\n") | ||
for _, line := range bodyLines { | ||
lineCleaned := strings.TrimSpace(line) | ||
|
||
// Parse their optionally-provided GitHub username | ||
githubMatches := githubPattern.FindStringSubmatch(lineCleaned) | ||
if len(githubMatches) != 0 { | ||
github := githubMatches[githubPattern.SubexpIndex("github")] | ||
r.GitHub = policy.Sanitize(github) | ||
r.GitHubSet = true | ||
continue | ||
} | ||
|
||
// Parse their requested reporting interval | ||
intervalMatches := intervalPattern.FindStringSubmatch(lineCleaned) | ||
if len(intervalMatches) != 0 { | ||
interval, err := time.ParseDuration(intervalMatches[intervalPattern.SubexpIndex("interval")]) | ||
if err == nil { | ||
r.ReportInterval = interval | ||
continue | ||
} | ||
} | ||
} | ||
|
||
return r, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package inbox | ||
|
||
import "regexp" | ||
|
||
var githubPattern = regexp.MustCompile(`(?i)github:\s*(?P<github>\S*)`) | ||
var intervalPattern = regexp.MustCompile(`(?i)interval:\s*(?P<interval>\S*)`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package inbox | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"log" | ||
"text/template" | ||
"time" | ||
|
||
"github.com/jackc/pgx" | ||
"github.com/karashiiro/operator/pkg/html" | ||
"github.com/karashiiro/operator/pkg/outlook" | ||
) | ||
|
||
func saveUpdatedInfo(conn *pgx.Conn, readers []*ReaderInfo) { | ||
for _, r := range readers { | ||
if r.GitHubSet { | ||
_, err := updateGitHub(conn, r.GitHub) | ||
if err != nil { | ||
log.Printf("Failed to update reader GitHub: %v\n", err) | ||
continue | ||
} | ||
} | ||
|
||
if r.ReportInterval.Minutes() > 0 { | ||
_, err := updateReportInterval(conn, r.ReportInterval) | ||
if err != nil { | ||
log.Printf("Failed to update reader report interval: %v\n", err) | ||
continue | ||
} | ||
} | ||
|
||
log.Printf("Sending update confirmation email to %s\n", r.Email) | ||
|
||
var updateMessage bytes.Buffer | ||
err := buildUpdateTemplate(&updateMessage, r.ReportInterval) | ||
if err != nil { | ||
log.Printf("Failed to build update template: %v\n", err) | ||
} | ||
|
||
err = outlook.SendEmail(r.Email, "Information updated", updateMessage.String()) | ||
if err != nil { | ||
log.Printf("Unable to send mail: %v\n", err) | ||
continue | ||
} | ||
|
||
log.Printf("Updated reader %s\n", r.Email) | ||
} | ||
} | ||
|
||
func buildUpdateTemplate(w io.Writer, interval time.Duration) error { | ||
t, err := template.ParseFS(html.Files, "confirm-update.gohtml") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = t.Execute(w, struct { | ||
Interval time.Duration | ||
}{ | ||
Interval: interval, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func updateGitHub(conn *pgx.Conn, gh string) (int64, error) { | ||
var github *string | ||
if gh != "" { | ||
github = &gh | ||
} | ||
|
||
t, err := conn.Exec(` | ||
UPDATE Reader SET github = $1; | ||
`, github) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return t.RowsAffected(), nil | ||
} | ||
|
||
func updateReportInterval(conn *pgx.Conn, interval time.Duration) (int64, error) { | ||
t, err := conn.Exec(` | ||
UPDATE Reader SET report_interval = $1; | ||
`, interval) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return t.RowsAffected(), nil | ||
} |