mirror of
https://github.com/documize/community.git
synced 2025-08-04 21:15:24 +02:00
removed old API
This commit is contained in:
parent
e284a46f86
commit
562872a4a8
105 changed files with 337 additions and 14496 deletions
|
@ -15,12 +15,14 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/section/provider"
|
||||
)
|
||||
|
||||
// Provider represents Airtable
|
||||
type Provider struct {
|
||||
Runtime env.Runtime
|
||||
Runtime *env.Runtime
|
||||
Store *domain.Store
|
||||
}
|
||||
|
||||
// Meta describes us
|
||||
|
|
|
@ -15,12 +15,14 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/section/provider"
|
||||
)
|
||||
|
||||
// Provider represents code snippet
|
||||
type Provider struct {
|
||||
Runtime env.Runtime
|
||||
Runtime *env.Runtime
|
||||
Store *domain.Store
|
||||
}
|
||||
|
||||
// Meta describes us.
|
||||
|
|
|
@ -69,7 +69,7 @@ func (h *Handler) RunSectionCommand(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
if !provider.Command(sectionName, provider.NewContext(ctx.OrgID, ctx.UserID), w, r) {
|
||||
if !provider.Command(sectionName, provider.NewContext(ctx.OrgID, ctx.UserID, ctx), w, r) {
|
||||
h.Runtime.Log.Info("Unable to run provider.Command() for: " + sectionName)
|
||||
response.WriteNotFoundError(w, "RunSectionCommand", sectionName)
|
||||
}
|
||||
|
@ -122,7 +122,7 @@ func (h *Handler) RefreshSections(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
pcontext := provider.NewContext(pm.OrgID, pm.UserID)
|
||||
pcontext := provider.NewContext(pm.OrgID, pm.UserID, ctx)
|
||||
|
||||
// Ask for data refresh
|
||||
data, ok := provider.Refresh(page.ContentType, pcontext, pm.Config, pm.RawBody)
|
||||
|
|
|
@ -21,12 +21,14 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/section/provider"
|
||||
)
|
||||
|
||||
// Provider represents Gemini
|
||||
type Provider struct {
|
||||
Runtime env.Runtime
|
||||
Runtime *env.Runtime
|
||||
Store *domain.Store
|
||||
}
|
||||
|
||||
// Meta describes us.
|
||||
|
@ -66,7 +68,7 @@ func (*Provider) Render(ctx *provider.Context, config, data string) string {
|
|||
}
|
||||
|
||||
// Command handles authentication, workspace listing and items retrieval.
|
||||
func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
||||
func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query()
|
||||
method := query.Get("method")
|
||||
|
||||
|
@ -77,13 +79,13 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R
|
|||
|
||||
switch method {
|
||||
case "secrets":
|
||||
secs(ctx, w, r)
|
||||
secs(ctx, p.Store, w, r)
|
||||
case "auth":
|
||||
auth(ctx, w, r)
|
||||
auth(ctx, p.Store, w, r)
|
||||
case "workspace":
|
||||
workspace(ctx, w, r)
|
||||
workspace(ctx, p.Store, w, r)
|
||||
case "items":
|
||||
items(ctx, w, r)
|
||||
items(ctx, p.Store, w, r)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,7 +99,7 @@ func (p *Provider) Refresh(ctx *provider.Context, config, data string) (newData
|
|||
return
|
||||
}
|
||||
|
||||
c.Clean(ctx)
|
||||
c.Clean(ctx, p.Store)
|
||||
|
||||
if len(c.URL) == 0 {
|
||||
p.Runtime.Log.Info("Gemini.Refresh received empty URL")
|
||||
|
@ -153,7 +155,7 @@ func (p *Provider) Refresh(ctx *provider.Context, config, data string) (newData
|
|||
return
|
||||
}
|
||||
|
||||
func auth(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
||||
func auth(ctx *provider.Context, store *domain.Store, w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
|
||||
|
@ -170,7 +172,7 @@ func auth(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
config.Clean(nil) // don't look at the database for the parameters
|
||||
config.Clean(nil, store) // don't look at the database for the parameters
|
||||
|
||||
if len(config.URL) == 0 {
|
||||
provider.WriteMessage(w, "gemini", "Missing URL value")
|
||||
|
@ -205,7 +207,7 @@ func auth(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
config.SaveSecrets(ctx)
|
||||
config.SaveSecrets(ctx, store)
|
||||
|
||||
defer res.Body.Close()
|
||||
var g = geminiUser{}
|
||||
|
@ -221,7 +223,7 @@ func auth(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
|||
provider.WriteJSON(w, g)
|
||||
}
|
||||
|
||||
func workspace(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
||||
func workspace(ctx *provider.Context, store *domain.Store, w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
|
||||
|
@ -238,7 +240,7 @@ func workspace(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
config.Clean(ctx)
|
||||
config.Clean(ctx, store)
|
||||
|
||||
if len(config.URL) == 0 {
|
||||
provider.WriteMessage(w, "gemini", "Missing URL value")
|
||||
|
@ -293,7 +295,7 @@ func workspace(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
|||
provider.WriteJSON(w, workspace)
|
||||
}
|
||||
|
||||
func items(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
||||
func items(ctx *provider.Context, store *domain.Store, w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
|
||||
|
@ -310,7 +312,7 @@ func items(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
config.Clean(ctx)
|
||||
config.Clean(ctx, store)
|
||||
|
||||
if len(config.URL) == 0 {
|
||||
provider.WriteMessage(w, "gemini", "Missing URL value")
|
||||
|
@ -368,7 +370,7 @@ func items(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
|||
provider.WriteJSON(w, items)
|
||||
}
|
||||
|
||||
func secs(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
|
||||
sec, _ := getSecrets(ctx)
|
||||
func secs(ctx *provider.Context, store *domain.Store, w http.ResponseWriter, r *http.Request) {
|
||||
sec, _ := getSecrets(ctx, store)
|
||||
provider.WriteJSON(w, sec)
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ package gemini
|
|||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/section/provider"
|
||||
)
|
||||
|
||||
|
@ -86,9 +87,9 @@ type geminiConfig struct {
|
|||
Filter map[string]interface{} `json:"filter"`
|
||||
}
|
||||
|
||||
func (c *geminiConfig) Clean(ctx *provider.Context) {
|
||||
func (c *geminiConfig) Clean(ctx *provider.Context, store *domain.Store) {
|
||||
if ctx != nil {
|
||||
sec, err := getSecrets(ctx)
|
||||
sec, err := getSecrets(ctx, store)
|
||||
if err == nil {
|
||||
if len(sec.APIKey) > 0 && len(sec.Username) > 0 && len(sec.URL) > 0 {
|
||||
c.APIKey = strings.TrimSpace(sec.APIKey)
|
||||
|
@ -102,12 +103,12 @@ func (c *geminiConfig) Clean(ctx *provider.Context) {
|
|||
c.URL = strings.TrimSpace(c.URL)
|
||||
}
|
||||
|
||||
func (c *geminiConfig) SaveSecrets(ctx *provider.Context) {
|
||||
func (c *geminiConfig) SaveSecrets(ctx *provider.Context, store *domain.Store) {
|
||||
var sec secrets
|
||||
sec.APIKey = strings.TrimSpace(c.APIKey)
|
||||
sec.Username = strings.TrimSpace(c.Username)
|
||||
sec.URL = strings.TrimSpace(c.URL)
|
||||
ctx.MarshalSecrets(sec)
|
||||
ctx.MarshalSecrets(sec, store)
|
||||
}
|
||||
|
||||
type secrets struct {
|
||||
|
@ -116,7 +117,7 @@ type secrets struct {
|
|||
APIKey string `json:"apikey"`
|
||||
}
|
||||
|
||||
func getSecrets(ctx *provider.Context) (sec secrets, err error) {
|
||||
err = ctx.UnmarshalSecrets(&sec)
|
||||
func getSecrets(ctx *provider.Context, store *domain.Store) (sec secrets, err error) {
|
||||
err = ctx.UnmarshalSecrets(&sec, store)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -17,32 +17,33 @@ import (
|
|||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/documize/community/core/api/request"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/section/provider"
|
||||
gogithub "github.com/google/go-github/github"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
func clientID() string {
|
||||
return request.ConfigString(meta.ConfigHandle(), "clientID")
|
||||
func clientID(ctx domain.RequestContext, s *domain.Store) string {
|
||||
return s.Setting.Get(meta.ConfigHandle(), "clientID")
|
||||
}
|
||||
|
||||
func clientSecret() string {
|
||||
return request.ConfigString(meta.ConfigHandle(), "clientSecret")
|
||||
func clientSecret(ctx domain.RequestContext, s *domain.Store) string {
|
||||
return s.Setting.Get(meta.ConfigHandle(), "clientSecret")
|
||||
}
|
||||
|
||||
func authorizationCallbackURL() string {
|
||||
func authorizationCallbackURL(ctx domain.RequestContext, s *domain.Store) string {
|
||||
// NOTE: URL value must have the path and query "/api/public/validate?section=github"
|
||||
return request.ConfigString(meta.ConfigHandle(), "authorizationCallbackURL")
|
||||
return s.Setting.Get(meta.ConfigHandle(), "authorizationCallbackURL")
|
||||
}
|
||||
|
||||
func validateToken(ptoken string) error {
|
||||
func validateToken(ctx provider.Context, s *domain.Store, ptoken string) error {
|
||||
// Github authorization check
|
||||
authClient := gogithub.NewClient((&gogithub.BasicAuthTransport{
|
||||
Username: clientID(),
|
||||
Password: clientSecret(),
|
||||
Username: clientID(ctx.Request, s),
|
||||
Password: clientSecret(ctx.Request, s),
|
||||
}).Client())
|
||||
_, _, err := authClient.Authorizations.Check(clientID(), ptoken)
|
||||
_, _, err := authClient.Authorizations.Check(clientID(ctx.Request, s), ptoken)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -56,14 +57,15 @@ func (*Provider) githubClient(config *githubConfig) *gogithub.Client {
|
|||
}
|
||||
|
||||
// Callback is called by a browser redirect from Github, via the validation endpoint
|
||||
func Callback(res http.ResponseWriter, req *http.Request) error {
|
||||
func Callback(rt *env.Runtime, s *domain.Store, res http.ResponseWriter, req *http.Request) error {
|
||||
ctx := domain.GetRequestContext(req)
|
||||
|
||||
code := req.URL.Query().Get("code")
|
||||
state := req.URL.Query().Get("state")
|
||||
|
||||
ghurl := "https://github.com/login/oauth/access_token"
|
||||
vals := "client_id=" + clientID()
|
||||
vals += "&client_secret=" + clientSecret()
|
||||
vals := "client_id=" + clientID(ctx, s)
|
||||
vals += "&client_secret=" + clientSecret(ctx, s)
|
||||
vals += "&code=" + code
|
||||
vals += "&state=" + state
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/section/provider"
|
||||
gogithub "github.com/google/go-github/github"
|
||||
)
|
||||
|
@ -43,7 +44,8 @@ func init() {
|
|||
|
||||
// Provider represents GitHub
|
||||
type Provider struct {
|
||||
Runtime env.Runtime
|
||||
Runtime *env.Runtime
|
||||
Store *domain.Store
|
||||
}
|
||||
|
||||
// Meta describes us.
|
||||
|
@ -67,8 +69,8 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
|
|||
CID string `json:"clientID"`
|
||||
URL string `json:"authorizationCallbackURL"`
|
||||
}
|
||||
ret.CID = clientID()
|
||||
ret.URL = authorizationCallbackURL()
|
||||
ret.CID = clientID(ctx.Request, p.Store)
|
||||
ret.URL = authorizationCallbackURL(ctx.Request, p.Store)
|
||||
provider.WriteJSON(w, ret)
|
||||
return
|
||||
}
|
||||
|
@ -86,7 +88,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
|
|||
if method == "saveSecret" { // secret Token update code
|
||||
|
||||
// write the new one, direct from JS
|
||||
if err = ctx.SaveSecrets(string(body)); err != nil {
|
||||
if err = ctx.SaveSecrets(string(body), p.Store); err != nil {
|
||||
p.Runtime.Log.Error("github settoken configuration", err)
|
||||
provider.WriteError(w, "github", err)
|
||||
return
|
||||
|
@ -108,7 +110,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
|
|||
|
||||
config.Clean()
|
||||
// always use DB version of the token
|
||||
config.Token = ctx.GetSecrets("token") // get the secret token in the database
|
||||
config.Token = ctx.GetSecrets("token", p.Store) // get the secret token in the database
|
||||
|
||||
client := p.githubClient(&config)
|
||||
|
||||
|
@ -119,11 +121,11 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
|
|||
if len(config.Token) == 0 {
|
||||
err = errors.New("empty github token")
|
||||
} else {
|
||||
err = validateToken(config.Token)
|
||||
err = validateToken(*ctx, p.Store, config.Token)
|
||||
}
|
||||
if err != nil {
|
||||
// token now invalid, so wipe it
|
||||
ctx.SaveSecrets("") // ignore error, already in an error state
|
||||
ctx.SaveSecrets("", p.Store) // ignore error, already in an error state
|
||||
p.Runtime.Log.Error("github check token validation", err)
|
||||
provider.WriteError(w, "github", err)
|
||||
return
|
||||
|
@ -156,7 +158,7 @@ func (p *Provider) Refresh(ctx *provider.Context, configJSON, data string) strin
|
|||
}
|
||||
|
||||
c.Clean()
|
||||
c.Token = ctx.GetSecrets("token")
|
||||
c.Token = ctx.GetSecrets("token", p.Store)
|
||||
|
||||
client := p.githubClient(&c)
|
||||
|
||||
|
@ -193,7 +195,7 @@ func (p *Provider) Render(ctx *provider.Context, config, data string) string {
|
|||
}
|
||||
|
||||
c.Clean()
|
||||
c.Token = ctx.GetSecrets("token")
|
||||
c.Token = ctx.GetSecrets("token", p.Store)
|
||||
|
||||
data = strings.TrimSpace(data)
|
||||
if len(data) == 0 {
|
||||
|
|
|
@ -20,7 +20,7 @@ import (
|
|||
gogithub "github.com/google/go-github/github"
|
||||
)
|
||||
|
||||
func listFailed(rt env.Runtime, method string, config githubConfig, client *gogithub.Client, w http.ResponseWriter) (failed bool) {
|
||||
func listFailed(rt *env.Runtime, method string, config githubConfig, client *gogithub.Client, w http.ResponseWriter) (failed bool) {
|
||||
switch method { // which list to choose?
|
||||
|
||||
case "owners":
|
||||
|
|
|
@ -16,12 +16,14 @@ import (
|
|||
|
||||
"github.com/documize/blackfriday"
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/section/provider"
|
||||
)
|
||||
|
||||
// Provider represents Markdown
|
||||
type Provider struct {
|
||||
Runtime env.Runtime
|
||||
Runtime *env.Runtime
|
||||
Store *domain.Store
|
||||
}
|
||||
|
||||
// Meta describes us
|
||||
|
|
|
@ -22,6 +22,7 @@ import (
|
|||
"net/url"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/section/provider"
|
||||
)
|
||||
|
||||
|
@ -29,7 +30,8 @@ const me = "papertrail"
|
|||
|
||||
// Provider represents Papertrail
|
||||
type Provider struct {
|
||||
Runtime env.Runtime
|
||||
Runtime *env.Runtime
|
||||
Store *domain.Store
|
||||
}
|
||||
|
||||
// Meta describes us.
|
||||
|
@ -45,7 +47,7 @@ func (*Provider) Meta() provider.TypeMeta {
|
|||
}
|
||||
|
||||
// Render converts Papertrail data into HTML suitable for browser rendering.
|
||||
func (*Provider) Render(ctx *provider.Context, config, data string) string {
|
||||
func (p *Provider) Render(ctx *provider.Context, config, data string) string {
|
||||
var search papertrailSearch
|
||||
var events []papertrailEvent
|
||||
var payload = papertrailRender{}
|
||||
|
@ -54,7 +56,7 @@ func (*Provider) Render(ctx *provider.Context, config, data string) string {
|
|||
json.Unmarshal([]byte(data), &search)
|
||||
json.Unmarshal([]byte(config), &c)
|
||||
|
||||
c.APIToken = ctx.GetSecrets("APIToken")
|
||||
c.APIToken = ctx.GetSecrets("APIToken", p.Store)
|
||||
|
||||
max := len(search.Events)
|
||||
if c.Max < max {
|
||||
|
@ -107,7 +109,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
|
|||
config.Clean()
|
||||
|
||||
if config.APIToken == provider.SecretReplacement || config.APIToken == "" {
|
||||
config.APIToken = ctx.GetSecrets("APIToken")
|
||||
config.APIToken = ctx.GetSecrets("APIToken", p.Store)
|
||||
}
|
||||
|
||||
if len(config.APIToken) == 0 {
|
||||
|
@ -117,7 +119,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
|
|||
|
||||
switch method {
|
||||
case "auth":
|
||||
auth(p.Runtime, ctx, config, w, r)
|
||||
auth(p.Runtime, p.Store, ctx, config, w, r)
|
||||
case "options":
|
||||
options(config, w, r)
|
||||
}
|
||||
|
@ -135,7 +137,7 @@ func (p *Provider) Refresh(ctx *provider.Context, config, data string) (newData
|
|||
|
||||
c.Clean()
|
||||
|
||||
c.APIToken = ctx.GetSecrets("APIToken")
|
||||
c.APIToken = ctx.GetSecrets("APIToken", p.Store)
|
||||
|
||||
if len(c.APIToken) == 0 {
|
||||
p.Runtime.Log.Error("missing API token", err)
|
||||
|
@ -160,7 +162,7 @@ func (p *Provider) Refresh(ctx *provider.Context, config, data string) (newData
|
|||
return
|
||||
}
|
||||
|
||||
func auth(rt env.Runtime, ctx *provider.Context, config papertrailConfig, w http.ResponseWriter, r *http.Request) {
|
||||
func auth(rt *env.Runtime, store *domain.Store, ctx *provider.Context, config papertrailConfig, w http.ResponseWriter, r *http.Request) {
|
||||
result, err := fetchEvents(rt, config)
|
||||
|
||||
if result == nil {
|
||||
|
@ -169,7 +171,7 @@ func auth(rt env.Runtime, ctx *provider.Context, config papertrailConfig, w http
|
|||
|
||||
if err != nil {
|
||||
|
||||
ctx.SaveSecrets(`{"APIToken":""}`) // invalid token, so reset it
|
||||
ctx.SaveSecrets(`{"APIToken":""}`, store) // invalid token, so reset it
|
||||
|
||||
if err.Error() == "forbidden" {
|
||||
provider.WriteForbidden(w)
|
||||
|
@ -180,7 +182,7 @@ func auth(rt env.Runtime, ctx *provider.Context, config papertrailConfig, w http
|
|||
return
|
||||
}
|
||||
|
||||
ctx.SaveSecrets(`{"APIToken":"` + config.APIToken + `"}`)
|
||||
ctx.SaveSecrets(`{"APIToken":"`+config.APIToken+`"}`, store)
|
||||
|
||||
provider.WriteJSON(w, result)
|
||||
}
|
||||
|
@ -253,7 +255,7 @@ func options(config papertrailConfig, w http.ResponseWriter, r *http.Request) {
|
|||
provider.WriteJSON(w, options)
|
||||
}
|
||||
|
||||
func fetchEvents(rt env.Runtime, config papertrailConfig) (result interface{}, err error) {
|
||||
func fetchEvents(rt *env.Runtime, config papertrailConfig) (result interface{}, err error) {
|
||||
var filter string
|
||||
if len(config.Query) > 0 {
|
||||
filter = fmt.Sprintf("q=%s", url.QueryEscape(config.Query))
|
||||
|
|
|
@ -19,8 +19,8 @@ import (
|
|||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/documize/community/core/api/request"
|
||||
"github.com/documize/community/core/log"
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
)
|
||||
|
||||
// SecretReplacement is a constant used to replace secrets in data-structures when required.
|
||||
|
@ -32,14 +32,14 @@ var sectionsMap = make(map[string]Provider)
|
|||
|
||||
// TypeMeta details a "smart section" that represents a "page" in a document.
|
||||
type TypeMeta struct {
|
||||
ID string `json:"id"`
|
||||
Order int `json:"order"`
|
||||
ContentType string `json:"contentType"`
|
||||
PageType string `json:"pageType"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Preview bool `json:"preview"` // coming soon!
|
||||
Callback func(http.ResponseWriter, *http.Request) error `json:"-"`
|
||||
ID string `json:"id"`
|
||||
Order int `json:"order"`
|
||||
ContentType string `json:"contentType"`
|
||||
PageType string `json:"pageType"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Preview bool `json:"preview"` // coming soon!
|
||||
Callback func(*env.Runtime, *domain.Store, http.ResponseWriter, *http.Request) error `json:"-"`
|
||||
}
|
||||
|
||||
// ConfigHandle returns the key name for database config table
|
||||
|
@ -61,14 +61,12 @@ type Context struct {
|
|||
UserID string
|
||||
prov Provider
|
||||
inCommand bool
|
||||
Request domain.RequestContext
|
||||
}
|
||||
|
||||
// NewContext is a convenience function.
|
||||
func NewContext(orgid, userid string) *Context {
|
||||
if orgid == "" || userid == "" {
|
||||
log.Error("NewContext incorrect orgid:"+orgid+" userid:"+userid, errors.New("bad section context"))
|
||||
}
|
||||
return &Context{OrgID: orgid, UserID: userid}
|
||||
func NewContext(orgid, userid string, ctx domain.RequestContext) *Context {
|
||||
return &Context{OrgID: orgid, UserID: userid, Request: ctx}
|
||||
}
|
||||
|
||||
// Register makes document section type available
|
||||
|
@ -104,11 +102,11 @@ func Command(section string, ctx *Context, w http.ResponseWriter, r *http.Reques
|
|||
}
|
||||
|
||||
// Callback passes parameters to the given section callback, the returned error indicates success.
|
||||
func Callback(section string, w http.ResponseWriter, r *http.Request) error {
|
||||
func Callback(section string, rt *env.Runtime, store *domain.Store, w http.ResponseWriter, r *http.Request) error {
|
||||
s, ok := sectionsMap[section]
|
||||
if ok {
|
||||
if cb := s.Meta().Callback; cb != nil {
|
||||
return cb(w, r)
|
||||
return cb(rt, store, w, r)
|
||||
}
|
||||
}
|
||||
return errors.New("section not found")
|
||||
|
@ -147,57 +145,47 @@ func WriteJSON(w http.ResponseWriter, v interface{}) {
|
|||
}
|
||||
|
||||
_, err = w.Write(j)
|
||||
log.IfErr(err)
|
||||
}
|
||||
|
||||
// WriteString writes string tp HTTP response.
|
||||
func WriteString(w http.ResponseWriter, data string) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, err := w.Write([]byte(data))
|
||||
log.IfErr(err)
|
||||
w.Write([]byte(data))
|
||||
}
|
||||
|
||||
// WriteEmpty returns just OK to HTTP response.
|
||||
func WriteEmpty(w http.ResponseWriter) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, err := w.Write([]byte("{}"))
|
||||
log.IfErr(err)
|
||||
w.Write([]byte("{}"))
|
||||
}
|
||||
|
||||
// WriteMarshalError write JSON marshalling error to HTTP response.
|
||||
func WriteMarshalError(w http.ResponseWriter, err error) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_, err2 := w.Write([]byte("{Error: 'JSON marshal failed'}"))
|
||||
log.IfErr(err2)
|
||||
log.Error("JSON marshall failed", err)
|
||||
w.Write([]byte("{Error: 'JSON marshal failed'}"))
|
||||
}
|
||||
|
||||
// WriteMessage write string to HTTP response.
|
||||
func WriteMessage(w http.ResponseWriter, section, msg string) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_, err := w.Write([]byte("{Message: " + msg + "}"))
|
||||
log.IfErr(err)
|
||||
log.Info(fmt.Sprintf("Error for section %s: %s", section, msg))
|
||||
w.Write([]byte("{Message: " + msg + "}"))
|
||||
}
|
||||
|
||||
// WriteError write given error to HTTP response.
|
||||
func WriteError(w http.ResponseWriter, section string, err error) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_, err2 := w.Write([]byte("{Error: 'Internal server error'}"))
|
||||
log.IfErr(err2)
|
||||
log.Error(fmt.Sprintf("Error for section %s", section), err)
|
||||
w.Write([]byte("{Error: 'Internal server error'}"))
|
||||
}
|
||||
|
||||
// WriteForbidden write 403 to HTTP response.
|
||||
func WriteForbidden(w http.ResponseWriter) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
_, err := w.Write([]byte("{Error: 'Unauthorized'}"))
|
||||
log.IfErr(err)
|
||||
w.Write([]byte("{Error: 'Unauthorized'}"))
|
||||
}
|
||||
|
||||
// Secrets handling
|
||||
|
@ -206,17 +194,17 @@ func WriteForbidden(w http.ResponseWriter) {
|
|||
// The secrets must be in the form of a JSON format string, for example `{"mysecret":"lover"}`.
|
||||
// An empty string signifies no valid secrets for this user/org combination.
|
||||
// Note that this function can only be called within the Command method of a section.
|
||||
func (c *Context) SaveSecrets(JSONobj string) error {
|
||||
func (c *Context) SaveSecrets(JSONobj string, s *domain.Store) error {
|
||||
if !c.inCommand {
|
||||
return errors.New("SaveSecrets() may only be called from within Command()")
|
||||
}
|
||||
m := c.prov.Meta()
|
||||
return request.UserConfigSetJSON(c.OrgID, c.UserID, m.ContentType, JSONobj)
|
||||
return s.Setting.SetUser(c.OrgID, c.UserID, m.ContentType, JSONobj)
|
||||
}
|
||||
|
||||
// MarshalSecrets to the database.
|
||||
// Parameter the same as for json.Marshal().
|
||||
func (c *Context) MarshalSecrets(sec interface{}) error {
|
||||
func (c *Context) MarshalSecrets(sec interface{}, s *domain.Store) error {
|
||||
if !c.inCommand {
|
||||
return errors.New("MarshalSecrets() may only be called from within Command()")
|
||||
}
|
||||
|
@ -224,7 +212,7 @@ func (c *Context) MarshalSecrets(sec interface{}) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.SaveSecrets(string(byts))
|
||||
return c.SaveSecrets(string(byts), s)
|
||||
}
|
||||
|
||||
// GetSecrets for the current context user/org.
|
||||
|
@ -232,9 +220,9 @@ func (c *Context) MarshalSecrets(sec interface{}) error {
|
|||
// JSONpath format is defined at https://dev.mysql.com/doc/refman/5.7/en/json-path-syntax.html .
|
||||
// An empty JSONpath returns the whole JSON object, as JSON.
|
||||
// Errors return the empty string.
|
||||
func (c *Context) GetSecrets(JSONpath string) string {
|
||||
func (c *Context) GetSecrets(JSONpath string, s *domain.Store) string {
|
||||
m := c.prov.Meta()
|
||||
return request.UserConfigGetJSON(c.OrgID, c.UserID, m.ContentType, JSONpath)
|
||||
return s.Setting.GetUser(c.OrgID, c.UserID, m.ContentType, JSONpath)
|
||||
}
|
||||
|
||||
// ErrNoSecrets is returned if no secret is found in the database.
|
||||
|
@ -242,8 +230,8 @@ var ErrNoSecrets = errors.New("no secrets in database")
|
|||
|
||||
// UnmarshalSecrets from the database.
|
||||
// Parameter the same as for "v" in json.Unmarshal().
|
||||
func (c *Context) UnmarshalSecrets(v interface{}) error {
|
||||
secTxt := c.GetSecrets("") // get all the json of the secrets
|
||||
func (c *Context) UnmarshalSecrets(v interface{}, s *domain.Store) error {
|
||||
secTxt := c.GetSecrets("", s) // get all the json of the secrets
|
||||
if len(secTxt) > 0 {
|
||||
return json.Unmarshal([]byte(secTxt), v)
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import (
|
|||
"fmt"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/section/airtable"
|
||||
"github.com/documize/community/domain/section/code"
|
||||
"github.com/documize/community/domain/section/gemini"
|
||||
|
@ -28,17 +29,17 @@ import (
|
|||
)
|
||||
|
||||
// Register sections
|
||||
func Register(rt env.Runtime) {
|
||||
provider.Register("code", &code.Provider{Runtime: rt})
|
||||
provider.Register("gemini", &gemini.Provider{Runtime: rt})
|
||||
provider.Register("github", &github.Provider{Runtime: rt})
|
||||
provider.Register("markdown", &markdown.Provider{Runtime: rt})
|
||||
provider.Register("papertrail", &papertrail.Provider{Runtime: rt})
|
||||
provider.Register("table", &table.Provider{Runtime: rt})
|
||||
provider.Register("code", &code.Provider{Runtime: rt})
|
||||
provider.Register("trello", &trello.Provider{Runtime: rt})
|
||||
provider.Register("wysiwyg", &wysiwyg.Provider{Runtime: rt})
|
||||
provider.Register("airtable", &airtable.Provider{Runtime: rt})
|
||||
func Register(rt *env.Runtime, s *domain.Store) {
|
||||
provider.Register("code", &code.Provider{Runtime: rt, Store: s})
|
||||
provider.Register("gemini", &gemini.Provider{Runtime: rt, Store: s})
|
||||
provider.Register("github", &github.Provider{Runtime: rt, Store: s})
|
||||
provider.Register("markdown", &markdown.Provider{Runtime: rt, Store: s})
|
||||
provider.Register("papertrail", &papertrail.Provider{Runtime: rt, Store: s})
|
||||
provider.Register("table", &table.Provider{Runtime: rt, Store: s})
|
||||
provider.Register("code", &code.Provider{Runtime: rt, Store: s})
|
||||
provider.Register("trello", &trello.Provider{Runtime: rt, Store: s})
|
||||
provider.Register("wysiwyg", &wysiwyg.Provider{Runtime: rt, Store: s})
|
||||
provider.Register("airtable", &airtable.Provider{Runtime: rt, Store: s})
|
||||
|
||||
p := provider.List()
|
||||
rt.Log.Info(fmt.Sprintf("Registered %d sections", len(p)))
|
||||
|
|
|
@ -1,82 +0,0 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package section
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/documize/community/domain/section/provider"
|
||||
)
|
||||
|
||||
type testsection provider.TypeMeta
|
||||
|
||||
var ts testsection
|
||||
|
||||
func init() {
|
||||
provider.Register("testsection", &ts)
|
||||
}
|
||||
|
||||
// Command is an end-point...
|
||||
func (ts *testsection) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {}
|
||||
|
||||
var didRefresh bool
|
||||
|
||||
// Refresh existing data, returning data in the format of the target system
|
||||
func (ts *testsection) Refresh(ctx *provider.Context, meta, data string) string {
|
||||
didRefresh = true
|
||||
return ""
|
||||
}
|
||||
|
||||
// Render converts data in the target system format into HTML
|
||||
func (*testsection) Render(ctx *provider.Context, meta, data string) string {
|
||||
return "testsection " + data
|
||||
}
|
||||
|
||||
func (*testsection) Meta() provider.TypeMeta {
|
||||
section := provider.TypeMeta{}
|
||||
|
||||
section.ID = "TestGUID"
|
||||
section.Title = "TestSection"
|
||||
section.Description = "A Test Section"
|
||||
section.ContentType = "testsection"
|
||||
|
||||
return section
|
||||
}
|
||||
|
||||
func TestSection(t *testing.T) {
|
||||
ctx := provider.NewContext("_orgid_", "_userid_")
|
||||
|
||||
if _, ok := provider.Refresh("testsection", ctx, "", ""); !ok {
|
||||
t.Error("did not find 'testsection' smart section (1)")
|
||||
}
|
||||
if !didRefresh {
|
||||
t.Error("did not run the test Refresh method")
|
||||
}
|
||||
out, ok := provider.Render("testsection", ctx, "meta", "dingbat")
|
||||
if !ok {
|
||||
t.Error("did not find 'testsection' smart section (2)")
|
||||
}
|
||||
if out != "testsection dingbat" {
|
||||
t.Error("wrong output from Render")
|
||||
}
|
||||
|
||||
sects := provider.GetSectionMeta()
|
||||
for _, v := range sects {
|
||||
if v.Title == "TestSection" {
|
||||
return
|
||||
}
|
||||
//t.Logf("%v %v", v.Order, v.Title)
|
||||
}
|
||||
t.Error("TestSection not in meta output")
|
||||
|
||||
}
|
|
@ -15,12 +15,14 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/section/provider"
|
||||
)
|
||||
|
||||
// Provider represents Table
|
||||
type Provider struct {
|
||||
Runtime env.Runtime
|
||||
Runtime *env.Runtime
|
||||
Store *domain.Store
|
||||
}
|
||||
|
||||
// Meta describes us
|
||||
|
|
|
@ -19,8 +19,8 @@ import (
|
|||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/documize/community/core/api/request"
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/section/provider"
|
||||
)
|
||||
|
||||
|
@ -38,7 +38,8 @@ func init() {
|
|||
|
||||
// Provider represents Trello
|
||||
type Provider struct {
|
||||
Runtime env.Runtime
|
||||
Runtime *env.Runtime
|
||||
Store *domain.Store
|
||||
}
|
||||
|
||||
// Meta describes us.
|
||||
|
@ -68,7 +69,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
|
|||
}
|
||||
|
||||
config.Clean()
|
||||
config.AppKey = request.ConfigString(meta.ConfigHandle(), "appKey")
|
||||
config.AppKey = p.Store.Setting.Get(meta.ConfigHandle(), "appKey")
|
||||
|
||||
if len(config.AppKey) == 0 {
|
||||
p.Runtime.Log.Info("missing trello App Key")
|
||||
|
@ -77,7 +78,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
|
|||
}
|
||||
|
||||
if len(config.Token) == 0 {
|
||||
config.Token = ctx.GetSecrets("token") // get a token, if we have one
|
||||
config.Token = ctx.GetSecrets("token", p.Store) // get a token, if we have one
|
||||
}
|
||||
|
||||
if method != "config" {
|
||||
|
@ -94,7 +95,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
|
|||
if err != nil {
|
||||
p.Runtime.Log.Error("failed to render cards", err)
|
||||
provider.WriteError(w, "trello", err)
|
||||
ctx.SaveSecrets("") // failure means our secrets are invalid
|
||||
ctx.SaveSecrets("", p.Store) // failure means our secrets are invalid
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -106,7 +107,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
|
|||
if err != nil {
|
||||
p.Runtime.Log.Error("failed to render board", err)
|
||||
provider.WriteError(w, "trello", err)
|
||||
ctx.SaveSecrets("") // failure means our secrets are invalid
|
||||
ctx.SaveSecrets("", p.Store) // failure means our secrets are invalid
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -118,7 +119,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
|
|||
if err != nil {
|
||||
p.Runtime.Log.Error("failed to get Trello lists", err)
|
||||
provider.WriteError(w, "trello", err)
|
||||
ctx.SaveSecrets("") // failure means our secrets are invalid
|
||||
ctx.SaveSecrets("", p.Store) // failure means our secrets are invalid
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -150,7 +151,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
|
|||
p.Runtime.Log.Error("failed save Trello secrets", e)
|
||||
}
|
||||
|
||||
ctx.SaveSecrets(string(b))
|
||||
ctx.SaveSecrets(string(b), p.Store)
|
||||
}
|
||||
|
||||
// Render just sends back HMTL as-is.
|
||||
|
|
|
@ -15,12 +15,14 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/section/provider"
|
||||
)
|
||||
|
||||
// Provider represents WYSIWYG
|
||||
type Provider struct {
|
||||
Runtime env.Runtime
|
||||
Runtime *env.Runtime
|
||||
Store *domain.Store
|
||||
}
|
||||
|
||||
// Meta describes us
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue