Skip to content

Commit

Permalink
feat: improve email text
Browse files Browse the repository at this point in the history
  • Loading branch information
surmon-china committed Dec 2, 2023
1 parent 61278e7 commit 4058204
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/modules/comment/comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export class CommentService {
const getMailContent = (subject = '') => {
const texts = [`${subject} on ${onWhere}.`, `${authorName}: ${comment.content}`]
const textHTML = texts.map((text) => `<p>${text}</p>`).join('')
const linkHTML = `<a href="${getPermalinkByID(comment.post_id)}" target="_blank">Reply to ${authorName}</a>`
const replyText = `Reply to ${authorName} #${comment.id}`
const commentLink = getPermalinkByID(comment.post_id) + `#comment-${comment.id}`
const linkHTML = `<a href="${commentLink}" target="_blank">${replyText}</a>`
return {
text: texts.join('\n'),
html: [textHTML, `<br>`, linkHTML].join('\n')
Expand Down
3 changes: 2 additions & 1 deletion src/modules/expansion/expansion.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { Module } from '@nestjs/common'
import { TagModule } from '@app/modules/tag/tag.module'
import { VoteModule } from '@app/modules/vote/vote.module'
import { ArticleModule } from '@app/modules/article/article.module'
import { CommentModule } from '@app/modules/comment/comment.module'
import { FeedbackModule } from '@app/modules/feedback/feedback.module'
Expand All @@ -14,7 +15,7 @@ import { StatisticService } from './expansion.service.statistic'
import { DBBackupService } from './expansion.service.dbbackup'

@Module({
imports: [TagModule, ArticleModule, CommentModule, FeedbackModule],
imports: [TagModule, VoteModule, ArticleModule, CommentModule, FeedbackModule],
controllers: [ExpansionController],
providers: [StatisticService, DBBackupService],
exports: [StatisticService, DBBackupService]
Expand Down
33 changes: 27 additions & 6 deletions src/modules/expansion/expansion.service.statistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import schedule from 'node-schedule'
import { Injectable } from '@nestjs/common'
import { CacheService } from '@app/processors/cache/cache.service'
import { EmailService } from '@app/processors/helper/helper.service.email'
import { VoteTarget, VoteType } from '@app/modules/vote/vote.model'
import { VoteService } from '@app/modules/vote/vote.service'
import { ArticleService } from '@app/modules/article/article.service'
import { CommentService } from '@app/modules/comment/comment.service'
import { FeedbackService } from '@app/modules/feedback/feedback.service'
Expand Down Expand Up @@ -39,6 +41,7 @@ export class StatisticService {
private readonly articleService: ArticleService,
private readonly commentService: CommentService,
private readonly feedbackService: FeedbackService,
private readonly voteService: VoteService,
private readonly tagService: TagService
) {
// daily data cleaning at 00:01
Expand All @@ -57,15 +60,33 @@ export class StatisticService {
private async dailyStatisticsTask(todayViews: number) {
const now = new Date()
const oneDayAgo = new Date(now.getTime() - 24 * 60 * 60 * 1000)
const todayNewComments = await this.commentService.countDocuments({
created_at: { $gte: oneDayAgo, $lt: now }
})
const createdAt = { $gte: oneDayAgo, $lt: now }
const [todayNewComments, todayArticleUpVotes, todayCommentUpVotes, todayCommentDownVotes] = await Promise.all([
this.commentService.countDocuments({ created_at: createdAt }),
this.voteService.countDocuments({
created_at: createdAt,
target_type: VoteTarget.Post,
vote_type: VoteType.Upvote
}),
this.voteService.countDocuments({
created_at: createdAt,
target_type: VoteTarget.Comment,
vote_type: VoteType.Upvote
}),
this.voteService.countDocuments({
created_at: createdAt,
target_type: VoteTarget.Comment,
vote_type: VoteType.Downvote
})
])

const emailContents = [
`Today views: ${todayViews}`,
`Today new comments: ${todayNewComments}`
// `Today Post votes: TODO`,
// `Today Comment votes: TODO`,
`Today new comments: ${todayNewComments}`,
`Today new post votes: +${todayArticleUpVotes}`,
`Today new comment votes: +${todayCommentUpVotes}, -${todayCommentDownVotes}`
]

this.emailService.sendMailAs(APP_CONFIG.APP.NAME, {
to: APP_CONFIG.APP.ADMIN_EMAIL,
subject: 'Daily Statistics',
Expand Down
2 changes: 1 addition & 1 deletion src/modules/vote/vote.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export class VoteController {
author: this.getAuthorString(voteAuthor),
userAgent: visitor.ua,
location: ipLocation,
link: getPermalinkByID(comment.post_id)
link: getPermalinkByID(comment.post_id) + `#comment-${comment.id}`
}
// email to admin
this.emailToTargetVoteMessage({
Expand Down
3 changes: 2 additions & 1 deletion src/modules/vote/vote.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { VoteController } from './vote.controller'
@Module({
imports: [OptionModule, ArticleModule, CommentModule, DisqusModule],
providers: [VoteProvider, VoteService],
controllers: [VoteController]
controllers: [VoteController],
exports: [VoteService]
})
export class VoteModule {}
5 changes: 5 additions & 0 deletions src/modules/vote/vote.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { Injectable } from '@nestjs/common'
import { FilterQuery, QueryOptions } from 'mongoose'
import { InjectModel } from '@app/transformers/model.transformer'
import { MongooseModel, MongooseDoc, MongooseID } from '@app/interfaces/mongoose.interface'
import { PaginateResult, PaginateQuery, PaginateOptions } from '@app/utils/paginate'
Expand Down Expand Up @@ -41,4 +42,8 @@ export class VoteService {
public batchDelete(voteIDs: MongooseID[]) {
return this.voteModel.deleteMany({ _id: { $in: voteIDs } }).exec()
}

public async countDocuments(filter: FilterQuery<Vote>, options?: QueryOptions<Vote>): Promise<number> {
return await this.voteModel.countDocuments(filter, options).exec()
}
}

0 comments on commit 4058204

Please sign in to comment.