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 FilterDuration and replace FilterTime in InitPostEvent… #544

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
86 changes: 86 additions & 0 deletions domain/filter/shorthand.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package filter

import (
"errors"
"time"

"github.com/gofrs/uuid"
Expand Down Expand Up @@ -93,3 +94,88 @@ func AddAnd(lhs, rhs Expr) Expr {
Rhs: rhs,
}
}

// 以下のいずれかを満たす
// * 期間中に始まる (time_start >= since AND time_start < until)
// * 期間中に終わる (time_end >= since AND time_end < until)
// * 期間より前に始まり期間より後に終わる (time_start < since AND time_end >= until)
//
// ただしsinceがゼロのとき time_start <= until
// untilがゼロのとき since <= time_end
// 双方がゼロのき nil を返す
func FilterDuration(since, until time.Time) (Expr, error) {
if since.IsZero() && until.IsZero() {
return nil, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここはエラーを返したい

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嘘です
エラーはnilでいいけどExprはnilではなくて空の&CmpExpr{}を返すのがよさそう

} else if since.IsZero() {
return &CmpExpr{
Attr: AttrTimeStart,
Relation: LessEq,
Value: until,
}, nil
} else if until.IsZero() {
return &CmpExpr{
Attr: AttrTimeEnd,
Relation: GreterEq,
Value: since,
}, nil
}

if since.After(until) {
return nil, errors.New("invalid time range")
}

// 期間中に始まる
startIn := &LogicOpExpr{
LogicOp: And,
Lhs: &CmpExpr{
Attr: AttrTimeStart,
Relation: GreterEq,
Value: since,
},
Rhs: &CmpExpr{
Attr: AttrTimeStart,
Relation: Less,
Value: until,
},
}

// 期間中に終わる
endIn := &LogicOpExpr{
LogicOp: And,
Lhs: &CmpExpr{
Attr: AttrTimeEnd,
Relation: GreterEq,
Value: since,
},
Rhs: &CmpExpr{
Attr: AttrTimeEnd,
Relation: Less,
Value: until,
},
}

// 期間より前に始まり期間より後に終わる
throughout := &LogicOpExpr{
LogicOp: And,
Lhs: &CmpExpr{
Attr: AttrTimeStart,
Relation: Less,
Value: since,
},
Rhs: &CmpExpr{
Attr: AttrTimeEnd,
Relation: GreterEq,
Value: until,
},
}

return &LogicOpExpr{
LogicOp: Or,
Lhs: throughout,
Rhs: &LogicOpExpr{
LogicOp: Or,
Lhs: endIn,
Rhs: startIn,
},
}, nil
}
10 changes: 7 additions & 3 deletions utils/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ func InitPostEventToTraQ(repo *db.GormRepository, secret, channelID, webhookID,
tomorrow := now.AddDate(0, 0, 1)

rooms, _ := repo.GetAllRooms(now, tomorrow, uuid.Nil)
events, _ := repo.GetAllEvents(filter.FilterTime(now, tomorrow))
expr, err := filter.FilterDuration(now, tomorrow)
if err != nil {
fmt.Println(err)
}
events, _ := repo.GetAllEvents(expr)
message := createMessage(now, rooms, events, origin)
err := RequestWebhook(message, secret, channelID, webhookID, 1)
err = RequestWebhook(message, secret, channelID, webhookID, 1)
if err != nil {
fmt.Println(err)
}
Expand Down Expand Up @@ -163,7 +167,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(tz.JST).Format("15:04"), event.TimeEnd.In(tz.JST).Format("15:04"),
event.TimeStart.In(tz.JST).Format("01/02 15:04"), event.TimeEnd.In(tz.JST).Format("01/02 15:04"),
event.Room.Place, combined[event.AllowTogether])
}

Expand Down
Loading