@@ -81,6 +81,7 @@ const PARAMS__RETURN_URL = 'return_url';
8181const PARAMS__SCOPE = 'scope' ;
8282const PARAMS__SEARCH_TYPE = 'search_type' ;
8383const PARAMS__TEXT = 'text' ;
84+ const PARAMS__USERNAME = 'username' ;
8485
8586// Read variables from environment
8687require ( 'dotenv' ) . config ( ) ;
@@ -118,6 +119,7 @@ const SCOPES = [
118119 'threads_manage_mentions' ,
119120 'threads_delete' ,
120121 'threads_location_tagging' ,
122+ 'threads_profile_discovery' ,
121123] ;
122124
123125app . use ( express . static ( 'public' ) ) ;
@@ -898,6 +900,99 @@ app.get('/oEmbed', async (req, res) => {
898900 } ) ;
899901} ) ;
900902
903+ app . get ( '/profileLookup' , async ( req , res ) => {
904+ const { username, before, after, limit } = req . query ;
905+ if ( ! username ) {
906+ return res . render ( 'profile_lookup' , {
907+ title : 'Profile Lookup' ,
908+ } ) ;
909+ }
910+
911+ const params = {
912+ [ PARAMS__USERNAME ] : username ,
913+ }
914+
915+ const profileLookupUrl = buildGraphAPIURL ( `profile_lookup` , params , req . session . access_token ) ;
916+
917+ let response = { } ;
918+ try {
919+ response = await axios . get ( profileLookupUrl , { httpsAgent : agent } ) ;
920+ } catch ( e ) {
921+ console . error ( e ?. response ?. data ?. error ?. message ?? e . message ) ;
922+ }
923+
924+ const displayName = response . data ?. name ;
925+ const profilePictureUrl = response . data ?. profile_picture_url ;
926+ const isVerified = response . data ?. is_verified ;
927+ const bio = response . data ?. biography ;
928+ const formatCount = count => count ? count . toLocaleString ( 'en-US' ) : undefined ;
929+ const followerCount = formatCount ( response . data ?. follower_count ) ;
930+ const likesCount = formatCount ( response . data ?. likes_count ) ;
931+ const quotesCount = formatCount ( response . data ?. quotes_count ) ;
932+ const repliesCount = formatCount ( response . data ?. replies_count ) ;
933+ const repostsCount = formatCount ( response . data ?. reposts_count ) ;
934+ const viewsCount = formatCount ( response . data ?. views_count ) ;
935+
936+ const profilePostsParams = {
937+ [ PARAMS__FIELDS ] : [
938+ FIELD__TEXT ,
939+ FIELD__MEDIA_TYPE ,
940+ FIELD__MEDIA_URL ,
941+ FIELD__PERMALINK ,
942+ FIELD__TIMESTAMP ,
943+ ] . join ( ',' ) ,
944+ limit : limit ?? DEFAULT_THREADS_QUERY_LIMIT ,
945+ [ PARAMS__USERNAME ] : username ,
946+ } ;
947+ if ( before ) {
948+ profilePostsParams . before = before ;
949+ }
950+ if ( after ) {
951+ profilePostsParams . after = after ;
952+ }
953+
954+ let threads = [ ] ;
955+ let paging = { } ;
956+
957+ const queryThreadsUrl = buildGraphAPIURL ( `profile_posts` , profilePostsParams , req . session . access_token ) ;
958+
959+ try {
960+ const queryResponse = await axios . get ( queryThreadsUrl , { httpsAgent : agent } ) ;
961+ threads = queryResponse . data . data ;
962+
963+ if ( queryResponse . data . paging ) {
964+ const { next, previous } = queryResponse . data . paging ;
965+
966+ if ( next ) {
967+ paging . nextUrl = getCursorUrlFromGraphApiPagingUrl ( req , next ) ;
968+ }
969+
970+ if ( previous ) {
971+ paging . previousUrl = getCursorUrlFromGraphApiPagingUrl ( req , previous ) ;
972+ }
973+ }
974+ } catch ( e ) {
975+ console . error ( e ?. response ?. data ?. error ?. message ?? e . message ) ;
976+ }
977+
978+ return res . render ( 'profile_lookup' , {
979+ title : 'Profile Lookup for @' . concat ( username ) ,
980+ username,
981+ displayName,
982+ profilePictureUrl,
983+ isVerified,
984+ bio,
985+ followerCount,
986+ likesCount,
987+ quotesCount,
988+ repliesCount,
989+ repostsCount,
990+ viewsCount,
991+ paging,
992+ threads,
993+ } ) ;
994+ } ) ;
995+
901996https
902997 . createServer ( {
903998 key : fs . readFileSync ( path . join ( __dirname , '../' + HOST + '-key.pem' ) ) ,
@@ -1013,6 +1108,7 @@ function getCursorUrlFromGraphApiPagingUrl(req, graphApiPagingUrl) {
10131108 setUrlParamIfPresent ( graphUrl , cursorUrl , 'limit' ) ;
10141109 setUrlParamIfPresent ( graphUrl , cursorUrl , 'before' ) ;
10151110 setUrlParamIfPresent ( graphUrl , cursorUrl , 'after' ) ;
1111+ setUrlParamIfPresent ( graphUrl , cursorUrl , 'username' ) ;
10161112
10171113 return cursorUrl . href ;
10181114}
0 commit comments