feat: warn user if it doesn't have message persistence permission

This commit is contained in:
JustSong
2023-04-16 18:51:58 +08:00
parent 3fca905b1e
commit dfdddc1064
6 changed files with 51 additions and 6 deletions
+1 -1
View File
@@ -133,7 +133,7 @@ func saveAndSendMessage(user *model.User, message *model.Message) error {
message.URL = fmt.Sprintf("%s/message/%s", common.ServerAddress, message.Link) message.URL = fmt.Sprintf("%s/message/%s", common.ServerAddress, message.Link)
} }
success := false success := false
if common.MessagePersistenceEnabled { if common.MessagePersistenceEnabled || user.SaveMessageToDatabase == common.SaveMessageToDatabaseAllowed {
defer func() { defer func() {
// Update the status of the message // Update the status of the message
status := common.MessageSendStatusFailed status := common.MessageSendStatusFailed
+3 -2
View File
@@ -43,6 +43,7 @@ type User struct {
TelegramChatId string `json:"telegram_chat_id"` TelegramChatId string `json:"telegram_chat_id"`
DiscordWebhookURL string `json:"discord_webhook_url"` DiscordWebhookURL string `json:"discord_webhook_url"`
SendEmailToOthers int `json:"send_email_to_others" gorm:"type:int;default:0"` SendEmailToOthers int `json:"send_email_to_others" gorm:"type:int;default:0"`
SaveMessageToDatabase int `json:"save_message_to_database" gorm:"type:int;default:0"`
} }
func GetMaxUserId() int { func GetMaxUserId() int {
@@ -52,7 +53,7 @@ func GetMaxUserId() int {
} }
func GetAllUsers(startIdx int, num int) (users []*User, err error) { func GetAllUsers(startIdx int, num int) (users []*User, err error) {
err = DB.Order("id desc").Limit(num).Offset(startIdx).Select([]string{"id", "username", "display_name", "role", "status", "email", "send_email_to_others"}).Find(&users).Error err = DB.Order("id desc").Limit(num).Offset(startIdx).Select([]string{"id", "username", "display_name", "role", "status", "email", "send_email_to_others", "save_message_to_database"}).Find(&users).Error
return users, err return users, err
} }
@@ -79,7 +80,7 @@ func GetUserById(id int, selectAll bool) (*User, error) {
"channel", "token", "channel", "token",
"wechat_test_account_id", "wechat_test_account_template_id", "wechat_test_account_open_id", "wechat_test_account_id", "wechat_test_account_template_id", "wechat_test_account_open_id",
"wechat_corp_account_id", "wechat_corp_account_agent_id", "wechat_corp_account_user_id", "wechat_corp_account_client_type", "wechat_corp_account_id", "wechat_corp_account_agent_id", "wechat_corp_account_user_id", "wechat_corp_account_client_type",
"bark_server", "telegram_chat_id", "bark_server", "telegram_chat_id", "save_message_to_database",
}).First(&user, "id = ?", id).Error }).First(&user, "id = ?", id).Error
} }
return &user, err return &user, err
+25 -3
View File
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { Button, Form, Label, Modal, Pagination, Table } from 'semantic-ui-react'; import { Button, Form, Label, Modal, Pagination, Table } from 'semantic-ui-react';
import { API, openPage, showError, showSuccess } from '../helpers'; import { API, openPage, showError, showSuccess, showWarning } from '../helpers';
import { ITEMS_PER_PAGE } from '../constants'; import { ITEMS_PER_PAGE } from '../constants';
@@ -149,14 +149,36 @@ const MessagesTable = () => {
})(); })();
}; };
const checkPermission = async () => {
// Check global permission
let res = await API.get('/api/status');
const { success, data } = res.data;
if (success) {
if (data.message_persistence) {
return;
}
}
// Check user permission
{
let res = await API.get('/api/user/self');
const { success, message, data } = res.data;
if (success) {
if (data.save_message_to_database !== 1) {
showWarning('您没有消息持久化的权限,消息未保存,请联系管理员。');
}
} else {
showError(message);
}
}
}
useEffect(() => { useEffect(() => {
// TODO: Prompt the user if message persistence is disabled
// TODO: Allow set persistence permission for each user
loadMessages(0) loadMessages(0)
.then() .then()
.catch((reason) => { .catch((reason) => {
showError(reason); showError(reason);
}); });
checkPermission().then();
}, []); }, []);
const viewMessage = async (id) => { const viewMessage = async (id) => {
+16
View File
@@ -85,6 +85,7 @@ const UsersTable = () => {
newUsers[realIdx].status = user.status; newUsers[realIdx].status = user.status;
newUsers[realIdx].role = user.role; newUsers[realIdx].role = user.role;
newUsers[realIdx].send_email_to_others = user.send_email_to_others; newUsers[realIdx].send_email_to_others = user.send_email_to_others;
newUsers[realIdx].save_message_to_database = user.save_message_to_database;
} }
setUsers(newUsers); setUsers(newUsers);
} else { } else {
@@ -295,6 +296,21 @@ const UsersTable = () => {
? '撤回发送任意邮件的权限' ? '撤回发送任意邮件的权限'
: '授予发送任意邮件的权限'} : '授予发送任意邮件的权限'}
</Dropdown.Item> </Dropdown.Item>
<Dropdown.Item
onClick={() => {
manageUser(
user.username,
user.save_message_to_database === 1
? 'disallow_save_message_to_database'
: 'allow_save_message_to_database',
idx
);
}}
>
{user.save_message_to_database === 1
? '撤回消息持久化的权限'
: '授予消息持久化的权限'}
</Dropdown.Item>
</Dropdown.Menu> </Dropdown.Menu>
</Dropdown> </Dropdown>
</div> </div>
+1
View File
@@ -2,5 +2,6 @@ export const toastConstants = {
SUCCESS_TIMEOUT: 500, SUCCESS_TIMEOUT: 500,
INFO_TIMEOUT: 3000, INFO_TIMEOUT: 3000,
ERROR_TIMEOUT: 5000, ERROR_TIMEOUT: 5000,
WARNING_TIMEOUT: 10000,
NOTICE_TIMEOUT: 20000 NOTICE_TIMEOUT: 20000
}; };
+5
View File
@@ -31,6 +31,7 @@ export function isMobile() {
} }
let showErrorOptions = { autoClose: toastConstants.ERROR_TIMEOUT }; let showErrorOptions = { autoClose: toastConstants.ERROR_TIMEOUT };
let showWarningOptions = { autoClose: toastConstants.WARNING_TIMEOUT };
let showSuccessOptions = { autoClose: toastConstants.SUCCESS_TIMEOUT }; let showSuccessOptions = { autoClose: toastConstants.SUCCESS_TIMEOUT };
let showInfoOptions = { autoClose: toastConstants.INFO_TIMEOUT }; let showInfoOptions = { autoClose: toastConstants.INFO_TIMEOUT };
let showNoticeOptions = { autoClose: false }; let showNoticeOptions = { autoClose: false };
@@ -74,6 +75,10 @@ export function showError(error) {
} }
} }
export function showWarning(message) {
toast.warn(message, showWarningOptions);
}
export function showSuccess(message) { export function showSuccess(message) {
toast.success(message, showSuccessOptions); toast.success(message, showSuccessOptions);
} }