Skip to content

Commit

Permalink
feat(api): improve enable-dms endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
PLhery committed Jun 23, 2023
1 parent f8be000 commit 212a63f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
8 changes: 3 additions & 5 deletions unfollow-ninja-server/src/api/user.ts
Expand Up @@ -42,10 +42,9 @@ export function createUserRouter(dao: Dao) {
session.twitterTokenSecret = null;
ctx.status = 204;
})
.get('/enable-dms', async (ctx) => {
.put('/enable-dms', async (ctx) => {
const session = ctx.session as NinjaSession;
const result = await dao.userEventDao.getFilteredUnfollowerEvents(session.userId, 1);
ctx.body = result;
await dao.getUserDao(session.userId).setUserParams({ dmLastEventId: result[0]?.id ?? -1 });
void dao.userEventDao.logWebEvent(
session.userId,
Expand All @@ -54,10 +53,9 @@ export function createUserRouter(dao: Dao) {
session.username,
(result[0]?.id ?? -1).toString()
);

ctx.status = 200;
ctx.status = 204;
})
.get('/disable-dms', async (ctx) => {
.put('/disable-dms', async (ctx) => {
const session = ctx.session as NinjaSession;
await dao.getUserDao(session.userId).setUserParams({ dmLastEventId: 0 });
void dao.userEventDao.logWebEvent(session.userId, WebEvent.enableDms, ctx.ip, session.username, '0');
Expand Down
3 changes: 3 additions & 0 deletions unfollow-ninja-server/src/tasks/sendDailyDM.ts
Expand Up @@ -26,6 +26,7 @@ export default class extends Task {
let dmSentCount = 0;
let dmAttemptCount = 0;
let unfollowersCount = 0;
let dmCheckedCount = 0;

for (const userId of [
...(await this.dao.getUserIdsByCategory(UserCategory.vip)),
Expand All @@ -36,6 +37,7 @@ export default class extends Task {
if (!lastEventId) {
continue; // not enabled
}
dmCheckedCount++;
const username = await userDao.getUsername();
const events = await this.dao.userEventDao.getFilteredUnfollowerEventsSinceId(userId, lastEventId, 250, 0);

Expand Down Expand Up @@ -93,6 +95,7 @@ export default class extends Task {
await userDao.setUserParams({ dmLastEventId: events[0].id });
}
}
metrics.gauge('sendDailyDm.checked', dmCheckedCount);
metrics.gauge('sendDailyDm.sent', dmSentCount);
metrics.gauge('sendDailyDm.attempt', dmAttemptCount);
metrics.gauge('sendDailyDm.unfollowers', unfollowersCount);
Expand Down

0 comments on commit 212a63f

Please sign in to comment.