Skip to content

Commit

Permalink
feat(styles): improve mirroring of code/metadata
Browse files Browse the repository at this point in the history
Also removed mirroring of images from USo-archive because it was causing
issues that I didn't want to deal with, as well as the fact that more
complex logic needs to be set in place for it to work properly.

Fixes #83
  • Loading branch information
vednoc committed Aug 26, 2021
1 parent 555b5fa commit d7d6fda
Showing 1 changed file with 74 additions and 37 deletions.
111 changes: 74 additions & 37 deletions modules/update/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,38 @@ import (
"userstyles.world/utils"
)

func isUSo(url string) bool {
func isArchive(url string) bool {
return strings.HasPrefix(url, utils.ArchiveURL)
}

func updateMeta(a, b *models.Style) bool {
func setStyleURL(a, b string) string {
if a != "" {
return a
}

return b
}

func updateArchiveMeta(a, b *models.Style) bool {
if a.Name == b.Name &&
a.Notes == b.Notes &&
a.Preview == b.Preview &&
a.Description == b.Description {
return false
}

return true
}

func updateMeta(a *models.Style, b *usercss.UserCSS) bool {
if a.Name == b.Name &&
a.Description == b.Description &&
a.Homepage == b.HomepageURL {
return false
}

return true
}

func getSourceCode(style models.Style) string {
if style.MirrorURL != "" {
return style.MirrorURL
Expand All @@ -35,52 +52,72 @@ func getSourceCode(style models.Style) string {
}

func Batch(batch models.Style) {
s := new(models.Style)
s.ID = batch.ID

// Update style metadata if style comes from USo-archive.
if isUSo(batch.Original) && batch.MirrorMeta {
importedStyle, err := utils.ImportFromArchive(batch.Original, models.APIUser{})
if err != nil {
log.Warn.Println("Failed to import a style from archive:", err.Error())
}
// Create new model.
style := new(models.Style)
style.ID = batch.ID

// Run update if fields differ.
if updateMeta(&batch, importedStyle) {
s.Name = importedStyle.Name
s.Notes = importedStyle.Notes
s.Preview = importedStyle.Preview
s.Description = importedStyle.Description

if err = models.UpdateStyle(s); err != nil {
log.Warn.Printf("Failed to mirror meta for %d: %s\n", batch.ID, err.Error())
}
if err = search.IndexStyle(s.ID); err != nil {
log.Warn.Printf("Failed to re-index style %d: %s\n", s.ID, err.Error())
}
}
}
// Don't update database record if nothing changed.
var updateReady bool

// Get new style source code.
style, err := usercss.ParseFromURL(getSourceCode(batch))
// Get new source code.
uc, err := usercss.ParseFromURL(getSourceCode(batch))
if err != nil {
log.Warn.Printf("Failed to parse style %d from URL: %s\n", batch.ID, err.Error())
return
}

// Exit if source code doesn't pass validation.
if errs := usercss.BasicMetadataValidation(style); errs != nil {
if errs := usercss.BasicMetadataValidation(uc); errs != nil {
log.Warn.Printf("Failed to validate style %d.\n", batch.ID)
return
}

// Mirror source code if versions don't match.
if style.Version != usercss.ParseFromString(batch.Code).Version {
s.Code = style.SourceCode
if err := models.UpdateStyle(s); err != nil {
log.Warn.Printf("Failed to mirror code for %d: %s\n", batch.ID, err)
return
// Set new source code.
if batch.MirrorCode {
if uc.Version != usercss.ParseFromString(batch.Code).Version {
style.Code = uc.SourceCode
updateReady = true
}
}

// Set new style metadata.
if batch.MirrorMeta {
url := setStyleURL(batch.MirrorURL, batch.Original)

if isArchive(url) {
s, err := utils.ImportFromArchive(url, models.APIUser{})
if err != nil {
log.Warn.Printf("Failed to import %s from archive: %s\n", url, err.Error())
}

if updateArchiveMeta(&batch, s) {
style.Name = s.Name
style.Notes = s.Notes
style.Preview = s.Preview
style.Description = s.Description
updateReady = true
}
} else {
if updateMeta(&batch, uc) {
style.Name = uc.Name
style.Description = uc.Description
style.Homepage = uc.HomepageURL
updateReady = true
}
}
}

if updateReady {
// Update database record.
if err = models.UpdateStyle(style); err != nil {
log.Warn.Printf("Failed to mirror style %d: %s\n", batch.ID, err.Error())
} else {
log.Info.Printf("Successfully mirrored style %d\n", batch.ID)
}

// Update search index.
if err = search.IndexStyle(style.ID); err != nil {
log.Warn.Printf("Failed to re-index style %d: %s\n", style.ID, err.Error())
}
log.Info.Printf("Mirrored code for style %d.\n", batch.ID)
}
}

0 comments on commit d7d6fda

Please sign in to comment.