Add current user ID to notifications' UserID
field (notification recient) when parsing
- also make an enum for notification types instead of magic numbers in the code
This commit is contained in:
parent
fae23cc243
commit
e94327b153
@ -7,7 +7,8 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (api API) GetNotifications(cursor string) (TweetResponse, error) {
|
||||
// TODO: pagination
|
||||
func (api *API) GetNotifications(cursor string) (TweetResponse, error) {
|
||||
url, err := url.Parse("https://api.twitter.com/2/notifications/all.json")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -23,7 +24,7 @@ func (api API) GetNotifications(cursor string) (TweetResponse, error) {
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (t *TweetResponse) ToTweetTroveAsNotifications() (TweetTrove, error) {
|
||||
func (t *TweetResponse) ToTweetTroveAsNotifications(current_user_id UserID) (TweetTrove, error) {
|
||||
ret, err := t.ToTweetTrove()
|
||||
if err != nil {
|
||||
return TweetTrove{}, err
|
||||
@ -45,11 +46,12 @@ func (t *TweetResponse) ToTweetTroveAsNotifications() (TweetTrove, error) {
|
||||
// Tweet entry (e.g., someone replied to you)
|
||||
notification = Notification{ID: NotificationID(notification_id)}
|
||||
}
|
||||
notification.UserID = current_user_id
|
||||
notification.SortIndex = entry.SortIndex
|
||||
if strings.Contains(entry.Content.Item.ClientEventInfo.Element, "replied") {
|
||||
notification.Type = 4
|
||||
notification.Type = NOTIFICATION_TYPE_REPLY
|
||||
} else if strings.Contains(entry.Content.Item.ClientEventInfo.Element, "recommended") {
|
||||
notification.Type = 11
|
||||
notification.Type = NOTIFICATION_TYPE_RECOMMENDED_POST
|
||||
}
|
||||
if entry.Content.Item.Content.Tweet.ID != 0 {
|
||||
notification.ActionTweetID = TweetID(entry.Content.Item.Content.Tweet.ID)
|
||||
@ -90,13 +92,13 @@ func ParseSingleNotification(n APINotification) Notification {
|
||||
// t.Entities.ReplyMentions = strings.TrimSpace(string([]rune(t.FullText)[0:t.DisplayTextRange[0]]))
|
||||
|
||||
if strings.HasSuffix(n.Message.Text, "followed you") {
|
||||
ret.Type = 5
|
||||
ret.Type = NOTIFICATION_TYPE_FOLLOW
|
||||
} else if strings.Contains(n.Message.Text, "liked") {
|
||||
ret.Type = 1
|
||||
ret.Type = NOTIFICATION_TYPE_LIKE
|
||||
} else if strings.Contains(n.Message.Text, "reposted") {
|
||||
ret.Type = 2
|
||||
ret.Type = NOTIFICATION_TYPE_RETWEET
|
||||
} else if strings.Contains(n.Message.Text, "There was a login to your account") {
|
||||
ret.Type = 9
|
||||
ret.Type = NOTIFICATION_TYPE_LOGIN
|
||||
}
|
||||
// TODO: more types?
|
||||
|
||||
|
@ -22,16 +22,19 @@ func TestParseNotificationsPage(t *testing.T) {
|
||||
err = json.Unmarshal(data, &resp)
|
||||
require.NoError(err)
|
||||
|
||||
tweet_trove, err := resp.ToTweetTroveAsNotifications()
|
||||
current_user_id := UserID(12345678)
|
||||
tweet_trove, err := resp.ToTweetTroveAsNotifications(current_user_id)
|
||||
require.NoError(err)
|
||||
|
||||
notif1, is_ok := tweet_trove.Notifications["FKncQJGVgAQAAAABSQ3bEYsN6BFN3re-ZsU"]
|
||||
assert.True(is_ok)
|
||||
assert.Equal(9, notif1.Type) // login
|
||||
assert.Equal(current_user_id, notif1.UserID)
|
||||
|
||||
notif2, is_ok := tweet_trove.Notifications["FKncQJGVgAQAAAABSQ3bEYsN6BFaOkNV8aw"]
|
||||
assert.True(is_ok)
|
||||
assert.Equal(2, notif2.Type) // retweet
|
||||
assert.Equal(current_user_id, notif2.UserID)
|
||||
assert.Equal(UserID(1458284524761075714), notif2.ActionUserID)
|
||||
assert.Equal(TweetID(1824915465275392037), notif2.ActionTweetID)
|
||||
assert.Equal(TimestampFromUnixMilli(1723928739342), notif2.SentAt)
|
||||
@ -39,21 +42,25 @@ func TestParseNotificationsPage(t *testing.T) {
|
||||
notif3, is_ok := tweet_trove.Notifications["FKncQJGVgAQAAAABSQ3bEYsN6BE-OY688aw"]
|
||||
assert.True(is_ok)
|
||||
assert.Equal(1, notif3.Type) // like
|
||||
assert.Equal(current_user_id, notif3.UserID)
|
||||
assert.Equal(UserID(1458284524761075714), notif3.ActionUserID)
|
||||
assert.Equal(TweetID(1824915465275392037), notif3.ActionTweetID)
|
||||
|
||||
notif4, is_ok := tweet_trove.Notifications["FKncQJGVgAQAAAABSQ3bEYsN6BGLlh8UIQs"]
|
||||
assert.True(is_ok)
|
||||
assert.Equal(11, notif4.Type) // recommended
|
||||
assert.Equal(current_user_id, notif4.UserID)
|
||||
|
||||
notif5, is_ok := tweet_trove.Notifications["FKncQJGVgAQAAAABSQ3bEYsN6BHS11EvITw"]
|
||||
assert.True(is_ok)
|
||||
assert.Equal(5, notif5.Type) // followed you
|
||||
assert.Equal(current_user_id, notif5.UserID)
|
||||
assert.Equal(UserID(28815778), notif5.ActionUserID)
|
||||
|
||||
notif6, is_ok := tweet_trove.Notifications["FKncQJGVgAQAAAABSQ3bEYsN6BE5ujkCepo"]
|
||||
assert.True(is_ok)
|
||||
assert.Equal(1, notif6.Type)
|
||||
assert.Equal(current_user_id, notif6.UserID)
|
||||
assert.Equal(UserID(1458284524761075714), notif6.ActionUserID)
|
||||
assert.Equal(TweetID(1826778617705115868), notif6.ActionTweetID)
|
||||
assert.Contains(notif6.UserIDs, UserID(1458284524761075714))
|
||||
|
@ -2,6 +2,22 @@ package scraper
|
||||
|
||||
type NotificationID string
|
||||
|
||||
type NotificationType int
|
||||
|
||||
const (
|
||||
NOTIFICATION_TYPE_LIKE = iota + 1
|
||||
NOTIFICATION_TYPE_RETWEET
|
||||
NOTIFICATION_TYPE_QUOTE_TWEET
|
||||
NOTIFICATION_TYPE_REPLY
|
||||
NOTIFICATION_TYPE_FOLLOW
|
||||
NOTIFICATION_TYPE_MENTION
|
||||
NOTIFICATION_TYPE_USER_IS_LIVE
|
||||
NOTIFICATION_TYPE_POLL_ENDED
|
||||
NOTIFICATION_TYPE_LOGIN
|
||||
NOTIFICATION_TYPE_COMMUNITY_PINNED_POST
|
||||
NOTIFICATION_TYPE_RECOMMENDED_POST
|
||||
)
|
||||
|
||||
type Notification struct {
|
||||
ID NotificationID
|
||||
Type int
|
||||
|
Loading…
x
Reference in New Issue
Block a user