Skip to content

Commit

Permalink
Merge pull request #82 from Dzalevski/refactor/storage-interface-impl…
Browse files Browse the repository at this point in the history
…ement
  • Loading branch information
moul authored Apr 26, 2021
2 parents 647ff3c + e601d45 commit d074dba
Show file tree
Hide file tree
Showing 22 changed files with 868 additions and 528 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ gin-bin
*.out

# better safe than sorry
config.txt
config.txt

# IDE Settings
.idea
1 change: 1 addition & 0 deletions AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions api/sgtm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,13 @@ message Session {
int64 user_id = 1 [(go.field) = {name: "UserID"}];
string discord_access_token = 2;
}

message UploadsByWeek {
int64 weekday = 1;
int64 quantity = 2;
}

message PostByKind {
int64 quantity = 1;
Post.Kind kind = 2;
}
2 changes: 1 addition & 1 deletion gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 6 additions & 33 deletions pkg/sgtm/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,11 @@ func (svc *Service) Me(ctx context.Context, req *sgtmpb.Me_Request) (*sgtmpb.Me_
return nil, err
}

var user sgtmpb.User
err = svc.rodb().
Where("id = ?", claims.Session.UserID).
First(&user).
Error
user, err := svc.storage.GetUserByID(claims.Session.UserID)
if err != nil {
return nil, err
}
ret := sgtmpb.Me_Response{User: &user}

return &ret, nil
return &sgtmpb.Me_Response{User: user}, nil
}

func (svc *Service) Ping(context.Context, *sgtmpb.Ping_Request) (*sgtmpb.Ping_Response, error) {
Expand All @@ -57,39 +51,18 @@ func (svc *Service) Status(context.Context, *sgtmpb.Status_Request) (*sgtmpb.Sta
}

func (svc *Service) UserList(context.Context, *sgtmpb.UserList_Request) (*sgtmpb.UserList_Response, error) {
ret := &sgtmpb.UserList_Response{}
err := svc.rodb().
Order("created_at desc").
Find(&ret.Users).
Error
users, err := svc.storage.GetLastUsersList(100)
if err != nil {
return nil, err
}

for _, user := range ret.Users {
user.Filter()
}
return ret, nil
return &sgtmpb.UserList_Response{Users: users}, nil
}

func (svc *Service) PostList(context.Context, *sgtmpb.PostList_Request) (*sgtmpb.PostList_Response, error) {
ret := &sgtmpb.PostList_Response{}
err := svc.rodb().
Order("sort_date desc").
Where(sgtmpb.Post{
Visibility: sgtmpb.Visibility_Public,
}).
Where("kind in (?)", sgtmpb.Post_TrackKind).
Limit(100).
Find(&ret.Posts).
Error
posts, err := svc.storage.GetPostList(100)
if err != nil {
return nil, err
}

for _, post := range ret.Posts {
post.Filter()
}

return ret, nil
return &sgtmpb.PostList_Response{Posts: posts}, nil
}
1 change: 1 addition & 0 deletions pkg/sgtm/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bwmarrin/snowflake"
"gorm.io/gorm"
"gorm.io/gorm/clause"

"moul.io/sgtm/pkg/sgtmpb"
)

Expand Down
14 changes: 2 additions & 12 deletions pkg/sgtm/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,7 @@ func (svc *Service) generateSitemap() *stm.Sitemap {
})
// users
{
var users []*sgtmpb.User
err := svc.rodb().
Find(&users).
Error
users, err := svc.storage.GetLastUsersList(1000)
if err != nil {
svc.logger.Error("query users for sitemap", zap.Error(err))
} else {
Expand All @@ -319,14 +316,7 @@ func (svc *Service) generateSitemap() *stm.Sitemap {
}
// posts
{
var posts []*sgtmpb.Post
err := svc.rodb().
Where(sgtmpb.Post{
Visibility: sgtmpb.Visibility_Public,
Kind: sgtmpb.Post_TrackKind,
}).
Find(&posts).
Error
posts, err := svc.storage.GetPostList(100)
if err != nil {
svc.logger.Error("query posts for sitemap", zap.Error(err))
} else {
Expand Down
77 changes: 15 additions & 62 deletions pkg/sgtm/page_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import (
"time"

"github.com/bwmarrin/discordgo"
jwt "github.com/dgrijalva/jwt-go"
"github.com/gosimple/slug"
"github.com/dgrijalva/jwt-go"
"go.uber.org/zap"
"golang.org/x/oauth2"
"gorm.io/gorm"

"moul.io/sgtm/pkg/sgtmpb"
)

Expand Down Expand Up @@ -123,66 +122,20 @@ func (svc *Service) httpAuthCallback(w http.ResponseWriter, r *http.Request) {
svc.logger.Debug("get user settings", zap.Any("user", discordUser))
}

// create/update user in DB
var dbUser sgtmpb.User
{
dbUser.Email = discordUser.Email
err := svc.rodb().Where(&dbUser).First(&dbUser).Error
switch {
case errors.Is(err, gorm.ErrRecordNotFound):
// user not found, creating it
dbUser = sgtmpb.User{
Email: discordUser.Email,
Avatar: fmt.Sprintf("https://cdn.discordapp.com/avatars/%s/%s.png", discordUser.ID, discordUser.Avatar),
Slug: slug.Make(discordUser.Username),
Locale: discordUser.Locale,
DiscordID: discordUser.ID,
DiscordUsername: fmt.Sprintf("%s#%s", discordUser.Username, discordUser.Discriminator),
// Firstname
// Lastname
}
// FIXME: check if slug already exists, if yes, append something to the slug
err = svc.rwdb().Transaction(func(tx *gorm.DB) error {
if err := tx.Create(&dbUser).Error; err != nil {
return err
}

registerEvent := sgtmpb.Post{AuthorID: dbUser.ID, Kind: sgtmpb.Post_RegisterKind}
if err := tx.Create(&registerEvent).Error; err != nil {
return err
}
svc.logger.Debug("new register", zap.Any("event", &registerEvent))

linkDiscordEvent := sgtmpb.Post{AuthorID: dbUser.ID, Kind: sgtmpb.Post_LinkDiscordAccountKind}
if err := tx.Create(&linkDiscordEvent).Error; err != nil {
return err
}
svc.logger.Debug("new link discord event", zap.Any("event", &registerEvent))

return nil
})
if err != nil {
svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity)
return
}

case err == nil:
// user exists
// FIXME: update user in DB if needed

loginEvent := sgtmpb.Post{AuthorID: dbUser.ID, Kind: sgtmpb.Post_LoginKind}
if err := svc.rwdb().Create(&loginEvent).Error; err != nil {
svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity)
return
}
svc.logger.Debug("new login", zap.Any("event", &loginEvent))

default:
// unexpected error
svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity)
return
}
dbUser := &sgtmpb.User{
Email: discordUser.Email,
DiscordID: discordUser.ID,
DiscordUsername: discordUser.Username,
Slug: discordUser.Username,
Locale: discordUser.Locale,
Avatar: discordUser.Avatar,
}
dbUser, err := svc.storage.CreateUser(dbUser)
if err != nil {
svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity)
return
}
svc.logger.Debug("user created/updated", zap.Any("user", &dbUser))

// prepare JWT token
var tokenString string
Expand Down
31 changes: 10 additions & 21 deletions pkg/sgtm/page_home.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net/http"
"time"

packr "github.com/gobuffalo/packr/v2"
"github.com/gobuffalo/packr/v2"
"go.uber.org/zap"
"moul.io/sgtm/pkg/sgtmpb"
)
Expand All @@ -24,7 +24,8 @@ func (svc *Service) homePage(box *packr.Box) func(w http.ResponseWriter, r *http
// tracking
{
viewEvent := sgtmpb.Post{AuthorID: data.UserID, Kind: sgtmpb.Post_ViewHomeKind}
if err := svc.rwdb().Create(&viewEvent).Error; err != nil {
err = svc.storage.CreatePost(&viewEvent)
if err != nil {
data.Error = "Cannot write activity: " + err.Error()
} else {
svc.logger.Debug("new view home", zap.Any("event", &viewEvent))
Expand All @@ -37,17 +38,8 @@ func (svc *Service) homePage(box *packr.Box) func(w http.ResponseWriter, r *http
if data.UserID == 0 {
limit = 10
}
if err := svc.rodb().
Model(&sgtmpb.Post{}).
Preload("Author").
Where(sgtmpb.Post{
Kind: sgtmpb.Post_TrackKind,
Visibility: sgtmpb.Visibility_Public,
}).
Order("sort_date desc").
Limit(limit). // FIXME: pagination
Find(&data.Home.LastTracks).
Error; err != nil {
data.Home.LastTracks, err = svc.storage.GetPostList(limit)
if err != nil {
data.Error = "Cannot fetch last tracks: " + err.Error()
}
for _, track := range data.Home.LastTracks {
Expand All @@ -57,16 +49,13 @@ func (svc *Service) homePage(box *packr.Box) func(w http.ResponseWriter, r *http

// last users
{
if err := svc.rodb().
Model(&sgtmpb.User{}).
Order("created_at desc").
Limit(10).
Find(&data.Home.LastUsers).
Error; err != nil {
if data.Home.LastUsers, err = svc.storage.GetLastUsersList(10); err != nil {
data.Error = "Cannot fetch last users: " + err.Error() // FIXME: use slice instead of string
}
for _, user := range data.Home.LastUsers {
user.ApplyDefaults()
if data.Home.LastUsers != nil {
for _, user := range data.Home.LastUsers {
user.ApplyDefaults()
}
}
}
// end of custom
Expand Down
35 changes: 14 additions & 21 deletions pkg/sgtm/page_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,14 @@ func (svc *Service) newPage(box *packr.Box) func(w http.ResponseWriter, r *http.

// check if track already exists
{
var alreadyExists sgtmpb.Post
err := svc.rodb().
Model(&sgtmpb.Post{}).
Where(sgtmpb.Post{IPFSCID: cid}).
First(&alreadyExists).
Error
if err == nil && alreadyExists.ID != 0 {
data.New.URLInvalidMsg = fmt.Sprintf(`This track already exists: <a href="/post/%d">%s</a>.`, alreadyExists.ID, alreadyExists.SafeTitle())
return nil
alreadyExists, err := svc.storage.GetTrackByCID(cid)
if err == nil {
if alreadyExists.ID != 0 {
data.New.URLInvalidMsg = fmt.Sprintf(`This track already exists: <a href="/post/%d">%s</a>.`, alreadyExists.ID, alreadyExists.SafeTitle())
return nil
}
}
}

mimeType := header.Header["Content-Type"][0]
fmt.Println("MIME", mimeType)
// FIXME: check that mimeType starts with audio/ maybe
Expand Down Expand Up @@ -143,18 +139,14 @@ func (svc *Service) newPage(box *packr.Box) func(w http.ResponseWriter, r *http.

// check if track already exists
{
var alreadyExists sgtmpb.Post
err := svc.rodb().
Model(&post).
Where(sgtmpb.Post{SoundCloudID: post.SoundCloudID}).
First(&alreadyExists).
Error
if err == nil && alreadyExists.ID != 0 {
data.New.URLInvalidMsg = fmt.Sprintf(`This track already exists: <a href="%s">%s</a>.`, alreadyExists.CanonicalURL(), alreadyExists.SafeTitle())
return nil
alreadyExists, err := svc.storage.GetTrackBySCID(post.SoundCloudID)
if err == nil {
if alreadyExists.ID != 0 {
data.New.URLInvalidMsg = fmt.Sprintf(`This track already exists: <a href="%s">%s</a>.`, alreadyExists.CanonicalURL(), alreadyExists.SafeTitle())
return nil
}
}
}

post.SoundCloudSecretToken = u.Query().Get("secret_token")
params := url.Values{}
if post.SoundCloudSecretToken != "" {
Expand Down Expand Up @@ -207,7 +199,8 @@ func (svc *Service) newPage(box *packr.Box) func(w http.ResponseWriter, r *http.
}
post := validate()
if post != nil {
if err := svc.rwdb().Create(&post).Error; err != nil {
err = svc.storage.CreatePost(post)
if err != nil {
svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity)
return
}
Expand Down
Loading

0 comments on commit d074dba

Please sign in to comment.