File size: 1,770 Bytes
d988ae4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Injectable, Logger, NotFoundException } from '@nestjs/common';
import { ClipboardService } from '../clipboard/clipboard.service';

@Injectable()
export class AdminService {
  private readonly logger = new Logger(AdminService.name);

  constructor(private readonly clipboardService: ClipboardService) { }

  async listClipboards() {
    return this.clipboardService.listClipboards();
  }

  async getClipboard(roomCode: string) {
    const clipboard = await this.clipboardService.getClipboard(roomCode);
    const ttl = await this.clipboardService.getClipboardTTL(roomCode);

    if (!clipboard) {
      throw new NotFoundException(`Clipboard ${roomCode} not found`);
    }

    return { ...clipboard, ttl };
  }

  async setPassword(roomCode: string, password?: string | null) {
    const clipboard = await this.clipboardService.updateClipboardPassword(roomCode, password);
    if (!clipboard) {
      throw new NotFoundException(`Clipboard ${roomCode} not found`);
    }

    this.logger.log(`Updated password for clipboard ${roomCode}`);
    return clipboard;
  }

  async setTTL(roomCode: string, ttlSeconds: number | null) {
    const updated = await this.clipboardService.setClipboardExpiration(roomCode, ttlSeconds);
    if (!updated) {
      throw new NotFoundException(`Clipboard ${roomCode} not found`);
    }

    this.logger.log(`Updated TTL for clipboard ${roomCode} -> ${ttlSeconds ?? 'none'}`);
    return { success: true, ttl: ttlSeconds };
  }

  async deleteClipboard(roomCode: string) {
    const success = await this.clipboardService.deleteClipboard(roomCode);
    if (!success) {
      throw new NotFoundException(`Clipboard ${roomCode} not found`);
    }

    this.logger.log(`Deleted clipboard ${roomCode}`);
    return { success };
  }
}