summaryrefslogtreecommitdiff
path: root/src/models/entities/poll.ts
blob: 204f102f5103f14b80a284f0d4290f42f476100a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { PrimaryColumn, Entity, Index, JoinColumn, Column, OneToOne } from 'typeorm';
import { id } from '../id';
import { Note } from './note';
import { User } from './user';

@Entity()
export class Poll {
	@PrimaryColumn(id())
	public id: string;

	@Index({ unique: true })
	@Column(id())
	public noteId: Note['id'];

	@OneToOne(type => Note, {
		onDelete: 'CASCADE'
	})
	@JoinColumn()
	public note: Note | null;

	@Column('timestamp with time zone', {
		nullable: true
	})
	public expiresAt: Date | null;

	@Column('boolean')
	public multiple: boolean;

	@Column('varchar', {
		length: 128, array: true, default: '{}'
	})
	public choices: string[];

	@Column('integer', {
		array: true,
	})
	public votes: number[];

	//#region Denormalized fields
	@Column('enum', {
		enum: ['public', 'home', 'followers', 'specified'],
		comment: '[Denormalized]'
	})
	public noteVisibility: 'public' | 'home' | 'followers' | 'specified';

	@Index()
	@Column({
		...id(),
		comment: '[Denormalized]'
	})
	public userId: User['id'];

	@Index()
	@Column('varchar', {
		length: 128, nullable: true,
		comment: '[Denormalized]'
	})
	public userHost: string | null;
	//#endregion
}

export type IPoll = {
	choices: string[];
	votes?: number[];
	multiple: boolean;
	expiresAt: Date;
};