Repository navigation
Expand file tree
/
Copy pathmessage.ts
More file actions
123 lines (108 loc) · 3.01 KB
/
Copy pathmessage.ts
File metadata and controls
123 lines (108 loc) · 3.01 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
JoinTable,
ManyToMany,
ManyToOne,
OneToMany,
OneToOne,
UpdateDateColumn,
} from "typeorm";
import { z } from "zod";
import { ActorMention } from "../util/activitypub/constants.js";
import type { AttributesOnly } from "../util/types.js";
import type { ApCache } from "./apcache.js";
import { type Attachment, PublicAttachment } from "./attachment.js";
import { BaseModel } from "./basemodel.js";
import type { Channel } from "./channel.js";
import { type Embed, PublicEmbed } from "./embed.js";
import type { User } from "./user.js";
@Entity("messages")
export class Message extends BaseModel {
/** The content of this message */
@Column({ nullable: true, type: String })
content: string | null;
/** The publish date of this message */
@CreateDateColumn()
published: Date;
/** The date this message was updated */
@UpdateDateColumn({ nullable: true, type: Date })
updated: Date | null;
/** The author of this message */
@ManyToOne("users", { onDelete: "CASCADE" })
author: User;
/** The channel this message is associated with */
@ManyToOne("channels", { onDelete: "CASCADE" })
channel: Channel;
/** the attached files of this message */
@OneToMany("attachments", (attachment: Attachment) => attachment.message, {
cascade: true,
orphanedRowAction: "delete",
})
files: Attachment[];
/** the attached embeds/url previews of this message */
@ManyToMany("embeds", { orphanedRowAction: "delete" })
@JoinTable()
embeds: Embed[];
/**
* The reference object this message was created from.
* Messages sent from here don't have this.
*/
@OneToOne("activitypub_objects", { nullable: true })
@JoinColumn()
reference_object: ApCache | null;
/**
* The client generated nonce of this messaged used to prevent duplicate message sending.
*/
@Column({ type: "uuid", nullable: true })
nonce: string | null;
public toPublic() {
return {
id: this.id,
content: this.content,
published: this.published,
updated: this.updated,
author_id: this.author.mention,
channel_id: this.channel.mention,
files: this.files ? this.files.map((x) => x.toPublic()) : [],
embeds: this.embeds ? this.embeds.map((x) => x.toPublic()) : [],
} as PublicMessage;
}
public toPrivate() {
return this.toPublic();
}
}
export type PublicMessage = Pick<
AttributesOnly<Message>,
"id" | "content" | "published" | "updated"
> & {
author_id: ActorMention;
channel_id: ActorMention;
embeds: PublicEmbed[];
};
export const PublicMessage: z.ZodType<PublicMessage> = z
.object({
id: z.string().uuid(),
content: z.string(),
published: z.date(),
updated: z.date(),
author_id: ActorMention,
channel_id: ActorMention,
files: z.array(PublicAttachment),
embeds: z.array(PublicEmbed),
})
.openapi("PublicMessage");
export const MessageCreateRequest = z.object({
content: z.string(),
files: z
.array(
z.object({
name: z.string(),
hash: z.string(),
}),
)
.optional(),
});
export type MessageCreateRequest = z.infer<typeof MessageCreateRequest>;