commit b03d151ab83ec2e223b0ff827713d8d707d38d80
parent b036c518b38ff3fd8b6ba80b2949d00813fea5fb
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Mon, 5 Jun 2023 20:35:08 -0700
room read-only
Diffstat:
6 files changed, 35 insertions(+), 1 deletion(-)
diff --git a/cmd/dkf/migrations/138.sql b/cmd/dkf/migrations/138.sql
@@ -0,0 +1,4 @@
+-- +migrate Up
+ALTER TABLE chat_rooms ADD COLUMN read_only TINYINT(1) NOT NULL DEFAULT 0;
+
+-- +migrate Down
diff --git a/pkg/database/tableChatRooms.go b/pkg/database/tableChatRooms.go
@@ -24,6 +24,7 @@ type ChatRoom struct {
Password string // Hashed password (sha512)
IsListed bool
IsEphemeral bool
+ ReadOnly bool
CreatedAt time.Time
OwnerUser *User
ReadRecord *ChatReadRecord
diff --git a/pkg/web/handlers/api/v1/msgInterceptor.go b/pkg/web/handlers/api/v1/msgInterceptor.go
@@ -15,6 +15,13 @@ import (
type MsgInterceptor struct{}
func (i MsgInterceptor) InterceptMsg(cmd *Command) {
+ if cmd.room.ReadOnly {
+ if cmd.room.OwnerUserID != nil && *cmd.room.OwnerUserID != cmd.authUser.ID {
+ cmd.err = fmt.Errorf("room is read-only")
+ return
+ }
+ }
+
// Only check maximum length of message if we are uploading a file
// Trim whitespaces and ensure minimum length
minLen := utils.Ternary(cmd.upload != nil, 0, minMsgLen)
diff --git a/pkg/web/handlers/api/v1/slashInterceptor.go b/pkg/web/handlers/api/v1/slashInterceptor.go
@@ -111,7 +111,8 @@ func handlePrivateRoomOwnerCmd(c *Command) (handled bool) {
handleGroupRmUserCmd(c) ||
handleSetModeWhitelistCmd(c) ||
handleSetModeStandardCmd(c) ||
- handleGetRoomWhitelistCmd(c)
+ handleGetRoomWhitelistCmd(c) ||
+ handleToggleReadOnlyCmd(c)
}
return false
}
@@ -734,6 +735,20 @@ func handleGetRoomWhitelistCmd(c *Command) (handled bool) {
return false
}
+func handleToggleReadOnlyCmd(c *Command) (handled bool) {
+ if c.message == "/ro" {
+ c.room.ReadOnly = !c.room.ReadOnly
+ c.room.DoSave(c.db)
+ if c.room.ReadOnly {
+ c.err = NewErrSuccess("room is now read-only")
+ } else {
+ c.err = NewErrSuccess("room is no longer read-only")
+ }
+ return true
+ }
+ return
+}
+
func handleAddGroupCmd(c *Command) (handled bool) {
if m := addGroupRgx.FindStringSubmatch(c.message); len(m) == 2 {
name := m[1]
diff --git a/pkg/web/handlers/api/v1/topBarHandler.go b/pkg/web/handlers/api/v1/topBarHandler.go
@@ -469,6 +469,9 @@ func (c *Command) zeroPublicMsg(raw, msg string) {
}
func (c *Command) rawMsg(user1 database.User, user2 *database.User, raw, msg string) {
+ if c.room.ReadOnly {
+ return
+ }
rawMsgRoom(c.db, user1, user2, raw, msg, c.roomKey, c.room.ID)
}
diff --git a/pkg/web/public/views/pages/chat-help.gohtml b/pkg/web/public/views/pages/chat-help.gohtml
@@ -153,6 +153,10 @@
<h5>Room owner</h5>
<div>
+ <div><code>/ro</code></div>
+ <p>Toggle the room "read-only" property. When "read-only" is enabled, only the owner can write in the room.</p>
+ </div>
+ <div>
<div><code>/whitelist username</code> (or: <code>/wl username</code>)</div>
<p>Toggle add/remove username from the whitelist</p>
</div>