1 import { Repository } from "./base";
2 import { v4 as uuid } from 'uuid';
4 export type Message = {
15 export type MessageWithNames = {
19 export class MailRepository extends Repository<Message> {
24 async createSystemMessage(from: string, to: string, subject: string, message: string) {
25 const msg: Message = {
36 await this.Insert(msg);
40 async markAsRead(id: string, to: string) {
41 return this.Save({read_at: Date.now()}, {
47 async getMessage(id: string, to: string): Promise<MessageWithNames> {
48 const res = await this.db.raw<MessageWithNames[]>(`select m.*, a.username
50 join accounts a on a.id = m.from_account
51 where m.to_account = ?
59 async countUnread(to: string): Promise<number> {
60 const res = await this.db.raw<{unread: number}>(`select count(id) as
62 where to_account = ? and read_at = 0`, to);
65 return parseInt(res[0].unread.toString()) || 0;
72 async listReceivedMessages(to: string): Promise<MessageWithNames[]> {
73 return this.db.raw<MessageWithNames[]>(`select m.*, a.username
75 join accounts a on a.id = m.from_account
76 where m.to_account = ?
77 order by sent_at desc`, to);