Skip to content

Commit

Permalink
✨ add FilterDuration and replace FilterTime in InitPostEventToTraQ
Browse files Browse the repository at this point in the history
  • Loading branch information
iChemy committed Jun 18, 2024
1 parent a4b1d19 commit 8280e0a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
82 changes: 82 additions & 0 deletions domain/filter/shorthand.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,85 @@ func AddAnd(lhs, rhs Expr) Expr {
Rhs: rhs,
}
}

func addOr(lhs, rhs Expr) Expr {
if lhs == nil && rhs == nil {
return nil
}
if lhs == nil {
return rhs
}
if rhs == nil {
return lhs
}
return &LogicOpExpr{
LogicOp: Or,
Lhs: lhs,
Rhs: rhs,
}
}

// 期間中に始まる
// time_start >= min AND time_start < max
//
// 期間中に終わる
// time_end >= min AND time_end < max
//
// 期間より前に始まり期間より後に終わる
// time_start < min AND time_end >= max
//
// min < max であるべき
func FilterDuration(min, max time.Time) Expr {
startAfterMin := &CmpExpr{
Attr: AttrTimeStart,
Relation: GreterEq,
Value: min,
}
startBeforeMax := &CmpExpr{
Attr: AttrTimeStart,
Relation: Less,
Value: max,
}
// 期間中に始まる
startIn := &LogicOpExpr{
LogicOp: And,
Lhs: startAfterMin,
Rhs: startBeforeMax,
}

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

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

return addOr(addOr(startIn, endIn), throughout)
}
6 changes: 3 additions & 3 deletions utils/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ 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))
events, _ := repo.GetAllEvents(filter.FilterDuration(now, tomorrow))
message := createMessage(now, rooms, events, origin)
err := RequestWebhook(message, secret, channelID, webhookID, 1)
if err != nil {
Expand Down Expand Up @@ -162,8 +162,8 @@ 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"),
eventMessage += fmt.Sprintf("- [%s](%s/events/%s) %s:%s ~ %s:%s @%s %s\n", event.Name, origin, event.ID,
event.TimeStart.In(tz.JST).Format("01/02"), event.TimeStart.In(tz.JST).Format("15:04"), event.TimeEnd.In(tz.JST).Format("01/02"), event.TimeEnd.In(tz.JST).Format("15:04"),
event.Room.Place, combined[event.AllowTogether])
}

Expand Down

0 comments on commit 8280e0a

Please sign in to comment.