Refactor to use nostrDate() and nostrNow()

This commit is contained in:
Alex Gleason 2023-07-08 15:04:50 -05:00
parent bdf8e9a61d
commit cc751f03a7
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
5 changed files with 15 additions and 12 deletions

View File

@ -4,7 +4,7 @@ import { getAuthor, getFilter, getFollows, publish } from '@/client.ts';
import { parseMetaContent } from '@/schema.ts';
import { signEvent } from '@/sign.ts';
import { toAccount, toStatus } from '@/transmute.ts';
import { buildLinkHeader, eventDateComparator, lookupAccount, paginationSchema, parseBody } from '@/utils.ts';
import { buildLinkHeader, eventDateComparator, lookupAccount, nostrNow, paginationSchema, parseBody } from '@/utils.ts';
const createAccountController: AppController = (c) => {
return c.json({ error: 'Please log in with Nostr.' }, 405);
@ -162,7 +162,7 @@ const updateCredentialsController: AppController = async (c) => {
kind: 0,
content: JSON.stringify(meta),
tags: [],
created_at: Math.floor(new Date().getTime() / 1000),
created_at: nostrNow(),
}, c);
publish(event);

View File

@ -1,6 +1,6 @@
import { lodash, nip19, uuid62, z } from '@/deps.ts';
import { AppController } from '@/app.ts';
import { parseBody } from '@/utils.ts';
import { nostrNow, parseBody } from '@/utils.ts';
const passwordGrantSchema = z.object({
grant_type: z.literal('password'),
@ -36,21 +36,21 @@ const createTokenController: AppController = async (c) => {
access_token: result.data.password,
token_type: 'Bearer',
scope: 'read write follow push',
created_at: Math.floor(new Date().getTime() / 1000),
created_at: nostrNow(),
});
case 'authorization_code':
return c.json({
access_token: result.data.code,
token_type: 'Bearer',
scope: 'read write follow push',
created_at: Math.floor(new Date().getTime() / 1000),
created_at: nostrNow(),
});
case 'client_credentials':
return c.json({
access_token: '_',
token_type: 'Bearer',
scope: 'read write follow push',
created_at: Math.floor(new Date().getTime() / 1000),
created_at: nostrNow(),
});
}
};

View File

@ -4,7 +4,7 @@ import { ISO6391, Kind, z } from '@/deps.ts';
import { type Event } from '@/event.ts';
import { signEvent } from '@/sign.ts';
import { toStatus } from '@/transmute.ts';
import { parseBody } from '@/utils.ts';
import { nostrNow, parseBody } from '@/utils.ts';
const createStatusSchema = z.object({
in_reply_to_id: z.string().regex(/[0-9a-f]{64}/).nullish(),
@ -74,7 +74,7 @@ const createStatusController: AppController = async (c) => {
kind: Kind.Text,
content: data.status ?? '',
tags,
created_at: Math.floor(new Date().getTime() / 1000),
created_at: nostrNow(),
}, c);
publish(event);
@ -115,7 +115,7 @@ const favouriteController: AppController = async (c) => {
['e', target.id],
['p', target.pubkey],
],
created_at: Math.floor(new Date().getTime() / 1000),
created_at: nostrNow(),
}, c);
publish(event);

View File

@ -6,7 +6,7 @@ import { Conf } from './config.ts';
import { getAuthor } from './client.ts';
import { verifyNip05Cached } from './nip05.ts';
import { getMediaLinks, type MediaLink, parseNoteContent } from './note.ts';
import { type Nip05, parseNip05 } from './utils.ts';
import { type Nip05, nostrDate, parseNip05 } from './utils.ts';
import { isCWTag } from 'https://gitlab.com/soapbox-pub/mostr/-/raw/c67064aee5ade5e01597c6d23e22e53c628ef0e2/src/nostr/tags.ts';
const DEFAULT_AVATAR = 'https://gleasonator.com/images/avi.png';
@ -39,7 +39,7 @@ async function toAccount(event: Event<0>, opts: ToAccountOpts = {}) {
avatar: picture || DEFAULT_AVATAR,
avatar_static: picture || DEFAULT_AVATAR,
bot: false,
created_at: event ? new Date(event.created_at * 1000).toISOString() : new Date().toISOString(),
created_at: event ? nostrDate(event.created_at).toISOString() : new Date().toISOString(),
discoverable: true,
display_name: name,
emojis: toEmojis(event),
@ -126,7 +126,7 @@ async function toStatus(event: Event<1>) {
account,
card,
content,
created_at: new Date(event.created_at * 1000).toISOString(),
created_at: nostrDate(event.created_at).toISOString(),
in_reply_to_id: replyTag ? replyTag[1] : null,
in_reply_to_account_id: null,
sensitive: !!cw,

View File

@ -6,6 +6,8 @@ import { lookupNip05Cached } from '@/nip05.ts';
/** Get the current time in Nostr format. */
const nostrNow = () => Math.floor(new Date().getTime() / 1000);
/** Convenience function to convert Nostr dates into native Date objects. */
const nostrDate = (seconds: number) => new Date(seconds * 1000);
/** Pass to sort() to sort events by date. */
const eventDateComparator = (a: Event, b: Event) => b.created_at - a.created_at;
@ -105,6 +107,7 @@ export {
eventDateComparator,
lookupAccount,
type Nip05,
nostrDate,
nostrNow,
type PaginationParams,
paginationSchema,