following.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package model
  2. var (
  3. _emptyFollowings = make([]*Following, 0)
  4. )
  5. // Black get if black.
  6. func (f *Following) Black() bool {
  7. return AttrBlack == Attr(f.Attribute)
  8. }
  9. // Friend get if both way following.
  10. func (f *Following) Friend() bool {
  11. return AttrFriend == Attr(f.Attribute)
  12. }
  13. // Following get if following.
  14. func (f *Following) Following() bool {
  15. return AttrFollowing == Attr(f.Attribute) || Attr(f.Attribute) == AttrFriend
  16. }
  17. // Whisper get if whisper.
  18. func (f *Following) Whisper() bool {
  19. return AttrWhisper == Attr(f.Attribute)
  20. }
  21. // Filter filter followings by the given attribute.
  22. func Filter(fs []*Following, attr uint32) (res []*Following) {
  23. for _, f := range fs {
  24. // NOTE: if current attribute evaluated by Attr() matched, then continue,
  25. // this includes the situation that matches black, friend, whisper, and no-relation directly.
  26. // Now we have following to deal with, since we know that the attribute friend
  27. // can either do not exist or exists with following at the same time,
  28. // to deal with this situation, we need to filter for items which have 1 on the bit that attr stands for,
  29. // and especially, the attribute it self cannot be black because the attribute black has the highest priority,
  30. // when it exists, it shadows other bits, including friend, following, whisper, no-relation,
  31. // there is no need to do further calculate,
  32. // more specifically, black when black included, the value of f.Attribute&attr may greater than 0
  33. // when f.Attribute is 128+2 or 128+1 and the corresponding attr is 2 or 1,
  34. // which is not as we expected.
  35. if f.Attribute == 4 {
  36. f.Attribute = 6
  37. }
  38. if (Attr(f.Attribute) == attr) || (!f.Black() && f.Attribute&attr > 0) {
  39. res = append(res, f)
  40. }
  41. }
  42. if len(res) == 0 {
  43. res = _emptyFollowings
  44. }
  45. return
  46. }
  47. // SortFollowings sort followings by the mtime desc.
  48. type SortFollowings []*Following
  49. func (fs SortFollowings) Len() int {
  50. return len(fs)
  51. }
  52. func (fs SortFollowings) Swap(i, j int) {
  53. fs[i], fs[j] = fs[j], fs[i]
  54. }
  55. func (fs SortFollowings) Less(i, j int) bool {
  56. if fs[i].MTime == fs[j].MTime {
  57. return fs[i].Mid < fs[j].Mid
  58. }
  59. return fs[i].MTime.Time().After(fs[j].MTime.Time())
  60. }