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.

chat.go 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2023 The casbin Authors. 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 object
  15. import (
  16. "fmt"
  17. "github.com/casbin/casibase/util"
  18. "xorm.io/core"
  19. )
  20. type Chat struct {
  21. Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
  22. Name string `xorm:"varchar(100) notnull pk" json:"name"`
  23. CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
  24. UpdatedTime string `xorm:"varchar(100)" json:"updatedTime"`
  25. // Organization string `xorm:"varchar(100)" json:"organization"`
  26. DisplayName string `xorm:"varchar(100)" json:"displayName"`
  27. Category string `xorm:"varchar(100)" json:"category"`
  28. Type string `xorm:"varchar(100)" json:"type"`
  29. User1 string `xorm:"varchar(100)" json:"user1"`
  30. User2 string `xorm:"varchar(100)" json:"user2"`
  31. Users []string `xorm:"varchar(100)" json:"users"`
  32. MessageCount int `json:"messageCount"`
  33. }
  34. func GetGlobalChats() ([]*Chat, error) {
  35. chats := []*Chat{}
  36. err := adapter.engine.Asc("owner").Desc("created_time").Find(&chats)
  37. if err != nil {
  38. return chats, err
  39. }
  40. return chats, nil
  41. }
  42. func GetChats(owner string) ([]*Chat, error) {
  43. chats := []*Chat{}
  44. err := adapter.engine.Desc("created_time").Find(&chats, &Chat{Owner: owner})
  45. if err != nil {
  46. return chats, err
  47. }
  48. return chats, nil
  49. }
  50. func getChat(owner, name string) (*Chat, error) {
  51. chat := Chat{Owner: "admin", Name: name}
  52. existed, err := adapter.engine.Get(&chat)
  53. if err != nil {
  54. return nil, err
  55. }
  56. if existed {
  57. return &chat, nil
  58. } else {
  59. return nil, nil
  60. }
  61. }
  62. func GetChat(id string) (*Chat, error) {
  63. owner, name := util.GetOwnerAndNameFromId(id)
  64. return getChat(owner, name)
  65. }
  66. func UpdateChat(id string, chat *Chat) (bool, error) {
  67. owner, name := util.GetOwnerAndNameFromId(id)
  68. _, err := getChat(owner, name)
  69. if err != nil {
  70. return false, err
  71. }
  72. if chat == nil {
  73. return false, nil
  74. }
  75. _, err = adapter.engine.ID(core.PK{owner, name}).AllCols().Update(chat)
  76. if err != nil {
  77. return false, err
  78. }
  79. // return affected != 0
  80. return true, nil
  81. }
  82. func AddChat(chat *Chat) (bool, error) {
  83. if chat.Type == "AI" && chat.User2 == "" {
  84. provider, err := GetDefaultModelProvider()
  85. if err != nil {
  86. return false, err
  87. }
  88. if provider != nil {
  89. chat.User2 = provider.Name
  90. }
  91. }
  92. affected, err := adapter.engine.Insert(chat)
  93. if err != nil {
  94. return false, err
  95. }
  96. return affected != 0, nil
  97. }
  98. func DeleteChat(chat *Chat) (bool, error) {
  99. affected, err := adapter.engine.ID(core.PK{chat.Owner, chat.Name}).Delete(&Chat{})
  100. if err != nil {
  101. return false, err
  102. }
  103. return affected != 0, nil
  104. }
  105. func (chat *Chat) GetId() string {
  106. return fmt.Sprintf("%s/%s", chat.Owner, chat.Name)
  107. }