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

リマインダーの実装(openapi) #1279

Open
wants to merge 6 commits into
base: fix/openapi
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
124 changes: 120 additions & 4 deletions controller/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@
return err
}

Jq.PushReminder(questionnaireID, params.ResponseDueDateTime)
if err != nil {
c.Logger().Errorf("failed to push reminder: %+v", err)
return err
}

return nil
})
if err != nil {
Expand Down Expand Up @@ -358,6 +364,17 @@
return err
}

err = Jq.DeleteReminder(questionnaireID)
if err != nil {
c.Logger().Errorf("failed to delete reminder: %+v", err)
return err
}
err = Jq.PushReminder(questionnaireID, params.ResponseDueDateTime)
if err != nil {
c.Logger().Errorf("failed to push reminder: %+v", err)
return err
}

return nil
})
if err != nil {
Expand Down Expand Up @@ -483,6 +500,12 @@
return err
}

err = Jq.DeleteReminder(questionnaireID)
if err != nil {
c.Logger().Errorf("failed to delete reminder: %+v", err)
return err
}

return nil
})
if err != nil {
Expand All @@ -498,12 +521,48 @@
}

func (q Questionnaire) GetQuestionnaireMyRemindStatus(c echo.Context, questionnaireID int) (bool, error) {
// todo: check remind status
return false, nil
status, err := Jq.CheckRemindStatus(questionnaireID)
if err != nil {
c.Logger().Errorf("failed to check remind status: %+v", err)
return false, echo.NewHTTPError(http.StatusInternalServerError, "failed to check remind status")
}

return status, nil
}

func (q Questionnaire) EditQuestionnaireMyRemindStatus(c echo.Context, questionnaireID int) error {
// todo: edit remind status
func (q Questionnaire) EditQuestionnaireMyRemindStatus(c echo.Context, questionnaireID int, isRemindEnabled bool) error {
if isRemindEnabled {
status, err := Jq.CheckRemindStatus(questionnaireID)
if err != nil {
c.Logger().Errorf("failed to check remind status: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to check remind status")
}
if status {
return nil
}

questionnaire, _, _, _, _, _, err := q.GetQuestionnaireInfo(c.Request().Context(), questionnaireID)
if err != nil {
if errors.Is(err, model.ErrRecordNotFound) {
c.Logger().Info("questionnaire not found")
return echo.NewHTTPError(http.StatusNotFound, "questionnaire not found")
}
c.Logger().Errorf("failed to get questionnaire: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get questionnaire")
}

err = Jq.PushReminder(questionnaireID, &questionnaire.ResTimeLimit.Time)
if err != nil {
c.Logger().Errorf("failed to push reminder: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to push reminder")
}
} else {
err := Jq.DeleteReminder(questionnaireID)
if err != nil {
c.Logger().Errorf("failed to delete reminder: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to delete reminder")
}
}
return nil
}

Expand Down Expand Up @@ -659,7 +718,7 @@
res = openapi.Response{
QuestionnaireId: questionnaireID,
ResponseId: resopnseID,
Respondent: userID,

Check failure on line 721 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / Lint

cannot use userID (variable of type string) as *string value in struct literal

Check failure on line 721 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / Lint

cannot use userID (variable of type string) as *string value in struct literal

Check failure on line 721 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / Build

cannot use userID (variable of type string) as *string value in struct literal
SubmittedAt: submittedAt,
ModifiedAt: modifiedAt,
IsDraft: params.IsDraft,
Expand Down Expand Up @@ -705,3 +764,60 @@
questionnaireID,
)
}

func createReminderMessage(questionnaireID int, title string, description string, administrators []string, resTimeLimit time.Time, targets []string, leftTimeText string) string {
resTimeLimitText := resTimeLimit.Local().Format("2006/01/02 15:04")
targetsMentionText := "@" + strings.Join(targets, " @")

return fmt.Sprintf(
`### アンケート『[%s](https://anke-to.trap.jp/questionnaires/%d)』の回答期限が迫っています!
==残り%sです!==
#### 管理者
%s
#### 説明
%s
#### 回答期限
%s
#### 対象者
%s
#### 回答リンク
https://anke-to.trap.jp/responses/new/%d
`,
title,
questionnaireID,
leftTimeText,
strings.Join(administrators, ","),
description,
resTimeLimitText,
targetsMentionText,
questionnaireID,
)
}

func (q Questionnaire) GetQuestionnaireResult(ctx echo.Context, questionnaireID int, userID string) (openapi.Result, error) {

Check failure on line 797 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L797

undefined: openapi.Result
Raw output
controller/questionnaire.go:797:110: undefined: openapi.Result

Check failure on line 797 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L797

undefined: openapi.Result
Raw output
controller/questionnaire.go:797:110: undefined: openapi.Result

Check failure on line 797 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L797

undefined: openapi.Result
Raw output
controller/questionnaire.go:797:110: undefined: openapi.Result

Check failure on line 797 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: openapi.Result

Check failure on line 797 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / Build

undefined: openapi.Result
res := openapi.Result{}

Check failure on line 798 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L798

undefined: openapi.Result
Raw output
controller/questionnaire.go:798:17: undefined: openapi.Result

Check failure on line 798 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L798

undefined: openapi.Result
Raw output
controller/questionnaire.go:798:17: undefined: openapi.Result

Check failure on line 798 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L798

undefined: openapi.Result
Raw output
controller/questionnaire.go:798:17: undefined: openapi.Result

Check failure on line 798 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: openapi.Result

Check failure on line 798 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / Build

undefined: openapi.Result

params := openapi.GetQuestionnaireResponsesParams{}
responses, err := q.GetQuestionnaireResponses(ctx, questionnaireID, params, userID)
if err != nil {
if errors.Is(echo.ErrNotFound, err) {
return openapi.Result{}, err

Check failure on line 804 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L804

undefined: openapi.Result
Raw output
controller/questionnaire.go:804:19: undefined: openapi.Result

Check failure on line 804 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L804

undefined: openapi.Result
Raw output
controller/questionnaire.go:804:19: undefined: openapi.Result

Check failure on line 804 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L804

undefined: openapi.Result
Raw output
controller/questionnaire.go:804:19: undefined: openapi.Result

Check failure on line 804 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: openapi.Result

Check failure on line 804 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / Build

undefined: openapi.Result
}
ctx.Logger().Errorf("failed to get questionnaire responses: %+v", err)
return openapi.Result{}, echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get questionnaire responses: %w", err))

Check failure on line 807 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L807

undefined: openapi.Result
Raw output
controller/questionnaire.go:807:18: undefined: openapi.Result

Check failure on line 807 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L807

undefined: openapi.Result
Raw output
controller/questionnaire.go:807:18: undefined: openapi.Result

Check failure on line 807 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L807

undefined: openapi.Result
Raw output
controller/questionnaire.go:807:18: undefined: openapi.Result

Check failure on line 807 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: openapi.Result

Check failure on line 807 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / Build

undefined: openapi.Result
}

for _, response := range responses {
tmp := openapi.ResultItem{

Check failure on line 811 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L811

undefined: openapi.ResultItem
Raw output
controller/questionnaire.go:811:18: undefined: openapi.ResultItem

Check failure on line 811 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L811

undefined: openapi.ResultItem
Raw output
controller/questionnaire.go:811:18: undefined: openapi.ResultItem

Check failure on line 811 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L811

undefined: openapi.ResultItem
Raw output
controller/questionnaire.go:811:18: undefined: openapi.ResultItem

Check failure on line 811 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: openapi.ResultItem
Body: response.Body,
IsDraft: response.IsDraft,
ModifiedAt: response.ModifiedAt,
QuestionnaireId: response.QuestionnaireId,
ResponseId: response.ResponseId,
SubmittedAt: response.SubmittedAt,
}
res = append(res, tmp)
}

return res, nil
}
158 changes: 158 additions & 0 deletions controller/reminder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package controller

import (
"context"
"slices"
"sort"
"sync"
"time"

"github.com/traPtitech/anke-to/model"
"github.com/traPtitech/anke-to/traq"
"golang.org/x/sync/semaphore"
)

type Job struct {
Timestamp time.Time
QuestionnaireID int
Action func()
}

type JobQueue struct {
jobs []*Job
mu sync.Mutex
}

var (
sem = semaphore.NewWeighted(1)
Jq = &JobQueue{}
Wg = &sync.WaitGroup{}
reminderTimingMinutes = []int{5, 30, 60, 1440, 10080}
reminderTimingStrings = []string{"5分", "30分", "1時間", "1日", "1週間"}
)

func (jq *JobQueue) Push(job *Job) {
jq.mu.Lock()
defer jq.mu.Unlock()
jq.jobs = append(jq.jobs, job)
sort.Slice(jq.jobs, func(i, j int) bool {
return jq.jobs[i].Timestamp.Before(jq.jobs[j].Timestamp)
})
}

func (jq *JobQueue) Pop() *Job {
jq.mu.Lock()
defer jq.mu.Unlock()
if len(jq.jobs) == 0 {
return nil
}
job := jq.jobs[0]
jq.jobs = jq.jobs[1:]
return job
}

func (jq *JobQueue) PushReminder(questionnaireID int, limit *time.Time) error {

for i, timing := range reminderTimingMinutes {
remindTimeStamp := limit.Add(-time.Duration(timing) * time.Minute)
if remindTimeStamp.Before(time.Now()) {
Jq.Push(&Job{
Timestamp: remindTimeStamp,
QuestionnaireID: questionnaireID,
Action: func() {
reminderAction(questionnaireID, reminderTimingStrings[i])
},
})
}
}

return nil
}

func (jq *JobQueue) DeleteReminder(questionnaireID int) error {
jq.mu.Lock()
defer jq.mu.Unlock()
if len(jq.jobs) == 1 && jq.jobs[0].QuestionnaireID == questionnaireID {
jq.jobs = []*Job{}
}
for i, job := range jq.jobs {
if job.QuestionnaireID == questionnaireID {
jq.jobs = append(jq.jobs[:i], jq.jobs[i+1:]...)
}
}

return nil
}

func (jq *JobQueue) CheckRemindStatus(questionnaireID int) (bool, error) {
jq.mu.Lock()
defer jq.mu.Unlock()
for _, job := range jq.jobs {
if job.QuestionnaireID == questionnaireID {
return true, nil
}
}
return false, nil
}

func reminderAction(questionnaireID int, leftTimeText string) error {
ctx := context.Background()
q := model.Questionnaire{}
questionnaire, _, _, administrators, _, respondants, err := q.GetQuestionnaireInfo(ctx, questionnaireID)
if err != nil {
return err
}

var reminderTargets []string
for _, target := range questionnaire.Targets {
if target.IsCanceled {
continue
}
if slices.Contains(respondants, target.UserTraqid) {
continue
}
reminderTargets = append(reminderTargets, target.UserTraqid)
}

reminderMessage := createReminderMessage(questionnaireID, questionnaire.Title, questionnaire.Description, administrators, questionnaire.ResTimeLimit.Time, reminderTargets, leftTimeText)
wh := traq.NewWebhook()
err = wh.PostMessage(reminderMessage)
if err != nil {
return err
}

return nil
}

func ReminderWorker() {
for {
job := Jq.Pop()
if job == nil {
time.Sleep(1 * time.Minute)
continue
}

if time.Until(job.Timestamp) > 0 {
time.Sleep(time.Until(job.Timestamp))
}

Wg.Add(1)
go func() {
defer Wg.Done()
job.Action()
}()
}
}

func ReminderInit() {
questionnaires, err := model.NewQuestionnaire().GetQuestionnairesInfoForReminder(context.Background())
if err != nil {
panic(err)
}
for _, questionnaire := range questionnaires {
err := Jq.PushReminder(questionnaire.ID, &questionnaire.ResTimeLimit.Time)
if err != nil {
panic(err)
}
}
}
1 change: 1 addition & 0 deletions docs/db_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,4 @@
| ---------------- | -------- | ---- | --- | ------- | ----- | -------- |
| questionnaire_id | int(11) | NO | PRI | _NULL_ |
| user_traqid | char(32) | NO | PRI | _NULL_ |
| is_canceled | boolean | NO | | false | | アンケートの対象者がキャンセルしたかどうか |
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.10.0
golang.org/x/sync v0.7.0
golang.org/x/sync v0.8.0
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.21.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,8 @@ golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
12 changes: 9 additions & 3 deletions handler/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"net/http"

"github.com/labstack/echo/v4"
"github.com/traPtitech/anke-to/controller"

Check failure on line 9 in handler/questionnaire.go

View workflow job for this annotation

GitHub Actions / Lint

could not import github.com/traPtitech/anke-to/controller (-: # github.com/traPtitech/anke-to/controller
"github.com/traPtitech/anke-to/model"
"github.com/traPtitech/anke-to/openapi"
)
Expand Down Expand Up @@ -118,7 +118,7 @@
status, err := q.GetQuestionnaireMyRemindStatus(ctx, questionnaireID)
if err != nil {
ctx.Logger().Errorf("failed to get questionnaire my remind status: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get questionnaire my remind status: %w", err))
return err
}
res.IsRemindEnabled = status

Expand All @@ -127,11 +127,17 @@

// (PATCH /questionnaires/{questionnaireID}/myRemindStatus)
func (h Handler) EditQuestionnaireMyRemindStatus(ctx echo.Context, questionnaireID openapi.QuestionnaireIDInPath) error {
params := openapi.EditQuestionnaireMyRemindStatusJSONRequestBody{}
if err := ctx.Bind(&params); err != nil {
ctx.Logger().Errorf("failed to bind request body: %+v", err)
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("failed to bind request body: %w", err))
}

q := controller.NewQuestionnaire()
err := q.EditQuestionnaireMyRemindStatus(ctx, questionnaireID)
err := q.EditQuestionnaireMyRemindStatus(ctx, questionnaireID, params.IsRemindEnabled)
if err != nil {
ctx.Logger().Errorf("failed to edit questionnaire my remind status: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to edit questionnaire my remind status: %w", err))
return err
}
return ctx.NoContent(200)
}
Expand Down
Loading
Loading