Skip to content

Commit

Permalink
feat: detect feat. relationship from title and description
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Touron <[email protected]>
  • Loading branch information
moul committed Nov 12, 2020
1 parent 4a0ed06 commit f8de419
Show file tree
Hide file tree
Showing 7 changed files with 353 additions and 288 deletions.
16 changes: 8 additions & 8 deletions api/sgtm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ message User {
/// relationships

repeated Post recent_posts = 50 [(go.field) = {tags: 'gorm:"foreignkey:AuthorID;PRELOAD:false"'}];
repeated Relationship relationships_as_source = 51 [(go.field) = {tags: 'gorm:"many2many:user_relationship;"'}];
repeated Relationship relationships_as_target = 52 [(go.field) = {tags: 'gorm:"many2many:relationship_user;"'}];
repeated Relationship relationships_as_source = 51 [(go.field) = {tags: 'gorm:"foreignKey:SourceUserID"'}];
repeated Relationship relationships_as_target = 52 [(go.field) = {tags: 'gorm:"foreignKey:TargetUserID"'}];
}

message Post {
Expand Down Expand Up @@ -198,8 +198,8 @@ message Post {

/// relationships

repeated Relationship relationships_as_source = 110 [(go.field) = {tags: 'gorm:"many2many:post_relationship;"'}];
repeated Relationship relationships_as_target = 111 [(go.field) = {tags: 'gorm:"many2many:relationship_post;"'}];
repeated Relationship relationships_as_source = 110 [(go.field) = {tags: 'gorm:"foreignKey:SourcePostID"'}];
repeated Relationship relationships_as_target = 111 [(go.field) = {tags: 'gorm:"foreignKey:TargetPostID"'}];

enum SoundCloudKind {
UnknownSoundCloudKind = 0;
Expand Down Expand Up @@ -239,13 +239,13 @@ message Relationship {
/// relationship fields

Kind kind = 10;
int64 source_post_id = 11 [(go.field) = {name: 'SourcePostID', tags: 'gorm:"many2many:post_relationship;"'}];
int64 source_post_id = 11 [(go.field) = {name: 'SourcePostID'}];
Post source_post = 12;
int64 target_post_id = 13 [(go.field) = {name: 'TargetPostID', tags: 'gorm:"many2many:relationship_post;"'}];
int64 target_post_id = 13 [(go.field) = {name: 'TargetPostID'}];
Post target_post = 14;
int64 source_user_id = 15 [(go.field) = {name: 'SourceUserID', tags: 'gorm:"many2many:user_relationship;"'}];
int64 source_user_id = 15 [(go.field) = {name: 'SourceUserID'}];
User source_user = 16;
int64 target_user_id = 17 [(go.field) = {name: 'TargetUserID', tags: 'gorm:"many2many:relationship_user;"'}];
int64 target_user_id = 17 [(go.field) = {name: 'TargetUserID'}];
User target_user = 18;
string source_raw = 19;
string target_raw = 20;
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.

1 change: 1 addition & 0 deletions pkg/sgtm/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func DBInit(db *gorm.DB, sfn *snowflake.Node) (*gorm.DB, error) {
err = db.AutoMigrate(
&sgtmpb.User{},
&sgtmpb.Post{},
&sgtmpb.Relationship{},
)
if err != nil {
return nil, err
Expand Down
8 changes: 8 additions & 0 deletions pkg/sgtm/page_base.tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ <h4 class="text-white">@{{.User.Slug}}</h4>
{{end}}
{{end}}

{{define "user_link_with_pict_and_name"}}
{{if .}}
<a href="{{.CanonicalURL}}"><img src="{{.Avatar}}" width="20" height="20" /> {{.DisplayName}}</a>
{{else}}
<b>Anonymous</b>
{{end}}
{{end}}

{{define "navbar_brand"}}
<a href="/" class="navbar-brand d-flex align-items-center">
{{ if .Opts.DevMode }}
Expand Down
71 changes: 65 additions & 6 deletions pkg/sgtm/page_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ package sgtm
import (
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"time"

"github.com/go-chi/chi"
packr "github.com/gobuffalo/packr/v2"
"go.uber.org/zap"
"gorm.io/gorm"
"moul.io/godev"
"moul.io/sgtm/pkg/sgtmpb"
)

var (
featRegex = regexp.MustCompile(`(?im)(feat.|feat|featuring|features)\s*[:= ]\s*@([^\s,]+)\s*`)
)

func (svc *Service) postPage(box *packr.Box) func(w http.ResponseWriter, r *http.Request) {
tmpl := loadTemplates(box, "base.tmpl.html", "post.tmpl.html")

Expand All @@ -27,7 +33,14 @@ func (svc *Service) postPage(box *packr.Box) func(w http.ResponseWriter, r *http
// custom
data.PageKind = "post"
postSlug := chi.URLParam(r, "post_slug")
query := svc.rodb().Preload("Author")
query := svc.rodb().
Preload("Author").
Preload("RelationshipsAsSource").
Preload("RelationshipsAsSource.TargetPost").
Preload("RelationshipsAsSource.TargetUser").
Preload("RelationshipsAsTarget").
Preload("RelationshipsAsTarget.SourcePost").
Preload("RelationshipsAsTarget.SourceUser")
id, err := strconv.ParseInt(postSlug, 10, 64)
if err == nil {
query = query.Where(sgtmpb.Post{ID: id, Kind: sgtmpb.Post_TrackKind})
Expand Down Expand Up @@ -146,7 +159,10 @@ func (svc *Service) postMaintenancePage(box *packr.Box) func(w http.ResponseWrit
return
}
postSlug := chi.URLParam(r, "post_slug")
query := svc.rodb().Preload("Author")
query := svc.rodb().
Preload("Author").
Preload("RelationshipsAsSource").
Preload("RelationshipsAsTarget")
id, err := strconv.ParseInt(postSlug, 10, 64)
if err == nil {
query = query.Where(sgtmpb.Post{ID: id, Kind: sgtmpb.Post_TrackKind})
Expand Down Expand Up @@ -196,8 +212,46 @@ func (svc *Service) postMaintenancePage(box *packr.Box) func(w http.ResponseWrit
}

if shouldDetectRelationships {
svc.error404Page(box)(w, r)
return
// FIXME: support more relationship kinds

err := svc.rwdb().Transaction(func(tx *gorm.DB) error {
// FIXME: avoid delete/recreate associations if they didn't changed

body := post.Title + "\n\n" + post.SafeDescription()

if err := tx.Model(&post).Association("RelationshipsAsSource").Clear(); err != nil {
return err
}
if err := tx.Model(&post).Association("RelationshipsAsTarget").Clear(); err != nil {
return err
}

for _, match := range featRegex.FindAllStringSubmatch(body, -1) {
target := strings.ToLower(strings.TrimSpace(match[len(match)-1]))
var user sgtmpb.User
err := svc.rodb().
Where("LOWER(slug) = ?", target).
First(&user).
Error
if err != nil {
svc.logger.Debug("cannot find the featured artist in DB", zap.Error(err))
continue
}

if err := tx.Model(&post).Association("RelationshipsAsSource").Append(&sgtmpb.Relationship{
SourcePostID: post.ID,
TargetUserID: user.ID,
Kind: sgtmpb.Relationship_FeaturingUserKind,
}); err != nil {
return err
}
}
return nil
})
if err != nil {
svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity)
return
}
}

switch r.URL.Query().Get("return") {
Expand Down Expand Up @@ -243,7 +297,10 @@ func (svc *Service) postEditPage(box *packr.Box) func(w http.ResponseWriter, r *
// fetch post from db
{
postSlug := chi.URLParam(r, "post_slug")
query := svc.rodb().Preload("Author")
query := svc.rodb().
Preload("Author").
Preload("RelationshipsAsSource").
Preload("RelationshipsAsTarget")
id, err := strconv.ParseInt(postSlug, 10, 64)
if err == nil {
query = query.Where(sgtmpb.Post{ID: id, Kind: sgtmpb.Post_TrackKind})
Expand Down Expand Up @@ -309,7 +366,9 @@ func (svc *Service) postEditPage(box *packr.Box) func(w http.ResponseWriter, r *
func (svc *Service) postDownloadPage(box *packr.Box) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
postSlug := chi.URLParam(r, "post_slug")
query := svc.rodb().Preload("Author")
query := svc.rodb().
Preload("Author")

id, err := strconv.ParseInt(postSlug, 10, 64)
if err == nil {
query = query.Where(sgtmpb.Post{ID: id, Kind: sgtmpb.Post_TrackKind})
Expand Down
8 changes: 8 additions & 0 deletions pkg/sgtm/page_post.tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ <h1>{{.Post.Post.Title}}</h1>
<div>📆 Added <span data-toggle="tooltip" data-placement="right" title="{{.Post.Post.SortDate | fromUnixNano | prettyDate}}">{{.Post.Post.SortDate | fromUnixNano | prettyAgo}}</span></div>
<!--<div><span class="fa fa-soundcloud"></span> {{.Post.Post.URL}}</div>-->

<!-- RELATIONSHIPS -->
{{ range $rel := .Post.Post.RelationshipsAsSource }}
{{$kind := .Kind.String}}
{{with eq $kind "FeaturingUserKind"}}{{":handshake:" | emojify}} feat. {{template "user_link_with_pict_and_name" $rel.TargetUser}}{{end}}
{{end}}
<!-- FIXME: .Post.Post.RelationshipsAsTarget -->



{{if or (len .Post.Comments) (.User)}}
<div class="card mt-3 p-1">
Expand Down
Loading

0 comments on commit f8de419

Please sign in to comment.