Repository navigation
fix(ui): avoid timezone-ambiguous comment timestamps #1166
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
df488ec
fix(api): expose timezone-safe timestamps
awaae001 1e83d8c
build: Ignore artalk binary in .gitignore
awaae001 991db35
refactor(comment): Rename getDate to getCreatedAt for clarity
awaae001 02aaeba
Merge remote-tracking branch 'origin/master' into fix/timezone-safe-t…
awaae001 452a2bb
test: add timestamp compatibility tests for comments and pages
awaae001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package dao_test | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/artalkjs/artalk/v2/internal/dao" | ||
| "github.com/artalkjs/artalk/v2/internal/entity" | ||
| "github.com/stretchr/testify/require" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| func TestCookedTimestampsAcrossTimezones(t *testing.T) { | ||
| originalLocal := time.Local | ||
| t.Cleanup(func() { time.Local = originalLocal }) | ||
| instant := time.Date(2025, 1, 2, 3, 4, 5, 0, time.UTC) | ||
| d := &dao.Dao{} | ||
|
|
||
| for _, zone := range []*time.Location{ | ||
| time.UTC, | ||
| time.FixedZone("UTC+8", 8*60*60), | ||
| time.FixedZone("UTC-5", -5*60*60), | ||
| } { | ||
| t.Run(zone.String(), func(t *testing.T) { | ||
| time.Local = zone | ||
| model := gorm.Model{CreatedAt: instant.In(zone)} | ||
| page := &entity.Page{Model: model, Key: "https://example.test/page"} | ||
| comment := &entity.Comment{Model: model, Page: page, User: &entity.User{}} | ||
|
|
||
| for _, response := range []any{d.CookComment(comment), d.CookPage(page)} { | ||
| data, err := json.Marshal(response) | ||
| require.NoError(t, err) | ||
| var fields map[string]json.RawMessage | ||
| require.NoError(t, json.Unmarshal(data, &fields)) | ||
| var unix int64 | ||
| var utc, legacy string | ||
| require.NoError(t, json.Unmarshal(fields["date_unix"], &unix)) | ||
| require.NoError(t, json.Unmarshal(fields["date_utc"], &utc)) | ||
| require.NoError(t, json.Unmarshal(fields["date"], &legacy)) | ||
| require.Equal(t, instant.Unix(), unix) | ||
| require.Equal(t, "2025-01-02T03:04:05Z", utc) | ||
| require.Equal(t, instant.In(zone).Format(dao.CommonDateTimeFormat), legacy) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { expect, it, vi } from 'vitest' | ||
| import CommentNode from '@/comment/comment-node' | ||
| import { Defaults } from '@/defaults' | ||
| import { parseDate } from '@/lib/utils' | ||
| import { makeNestCommentNodeList } from '@/list/nest' | ||
| import type { CommentData } from '@/types' | ||
|
|
||
| it('keeps timestamp parsing, comment display and date sorting compatible', () => { | ||
| const instant = Date.parse('2025-01-02T03:04:05Z') | ||
| const legacy = '2025-01-01 03:04:05' | ||
| const base: CommentData = { | ||
| id: 1, | ||
| rid: 0, | ||
| content: '', | ||
| nick: 'Synthetic user', | ||
| email_encrypted: '', | ||
| link: '', | ||
| ua: '', | ||
| date: '2025-01-08 00:00:00', | ||
| is_collapsed: false, | ||
| is_pending: false, | ||
| is_pinned: false, | ||
| is_verified: false, | ||
| is_allow_reply: true, | ||
| page_key: '/test', | ||
| visible: true, | ||
| site_name: 'test', | ||
| vote_up: 0, | ||
| vote_down: 0, | ||
| } | ||
| const opts = { | ||
| ...Defaults, | ||
| flatMode: false, | ||
| getApi: vi.fn(), | ||
| replyComment: vi.fn(), | ||
| editComment: vi.fn(), | ||
| dateFormatter: (date: Date) => date.toISOString(), | ||
| } | ||
|
|
||
| // Unix seconds take precedence; UTC strings must also preserve the instant. | ||
| expect(+parseDate({ ...base, date_unix: instant / 1000, date_utc: '2025-01-09T00:00:00Z' })).toBe( | ||
| instant, | ||
| ) | ||
| for (const date_utc of [ | ||
| '2025-01-02T03:04:05Z', | ||
| '2025-01-02T11:04:05+08:00', | ||
| '2025-01-01T22:04:05-05:00', | ||
| ]) { | ||
| expect(+parseDate({ ...base, date_utc })).toBe(instant) | ||
| } | ||
|
|
||
| const comments: CommentData[] = [ | ||
| base, | ||
| { ...base, id: 3, rid: 1, date_utc: '2025-01-03T03:04:05Z' }, | ||
| { ...base, id: 4, rid: 1, date: legacy }, | ||
| { ...base, id: 2, rid: 1, date_unix: instant / 1000, date_utc: '2025-01-09T00:00:00Z' }, | ||
| ] | ||
| const expected = [instant, Date.parse('2025-01-03T03:04:05Z'), +new Date(2025, 0, 1, 3, 4, 5)] | ||
| for (const [index, id] of [2, 3, 4].entries()) { | ||
| const data = comments.find((comment) => comment.id === id)! | ||
| const node = new CommentNode(data, opts) | ||
| expect(+parseDate(data)).toBe(expected[index]) | ||
| expect(+node.getCreatedAt()).toBe(expected[index]) | ||
| expect(node.getDateFormatted()).toBe(new Date(expected[index]).toISOString()) | ||
| } | ||
| expect(makeNestCommentNodeList(comments, 'DATE_ASC')[0].children.map((node) => node.id)).toEqual([ | ||
| 4, 2, 3, | ||
| ]) | ||
| expect(makeNestCommentNodeList(comments, 'DATE_DESC')[0].children.map((node) => node.id)).toEqual( | ||
| [3, 2, 4], | ||
| ) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.