Skip to content

Commit

Permalink
🔄 Sync from monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
mojo-machine[bot] committed Mar 13, 2024
1 parent 1fcf508 commit 8e654be
Show file tree
Hide file tree
Showing 15 changed files with 329 additions and 108 deletions.
7 changes: 4 additions & 3 deletions lib/discourse/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import (
"net/url"

"github.com/wearemojo/mojo-public-go/lib/cher"
"github.com/wearemojo/mojo-public-go/lib/jsonclient"
"github.com/wearemojo/mojo-public-go/lib/merr"
)

type ConnectClient struct {
client *Client
client *jsonclient.Client

connectSecret string
}

func NewConnectClient(client *Client, connectSecret string) *ConnectClient {
return &ConnectClient{
client: client,
client: client.AsSystem().client,

connectSecret: connectSecret,
}
Expand All @@ -40,7 +41,7 @@ func (c *ConnectClient) SyncSSO(ctx context.Context, params url.Values) error {
"sig": sig,
}

return c.client.systemClient().Do(ctx, "POST", "/admin/users/sync_sso", nil, req, nil)
return c.client.Do(ctx, "POST", "/admin/users/sync_sso", nil, req, nil)
}

func (c *ConnectClient) sign(sso string) string {
Expand Down
27 changes: 18 additions & 9 deletions lib/discourse/discourse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (

"github.com/wearemojo/mojo-public-go/lib/httpclient"
"github.com/wearemojo/mojo-public-go/lib/jsonclient"
"github.com/wearemojo/mojo-public-go/lib/merr"
)

const ErrEmptyParam = merr.Code("empty_param")

type Client struct {
BaseURL string

Expand All @@ -22,20 +25,26 @@ func NewClient(baseURL, apiKey string) *Client {
}
}

func (c *Client) client(header http.Header) *jsonclient.Client {
return jsonclient.NewClient(
c.BaseURL,
httpclient.NewClient(10*time.Second, roundTripper{header}),
)
type IdentifiedClient struct {
client *jsonclient.Client
}

func (c *Client) identifiedClient(header http.Header) *IdentifiedClient {
return &IdentifiedClient{
client: jsonclient.NewClient(
c.BaseURL,
httpclient.NewClient(10*time.Second, roundTripper{header}),
),
}
}

func (c *Client) usernameClient(username string) *jsonclient.Client {
return c.client(http.Header{
func (c *Client) AsUsername(username string) *IdentifiedClient {
return c.identifiedClient(http.Header{
"Api-Key": []string{c.apiKey},
"Api-Username": []string{username},
})
}

func (c *Client) systemClient() *jsonclient.Client {
return c.usernameClient("system")
func (c *Client) AsSystem() *IdentifiedClient {
return c.AsUsername("system")
}
16 changes: 16 additions & 0 deletions lib/discourse/endpoints_admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package discourse

import (
"context"
"fmt"
)

func (c *IdentifiedClient) AdminGetUserByID(ctx context.Context, userID int) (res *User, err error) {
path := fmt.Sprintf("/admin/users/%d", userID)
return res, c.client.Do(ctx, "GET", path, nil, nil, &res)
}

func (c *IdentifiedClient) AdminAnonymizeUser(ctx context.Context, userID int) error {
path := fmt.Sprintf("/admin/users/%d/anonymize", userID)
return c.client.Do(ctx, "PUT", path, nil, nil, nil)
}
18 changes: 18 additions & 0 deletions lib/discourse/endpoints_categories.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package discourse

import (
"context"
"net/url"
)

type ListCategoriesOptions struct {
IncludeSubcategories bool
}

func (c *IdentifiedClient) ListCategories(ctx context.Context, options *ListCategoriesOptions) (res *CategoryListResult, err error) {
params := url.Values{}
if options != nil && options.IncludeSubcategories {
params.Set("include_subcategories", "true")
}
return res, c.client.Do(ctx, "GET", "/categories", params, nil, &res)
}
15 changes: 15 additions & 0 deletions lib/discourse/endpoints_posts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package discourse

import (
"context"
"fmt"
)

func (c *IdentifiedClient) CreatePost(ctx context.Context, req *PostNew) (res *Post, err error) {
return res, c.client.Do(ctx, "POST", "/posts", nil, req, &res)
}

func (c *IdentifiedClient) GetPostByID(ctx context.Context, postID int) (res *Post, err error) {
path := fmt.Sprintf("/posts/%d.json", postID)
return res, c.client.Do(ctx, "GET", path, nil, nil, &res)
}
35 changes: 35 additions & 0 deletions lib/discourse/endpoints_topics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package discourse

import (
"context"
"fmt"
"net/url"
"strconv"

"github.com/wearemojo/mojo-public-go/lib/slicefn"
)

type GetTopicOptions struct {
Print bool
}

func (c *IdentifiedClient) GetTopic(ctx context.Context, topicID int, options *GetTopicOptions) (res *PostStreamResult, err error) {
path := fmt.Sprintf("/t/%d", topicID)
params := url.Values{}
if options != nil && options.Print {
params.Set("print", "true")
}
return res, c.client.Do(ctx, "GET", path, params, nil, &res)
}

func (c *IdentifiedClient) ListTopicPostIDs(ctx context.Context, topicID int) (res *PostIDsResult, err error) {
path := fmt.Sprintf("/t/%d/posts_ids", topicID)
params := url.Values{"post_number": {"0"}}
return res, c.client.Do(ctx, "GET", path, params, nil, &res)
}

func (c *IdentifiedClient) ListTopicPostsByIDs(ctx context.Context, topicID int, postIDs []int) (res *PostStreamResult, err error) {
path := fmt.Sprintf("/t/%d/posts", topicID)
params := url.Values{"post_ids[]": slicefn.Map(postIDs, strconv.Itoa)}
return res, c.client.Do(ctx, "GET", path, params, nil, &res)
}
27 changes: 27 additions & 0 deletions lib/discourse/endpoints_users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package discourse

import (
"context"
"fmt"
"net/url"

"github.com/wearemojo/mojo-public-go/lib/merr"
)

func (c *IdentifiedClient) GetUserByUsername(ctx context.Context, username string) (res *UserResult, err error) {
if username == "" {
return nil, merr.New(ctx, ErrEmptyParam, merr.M{"param": "username"})
}

path := fmt.Sprintf("/users/%s", url.PathEscape(username))
return res, c.client.Do(ctx, "GET", path, nil, nil, &res)
}

func (c *IdentifiedClient) GetUserByExternalID(ctx context.Context, externalID string) (res *UserResult, err error) {
if externalID == "" {
return nil, merr.New(ctx, ErrEmptyParam, merr.M{"param": "externalID"})
}

path := fmt.Sprintf("/users/by-external/%s", url.PathEscape(externalID))
return res, c.client.Do(ctx, "GET", path, nil, nil, &res)
}
51 changes: 51 additions & 0 deletions lib/discourse/plugin_dataexplorer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package discourse

import (
"context"
"fmt"
)

// https://github.com/discourse/discourse-data-explorer

type PluginDataExplorerRunQueryOptions struct {
// https://meta.discourse.org/t/120063
// https://github.com/discourse/discourse-data-explorer/blob/2f1044820c479424d29d94df389360b1d9dee871/app/controllers/discourse_data_explorer/query_controller.rb#L137

Params map[string]any
Explain bool
Download bool
LimitAll bool
}

type PluginDataExplorerQueryResult struct {
// Errors indicates what went wrong with the query.
//
// At the time of writing, it is always empty when the request is successful.
//
// https://github.com/discourse/discourse-data-explorer/blob/2f1044820c479424d29d94df389360b1d9dee871/app/controllers/discourse_data_explorer/query_controller.rb#L193
Errors []string `json:"errors"`

Duration float64 `json:"duration"`
ResultCount int `json:"result_count"`
Params map[string]any `json:"params"`
Columns []string `json:"columns"`
DefaultLimit int `json:"default_limit"`
Explain *string `json:"explain"`
Relations map[string][]map[string]any `json:"relations"`
ColRender map[string]string `json:"col_render"`
Rows [][]any `json:"rows"`
}

func (c *IdentifiedClient) PluginDataExplorerRunQuery(ctx context.Context, queryID int, options *PluginDataExplorerRunQueryOptions) (res *PluginDataExplorerQueryResult, err error) {
path := fmt.Sprintf("/admin/plugins/explorer/queries/%d/run", queryID)
body := map[string]any{}
if options != nil {
body["params"] = options.Params
body["explain"] = options.Explain
body["download"] = options.Download
if options.LimitAll {
body["limit"] = "ALL"
}
}
return res, c.client.Do(ctx, "POST", path, nil, body, &res)
}
37 changes: 37 additions & 0 deletions lib/discourse/plugin_discoursereactions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package discourse

import (
"context"
"fmt"
"net/url"

"github.com/wearemojo/mojo-public-go/lib/merr"
)

// https://github.com/discourse/discourse-reactions

type PluginDiscourseReactionsReactionType string

const (
PluginDiscourseReactionsReactionTypeEmoji PluginDiscourseReactionsReactionType = "emoji"
)

type PluginDiscourseReactionsPostReaction struct {
ID string `json:"id"`
Type PluginDiscourseReactionsReactionType `json:"type"`
Count int `json:"count"`
}

type PluginDiscourseReactionsPostReactionCurrentUser struct {
ID string `json:"id"`
Type PluginDiscourseReactionsReactionType `json:"type"`
}

func (c *IdentifiedClient) PluginDiscourseReactionsToggleReaction(ctx context.Context, postID int, reactionID string) (res *Post, err error) {
if reactionID == "" {
return nil, merr.New(ctx, ErrEmptyParam, merr.M{"param": "reactionID"})
}

path := fmt.Sprintf("/discourse-reactions/posts/%d/custom-reactions/%s/toggle", postID, url.PathEscape(reactionID))
return res, c.client.Do(ctx, "GET", path, nil, nil, &res)
}
33 changes: 0 additions & 33 deletions lib/discourse/posts.go

This file was deleted.

60 changes: 60 additions & 0 deletions lib/discourse/types_actual.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package discourse

import (
"time"
)

// these are the actual types

// name as `FooNew` if it's a type that's used to create a new `Foo`

type User struct {
ID int `json:"id"`
Username string `json:"username"`
AvatarTemplate *string `json:"avatar_template"`
SingleSignOnRecord *UserSingleSignOnRecord `json:"single_sign_on_record"`
}

type UserSingleSignOnRecord struct {
ExternalID string `json:"external_id"`
}

type Category struct {
ID int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
ReadRestricted bool `json:"read_restricted"`
SubcategoryIDs []int `json:"subcategory_ids"`
SubcategoryList []Category `json:"subcategory_list"`
}

type PostNew struct {
Title *string `json:"title"`
Raw string `json:"raw"`
TopicID *int `json:"topic_id"`
Category *int `json:"category"`
ReplyToPostNumber *int `json:"reply_to_post_number"`
}

type Post struct {
ID int `json:"id"`
Username string `json:"username"`
AvatarTemplate string `json:"avatar_template"`
CreatedAt time.Time `json:"created_at"`
Raw string `json:"raw"`
Cooked string `json:"cooked"`
PostNumber int `json:"post_number"`
PostType PostType `json:"post_type"`
UpdatedAt time.Time `json:"updated_at"`
ReplyToPostNumber *int `json:"reply_to_post_number"`
TopicID int `json:"topic_id"`
Version int `json:"version"`
UserID int `json:"user_id"`
Hidden bool `json:"hidden"`
DeletedAt *time.Time `json:"deleted_at"`
Wiki bool `json:"wiki"`

// Discourse Reactions plugin
Reactions []PluginDiscourseReactionsPostReaction `json:"reactions"`
CurrentUserReaction *PluginDiscourseReactionsPostReactionCurrentUser `json:"current_user_reaction"`
}
12 changes: 12 additions & 0 deletions lib/discourse/types_enum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package discourse

type PostType int

const (
// https://github.com/discourse/discourse/blob/ce3f592275295f201f1332c0c5069897341d5f47/app/models/post.rb#L172

PostTypeRegular PostType = 1
PostTypeModeratorAction PostType = 2
PostTypeSmallAction PostType = 3
PostTypeWhisper PostType = 4
)
Loading

0 comments on commit 8e654be

Please sign in to comment.