plugin_message.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package core
  2. import (
  3. "encoding/json"
  4. "time"
  5. "github.com/cdle/sillyplus/utils"
  6. )
  7. type PMsg struct {
  8. Class string `json:"class"`
  9. Unix int `json:"unix"`
  10. Content string `json:"content"`
  11. Version string `json:"version"`
  12. }
  13. var plugin_messages = MakeBucket("plugin_messages")
  14. func WritePluginMessage(uuid string, class string, content string) {
  15. if uuid == "" {
  16. return
  17. }
  18. var version = ""
  19. f := GetFunctionByUUID(uuid)
  20. if f != nil {
  21. version = f.Version
  22. }
  23. var data = plugin_messages.GetBytes(uuid)
  24. pmsgs := []PMsg{}
  25. json.Unmarshal(data, &pmsgs)
  26. s := &Strings{}
  27. ok := false
  28. new := PMsg{
  29. Class: class,
  30. Unix: int(time.Now().Unix()),
  31. Content: content,
  32. Version: version,
  33. }
  34. for i := range pmsgs {
  35. if pmsgs[i].Class != class {
  36. continue
  37. }
  38. if content == "" {
  39. continue
  40. }
  41. if s.Similarity(pmsgs[i].Content, content) > 0.9 {
  42. pmsgs[i] = new
  43. ok = true
  44. break
  45. }
  46. }
  47. if !ok {
  48. pmsgs = append(pmsgs, new)
  49. }
  50. plugin_messages.Set(uuid, utils.JsonMarshal(pmsgs))
  51. }
  52. func GetPluginMessage(uuid string) []PMsg {
  53. var data = plugin_messages.GetBytes(uuid)
  54. pmsgs := []PMsg{}
  55. json.Unmarshal(data, &pmsgs)
  56. return pmsgs
  57. }