Skip to content

Commit

Permalink
parse uri hostname into platform name
Browse files Browse the repository at this point in the history
  • Loading branch information
auggod committed Feb 16, 2022
1 parent 1919591 commit 442bd0c
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 234 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/golang/protobuf v1.4.3
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/uuid v1.1.2
github.com/goware/urlx v0.3.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4
github.com/grpc-ecosystem/grpc-gateway/v2 v2.0.1
github.com/jackc/pgx/v4 v4.11.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
Expand Down Expand Up @@ -212,6 +216,8 @@ github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/goware/urlx v0.3.1 h1:BbvKl8oiXtJAzOzMqAQ0GfIhf96fKeNEZfm9ocNSUBI=
github.com/goware/urlx v0.3.1/go.mod h1:h8uwbJy68o+tQXCGZNa9D73WN8n0r9OBae5bUnLcgjw=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4 h1:z53tR0945TRRQO/fLEVPI6SMv7ZflF0TEaTAoU7tOzg=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
Expand Down
2 changes: 1 addition & 1 deletion proto/user/streetaddress.validator.pb.go

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

2 changes: 1 addition & 1 deletion proto/user/tag.validator.pb.go

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

323 changes: 158 additions & 165 deletions proto/user/usergroup_messages.pb.go

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions proto/user/usergroup_messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ message UserGroupCreateRequest {
string avatar = 5; // UUID
string banner = 6; // UUID
string group_type = 8; // required UUID
repeated Tag tags = 9; // optional UUIDs
repeated string tags = 9; // optional UUIDs
string group_email = 10; // optional
repeated Link links = 11;
repeated string links = 11;
// repeated User followers = 9;
// repeated UserGroup members = 10;
// repeated UserGroup memberOfGroups = 11;
Expand All @@ -72,8 +72,8 @@ message UserGroupUpdateRequest {
optional string group_type = 7;
optional string group_email = 9;
optional string owner_id = 10;
repeated Link links = 11;
repeated Tag tags = 12;
repeated string links = 11;
repeated string tags = 12;

//optional StreetAddress address = 8;
//optional string owner_id = 7; // required
Expand Down
28 changes: 0 additions & 28 deletions proto/user/usergroup_messages.validator.pb.go

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

63 changes: 47 additions & 16 deletions server/usergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

uuid "github.com/google/uuid"
"github.com/goware/urlx"
"github.com/uptrace/bun"

"github.com/resonatecoop/user-api/model"
Expand Down Expand Up @@ -98,11 +99,12 @@ func (s *Server) AddUserGroup(ctx context.Context, usergroup *pbUser.UserGroupCr
if usergroup.Tags != nil {
tags := make([]model.Tag, len(usergroup.Tags))
names := make([]string, len(usergroup.Tags))
tagType := "genre" // defaults to genre tags for now

for i := range usergroup.Tags {
tag := model.Tag{
Name: usergroup.Tags[i].Name,
Type: "genre",
Name: usergroup.Tags[i],
Type: tagType,
}
tag.ID = uuid.Must(uuid.NewRandom())
names[i] = tag.Name
Expand All @@ -114,7 +116,7 @@ func (s *Server) AddUserGroup(ctx context.Context, usergroup *pbUser.UserGroupCr
// find existing tags
err := s.db.NewSelect().
Model(&existing).
Where("type = ? AND name IN (?)", "genre", bun.In(names)).
Where("type = ? AND name IN (?)", tagType, bun.In(names)).
Scan(ctx)

if err != nil {
Expand Down Expand Up @@ -161,9 +163,13 @@ func (s *Server) AddUserGroup(ctx context.Context, usergroup *pbUser.UserGroupCr
links := make([]model.Link, len(usergroup.Links))

for i := range usergroup.Links {
uri := usergroup.Links[i]

platform := s.getPlatform(uri)

link := model.Link{
URI: usergroup.Links[i].Uri,
Platform: usergroup.Links[i].Platform,
URI: usergroup.Links[i],
Platform: platform,
}
link.ID = uuid.Must(uuid.NewRandom())
uris[i] = link.URI
Expand Down Expand Up @@ -277,11 +283,12 @@ func (s *Server) UpdateUserGroup(ctx context.Context, UserGroupUpdateRequest *pb
if UserGroupUpdateRequest.Tags != nil {
tags := make([]model.Tag, len(UserGroupUpdateRequest.Tags))
names := make([]string, len(UserGroupUpdateRequest.Tags))
tagType := "genre" // defaults to genre tags for now

for i := range UserGroupUpdateRequest.Tags {
tag := model.Tag{
Name: UserGroupUpdateRequest.Tags[i].Name,
Type: "genre",
Name: UserGroupUpdateRequest.Tags[i],
Type: tagType,
}
tag.ID = uuid.Must(uuid.NewRandom())
names[i] = tag.Name
Expand All @@ -293,7 +300,7 @@ func (s *Server) UpdateUserGroup(ctx context.Context, UserGroupUpdateRequest *pb
// find existing tags
err := s.db.NewSelect().
Model(&existing).
Where("type = ? AND name IN (?)", "genre", bun.In(names)).
Where("type = ? AND name IN (?)", tagType, bun.In(names)).
Scan(ctx)

if err != nil {
Expand Down Expand Up @@ -340,9 +347,12 @@ func (s *Server) UpdateUserGroup(ctx context.Context, UserGroupUpdateRequest *pb
uris := make([]string, len(UserGroupUpdateRequest.Links))

for i := range UserGroupUpdateRequest.Links {
uri := UserGroupUpdateRequest.Links[i]
platform := s.getPlatform(uri)

link := model.Link{
URI: UserGroupUpdateRequest.Links[i].Uri,
Platform: UserGroupUpdateRequest.Links[i].Platform,
URI: uri,
Platform: platform,
}
link.ID = uuid.Must(uuid.NewRandom())
uris[i] = link.URI
Expand Down Expand Up @@ -393,6 +403,8 @@ func (s *Server) UpdateUserGroup(ctx context.Context, UserGroupUpdateRequest *pb
}
}

// TODO prune orphan links?

updatedUserGroupValues["links"] = result
}

Expand Down Expand Up @@ -556,17 +568,13 @@ func (s *Server) checkRequiredAddUserGroupAttributes(ctx context.Context, usergr

if usergroup.Links != nil {
for i := range usergroup.Links {
platform := usergroup.Links[i].Platform
uri := usergroup.Links[i].Uri
uri := usergroup.Links[i]

_, err := url.ParseRequestURI(uri)

if err != nil {
return fmt.Errorf("invalid url %v", uri)
}

if platform != "" && platform != "facebook" && platform != "twitter" && platform != "soundcloud" && platform != "youtube" && platform != "bandcamp" {
return fmt.Errorf("invalid platform %v", uri)
}
}
}

Expand All @@ -590,3 +598,26 @@ func (s *Server) checkRequiredAddUserGroupAttributes(ctx context.Context, usergr

return nil
}

func (s *Server) getPlatform(uri string) string {
parsed, _ := urlx.Parse(uri)
hostname := parsed.Hostname()
platform := ""

switch {
case hostname == "youtube.com" || hostname == "youtu.be":
platform = "youtube"
case hostname == "facebook.com":
platform = "facebook"
case hostname == "soundcloud.com":
platform = "soundcloud"
case hostname == "twitter.com":
platform = "twitter"
case hostname == "bandcamp.com":
platform = "bandcamp"
case hostname == "vimeo.com":
platform = "vimeo"
}

return platform
}
2 changes: 1 addition & 1 deletion statik/statik.go

Large diffs are not rendered by default.

22 changes: 4 additions & 18 deletions third_party/OpenAPI/user/user.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -499,20 +499,6 @@
}
}
},
"userTag": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string"
},
"name": {
"type": "string"
}
}
},
"userUserAddRequest": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -585,7 +571,7 @@
"tags": {
"type": "array",
"items": {
"$ref": "#/definitions/userTag"
"type": "string"
}
},
"groupEmail": {
Expand All @@ -594,7 +580,7 @@
"links": {
"type": "array",
"items": {
"$ref": "#/definitions/userLink"
"type": "string"
}
}
}
Expand Down Expand Up @@ -739,13 +725,13 @@
"links": {
"type": "array",
"items": {
"$ref": "#/definitions/userLink"
"type": "string"
}
},
"tags": {
"type": "array",
"items": {
"$ref": "#/definitions/userTag"
"type": "string"
}
}
}
Expand Down

0 comments on commit 442bd0c

Please sign in to comment.