Skip to content

Commit

Permalink
Merge pull request #445 from traPtitech/fix/issue-218
Browse files Browse the repository at this point in the history
add attendee info to ical
  • Loading branch information
iChemy authored Aug 28, 2023
2 parents b3c88a9 + 628d8cf commit e5ef51f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 43 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/traPtitech/knoQ
go 1.20

require (
github.com/arran4/golang-ical v0.0.0-20230425234049-f69e132f2b0c
github.com/carlescere/scheduler v0.0.0-20170109141437-ee74d2f83d82
github.com/go-gormigrate/gormigrate/v2 v2.1.0
github.com/go-sql-driver/mysql v1.7.1
Expand All @@ -13,7 +14,6 @@ require (
github.com/jszwec/csvutil v1.8.0
github.com/labstack/echo-contrib v0.15.0
github.com/labstack/echo/v4 v4.10.2
github.com/lestrrat-go/ical v0.0.0-20201229070544-61f83f92eaf4
github.com/ory/dockertest/v3 v3.10.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/stretchr/testify v1.8.4
Expand Down Expand Up @@ -50,9 +50,7 @@ require (
github.com/imdario/mergo v0.3.12 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/lestrrat-go/bufferpool v0.0.0-20210118235918-2deb6a84c94c // indirect
github.com/lib/pq v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2B
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/arran4/golang-ical v0.0.0-20230425234049-f69e132f2b0c h1:bmHPCBB1T8YZpQI+Ch0RuICrozVFmPAjiBQZvAjtpRI=
github.com/arran4/golang-ical v0.0.0-20230425234049-f69e132f2b0c/go.mod h1:BSTTrYHuM12oAL8jDdcmPdw02SBThKYWNFHQlvEG6b0=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
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=
Expand Down Expand Up @@ -145,11 +147,6 @@ github.com/labstack/echo/v4 v4.10.2 h1:n1jAhnq/elIFTHr1EYpiYtyKgx4RW9ccVgkqByZaN
github.com/labstack/echo/v4 v4.10.2/go.mod h1:OEyqf2//K1DFdE57vw2DRgWY0M7s65IVQO2FzvI4J5k=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/lestrrat-go/bufferpool v0.0.0-20180220091733-e7784e1b3e37/go.mod h1:vs3QXw2t0jsgjLEG7JZt0uE1jcSkxnQr+5bhQ80UJHE=
github.com/lestrrat-go/bufferpool v0.0.0-20210118235918-2deb6a84c94c h1:Txi8K4WxH1BefzJqBrWIRH0JiiZlR4lUUsP+SPOqRXc=
github.com/lestrrat-go/bufferpool v0.0.0-20210118235918-2deb6a84c94c/go.mod h1:KwmXn5FpkX4is3nM+CKsCnFSqdY7ujdNpE2L/KztlDM=
github.com/lestrrat-go/ical v0.0.0-20201229070544-61f83f92eaf4 h1:fki3ZusGjvcjvWqK4QxzQ2/dJ5RnWaOawQDtjhO2oQw=
github.com/lestrrat-go/ical v0.0.0-20201229070544-61f83f92eaf4/go.mod h1:+LubDFwR2Ub76rO5RSWSyyl5TeP6/2blWXUNp8cW6ds=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
Expand All @@ -167,6 +164,7 @@ github.com/moby/sys/mountinfo v0.5.0/go.mod h1:3bMD3Rg+zkqx8MRYPi7Pyb0Ie97QEBmdx
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 h1:rzf0wL0CHVc8CEsgyygG0Mn9CNCCPZqOPaz8RiiHYQk=
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635/go.mod h1:FBS0z0QWA44HXygs7VXDUOGoN/1TV3RuWkLO04am3wc=
github.com/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM=
Expand Down Expand Up @@ -199,7 +197,6 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
Expand Down Expand Up @@ -380,6 +377,7 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
72 changes: 41 additions & 31 deletions presentation/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/traPtitech/knoQ/domain"

ics "github.com/arran4/golang-ical"
"github.com/gofrs/uuid"
"github.com/lestrrat-go/ical"
)

type ScheduleStatus int
Expand Down Expand Up @@ -105,45 +105,55 @@ type EventRes struct {
Model
}

func iCalVeventFormat(e *domain.Event, host string) *ical.Event {
timeLayout := "20060102T150405Z"
vevent := ical.NewEvent()
_ = vevent.AddProperty("uid", e.ID.String())
_ = vevent.AddProperty("dtstamp", time.Now().UTC().Format(timeLayout))
_ = vevent.AddProperty("dtstart", e.TimeStart.UTC().Format(timeLayout))
_ = vevent.AddProperty("dtend", e.TimeEnd.UTC().Format(timeLayout))
_ = vevent.AddProperty("created", e.CreatedAt.UTC().Format(timeLayout))
_ = vevent.AddProperty("last-modified", e.UpdatedAt.UTC().Format(timeLayout))
_ = vevent.AddProperty("summary", e.Name)
func iCalVeventFormat(e *domain.Event, host string, userMap map[uuid.UUID]*domain.User) *ics.VEvent {
vevent := ics.NewEvent(e.ID.String())
vevent.SetDtStampTime(time.Now().UTC())
vevent.SetStartAt(e.TimeStart.UTC())
vevent.SetEndAt(e.TimeEnd.UTC())
vevent.SetCreatedTime(e.CreatedAt.UTC())
vevent.SetModifiedAt(e.UpdatedAt.UTC())
vevent.SetSummary(e.Name)
e.Description += "\n\n"
e.Description += "-----------------------------------\n"
e.Description += "イベント詳細ページ\n"
e.Description += fmt.Sprintf("%s/events/%v", host, e.ID)
_ = vevent.AddProperty("description", e.Description)
_ = vevent.AddProperty("location", e.Room.Place)
_ = vevent.AddProperty("organizer", e.CreatedBy.DisplayName)

vevent.SetDescription(e.Description)
vevent.SetLocation(e.Room.Place)
vevent.SetOrganizer(e.CreatedBy.DisplayName)
for _, v := range e.Attendees {
user := userMap[v.UserID]
userName := fmt.Sprintf("@%s", user.Name)
userDisplayName := ics.WithCN(user.DisplayName)
var ps ics.ParticipationStatus
switch v.Schedule {
case domain.Attendance:
ps = ics.ParticipationStatusAccepted
case domain.Absent:
ps = ics.ParticipationStatusDeclined
default:
ps = ics.ParticipationStatusNeedsAction
}
vevent.AddAttendee(userName, ps, userDisplayName)
}
return vevent
}

func ICalFormat(events []*domain.Event, host string) *ical.Calendar {
c := ical.New()
ical.NewEvent()
tz := ical.NewTimezone()
_ = tz.AddProperty("TZID", "Asia/Tokyo")
std := ical.NewStandard()
_ = std.AddProperty("TZOFFSETFROM", "+9000")
_ = std.AddProperty("TZOFFSETTO", "+9000")
_ = std.AddProperty("TZNAME", "JST")
_ = std.AddProperty("DTSTART", "19700101T000000")
_ = tz.AddEntry(std)
_ = c.AddEntry(tz)

func ICalFormat(events []*domain.Event, host string, userMap map[uuid.UUID]*domain.User) *ics.Calendar {
var tz ics.VTimezone
var std ics.Standard
cal := ics.NewCalendar()
tz.ComponentBase.AddProperty(ics.ComponentProperty(ics.PropertyTzid), "Asia/Tokyo")
std.ComponentBase.AddProperty(ics.ComponentProperty(ics.PropertyTzoffsetfrom), "+0900")
std.ComponentBase.AddProperty(ics.ComponentProperty(ics.PropertyTzoffsetto), "+0900")
std.ComponentBase.AddProperty(ics.ComponentProperty(ics.PropertyTzname), "JST")
std.ComponentBase.AddProperty(ics.ComponentProperty(ics.PropertyDtstart), "19700101T000000")
tz.Components = append(tz.Components, &std)
cal.Components = append(cal.Components, &tz)
for _, e := range events {
vevent := iCalVeventFormat(e, host)
_ = c.AddEntry(vevent)
vevent := iCalVeventFormat(e, host, userMap)
cal.AddVEvent(vevent)
}
return c
return cal
}

func GenerateEventWebhookContent(method string, e *EventDetailRes, nofiticationTargets []string, origin string, isMention bool) string {
Expand Down
15 changes: 12 additions & 3 deletions router/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"net/http"

"github.com/lestrrat-go/ical"
"github.com/traPtitech/knoQ/domain"
"github.com/traPtitech/knoQ/domain/filter"
"github.com/traPtitech/knoQ/parsing"
Expand Down Expand Up @@ -247,8 +246,18 @@ func (h *Handlers) HandleGetiCalByPrivateID(c echo.Context) error {
return judgeErrorResponse(err)
}

cal := presentation.ICalFormat(events, h.Origin)
users, err := h.Repo.GetAllUsers(false, true, info)
if err != nil {
return judgeErrorResponse(err)
}

userMap := make(map[uuid.UUID]*domain.User)
for _, user := range users {
userMap[user.ID] = user
}

cal := presentation.ICalFormat(events, h.Origin, userMap)
var buf bytes.Buffer
_ = ical.NewEncoder(&buf).Encode(cal)
_ = cal.SerializeTo(&buf)
return c.Blob(http.StatusOK, "text/calendar", buf.Bytes())
}

0 comments on commit e5ef51f

Please sign in to comment.