Skip to content

Commit 0a1370f

Browse files
committed
add timezone offset
1 parent 785b983 commit 0a1370f

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
lines changed

pages/api/open/comments.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ export default async function handler(
8888
pageId: string
8989
}
9090

91+
const timezoneOffsetInHour = req.headers['x-timezone-offset']
92+
9193
const isDeleted = await projectService.isDeleted(query.appId)
9294

9395
if (isDeleted) {
@@ -121,15 +123,19 @@ export default async function handler(
121123
},
122124
)
123125

124-
const comments = await commentService.getComments(query.appId, {
125-
approved: true,
126-
parentId: null,
127-
pageSlug: query.pageId,
128-
page: Number(query.page) || 1,
129-
select: {
130-
by_nickname: true,
126+
const comments = await commentService.getComments(
127+
query.appId,
128+
Number(timezoneOffsetInHour),
129+
{
130+
approved: true,
131+
parentId: null,
132+
pageSlug: query.pageId,
133+
page: Number(query.page) || 1,
134+
select: {
135+
by_nickname: true,
136+
},
131137
},
132-
})
138+
)
133139

134140
queryCommentStat.end()
135141

pages/api/project/[projectId]/comments.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export default async function handler(
1818
page: string
1919
}
2020

21+
const timezoneOffsetInHour = req.headers['x-timezone-offset'] || 0
22+
2123
// only owner can get comments
2224
const project = (await projectService.get(projectId, {
2325
select: {
@@ -36,7 +38,7 @@ export default async function handler(
3638
}
3739
})
3840

39-
const comments = await commentService.getComments(projectId, {
41+
const comments = await commentService.getComments(projectId, Number(timezoneOffsetInHour), {
4042
parentId: null,
4143
page: Number(page),
4244
onlyOwn: true,

service/comment.service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { statService } from './stat.service'
99
import { EmailService } from './email.service'
1010
import { TokenService } from './token.service'
1111
import { makeConfirmReplyNotificationTemplate } from '../templates/confirm_reply_notification'
12+
import utc from 'dayjs/plugin/utc'
13+
dayjs.extend(utc)
1214

1315
export const markdown = MarkdownIt({
1416
linkify: true,
@@ -39,6 +41,7 @@ export class CommentService extends RequestScopeService {
3941

4042
async getComments(
4143
projectId: string,
44+
timezoneOffset: number,
4245
options?: {
4346
parentId?: string
4447
page?: number
@@ -105,7 +108,7 @@ export class CommentService extends RequestScopeService {
105108
const allComments = await Promise.all(
106109
comments.map(async (comment: Comment) => {
107110
// get replies
108-
const replies = await this.getComments(projectId, {
111+
const replies = await this.getComments(projectId, timezoneOffset, {
109112
...options,
110113
page: 1,
111114
// hard code 100 because we havent implement pagination in nested comment
@@ -115,7 +118,7 @@ export class CommentService extends RequestScopeService {
115118
select,
116119
})
117120

118-
const parsedCreatedAt = dayjs(comment.createdAt).format(
121+
const parsedCreatedAt = dayjs.utc(comment.createdAt).utcOffset(timezoneOffset).format(
119122
'YYYY-MM-DD HH:mm',
120123
)
121124
const parsedContent = markdown.render(comment.content) as string

utils.client.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import axios from "axios";
22

33
export const apiClient = axios.create({
44
baseURL: "/api",
5+
headers: {
6+
'x-timezone-offset': -new Date().getTimezoneOffset()
7+
}
58
});
69

710
export const VERSION = '1.1.4'

widget/Widget.svelte

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
loadingComments = true
4040
try {
4141
const res = await api.get(`/api/open/comments`, {
42+
headers: {
43+
'x-timezone-offset': -new Date().getTimezoneOffset()
44+
},
4245
params: {
4346
page: p,
4447
appId: attrs.appId,

0 commit comments

Comments
 (0)