-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tweet.go
189 lines (162 loc) · 5.08 KB
/
tweet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package graphql
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/lib/pq"
)
// A Tweet is an archived tweet.
type Tweet struct {
ID string `json:"id"`
Text string `json:"text"`
Hashtags []string `json:"hashtags"`
Symbols []string `json:"symbols"`
UserMentions []string `json:"user_mentions"`
Urls []*URI `json:"urls"`
ScreenName string `json:"screen_name"`
FavoriteCount int `json:"favorite_count"`
RetweetCount int `json:"retweet_count"`
Posted time.Time `json:"posted"`
}
// TwitterURL is a representation of data from cacophony.
type TwitterURL struct {
Link *URI
TweetIDs []string
CreatedAt time.Time
ModifiedAt time.Time
}
// Tweets returns an array of tweets.
func (tu *TwitterURL) Tweets(ctx context.Context) ([]*Tweet, error) {
tweets := make([]*Tweet, len(tu.TweetIDs))
for i, id := range tu.TweetIDs {
t, _ := GetTweet(ctx, id)
tweets[i] = t
}
return tweets, nil
}
// IsLinkable exists to show that this method implements the Linkable type in
// graphql.
func (tu *TwitterURL) IsLinkable() {}
// URI returns a link to this tweet.
func (tu *TwitterURL) URI() *URI {
return tu.Link
}
func (tu *TwitterURL) GetURI() URI {
return *tu.URI()
}
// Save inserts or updates a tweet into the database.
func (t *Tweet) Save(ctx context.Context) error {
if _, err := db.ExecContext(
ctx,
`
INSERT INTO tweets(id, text, hashtags, symbols, user_mentions, urls, screen_name, favorites, retweets, posted, created_at, modified_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $11)
ON CONFLICT (id) DO UPDATE
SET (text, hashtags, symbols, user_mentions, urls, screen_name, favorites, retweets, posted, modified_at) = ($2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
WHERE tweets.id = $1;
`,
t.ID,
t.Text,
pq.Array(t.Hashtags),
pq.Array(t.Symbols),
pq.Array(t.UserMentions),
pq.Array(t.Urls),
t.ScreenName,
t.FavoriteCount,
t.RetweetCount,
t.Posted,
time.Now(),
); err != nil {
return err
}
return nil
}
// IsLinkable exists to show that this method implements the Linkable type in
// graphql.
func (t *Tweet) IsLinkable() {}
// GetTweet returns a single tweet by id.
func GetTweet(ctx context.Context, id string) (*Tweet, error) {
var tweet Tweet
uris := []string{}
row := db.QueryRowContext(ctx, "SELECT id, text, hashtags, symbols, user_mentions, urls, screen_name, favorites, retweets, posted FROM tweets WHERE id = $1", id)
err := row.Scan(&tweet.ID, &tweet.Text, pq.Array(&tweet.Hashtags), pq.Array(&tweet.Symbols), pq.Array(&tweet.UserMentions), pq.Array(&uris), &tweet.ScreenName, &tweet.FavoriteCount, &tweet.RetweetCount, &tweet.Posted)
switch {
case err == sql.ErrNoRows:
return nil, fmt.Errorf("no tweet %s", id)
case err != nil:
return nil, fmt.Errorf("error with get: %w", err)
default:
for _, v := range uris {
tweet.Urls = append(tweet.Urls, NewURI(v))
}
return &tweet, nil
}
}
// GetTweets returns an array of tweets from the database.
func GetTweets(ctx context.Context, limit, offset int) ([]*Tweet, error) {
rows, err := db.QueryContext(ctx, "SELECT id, text, hashtags, symbols, user_mentions, urls, screen_name, favorites, retweets, posted FROM tweets ORDER BY posted DESC LIMIT $1 OFFSET $2", limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
tweets := make([]*Tweet, 0)
for rows.Next() {
uris := []string{}
tweet := new(Tweet)
err := rows.Scan(&tweet.ID, &tweet.Text, pq.Array(&tweet.Hashtags), pq.Array(&tweet.Symbols), pq.Array(&tweet.UserMentions), pq.Array(&uris), &tweet.ScreenName, &tweet.FavoriteCount, &tweet.RetweetCount, &tweet.Posted)
if err != nil {
return nil, err
}
for _, v := range uris {
tweet.Urls = append(tweet.Urls, NewURI(v))
}
tweets = append(tweets, tweet)
}
if err = rows.Err(); err != nil {
return nil, err
}
return tweets, nil
}
// URI returns a link to this tweet.
func (t *Tweet) URI() *URI {
return NewURI(fmt.Sprintf("https://twitter.com/%s/status/%s", t.ScreenName, t.ID))
}
func (t *Tweet) GetURI() URI {
return *t.URI()
}
// GetTweetsByScreenName returns an array of tweets from the database filtered by screenname.
func GetTweetsByScreenName(ctx context.Context, screenName string, limit, offset int) ([]*Tweet, error) {
rows, err := db.QueryContext(ctx, "SELECT id, text, hashtags, symbols, user_mentions, urls, screen_name, favorites, retweets, posted FROM tweets WHERE screen_name = $3 ORDER BY posted DESC LIMIT $1 OFFSET $2", limit, offset, screenName)
if err != nil {
return nil, err
}
defer rows.Close()
var tweets []*Tweet
for rows.Next() {
var uris []string
tweet := new(Tweet)
if err := rows.Scan(
&tweet.ID,
&tweet.Text,
pq.Array(&tweet.Hashtags),
pq.Array(&tweet.Symbols),
pq.Array(&tweet.UserMentions),
pq.Array(&uris),
&tweet.ScreenName,
&tweet.FavoriteCount,
&tweet.RetweetCount,
&tweet.Posted,
); err != nil {
return nil, err
}
for _, v := range uris {
tweet.Urls = append(tweet.Urls, NewURI(v))
}
tweets = append(tweets, tweet)
}
if err = rows.Err(); err != nil {
return nil, err
}
return tweets, nil
}