aboutsummaryrefslogtreecommitdiff
path: root/vnext/server/common/MessageUtils.js
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2022-10-29 23:35:20 +0300
committerGravatar Vitaly Takmazov2023-01-13 10:37:58 +0300
commit47218df561e877265c739ffab59b760318ea3143 (patch)
tree166dd029133aca8d0d60af50535bb579bd6b8096 /vnext/server/common/MessageUtils.js
parent3a403e4023ce3b7074a7922e7b504263ddc7a3d1 (diff)
Merge server routes from Next version
Diffstat (limited to 'vnext/server/common/MessageUtils.js')
-rw-r--r--vnext/server/common/MessageUtils.js66
1 files changed, 66 insertions, 0 deletions
diff --git a/vnext/server/common/MessageUtils.js b/vnext/server/common/MessageUtils.js
new file mode 100644
index 00000000..f766faf2
--- /dev/null
+++ b/vnext/server/common/MessageUtils.js
@@ -0,0 +1,66 @@
+/**
+ * check if message is PM
+ *
+ * @param {import('../../src/api').Message} msg message
+ */
+ export function isPM(msg) {
+ return !msg.mid;
+}
+
+/**
+ * check if message is reply
+ *
+ * @param {import('../../src/api').Message} msg message
+ */
+export function isReply(msg) {
+ return msg.rid && msg.rid > 0;
+}
+
+/**
+ * check if message is service one
+ *
+ * @param {import('../../src/api').Message} msg message
+ */
+export function isService(msg) {
+ return msg.service && msg.service;
+}
+
+/**
+ * format notification title
+ *
+ * @param {import('../../src/api').Message} msg message
+ * @returns {string} formatted title
+ */
+export function formatTitle(msg) {
+ if (isReply(msg)) {
+ return `Reply by ${msg.user.uname}:`;
+ } else if (isPM(msg)) {
+ return `Private message from ${msg.user.uname}:`;
+ }
+ return `${msg.user.uname}`;
+}
+
+/**
+ * format notification quote
+ *
+ * @param { import('../../src/api').Message } msg message
+ * @returns {string} formatted quote line
+ */
+export function formatQuote(msg) {
+ if (isReply(msg)) {
+ return msg.replyQuote || '';
+ } else if (isPM(msg)) {
+ return '';
+ }
+ return (msg.tags || []).map(t => `*${t}`).join(', ');
+}
+
+/**
+ * format notification body
+ *
+ * @param {import('../../src/api').Message} msg message
+ * @returns {string} formatted body
+ */
+export function formatMessage(msg) {
+ return msg.body || 'Sent an image';
+}