-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plus): when purchasing membership, automatically enroll in rss feed
In an effort to improve our communication with pico+ users, we want to automatically enroll them into our user notification feed.
- Loading branch information
Showing
4 changed files
with
186 additions
and
74 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 |
---|---|---|
|
@@ -52,6 +52,22 @@ func TestPaymentWebhook(t *testing.T) { | |
mux.ServeHTTP(responseRecorder, request) | ||
|
||
testResponse(t, responseRecorder, 200, "text/plain") | ||
|
||
posts, err := apiConfig.Dbpool.FindPostsForUser(&db.Pager{Num: 1000, Page: 0}, testUserID, "feeds") | ||
if err != nil { | ||
t.Error("could not find posts for user") | ||
} | ||
for _, post := range posts.Data { | ||
if post.Filename != "pico-plus" { | ||
continue | ||
} | ||
expectedText := `=: email [email protected] | ||
=: digest_interval 1day | ||
=> https://auth.pico.sh/rss/123` | ||
if post.Text != expectedText { | ||
t.Errorf("Want pico plus feed file %s, got %s", expectedText, post.Text) | ||
} | ||
} | ||
} | ||
|
||
func TestUser(t *testing.T) { | ||
|
@@ -195,6 +211,7 @@ type ApiExample struct { | |
|
||
type AuthDb struct { | ||
*stub.StubDB | ||
Posts []*db.Post | ||
} | ||
|
||
func (a *AuthDb) AddPicoPlusUser(username, email, from, txid string) error { | ||
|
@@ -230,6 +247,21 @@ func (a *AuthDb) FindFeatureForUser(userID string, feature string) (*db.FeatureF | |
return &db.FeatureFlag{ID: "2", UserID: userID, Name: "plus", ExpiresAt: &oneDayWarning, CreatedAt: &now}, nil | ||
} | ||
|
||
func (a *AuthDb) InsertPost(post *db.Post) (*db.Post, error) { | ||
a.Posts = append(a.Posts, post) | ||
return post, nil | ||
} | ||
|
||
func (a *AuthDb) FindPostsForUser(pager *db.Pager, userID, space string) (*db.Paginate[*db.Post], error) { | ||
return &db.Paginate[*db.Post]{ | ||
Data: a.Posts, | ||
}, nil | ||
} | ||
|
||
func (a *AuthDb) UpsertToken(string, string) (string, error) { | ||
return "123", nil | ||
} | ||
|
||
func NewAuthDb(logger *slog.Logger) *AuthDb { | ||
sb := stub.NewStubDB(logger) | ||
return &AuthDb{ | ||
|
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,93 @@ | ||
package shared | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/gorilla/feeds" | ||
"github.com/picosh/pico/db" | ||
) | ||
|
||
func UserFeed(me db.DB, userID, token string) (*feeds.Feed, error) { | ||
var err error | ||
if token == "" { | ||
token, err = me.UpsertToken(userID, "pico-rss") | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
href := fmt.Sprintf("https://auth.pico.sh/rss/%s", token) | ||
|
||
feed := &feeds.Feed{ | ||
Title: "pico+", | ||
Link: &feeds.Link{Href: href}, | ||
Description: "get notified of important membership updates", | ||
Author: &feeds.Author{Name: "team pico"}, | ||
} | ||
var feedItems []*feeds.Item | ||
|
||
now := time.Now() | ||
ff, err := me.FindFeatureForUser(userID, "plus") | ||
if err != nil { | ||
// still want to send an empty feed | ||
} else { | ||
createdAt := ff.CreatedAt | ||
createdAtStr := createdAt.Format("2006-01-02 15:04:05") | ||
id := fmt.Sprintf("pico-plus-activated-%d", createdAt.Unix()) | ||
content := `Thanks for joining pico+! You now have access to all our premium services for exactly one year. We will send you pico+ expiration notifications through this RSS feed. Go to <a href="https://pico.sh/getting-started#next-steps">pico.sh/getting-started#next-steps</a> to start using our services.` | ||
plus := &feeds.Item{ | ||
Id: id, | ||
Title: fmt.Sprintf("pico+ membership activated on %s", createdAtStr), | ||
Link: &feeds.Link{Href: "https://pico.sh"}, | ||
Content: content, | ||
Created: *createdAt, | ||
Updated: *createdAt, | ||
Description: content, | ||
Author: &feeds.Author{Name: "team pico"}, | ||
} | ||
feedItems = append(feedItems, plus) | ||
|
||
oneMonthWarning := ff.ExpiresAt.AddDate(0, -1, 0) | ||
mo := genFeedItem(now, *ff.ExpiresAt, oneMonthWarning, "1-month") | ||
if mo != nil { | ||
feedItems = append(feedItems, mo) | ||
} | ||
|
||
oneWeekWarning := ff.ExpiresAt.AddDate(0, 0, -7) | ||
wk := genFeedItem(now, *ff.ExpiresAt, oneWeekWarning, "1-week") | ||
if wk != nil { | ||
feedItems = append(feedItems, wk) | ||
} | ||
|
||
oneDayWarning := ff.ExpiresAt.AddDate(0, 0, -2) | ||
day := genFeedItem(now, *ff.ExpiresAt, oneDayWarning, "1-day") | ||
if day != nil { | ||
feedItems = append(feedItems, day) | ||
} | ||
} | ||
|
||
feed.Items = feedItems | ||
return feed, nil | ||
} | ||
|
||
func genFeedItem(now time.Time, expiresAt time.Time, warning time.Time, txt string) *feeds.Item { | ||
if now.After(warning) { | ||
content := fmt.Sprintf( | ||
"Your pico+ membership is going to expire on %s", | ||
expiresAt.Format("2006-01-02 15:04:05"), | ||
) | ||
return &feeds.Item{ | ||
Id: fmt.Sprintf("%d", warning.Unix()), | ||
Title: fmt.Sprintf("pico+ %s expiration notice", txt), | ||
Link: &feeds.Link{Href: "https://pico.sh"}, | ||
Content: content, | ||
Created: warning, | ||
Updated: warning, | ||
Description: content, | ||
Author: &feeds.Author{Name: "team pico"}, | ||
} | ||
} | ||
|
||
return 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