Skip to content

Commit

Permalink
webhooks upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Nov 6, 2023
1 parent 4bdf92d commit 1c7acf9
Show file tree
Hide file tree
Showing 22 changed files with 344 additions and 701 deletions.
4 changes: 2 additions & 2 deletions routes/reviews/endpoints/add_review/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {
switch targetType {
case "bot":
err = bothooks.Send(bothooks.With{
Data: events.WebhookBotNewReviewData{
Data: events.WebhookNewReviewData{
ReviewID: reviewId,
Content: payload.Content,
Stars: payload.Stars,
Expand All @@ -220,7 +220,7 @@ func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {
}
case "server":
err = serverhooks.Send(serverhooks.With{
Data: events.WebhookServerNewReviewData{
Data: events.WebhookNewReviewData{
ReviewID: reviewId,
Content: payload.Content,
Stars: payload.Stars,
Expand Down
4 changes: 2 additions & 2 deletions routes/reviews/endpoints/edit_review/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {
switch targetType {
case "bot":
err = bothooks.Send(bothooks.With{
Data: events.WebhookBotEditReviewData{
Data: events.WebhookEditReviewData{
ReviewID: rid,
Content: events.Changeset[string]{
Old: content,
Expand All @@ -120,7 +120,7 @@ func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {
}
case "server":
err = serverhooks.Send(serverhooks.With{
Data: events.WebhookServerEditReviewData{
Data: events.WebhookEditReviewData{
ReviewID: rid,
Content: events.Changeset[string]{
Old: content,
Expand Down
4 changes: 2 additions & 2 deletions routes/reviews/endpoints/remove_review/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {
switch targetType {
case "bot":
err = bothooks.Send(bothooks.With{
Data: events.WebhookBotDeleteReviewData{
Data: events.WebhookDeleteReviewData{
ReviewID: rid,
Content: content,
Stars: stars,
Expand All @@ -89,7 +89,7 @@ func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {
}
case "server":
err = serverhooks.Send(serverhooks.With{
Data: events.WebhookServerDeleteReviewData{
Data: events.WebhookDeleteReviewData{
ReviewID: rid,
Content: content,
Stars: stars,
Expand Down
6 changes: 3 additions & 3 deletions routes/votes/endpoints/put_user_entity_votes/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {
err = bothooks.Send(bothooks.With{
UserID: uid,
BotID: targetId,
Data: events.WebhookBotVoteData{
Data: events.WebhookNewVoteData{
Votes: nvc,
PerUser: vi.VoteInfo.PerUser,
},
Expand All @@ -368,7 +368,7 @@ func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {
err = teamhooks.Send(teamhooks.With{
UserID: uid,
TeamID: targetId,
Data: events.WebhookTeamVoteData{
Data: events.WebhookNewVoteData{
Votes: nvc,
PerUser: vi.VoteInfo.PerUser,
Downvote: upvote == "false",
Expand All @@ -378,7 +378,7 @@ func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {
err = serverhooks.Send(serverhooks.With{
UserID: uid,
ServerID: targetId,
Data: events.WebhookServerVoteData{
Data: events.WebhookNewVoteData{
Votes: nvc,
PerUser: vi.VoteInfo.PerUser,
Downvote: upvote == "false",
Expand Down
4 changes: 3 additions & 1 deletion routes/webhooks/endpoints/get_test_webhook_meta/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package get_test_webhook_meta

import (
"net/http"
"slices"

"popplio/state"
"popplio/teams"
Expand Down Expand Up @@ -70,7 +71,8 @@ func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {
var data = types.GetTestWebhookMeta{}

for _, evt := range events.Registry {
if evt.Event.TargetType() == targetType {
evtTgtType := evt.Event.TargetTypes()
if slices.Contains(evtTgtType, targetType) {
data.Types = append(data.Types, types.TestWebhookType{
Type: string(evt.Event.Event()),
Data: evt.TestVars,
Expand Down
4 changes: 3 additions & 1 deletion routes/webhooks/endpoints/test_webhook/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test_webhook
import (
"net/http"
"reflect"
"slices"
"time"

"popplio/state"
Expand Down Expand Up @@ -119,7 +120,8 @@ func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {

for _, evt := range events.Registry {
if string(evt.Event.Event()) == eventType {
if evt.Event.TargetType() != targetType {
tgtTypes := evt.Event.TargetTypes()
if slices.Contains(tgtTypes, targetType) {
return uapi.HttpResponse{
Status: http.StatusBadRequest,
Json: types.ApiError{
Expand Down
4 changes: 3 additions & 1 deletion webhooks/bothooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"popplio/state"
"popplio/webhooks/events"
"popplio/webhooks/sender"
"slices"
"time"

"github.com/infinitybotlist/eureka/dovewing"
Expand All @@ -32,7 +33,8 @@ func Send(with With) error {
return nil
}

if with.Data.TargetType() != EntityType {
targetTypes := with.Data.TargetTypes()
if !slices.Contains(targetTypes, EntityType) {
return errors.New("invalid event type")
}

Expand Down
82 changes: 0 additions & 82 deletions webhooks/events/bot_vote.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,44 @@ import (
"fmt"

"github.com/bwmarrin/discordgo"
docs "github.com/infinitybotlist/eureka/doclib"
"github.com/infinitybotlist/eureka/dovewing/dovetypes"
)

const WebhookTypeBotDeleteReview WebhookType = "BOT_DELETE_REVIEW"

type WebhookBotDeleteReviewData struct {
type WebhookDeleteReviewData struct {
ReviewID string `json:"review_id" description:"The ID of the review"`
Content string `json:"content" description:"The content of the review at time of deletion"`
Stars int32 `json:"stars" description:"The number of stars the auther gave to the review at time of deletion"`
}

func (v WebhookBotDeleteReviewData) TargetType() string {
return "bot"
func (v WebhookDeleteReviewData) TargetTypes() []string {
return []string{
"bot",
"server",
}
}

func (n WebhookDeleteReviewData) Event() string {
return "DELETE_REVIEW"
}

func (n WebhookDeleteReviewData) Summary() string {
return "Delete Review"
}

func (n WebhookBotDeleteReviewData) Event() WebhookType {
return WebhookTypeBotDeleteReview
func (n WebhookDeleteReviewData) Description() string {
return "This webhook is sent when a user delete their review on an entity."
}

func (n WebhookBotDeleteReviewData) CreateHookParams(creator *dovetypes.PlatformUser, targets Target) *discordgo.WebhookParams {
func (n WebhookDeleteReviewData) CreateHookParams(creator *dovetypes.PlatformUser, targets Target) *discordgo.WebhookParams {
return &discordgo.WebhookParams{
Embeds: []*discordgo.MessageEmbed{
{
URL: "https://botlist.site/" + targets.Bot.ID,
URL: "https://botlist.site/" + targets.GetID(),
Thumbnail: &discordgo.MessageEmbedThumbnail{
URL: targets.Bot.Avatar,
URL: targets.GetAvatarURL(),
},
Title: ":x: Bot Review Deleted!",
Description: creator.DisplayName + " has deleted his review for bot " + targets.Bot.Username,
Description: creator.DisplayName + " has deleted his review for " + targets.GetTargetName(),
Color: 0x8A6BFD,
Fields: []*discordgo.MessageEmbedField{
{
Expand Down Expand Up @@ -64,7 +72,7 @@ func (n WebhookBotDeleteReviewData) CreateHookParams(creator *dovetypes.Platform
},
{
Name: "Review Page",
Value: "[View " + targets.Bot.Username + "](https://botlist.site/" + targets.Bot.ID + ")",
Value: targets.GetViewLink(),
Inline: true,
},
},
Expand All @@ -73,22 +81,6 @@ func (n WebhookBotDeleteReviewData) CreateHookParams(creator *dovetypes.Platform
}
}

func (n WebhookBotDeleteReviewData) Docs() *docs.WebhookDoc {
return &docs.WebhookDoc{
Name: "DeleteBotReview",
Summary: "Delete Bot Review",
Tags: []string{
"Webhooks",
},
Description: `This webhook is sent when a user deletes their review on a bot.`,
Format: WebhookResponse{
Type: WebhookBotDeleteReviewData{}.Event(),
Data: WebhookBotDeleteReviewData{},
},
FormatName: "WebhookResponse-WebhookDeleteBotReviewData",
}
}

func init() {
RegisterEvent(WebhookBotDeleteReviewData{})
RegisterEvent(WebhookDeleteReviewData{})
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,44 @@ import (
"fmt"

"github.com/bwmarrin/discordgo"
docs "github.com/infinitybotlist/eureka/doclib"
"github.com/infinitybotlist/eureka/dovewing/dovetypes"
)

const WebhookTypeBotEditReview WebhookType = "BOT_EDIT_REVIEW"

type WebhookBotEditReviewData struct {
type WebhookEditReviewData struct {
ReviewID string `json:"review_id" description:"The ID of the review"`
Stars Changeset[int32] `json:"stars" description:"The number of stars the auther gave to the review"`
Content Changeset[string] `json:"content" description:"The content of the review"`
}

func (v WebhookBotEditReviewData) TargetType() string {
return "bot"
func (v WebhookEditReviewData) TargetTypes() []string {
return []string{
"bot",
"server",
}
}

func (n WebhookEditReviewData) Event() string {
return "EDIT_REVIEW"
}

func (n WebhookEditReviewData) Summary() string {
return "Edit Review"
}

func (n WebhookBotEditReviewData) Event() WebhookType {
return WebhookTypeBotEditReview
func (n WebhookEditReviewData) Description() string {
return "This webhook is sent when a user edits an existing review on an entity."
}

func (n WebhookBotEditReviewData) CreateHookParams(creator *dovetypes.PlatformUser, targets Target) *discordgo.WebhookParams {
func (n WebhookEditReviewData) CreateHookParams(creator *dovetypes.PlatformUser, targets Target) *discordgo.WebhookParams {
return &discordgo.WebhookParams{
Embeds: []*discordgo.MessageEmbed{
{
URL: "https://botlist.site/" + targets.Bot.ID,
URL: "https://botlist.site/" + targets.GetID(),
Thumbnail: &discordgo.MessageEmbedThumbnail{
URL: targets.Bot.Avatar,
URL: targets.GetAvatarURL(),
},
Title: "📝 Review Editted!",
Description: ":heart: " + creator.DisplayName + " has editted a review for bot " + targets.Bot.Username,
Description: ":heart: " + creator.DisplayName + " has editted a review for " + targets.GetTargetName(),
Color: 0x8A6BFD,
Fields: []*discordgo.MessageEmbedField{
{
Expand Down Expand Up @@ -74,7 +82,7 @@ func (n WebhookBotEditReviewData) CreateHookParams(creator *dovetypes.PlatformUs
},
{
Name: "Review Page",
Value: "[View " + targets.Bot.Username + "](https://botlist.site/" + targets.Bot.ID + ")",
Value: targets.GetViewLink(),
Inline: true,
},
},
Expand All @@ -83,22 +91,6 @@ func (n WebhookBotEditReviewData) CreateHookParams(creator *dovetypes.PlatformUs
}
}

func (n WebhookBotEditReviewData) Docs() *docs.WebhookDoc {
return &docs.WebhookDoc{
Name: "EditBotReview",
Summary: "Edit Bot Review",
Tags: []string{
"Webhooks",
},
Description: `This webhook is sent when a user edits an existing review on a bot.`,
Format: WebhookResponse{
Type: WebhookBotEditReviewData{}.Event(),
Data: WebhookBotEditReviewData{},
},
FormatName: "WebhookResponse-WebhookEditBotReviewData",
}
}

func init() {
RegisterEvent(WebhookBotEditReviewData{})
RegisterEvent(WebhookEditReviewData{})
}
Loading

0 comments on commit 1c7acf9

Please sign in to comment.