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 16, 2024
1 parent 0e3cfc7 commit 674ce41
Show file tree
Hide file tree
Showing 16 changed files with 17,800 additions and 106 deletions.
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo v0.49.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0
go.opentelemetry.io/otel/trace v1.24.0
golang.org/x/net v0.22.0
golang.org/x/oauth2 v0.18.0
golang.org/x/sync v0.6.0
google.golang.org/api v0.170.0
Expand All @@ -40,12 +41,12 @@ require (
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.3 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
Expand All @@ -61,9 +62,9 @@ require (
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QD
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
Expand All @@ -83,8 +83,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfFxPRy3Bf7vr3h0cechB90XaQs=
github.com/googleapis/enterprise-certificate-proxy v0.3.2/go.mod h1:VLSiSSBs/ksPL8kq3OBOQ6WRI2QnaFynd1DCjZ62+V0=
github.com/googleapis/gax-go/v2 v2.12.2 h1:mhN09QQW1jEWeMF74zGR81R30z4VJzjZsfkUhuHF+DA=
github.com/googleapis/gax-go/v2 v2.12.2/go.mod h1:61M8vcyyXR2kqKFxKrfA22jaA8JGF7Dc8App1U3H6jc=
github.com/googleapis/gax-go/v2 v2.12.3 h1:5/zPPDvw8Q1SuXjrqrZslrqT7dL/uJT2CQii/cLCKqA=
github.com/googleapis/gax-go/v2 v2.12.3/go.mod h1:AKloxT6GtNbaLm8QTNSidHUVsHYcBHwWRvkNFJUQcS4=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/jamescun/basex v0.0.0-20180407124237-e1bcb39ab18e h1:hNqjOAKBqcxNss3cdPCnvrWlprJqMvXvfY0EMKqwZ4E=
Expand Down Expand Up @@ -151,8 +151,8 @@ go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI=
go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco=
go.opentelemetry.io/otel/sdk v1.22.0 h1:6coWHw9xw7EfClIC/+O31R8IY3/+EiRFHevmHafB2Gw=
go.opentelemetry.io/otel/sdk v1.22.0/go.mod h1:iu7luyVGYovrRpe2fmj3CVKouQNdTOkxtLzPvPz1DOc=
go.opentelemetry.io/otel/sdk v1.24.0 h1:YMPPDNymmQN3ZgczicBY3B6sf9n62Dlj9pWD3ucgoDw=
go.opentelemetry.io/otel/sdk v1.24.0/go.mod h1:KVrIYw6tEubO9E96HQpcmpTKDVn9gdv35HoYiQWGDFg=
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
Expand Down
15 changes: 11 additions & 4 deletions lib/discourse/endpoints_topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type GetTopicOptions struct {
Print bool
}

func (c *IdentifiedClient) GetTopic(ctx context.Context, topicID int, options *GetTopicOptions) (res *PostStreamResult, err error) {
func (c *IdentifiedClient) GetTopic(ctx context.Context, topicID int, options *GetTopicOptions) (res *TopicResult, err error) {
path := fmt.Sprintf("/t/%d", topicID)
params := url.Values{}
if options != nil && options.Print {
Expand All @@ -22,9 +22,16 @@ func (c *IdentifiedClient) GetTopic(ctx context.Context, topicID int, options *G
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"}}
type ListTopicPostIDsOptions struct {
PostNumber int
}

func (c *IdentifiedClient) ListTopicPostIDs(ctx context.Context, topicID int, options *ListTopicPostIDsOptions) (res *PostIDsResult, err error) {
path := fmt.Sprintf("/t/%d/post_ids", topicID)
params := url.Values{}
if options != nil {
params.Set("post_number", strconv.Itoa(options.PostNumber))
}
return res, c.client.Do(ctx, "GET", path, params, nil, &res)
}

Expand Down
51 changes: 0 additions & 51 deletions lib/discourse/plugin_dataexplorer.go

This file was deleted.

93 changes: 93 additions & 0 deletions lib/discourse/plugin_dcdataexplorer.go
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)
}
37 changes: 37 additions & 0 deletions lib/discourse/plugin_dcreactions.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 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)
}
37 changes: 0 additions & 37 deletions lib/discourse/plugin_discoursereactions.go

This file was deleted.

27 changes: 25 additions & 2 deletions lib/discourse/types_actual.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ type Category struct {
SubcategoryList []Category `json:"subcategory_list"`
}

type Topic struct {
ID int `json:"id"`
Title string `json:"title"`
FancyTitle string `json:"fancy_title"`
Slug string `json:"slug"`
PostsCount int `json:"posts_count"`
ReplyCount int `json:"reply_count"`
HighestPostNumber int `json:"highest_post_number"`
ImageURL *string `json:"image_url"`
CreatedAt time.Time `json:"created_at"`
LastPostedAt *time.Time `json:"last_posted_at"`
Archetype Archetype `json:"archetype"`
Pinned bool `json:"pinned"`
Unpinned *time.Time `json:"unpinned"`
Visible bool `json:"visible"`
Closed bool `json:"closed"`
Archived bool `json:"archived"`
Tags []string `json:"tags"`
CategoryID int `json:"category_id"`
PinnedGlobally bool `json:"pinned_globally"`
FeaturedLink *string `json:"featured_link"`
}

type PostNew struct {
Title *string `json:"title"`
Raw string `json:"raw"`
Expand Down Expand Up @@ -55,6 +78,6 @@ type Post struct {
Wiki bool `json:"wiki"`

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

type Archetype string

const (
// https://github.com/discourse/discourse/blob/8cf2f909f5053b13d8f6a79703aaf7fbdb3f6423/lib/archetype.rb

ArchetypeRegular Archetype = "regular"
ArchetypePrivateMessage Archetype = "private_message"
ArchetypeBanner Archetype = "banner"
)

type PostType int

const (
Expand Down
5 changes: 3 additions & 2 deletions lib/discourse/types_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type CategoryList struct {
Categories []Category `json:"categories"`
}

type PostList struct {
Posts []Post `json:"posts"`
type PostStream struct {
Posts []Post `json:"posts"`
Stream []int `json:"stream"`
}
9 changes: 8 additions & 1 deletion lib/discourse/types_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ type CategoryListResult struct {
CategoryList CategoryList `json:"category_list"`
}

type TopicResult struct {
PostStreamResult
Topic

UserID int `json:"user_id"`
}

type PostStreamResult struct {
PostStream PostList `json:"post_stream"`
PostStream PostStream `json:"post_stream"`
}

type PostIDsResult struct {
Expand Down
Loading

0 comments on commit 674ce41

Please sign in to comment.