-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
54 changed files
with
679 additions
and
1,565 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,7 +56,6 @@ linters: | |
- contextcheck | ||
- durationcheck | ||
- errorlint | ||
- goconst | ||
- goimports | ||
- revive | ||
- misspell | ||
|
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,75 @@ | ||
package db | ||
|
||
import ( | ||
"strings" | ||
|
||
"entgo.io/ent/dialect/sql" | ||
|
||
"github.com/satisfactorymodding/smr-api/generated" | ||
"github.com/satisfactorymodding/smr-api/generated/ent" | ||
"github.com/satisfactorymodding/smr-api/generated/ent/mod" | ||
"github.com/satisfactorymodding/smr-api/generated/ent/modtag" | ||
"github.com/satisfactorymodding/smr-api/models" | ||
) | ||
|
||
func ConvertModFilter(query *ent.ModQuery, filter *models.ModFilter, count bool, unapproved bool) *ent.ModQuery { | ||
query = query.WithTags() | ||
|
||
if len(filter.Ids) > 0 { | ||
query = query.Where(mod.IDIn(filter.Ids...)) | ||
} else if len(filter.References) > 0 { | ||
query = query.Where(mod.ModReferenceIn(filter.References...)) | ||
} else if filter != nil { | ||
query = query. | ||
Limit(*filter.Limit). | ||
Offset(*filter.Offset) | ||
|
||
if *filter.OrderBy != generated.ModFieldsSearch { | ||
if string(*filter.OrderBy) == "last_version_date" { | ||
query = query.Modify(func(s *sql.Selector) { | ||
s.OrderExpr(sql.ExprP("case when last_version_date is null then 1 else 0 end, last_version_date")) | ||
}).Clone() | ||
} else { | ||
query = query.Order(sql.OrderByField( | ||
filter.OrderBy.String(), | ||
OrderToOrder(filter.Order.String()), | ||
).ToFunc()) | ||
} | ||
} | ||
|
||
if filter.Search != nil && *filter.Search != "" { | ||
cleanSearch := strings.ReplaceAll(strings.TrimSpace(*filter.Search), " ", " & ") | ||
|
||
query = query.Where(func(s *sql.Selector) { | ||
join := sql.SelectExpr(sql.ExprP("id, (similarity(name, ?) * 2 + similarity(short_description, ?) + similarity(full_description, ?) * 0.5) as s", cleanSearch, cleanSearch, cleanSearch)) | ||
join.From(sql.Table(mod.Table)).As("t1") | ||
s.Join(join).On(s.C(mod.FieldID), join.C("id")) | ||
}) | ||
|
||
query = query.Where(func(s *sql.Selector) { | ||
s.Where(sql.ExprP(`"t1"."s" > 0.2`)) | ||
}) | ||
|
||
if !count && *filter.OrderBy == generated.ModFieldsSearch { | ||
query = query.Order(func(s *sql.Selector) { | ||
s.OrderExpr(sql.ExprP(`"t1"."s" DESC`)) | ||
}) | ||
} | ||
} | ||
|
||
if filter.Hidden == nil || !(*filter.Hidden) { | ||
query = query.Where(mod.Hidden(false)) | ||
} | ||
|
||
if filter.TagIDs != nil && len(filter.TagIDs) > 0 { | ||
query = query.Where(func(s *sql.Selector) { | ||
t := sql.Table(modtag.Table) | ||
s.Join(t).OnP(sql.ExprP("mod_tags.tag_id in ? AND mod_tags.mod_id = mods.id", filter.TagIDs)) | ||
}) | ||
} | ||
} | ||
|
||
query = query.Where(mod.Approved(!unapproved), mod.Denied(false)) | ||
|
||
return query | ||
} |
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,103 @@ | ||
package db | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
|
||
"github.com/satisfactorymodding/smr-api/generated/ent" | ||
"github.com/satisfactorymodding/smr-api/generated/ent/user" | ||
"github.com/satisfactorymodding/smr-api/oauth" | ||
"github.com/satisfactorymodding/smr-api/storage" | ||
"github.com/satisfactorymodding/smr-api/util" | ||
) | ||
|
||
func CompleteOAuthFlow(ctx context.Context, u *oauth.UserData, userAgent string) (*string, error) { | ||
avatarURL := u.Avatar | ||
u.Avatar = "" | ||
|
||
find := From(ctx).User.Query().Where(user.Email(u.Email)) | ||
|
||
if u.Site == oauth.SiteGithub { | ||
find = find.Where(user.GithubID(u.ID)) | ||
} else if u.Site == oauth.SiteGoogle { | ||
find = find.Where(user.GoogleID(u.ID)) | ||
} else if u.Site == oauth.SiteFacebook { | ||
find = find.Where(user.FacebookID(u.ID)) | ||
} | ||
|
||
found, err := find.First(ctx) | ||
if err != nil && !ent.IsNotFound(err) { | ||
return nil, err | ||
} | ||
|
||
newUser := false | ||
if ent.IsNotFound(err) { | ||
var err error | ||
create := From(ctx).User. | ||
Create(). | ||
SetEmail(u.Email). | ||
SetAvatar(u.Avatar). | ||
SetJoinedFrom(string(u.Site)). | ||
SetUsername(u.Username) | ||
|
||
if u.Site == oauth.SiteGithub { | ||
create = create.SetGithubID(u.ID) | ||
} else if u.Site == oauth.SiteGoogle { | ||
create = create.SetGoogleID(u.ID) | ||
} else if u.Site == oauth.SiteFacebook { | ||
create = create.SetFacebookID(u.ID) | ||
} | ||
|
||
found, err = create.Save(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
newUser = true | ||
} | ||
|
||
if !newUser { | ||
var update *ent.UserUpdateOne | ||
if u.Site == oauth.SiteGithub && found.GithubID == "" { | ||
update = found.Update().SetGithubID(u.ID) | ||
} else if u.Site == oauth.SiteGoogle && found.GoogleID == "" { | ||
update = found.Update().SetGoogleID(u.ID) | ||
} else if u.Site == oauth.SiteFacebook && found.FacebookID == "" { | ||
update = found.Update().SetFacebookID(u.ID) | ||
} | ||
|
||
if update != nil { | ||
if err := update.Exec(ctx); err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
|
||
// TODO Archive old deleted sessions to cold storage | ||
|
||
session, err := From(ctx).UserSession. | ||
Create(). | ||
SetUserID(found.ID). | ||
SetToken(util.GenerateUserToken()). | ||
SetUserAgent(userAgent). | ||
Save(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if avatarURL != "" && newUser { | ||
avatarData, err := util.LinkToWebp(ctx, avatarURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
success, avatarKey := storage.UploadUserAvatar(ctx, found.ID, bytes.NewReader(avatarData)) | ||
if success { | ||
if err := found.Update().SetAvatar(storage.GenerateDownloadLink(avatarKey)).Exec(ctx); err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
|
||
return &session.Token, nil | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.