File size: 4,032 Bytes
1e92f2d |
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 |
const { parse } = require('url');
const generateMetaInfo = require('../../shared/generate-meta-info');
// Don't even try if the path is /<value> any of these
// TODO: Longer, more complete deny list here
const PATH_DENY_LIST = ['robots.txt', 'home', 'messages', 'notifications'];
type Meta = {
title: string,
description: string,
extra: string,
};
export default (
url: string,
request: (query: string) => Promise
): Promise<Meta> => {
const { pathname = '' } = parse(url);
const [, first, second, third] = pathname.split('/');
if (third || first.length === 0) return Promise.resolve(generateMetaInfo());
let promise;
switch (first) {
case 'explore': {
promise = Promise.resolve(generateMetaInfo({ type: 'explore' }));
break;
}
case 'thread': {
/**
* Thread
*/
promise = request(/* GraphQL */ `
{
thread(id: "${second}") {
content {
title
body
}
type
channel {
name
isPrivate
}
community {
profilePhoto
}
}
}
`).then(res => {
const {
thread: { type, content, channel },
} = res.data;
return generateMetaInfo({
type: 'thread',
data: {
title: content.title,
type,
body: content.body,
channelName: channel.name,
privateChannel: channel.isPrivate,
image: community.profilePhoto,
},
});
});
break;
}
case 'users': {
/**
* User
*/
promise = request(/* GraphQL */ `
{
user(username: "${second}") {
name
username
description
profilePhoto
# coverPhoto
}
}
`).then(res => {
const { user } = res.data;
return generateMetaInfo({
type: 'user',
data: {
username: user.username,
description: user.description,
name: user.name,
image: user.profilePhoto,
},
});
});
break;
}
default: {
const isDenyListed = PATH_DENY_LIST.includes(first);
if (second && !isDenyListed) {
/**
* Channel
*/
promise = request(/* GraphQL */ `
{
channel(channelSlug: "${second}", communitySlug: "${first}") {
name
description
isPrivate
community {
name
profilePhoto
}
}
}
`).then(res => {
const {
channel,
channel: { community },
} = res.data;
return generateMetaInfo({
type: 'channel',
data: {
name: channel.name,
description: channel.description,
communityName: community.name,
private: channel.isPrivate,
image: community.profilePhoto,
},
});
});
} else if (!isDenyListed) {
/**
* Community
*/
promise = request(/* GraphQL */ `
{
community(slug: "${first}") {
name
description
profilePhoto
}
}
`).then(res => {
const { community } = res.data;
return generateMetaInfo({
type: 'community',
data: {
name: community.name,
description: community.description,
image: community.profilePhoto,
},
});
});
}
break;
}
}
if (!promise) return Promise.resolve(generateMetaInfo());
return promise.catch(err => {
console.error(`⚠️ Failed to load metadata for ${url}! ⚠️`);
console.error(err);
return generateMetaInfo();
});
};
|