-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.module
286 lines (256 loc) · 7.78 KB
/
twitter.module
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* @file
* Provides API integration with the Twitter microblogging service.
*/
define('TWITTER_HOMEPAGE', 'http://twitter.com');
define('TWITTER_SEARCH', 'http://twitter.com/search');
/**
* Implements hook_menu().
*/
function twitter_menu() {
$items['twitter/oauth'] = array(
'title' => 'Twitter',
'access callback' => TRUE,
'page callback' => 'drupal_get_form',
'page arguments' => array('twitter_oauth_callback'),
'type' => MENU_CALLBACK,
'file' => 'twitter.pages.inc',
);
$items['admin/config/services/twitter'] = array(
'title' => 'Twitter',
'description' => 'Configure integration with Twitter (and compatible) API services.',
'page callback' => 'drupal_get_form',
'page arguments' => array('twitter_admin_form'),
'access arguments' => array('administer site configuration'),
'file' => 'twitter.pages.inc',
);
$items['admin/config/services/twitter/default'] = array(
'title' => 'Twitter',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['user/%user_category/edit/twitter'] = array(
'title' => 'Twitter accounts',
'page callback' => 'twitter_user_settings',
'page arguments' => array(1),
'access callback' => 'twitter_edit_access',
'access arguments' => array(1),
'load arguments' => array('%map', '%index'),
'weight' => 10,
'file' => 'twitter.pages.inc',
'type' => MENU_LOCAL_TASK,
);
$items['user/%user/edit/twitter/global/%'] = array(
'title' => 'Twitter accounts',
'page callback' => 'drupal_get_form',
'page arguments' => array('twitter_user_make_global', 1, 5),
'access arguments' => array('make twitter accounts global'),
'file' => 'twitter.pages.inc',
);
return $items;
}
/**
* Access callback for twitter account editing.
*/
function twitter_edit_access($account) {
return user_edit_access($account) && user_access('add twitter accounts');
}
/**
* Implements hook_permission().
*/
function twitter_permission() {
return array(
'add twitter accounts' => array(
'title' => t('Add Twitter accounts'),
),
'use global twitter account' => array(
'title' => t('Use the site global Twitter account'),
),
'make twitter accounts global' => array(
'title' => t('Assign a Twitter account as the site global account.'),
),
'import own tweets' => array(
'title' => t('Import own tweets to the site.'),
),
);
}
/**
* Implements hook_user_categories().
*/
function twitter_user_categories() {
return array(
array(
'name' => 'twitter',
'title' => t('Twitter accounts'),
'weight' => 3,
),
);
}
/**
* Implements hook_theme().
*/
function twitter_theme() {
return array(
'twitter_account_list_form' => array(
'render element' => 'form',
),
);
}
/**
* Very lightweight helper function to generate a TinyURL for a given post.
*/
function twitter_shorten_url($url) {
if (module_exists('shorten')) {
return shorten_url($url);
}
else {
$response = drupal_http_request("http://tinyurl.com/api-create.php?url=" . $url);
if ($response->code == 200) {
return $response->data;
}
else {
return $url;
}
}
}
/**
* Implements hook_cron().
*
* Imports new Twitter statuses for site users, and deletes expired tweets.
*/
function twitter_cron() {
if (!variable_get('twitter_import', TRUE)) {
return;
}
module_load_include('inc', 'twitter');
// Pull up a list of Twitter accounts that are flagged for updating,
// sorted by how long it's been since we last updated them. This ensures
// that the most out-of-date accounts get updated first.
$result = db_query_range("SELECT twitter_uid FROM {twitter_account} WHERE import = :import ORDER BY last_refresh ASC", 0, 20, array(':import' => 1));
foreach ($result as $account) {
twitter_fetch_user_timeline($account->twitter_uid);
}
// Nuke old statuses.
if ($age = variable_get('twitter_expire', 0)) {
db_delete('twitter')
->condition('created_time', REQUEST_TIME - $age, '<')
->execute();
}
}
/**
* Implements hook_filter_info().
*/
function twitter_filter_info() {
$filters['twitter_username'] = array(
'title' => t('Twitter @username converter'),
'description' => t('Converts Twitter-style @usernames into links to Twitter account pages.'),
'process callback' => '_twitter_filter_username',
'tips callback' => '_twitter_filter_tip_username',
);
$filters['twitter_hashtag'] = array(
'title' => t('Twitter #hashtag converter'),
'description' => t('Converts Twitter-style #hashtags into links to hashtags.org.'),
'process callback' => '_twitter_filter_hashtag',
'tips callback' => '_twitter_filter_tip_hashtag',
);
return $filters;
}
/**
* Filter tips callback function for $filters[0] in hook_filter_info().
*/
function _twitter_filter_tip_username($filter, $format, $long = FALSE) {
return t('Twitter-style @usernames are linked to their Twitter account pages.');
}
/**
* Filter tips callback function for $filters[1] in hook_filter_info().
*/
function _twitter_filter_tip_hashtag($format, $long = FALSE) {
return t('Twitter-style #hashtags are linked to !url.', array('!url' => '<a href="http://search.twitter.com/">search.twitter.com</a>'));
}
/**
* Callback for twitter @username converter
*/
function _twitter_filter_username($text, $filter) {
$prefix = '@';
$destination = TWITTER_HOMEPAGE;
return _twitter_filter_text($text, $prefix, $destination);
}
/**
* Callback for twitter #hashtag converter
*/
function _twitter_filter_hashtag($text, $filter) {
$prefix = '#';
$destination = TWITTER_SEARCH;
return _twitter_filter_text($text, $prefix, $destination);
}
/**
* This helper function converts Twitter-style @usernames and #hashtags into
* actual links.
*/
function _twitter_filter_text($text, $prefix, $destination) {
$matches = array(
'/\>' . $prefix . '([a-z0-9_]+)/i',
'/^' . $prefix . '([a-z0-9_]+)/i',
'/(\s+)' . $prefix . '([a-z0-9_]+)/i',
);
$replacements = array(
'><a href="' . $destination . '/${1}">' . $prefix . '${1}</a>',
'<a href="' . $destination . '/${1}">' . $prefix . '${1}</a>',
'${1}<a href="' . $destination . '/${2}">' . $prefix . '${2}</a>',
);
return preg_replace($matches, $replacements, $text);
}
/**
/**
* Implements hook_user_load().
*/
function twitter_user_load($accounts) {
foreach ($accounts as $uid => $account) {
$accounts[$uid]->twitter_accounts = module_invoke_all('twitter_accounts', $account);
}
}
/**
* An implementation of hook_twitter_accounts. We want to move this into a
* separate module eventually, but sticking the code here and using a hook
* lets other modules solve the 'what accounts can a user post with' problem
* in cleaner ways.
*/
function twitter_twitter_accounts($account) {
module_load_include('inc', 'twitter');
$query = db_select('twitter_account', 'ta')
->fields('ta', array('twitter_uid'));
if (user_access('use global twitter account')) {
$or = db_or();
$or->condition('ta.uid', $account->uid);
$or->condition('ta.is_global', 1);
$query->condition($or);
}
else {
$query->condition('ta.uid', $account->uid);
}
$twitter_accounts = array();
foreach ($query->execute()->fetchCol() as $twitter_uid) {
$twitter_accounts[] = twitter_account_load($twitter_uid);
}
return $twitter_accounts;
}
/**
* Detect whether we should use oauth. This can probably just go now :)
*/
function _twitter_use_oauth() {
return module_exists('oauth_common') && variable_get('twitter_consumer_key', '') && variable_get('twitter_consumer_secret', '');
}
/**
* Implements hook_views_api().
*/
function twitter_views_api() {
return array('api' => 2);
}
/**
* Implements hook_admin_paths_alter().
*
* OAuth callback disagrees with overlay.
*/
function twitter_admin_paths_alter(&$paths) {
$paths['user/*/edit/twitter'] = FALSE;
}