Skip to content

Commit 4293ce8

Browse files
committed
Profile Posts
1 parent 072a32d commit 4293ce8

2 files changed

Lines changed: 90 additions & 6 deletions

File tree

src/index.js

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -901,15 +901,15 @@ app.get('/oEmbed', async (req, res) => {
901901
});
902902

903903
app.get('/profileLookup', async (req, res) => {
904-
const { profile } = req.query;
905-
if (!profile) {
904+
const { username, before, after, limit } = req.query;
905+
if (!username) {
906906
return res.render('profile_lookup', {
907907
title: 'Profile Lookup',
908908
});
909909
}
910910

911911
const params = {
912-
[PARAMS__USERNAME]: profile,
912+
[PARAMS__USERNAME]: username,
913913
}
914914

915915
const profileLookupUrl = buildGraphAPIURL(`profile_lookup`, params, req.session.access_token);
@@ -921,7 +921,6 @@ app.get('/profileLookup', async (req, res) => {
921921
console.error(e?.response?.data?.error?.message ?? e.message);
922922
}
923923

924-
const username = response.data?.username;
925924
const displayName = response.data?.name;
926925
const profilePictureUrl = response.data?.profile_picture_url;
927926
const isVerified = response.data?.is_verified;
@@ -934,8 +933,50 @@ app.get('/profileLookup', async (req, res) => {
934933
const repostsCount = formatCount(response.data?.reposts_count);
935934
const viewsCount = formatCount(response.data?.views_count);
936935

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+
937978
return res.render('profile_lookup', {
938-
title: 'Profile Lookup for @'.concat(profile),
979+
title: 'Profile Lookup for @'.concat(username),
939980
username,
940981
displayName,
941982
profilePictureUrl,
@@ -947,6 +988,8 @@ app.get('/profileLookup', async (req, res) => {
947988
repliesCount,
948989
repostsCount,
949990
viewsCount,
991+
paging,
992+
threads,
950993
});
951994
});
952995

@@ -1065,6 +1108,7 @@ function getCursorUrlFromGraphApiPagingUrl(req, graphApiPagingUrl) {
10651108
setUrlParamIfPresent(graphUrl, cursorUrl, 'limit');
10661109
setUrlParamIfPresent(graphUrl, cursorUrl, 'before');
10671110
setUrlParamIfPresent(graphUrl, cursorUrl, 'after');
1111+
setUrlParamIfPresent(graphUrl, cursorUrl, 'username');
10681112

10691113
return cursorUrl.href;
10701114
}

views/profile_lookup.pug

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ block content
2323
a(href='https://www.threads.net/@facebook') @facebook
2424

2525
form.profileLookup(action='/profileLookup' id='form' method='GET')
26-
input(type='text' placeholder='Enter the username of a Threads profile to lookup' name='profile' autocomplete='off')
26+
input(type='text' placeholder='Enter the username of a Threads profile to lookup' name='username' autocomplete='off')
2727

2828
input(type='submit' value='Search')
2929

30+
br
31+
br
32+
3033
if username
3134
div
3235
.pictures
@@ -36,6 +39,8 @@ block content
3639
if isVerified
3740
img(src='/img/verified.png' alt='Verified Badge' width=20 height=20)
3841

42+
br
43+
3944
table
4045
tbody
4146
tr
@@ -65,3 +70,38 @@ block content
6570
tr
6671
th(colspan=2) Views
6772
td(colspan=2)=viewsCount
73+
74+
br
75+
br
76+
77+
table.threads-list
78+
thead
79+
tr
80+
th ID
81+
th Created On
82+
th Media Type
83+
th Text
84+
th Permalink
85+
th Media URL
86+
tbody
87+
each thread in threads
88+
tr.threads-list-item
89+
td.thread-id
90+
a(href=`/threads/${thread.id}`)=thread.id
91+
td.thread-timestamp=thread.timestamp
92+
td.thread-type=thread.media_type
93+
td.thread-text=thread.text
94+
td.thread-permalink
95+
a(href=thread.permalink target='_blank') View on Threads
96+
td.thread-media-url
97+
if thread.media_url
98+
a(href=thread.media_url target='_blank') View Media File
99+
100+
div.paging
101+
if paging.nextUrl
102+
div.paging-next
103+
a(href=paging.nextUrl) Next
104+
105+
if paging.previousUrl
106+
div.paging-previous
107+
a(href=paging.previousUrl) Previous

0 commit comments

Comments
 (0)