summaryrefslogtreecommitdiff
path: root/src/Events/RoomMessageEvent.php
blob: 3ccc408f241f592ee7904f24b7d94794284585ec (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

namespace App\Events;

use App\Database;
use App\Support\ConnectsToDatabase;
use App\Types\EventType;
use App\Types\MembershipState;
use App\Types\MessageType;

class RoomMessageEvent extends Event implements ConnectsToDatabase
{
  public function __construct(
    private string $id = "",
    private string $sender = "",
    private \DateTime $originServerTimestamp = new \DateTime("now"),
    private array $content = [],
    private array $unsigned = [],
    private string $roomId = "",
  )
  {
    parent::__construct(EventType::ROOM_MESSAGE);
  }

  public function fromJson(string $json): self
  {
  }

  public function toJsonEncodeable(): array
  {
    return [
      "type" => $this->type,
      "sender" => $this->sender,
      "event_id" => $this->id,
      "origin_server_ts" => $this->originServerTimestamp->format("U.v"),
      "content" => [
        "body" => $this->body,
        "msgtype" => MessageType::TEXT,
      ],
      "unsigned" => [
        "age" => 1234,
        "membership" => MembershipState::JOIN,
      ],
      "room_id" => $this->roomId,
    ];
  }

  public static function fromDatabase(array $row): self
  {
  }

  public static function fetch(): ?self
  {
  }

  public static function fetchAll(): array
  {
  }

  public function insert(): bool
  {
    return !! Database::getInstance()->query(
      <<<SQL
        insert into room_events (id, type, sender, origin_server_timestamp, content, unsigned, room_id)
        values (:id, :type, :sender, to_timestamp(:origin_server_timestamp), :content, :unsigned, :room_id)
      SQL,
      [
        "id" => $this->id,
        "type" => $this->type->value,
        "sender" => $this->sender,
        "origin_server_timestamp" => (new \DateTime("now"))->format("U.v"),
        "content" => json_encode($this->content),
        "unsigned" => json_encode($this->unsigned),
        "room_id" => $this->roomId,
      ]
    );
  }

  public function update(): bool
  {
  }

  public function delete(): bool
  {
  }
}