add community handshake states table and enum

This commit is contained in:
einhornimmond 2025-10-13 07:40:38 +02:00
parent 905388f1a0
commit d6d626c848
5 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,19 @@
export async function upgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
await queryFn(`
CREATE TABLE community_handshake_states (
id int unsigned NOT NULL AUTO_INCREMENT,
handshake_id int unsigned NOT NULL,
one_time_code int unsigned NOT NULL,
public_key binary(32) NOT NULL,
status varchar(255) NOT NULL DEFAULT 'OPEN_CONNECTION',
last_error text,
created_at datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id),
KEY idx_public_key (public_key),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`)
}
export async function downgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
await queryFn(`DROP TABLE community_handshake_states;`)
}

View File

@ -0,0 +1,39 @@
import { CommunityHandshakeStateType } from '../enum'
import { BaseEntity, Column, CreateDateColumn, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm'
import { FederatedCommunity } from './FederatedCommunity'
@Entity('community_handshake_states')
export class CommunityHandshakeState extends BaseEntity {
@PrimaryGeneratedColumn({ unsigned: true })
id: number
@Column({ name: 'handshake_id', type: 'int', unsigned: true })
handshakeId: number
@Column({ name: 'one_time_code', type: 'int', unsigned: true })
oneTimeCode: number
@Column({ name: 'public_key', type: 'binary', length: 32 })
publicKey: Buffer
@Column({
type: 'varchar',
length: 255,
default: CommunityHandshakeStateType.OPEN_CONNECTION,
nullable: false,
})
status: CommunityHandshakeStateType
@Column({ name: 'last_error', type: 'text', nullable: true })
lastError?: string
@CreateDateColumn({ name: 'created_at', type: 'datetime', precision: 3 })
createdAt: Date
@UpdateDateColumn({ name: 'updated_at', type: 'datetime', precision: 3 })
updatedAt: Date
@ManyToOne(() => FederatedCommunity, (federatedCommunity) => federatedCommunity.communityHandshakeStates)
@JoinColumn({ name: 'public_key', referencedColumnName: 'publicKey' })
federatedCommunity: FederatedCommunity
}

View File

@ -5,10 +5,12 @@ import {
Entity,
JoinColumn,
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm'
import { Community } from './Community'
import { CommunityHandshakeState } from './CommunityHandshakeState'
@Entity('federated_communities')
export class FederatedCommunity extends BaseEntity {
@ -60,4 +62,8 @@ export class FederatedCommunity extends BaseEntity {
)
@JoinColumn({ name: 'public_key', referencedColumnName: 'publicKey' })
community?: Community
@OneToMany(() => CommunityHandshakeState, (communityHandshakeState) => communityHandshakeState.federatedCommunity)
@JoinColumn({ name: 'public_key', referencedColumnName: 'publicKey' })
communityHandshakeStates: CommunityHandshakeState[]
}

View File

@ -0,0 +1,8 @@
export enum CommunityHandshakeStateType {
OPEN_CONNECTION = 'OPEN_CONNECTION',
OPEN_CONNECTION_CALLBACK = 'OPEN_CONNECTION_CALLBACK',
SUCCESS = 'SUCCESS',
FAILED = 'FAILED',
EXPIRED = 'EXPIRED'
}

View File

@ -0,0 +1 @@
export * from './CommunityHandshakeStateType'