Users can only edit & delete their own messages (except for admin users)

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello 2024-04-28 16:36:54 +02:00
parent a771710094
commit 0cab21f344
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 24 additions and 13 deletions

View File

@ -235,9 +235,18 @@ function redrawMessages() {
fetchMoreButton.classList.remove('d-none')
}
function removeAllPopovers() {
for (let popover of document.querySelectorAll('*[aria-describedby*="popover"]')) {
let instance = bootstrap.Popover.getInstance(popover)
if (instance)
instance.dispose()
}
}
function registerSendPrivateMessageContextMenu(message, element) {
element.addEventListener('contextmenu', (menu_event) => {
menu_event.preventDefault()
removeAllPopovers()
const popover = bootstrap.Popover.getOrCreateInstance(element, {
'title': message['author'],
'content': `<a id="send-private-message-link-${message['id']}" class="nav-link" href="#" tabindex="0">Envoyer un message privé</a>`,
@ -247,7 +256,7 @@ function registerSendPrivateMessageContextMenu(message, element) {
document.getElementById('send-private-message-link-' + message['id']).addEventListener('click', event => {
event.preventDefault()
popover.hide()
popover.dispose()
socket.send(JSON.stringify({
'type': 'start_private_chat',
'user_id': message['author_id'],
@ -259,10 +268,13 @@ function registerSendPrivateMessageContextMenu(message, element) {
function registerMessageContextMenu(message, element) {
element.addEventListener('contextmenu', (menu_event) => {
menu_event.preventDefault()
removeAllPopovers()
let content = `<a id="send-private-message-link-msg-${message['id']}" class="nav-link" href="#" tabindex="0">Envoyer un message privé</a>`
content += `<hr class="my-1">`
content += `<a class="nav-link" href="#" tabindex="0">Modifier</a>`
content += `<a class="nav-link" href="#" tabindex="0">Supprimer</a>`
if (message['author_id'] === USER_ID || IS_ADMIN) {
content += `<hr class="my-1">`
content += `<a class="nav-link" href="#" tabindex="0">Modifier</a>`
content += `<a class="nav-link" href="#" tabindex="0">Supprimer</a>`
}
const popover = bootstrap.Popover.getOrCreateInstance(element, {
'content': content,
'html': true,
@ -272,7 +284,7 @@ function registerMessageContextMenu(message, element) {
document.getElementById('send-private-message-link-msg-' + message['id']).addEventListener('click', event => {
event.preventDefault()
popover.hide()
popover.dispose()
socket.send(JSON.stringify({
'type': 'start_private_chat',
'user_id': message['author_id'],
@ -296,13 +308,7 @@ function toggleFullscreen() {
}
document.addEventListener('DOMContentLoaded', () => {
document.addEventListener('click', () => {
for (let popover of document.querySelectorAll('span[aria-describedby*="popover"]')) {
let instance = bootstrap.Popover.getInstance(popover)
if (instance)
instance.hide()
}
})
document.addEventListener('click', removeAllPopovers)
/**
* Process the received data from the server.

View File

@ -88,3 +88,8 @@
</form>
</div>
</div>
<script>
const USER_ID = {{ request.user.id }}
const IS_ADMIN = {{ request.user.registration.is_admin|yesno:"true,false" }}
</script>