Skip to content

Commit

Permalink
Merge pull request #69 from sgtm-club/dev/moul/tags
Browse files Browse the repository at this point in the history
feat: support multiple tags
  • Loading branch information
moul committed Nov 10, 2020
2 parents 9638309 + cbca762 commit 2b0e64f
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 182 deletions.
11 changes: 6 additions & 5 deletions api/sgtm.proto
Expand Up @@ -152,7 +152,7 @@ message Post {

/// track

string genre = 40;
string genre = 40 [deprecated = true]; // replaced by 'tags'
uint64 duration = 41;
string artwork_url = 42 [(go.field) = {name: 'ArtworkURL'}];
double bpm = 43 [(go.field) = {name: 'BPM'}];
Expand All @@ -163,6 +163,7 @@ message Post {
int64 provider_created_at = 48;
int64 provider_updated_at = 49;
string provider_metadata = 50;
string tags = 51; // comma separated list of tags

// soundcloud post
string soundcloud_secret_token = 80 [(go.field) = {name: 'SoundCloudSecretToken'}];
Expand All @@ -171,10 +172,10 @@ message Post {

// ipfs post
string ipfs_cid = 90 [(go.field) = {name: 'IPFSCID'}];
string mime_type = 51 [(go.field) = {name: 'MIMEType'}];
int64 size_bytes = 52;
string file_extension = 53;
string attachment_filename = 54;
string mime_type = 91 [(go.field) = {name: 'MIMEType'}];
int64 size_bytes = 92;
string file_extension = 93;
string attachment_filename = 94;

/// tracking activities
int64 target_user_id = 101 [(go.field) = {name: 'TargetUserID'}];
Expand Down
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.

2 changes: 1 addition & 1 deletion pkg/sgtm/page_home.tmpl.html
Expand Up @@ -62,7 +62,7 @@ <h6 class="card-header">
<div class="media-body">
<a href="{{.CanonicalURL}}"><h5 class="mt-0 d-inline-block">{{.Title}}</h5></a>
<div>
{{with .Genre}}<div>📁 Genre: <span class="badge badge-secondary">{{.}}</span></div>{{end}}
{{with .TagList}}<div>📁 Tags: {{range .}}<span class="badge badge-secondary">{{.}}</span> {{end}}</div>{{end}}
{{if .Duration}}<div>⏱ Duration: <span data-toggle="tooltip" data-placement="right" title="{{.GoDuration}}">{{.GoDuration | prettyDuration}}</span></div>{{end}}
</div>
</div>
Expand Down
17 changes: 11 additions & 6 deletions pkg/sgtm/page_new.go
Expand Up @@ -173,18 +173,23 @@ func (svc *Service) newPage(box *packr.Box) func(w http.ResponseWriter, r *http.
post.ProviderCreatedAt = createdAt.UnixNano()
post.SortDate = createdAt.UnixNano()
}
post.Genre = track.Genre
tags := append(
[]string{track.Genre},
strings.Split(track.TagList, " ")...,
)
post.Tags = strings.Join(tags, ", ")
post.Duration = track.Duration
post.ArtworkURL = strings.ReplaceAll(track.ArtworkUrl, "-large.jpg", "-t500x500.jpg")
post.ISRC = track.ISRC
post.BPM = track.Bpm
post.KeySignature = track.KeySignature
post.ProviderDescription = track.Description
// post.Body = track.Description
/*post.Tags = track.TagList
post.WaveformURL = track.WaveformURL
post.License = track.License
track.User*/
/*
post.Body = track.Description
post.WaveformURL = track.WaveformURL
post.License = track.License
track.User
*/
if track.Downloadable {
post.DownloadURL = track.DownloadUrl
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sgtm/page_post.tmpl.html
Expand Up @@ -41,7 +41,7 @@ <h1>{{.Post.Post.Title}}</h1>
</audio>
{{end}}{{end}}
{{with .Post.Post.SafeDescription}}{{. | markdownify}}{{end}}
{{with .Post.Post.Genre}}<div>📁 Genre: <span class="badge badge-secondary">{{.}}</span></div>{{end}}
{{with .Post.Post.TagList}}<div>📁 Tags: {{range .}}<span class="badge badge-secondary">{{.}}</span> {{end}}</div>{{end}}
{{if .Post.Post.Duration}}<div>⏱ Duration: <span data-toggle="tooltip" data-placement="right" title="{{.Post.Post.GoDuration}}">{{.Post.Post.GoDuration | prettyDuration}}</span></div>{{end}}
{{with .Post.Post.BPM}}
<div>⏩ BPM: {{.}}</div>
Expand Down
2 changes: 1 addition & 1 deletion pkg/sgtm/page_profile.tmpl.html
Expand Up @@ -40,7 +40,7 @@ <h2>🎶 Tracks by {{.Profile.User.DisplayName}}</h2>
<a href="{{.CanonicalURL}}"><small class="text-muted">{{.SortDate | fromUnixNano | prettyAgo}}</small></a>
<div>
{{with .SafeDescription}}<p>{{.}}</p>{{end}}
{{with .Genre}}<div>📁 Genre: <span class="badge badge-secondary">{{.}}</span></div>{{end}}
{{with .TagList}}<div>📁 Tags: {{range .}}<span class="badge badge-secondary">{{.}}</span> {{end}}</div>{{end}}
{{if .Duration}}<div>⏱ Duration: <span data-toggle="tooltip" data-placement="right" title="{{.GoDuration}}">{{.GoDuration | prettyDuration}}</span></div>{{end}}
<!--{{with .DownloadURL}}<div><a href="{{.}}" class="btn">⬇️ Download</a></div>{{end}}-->
</div>
Expand Down
22 changes: 17 additions & 5 deletions pkg/sgtm/processing_worker.go
Expand Up @@ -16,7 +16,7 @@ type processingWorkerDriver struct {
started bool
wg *sync.WaitGroup

trackMigrations []func(*sgtmpb.Post) error
trackMigrations []func(*sgtmpb.Post, *gorm.DB) error
}

func (svc *Service) StartProcessingWorker() error {
Expand Down Expand Up @@ -72,20 +72,20 @@ func (svc *Service) processingLoop(i int) error {
return fmt.Errorf("failed to fetch tracks that need to be processed: %w", err)
}

err = svc.rwdb().Transaction(func(db *gorm.DB) error {
err = svc.rwdb().Transaction(func(tx *gorm.DB) error {
for _, entryPtr := range outdated {
entry := entryPtr
version := 1
for _, migration := range svc.processingWorker.trackMigrations {
err := migration(entry)
err := migration(entry, tx)
if err != nil {
entry.ProcessingError = err.Error()
break
}
entry.ProcessingVersion = int64(version)
version++
}
if err := db.
if err := tx.
Model(&entry).
Updates(map[string]interface{}{
"processing_version": entry.ProcessingVersion,
Expand Down Expand Up @@ -113,7 +113,19 @@ func (svc *Service) processingLoop(i int) error {
}

func (svc *Service) setupMigrations() {
svc.processingWorker.trackMigrations = []func(*sgtmpb.Post) error{
svc.processingWorker.trackMigrations = []func(*sgtmpb.Post, *gorm.DB) error{
// migrate track.Genre to track.Tags
func(post *sgtmpb.Post, tx *gorm.DB) error {
if post.Tags != "" || post.Genre == "" { // nolint:staticcheck
// nothing to do
return nil
}

return tx.Model(post).Updates(map[string]interface{}{
"tags": post.Genre, // nolint:staticcheck
"genre": "",
}).Error
},
/*
// FIXME: try downloading the mp3 locally
func(post *sgtmpb.Post) error { return fmt.Errorf("not implemented") },
Expand Down
11 changes: 11 additions & 0 deletions pkg/sgtmpb/helpers.go
Expand Up @@ -50,6 +50,17 @@ func (p *Post) Filter() {
func (p *Post) IsSoundCloud() bool { return p.GetProvider() == Provider_SoundCloud }
func (p *Post) IsIPFS() bool { return p.GetProvider() == Provider_IPFS }

func (p *Post) TagList() []string {
if strings.TrimSpace(p.Tags) == "" {
return nil
}
tags := strings.Split(p.Tags, ",")
for idx, tag := range tags {
tags[idx] = strings.TrimSpace(tag)
}
return tags
}

// User

func (u *User) ApplyDefaults() {
Expand Down

0 comments on commit 2b0e64f

Please sign in to comment.