Skip to content

Commit

Permalink
jsr part 2
Browse files Browse the repository at this point in the history
Signed-off-by: Jersey <[email protected]>
  • Loading branch information
williamhorning committed Mar 14, 2024
1 parent 20bcf52 commit edd5945
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 19 deletions.
9 changes: 6 additions & 3 deletions packages/bolt/bolt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ import {

export class Bolt extends EventEmitter<plugin_events> {
bridge: bolt_bridges;
cmds = new bolt_commands();
cmds: bolt_commands = new bolt_commands();
config: config;
db: {
mongo: MongoClient;
redis: Awaited<ReturnType<typeof connect>>;
};
plugins = new Map<string, bolt_plugin<unknown>>();
plugins: Map<string, bolt_plugin<unknown>> = new Map<
string,
bolt_plugin<unknown>
>();

static async setup(cfg: Partial<config>) {
static async setup(cfg: Partial<config>): Promise<Bolt> {
const config = define_config(cfg);

Deno.env.set('BOLT_ERROR_HOOK', config.errorURL || '');
Expand Down
26 changes: 14 additions & 12 deletions packages/bolt/bridges/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,27 @@ export class bolt_bridges {
this.bolt.cmds.set('bridge', bridge_commands(bolt));
}

async get_bridge_message(id: string) {
async get_bridge_message(id: string): Promise<bridge_platform[] | null> {
const redis_data = await this.bolt.db.redis.get(`bolt-bridge-${id}`);
if (redis_data === null) return [] as bridge_platform[];
return JSON.parse(redis_data) as bridge_platform[];
}

is_bridged(msg: deleted_message<unknown>) {
is_bridged(msg: deleted_message<unknown>): boolean {
const platform = this.bolt.plugins.get(msg.platform.name);
if (!platform) return false;
const platsays = platform.is_bridged(msg);
if (platsays !== 'query') return platsays;
return Boolean(this.bridged_message_id_map.get(msg.id));
}

async get_bridge({ _id, channel }: { _id?: string; channel?: string }) {
async get_bridge({
_id,
channel
}: {
_id?: string;
channel?: string;
}): Promise<bridge_document | undefined> {
const query = {} as Record<string, string>;

if (_id) {
Expand All @@ -64,14 +70,10 @@ export class bolt_bridges {
return (await this.bridge_collection.findOne(query)) || undefined;
}

async update_bridge(bridge: bridge_document) {
return await this.bridge_collection.replaceOne(
{ _id: bridge._id },
bridge,
{
upsert: true
}
);
async update_bridge(bridge: bridge_document): Promise<void> {
await this.bridge_collection.replaceOne({ _id: bridge._id }, bridge, {
upsert: true
});
}

private async handle_message(
Expand Down Expand Up @@ -161,7 +163,7 @@ export class bolt_bridges {
let replytoid;
if ('replytoid' in msg && msg.replytoid) {
try {
replytoid = (await this.get_bridge_message(msg.replytoid)).find(
replytoid = (await this.get_bridge_message(msg.replytoid))?.find(
i => i.channel === platform.channel && i.plugin === platform.plugin
)?.id;
} catch {
Expand Down
14 changes: 14 additions & 0 deletions packages/bolt/deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"exports": "./mod.ts",
// TODO: move deps over here
"imports": {

},
// TODO: DO NOT PUBLISH UNDER THE BOLT NAME
"name": "@jersey/bolt",
"unstable": ["temporal"],
"test": {
"include": ["./_tests.ts"]
},
"version": "0.5.9"
}
4 changes: 2 additions & 2 deletions packages/bolt/migrations/fourtofourbeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {
to_db: 'bridgev1',
translate: (
items: (Document | { _id: string; value: string | unknown })[]
) => {
): Document[] => {
const obj = {} as {
[key: string]: {
platform: string;
Expand Down Expand Up @@ -48,6 +48,6 @@ export default {
});
}

return documents as Document[];
return documents;
}
};
9 changes: 7 additions & 2 deletions packages/bolt/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { nanoid } from './_deps.ts';
import { create_message } from './messages.ts';
import { create_message, message } from './messages.ts';

function get_replacer() {
const seen = new WeakSet();
Expand All @@ -21,7 +21,12 @@ export async function log_error(
e: Error,
extra: Record<string, unknown> = {},
_id: () => string = nanoid
) {
): Promise<{
e: Error;
extra: Record<string, unknown>;
uuid: string;
message: message<unknown> & { uuid?: string };
}> {
const uuid = _id();

const error_hook = Deno.env.get('BOLT_ERROR_HOOK');
Expand Down

0 comments on commit edd5945

Please sign in to comment.