summaryrefslogtreecommitdiff
path: root/lib/emote.php
blob: 14f60884ba886ba773bc010da3ddd21e6eb38918 (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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/user.php";

class Emote
{
    public string $id;
    public string $code;
    public string $ext;
    public mixed $uploaded_by;
    public int $created_at;
    public mixed $rating;
    public bool $is_in_user_set;
    public int $visibility;

    public string|null $source;

    public array $tags;

    public static function from_array(array $arr): Emote
    {
        $e = new Emote();

        $e->id = $arr["id"];
        $e->code = $arr["code"];
        $e->ext = $arr["ext"] ?? "webp";
        $e->uploaded_by = $arr["uploaded_by"];
        $e->created_at = strtotime($arr["created_at"] ?? 0);
        $e->is_in_user_set = $arr["is_in_user_set"] ?? false;
        $e->visibility = $arr["visibility"];
        $e->source = $arr["source"] ?? null;
        $e->tags = $arr["tags"] ?? [];

        if (isset($arr["total_rating"], $arr["average_rating"])) {
            $e->rating = [
                "total" => $arr["total_rating"],
                "average" => $arr["average_rating"]
            ];
        } else {
            $e->rating = $arr["rating"] ?? null;
        }

        return $e;
    }

    public static function from_array_with_user(array $arr, PDO &$db): Emote
    {
        if ($arr["uploaded_by"]) {
            $arr["uploaded_by"] = User::get_user_by_id($db, $arr["uploaded_by"]);
        }

        return Emote::from_array($arr);
    }

    function get_id()
    {
        return $this->id;
    }

    function get_code()
    {
        return $this->code;
    }

    function get_ext()
    {
        return $this->ext;
    }

    function get_created_at()
    {
        return $this->created_at;
    }

    function get_uploaded_by()
    {
        return $this->uploaded_by;
    }

    function is_added_by_user()
    {
        return $this->is_in_user_set;
    }

    function get_rating()
    {
        return $this->rating;
    }

    function get_visibility()
    {
        return $this->visibility;
    }

    function get_source()
    {
        return $this->source;
    }

    function get_tags(): array
    {
        return $this->tags;
    }
}

class Emoteset
{
    public string $id;
    public string $name;
    public User|null $owner;
    public array $emotes, $editors;

    public bool $is_default;

    public static function from_array(array $arr): Emoteset
    {
        $s = new Emoteset();

        $s->id = $arr["id"];
        $s->name = $arr["name"];
        $s->owner = $arr["owner_id"];
        $s->emotes = $arr["emotes"] ?? [];
        $s->editors = $arr["editors"] ?? [];
        $s->is_default = $arr["is_default"] ?? false;

        return $s;
    }

    public static function from_array_extended(array $arr, string $user_id, PDO &$db): Emoteset
    {
        if ($arr["owner_id"]) {
            $arr["owner_id"] = User::get_user_by_id($db, $arr["owner_id"]);
        }

        $arr["emotes"] = fetch_all_emotes_from_emoteset($db, $arr["id"], $user_id);

        $stmt = $db->prepare('SELECT u.id, u.username FROM users u
            INNER JOIN emote_sets es ON es.id = ?
            INNER JOIN acquired_emote_sets aes ON aes.emote_set_id = es.id
            WHERE aes.user_id = u.id
        ');
        $stmt->execute([$arr["id"]]);
        $arr["editors"] = $stmt->fetchAll(PDO::FETCH_ASSOC);

        return Emoteset::from_array($arr);
    }

    public static function get_all_user_emotesets(PDO &$db, string $user_id): array
    {
        $stmt = $db->prepare("SELECT es.*, aes.is_default FROM emote_sets es
            INNER JOIN acquired_emote_sets aes ON aes.emote_set_id = es.id
            WHERE aes.user_id = ?
        ");
        $stmt->execute([$user_id]);

        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $emote_sets = [];

        foreach ($rows as $row) {
            array_push($emote_sets, Emoteset::from_array_extended($row, $user_id, $db));
        }

        return $emote_sets;
    }
}

function fetch_all_emotes_from_emoteset(PDO &$db, string $emote_set_id, string $user_id, int|null $limit = null): array
{
    // fetching emotes
    $sql = "SELECT 
        e.id, e.created_at, e.visibility,
        CASE 
            WHEN esc.code IS NOT NULL THEN esc.code 
            ELSE e.code
        END AS code,
        CASE 
            WHEN esc.code IS NOT NULL THEN e.code 
            ELSE NULL 
        END AS original_code,
        CASE WHEN up.private_profile = FALSE OR up.id = ? THEN e.uploaded_by ELSE NULL END AS uploaded_by
        FROM emotes e
        LEFT JOIN user_preferences up ON up.id = e.uploaded_by
        INNER JOIN emote_set_contents AS esc
        ON esc.emote_set_id = ?
        WHERE esc.emote_id = e.id";

    if ($limit) {
        $sql .= " LIMIT $limit";
    }

    $stmt = $db->prepare($sql);
    $stmt->execute([$user_id, $emote_set_id]);

    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $emotes = [];

    // fetching uploaders
    foreach ($rows as $row) {
        if ($row["uploaded_by"]) {
            $row["uploaded_by"] = User::get_user_by_id($db, $row["uploaded_by"]);
        }

        array_push($emotes, Emote::from_array($row));
    }

    return $emotes;
}

function html_random_emote(PDO &$db)
{
    $stmt = $db->prepare("SELECT id, code FROM emotes WHERE visibility = 1 ORDER BY RAND() LIMIT 1");
    $stmt->execute();

    if ($row = $stmt->fetch()) {
        echo ''
            ?>
        <section class="box" id="box-random-emote">
            <div class="box navtab">
                <p>Random emote</p>
            </div>
            <div class="box content center">
                <a href="/emotes?id=<?= $row["id"] ?>">
                    <img src="/static/userdata/emotes/<?= $row["id"] ?>/3x.webp" alt="<?= $row["code"] ?>" width="192">
                </a>
            </div>
        </section>
        <?php
        ;
    }
}

function html_featured_emote(PDO &$db)
{
    $stmt = $db->prepare("SELECT e.id, e.code FROM emotes e
    INNER JOIN emote_sets es ON es.is_featured = TRUE
    INNER JOIN emote_set_contents esc ON es.id = esc.emote_set_id
    WHERE e.visibility = 1 AND e.id = esc.emote_id ORDER BY esc.added_at DESC LIMIT 1");
    $stmt->execute();

    if ($row = $stmt->fetch()) {
        echo ''
            ?>
        <section class="box" id="box-featured-emote">
            <div class="box navtab">
                <p>Featured emote</p>
            </div>
            <div class="box content center">
                <a href="/emotes?id=<?= $row["id"] ?>">
                    <img src="/static/userdata/emotes/<?= $row["id"] ?>/3x.webp" alt="<?= $row["code"] ?>" width="192">
                </a>
            </div>
        </section>
        <?php
        ;
    }
}

function html_display_emotes(array $emotes, int $scale = 3)
{
    $emote_wall = intval($_COOKIE["emotelist_wall"] ?? "0") == 1;

    foreach ($emotes as $e) {
        echo "<a class='box emote column justify-center items-center' href='/emotes?id={$e->id}'>";

        if ($e->is_added_by_user()) {
            echo '<img src="/static/img/icons/yes.png" class="emote-check" />';
        }

        // icon
        echo '<div class="flex justify-center items-center grow emote-icon">';
        $scale = $emote_wall ? "3" : ((string) $scale);
        echo "<img src='/static/userdata/emotes/{$e->id}/{$scale}x.webp' alt='{$e->code}' />";
        echo '</div>';

        // info
        echo '<div class="flex column justify-bottom items-center emote-desc';
        if ($emote_wall) {
            echo ' none';
        }
        echo '">';

        echo "<h1 title='{$e->code}'>{$e->code}</h1>";
        if ($e->get_uploaded_by()) {
            echo "<p>{$e->uploaded_by->username}</p>";
        }

        echo '</div></a>';
    }
}

function html_display_emoteset(array $emotesets)
{
    foreach ($emotesets as $es) {
        echo "<a href='/emotesets/?id={$es->id}' class='box column small-gap'>";

        echo '<div>';
        echo "<p>$es->name</p>";
        echo '</div>';

        echo '<div class="small-gap row">';

        foreach ($es->emotes as $e) {
            echo "<img src='/static/userdata/emotes/{$e->id}/1x.webp' alt='{$e->code}' title='{$e->code}' height='16' />";
        }

        echo '</div></a>';

    }
}

function html_emotelist_mode()
{
    echo '' ?>
    <div class="row small-gap" id="control-emotelist" style="display: none;">
        <a href="#" onclick="set_emotelist_mode('wall')">
            <img src="/static/img/icons/emotes/emote.png" alt="Emote Wall" title="Emote Wall">
        </a>
        <a href="#" onclick="set_emotelist_mode('table')">
            <img src="/static/img/icons/table.png" alt="Emote Descriptions" title="Emote Descriptions">
        </a>
    </div>
    <script>
        document.getElementById("control-emotelist").style.display = "flex";

        function set_emotelist_mode(mode) {
            const elements = document.querySelectorAll(".emote .emote-desc");

            for (const element of elements) {
                if (mode == "wall") {
                    element.classList.add("none");
                    document.cookie = "emotelist_wall = 1; expires=Tue, 19 Jan 2038 00:00:00 UTC; path=/";
                } else {
                    element.classList.remove("none");
                    document.cookie = "emotelist_wall = 0; expires=Tue, 19 Jan 2038 00:00:00 UTC; path=/";
                }
            }
        }
    </script>
    <?php ;
}