Skip to content

Commit

Permalink
Fix pinned tweets
Browse files Browse the repository at this point in the history
(added to the beginning of the timeline)
Close imperatrona#23
Alexander Sheiko committed Jan 6, 2021
1 parent 142387a commit 1e04820
Showing 2 changed files with 29 additions and 2 deletions.
13 changes: 13 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
@@ -146,6 +146,19 @@ type (
} `json:"content,omitempty"`
} `json:"entries"`
} `json:"addEntries"`
PinEntry struct {
Entry struct {
Content struct {
Item struct {
Content struct {
Tweet struct {
ID string `json:"id"`
} `json:"tweet"`
} `json:"content"`
} `json:"item"`
} `json:"content"`
} `json:"entry"`
} `json:"pinEntry,omitempty"`
} `json:"instructions"`
} `json:"timeline"`
}
18 changes: 16 additions & 2 deletions util.go
Original file line number Diff line number Diff line change
@@ -84,8 +84,13 @@ func getTimeline(ctx context.Context, query string, maxTweetsNbr int, fetchFunc
}

if tweetsNbr < maxTweetsNbr {
if tweet.IsPin && nextCursor != "" {
continue
}
nextCursor = next
channel <- &Result{Tweet: *tweet}
} else {
break
}
tweetsNbr++
}
@@ -192,9 +197,15 @@ func parseTimeline(timeline *timeline) ([]*Tweet, string) {
}

var cursor string
var pinnedTweet *Tweet
var orderedTweets []*Tweet
if len(timeline.Timeline.Instructions) > 0 {
for _, entry := range timeline.Timeline.Instructions[0].AddEntries.Entries {
for _, instruction := range timeline.Timeline.Instructions {
if instruction.PinEntry.Entry.Content.Item.Content.Tweet.ID != "" {
if tweet, ok := tweets[instruction.PinEntry.Entry.Content.Item.Content.Tweet.ID]; ok {
pinnedTweet = &tweet
}
}
for _, entry := range instruction.AddEntries.Entries {
if tweet, ok := tweets[entry.Content.Item.Content.Tweet.ID]; ok {
orderedTweets = append(orderedTweets, &tweet)
}
@@ -203,5 +214,8 @@ func parseTimeline(timeline *timeline) ([]*Tweet, string) {
}
}
}
if pinnedTweet != nil && len(orderedTweets) > 0 {
orderedTweets = append([]*Tweet{pinnedTweet}, orderedTweets...)
}
return orderedTweets, cursor
}

0 comments on commit 1e04820

Please sign in to comment.