You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_dispatch.go 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright © 2023 OpenIM open source community. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package service
  15. import (
  16. "context"
  17. "errors"
  18. "github.com/OpenIMSDK/OpenKF/server/internal/dal/dao"
  19. "github.com/shomali11/slacker"
  20. )
  21. // USER_DISPATCH_QUEUE_KEY user dispatch queue key.
  22. const USER_DISPATCH_QUEUE_KEY = "openkf:user_dispatch_queue"
  23. const USER_SLACK_MAP_KEY = "openkf:user_slack_map"
  24. // UserDispatchService user service.
  25. type UserDispatchService struct {
  26. Service
  27. UserDispatchDao *dao.UserDispatchDao
  28. SysUserDao *dao.SysUserDao
  29. }
  30. // NewUserDispatchService return new service with context.
  31. func NewUserDispatchService(c context.Context) *UserDispatchService {
  32. return &UserDispatchService{
  33. Service: Service{
  34. ctx: c,
  35. },
  36. UserDispatchDao: dao.NewUserDispatchDao(),
  37. SysUserDao: dao.NewSysUserDao(),
  38. }
  39. }
  40. // AddUser add user to enqueue.
  41. func (s *UserDispatchService) AddUser(uuid string) error {
  42. _, err := s.UserDispatchDao.AddUser(USER_DISPATCH_QUEUE_KEY, uuid)
  43. return err
  44. }
  45. // GetUser get user and update timestamp.
  46. func (s *UserDispatchService) GetUser() (string, error) {
  47. res, err := s.UserDispatchDao.GetUser(USER_DISPATCH_QUEUE_KEY)
  48. if err != nil {
  49. return "", errors.New("can not find a user")
  50. }
  51. // return a default value if res is empty
  52. if res == "" {
  53. u, err := s.SysUserDao.First()
  54. if err != nil {
  55. return "", err
  56. }
  57. return u.UUID, nil
  58. }
  59. return res, nil
  60. }
  61. // DeleteUser delete user from queue.
  62. func (s *UserDispatchService) DeleteUser(uuid string) error {
  63. _, err := s.UserDispatchDao.RemoveUser(USER_DISPATCH_QUEUE_KEY, uuid)
  64. return err
  65. }
  66. // SetSlackMap set slack map.
  67. func (s *UserDispatchService) SetSlackMap(customID, staffID string, botContext slacker.BotContext) error {
  68. return s.UserDispatchDao.SetSlackMap(USER_SLACK_MAP_KEY, customID, staffID, botContext.Event().ChannelID)
  69. }
  70. // GetSlackMap get slack map.
  71. func (s *UserDispatchService) GetSlackMap(customID string) *dao.SlackMap {
  72. return s.UserDispatchDao.GetSlackMap(USER_SLACK_MAP_KEY, customID)
  73. }
  74. // GetStaffID get all staff id.
  75. func (s *UserDispatchService) GetSlackIDs() ([]string, error) {
  76. return s.UserDispatchDao.GetSlackIDs(USER_SLACK_MAP_KEY)
  77. }
  78. // SlackUserFilter filter user by slack id.
  79. func (s *UserDispatchService) SlackUserFilter(uid string) bool {
  80. ids, err := s.GetSlackIDs()
  81. if err != nil || len(ids) == 0 {
  82. return false
  83. }
  84. for _, id := range ids {
  85. if id == uid {
  86. return true
  87. }
  88. }
  89. return false
  90. }