Skip to content

Commit

Permalink
Merge pull request #80 from juni-b-queer/agent-save-session-data
Browse files Browse the repository at this point in the history
feat: New IsNewPost validator to easily filter out the imported posts
  • Loading branch information
juni-b-queer authored Nov 21, 2024
2 parents 683baa0 + d07fbac commit aeaa307
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/dist

.env
tests/temp

.idea/

Expand Down
21 changes: 21 additions & 0 deletions src/validations/message-validators/post/PostValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,24 @@ export class IsReplyValidator extends AbstractMessageValidator {
return handlerAgent.hasPostReply(message);
}
}

export class IsNewPost extends AbstractMessageValidator {
constructor() {
super();
}

static make(): IsNewPost {
return new IsNewPost();
}

async handle(
handlerAgent: HandlerAgent,
message: CreateSkeetMessage
): Promise<boolean> {
const createdAt = new Date(message.record.createdAt);
const now = new Date();
const oneDay = 24 * 60 * 60 * 1000;

return now.getTime() - createdAt.getTime() < oneDay;
}
}
60 changes: 60 additions & 0 deletions tests/validations/post/PostValidators/IsNewPost.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
CreateSkeetMessage,
CreateSkeetMessageFactory,
CreateSkeetRecord,
CreateSkeetRecordFactory,
HandlerAgent,
IsNewPost,
} from '../../../../src';
import { BskyAgent } from '@atproto/api';
import dotenv from 'dotenv';
import fs from 'fs';

const sessPath = './tests/temp/val/post/isNewPost';
dotenv.config();
process.env.SESSION_DATA_PATH = sessPath;

describe('IsNewPost', () => {
afterAll(() => {
fs.rmSync(sessPath, {
recursive: true,
force: true,
});
});
fs.mkdirSync(sessPath, { recursive: true });
const validator = IsNewPost.make();
const botDid = 'did:plc:bot';
const bskyAgent: BskyAgent = {
session: {
did: botDid,
},
} as BskyAgent;
const handlerAgent: HandlerAgent = new HandlerAgent(
'name',
'handle',
'password',
bskyAgent
);

test('handle returns true if message is created within the last 24 hours', async () => {
const recentDate = new Date();
recentDate.setHours(recentDate.getHours() - 1);
const message: CreateSkeetMessage = CreateSkeetMessageFactory.factory()
.record({
createdAt: recentDate.toISOString(),
} as CreateSkeetRecord)
.create();

expect(await validator.handle(handlerAgent, message)).toBe(true);
});

test('handle returns false if message is created more than 24 hours ago', async () => {
const oldDate = new Date();
oldDate.setDate(oldDate.getDate() - 2);
const message: CreateSkeetMessage = CreateSkeetMessageFactory.factory()
.record({ createdAt: oldDate.toISOString() } as CreateSkeetRecord)
.create();

expect(await validator.handle(handlerAgent, message)).toBe(false);
});
});

0 comments on commit aeaa307

Please sign in to comment.