-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
17 changed files
with
181 additions
and
21 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
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
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,2 @@ | ||
DROP TABLE public.embed_domain; | ||
DROP FUNCTION public.update_updated_at(); |
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,28 @@ | ||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; | ||
|
||
CREATE TABLE public.embed_domain ( | ||
embed_domain_id uuid DEFAULT uuid_generate_v4() NOT NULL, | ||
"domain" text NOT NULL, | ||
email text NOT NULL, | ||
created_at timestamptz DEFAULT now() NOT NULL, | ||
ads bool DEFAULT true NOT NULL, | ||
updated_at timestamptz DEFAULT now() NOT NULL, | ||
CONSTRAINT embed_domain_pk PRIMARY KEY (embed_domain_id), | ||
CONSTRAINT embed_domain_unique UNIQUE (domain) | ||
); | ||
|
||
CREATE OR REPLACE FUNCTION public.update_updated_at() | ||
RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS $function$ | ||
BEGIN | ||
NEW.updated_at = now(); | ||
RETURN NEW; | ||
END; | ||
$function$ | ||
; | ||
|
||
create trigger update_updated_at before | ||
update | ||
on | ||
public.embed_domain for each row execute function update_updated_at(); |
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,60 @@ | ||
package embed | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/go-pg/pg/v10" | ||
cs "github.com/webtor-io/common-services" | ||
"github.com/webtor-io/lazymap" | ||
"github.com/webtor-io/web-ui-v2/services/claims" | ||
"github.com/webtor-io/web-ui-v2/services/models" | ||
) | ||
|
||
type DomainSettings struct { | ||
lazymap.LazyMap | ||
pg *cs.PG | ||
claims *claims.Claims | ||
} | ||
type DomainSettingsData struct { | ||
Ads bool `json:"ads"` | ||
} | ||
|
||
func NewDomainSettings(pg *cs.PG, claims *claims.Claims) *DomainSettings { | ||
return &DomainSettings{ | ||
pg: pg, | ||
claims: claims, | ||
LazyMap: lazymap.New(&lazymap.Config{ | ||
Expire: time.Minute, | ||
ErrorExpire: 10 * time.Second, | ||
}), | ||
} | ||
} | ||
|
||
func (s *DomainSettings) get(domain string) (*DomainSettingsData, error) { | ||
if s.pg == nil || s.pg.Get() == nil || s.claims == nil { | ||
return &DomainSettingsData{}, nil | ||
} | ||
db := s.pg.Get() | ||
em := &models.EmbedDomain{} | ||
err := db.Model(em).Where("domain = ?", domain).Select() | ||
if err == pg.ErrNoRows { | ||
return &DomainSettingsData{Ads: true}, nil | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
cl, err := s.claims.Get(em.Email) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &DomainSettingsData{Ads: em.Ads || !cl.Claims.Embed.NoAds}, nil | ||
} | ||
|
||
func (s *DomainSettings) Get(domain string) (*DomainSettingsData, error) { | ||
resp, err := s.LazyMap.Get(domain, func() (interface{}, error) { | ||
return s.get(domain) | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return resp.(*DomainSettingsData), nil | ||
} |
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,17 @@ | ||
package models | ||
|
||
import ( | ||
"time" | ||
|
||
uuid "github.com/satori/go.uuid" | ||
) | ||
|
||
type EmbedDomain struct { | ||
tableName struct{} `pg:"embed_domain"` | ||
ID uuid.UUID `pg:"embed_domain_id,type:uuid,pk,default:uuid_generate_v4()"` | ||
Domain string | ||
Email string | ||
Ads bool | ||
CreatedAt time.Time | ||
UpdatedAt time.Time | ||
} |
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
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
{{ define "head_extra" }} | ||
{{ end }} | ||
{{ define "get_extra" }} | ||
{{ end }} | ||
{{ define "embed_extra" }} | ||
{{ end }} |
Oops, something went wrong.