diff --git a/go.mod b/go.mod index d65e4217..0dc8cc3d 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.20 require ( github.com/arran4/golang-ical v0.1.0 - github.com/carlescere/scheduler v0.0.0-20170109141437-ee74d2f83d82 github.com/go-gormigrate/gormigrate/v2 v2.1.1 github.com/go-sql-driver/mysql v1.7.1 github.com/gofrs/uuid v4.4.0+incompatible @@ -16,6 +15,7 @@ require ( github.com/labstack/echo/v4 v4.11.1 github.com/ory/dockertest/v3 v3.10.0 github.com/patrickmn/go-cache v2.1.0+incompatible + github.com/robfig/cron/v3 v3.0.1 github.com/stretchr/testify v1.8.4 github.com/traPtitech/go-traq v0.0.0-20220822084224-d405d26bce99 go.uber.org/zap v1.26.0 diff --git a/go.sum b/go.sum index 95ad4ac3..1e05b885 100644 --- a/go.sum +++ b/go.sum @@ -13,8 +13,6 @@ github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEV github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk= github.com/arran4/golang-ical v0.1.0 h1:Oz0Rd5fpeNoHNFF9B9H5uYZyt1ubuZSZ3LVdHD5KvZI= github.com/arran4/golang-ical v0.1.0/go.mod h1:BSTTrYHuM12oAL8jDdcmPdw02SBThKYWNFHQlvEG6b0= -github.com/carlescere/scheduler v0.0.0-20170109141437-ee74d2f83d82 h1:9bAydALqAjBfPHd/eAiJBHnMZUYov8m2PkXVr+YGQeI= -github.com/carlescere/scheduler v0.0.0-20170109141437-ee74d2f83d82/go.mod h1:tyA14J0sA3Hph4dt+AfCjPrYR13+vVodshQSM7km9qw= github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4= github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= @@ -160,6 +158,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= diff --git a/main.go b/main.go index 5adc4829..6aada222 100644 --- a/main.go +++ b/main.go @@ -10,15 +10,16 @@ import ( "github.com/traPtitech/knoQ/domain" "github.com/traPtitech/knoQ/infra/db" - "github.com/traPtitech/knoQ/repository" "github.com/traPtitech/knoQ/infra/traq" + "github.com/traPtitech/knoQ/repository" "github.com/traPtitech/knoQ/utils" + "github.com/traPtitech/knoQ/utils/tz" "golang.org/x/oauth2" "github.com/traPtitech/knoQ/router" - "github.com/carlescere/scheduler" "github.com/gorilla/sessions" + "github.com/robfig/cron/v3" "go.uber.org/zap" ) @@ -42,8 +43,6 @@ var ( webhookSecret = getenv("WEBHOOK_SECRET", "") activityChannelID = getenv("ACTIVITY_CHANNEL_ID", "") dailyChannelID = getenv("DAILY_CHANNEL_ID", "") - - jst, _ = time.LoadLocation("Asia/Tokyo") ) func main() { @@ -53,7 +52,7 @@ func main() { domain.DEVELOPMENT, _ = strconv.ParseBool(development) gormRepo := db.GormRepository{} - err := gormRepo.Setup(mariadbHost, mariadbUser, mariadbPassword, mariadbDatabase, mariadbPort, tokenKey, gormLogLevel, jst) + err := gormRepo.Setup(mariadbHost, mariadbUser, mariadbPassword, mariadbDatabase, mariadbPort, tokenKey, gormLogLevel, tz.JST) if err != nil { panic(err) } @@ -94,9 +93,21 @@ func main() { e := handler.SetupRoute() // webhook - job := utils.InitPostEventToTraQ(&repo.GormRepo, handler.WebhookSecret, - handler.DailyChannelId, handler.WebhookID, handler.Origin) - _, _ = scheduler.Every().Day().At("08:00").Run(job) + c := cron.New(cron.WithLocation(tz.JST)) + _, err = c.AddFunc( + "0 8 * * *", + utils.InitPostEventToTraQ( + &repo.GormRepo, + handler.WebhookSecret, + handler.DailyChannelId, + handler.WebhookID, + handler.Origin, + ), + ) + if err != nil { + panic(err) + } + c.Start() // サーバースタート go func() { diff --git a/router/presentation/event.go b/router/presentation/event.go index 0de5e2f2..c9af7130 100644 --- a/router/presentation/event.go +++ b/router/presentation/event.go @@ -8,6 +8,7 @@ import ( "time" "github.com/traPtitech/knoQ/domain" + "github.com/traPtitech/knoQ/utils/tz" ics "github.com/arran4/golang-ical" "github.com/gofrs/uuid" @@ -161,7 +162,6 @@ func ICalFormat(events []*domain.Event, host string, userMap map[uuid.UUID]*doma } func GenerateEventWebhookContent(method string, e *EventDetailRes, nofiticationTargets []string, origin string, isMention bool) string { - jst, _ := time.LoadLocation("Asia/Tokyo") timeFormat := "01/02(Mon) 15:04" var content string switch method { @@ -172,7 +172,7 @@ func GenerateEventWebhookContent(method string, e *EventDetailRes, nofiticationT } content += fmt.Sprintf("### [%s](%s/events/%s)", e.Name, origin, e.ID) + "\n" content += fmt.Sprintf("- 主催: [%s](%s/groups/%s)", e.GroupName, origin, e.Group.ID) + "\n" - content += fmt.Sprintf("- 日時: %s ~ %s", e.TimeStart.In(jst).Format(timeFormat), e.TimeEnd.In(jst).Format(timeFormat)) + "\n" + content += fmt.Sprintf("- 日時: %s ~ %s", e.TimeStart.In(tz.JST).Format(timeFormat), e.TimeEnd.In(tz.JST).Format(timeFormat)) + "\n" content += fmt.Sprintf("- 場所: %s", e.Room.Place) + "\n" content += "\n" diff --git a/router/presentation/room.go b/router/presentation/room.go index cca48be6..b9406597 100644 --- a/router/presentation/room.go +++ b/router/presentation/room.go @@ -5,6 +5,7 @@ import ( "github.com/gofrs/uuid" "github.com/traPtitech/knoQ/domain" + "github.com/traPtitech/knoQ/utils/tz" ) //go:generate go run github.com/fuji8/gotypeconverter/cmd/gotypeconverter@latest -s RoomReq -d domain.WriteRoomParams -o converter.go . @@ -75,17 +76,16 @@ func ConvdomainRoomToRoomRes(src domain.Room) (dst RoomRes) { func ChangeRoomCSVReqTodomainWriteRoomParams(src RoomCSVReq, userID uuid.UUID) (*domain.WriteRoomParams, error) { layout := "2006/01/02 15:04" - jst, _ := time.LoadLocation("Asia/Tokyo") var params domain.WriteRoomParams var err error params.Place = src.Location - params.TimeStart, err = time.ParseInLocation(layout, src.StartDate+" "+src.StartTime, jst) + params.TimeStart, err = time.ParseInLocation(layout, src.StartDate+" "+src.StartTime, tz.JST) if err != nil { return nil, err } - params.TimeEnd, err = time.ParseInLocation(layout, src.EndDate+" "+src.EndTime, jst) + params.TimeEnd, err = time.ParseInLocation(layout, src.EndDate+" "+src.EndTime, tz.JST) if err != nil { return nil, err } diff --git a/utils/cron.go b/utils/cron.go index 7edb6e52..9112c6a4 100644 --- a/utils/cron.go +++ b/utils/cron.go @@ -9,11 +9,10 @@ import ( "github.com/traPtitech/knoQ/domain" "github.com/traPtitech/knoQ/domain/filter" "github.com/traPtitech/knoQ/infra/db" + "github.com/traPtitech/knoQ/utils/tz" "golang.org/x/exp/slices" ) -var jst, _ = time.LoadLocation("Asia/Tokyo") - type timeTable struct { name string start time.Time @@ -41,7 +40,7 @@ func InitPostEventToTraQ(repo *db.GormRepository, secret, channelID, webhookID, func setTimeFromString(t time.Time, str string) time.Time { s, _ := time.Parse(time.TimeOnly, str) - return time.Date(t.Year(), t.Month(), t.Day(), s.Hour(), s.Minute(), s.Second(), 0, jst) + return time.Date(t.Year(), t.Month(), t.Day(), s.Hour(), s.Minute(), s.Second(), 0, tz.JST) } // makeRoomAvailableByTimeTable timeTables の各時間帯を行、rooms の各部屋を列とする表を map 形式で作成する。 unVerified の部屋は無視する。 @@ -97,7 +96,7 @@ func makeRoomAvailableByTimeTable(rooms []*domain.Room, timeTables []timeTable, } func createMessage(t time.Time, rooms []*domain.Room, events []*db.Event, origin string) string { - date := t.In(jst).Format("01/02(Mon)") + date := t.In(tz.JST).Format("01/02(Mon)") combined := map[bool]string{ true: "(併用可)", false: "", @@ -164,7 +163,7 @@ func createMessage(t time.Time, rooms []*domain.Room, events []*db.Event, origin } else { for _, event := range events { eventMessage += fmt.Sprintf("- [%s](%s/events/%s) %s ~ %s @%s %s\n", event.Name, origin, event.ID, - event.TimeStart.In(jst).Format("15:04"), event.TimeEnd.In(jst).Format("15:04"), + event.TimeStart.In(tz.JST).Format("15:04"), event.TimeEnd.In(tz.JST).Format("15:04"), event.Room.Place, combined[event.AllowTogether]) } diff --git a/utils/format.go b/utils/format.go deleted file mode 100644 index 0fbaa362..00000000 --- a/utils/format.go +++ /dev/null @@ -1,14 +0,0 @@ -package utils - -import "time" - -func StrToTime(s string) (time.Time, error) { - t, err := time.Parse("15:04", s) - if err != nil { - t, err = time.Parse("15:04:05", s) - if err != nil { - return time.Time{}, err - } - } - return t, nil -} diff --git a/utils/tz/jst.go b/utils/tz/jst.go new file mode 100644 index 00000000..d7a20430 --- /dev/null +++ b/utils/tz/jst.go @@ -0,0 +1,5 @@ +package tz + +import "time" + +var JST = time.FixedZone("Asia/Tokyo", 9*60*60)