group.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package agent
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "bet24.com/log"
  7. )
  8. // 链接类型
  9. const (
  10. _ = iota // 0=无效
  11. LINK_FACEBOOK // 1=facebook 链接
  12. LINK_TELGRAM // 2=telgram 链接
  13. LINK_WHATSAPP // 3=whatsapp 链接
  14. )
  15. // 群信息
  16. type groupInfo struct {
  17. UserID int // 用户ID
  18. Links []*linkInfo
  19. }
  20. // 链接信息
  21. type linkInfo struct {
  22. Id int // 类型
  23. Name string // 群名称
  24. Url string // Url
  25. }
  26. func newUserGroup(userId int) *groupInfo {
  27. list := getGroup(userId)
  28. g := &groupInfo{}
  29. g.UserID = userId
  30. g.Links = append(g.Links, list...)
  31. return g
  32. }
  33. func (this *groupInfo) updateLink(id int, name, url string) int {
  34. if id != LINK_FACEBOOK && id != LINK_TELGRAM && id != LINK_WHATSAPP {
  35. log.Debug("agent.group.updateLink invalid id=%d name=%s url=%s", id, name, url)
  36. return 0
  37. }
  38. if name == "" || url == "" {
  39. log.Debug("agent.group.updateLink is empty id=%d name=%s url=%s", id, name, url)
  40. return 0
  41. }
  42. for _, v := range this.Links {
  43. if v.Id != id {
  44. continue
  45. }
  46. v.Name = name
  47. v.Url = url
  48. go this.saveLinkToDB()
  49. return 1
  50. }
  51. // 新增
  52. this.Links = append(this.Links, &linkInfo{
  53. Id: id,
  54. Name: name,
  55. Url: url,
  56. })
  57. go this.saveLinkToDB()
  58. return 1
  59. }
  60. func (this *groupInfo) saveLinkToDB() {
  61. buf, err := json.Marshal(this.Links)
  62. if err != nil {
  63. log.Error("agent.group.saveLinkToDB json marshal fail %v", err)
  64. return
  65. }
  66. updateGroup(this.UserID, string(buf))
  67. }
  68. func (this *groupInfo) clearOut() {
  69. ctx, cancel := context.WithTimeout(context.Background(), 1*time.Hour)
  70. select {
  71. case <-ctx.Done():
  72. // 清理
  73. mgr.clear(this.UserID)
  74. }
  75. cancel()
  76. }