Skip to content

Commit

Permalink
Added "Delete All Messages Forever Now" button! 🎉
Browse files Browse the repository at this point in the history
Fixes #10
Fixes #203
  • Loading branch information
elimisteve committed Dec 18, 2020
1 parent 13dc346 commit d3f89d7
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 11 deletions.
21 changes: 19 additions & 2 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ import (
type Message []byte

type OutgoingPayload struct {
Ephemeral []Message `json:"ephemeral"`
Ephemeral []Message `json:"ephemeral"`
FromServer FromServer `json:"from_server,omitempty"`
}

type FromServer struct {
AllMessagesDeleted bool `json:"all_messages_deleted,omitempty"`
}

type ToServer struct {
TTL *int `json:"ttl_secs"`
TTL *int `json:"ttl_secs"`
DeleteAllMessages bool `json:"delete_all_messages"`
}

type IncomingPayload struct {
Expand Down Expand Up @@ -75,6 +81,17 @@ func messageReader(room *Room, client *Client) {
continue
}

if payload.ToServer.DeleteAllMessages {
err = room.DeleteAllMessages()
if err != nil {
room.BroadcastMessages(client, Message(err.Error()))
log.Errorf("Error deleting all messages from room %s -- %s", room.ID, err)
continue
}
room.BroadcastDeleteSignal()
continue
}

err = room.AddMessages(payload.Ephemeral, payload.ToServer.TTL)
if err != nil {
log.Debugf("Error from AddMessages: %v", err)
Expand Down
11 changes: 11 additions & 0 deletions pg_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ func (cl *PGClient) Get(urlSuffix string) (*http.Response, error) {
return http.DefaultClient.Do(req)
}

func (cl *PGClient) Delete(urlSuffix string) (*http.Response, error) {
log.Debugf("DELETE'ing from %s", urlSuffix)

req, _ := http.NewRequest("DELETE", cl.BaseURL+urlSuffix, nil)
// req.Header.Add("Prefer", "return=representation")
req.Header.Add("Prefer", "return=none")
// req.Header.Add("Content-Type", "application/json")

return http.DefaultClient.Do(req)
}

func (cl *PGClient) GetInto(urlSuffix string, respobj interface{}) error {
resp, err := cl.Get(urlSuffix)
if err != nil {
Expand Down
61 changes: 61 additions & 0 deletions room.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -180,6 +182,39 @@ func (r *Room) BroadcastMessages(sender *Client, msgs ...Message) {
}
}

func (r *Room) DeleteAllMessages() error {
resp, err := r.pgClient.Delete("/messages?room_id=eq." + r.ID)
if err != nil {
return err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

if len(body) != 0 {
return fmt.Errorf("Error deleting messages: `%s`", body)
}

return nil
}

func (r *Room) BroadcastDeleteSignal() {
r.clientLock.RLock()
defer r.clientLock.RUnlock()

for _, client := range r.Clients {
go func(client *Client) {
err := client.SendDeleteSignal()
if err != nil {
log.Debugf("Error sending message. Err: %s", err)
}
}(client)
}
}

type Client struct {
wsConn *websocket.Conn
writeLock sync.Mutex
Expand Down Expand Up @@ -208,6 +243,32 @@ func (c *Client) SendMessages(msgs ...Message) error {
return nil
}

func (c *Client) SendDeleteSignal() error {
c.writeLock.Lock()
defer c.writeLock.Unlock()

outgoing := OutgoingPayload{
Ephemeral: []Message{},
FromServer: FromServer{
AllMessagesDeleted: true,
},
}

body, err := json.Marshal(outgoing)
if err != nil {
return err
}

err = c.wsConn.WriteMessage(websocket.TextMessage, body)
if err != nil {
log.Debugf("Error sending message to client. Removing client from room. Err: %s", err)
c.room.RemoveClient(c)
return err
}

return nil
}

func (c *Client) SendError(errStr string, secretErr error) error {
c.writeLock.Lock()
defer c.writeLock.Unlock()
Expand Down
19 changes: 18 additions & 1 deletion src/components/chat/MessageForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
addSuggestion
} from '../../actions/chatActions';

import { chatHandler } from '../../epics/chatEpics';

class MessageForm extends Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -130,6 +132,10 @@ class MessageForm extends Component {
this.messageInput = input;
}

onDeleteAllMsgs = (e) => {
chatHandler.sendDeleteAllMessagesSignalToServer();
}

render() {
const { message, showEmojiPicker } = this.props.chat;
const { messageUpdate, togglePicker } = this.props;
Expand All @@ -154,7 +160,18 @@ class MessageForm extends Component {
<div className="chat-icons">
<FaSmileO size={24}
className="emoji-picker-icon"
onClick={togglePicker} />
onClick={togglePicker}
/>

<div className="right-chat-icons">
<button
style={{height: '100%', padding: '0 8px 0 10px'}}
className="delete-all-msgs"
onClick={this.onDeleteAllMsgs}
>
Delete All Messages Forever Now
</button>
</div>
</div>

<div className="message" onKeyDown={this.handleKeyDown}>
Expand Down
2 changes: 1 addition & 1 deletion src/epics/chatEpics.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import createDetectVisibilityObservable from './helpers/createDetectPageVisibili

const authUrl = `${window.location.origin}/api/login`;
const wsUrl = `${window.location.origin.replace('http', 'ws')}/api/ws/messages/all`;
const chatHandler = new ChatHandler(wsUrl);
export const chatHandler = new ChatHandler(wsUrl);

import { Observable } from 'rxjs/Observable';
import { ajax } from 'rxjs/observable/dom/ajax';
Expand Down
14 changes: 14 additions & 0 deletions src/epics/helpers/ChatHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class ChatHandler {
.catch(error => console.error('An error occurred in ChatHandler', error))
.subscribe();
}
if (data && data.from_server) {
if (data.from_server.all_messages_deleted) {
alert("All messages deleted from server! (Refresh this page to remove them from this browser tab.)");
}
}
}

resolveIncomingMessageTypeObservable = (message) => {
Expand Down Expand Up @@ -144,6 +149,15 @@ class ChatHandler {
this.send({ tags, ttl });
}

sendDeleteAllMessagesSignalToServer = () => {
const msgForServer = {
to_server: {
delete_all_messages: true
}
}
this.ws.send(JSON.stringify(msgForServer));
}

sendMessageToServer = (ttl_secs, fileBlob, saveName, senderMinilockID) => {
const reader = new FileReader();
reader.addEventListener("loadend", () => {
Expand Down
30 changes: 23 additions & 7 deletions src/static/sass/_layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,7 @@ header {
flex-grow: 2;
border: none;
}
.chat-icons {
display: flex;
flex-direction: row;
margin-bottom: 5px;
}
.emoji-picker-icon {
float: left;
margin-left: 8px;
cursor: pointer;
}
}
Expand Down Expand Up @@ -341,3 +334,26 @@ textarea {
max-height: 40vh !important;
width: 100% !important;
}

// All widths
.chat-icons {
display: flex;
flex-direction: row;
margin-bottom: 5px;
width: 100%;
}
.chat-icons .right-chat-icons {
display: flex;
flex-direction: row;
height: 100%;
margin-left: auto;
}
.right-chat-icons .delete-all-msgs {
display: flex;
flex-direction: row;
background-color: #c00;
color: white;
}
.delete-all-msgs:hover {
background-color: #e00;
}

0 comments on commit d3f89d7

Please sign in to comment.