Skip to content

Commit

Permalink
Merge pull request #544 from traPtitech/fix/issue541
Browse files Browse the repository at this point in the history
✨ add FilterDuration and replace FilterTime in InitPostEvent…
  • Loading branch information
iChemy authored Jul 1, 2024
2 parents 959ed1d + 0b24a6a commit a9a665d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
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 &CmpExpr{}, nil
} 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

0 comments on commit a9a665d

Please sign in to comment.