Skip to content

Commit

Permalink
Merge pull request #2 from thanhnguyen2187/feat/engagement-reaction-push
Browse files Browse the repository at this point in the history
feat: engagement reaction push
  • Loading branch information
huynguyenh authored Jun 13, 2023
2 parents 1dc2792 + b7eb373 commit ca2d2c8
Show file tree
Hide file tree
Showing 13 changed files with 265 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pkg/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type subAdapter struct {
func New(cfg *config.Config, l logger.Logger) IAdapter {
return &Adapter{
subAdapter: subAdapter{
Fortress: fortress.New(cfg.Endpoint.Fortress),
Fortress: fortress.New(cfg.Endpoint.Fortress, cfg.ApiServer.APIKey),
Mochi: mochi.New(cfg.Endpoint.Mochi),
},
}
Expand Down
161 changes: 141 additions & 20 deletions pkg/adapter/fortress/fortress.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,45 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"

"github.com/dwarvesf/fortress-discord/pkg/model"
)

type Fortress struct {
Url string
Url string
ApiKey string
}

func New(url string) FortressAdapter {
func New(url, apiKey string) FortressAdapter {
return &Fortress{
Url: url,
Url: url,
ApiKey: apiKey,
}
}

func (f *Fortress) makeReq(subURL, method string, body io.Reader) (*http.Request, error) {
url := f.Url + subURL
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}

req.Header.Set("Authorization", "ApiKey "+f.ApiKey)
req.Header.Set("Content-Type", "application/json")
return req, nil
}

// GetChangelogs implements FortressAdapter
func (f *Fortress) GetChangelogs() (changelogs *model.ChangelogDigest, err error) {
resp, err := http.Get(f.Url + "/api/v1/notion/notion-changelog/projects/available")
req, err := f.makeReq("/api/v1/notion/changelogs/projects/available", http.MethodGet, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -38,7 +58,12 @@ func (f *Fortress) GetChangelogs() (changelogs *model.ChangelogDigest, err error
}

func (f *Fortress) GetCommunityEarn() (earns *model.AdapterEarn, err error) {
resp, err := http.Get(f.Url + "/api/v1/notion/earn")
req, err := f.makeReq("/api/v1/notion/earn", http.MethodGet, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -53,14 +78,20 @@ func (f *Fortress) GetCommunityEarn() (earns *model.AdapterEarn, err error) {
}

func (f *Fortress) GetTechRadar(ringFilter string, q *string) (techRadars *model.AdapterTechRadar, err error) {
url := f.Url + "/api/v1/notion/tech-radar?"
url := "/api/v1/notion/tech-radar?"
if q != nil {
url += "&name=" + *q
}
if ringFilter != "" {
url += "&ring=" + ringFilter
}
resp, err := http.Get(url)

req, err := f.makeReq(url, http.MethodGet, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -75,10 +106,16 @@ func (f *Fortress) GetTechRadar(ringFilter string, q *string) (techRadars *model
}

func (f *Fortress) GetNewSubscribers() (subscribers *model.AdapterSubscriber, err error) {
resp, err := http.Get(f.Url + "/api/v1/notion/audiences")
req, err := f.makeReq("/api/v1/notion/audiences", http.MethodGet, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid call, code %v", resp.StatusCode)
Expand All @@ -90,10 +127,16 @@ func (f *Fortress) GetNewSubscribers() (subscribers *model.AdapterSubscriber, er
}

func (f *Fortress) GetOpenPositions() (posistions *model.AdapterHiringPosition, err error) {
resp, err := http.Get(f.Url + "/api/v1/notion/hiring-positions")
req, err := f.makeReq("/api/v1/notion/hiring-positions", http.MethodGet, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid call, code %v", resp.StatusCode)
Expand All @@ -105,10 +148,16 @@ func (f *Fortress) GetOpenPositions() (posistions *model.AdapterHiringPosition,
}

func (f *Fortress) GetUpcomingEvents() (events *model.AdapterEvent, err error) {
resp, err := http.Get(f.Url + "/api/v1/notion/events")
req, err := f.makeReq("/api/v1/notion/events", http.MethodGet, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid call, code %v", resp.StatusCode)
Expand All @@ -120,10 +169,16 @@ func (f *Fortress) GetUpcomingEvents() (events *model.AdapterEvent, err error) {
}

func (f *Fortress) GetStaffingDemands() (events *model.AdapterStaffingDemands, err error) {
resp, err := http.Get(f.Url + "/api/v1/notion/staffing-demands")
req, err := f.makeReq("/api/v1/notion/staffing-demands", http.MethodGet, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid call, code %v", resp.StatusCode)
Expand All @@ -135,7 +190,12 @@ func (f *Fortress) GetStaffingDemands() (events *model.AdapterStaffingDemands, e
}

func (f *Fortress) GetProjectMilestones(q string) (milestone *model.AdapterProjectMilestone, err error) {
resp, err := http.Get(f.Url + "/api/v1/notion/projects/milestones?project_name=" + q)
req, err := f.makeReq("/api/v1/notion/projects/milestones?project_name="+q, http.MethodGet, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -150,7 +210,12 @@ func (f *Fortress) GetProjectMilestones(q string) (milestone *model.AdapterProje
}

func (f *Fortress) GetInternalDigest() (digest *model.AdapterDigest, err error) {
resp, err := http.Get(f.Url + "/api/v1/notion/digests")
req, err := f.makeReq("/api/v1/notion/digests", http.MethodGet, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -165,10 +230,16 @@ func (f *Fortress) GetInternalDigest() (digest *model.AdapterDigest, err error)
}

func (f *Fortress) GetExternalDigest() (digest *model.AdapterDigest, err error) {
resp, err := http.Get(f.Url + "/api/v1/notion/updates")
req, err := f.makeReq("/api/v1/notion/updates", http.MethodGet, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid call, code %v", resp.StatusCode)
Expand All @@ -180,10 +251,16 @@ func (f *Fortress) GetExternalDigest() (digest *model.AdapterDigest, err error)
}

func (f *Fortress) GetMemos() (memos *model.AdapterMemo, err error) {
resp, err := http.Get(f.Url + "/api/v1/notion/memos")
req, err := f.makeReq("/api/v1/notion/memos", http.MethodGet, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid call, code %v", resp.StatusCode)
Expand All @@ -195,10 +272,16 @@ func (f *Fortress) GetMemos() (memos *model.AdapterMemo, err error) {
}

func (f *Fortress) GetActiveIssues() (issues *model.AdapterIssue, err error) {
resp, err := http.Get(f.Url + "/api/v1/notion/issues")
req, err := f.makeReq("/api/v1/notion/issues", http.MethodGet, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid call, code %v", resp.StatusCode)
Expand All @@ -222,10 +305,16 @@ func (f *Fortress) LogTechRadarTopic(topicName string, discordId string) error {
}

jsonValue, _ := json.Marshal(radarTopic)
resp, err := http.Post(f.Url+"/api/v1/notion/tech-radar", "application/json", bytes.NewBuffer(jsonValue))
req, err := f.makeReq("/api/v1/notion/tech-radar", http.MethodPost, bytes.NewBuffer(jsonValue))
if err != nil {
return err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
Expand All @@ -251,7 +340,7 @@ func (f *Fortress) SendChangelog(c *model.Changelog) error {
}

// post to fortress
req := SendChangelogReq{
bodyReq := SendChangelogReq{
ProjectPageID: c.RowID,
IsPreview: false,
From: struct {
Expand All @@ -263,11 +352,43 @@ func (f *Fortress) SendChangelog(c *model.Changelog) error {
},
}

jsonValue, _ := json.Marshal(req)
resp, err := http.Post(f.Url+"/api/v1/notion/notion-changelog/project", "application/json", bytes.NewBuffer(jsonValue))
jsonValue, _ := json.Marshal(bodyReq)

req, err := f.makeReq("/api/v1/notion/changelogs/project", http.MethodPost, bytes.NewBuffer(jsonValue))
if err != nil {
return err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
var errMsg ErrorMessage
if err := json.NewDecoder(resp.Body).Decode(&errMsg); err != nil {
return errors.New("invalid decoded, error " + err.Error())
}
return errors.New("invalid call, code " + strconv.Itoa(resp.StatusCode) + " " + errMsg.Message)
}

return nil
}

func (f *Fortress) UpsertRollupRecord(record *model.EngagementsRollupRecord) error {
jsonValue, err := json.Marshal(record)
req, err := f.makeReq("/api/v1/engagements/rollup", http.MethodPost, bytes.NewBuffer(jsonValue))
if err != nil {
return err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
Expand Down
2 changes: 2 additions & 0 deletions pkg/adapter/fortress/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ type FortressAdapter interface {

GetChangelogs() (digests *model.ChangelogDigest, err error)
SendChangelog(changelog *model.Changelog) error

UpsertRollupRecord(record *model.EngagementsRollupRecord) error
}
4 changes: 3 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type DiscordIds struct {
}

type ApiServer struct {
APIKey string
Port string
AllowedOrigins string
}
Expand All @@ -51,7 +52,8 @@ func Generate(v ENV) *Config {
Debug: v.GetBool("DEBUG"),
Env: v.GetString("ENV"),
ApiServer: ApiServer{
Port: v.GetString("PORT"),
Port: v.GetString("PORT"),
APIKey: v.GetString("FORTRESS_API_KEY"),
},
Endpoint: Endpoint{
Fortress: v.GetString("FORTRESS_ENDPOINT"),
Expand Down
2 changes: 2 additions & 0 deletions pkg/discord/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ type Command struct {
L logger.Logger
Cmds map[string]base.TextCommander
View view.Viewer
S service.Servicer
}

func New(cfg *config.Config, l logger.Logger, svc service.Servicer, view view.Viewer, msgHistory *history.MsgHistory) *Command {
cmd := &Command{
Cmds: make(map[string]base.TextCommander),
L: l,
View: view,
S: svc,
}

// register all commands here
Expand Down
7 changes: 5 additions & 2 deletions pkg/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package discord

import (
"github.com/bwmarrin/discordgo"

"github.com/dwarvesf/fortress-discord/pkg/config"
"github.com/dwarvesf/fortress-discord/pkg/discord/command"
"github.com/dwarvesf/fortress-discord/pkg/discord/history"
Expand Down Expand Up @@ -43,9 +42,13 @@ func (d *Discord) ListenAndServe() (*discordgo.Session, error) {

// register interaction, right now use for sending updates
d.Session.AddHandler(d.onInteractionCreate)
d.Session.AddHandler(d.onReactionCreate)
d.Session.AddHandler(d.onReactionRemove)
d.Session.AddHandler(d.onAllReactionsRemove)

// intents to receive message
d.Session.Identify.Intents = discordgo.IntentsGuildMessages
d.Session.Identify.Intents = discordgo.IntentsGuildMessages |
discordgo.IntentsGuildMessageReactions

err := d.Session.Open()
if err != nil {
Expand Down
Loading

0 comments on commit ca2d2c8

Please sign in to comment.