Skip to content

Commit

Permalink
时区支持(部分)
Browse files Browse the repository at this point in the history
  • Loading branch information
movsb committed Nov 15, 2024
1 parent 8cc9c27 commit 880a410
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 14 deletions.
1 change: 1 addition & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (s *Server) Serve(ctx context.Context, testing bool, cfg *config.Config, re
})

log.Println(`DevMode:`, service.DevMode())
log.Println(`Time.Now:`, time.Now().Format(time.RFC3339))

instantNotifier := notify.NewConsoleNotify()
if token := cfg.Notify.Chanify.Token; token != "" {
Expand Down
3 changes: 1 addition & 2 deletions modules/logs/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ func (w *_ResponseWriter) Write(b []byte) (int, error) {
}

func (l *RequestLogger) Handler(h http.Handler) http.Handler {
tz := time.FixedZone(`China`, 8*60*60)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
takeOver := &_ResponseWriter{
ResponseWriter: w,
Expand All @@ -102,7 +101,7 @@ func (l *RequestLogger) Handler(h http.Handler) http.Handler {
l.counter.Store(0)
l.lock.Lock()
defer l.lock.Unlock()
now := time.Now().In(tz).Format(`2006-01-02 15:04:05`)
now := time.Now().Format(time.RFC3339)
ac := auth.Context(r.Context())
fmt.Fprintf(l.f,
"%s %-15s %3d %-8s %-32s %-32s %-32s\n",
Expand Down
4 changes: 1 addition & 3 deletions modules/utils/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ func (m *Maintenance) EstimatedString() string {
if m.Estimated < 0 {
return `(未知)`
}
t := time.Now().Add(m.Estimated)
tz := time.FixedZone(`China`, 8*60*60)
return t.In(tz).Format(`2006-01-02 15:04:05 -0700`)
return time.Now().Add(m.Estimated).Format(time.RFC3339)
}

func (m *Maintenance) Handler(exception func(ctx context.Context) bool) func(http.Handler) http.Handler {
Expand Down
4 changes: 4 additions & 0 deletions protocols/comment.proto
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ message Comment {
int32 avatar = 18;

int32 modified = 19;

// 创建时间和修改时间所在的时区。
string date_timezone = 20;
string modified_timezone = 21;
}

message GetCommentRequest {
Expand Down
4 changes: 4 additions & 0 deletions protocols/post.proto
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ message Post {

// 文章的链接。
string link = 19;

// 创建时间和修改时间所在的时区。
string date_timezone = 20;
string modified_timezone = 21;
}

enum LinkKind {
Expand Down
11 changes: 11 additions & 0 deletions service/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ func (s *Service) UpdateComment(ctx context.Context, req *proto.UpdateCommentReq
case `modified`:
hasModified = true
data[`modified`] = time.Now().Unix()
case `modified_timezone`:
// hasModified = true
data[mask] = req.Comment.ModifiedTimezone
}
}
if !hasModified {
Expand Down Expand Up @@ -385,13 +388,21 @@ func (s *Service) CreateComment(ctx context.Context, in *proto.Comment) (*proto.
Source: in.Source,
}

c.ModifiedTimezone = in.ModifiedTimezone
if in.Modified > 0 {
c.Modified = in.Modified
}

c.DateTimezone = in.DateTimezone
if c.ModifiedTimezone == `` && c.DateTimezone != `` {
c.ModifiedTimezone = c.DateTimezone
}

if in.Date > 0 {
c.Date = in.Date
if in.Modified == 0 {
c.Modified = c.Date
c.ModifiedTimezone = c.DateTimezone
}
} else {
c.Date = int32(now.Unix())
Expand Down
10 changes: 10 additions & 0 deletions service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type Service struct {

home *url.URL

// 服务器默认的时区。
timeLocation *time.Location

cfg *config.Config

postDataFS theme_fs.FS
Expand Down Expand Up @@ -149,6 +152,9 @@ func newService(ctx context.Context, cancel context.CancelFunc, cfg *config.Conf
cfg: cfg,
postDataFS: &theme_fs.Empty{},

// TODO 可配置使用的时区,而不是使用服务器当前时间或者硬编码成+8时区。
timeLocation: time.Now().Location(),

db: db,
tdb: taorm.NewDB(db),
auth: auther,
Expand Down Expand Up @@ -268,6 +274,10 @@ func newService(ctx context.Context, cancel context.CancelFunc, cfg *config.Conf

// 从 Context 中取出用户并且必须为 Admin/System,否则 panic。
func (s *Service) MustBeAdmin(ctx context.Context) *auth.AuthContext {
return MustBeAdmin(ctx)
}

func MustBeAdmin(ctx context.Context) *auth.AuthContext {
ac := auth.Context(ctx)
if ac == nil {
panic("AuthContext 不应为 nil")
Expand Down
6 changes: 6 additions & 0 deletions service/models/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type Comment struct {
Modified int32 `json:"modified"`
SourceType string `json:"source_type"`
Source string `json:"source"`

DateTimezone string
ModifiedTimezone string
}

// TableName ...
Expand Down Expand Up @@ -52,6 +55,9 @@ func (c *Comment) ToProto(redact func(c *proto.Comment)) *proto.Comment {
SourceType: c.SourceType,
Source: c.Source,
DateFuzzy: timeago.Chinese.Format(time.Unix(int64(c.Date), 0)),

DateTimezone: c.DateTimezone,
ModifiedTimezone: c.ModifiedTimezone,
}
redact(&comment)
return &comment
Expand Down
6 changes: 6 additions & 0 deletions service/models/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type Post struct {
Metas PostMeta
Source string
SourceType string

DateTimezone string
ModifiedTimezone string
}

// NOTE 如果要添加字段,记得同步 isEmpty 方法。
Expand Down Expand Up @@ -179,6 +182,9 @@ func (p *Post) ToProto(redact func(p *proto.Post) error) (*proto.Post, error) {
SourceType: p.SourceType,

LastCommentedAt: p.LastCommentedAt,

DateTimezone: p.DateTimezone,
ModifiedTimezone: p.ModifiedTimezone,
}
err := redact(&out)
return &out, err
Expand Down
12 changes: 12 additions & 0 deletions service/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,17 +500,24 @@ func (s *Service) CreatePost(ctx context.Context, in *proto.Post) (*proto.Post,
return nil, status.Error(codes.InvalidArgument, "内容不应为空。")
}

p.ModifiedTimezone = in.ModifiedTimezone
if in.Modified > 0 {
p.Modified = in.Modified
}

p.DateTimezone = in.DateTimezone
if in.Date > 0 {
p.Date = in.Date
if in.Modified == 0 {
p.Modified = p.Date
p.ModifiedTimezone = p.DateTimezone
}
} else {
p.Date = now
p.Modified = now
// TODO 设置时区。
p.DateTimezone = ``
p.ModifiedTimezone = ``
}

if in.Status != "" {
Expand Down Expand Up @@ -577,6 +584,7 @@ func (s *Service) UpdatePost(ctx context.Context, in *proto.UpdatePostRequest) (
// 适用于导入三方数据的时候更新导入。
if !in.DoNotTouch {
m[`modified`] = now
// TODO 使用 now 的时区对应名修改 modified_timezone
}

var hasSourceType, hasSource bool
Expand Down Expand Up @@ -608,6 +616,10 @@ func (s *Service) UpdatePost(ctx context.Context, in *proto.UpdatePostRequest) (
hasType = true
case `date`:
m[`date`] = in.Post.Date
case `date_timezone`:
m[path] = in.Post.DateTimezone
case `modified_timezone`:
m[path] = in.Post.ModifiedTimezone
case `status`:
m[`status`] = in.Post.Status
default:
Expand Down
4 changes: 4 additions & 0 deletions setup/data/schemas.sqlite.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ CREATE TABLE IF NOT EXISTS `options` (
CREATE TABLE IF NOT EXISTS `posts` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`date` INTEGER NOT NULL,
`date_timezone` TEXT NOT NULL,
`modified` INTEGER NOT NULL,
`modified_timezone` TEXT NOT NULL,
`last_commented_at` INTEGER NOT NULL,
`title` TEXT NOT NULL,
`slug` TEXT NOT NULL,
Expand All @@ -35,7 +37,9 @@ CREATE TABLE IF NOT EXISTS `comments` (
`url` TEXT NOT NULL,
`ip` TEXT NOT NULL,
`date` INTEGER NOT NULL,
`date_timezone` TEXT NOT NULL,
`modified` INTEGER NOT NULL,
`modified_timezone` TEXT NOT NULL,
`source_type` TEXT NOT NULL,
`source` TEXT NOT NULL,
`parent` INTEGER NOT NULL,
Expand Down
5 changes: 5 additions & 0 deletions setup/migration/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func Init(db *sql.DB, path string) {
Status: `public`,
SourceType: `markdown`,
Source: `你好,世界!这是您的第一篇文章。`,

// TODO 用配置时区。
DateTimezone: ``,
// TODO 用配置时区。
ModifiedTimezone: ``,
}).MustCreate()
})
}
1 change: 1 addition & 0 deletions setup/migration/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var gVersions = []VersionUpdater{
{27, v27},
{28, v28},
{29, v29},
{30, v30},
}

// MaxVersionNumber ...
Expand Down
7 changes: 7 additions & 0 deletions setup/migration/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,10 @@ func v29(tx *sql.Tx) {
mustExec(tx, `delete from comments where id=?`, id)
}
}

func v30(tx *sql.Tx) {
mustExec(tx, "ALTER TABLE posts ADD COLUMN `date_timezone` TEXT NOT NULL DEFAULT ''")
mustExec(tx, "ALTER TABLE posts ADD COLUMN `modified_timezone` TEXT NOT NULL DEFAULT ''")
mustExec(tx, "ALTER TABLE comments ADD COLUMN `date_timezone` TEXT NOT NULL DEFAULT ''")
mustExec(tx, "ALTER TABLE comments ADD COLUMN `modified_timezone` TEXT NOT NULL DEFAULT ''")
}
60 changes: 55 additions & 5 deletions theme/blog/statics/scripts/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,50 @@ document.write(function(){/*
</div>
*/}.toString().slice(14,-3));

class TimeWithZone {
constructor(timestamp, zone) {
const now = new Date();
if (typeof timestamp != 'number') {
timestamp = now.getTime() / 1000;
zone = TimeWithZone.getTimezone();
} else if (typeof zone != 'string' || zone == '') {
zone = TimeWithZone.getTimezone();
}
this._timestamp = timestamp;
this._zone = zone;
}

get time() { return this._timestamp; }
get zone() { return this._zone; }

format() {
const options = {
timeZone: this._zone,
timeZoneName: 'longOffset',
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hourCycle: 'h24',
}
return new Intl.DateTimeFormat(navigator.language, options).format(new Date(this._timestamp));
}

toJSON() {
return new Date(this._timestamp).toJSON();
}

static getTimezone() {
try {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
} catch {
return '';
}
}
}

class CommentAPI
{
constructor(postID) {
Expand Down Expand Up @@ -90,6 +134,8 @@ class CommentAPI
c.date = +(c.date ?? 0);
c.modified = +(c.modified ?? 0);
c.date_fuzzy = c.date_fuzzy ?? '';
c.date_timezone = c.date_timezone ?? '';
c.modified_timezone = c.modified_timezone ?? '';

c.is_admin = c.is_admin ?? false;
c.geo_location = c.geo_location ?? '';
Expand Down Expand Up @@ -131,7 +177,8 @@ class CommentAPI
comment: {
source_type: 'markdown',
source: source,
modified: modified,
modified: modified.time,
modified_timezone: modified.zone,
},
update_mask: 'source,sourceType,modified'
})
Expand Down Expand Up @@ -681,7 +728,7 @@ class Comment {
};

let loggedin = cmt.ip != '';
let date = new Date(cmt.date * 1000);
let date = new TimeWithZone(cmt.date, cmt.date_timezone);

// 登录后可以显示评论者的详细信息
let info = '';
Expand All @@ -692,7 +739,7 @@ class Comment {
网址:${cmt.url}
地址:${cmt.ip}
位置:${cmt.geo_location}
日期:${date.toLocaleString()}
日期:${date.format()}
`;
}

Expand Down Expand Up @@ -720,7 +767,7 @@ class Comment {
<div class="comment-meta">
<span class="${cmt.is_admin ? "author" : "nickname"}">${h2t(cmt.author)}</span>
${urlContent}
<time class="date" datetime="${date.toJSON()}" title="${date.toLocaleString()}" data-unix="${Math.floor(date.getTime()/1000)}">${cmt.date_fuzzy}</time>
<time class="date" datetime="${date.toJSON()}" title="${date.format()}" data-unix="${date.time}">${cmt.date_fuzzy}</time>
</div>
${cmt.source_type === 'markdown'
? `<div class="comment-content html-content reset-list-style-type">${cmt.content}</div>`
Expand Down Expand Up @@ -897,7 +944,7 @@ class Comment {
let id = this.being_edited;
let raw = this.list.comments[id];

let updated = await this.api.updateComment(id, raw.modified, source);
let updated = await this.api.updateComment(id, new TimeWithZone(raw.modified), source);
this.list.update(updated);

this.clearContent();
Expand All @@ -920,6 +967,9 @@ class Comment {
}
async createComment() {
let body = this.formData();

body.date_timezone = TimeWithZone.getTimezone();

let cmt = await this.api.createComment(body);
this.list.insert(cmt);
this.toggle_post_comment_button();
Expand Down
4 changes: 0 additions & 4 deletions theme/data/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/movsb/taoblog/cmd/config"
"github.com/movsb/taoblog/modules/auth"
"github.com/movsb/taoblog/protocols/go/proto"
"github.com/xeonx/timeago"
)

// PostData ...
Expand Down Expand Up @@ -105,9 +104,6 @@ func (p *Post) DateString() string {
y, m, d := t.Date()
return fmt.Sprintf("%d年%02d月%02d日", y, m, d)
}
func (p *Post) ShortDateString() string {
return timeago.Chinese.Format(time.Unix(int64(p.Date), 0))
}
func (p *Post) CommentString() string {
if p.Comments == 0 {
return `没有评论`
Expand Down

0 comments on commit 880a410

Please sign in to comment.