Skip to content

Commit

Permalink
feat(api): add hadDmAuth and improve dmAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
PLhery committed Jun 25, 2023
1 parent 01b1288 commit e696dd7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions unfollow-ninja-server/src/api.ts
Expand Up @@ -92,6 +92,7 @@ const router = new Router()
profilePic: session.profilePic,
hasNotificationsEnabled: params.dmLastEventId !== 0,
otherProfiles: Object.values(session.otherProfiles || {}),
hasDMAuth: Boolean(params.dmRefreshToken),
};
}
})
Expand Down
18 changes: 16 additions & 2 deletions unfollow-ninja-server/src/api/auth.ts
Expand Up @@ -184,6 +184,7 @@ export function createAuthRouter(dao: Dao) {
country,
profilePic: session.profilePic,
hasNotificationsEnabled: params.dmLastEventId !== 0,
hasDMAuth: Boolean(params.dmRefreshToken),
otherProfiles: Object.values(session.otherProfiles || {}),
})
);
Expand Down Expand Up @@ -235,13 +236,25 @@ export function createAuthRouter(dao: Dao) {

const { id } = (await client.v2.me()).data;
if (id !== ctx.session.userId) {
ctx.body = { status: 'Oops, it looks like you logged in with the wrong account..' };
ctx.body =
'Oops, it looks like you logged in with the wrong account. Go to twitter.com, set the right account, and try again!';
ctx.status = 401;
return;
}
await dao.getUserDao(id).setUserParams({ dmRefreshToken: refreshToken });

if (!(await dao.getUserDao(id).getDmLastEventId())) {
const result = await dao.userEventDao.getFilteredUnfollowerEvents(id, 1);
await dao.getUserDao(id).setUserParams({ dmLastEventId: result[0]?.id ?? -1 });
void dao.userEventDao.logWebEvent(id, WebEvent.enableDms, ctx.ip, '', (result[0]?.id ?? -1).toString());
}

ctx.status = 200;
ctx.body = 'You successfully logged in!';
ctx.type = 'html';
ctx.body = `You successfully logged in! redirecting to the dashboard...
<script>
window.location.replace('${process.env.WEB_URL}/dashboard');
</script>`;
})
.post('/set-profile', async (ctx) => {
const session = ctx.session as NinjaSession;
Expand Down Expand Up @@ -277,6 +290,7 @@ export function createAuthRouter(dao: Dao) {
country,
profilePic: session.profilePic,
hasNotificationsEnabled: params.dmLastEventId !== 0,
hasDMAuth: Boolean(params.dmRefreshToken),
otherProfiles: Object.values(session.otherProfiles || {}),
};
});
Expand Down
4 changes: 4 additions & 0 deletions unfollow-ninja-server/src/tasks/sendDailyDM.ts
Expand Up @@ -38,6 +38,10 @@ export default class extends Task {
if (!lastEventId) {
continue; // not enabled
}
const hasNewDmTwitterApi = await userDao.hasNewDmTwitterApi();
if (!hasNewDmTwitterApi) {
continue;
}
dmCheckedCount++;
const username = await userDao.getUsername();
const events = await this.dao.userEventDao.getFilteredUnfollowerEventsSinceId(userId, lastEventId, 250, 0);
Expand Down

0 comments on commit e696dd7

Please sign in to comment.