diff --git a/src/rvm/index.ts b/src/rvm/index.ts index 9924d49..e762462 100644 --- a/src/rvm/index.ts +++ b/src/rvm/index.ts @@ -197,6 +197,7 @@ export class River { await this.db.insert(dbSchema.channelTable).values({ id: channelId, createdBy: message.messageData.rid, + createdAt: message.messageData.timestamp, uri: message.messageData.body.uri, }).onConflictDoUpdate({ target: dbSchema.channelTable.id, set: { id: channelId, uri: message.messageData.body.uri } }); return channelId; @@ -219,6 +220,7 @@ export class River { await this.db.insert(dbSchema.itemTable).values({ id: itemId, createdBy: message.messageData.rid, + createdAt: message.messageData.timestamp, uri: message.messageData.body.uri, }).onConflictDoUpdate({ target: dbSchema.itemTable.id, set: { id: itemId, uri: message.messageData.body.uri } }); // create the uri row, that will then get updated by lambda @@ -272,6 +274,7 @@ export class River { await this.db.insert(dbSchema.submissionsTable).values({ id: submissionId, createdBy: message.messageData.rid, + createdAt: message.messageData.timestamp, itemId: itemId, channelId: channelId, status: isOwner ? 3 : 0, // channel owenrs/mods get their submissions automatically set to 2 (0 = pending, 1 = declined, 2 = accepted, 3 = owner/mod) @@ -355,6 +358,8 @@ export class River { // add this table await this.db.insert(dbSchema.responseInfo).values({ id: responseId, + createdBy: message.messageData.rid, + createdAt: message.messageData.timestamp, targetMessageId: messageId, response: response, }); diff --git a/src/rvm/schema.ts b/src/rvm/schema.ts index cdd71be..2ab8b89 100644 --- a/src/rvm/schema.ts +++ b/src/rvm/schema.ts @@ -2,156 +2,160 @@ import { integer, pgTable, text, - timestamp, - numeric, bigint, boolean, - foreignKey -} from 'drizzle-orm/pg-core' +} from "drizzle-orm/pg-core"; import { relations } from "drizzle-orm"; /* -* **************** -* USERS -* **************** -*/ - -// export const users = pgTable('users', { -// id: text('userid').primaryKey(), -// to: text('to'), -// recovery: text('recovery'), -// timestamp: timestamp('timestamp'), -// log_addr: text('log_addr'), -// block_num: numeric('block_num'), -// }) + * **************** + * AUTH (PRIVATE) + * **************** + */ + +export const authTable = pgTable("auth", { + id: text("id").primaryKey(), // email and/or hash of user email + magicLinkStatus: integer("magiclink_status"), + passkeyEncodedPubKey: text("passkey_encoded_pubkey"), + deterministicSmartAccount: text("deterministic_smart_account"), + rid: bigint("rid", { mode: "bigint" }), + encryptedSigningKey: text("encrypted_signing_key"), +}); + +export const authRelations = relations(authTable, ({ one }) => ({ + userInfo: one(userTable, { + fields: [authTable.rid], + references: [userTable.id], + }), +})); /* -* **************** -* KEYS -* **************** -*/ - -// export const keyTable = pgTable( -// 'keys', -// { -// userid: numeric('userid') -// .notNull() -// .references(() => users.id), -// custodyAddress: text('custodyAddress').notNull(), -// deviceid: text('deviceid').notNull(), -// publickey: text('publickey').notNull(), // toHex(uint8array pub key data) -// encryptedprivatekey: text('encryptedprivatekey').notNull(), // toHex(encrypted uint8array priv key data) -// }, -// (table) => ({ -// primaryKey: [table.userid, table.custodyAddress, table.deviceid], -// }), -// ) + * **************** + * USERS + * **************** + */ + +export const userTable = pgTable("users", { + id: bigint("id", { mode: "bigint" }), + custodyAddress: text("custody_address"), + recoveryAddress: text("recovery_address"), +}); + +export const userRelations = relations(userTable, ({ one, many }) => ({ + keyInfo: many(keyTable) +})); /* -* **************** -* MESSAGES -* **************** -*/ - -// private async _storeValidMessage( -// messageId: string, -// message: Message -// ): Promise { -// await this.db.insert(dbSchema.messageTable).values({ -// id: messageId, -// rid: message.messageData.rid, -// timestamp: message.messageData.timestamp, -// type: message.messageData.type, -// body: jsonStringifyBigIntSafe(message.messageData.body), -// signer: toHex(message.signer), -// hashType: message.hashType, -// hash: toHex(message.hash), -// sigType: message.sigType, -// sig: toHex(message.sig), -// }); - -export const messageTable = pgTable('messages', { - id: text('id').primaryKey(), - rid: bigint('rid', { mode: 'bigint' }), - timestamp: bigint('timestamp', { mode: 'bigint' }), - type: integer('type'), - body: text('body'), // this will be JSON.stringified message body object - signer: text('signer'), - hashType: integer('hashtype'), - hash: text('hash'), - sigType: integer('sigtype'), - sig: text('sig') + * **************** + * KEYS + * **************** + */ + +export const keyTable = pgTable("key_info", { + id: bigint("id", { mode: "bigint" }), + rid: bigint("rid", { mode: "bigint" }), + publickKeyBytes: text("public_key_bytes"), + status: integer("status") // 0 = null, 1 = added, 2 = removed }); +export const keyRelations = relations(keyTable, ({ one }) => ({ + userInfo: one(userTable, { + fields: [keyTable.rid], + references: [userTable.id], + }), +})); + + /* -* **************** -* URI -* **************** -*/ + * **************** + * MESSAGES + * **************** + */ + +export const messageTable = pgTable("messages", { + id: text("id").primaryKey(), + rid: bigint("rid", { mode: "bigint" }), + timestamp: bigint("timestamp", { mode: "bigint" }), + type: integer("type"), + body: text("body"), // this will be JSON.stringified message body object + signer: text("signer"), + hashType: integer("hashtype"), + hash: text("hash"), + sigType: integer("sigtype"), + sig: text("sig"), +}); + +/* + * **************** + * URI + * **************** + */ export const uriInfo = pgTable("uri_info", { id: text("id").primaryKey(), name: text("name"), description: text("description"), imageUri: text("imageuri"), - animationUri: text("animationuri") + animationUri: text("animationuri"), }); /* -* **************** -* CHANNELS -* **************** -*/ + * **************** + * CHANNELS + * **************** + */ -export const channelTable = pgTable('channels', { - id: text('id').primaryKey(), - createdBy: bigint("createdby", {mode: "bigint"}), - uri: text('uri') -}) +export const channelTable = pgTable("channels", { + id: text("id").primaryKey(), + createdBy: bigint("createdby", { mode: "bigint" }), + createdAt: bigint("createdAt", { mode: "bigint" }), + uri: text("uri"), +}); export const channelRelations = relations(channelTable, ({ one, many }) => ({ uriInfo: one(uriInfo, { fields: [channelTable.uri], references: [uriInfo.id], }), - submissions: many(submissionsTable) + submissions: many(submissionsTable), })); - /* -* **************** -* ITEMS -* **************** -*/ + * **************** + * ITEMS + * **************** + */ -export const itemTable = pgTable('items', { - id: text('id').primaryKey(), - createdBy: bigint("createdby", {mode: "bigint"}), - uri: text('uri') -}) +export const itemTable = pgTable("items", { + id: text("id").primaryKey(), + createdBy: bigint("createdby", { mode: "bigint" }), + createdAt: bigint("createdAt", { mode: "bigint" }), + uri: text("uri"), +}); export const itemRelations = relations(itemTable, ({ one, many }) => ({ uriInfo: one(uriInfo, { fields: [itemTable.uri], references: [uriInfo.id], }), - submissionTable: many(submissionsTable) + submissionTable: many(submissionsTable), })); /* -* **************** -* SUBMISSIONS -* **************** -*/ - -export const submissionsTable = pgTable('submissions', { - id: text('id').primaryKey(), - createdBy: bigint("createdby", {mode: "bigint"}), + * **************** + * SUBMISSIONS + * **************** + */ + +export const submissionsTable = pgTable("submissions", { + id: text("id").primaryKey(), + createdBy: bigint("createdby", { mode: "bigint" }), + createdAt: bigint("createdAt", { mode: "bigint" }), status: integer("status"), // 0 = pending | 1 = rejected | 2 = accepted | 3 = owner submission - itemId: text('itemid').notNull(), - channelId: text('channelid').notNull(), - response: text('response') -}) + itemId: text("itemid").notNull(), + channelId: text("channelid").notNull(), + response: text("response"), +}); export const submissionsRelations = relations(submissionsTable, ({ one }) => ({ itemId: one(itemTable, { @@ -165,20 +169,24 @@ export const submissionsRelations = relations(submissionsTable, ({ one }) => ({ responseInfo: one(responseInfo, { fields: [submissionsTable.response], references: [responseInfo.targetMessageId], - }), + }), })); /* -* **************** -* RESPONSES -* **************** -*/ + * **************** + * RESPONSES + * **************** + */ // TODO: unsolved not able to render all of responesInfo when querying submissionTable -// reserach more into foreign keys +// reserach more into foreign keys export const responseInfo = pgTable("response_info", { id: text("id").primaryKey(), - targetMessageId: text("targetmessageid").references(() => submissionsTable.id), - response: boolean("response") + createdBy: bigint("createdby", { mode: "bigint" }), + createdAt: bigint("createdAt", { mode: "bigint" }), + targetMessageId: text("targetmessageid").references( + () => submissionsTable.id + ), + response: boolean("response"), });