Files
chat-deploy/internal/rpc/admin/scheduled_task.go
kim.dev.6789 b7f8db7d08 复制项目
2026-01-14 22:35:45 +08:00

103 lines
3.1 KiB
Go

// Copyright © 2023 OpenIM open source community. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package admin
import (
"context"
chatdb "git.imall.cloud/openim/chat/pkg/common/db/table/chat"
"git.imall.cloud/openim/chat/pkg/common/mctx"
"git.imall.cloud/openim/chat/pkg/protocol/admin"
"github.com/openimsdk/tools/errs"
)
// ==================== 定时任务管理相关 RPC ====================
// GetScheduledTasks 获取定时任务列表(管理员接口,可查看所有任务)
func (o *adminServer) GetScheduledTasks(ctx context.Context, req *admin.GetScheduledTasksReq) (*admin.GetScheduledTasksResp, error) {
// 验证管理员权限
_, err := mctx.CheckAdmin(ctx)
if err != nil {
return nil, err
}
// 获取所有定时任务列表
total, tasks, err := o.ChatDatabase.GetAllScheduledTasks(ctx, req.Pagination)
if err != nil {
return nil, err
}
// 转换为响应格式
taskInfos := make([]*admin.ScheduledTaskInfo, 0, len(tasks))
for _, task := range tasks {
taskInfos = append(taskInfos, convertScheduledTaskToAdminProto(task))
}
return &admin.GetScheduledTasksResp{
Total: total,
Tasks: taskInfos,
}, nil
}
// DeleteScheduledTask 删除定时任务(管理员接口,可删除任何任务)
func (o *adminServer) DeleteScheduledTask(ctx context.Context, req *admin.DeleteScheduledTaskReq) (*admin.DeleteScheduledTaskResp, error) {
// 验证管理员权限
_, err := mctx.CheckAdmin(ctx)
if err != nil {
return nil, err
}
// 验证请求参数
if len(req.TaskIDs) == 0 {
return nil, errs.ErrArgs.WrapMsg("taskIDs is required")
}
// 删除任务
if err := o.ChatDatabase.DeleteScheduledTask(ctx, req.TaskIDs); err != nil {
return nil, err
}
return &admin.DeleteScheduledTaskResp{}, nil
}
// convertScheduledTaskToAdminProto 将数据库模型转换为 admin protobuf 消息
func convertScheduledTaskToAdminProto(task *chatdb.ScheduledTask) *admin.ScheduledTaskInfo {
messages := make([]*admin.ScheduledTaskMessage, 0, len(task.Messages))
for _, msg := range task.Messages {
messages = append(messages, &admin.ScheduledTaskMessage{
Type: msg.Type,
Content: msg.Content,
Thumbnail: msg.Thumbnail,
Duration: msg.Duration,
FileSize: msg.FileSize,
Width: msg.Width,
Height: msg.Height,
})
}
return &admin.ScheduledTaskInfo{
Id: task.ID,
UserID: task.UserID,
Name: task.Name,
CronExpression: task.CronExpression,
Messages: messages,
RecvIDs: task.RecvIDs,
GroupIDs: task.GroupIDs,
Status: task.Status,
CreateTime: task.CreateTime.UnixMilli(),
UpdateTime: task.UpdateTime.UnixMilli(),
}
}