Skip to content

Commit

Permalink
feat(pad-settings): added possibility to delete pad by the creator
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTV12345 committed Oct 28, 2024
1 parent ac44898 commit 4939831
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 15 deletions.
9 changes: 0 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@
"pad.settings.colorcheck": "Autorenfarben anzeigen",
"pad.settings.linenocheck": "Zeilennummern",
"pad.settings.rtlcheck": "Inhalt von rechts nach links lesen?",
"pad.settings.fontType": "Schriftart:",
"pad.settings.delete": "Pad löschen",
"pad.delete.confirm": "Möchtest du dieses Pad wirklich löschen?",
"pad.settings.fontType": "Schriftart:",
"pad.settings.fontType.normal": "Normal",
"pad.settings.language": "Sprache:",
"pad.settings.about": "Über",
Expand Down
2 changes: 2 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
"pad.settings.fontType": "Font type:",
"pad.settings.fontType.normal": "Normal",
"pad.settings.language": "Language:",
"pad.settings.deletePad": "Delete Pad",
"pad.delete.confirm": "Do you really want to delete this pad?",
"pad.settings.about": "About",
"pad.settings.poweredBy": "Powered by",

Expand Down
42 changes: 41 additions & 1 deletion src/node/handler/PadMessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {RateLimiterMemory} from 'rate-limiter-flexible';
import {ChangesetRequest, PadUserInfo, SocketClientRequest} from "../types/SocketClientRequest";
import {APool, AText, PadAuthor, PadType} from "../types/PadType";
import {ChangeSet} from "../types/ChangeSet";
import {ChatMessageMessage, ClientReadyMessage, ClientSaveRevisionMessage, ClientSuggestUserName, ClientUserChangesMessage, ClientVarMessage, CustomMessage, UserNewInfoMessage} from "../../static/js/types/SocketIOMessage";
import {ChatMessageMessage, ClientReadyMessage, ClientSaveRevisionMessage, ClientSuggestUserName, ClientUserChangesMessage, ClientVarMessage, CustomMessage, PadDeleteMessage, UserNewInfoMessage} from "../../static/js/types/SocketIOMessage";
import {Builder} from "../../static/js/Builder";
const webaccess = require('../hooks/express/webaccess');
const { checkValidRev } = require('../utils/checkValidRev');
Expand Down Expand Up @@ -211,6 +211,45 @@ exports.handleDisconnect = async (socket:any) => {
});
};


const handlePadDelete = async (socket: any, padDeleteMessage: PadDeleteMessage) => {
const session = sessioninfos[socket.id];
if (!session || !session.author || !session.padId) throw new Error('session not ready');
if (await padManager.doesPadExist(padDeleteMessage.data.padId)) {
const retrievedPad = await padManager.getPad(padDeleteMessage.data.padId)
// Only the one doing the first revision can delete the pad, otherwise people could troll a lot
const firstContributor = await retrievedPad.getRevisionAuthor(0)
if (session.author === firstContributor) {
retrievedPad.remove()
} else {

type ShoutMessage = {
message: string,
sticky: boolean,
}

const messageToShout: ShoutMessage = {
message: 'You are not the creator of this pad, so you cannot delete it',
sticky: false
}
const messageToSend = {
type: "COLLABROOM",
data: {
type: "shoutMessage",
payload: {
message: messageToShout,
timestamp: Date.now()
}
}
}
socket.emit('shout',
messageToSend
)
}
}
}


/**
* Handles a message from a user
* @param socket the socket.io Socket object for the client
Expand Down Expand Up @@ -350,6 +389,7 @@ exports.handleMessage = async (socket:any, message: ClientVarMessage) => {
stats.counter('pendingEdits').inc();
await padChannels.enqueue(thisSession.padId, {socket, message});
break;
case 'PAD_DELETE': await handlePadDelete(socket, message.data as unknown as PadDeleteMessage); break;
case 'USERINFO_UPDATE': await handleUserInfoUpdate(socket, message as unknown as UserNewInfoMessage); break;
case 'CHAT_MESSAGE': await handleChatMessage(socket, message as unknown as ChatMessageMessage); break;
case 'GET_CHAT_MESSAGES': await handleGetChatMessages(socket, message); break;
Expand Down
9 changes: 9 additions & 0 deletions src/static/js/pad_editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,20 @@ const padeditor = (() => {
padutils.setCheckbox($('#options-rtlcheck'), ('rtl' === html10n.getDirection()));
});



// font family change
$('#viewfontmenu').on('change', () => {
pad.changeViewOption('padFontFamily', $('#viewfontmenu').val());
});

// delete pad
$('#delete-pad').on('click', () => {
if (window.confirm(html10n.get('pad.delete.confirm'))) {
pad.collabClient.sendMessage({type: 'PAD_DELETE', data:{padId: pad.getPadId()}});
}
})

// Language
html10n.bind('localized', () => {
$('#languagemenu').val(html10n.getLanguage());
Expand Down
10 changes: 9 additions & 1 deletion src/static/js/types/SocketIOMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ export type ClientSaveRevisionMessage = {
type: 'SAVE_REVISION'
}


export type PadDeleteMessage = {
type: 'PAD_DELETE'
data: {
padId: string
}
}

export type GetChatMessageMessage = {
type: 'GET_CHAT_MESSAGES',
start: number,
Expand Down Expand Up @@ -283,7 +291,7 @@ export type ChangesetRequestMessage = {

export type CollabroomMessage = {
type: 'COLLABROOM'
data: ClientSendUserInfoUpdate | ClientUserChangesMessage | ChatMessageMessage | GetChatMessageMessage | ClientSaveRevisionMessage | ClientMessageMessage
data: ClientSendUserInfoUpdate | ClientUserChangesMessage | ChatMessageMessage | GetChatMessageMessage | ClientSaveRevisionMessage | ClientMessageMessage | PadDeleteMessage
}

export type ClientVarMessage = | ClientVarData | ClientDisconnectedMessage | ClientReadyMessage| ChangesetRequestMessage | CollabroomMessage | CustomMessage
Expand Down
4 changes: 4 additions & 0 deletions src/static/skins/colibris/src/components/popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,7 @@
.skin-variant-container {
text-transform: capitalize;
}

#delete-pad {
background-color: #ff7b72;
}
4 changes: 2 additions & 2 deletions src/templates/pad.html
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ <h2 data-l10n-id="pad.settings.myView"></h2>
</p>
<% e.end_block(); %>
</div>

<button data-l10n-id="pad.settings.delete" id="delete-pad">Delete pad</button>
<h2 data-l10n-id="pad.settings.about">About</h2>
<span data-l10n-id="pad.settings.poweredBy">Powered by</span>
<a href="https://etherpad.org">Etherpad</a>
<a href="https://etherpad.org" target="_blank" referrerpolicy="no-referrer" rel="noopener">Etherpad</a>
<% if (settings.exposeVersion) { %>(commit <%=settings.getGitCommit()%>)<% } %>
</div></div>

Expand Down
13 changes: 13 additions & 0 deletions src/tests/backend/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,19 @@ export const sendUserChanges = async (socket:any, data:any) => await sendMessage
},
});


/*
* Convenience function to send a delete pad request.
*/
export const sendPadDelete = async (socket:any, data:any) => await sendMessage(socket, {
type: 'PAD_DELETE',
component: 'pad',
data: {
padId: data.padId
},
});


/**
* Convenience function that waits for an ACCEPT_COMMIT message. Asserts that the new revision
* matches the expected revision.
Expand Down
2 changes: 1 addition & 1 deletion ui/pad.html
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ <h2 data-l10n-id="pad.settings.myView"></h2>
</p>

</div>

<button data-l10n-id="pad.settings.delete">Delete pad</button>
<h2 data-l10n-id="pad.settings.about">About</h2>
<span data-l10n-id="pad.settings.poweredBy">Powered by</span>
<a href="https://etherpad.org">Etherpad</a>
Expand Down

0 comments on commit 4939831

Please sign in to comment.