This commit is contained in:
Troplo 2022-08-06 19:22:21 +10:00
parent a6351e7588
commit d3c0360ee7
10 changed files with 209 additions and 34 deletions

View file

@ -0,0 +1,20 @@
"use strict"
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn("ChatAssociations", "notifications", {
type: Sequelize.ENUM(["all", "none", "mentions"]),
defaultValue: "all",
allowNull: false
})
},
async down(queryInterface, Sequelize) {
/**
* Add reverting commands here.
*
* Example:
* await queryInterface.dropTable('users');
*/
}
}

View file

@ -45,6 +45,11 @@ module.exports = (sequelize, DataTypes) => {
updatedAt: { updatedAt: {
allowNull: false, allowNull: false,
type: DataTypes.DATE type: DataTypes.DATE
},
notifications: {
type: DataTypes.ENUM(["all", "none", "mentions"]),
defaultValue: "all",
allowNull: false
} }
}, },
{ {

View file

@ -142,7 +142,13 @@ async function createMessage(req, type, content, association, userId) {
associationId: user.dataValues.id, associationId: user.dataValues.id,
keyId: `${ keyId: `${
message.dataValues.id message.dataValues.id
}-${message.dataValues.updatedAt.toISOString()}` }-${message.dataValues.updatedAt.toISOString()}`,
notify:
association.notifications === "all" ||
(association.notifications === "mentions" &&
message.content
.toLowerCase()
.includes(association.user.username.toLowerCase()))
}) })
}) })
} }
@ -771,6 +777,28 @@ router.put("/:id", auth, async (req, res, next) => {
} }
}) })
router.put("/settings/:id", auth, async (req, res, next) => {
try {
const io = req.app.get("io")
const association = await ChatAssociation.findOne({
where: {
userId: req.user.id,
id: req.params.id
}
})
if (association) {
await association.update({
notifications: req.body.notifications
})
res.sendStatus(204)
} else {
throw Errors.invalidParameter("chat association id")
}
} catch (err) {
next(err)
}
})
router.put("/:id/message/edit", auth, async (req, res, next) => { router.put("/:id/message/edit", auth, async (req, res, next) => {
try { try {
const io = req.app.get("io") const io = req.app.get("io")
@ -1017,7 +1045,6 @@ router.post(
attributes: [ attributes: [
"username", "username",
"name", "name",
"avatar", "avatar",
"id", "id",
"createdAt", "createdAt",
@ -1032,7 +1059,6 @@ router.post(
attributes: [ attributes: [
"username", "username",
"name", "name",
"avatar", "avatar",
"id", "id",
"createdAt", "createdAt",
@ -1111,7 +1137,6 @@ router.post(
attributes: [ attributes: [
"username", "username",
"name", "name",
"avatar", "avatar",
"id", "id",
"createdAt", "createdAt",
@ -1141,7 +1166,6 @@ router.post(
attributes: [ attributes: [
"username", "username",
"name", "name",
"avatar", "avatar",
"id", "id",
"createdAt", "createdAt",
@ -1156,7 +1180,6 @@ router.post(
attributes: [ attributes: [
"username", "username",
"name", "name",
"avatar", "avatar",
"id", "id",
"createdAt", "createdAt",
@ -1187,7 +1210,6 @@ router.post(
attributes: [ attributes: [
"username", "username",
"name", "name",
"avatar", "avatar",
"id", "id",
"createdAt", "createdAt",
@ -1221,7 +1243,13 @@ router.post(
}) })
}, },
associationId: association.id, associationId: association.id,
keyId: `${message.id}-${message.updatedAt.toISOString()}` keyId: `${message.id}-${message.updatedAt.toISOString()}`,
notify:
association.notifications === "all" ||
(association.notifications === "mentions" &&
message.content
.toLowerCase()
.includes(association.user.username.toLowerCase()))
}) })
} }
res.json({ res.json({
@ -1288,12 +1316,10 @@ router.post("/:id/message", auth, limiter, async (req, res, next) => {
attributes: [ attributes: [
"username", "username",
"name", "name",
"avatar", "avatar",
"id", "id",
"createdAt", "createdAt",
"updatedAt", "updatedAt",
"bot" "bot"
] ]
} }
@ -1305,12 +1331,10 @@ router.post("/:id/message", auth, limiter, async (req, res, next) => {
attributes: [ attributes: [
"username", "username",
"name", "name",
"avatar", "avatar",
"id", "id",
"createdAt", "createdAt",
"updatedAt", "updatedAt",
"bot" "bot"
] ]
} }
@ -1376,12 +1400,10 @@ router.post("/:id/message", auth, limiter, async (req, res, next) => {
attributes: [ attributes: [
"username", "username",
"name", "name",
"avatar", "avatar",
"id", "id",
"createdAt", "createdAt",
"updatedAt", "updatedAt",
"bot" "bot"
], ],
include: [ include: [
@ -1412,12 +1434,10 @@ router.post("/:id/message", auth, limiter, async (req, res, next) => {
attributes: [ attributes: [
"username", "username",
"name", "name",
"avatar", "avatar",
"id", "id",
"createdAt", "createdAt",
"updatedAt", "updatedAt",
"bot" "bot"
] ]
} }
@ -1429,12 +1449,10 @@ router.post("/:id/message", auth, limiter, async (req, res, next) => {
attributes: [ attributes: [
"username", "username",
"name", "name",
"avatar", "avatar",
"id", "id",
"createdAt", "createdAt",
"updatedAt", "updatedAt",
"bot" "bot"
], ],
include: [ include: [
@ -1462,12 +1480,10 @@ router.post("/:id/message", auth, limiter, async (req, res, next) => {
attributes: [ attributes: [
"username", "username",
"name", "name",
"avatar", "avatar",
"id", "id",
"createdAt", "createdAt",
"updatedAt", "updatedAt",
"bot" "bot"
] ]
} }
@ -1492,7 +1508,13 @@ router.post("/:id/message", auth, limiter, async (req, res, next) => {
}) })
}, },
associationId: association.id, associationId: association.id,
keyId: `${message.id}-${message.updatedAt.toISOString()}` keyId: `${message.id}-${message.updatedAt.toISOString()}`,
notify:
association.notifications === "all" ||
(association.notifications === "mentions" &&
message.content
.toLowerCase()
.includes(association.user.username.toLowerCase()))
}) })
} }
res.json({ res.json({

View file

@ -1,6 +1,6 @@
{ {
"name": "colubrina", "name": "colubrina",
"version": "1.0.15", "version": "1.0.16",
"private": true, "private": true,
"author": "Troplo <troplo@troplo.com>", "author": "Troplo <troplo@troplo.com>",
"scripts": { "scripts": {

View file

@ -547,17 +547,20 @@ export default {
) )
}) })
this.$socket.on("message", (message) => { this.$socket.on("message", (message) => {
if (message.notify) {
this.$store.state.communicationNotifications += 1 this.$store.state.communicationNotifications += 1
this.$store.state.chats.find( this.$store.state.chats.find(
(chat) => chat.id === message.associationId (chat) => chat.id === message.associationId
).unread += 1 ).unread += 1
}
if ( if (
(message.userId !== this.$store.state.user.id && (message.userId !== this.$store.state.user.id &&
parseInt(this.$route.params?.id) !== message.associationId && parseInt(this.$route.params?.id) !== message.associationId &&
this.$store.state.user?.storedStatus !== "busy") || this.$store.state.user?.storedStatus !== "busy") ||
(message.userId !== this.$store.state.user.id && (message.userId !== this.$store.state.user.id &&
this.$store.state.user?.storedStatus !== "busy" && this.$store.state.user?.storedStatus !== "busy" &&
!document.hasFocus()) !document.hasFocus() &&
message.notify)
) { ) {
if (localStorage.getItem("messageAudio")) { if (localStorage.getItem("messageAudio")) {
if (JSON.parse(localStorage.getItem("messageAudio"))) { if (JSON.parse(localStorage.getItem("messageAudio"))) {

View file

@ -1,3 +1,6 @@
.mentioned-message {
box-shadow: -2px 0 0 0 var(--v-primary-base);
}
.offset-message { .offset-message {
padding-left: 53px; padding-left: 53px;
} }

View file

@ -37,6 +37,11 @@
@keyup.esc="handleEsc" @keyup.esc="handleEsc"
@click:append-outer="handleMessage()" @click:append-outer="handleMessage()"
@keydown.enter.exact.prevent="handleMessage()" @keydown.enter.exact.prevent="handleMessage()"
@keydown.tab.exact.prevent="tabCompletion()"
@input="
completions = []
completionWord = ''
"
v-model="message" v-model="message"
@paste="handlePaste" @paste="handlePaste"
rows="1" rows="1"
@ -110,10 +115,51 @@ export default {
return { return {
message: "", message: "",
file: null, file: null,
blobURL: null blobURL: null,
mentions: false,
users: [],
completions: [],
completionWord: ""
} }
}, },
methods: { methods: {
tabCompletion() {
if (!this.completions.length) {
const word = this.message.split(" ").pop().toLowerCase()
const user = this.chat.chat.users.find((u) =>
u.username.toLowerCase().startsWith(word)
)
if (user) {
if (word.length) {
this.message = this.message.replace(
this.message.split(" ").pop(),
user.username + ", "
)
} else {
this.message = this.message + user.username + ", "
}
this.$nextTick(() => {
this.completionWord = this.message.split(" ").pop()
this.completions.push(user.username)
})
}
} else {
const user = this.chat.chat.users.find(
(u) =>
u.username.toLowerCase().startsWith(this.completionWord) &&
!this.completions.includes(u.username)
)
if (user) {
this.message = this.message.replace(
this.completions[this.completions.length - 1] + ", ",
user.username + ", "
)
this.$nextTick(() => {
this.completions.push(user.username)
})
}
}
},
handleEditMessage() { handleEditMessage() {
if (!this.message?.length) { if (!this.message?.length) {
this.editLastMessage() this.editLastMessage()

View file

@ -11,8 +11,58 @@
absolute absolute
offset-y offset-y
class="rounded-l" class="rounded-l"
style="z-index: 20"
> >
<v-list class="rounded-l" v-if="context.user.item"> <v-list class="rounded-l" v-if="context.user.item">
<v-menu
:nudge-right="10"
:close-delay="100"
:close-on-content-click="false"
:close-on-click="false"
bottom
offset-x
open-on-hover
>
<template v-slot:activator="{ on }">
<v-list-item v-on="on">
<v-list-item-title>Notification Settings</v-list-item-title
><v-icon class="ml-2">mdi-arrow-right</v-icon>
</v-list-item>
</template>
<div>
<v-list>
<v-list-item @click="setNotifications('all')">
<v-list-item-title>All messages</v-list-item-title>
<v-icon v-if="context.user.raw.notifications === 'all'"
>mdi-check</v-icon
>
</v-list-item>
<v-list-item two-line @click="setNotifications('mentions')">
<v-list-item-content>
<v-list-item-title
>Mentions only
<v-icon
style="float: right"
v-if="context.user.raw.notifications === 'mentions'"
>mdi-check</v-icon
></v-list-item-title
>
<v-list-item-subtitle
>Mentions are performed when your username is sent in the
chat.</v-list-item-subtitle
>
</v-list-item-content>
</v-list-item>
<v-list-item @click="setNotifications('none')">
<v-list-item-title>None</v-list-item-title>
<v-icon v-if="context.user.raw.notifications === 'none'"
>mdi-check</v-icon
>
</v-list-item>
</v-list>
</div>
</v-menu>
<v-list-item <v-list-item
v-if="context.user.item.type === 'direct'" v-if="context.user.item.type === 'direct'"
@click=" @click="
@ -450,7 +500,7 @@
<v-list-item <v-list-item
:to="'/communications/' + item.id" :to="'/communications/' + item.id"
@contextmenu=" @contextmenu="
show($event, 'user', getDirectRecipient(item), item.id) show($event, 'user', getDirectRecipient(item), item)
" "
> >
<v-badge <v-badge
@ -707,6 +757,19 @@ export default {
} }
}, },
methods: { methods: {
setNotifications(value) {
this.axios
.put("/api/v1/communications/settings/" + this.context.user.raw.id, {
notifications: value
})
.then(() => {
this.context.user.raw.notifications = value
this.$store.dispatch("getChats")
})
.catch((e) => {
AjaxErrorHandler(this.$store)(e)
})
},
groupSettings(id) { groupSettings(id) {
this.settings.item = this.$store.state.chats.find( this.settings.item = this.$store.state.chats.find(
(chat) => chat.id === id (chat) => chat.id === id
@ -717,14 +780,14 @@ export default {
this.leave.item = this.$store.state.chats.find((chat) => chat.id === id) this.leave.item = this.$store.state.chats.find((chat) => chat.id === id)
this.leave.dialog = true this.leave.dialog = true
}, },
show(e, context, item, id, state) { show(e, context, item, raw, state) {
if (!state) { if (!state) {
e.preventDefault() e.preventDefault()
this.context[context].value = false this.context[context].value = false
this.context[context].x = e.clientX this.context[context].x = e.clientX
this.context[context].y = e.clientY this.context[context].y = e.clientY
this.context[context].item = item this.context[context].item = item
this.context[context].id = id this.context[context].raw = raw
this.$nextTick(() => { this.$nextTick(() => {
this.context[context].value = true this.context[context].value = true
}) })
@ -734,7 +797,7 @@ export default {
this.$store.state.context[context].x = e.clientX this.$store.state.context[context].x = e.clientX
this.$store.state.context[context].y = e.clientY this.$store.state.context[context].y = e.clientY
this.$store.state.context[context].item = item this.$store.state.context[context].item = item
this.$store.state.context[context].id = id this.$store.state.context[context].raw = raw
this.$nextTick(() => { this.$nextTick(() => {
this.$store.state.context[context].value = true this.$store.state.context[context].value = true
}) })

View file

@ -39,7 +39,8 @@
:key="message.keyId" :key="message.keyId"
:class="{ :class="{
'message-hover': hover, 'message-hover': hover,
'pa-0': $vuetify.breakpoint.mobile 'pa-0': $vuetify.breakpoint.mobile,
'mentioned-message': mentioned
}" }"
:id="'message-' + index" :id="'message-' + index"
@contextmenu="show($event, 'message', message)" @contextmenu="show($event, 'message', message)"
@ -894,6 +895,13 @@ export default {
} }
} }
}, },
computed: {
mentioned() {
return this.message.content
.toLowerCase()
.includes(this.$store.state.user.username.toLowerCase())
}
},
methods: { methods: {
pinMessage() { pinMessage() {
this.axios this.axios

View file

@ -96,6 +96,7 @@ module.exports = {
`dist/**/*`, `dist/**/*`,
`node_modules/**/*`, `node_modules/**/*`,
`package.json`, `package.json`,
`src/background.js`,
`background.js` `background.js`
], ],
appId: "com.troplo.colubrina", appId: "com.troplo.colubrina",
@ -103,7 +104,11 @@ module.exports = {
publish: ["github"] publish: ["github"]
}, },
linux: { linux: {
publish: ["github"] publish: ["github"],
target: ["deb", "AppImage", "rpm", "zip", "tar.gz", "pacman"],
category: "Network",
synopsis: "Instant Messaging",
maintainer: "Troplo <troplo@troplo.com>"
}, },
mac: { mac: {
publish: ["github"] publish: ["github"]