This repository has been archived by the owner on Oct 1, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #973 from Lokiedu/feature/comment-notifications-ap…
…i-ext Extend comment email notification API
- Loading branch information
Showing
24 changed files
with
1,074 additions
and
425 deletions.
There are no files selected for viewing
This file contains 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,53 @@ | ||
export async function up(knex) { | ||
// Replace `mute_all_posts` with `comment_notifications` | ||
// Initialize`more.last_comment_notification_at` for each user. | ||
const users = await knex('users') | ||
.select('id', 'more'); | ||
|
||
for (const user of users) { | ||
if (!user.more) { | ||
user.more = {}; | ||
} | ||
|
||
let comment_notifications = 'on'; | ||
if (user.more.mute_all_posts) { | ||
comment_notifications = 'off'; | ||
} | ||
|
||
const more = { | ||
...users.more, | ||
comment_notifications, | ||
last_comment_notification_at: new Date().toJSON() | ||
}; | ||
|
||
delete more.mute_all_posts; | ||
|
||
await knex('users').where('id', user.id).update({ more }); | ||
} | ||
} | ||
|
||
export async function down(knex) { | ||
const users = await knex('users') | ||
.select('id', 'more'); | ||
|
||
for (const user of users) { | ||
if (!user.more) { | ||
user.more = {}; | ||
} | ||
|
||
let mute_all_posts = false; | ||
if (user.more.comment_notifications === 'off') { | ||
mute_all_posts = false; | ||
} | ||
|
||
const more = { | ||
...users.more, | ||
mute_all_posts, | ||
}; | ||
|
||
delete more.last_comment_notification_at; | ||
delete more.comment_notifications; | ||
|
||
await knex('users').where('id', user.id).update({ more }); | ||
} | ||
} |
This file contains 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 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 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,66 @@ | ||
import path from 'path'; | ||
import faker from 'faker'; | ||
import moment from 'moment'; | ||
import md5 from 'md5'; | ||
import { renderFile } from 'ejs'; | ||
import { promisify } from 'bluebird'; | ||
import { API_HOST } from '../../../config'; | ||
|
||
const renderFileAsync = promisify(renderFile); | ||
|
||
const commonTemplateParams = { | ||
host: API_HOST | ||
}; | ||
|
||
function post({ numComments } = { numComments: 1 }) { | ||
return { | ||
id: faker.random.uuid(), | ||
title: faker.name.title(), | ||
url: `https://www.libertysoil.com/post/${faker.random.uuid()}`, | ||
author: user(), | ||
comments: Array.apply(null, Array(numComments)).map(() => comment()), | ||
}; | ||
} | ||
|
||
function user({ avatarSize } = { avatarSize: 36 }) { | ||
return { | ||
name: faker.name.findName(), | ||
url: `https://www.libertysoil.com/u/${faker.random.uuid()}`, | ||
avatarUrl: `http://www.gravatar.com/avatar/${md5(faker.internet.email().toLowerCase())}?s=${avatarSize}&r=g&d=retro`, | ||
}; | ||
} | ||
|
||
function comment() { | ||
return { | ||
text: faker.lorem.paragraph(), | ||
date: moment(faker.date.recent()).format('Do [of] MMMM YYYY'), | ||
author: user({ avatarSize: 17 }), | ||
}; | ||
} | ||
|
||
const templateParams = { | ||
'new-comments.ejs': { | ||
posts: [ | ||
post({ numComments: 3 }), | ||
post({ numComments: 1 }), | ||
post({ numComments: 4 }), | ||
] | ||
}, | ||
'new-comment.ejs': { | ||
post: post(), | ||
} | ||
}; | ||
|
||
export async function renderEmailTemplate(ctx) { | ||
const filePath = path.join('src/email-templates', ctx.params.name); | ||
|
||
try { | ||
ctx.body = await renderFileAsync(filePath, { | ||
...commonTemplateParams, | ||
...templateParams[ctx.params.name], | ||
}); | ||
} catch (e) { | ||
console.error(e); // eslint-disable-line no-console | ||
ctx.body = e.stack; | ||
} | ||
} |
This file contains 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 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 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 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 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
Oops, something went wrong.