video.go 4.5 KB

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