Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 add webhook to generate msg when today's rooms are updated #573

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions domain/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type RoomRepository interface {

GetRoom(roomID uuid.UUID, excludeEventID uuid.UUID) (*Room, error)
GetAllRooms(start time.Time, end time.Time, excludeEventID uuid.UUID) ([]*Room, error)
GetAllVerifiedRooms(start time.Time, end time.Time, excludeEventID uuid.UUID) ([]*Room, error)
IsRoomAdmins(roomID uuid.UUID, info *ConInfo) bool
}

Expand Down
32 changes: 30 additions & 2 deletions infra/db/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func roomFullPreload(tx *gorm.DB) *gorm.DB {
return tx.Preload("Events").Preload("Admins").Preload("CreatedBy")
}


//go:generate go run github.com/fuji8/gotypeconverter/cmd/gotypeconverter@latest -s CreateRoomParams -d Room -o converter.go .
type CreateRoomParams struct {
domain.WriteRoomParams
Expand All @@ -25,7 +24,6 @@ type CreateRoomParams struct {
CreatedBy uuid.UUID
}


//go:generate go run github.com/fuji8/gotypeconverter/cmd/gotypeconverter@latest -s UpdateRoomParams -d Room -o converter.go .
type UpdateRoomParams struct {
domain.WriteRoomParams
Expand Down Expand Up @@ -89,6 +87,21 @@ func (repo GormRepository) GetAllRooms(start, end time.Time, excludeEventID uuid
return r, nil
}

func (repo GormRepository) GetAllVerifiedRooms(start, end time.Time, excludeEventID uuid.UUID) ([]*domain.Room, error) {
var rooms []*Room
var err error
if excludeEventID == uuid.Nil {
rooms, err = getAllVerifiedRooms(roomFullPreload(repo.db), start, end)
} else {
rooms, err = getAllVerifiedRooms(roomExcludeEventPreload(repo.db, excludeEventID), start, end)
}
if err != nil {
return nil, defaultErrorHandling(err)
}
r := ConvSPRoomToSPdomainRoom(rooms)
return r, nil
}

func createRoom(db *gorm.DB, roomParams CreateRoomParams) (*Room, error) {
room := ConvCreateRoomParamsToRoom(roomParams)
err := db.Create(&room).Error
Expand Down Expand Up @@ -132,3 +145,18 @@ func getAllRooms(db *gorm.DB, start, end time.Time) ([]*Room, error) {
err := db.Debug().Order("time_start").Find(&rooms).Error
return rooms, err
}

func getAllVerifiedRooms(db *gorm.DB, start, end time.Time) ([]*Room, error) {
rooms := make([]*Room, 0)
if !start.IsZero() {
db = db.Where("time_start >= ?", start)
}
if !end.IsZero() {
db = db.Where("time_end <= ?", end)
}

db.Where("verified = ?", true)

err := db.Debug().Order("time_start").Find(&rooms).Error
return rooms, err
}
5 changes: 5 additions & 0 deletions repository/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func (repo *Repository) GetAllRooms(start time.Time, end time.Time, excludeEvent
return rs, defaultErrorHandling(err)
}

func (repo *Repository) GetAllVerifiedRooms(start time.Time, end time.Time, excludeEventID uuid.UUID) ([]*domain.Room, error) {
rs, err := repo.GormRepo.GetAllVerifiedRooms(start, end, excludeEventID)
return rs, defaultErrorHandling(err)
}

func (repo *Repository) IsRoomAdmins(roomID uuid.UUID, info *domain.ConInfo) bool {
room, err := repo.GetRoom(roomID, uuid.Nil)
if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions router/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"time"
Expand All @@ -11,6 +12,7 @@
"github.com/traPtitech/knoQ/router/logging"
"github.com/traPtitech/knoQ/router/presentation"
"github.com/traPtitech/knoQ/utils"
"github.com/traPtitech/knoQ/utils/tz"

"github.com/gofrs/uuid"
"go.uber.org/zap"
Expand Down Expand Up @@ -225,6 +227,37 @@
_ = utils.RequestWebhook(content, h.WebhookSecret, h.ActivityChannelID, h.WebhookID, 1)
}

func (h *Handlers) WebhookRoomHandler(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
s, _ := time.Parse(time.TimeOnly, "06:00:00")
t := time.Now().In(tz.JST)
now := time.Date(t.Year(), t.Month(), t.Day(), s.Hour(), s.Minute(), s.Second(), 0, tz.JST)
tomorrow := now.AddDate(0, 0, 1)

todaysRoomsBefore, _ := h.Repo.GetAllVerifiedRooms(now, tomorrow, uuid.Nil)

println("RoomsBefore")
for _, room := range todaysRoomsBefore {
fmt.Printf("%v\n", *room)
}

err := next(c)

todaysRoomsAfter, _ := h.Repo.GetAllVerifiedRooms(now, tomorrow, uuid.Nil)

println("RoomsAfter")
for _, room := range todaysRoomsAfter {
fmt.Printf("%v\n", *room)
}

content := utils.GenerateTodaysEventContent(now, todaysRoomsAfter, nil, h.Origin)

print(content)

return err
}
}

// getRequestUserID sessionからuserを返します
func getRequestUserID(c echo.Context) (uuid.UUID, error) {
sess, err := session.Get("session", c)
Expand Down Expand Up @@ -281,3 +314,8 @@
}
c.SetCookie(sess)
}

func isTodaysVerifiedRoomsUpdated(todaysRoomsBefore, todaysRoomsAfter []*domain.Room) bool {

Check failure on line 318 in router/middleware.go

View workflow job for this annotation

GitHub Actions / Lint

func `isTodaysVerifiedRoomsUpdated` is unused (unused)

return false
}
2 changes: 1 addition & 1 deletion router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (h *Handlers) SetupRoute() *echo.Echo {
// サービス管理者権限が必要
roomsAPIWithPrivilegeAuth := roomsAPI.Group("", h.PrivilegeUserMiddleware)
{
roomsAPIWithPrivilegeAuth.POST("/all", h.HandleCreateVerifedRooms)
roomsAPIWithPrivilegeAuth.POST("/all", h.HandleCreateVerifedRooms, h.WebhookRoomHandler)
roomsAPIWithPrivilegeAuth.POST("/:roomid/verified", h.HandleVerifyRoom)
roomsAPIWithPrivilegeAuth.DELETE("/:roomid/verified", h.HandleUnVerifyRoom)
}
Expand Down
4 changes: 2 additions & 2 deletions utils/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func InitPostEventToTraQ(repo *db.GormRepository, secret, channelID, webhookID,
fmt.Println(err)
}
events, _ := repo.GetAllEvents(expr)
message := createMessage(now, rooms, events, origin)
message := GenerateTodaysEventContent(now, rooms, events, origin)
err = RequestWebhook(message, secret, channelID, webhookID, 1)
if err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -99,7 +99,7 @@ func makeRoomAvailableByTimeTable(rooms []*domain.Room, timeTables []timeTable,
return roomAvailable
}

func createMessage(t time.Time, rooms []*domain.Room, events []*db.Event, origin string) string {
func GenerateTodaysEventContent(t time.Time, rooms []*domain.Room, events []*db.Event, origin string) string {
date := t.In(tz.JST).Format("01/02(Mon)")
combined := map[bool]string{
true: "(併用可)",
Expand Down
Loading