Make sure all associated entities of a task are deleted when the task is deleted

This commit is contained in:
kolaente 2021-07-19 23:52:58 +02:00
parent d28390d792
commit e4a0066e20
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B

View file

@ -1353,6 +1353,49 @@ func (t *Task) Delete(s *xorm.Session, a web.Auth) (err error) {
return err return err
} }
// Delete Favorites
err = removeFromFavorite(s, t.ID, a, FavoriteKindTask)
if err != nil {
return
}
// Delete label associations
_, err = s.Where("task_id = ?", t.ID).Delete(&LabelTask{})
if err != nil {
return
}
// Delete task attachments
attachments, err := getTaskAttachmentsByTaskIDs(s, []int64{t.ID})
if err != nil {
return err
}
for _, attachment := range attachments {
// Using the attachment delete method here because that takes care of removing all files properly
err = attachment.Delete(s, a)
if err != nil && !IsErrTaskAttachmentDoesNotExist(err) {
return err
}
}
// Delete all comments
_, err = s.Where("task_id = ?", t.ID).Delete(&TaskComment{})
if err != nil {
return
}
// Delete all relations
_, err = s.Where("task_id = ? OR other_task_id = ?", t.ID, t.ID).Delete(&TaskRelation{})
if err != nil {
return
}
// Delete all reminders
_, err = s.Where("task_id = ?", t.ID).Delete(&TaskReminder{})
if err != nil {
return
}
doer, _ := user.GetFromAuth(a) doer, _ := user.GetFromAuth(a)
err = events.Dispatch(&TaskDeletedEvent{ err = events.Dispatch(&TaskDeletedEvent{
Task: t, Task: t,