stream.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package googlereader // import "miniflux.app/v2/internal/googlereader"
  4. import (
  5. "fmt"
  6. "strings"
  7. )
  8. type StreamType int
  9. const (
  10. // NoStream - no stream type
  11. NoStream StreamType = iota
  12. // ReadStream - read stream type
  13. ReadStream
  14. // StarredStream - starred stream type
  15. StarredStream
  16. // ReadingListStream - reading list stream type
  17. ReadingListStream
  18. // KeptUnreadStream - kept unread stream type
  19. KeptUnreadStream
  20. // BroadcastStream - broadcast stream type
  21. BroadcastStream
  22. // BroadcastFriendsStream - broadcast friends stream type
  23. BroadcastFriendsStream
  24. // LabelStream - label stream type
  25. LabelStream
  26. // FeedStream - feed stream type
  27. FeedStream
  28. // LikeStream - like stream type
  29. LikeStream
  30. )
  31. // Stream defines a stream type and its ID.
  32. type Stream struct {
  33. Type StreamType
  34. ID string
  35. }
  36. func (s Stream) String() string {
  37. return fmt.Sprintf("%v - '%s'", s.Type, s.ID)
  38. }
  39. func (st StreamType) String() string {
  40. switch st {
  41. case NoStream:
  42. return "NoStream"
  43. case ReadStream:
  44. return "ReadStream"
  45. case StarredStream:
  46. return "StarredStream"
  47. case ReadingListStream:
  48. return "ReadingListStream"
  49. case KeptUnreadStream:
  50. return "KeptUnreadStream"
  51. case BroadcastStream:
  52. return "BroadcastStream"
  53. case BroadcastFriendsStream:
  54. return "BroadcastFriendsStream"
  55. case LabelStream:
  56. return "LabelStream"
  57. case FeedStream:
  58. return "FeedStream"
  59. case LikeStream:
  60. return "LikeStream"
  61. default:
  62. return st.String()
  63. }
  64. }
  65. func getStream(streamID string, userID int64) (Stream, error) {
  66. switch {
  67. case strings.HasPrefix(streamID, feedPrefix):
  68. return Stream{Type: FeedStream, ID: strings.TrimPrefix(streamID, feedPrefix)}, nil
  69. case strings.HasPrefix(streamID, fmt.Sprintf(userStreamPrefix, userID)),
  70. strings.HasPrefix(streamID, streamPrefix):
  71. id := strings.TrimPrefix(streamID, fmt.Sprintf(userStreamPrefix, userID))
  72. id = strings.TrimPrefix(id, streamPrefix)
  73. switch id {
  74. case read:
  75. return Stream{ReadStream, ""}, nil
  76. case starred:
  77. return Stream{StarredStream, ""}, nil
  78. case readingList:
  79. return Stream{ReadingListStream, ""}, nil
  80. case keptUnread:
  81. return Stream{KeptUnreadStream, ""}, nil
  82. case broadcast:
  83. return Stream{BroadcastStream, ""}, nil
  84. case broadcastFriends:
  85. return Stream{BroadcastFriendsStream, ""}, nil
  86. case like:
  87. return Stream{LikeStream, ""}, nil
  88. default:
  89. return Stream{NoStream, ""}, fmt.Errorf("googlereader: unknown stream with id: %s", id)
  90. }
  91. case strings.HasPrefix(streamID, fmt.Sprintf(userLabelPrefix, userID)),
  92. strings.HasPrefix(streamID, labelPrefix):
  93. id := strings.TrimPrefix(streamID, fmt.Sprintf(userLabelPrefix, userID))
  94. id = strings.TrimPrefix(id, labelPrefix)
  95. return Stream{LabelStream, id}, nil
  96. case streamID == "":
  97. return Stream{NoStream, ""}, nil
  98. default:
  99. return Stream{NoStream, ""}, fmt.Errorf("googlereader: unknown stream type: %s", streamID)
  100. }
  101. }
  102. func getStreams(streamIDs []string, userID int64) ([]Stream, error) {
  103. streams := make([]Stream, 0, len(streamIDs))
  104. for _, streamID := range streamIDs {
  105. stream, err := getStream(streamID, userID)
  106. if err != nil {
  107. return []Stream{}, err
  108. }
  109. streams = append(streams, stream)
  110. }
  111. return streams, nil
  112. }