From fe38d8cf7a891070a37ce70ce1ac73a11be2f46d Mon Sep 17 00:00:00 2001 From: Thomas Lynch Date: Sat, 19 Nov 2022 16:29:41 +1100 Subject: [PATCH] Add most stuff under the "public endpoints" api section --- app/board.go | 83 ++++++++++++++++++++++++++++++++++++++++ app/global.go | 32 ++++++++++++++++ app/models/common.go | 6 +++ app/models/custompage.go | 15 ++++++++ app/models/settings.go | 43 +++++++++++++++++++++ app/models/webring.go | 30 +++++++++++++++ app/post.go | 50 ++++++++++++++++++++++++ 7 files changed, 259 insertions(+) create mode 100644 app/global.go create mode 100644 app/models/common.go create mode 100644 app/models/custompage.go create mode 100644 app/models/settings.go create mode 100644 app/models/webring.go create mode 100644 app/post.go diff --git a/app/board.go b/app/board.go index 2dff529..cbb1620 100644 --- a/app/board.go +++ b/app/board.go @@ -120,3 +120,86 @@ func (c *Client) GetLogs(ctx context.Context, options *GetLogsOptions) (GetLogsR return res, nil } + +type GetCustomPageOptions struct { + Board string + CustomPage string +} + +type GetCustomPageResponse struct { + *models.CustomPage +} + +func (c *Client) GetCustomPage(ctx context.Context, options *GetCustomPageOptions) (*GetCustomPageResponse, error) { + + url := fmt.Sprintf("%s/%s/custompage/%s.json", c.BaseURL, options.Board, options.CustomPage) + + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, err + } + + req = req.WithContext(ctx) + + res := GetCustomPageResponse{} + if err := c.sendRequest(req, &res, nil); err != nil { + return nil, err + } + + return &res, nil + +} + +type GetBannersOptions struct { + Board string +} + +type GetBannersResponse []string + +func (c *Client) GetBanners(ctx context.Context, options *GetBannersOptions) (GetBannersResponse, error) { + + url := fmt.Sprintf("%s/%s/banners.json", c.BaseURL, options.Board) + + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, err + } + + req = req.WithContext(ctx) + + res := GetBannersResponse{} + if err := c.sendRequest(req, &res, nil); err != nil { + return nil, err + } + + return res, nil + +} + +type GetSettingsOptions struct { + Board string +} + +type GetSettingsResponse struct { + *models.CustomPage +} + +func (c *Client) GetSettings(ctx context.Context, options *GetSettingsOptions) (*GetSettingsResponse, error) { + + url := fmt.Sprintf("%s/%s/settings.json", c.BaseURL, options.Board) + + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, err + } + + req = req.WithContext(ctx) + + res := GetSettingsResponse{} + if err := c.sendRequest(req, &res, nil); err != nil { + return nil, err + } + + return &res, nil + +} diff --git a/app/global.go b/app/global.go new file mode 100644 index 0000000..f85e4c9 --- /dev/null +++ b/app/global.go @@ -0,0 +1,32 @@ +package jschan + +import ( + "context" + "fmt" + "jschan/app/models" + "net/http" +) + +type GetGlobalSettingsResponse struct { + *models.GlobalSettings +} + +func (c *Client) GetGlobalSettings(ctx context.Context) (*CatalogResponse, error) { + + url := fmt.Sprintf("%s/settings.json", c.BaseURL) + + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, err + } + + req = req.WithContext(ctx) + + res := CatalogResponse{} + if err := c.sendRequest(req, &res, nil); err != nil { + return nil, err + } + + return &res, nil + +} diff --git a/app/models/common.go b/app/models/common.go new file mode 100644 index 0000000..050bb24 --- /dev/null +++ b/app/models/common.go @@ -0,0 +1,6 @@ +package models + +type RawAndMarkdownMessage struct { + Raw string `json:"raw"` + Markdown string `json:"markdown"` +} diff --git a/app/models/custompage.go b/app/models/custompage.go new file mode 100644 index 0000000..d788924 --- /dev/null +++ b/app/models/custompage.go @@ -0,0 +1,15 @@ +package models + +import ( + "time" +) + +type CustomPage struct { + Board string `json:"board"` + Page string `json:"page"` + Title string `json:"title"` + Message RawAndMarkdownMessage `json:"message"` + Date time.Time `json:"date"` + Edited interface{} `json:"edited"` + ID string `json:"_id"` +} diff --git a/app/models/settings.go b/app/models/settings.go new file mode 100644 index 0000000..791be47 --- /dev/null +++ b/app/models/settings.go @@ -0,0 +1,43 @@ +package models + +type BoardSettings struct { + CustomPages []string `json:"customPages"` + Announcement RawAndMarkdownMessage `json:"announcement"` + AllowedFileTypes AllowedFileTypes `json:"allowedFileTypes"` + MaxFiles int `json:"maxFiles"` + CaptchaMode int `json:"captchaMode"` + ForceAnon bool `json:"forceAnon"` + SageOnlyEmail bool `json:"sageOnlyEmail"` + CustomFlags bool `json:"customFlags"` + ForceThreadMessage bool `json:"forceThreadMessage"` + ForceThreadFile bool `json:"forceThreadFile"` + ForceThreadSubject bool `json:"forceThreadSubject"` + DisableReplySubject bool `json:"disableReplySubject"` + MinThreadMessageLength int `json:"minThreadMessageLength"` + MinReplyMessageLength int `json:"minReplyMessageLength"` + MaxThreadMessageLength int `json:"maxThreadMessageLength"` + MaxReplyMessageLength int `json:"maxReplyMessageLength"` + DefaultName string `json:"defaultName"` +} + +type AllowedFileTypes struct { + AnimatedImage bool `json:"animatedImage"` + Image bool `json:"image"` + Video bool `json:"video"` + Audio bool `json:"audio"` + Other bool `json:"other"` +} + +type GlobalSettings struct { + CaptchaOptions CaptchaOptions `json:"captchaOptions"` +} + +type CaptchaOptions struct { + Type string `json:"type"` + Grid Grid `json:"grid,omitempty"` +} + +type Grid struct { + Size int `json:"size"` + Question string `json:"question"` +} diff --git a/app/models/webring.go b/app/models/webring.go new file mode 100644 index 0000000..ed3912a --- /dev/null +++ b/app/models/webring.go @@ -0,0 +1,30 @@ +package models + +import ( + "time" +) + +type Webring struct { + Name string `json:"name"` + URL string `json:"url"` + Endpoint string `json:"endpoint"` + Logo []string `json:"logo"` + Following []string `json:"following"` + Blacklist []string `json:"blacklist"` + Known []string `json:"known"` + Boards []WebringBoard `json:"boards"` +} + +type WebringBoard struct { + URI string `json:"uri"` + Title string `json:"title"` + Subtitle string `json:"subtitle"` + Path string `json:"path"` + PostsPerHour int `json:"postsPerHour"` + PostsPerDay int `json:"postsPerDay"` + TotalPosts int `json:"totalPosts"` + UniqueUsers int `json:"uniqueUsers"` + Nsfw bool `json:"nsfw"` + Tags []string `json:"tags"` + LastPostTimestamp time.Time `json:"lastPostTimestamp"` +} diff --git a/app/post.go b/app/post.go new file mode 100644 index 0000000..d7f0ab8 --- /dev/null +++ b/app/post.go @@ -0,0 +1,50 @@ +package jschan + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" +) + +type PostMakePostOptions struct { + Board string + Thread int + Name string + Message string + Subject string + Email string + PostPassword string + //TODO: Files + Spoiler []string + SpoilerAll bool + StripFilename []string + CustomFlag string + //Array for grid captcha, submitted as single param if len()==1 + Captcha []string +} + +func (c *Client) MakePost(ctx context.Context, options *PostMakePostOptions) error { + + formData := url.Values{} + //TODO: post params + + endodedBody := strings.NewReader(formData.Encode()) + + url := fmt.Sprintf("%s/forms/%s/post", c.BaseURL, options.Board) + + req, err := http.NewRequest(http.MethodPost, url, endodedBody) + if err != nil { + return err + } + + req = req.WithContext(ctx) + + if err := c.sendRequest(req, nil, nil); err != nil { + return err + } + + return nil + +}