aboutsummaryrefslogtreecommitdiff
path: root/src/Post.js
blob: 54ab601370b005e2a85e7b24a23aa2aa4891c2f7 (plain) (blame)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import React, { Component } from "react";
import {
  Image,
  Icon,
  Modal,
  Comment,
  Flag,
  Embed,
  Form,
  Button,
  Confirm
} from "semantic-ui-react";
import Moment from "react-moment";
import "moment/locale/es";
import { avatars } from "./Quotes";
import ReplyForm from "./ReplyForm";

const ImageModal = ({ href, trigger }) => (
  <Modal trigger={trigger} basic size="fullscreen">
    <Modal.Content image>
      <Modal.Description style={{ textAlign: "center" }}>
        <Image as="a" src={href} />
      </Modal.Description>
    </Modal.Content>
  </Modal>
);

const ReportModal = ({ trigger, threadId, postId, dir }) => (
  <Modal trigger={trigger} size="tiny">
    <Modal.Header>Reportar</Modal.Header>
    <Modal.Content>
      <Modal.Description>
        <p>
          Para pedir que el post <b>#{postId}</b> sea eliminado, indica una
          razón y presiona el botón Reportar.
        </p>
        <p>
          Normalmente eliminamos los mensajes que son considerados spam o flood.
          <br />
          Si deseas pedir la prohibición de acceso a algún usuario persistente,
          te recomendamos hacerlo en la sección /bai/.
        </p>
        <Form>
          <Form.Field>
            <Form.Input label="Razón" />
          </Form.Field>
        </Form>
      </Modal.Description>
    </Modal.Content>
    <Modal.Actions>
      <Button color="red">Reportar</Button>
    </Modal.Actions>
  </Modal>
);

const QuickReplyModal = ({ trigger, currentBoard, id, locked, replyIndex }) => (
  <Modal trigger={trigger} size="tiny">
    <Modal.Header>Respuesta rápida</Modal.Header>.
    <Modal.Content className="replyModal">
      <ReplyForm
        currentBoard={currentBoard}
        parent={id}
        locked={locked}
        nightMode={false}
        quickReply
        replyIndex={replyIndex}
      />
    </Modal.Content>
  </Modal>
);

class Post extends Component {
  constructor(props) {
    super(props);
    this.state = {
      deleteDialog: false
    };
    this.showDialog = this.showDialog.bind(this);
    this.handleCancel = this.handleCancel.bind(this);
    this.handleConfirm = this.handleConfirm.bind(this);
  }

  showDialog() {
    this.setState({ deleteDialog: true });
  }

  handleCancel() {
    this.setState({ deleteDialog: false });
  }

  handleConfirm() {
    this.setState({ deleteDialog: false });
    const { post, currentBoard } = this.props;
    let password = JSON.parse(localStorage.getItem("settings")).postPassword;
    fetch(
      `https://bienvenidoainternet.org/cgi/api/delete?dir=${currentBoard.dir}&id=${post.id}&password=${password}`
    )
      .then(response => {
        return response.json();
      })
      .then(resource => {
        // TODO: Reportar resultado
      });
  }

  render() {
    const {
      index,
      post,
      locked,
      threadId,
      currentBoard,
      nightMode,
      totalReplies
    } = this.props;
    const filesize = require("filesize");

    // Manejo de posts eliminados
    if (post.IS_DELETED > 0) {
      return (
        <Comment inverted={nightMode}>
          <Comment.Avatar
            src={`https://bienvenidoainternet.org/static/css/img/picnicbdy.gif`}
          />
          <Comment.Content>
            <Comment.Author as="a">
              #{currentBoard.board_type === 0 ? post.id : index + 1}
            </Comment.Author>
            <Comment.Metadata>
              <div>
                <Moment fromNow unix locale="es" date={post.timestamp} />
              </div>
            </Comment.Metadata>
            <Comment.Text>
              <span className="deleted">
                Eliminado por el {post.IS_DELETED === 1 ? "usuario." : "Staff."}
              </span>
            </Comment.Text>
          </Comment.Content>
        </Comment>
      );
    }

    // Obtener un avatar aleatorio basado en ID
    let userID = post.timestamp_formatted.split(" ID:")[1];
    if (userID === undefined) {
      userID = "";
    }

    const seedrandom = require("seedrandom");
    const rng = seedrandom(threadId + index);
    const idRng = seedrandom(userID);
    let idColor =
      "rgb(" +
      Math.round(idRng() * 255) +
      ", " +
      Math.round(idRng() * 200) +
      ", " +
      Math.round(idRng() * 200) +
      ")";

    let rndAvatar, hue;
    if (userID !== "") {
      let i = Math.round(idRng() * (avatars.length - 1));
      rndAvatar = avatars[i];
      hue = Math.round(idRng() * 360);
    } else {
      let i = Math.round(rng() * (avatars.length - 1));
      rndAvatar = avatars[i];
      hue = Math.round(rng() * 360);
    }

    // Obetener bandera del pais (para /world)
    let flag;
    if (currentBoard.dir === "world") {
      flag = post.name.match("[A-Z][A-Z]");
      if (flag !== null) {
        flag = flag[0].toLowerCase();
      } else {
        flag = "kp"; // heh
      }
    }

    // Fix: imagenes en dominio incorrecto
    post.message = post.message.replace(
      '<img src="/',
      '<img src="https://bienvenidoainternet.org/'
    );

    // Fix para reach-router
    if (currentBoard.board_type === 0) {
      post.message = post.message.replace("/res/", "/read/");
      post.message = post.message.replace(".html#", "/");
    }

    if (post.tripcode === " (★ ****-****)") {
      post.tripcode = "";
    }

    // Obtener lista de videos de youtube incrustados
    const youtubeRe = RegExp(
      /(?:https?:\/\/)?(?:www\.)?youtu(.be\/|be\.com\/watch\?v=)(\w{11})/g
    );
    const youtubeVideos = post.message.match(youtubeRe);

    // El post es nuestro?
    const ownPosts = JSON.parse(localStorage.getItem("ownPosts"));
    let isMine = false;
    if (ownPosts !== null) {
      if (Object.prototype.hasOwnProperty.call(ownPosts, currentBoard.dir)) {
        ownPosts[currentBoard.dir].forEach(reply => {
          if (reply.thread_id === post.parentid) {
            if (reply.reply_id === post.id) {
              isMine = true;
            }
          }
        });
      }
    }

    let iconColor = "grey";
    let icon = "user";
    let settingBrowserId = JSON.parse(localStorage.getItem("settings"))
      .browserId;

    if (userID.length > 8 && settingBrowserId) {
      let sufix = userID.substr(8, 1);
      const browser = {
        F: "firefox",
        C: "chrome",
        s: "safari",
        S: "SeaMonkey",
        o: "opera",
        I: "internet explorer",
        E: "microsoft edge",
        a: "android"
      };
      if (Object.keys(browser).includes(sufix)) {
        icon = browser[sufix];
      }
    }

    if (userID === "CAP_USER*") {
      iconColor = "blue";
      icon = "check circle";
      userID = "Usuario verificado";
    } else if (isMine) {
      iconColor = "olive";
    } else if (userID.startsWith("???")) {
      icon = "user secret";
    }
    if (post.name === "Renard ★") {
      iconColor = "yellow";
      icon = "chess queen";
      userID = "Bai-Client Dev";
      idColor = "#FBBD08";
    }
    if (post.name === "TOW ★") {
      iconColor = "pink";
      icon = "chess queen";
      userID = "Weabot Dev";
      idColor = "#e03997";
    }
    if (post.name === "Staff ★") {
      userID = "Staff de BaI";
      idColor = "rgba(255, 255, 255, 0.7);";
    }

    let hasVideo = post.file.endsWith(".webm");
    let hasAudio =
      post.file.endsWith(".mp3") ||
      post.file.endsWith(".opus") ||
      post.file.endsWith(".ogg");
    let isMime =
      post.file.endsWith(".epub") ||
      post.file.endsWith(".mod") ||
      post.file.endsWith(".pdf") ||
      post.file.endsWith(".s3m") ||
      post.file.endsWith(".swf") ||
      post.file.endsWith(".torrent") ||
      post.file.endsWith(".xm");

    let isDeleteable = true;
    // No se pueden eliminar hilos con más de 5 respuestas
    if (index === 0 && totalReplies > 5) {
      isDeleteable = false;
    }
    // No se pueden eliminar hilos/post de /cero/
    if (currentBoard.dir === "0") {
      isDeleteable = false;
    }
    // No se pueden eliminar post con una antiguedad superior a 24 horas
    if (Date.now() / 1000 - post.timestamp > 86400) {
      isDeleteable = false;
    }

    let settingRenderAvatar = JSON.parse(localStorage.getItem("settings"))
      .showAvatars;
    let settingEmbedYoutube = JSON.parse(localStorage.getItem("settings"))
      .embedYoutube;
    let settingColorifyIDs = JSON.parse(localStorage.getItem("settings"))
      .colorifyIDs;

    return (
      <Comment inverted={nightMode}>
        {settingRenderAvatar && (
          <Comment.Avatar
            src={`https://bienvenidoainternet.org/static/ico/${rndAvatar}.gif`}
            style={{ filter: `hue-rotate(${hue}deg)` }}
          />
        )}
        <Comment.Content>
          <Comment.Author
            as="a"
            href={post.mail !== "" ? `mailto:${post.email}` : null}
          >
            #{currentBoard.board_type === 0 ? post.id : index + 1}{" "}
            <span
              className={post.email === "sage" ? "username sage" : "username"}
            >
              {currentBoard.dir === "world"
                ? post.name.split("<em>")[0]
                : post.name}{" "}
              {currentBoard.dir === "world" ? <Flag name={flag} /> : null}
            </span>
            <span className="tripcode">{post.tripcode}</span>
          </Comment.Author>
          <Comment.Metadata>
            <div>
              <Moment fromNow unix locale="es" date={post.timestamp} />
            </div>
            <div>
              <Icon name={icon} color={iconColor} />
              {settingColorifyIDs ? (
                <span style={{ color: idColor }}>{userID}</span>
              ) : (
                userID
              )}
              {isMine ? " (Tú)" : null}
            </div>
          </Comment.Metadata>
          <Comment.Text>
            {post.file !== "" ? (
              <div className="imageContainer">
                {hasVideo && (
                  // eslint-disable-next-line jsx-a11y/media-has-caption
                  <video
                    src={`https://bienvenidoainternet.org/${currentBoard.dir}/src/${post.file}`}
                    controls
                    poster={`https://bienvenidoainternet.org/${currentBoard.dir}/thumb/${post.thumb}`}
                    width="400"
                  />
                )}
                {hasAudio && (
                  // eslint-disable-next-line jsx-a11y/media-has-caption
                  <audio
                    src={`https://bienvenidoainternet.org/${currentBoard.dir}/src/${post.file}`}
                    controls
                  />
                )}
                {!hasVideo && !hasAudio && !isMime && (
                  <ImageModal
                    href={`https://bienvenidoainternet.org/${currentBoard.dir}/src/${post.file}`}
                    trigger={
                      <Image
                        centered
                        className="postImage"
                        src={`https://bienvenidoainternet.org/${currentBoard.dir}/thumb/${post.thumb}`}
                      />
                    }
                  />
                )}
                {isMime && (
                  <Image
                    size="mini"
                    ui={false}
                    src={`https://bienvenidoainternet.org/static/${post.thumb}`}
                  />
                )}

                <div>
                  <a
                    target="_blank"
                    rel="noopener noreferrer"
                    href={`https://bienvenidoainternet.org/${currentBoard.dir}/src/${post.file}`}
                  >
                    {post.file}
                  </a>{" "}
                  {!hasAudio &&
                    !isMime &&
                    `${post.image_width}x${post.image_height}`}{" "}
                  {filesize(post.file_size)}
                </div>
              </div>
            ) : null}

            {youtubeVideos !== null && settingEmbedYoutube
              ? youtubeVideos.map((url, i) => {
                  let id = url.split("?v=")[1];
                  return (
                    <div className="playerContainer" key={i}>
                      <Embed
                        id={id}
                        source="youtube"
                        key={i}
                        placeholder={`https://img.youtube.com/vi/${id}/hqdefault.jpg`}
                      />
                    </div>
                  );
                })
              : null}

            <div
              className={`postMessage ${
                currentBoard.dir === "zonavip" ? "vipFont" : null
              }`}
              dangerouslySetInnerHTML={{ __html: post.message }}
            />
          </Comment.Text>
          <Comment.Actions>
            {locked ? null : (
              <QuickReplyModal
                trigger={<Comment.Action>Responder</Comment.Action>}
                currentBoard={currentBoard}
                id={threadId}
                locked={locked}
                replyIndex={currentBoard.board_type === 0 ? post.id : index + 1}
              />
            )}
            <ReportModal
              trigger={<Comment.Action>Reportar</Comment.Action>}
              postId={post.id}
            />
            {isDeleteable && (
              <Comment.Action onClick={this.showDialog}>
                Eliminar
              </Comment.Action>
            )}
          </Comment.Actions>
        </Comment.Content>
        <Confirm
          open={this.state.deleteDialog}
          content="¿Estás seguro de querer eliminar este post?"
          onCancel={this.handleCancel}
          onConfirm={this.handleConfirm}
          confirmButton="Eliminar"
          size="small"
        />
      </Comment>
    );
  }
}

export default Post;