User setting to sort comments #2307

Open
andileni wants to merge 3 commits from andileni/vikunja:main into main
7 changed files with 33 additions and 3 deletions

View File

@ -269,7 +269,8 @@ async function loadComments(taskId: ITask['id']) {
newComment.taskId = taskId
commentEdit.taskId = taskId
commentToDelete.taskId = taskId
comments.value = await taskCommentService.getAll({taskId})
let order = authStore.settings.frontendSettings.commentOrder;
comments.value = await taskCommentService.getAll({taskId}, {orderBy: order})
}
watch(

View File

@ -150,6 +150,11 @@
"dark": "Dark"
}
},
"commentOrder": {
"title": "Comment order",
"asc": "Oldest first",
"desc": "Newest first"
},
"apiTokens": {
"title": "API Tokens",
"general": "API tokens allow you to use Vikunja's API without user credentials.",
@ -1206,4 +1211,4 @@
"years": "year|years"
}
}
}
}

View File

@ -10,6 +10,7 @@ export interface IFrontendSettings {
quickAddMagicMode: PrefixMode
colorSchema: BasicColorSchema
filterIdUsedOnOverview: IProject['id'] | null
commentOrder: string
}
export interface IUserSettings extends IAbstract {

View File

@ -19,6 +19,7 @@ export default class UserSettingsModel extends AbstractModel<IUserSettings> impl
playSoundWhenDone: true,
quickAddMagicMode: PrefixMode.Default,
colorSchema: 'auto',
commentOrder: 'asc',
}
constructor(data: Partial<IUserSettings> = {}) {

View File

@ -103,6 +103,7 @@ export const useAuthStore = defineStore('auth', () => {
playSoundWhenDone: true,
quickAddMagicMode: PrefixMode.Default,
colorSchema: 'auto',
commentOrder: 'asc',
...newSettings.frontendSettings,
},
})

View File

@ -187,6 +187,23 @@
</div>
</label>
</div>
<div class="field">
<label class="is-flex is-align-items-center">
<span>
{{ $t('user.settings.commentOrder.title') }}
</span>
<div class="select ml-2">
<select v-model="settings.frontendSettings.commentOrder">
<option value="asc">
{{ $t('user.settings.commentOrder.asc') }}
</option>
<option value="desc">
{{ $t('user.settings.commentOrder.desc') }}
</option>
</select>
</div>
</label>
</div>
<x-button
v-cy="'saveGeneralSettings'"

View File

@ -18,6 +18,7 @@ package models
import (
"time"
"fmt"
"code.vikunja.io/api/pkg/db"
@ -37,6 +38,9 @@ type TaskComment struct {
Author *user.User `xorm:"-" json:"author"`
TaskID int64 `xorm:"not null" json:"-" param:"task"`
// The query parameter to order the items by. This can be either asc or desc, with asc being the default.
OrderBy []string `query:"order_by" json:"order_by"`
Review

There should be a SortBy as well for the property to sort comments by. "Order asc / desc" does not tell you which property the sorting is using. Similar to how sorting is done in the TaskCollection.

No need to implement any other property than created in this PR but it should be extendable without breaking changes in the future.

Please exclude this from json as it's not used in the actual comment and document this as query parameter in the swagger annotation comment.

There should be a `SortBy` as well for the property to sort comments by. "Order asc / desc" does not tell you which property the sorting is using. Similar to how sorting is done in the `TaskCollection`. No need to implement any other property than `created` in this PR but it should be extendable without breaking changes in the future. Please exclude this from `json` as it's not used in the actual comment and document this as query parameter in the swagger annotation comment.
Reactions ReactionMap `xorm:"-" json:"reactions"`
Created time.Time `xorm:"created" json:"created"`
@ -270,7 +274,7 @@ func (tc *TaskComment) ReadAll(s *xorm.Session, auth web.Auth, search string, pa
query := s.
Where(builder.And(where...)).
Join("LEFT", "users", "users.id = task_comments.author_id").
OrderBy("task_comments.created asc")
OrderBy(fmt.Sprintf("task_comments.created %s", tc.OrderBy[0]))
Review

Please use "task_comments.created " + orderBy and make sure the property to order is defined. With this approach, the api will crash when you do not pass an order parameter.

Please use `"task_comments.created " + orderBy` and make sure the property to order is defined. With this approach, the api will crash when you do not pass an order parameter.
if limit > 0 {
query = query.Limit(limit, start)
}