-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
0e3cfc7
commit 674ce41
Showing
16 changed files
with
17,800 additions
and
106 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 was deleted.
Oops, something went wrong.
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,93 @@ | ||
package discourse | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/wearemojo/mojo-public-go/lib/merr" | ||
"github.com/wearemojo/mojo-public-go/lib/slicefn" | ||
) | ||
|
||
// https://github.com/discourse/discourse-data-explorer | ||
|
||
type PluginDCDataExplorerRunQueryOptions 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 PluginDCDataExplorerQueryResult 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:"colrender"` | ||
Rows [][]any `json:"rows"` | ||
} | ||
|
||
func PluginDCDataExplorerQueryResultUnmarshal[T any](ctx context.Context, res *PluginDCDataExplorerQueryResult) ([]T, error) { | ||
colLen := len(res.Columns) | ||
|
||
mapped, err := slicefn.MapE(res.Rows, func(row []any) (map[string]any, error) { | ||
if len(row) != colLen { | ||
return nil, merr.New(ctx, "column_count_mismatch", merr.M{"expected": colLen, "actual": len(row)}) | ||
} | ||
|
||
out := map[string]any{} | ||
for i, col := range res.Columns { | ||
out[col] = row[i] | ||
} | ||
return out, nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
data, err := json.Marshal(mapped) | ||
if err != nil { | ||
return nil, merr.New(ctx, "json_marshal_failed", nil, err) | ||
} | ||
|
||
var out []T | ||
if err := json.Unmarshal(data, &out); err != nil { | ||
return nil, merr.New(ctx, "json_unmarshal_failed", nil, err) | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
func (c *IdentifiedClient) PluginDCDataExplorerRunQuery(ctx context.Context, queryID int, options *PluginDCDataExplorerRunQueryOptions) (res *PluginDCDataExplorerQueryResult, err error) { | ||
path := fmt.Sprintf("/admin/plugins/explorer/queries/%d/run", queryID) | ||
body := map[string]any{} | ||
if options != nil { | ||
body["explain"] = strconv.FormatBool(options.Explain) | ||
body["download"] = options.Download | ||
if options.Params != nil { | ||
data, err := json.Marshal(options.Params) | ||
if err != nil { | ||
return nil, merr.New(ctx, "json_marshal_failed", nil, err) | ||
} | ||
body["params"] = string(data) | ||
} | ||
if options.LimitAll { | ||
body["limit"] = "ALL" | ||
} | ||
} | ||
return res, c.client.Do(ctx, "POST", path, nil, body, &res) | ||
} |
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,37 @@ | ||
package discourse | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/wearemojo/mojo-public-go/lib/merr" | ||
) | ||
|
||
// https://github.com/discourse/discourse-reactions | ||
|
||
type PluginDCReactionsReactionType string | ||
|
||
const ( | ||
PluginDCReactionsReactionTypeEmoji PluginDCReactionsReactionType = "emoji" | ||
) | ||
|
||
type PluginDCReactionsPostReaction struct { | ||
ID string `json:"id"` | ||
Type PluginDCReactionsReactionType `json:"type"` | ||
Count int `json:"count"` | ||
} | ||
|
||
type PluginDCReactionsPostReactionCurrentUser struct { | ||
ID string `json:"id"` | ||
Type PluginDCReactionsReactionType `json:"type"` | ||
} | ||
|
||
func (c *IdentifiedClient) PluginDCReactionsToggleReaction(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) | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.