video.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package model
  2. import (
  3. "fmt"
  4. "sort"
  5. xtime "go-common/library/time"
  6. )
  7. const (
  8. bit1 = int8(1)
  9. bit2 = int8(1) << 1
  10. StateDefaultPublic = int8(0) // binary 00 / int 0
  11. StateDefaultNoPublic = int8(0) | bit1 // binary 01 / int 1
  12. StateNormalPublic = bit2 | int8(0) // binary 10 / int 2
  13. StateNormalNoPublic = bit2 | bit1 // binary 11 / int 3
  14. // DefaultFolderName default name of favorite folder
  15. DefaultFolderName = "默认收藏夹"
  16. // AllFidFlag all folder id flag
  17. AllFidFlag = -1
  18. // CDFlag cool down flag
  19. CDFlag = -1
  20. // search error code
  21. SearchErrWordIllegal = -110 // 非法搜索词错误
  22. // clean state
  23. StateAllowToClean = 0
  24. StateCleaning = 1
  25. StateCleanCD = 2
  26. )
  27. type VideoFolder struct {
  28. Fid int64 `json:"fid"`
  29. Mid int64 `json:"mid"`
  30. Name string `json:"name"`
  31. MaxCount int `json:"max_count"`
  32. CurCount int `json:"cur_count"`
  33. AttenCount int `json:"atten_count"`
  34. Favoured int8 `json:"favoured"`
  35. State int8 `json:"state"`
  36. CTime xtime.Time `json:"ctime"`
  37. MTime xtime.Time `json:"mtime"`
  38. Cover []*Cover `json:"cover,omitempty"`
  39. }
  40. // IsPublic return true if folder is public.
  41. func (f *VideoFolder) IsPublic() bool {
  42. return f.State&bit1 == int8(0)
  43. }
  44. // IsDefault return true if folder is default.
  45. func (f *VideoFolder) IsDefault() bool {
  46. return f.State&bit2 == int8(0)
  47. }
  48. // StatePub return folder's public state.
  49. func (f *VideoFolder) StatePub() int8 {
  50. return f.State & bit1
  51. }
  52. // StateDef return folder's default state.
  53. func (f *VideoFolder) StateDef() int8 {
  54. return f.State & bit2
  55. }
  56. // IsDefault return true if state is default state.
  57. func IsDefault(state int8) bool {
  58. return (state&(int8(1)<<1) == int8(0))
  59. }
  60. // CheckPublic check user update public value in [0, 1].
  61. func CheckPublic(state int8) bool {
  62. return state == int8(0) || state == bit1
  63. }
  64. type VideoFolders []*VideoFolder
  65. func (f VideoFolders) Len() int { return len(f) }
  66. func (f VideoFolders) Less(i, j int) bool {
  67. if f[i].IsDefault() {
  68. return true
  69. }
  70. if f[j].IsDefault() {
  71. return false
  72. }
  73. if f[i].Fid > f[j].Fid {
  74. return true
  75. }
  76. return false
  77. }
  78. func (f VideoFolders) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
  79. // Cover image
  80. type Cover struct {
  81. Aid int64 `json:"aid"`
  82. Pic string `json:"pic"`
  83. }
  84. // VideoFolderSort folder index.
  85. type VideoFolderSort struct {
  86. ID int64 `json:"id"`
  87. Mid int64 `json:"mid"`
  88. Sort []int64 `json:"sort"`
  89. Map map[int64]struct{} `json:"map"`
  90. CTime xtime.Time `json:"ctime"`
  91. MTime xtime.Time `json:"mtime"`
  92. }
  93. // Index return index for fids.
  94. func (f *VideoFolderSort) Index() []byte {
  95. var (
  96. i int
  97. v int64
  98. fs = f.Sort
  99. n = len(fs) * 8
  100. b = make([]byte, n)
  101. )
  102. for i = 0; i < n; i += 8 {
  103. v = fs[i/8]
  104. b[i] = byte(v >> 56)
  105. b[i+1] = byte(v >> 48)
  106. b[i+2] = byte(v >> 40)
  107. b[i+3] = byte(v >> 32)
  108. b[i+4] = byte(v >> 24)
  109. b[i+5] = byte(v >> 16)
  110. b[i+6] = byte(v >> 8)
  111. b[i+7] = byte(v)
  112. }
  113. return b
  114. }
  115. // SetIndex set sort fids.
  116. func (f *VideoFolderSort) SetIndex(b []byte) (err error) {
  117. var (
  118. i int
  119. id int64
  120. n = len(b)
  121. ids = make([]int64, n)
  122. )
  123. if len(b)%8 != 0 {
  124. err = fmt.Errorf("invalid sort index:%v", b)
  125. return
  126. }
  127. f.Map = make(map[int64]struct{}, n)
  128. for i = 0; i < n; i += 8 {
  129. id = int64(b[i+7]) |
  130. int64(b[i+6])<<8 |
  131. int64(b[i+5])<<16 |
  132. int64(b[i+4])<<24 |
  133. int64(b[i+3])<<32 |
  134. int64(b[i+2])<<40 |
  135. int64(b[i+1])<<48 |
  136. int64(b[i])<<56
  137. ids[i/8] = id
  138. f.Map[id] = struct{}{}
  139. }
  140. f.Sort = ids
  141. return
  142. }
  143. // SortFavs sort the favorites by index.
  144. func (f *VideoFolderSort) SortFavs(fs map[int64]*VideoFolder, isSelf bool) (res []*VideoFolder, update bool) {
  145. var (
  146. ok bool
  147. id int64
  148. sorted []int64
  149. fav *VideoFolder
  150. idx = f.Sort
  151. )
  152. res = make([]*VideoFolder, 0, len(fs))
  153. if len(f.Sort) == 0 {
  154. for _, fav = range fs {
  155. if !isSelf && !fav.IsPublic() {
  156. continue
  157. }
  158. res = append(res, fav)
  159. }
  160. sort.Sort(VideoFolders(res))
  161. return
  162. }
  163. if len(idx) != len(fs) {
  164. sorted = append(sorted, idx[0])
  165. for id = range fs {
  166. if _, ok = f.Map[id]; !ok {
  167. sorted = append(sorted, id)
  168. }
  169. }
  170. for _, id := range idx[1:] {
  171. if _, ok = fs[id]; ok {
  172. sorted = append(sorted, id)
  173. }
  174. }
  175. update = true
  176. f.Sort = sorted
  177. }
  178. for _, id = range f.Sort {
  179. if fav, ok = fs[id]; ok {
  180. if !isSelf && !fav.IsPublic() {
  181. continue
  182. }
  183. res = append(res, fav)
  184. }
  185. }
  186. return
  187. }