File size: 2,346 Bytes
183a3de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { appContext } from "../context.js";
export const MessageType = {
  DirectMessage: 0,
  GroupMessage: 1,
};
export class Message {
  constructor(data) {
    this.type = MessageType.DirectMessage;
    this.data = data;
    this.threadId = data.uidFrom === "0" ? data.idTo : data.uidFrom;
    this.isSelf = data.uidFrom === "0";
    if (data.idTo === "0") data.idTo = appContext.uid;
    if (data.uidFrom === "0") data.uidFrom = appContext.uid;
  }
}
export class GroupMessage {
  constructor(data) {
    this.type = MessageType.GroupMessage;
    this.data = data;
    this.threadId = data.idTo;
    this.isSelf = data.uidFrom === "0";
    if (data.uidFrom === "0") data.uidFrom = appContext.uid;
  }
}

export function MessageMention(uid, length = 1, offset = 0, autoFormat = false) {
  if (typeof offset !== "number" || typeof length !== "number") {
    throw new Error("Invalid Length, Offset! Length and Offset must be numbers");
  }

  const mention = {
    pos: offset,
    len: length,
    uid: uid,
    type: uid === "-1" ? 1 : 0,
  };

  if (autoFormat) {
    return JSON.stringify([mention]);
  } else {
    return mention;
  }
}

export function MessageStyle(offset = 0, length = 1, color = "ffffff", size = "18", bold = false, italic = false, underline = false, strike = false, autoFormat = true) {
  if (typeof offset !== "number" || typeof length !== "number") {
    throw new Error("Invalid Length, Offset! Length and Offset must be numbers");
  }

  let styleValue = [];

  if (bold) styleValue.push("b");
  if (italic) styleValue.push("i");
  if (underline) styleValue.push("u");
  if (strike) styleValue.push("s");
  styleValue.push("c_" + color.replace("#", ""));
  styleValue.push("f_" + size);

  const styleObject = {
    start: offset,
    len: length,
    st: styleValue.join(","),
  };

  if (autoFormat) {
    return JSON.stringify({
      styles: [styleObject],
      ver: 0,
    });
  } else {
    return styleObject;
  }
}

export function MultiMsgStyle(listStyle) {
  const styles = listStyle.map((style) => {
    if (typeof style === "string") {
      return JSON.parse(style).styles[0];
    }
    return style;
  });

  const styleFormat = JSON.stringify({
    styles: styles,
    ver: 0,
  });

  return styleFormat;
}