Skip to content

Commit

Permalink
Merge pull request #37 from Trustroots/nrcommon
Browse files Browse the repository at this point in the history
Nrcommon
  • Loading branch information
shuesken authored Sep 27, 2024
2 parents f717fd8 + ac4fe82 commit 8e36b23
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 341 deletions.
2 changes: 1 addition & 1 deletion nrapp/redux/eventsSlice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ID_SEPARATOR } from "@/constants";
import { Event } from "@/typesTEMPORARY";
import { Event } from "@/../nrcommon/mod";
import {
createEntityAdapter,
createSlice,
Expand Down
21 changes: 0 additions & 21 deletions nrapp/typesTEMPORARY.ts

This file was deleted.

8 changes: 8 additions & 0 deletions nrcommon/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@trustroots/nrcommon",
"version": "0.0.1",
"exports": "./mod.ts",
"imports": {
"zod": "npm:zod@^3.23.8"
}
}
2 changes: 2 additions & 0 deletions nrcommon/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { z } from "zod";
export { z };
73 changes: 73 additions & 0 deletions nrcommon/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { z } from "./deps.ts"

import { version as PACKAGE_VERSION } from "./deno.json" with { type: "json" };
export const CONTENT_MINIMUM_LENGTH = 3;
export const CONTENT_MAXIMUM_LENGTH = 300;

function isHex(s: string) {
return s.split("").every((c) => "0123456789abcdef".split("").includes(c));
}

function isPlusCode(code: string) {
const re =
/(^|\s)([23456789C][23456789CFGHJMPQRV][23456789CFGHJMPQRVWX]{6}\+[23456789CFGHJMPQRVWX]{2,7})(\s|$)/i;
return re.test(code);
}

export const eventSchema = z
.object({
id: z.string().length(32),
pubkey: z.string().length(32),
kind: z.number(),
created_at: z.number(),
tags: z.string().array().array(),
content: z.string(),
sig: z.string(),
})
.strict();

export type Event = z.infer<typeof eventSchema>;

function hasOpenLocationCode(tags: string[][]): boolean {
const namespaces = tags
.filter((tag) => tag[0] === "L")
.map((tag) => tag.slice(1))
.flat();
const hasOpenLocationCodeNamespace = namespaces.includes("open-location-code");
if (!hasOpenLocationCodeNamespace) return false;

const plusCodeTags = tags.filter(
(tag) => tag.length > 3 && tag[0] === "l" && tag[2] === "open-location-code"
);
if (plusCodeTags.length === 0) return false;

const plusCodes = plusCodeTags.map((plusCodeTag) => plusCodeTag[1]);
const validPlusCodes = plusCodes.every(isPlusCode);

if (!validPlusCodes) return false;

return true;
}

function hasVersion(tags: string[][]): boolean {
const versionTags = tags.filter((tag) => tag[0] === "kind30398_version");
if (versionTags.length !== 1) return false;
const versionTag = versionTags[0];
if (versionTag.length !== 2) return false;
const version = versionTag[1];
if (version !== PACKAGE_VERSION) return false
return true
}

export const kind30398EventSchema = eventSchema.extend({
kind: z.literal(30398),
tags: z
.string()
.array()
.array()
.refine(hasOpenLocationCode, { message: "no valid open-location-code label" })
.refine(hasVersion, { message: "no valid kind30398_version" }),
content: z.string().max(CONTENT_MAXIMUM_LENGTH, `content is above max length of ${CONTENT_MAXIMUM_LENGTH}`).min(CONTENT_MINIMUM_LENGTH, `content is below min length of ${CONTENT_MINIMUM_LENGTH}`)
});

export type Kind30398Event = z.infer<typeof kind30398EventSchema>;
193 changes: 0 additions & 193 deletions nrserver/repost.ts

This file was deleted.

Loading

0 comments on commit 8e36b23

Please sign in to comment.