From 44e8f2d6bc76410f4a3763093315541709af5119 Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Wed, 3 Apr 2019 15:27:47 +0200 Subject: [PATCH] add(messaging): import webhook type --- messaging/internal/service/service.go | 2 +- messaging/types/webhook.gen.go | 67 +++++++++++++++++++++++++++ messaging/types/webhook.go | 66 ++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 messaging/types/webhook.gen.go create mode 100644 messaging/types/webhook.go diff --git a/messaging/internal/service/service.go b/messaging/internal/service/service.go index 56ce46d6a..03d340764 100644 --- a/messaging/internal/service/service.go +++ b/messaging/internal/service/service.go @@ -30,7 +30,7 @@ func Init() error { return err } - _, err := http.New(&config.HTTPClient{ + _, err = http.New(&config.HTTPClient{ Timeout: 10, }) if err != nil { diff --git a/messaging/types/webhook.gen.go b/messaging/types/webhook.gen.go new file mode 100644 index 000000000..039ae284a --- /dev/null +++ b/messaging/types/webhook.gen.go @@ -0,0 +1,67 @@ +package types + +// Hello! This file is auto-generated. + +type ( + + // WebhookSet slice of Webhook + // + // This type is auto-generated. + WebhookSet []*Webhook +) + +// Walk iterates through every slice item and calls w(Webhook) err +// +// This function is auto-generated. +func (set WebhookSet) Walk(w func(*Webhook) error) (err error) { + for i := range set { + if err = w(set[i]); err != nil { + return + } + } + + return +} + +// Filter iterates through every slice item, calls f(Webhook) (bool, err) and return filtered slice +// +// This function is auto-generated. +func (set WebhookSet) Filter(f func(*Webhook) (bool, error)) (out WebhookSet, err error) { + var ok bool + out = WebhookSet{} + for i := range set { + if ok, err = f(set[i]); err != nil { + return + } else if ok { + out = append(out, set[i]) + } + } + + return +} + +// FindByID finds items from slice by its ID property +// +// This function is auto-generated. +func (set WebhookSet) FindByID(ID uint64) *Webhook { + for i := range set { + if set[i].ID == ID { + return set[i] + } + } + + return nil +} + +// IDs returns a slice of uint64s from all items in the set +// +// This function is auto-generated. +func (set WebhookSet) IDs() (IDs []uint64) { + IDs = make([]uint64, len(set)) + + for i := range set { + IDs[i] = set[i].ID + } + + return +} diff --git a/messaging/types/webhook.go b/messaging/types/webhook.go new file mode 100644 index 000000000..7f2bcae06 --- /dev/null +++ b/messaging/types/webhook.go @@ -0,0 +1,66 @@ +package types + +import ( + "time" + + "mime/multipart" + + "github.com/crusttech/crust/internal/rules" +) + +type ( + Webhook struct { + ID uint64 `json:"id" db:"id"` + + Kind WebhookKind `json:"kind" db:"webhook_kind"` + AuthToken string `json:"-" db:"webhook_token"` + + OwnerUserID uint64 `json:"userId" db:"rel_owner"` + + // Created bot User ID + UserID uint64 `json:"userId" db:"rel_user"` + ChannelID uint64 `json:"channelId" db:"rel_channel"` + + // Outgoing webhook details + OutgoingTrigger string `json:"trigger" db:"outgoing_trigger"` + OutgoingURL string `json:"url" db:"outgoing_url"` + + CreatedAt time.Time `json:"createdAt,omitempty" db:"created_at"` + UpdatedAt *time.Time `json:"updatedAt,omitempty" db:"updated_at"` + DeletedAt *time.Time `json:"deletedAt,omitempty" db:"deleted_at"` + } + + WebhookRequest struct { + Username string + + Avatar *multipart.FileHeader + AvatarURL string + + OutgoingTrigger string + OutgoingURL string + } + + WebhookFilter struct { + ChannelID uint64 + OwnerUserID uint64 + OutgoingTrigger string + } + + WebhookBody struct { + Text string `json:"text"` + Avatar string `json:"avatar,omitempty"` + Username string `json:"username,omitempty"` + } + + WebhookKind string +) + +const ( + IncomingWebhook WebhookKind = "incoming" + OutgoingWebhook = "outgoing" +) + +// Resource returns a system resource ID for this type +func (wh Webhook) PermissionResource() rules.Resource { + return WebhookPermissionResource.AppendID(wh.ID) +}