-
Notifications
You must be signed in to change notification settings - Fork 253
/
postHelpers.js
103 lines (82 loc) · 2.75 KB
/
postHelpers.js
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
import _ from 'lodash';
import { getHtml } from '../components/Story/Body';
import { extractImageTags, extractLinks } from './parser';
import { categoryRegex } from './regexHelpers';
import { jsonParse } from './formatter';
import DMCA from '../../common/constants/dmca.json';
import whiteListedApps from './apps';
const appVersion = require('../../../package.json').version;
export const isPostDeleted = post => post.title === 'deleted' && post.body === 'deleted';
export const isPostTaggedNSFW = post => {
if (post.parent_permlink === 'nsfw') return true;
const postJSONMetaData = _.attempt(JSON.parse, post.json_metadata);
if (_.isError(postJSONMetaData)) return false;
return _.includes(postJSONMetaData.tags, 'nsfw');
};
export function dropCategory(url) {
return url.replace(categoryRegex, '');
}
/**
* Gets app data from a post.
* Only Returns app info from apps whitelisted in apps.json
* @param post
* @returns An empty object if app is not valid otherwise an object with {appName: String, version: String}
*/
export function getAppData(post) {
try {
const jsonMetadata = jsonParse(post.json_metadata);
const appDetails = _.get(jsonMetadata, 'app', '');
const appData = _.split(appDetails, '/');
const appKey = _.get(appData, 0, '');
const version = _.get(appData, 1, '');
if (whiteListedApps[appKey]) {
return {
appName: whiteListedApps[appKey],
version,
};
}
return {};
} catch (error) {
return {};
}
}
export const isBannedPost = post => {
const bannedAuthors = _.get(DMCA, 'authors', []);
const bannedPosts = _.get(DMCA, 'posts', []);
const postURL = `${post.author}/${post.permlink}`;
return _.includes(bannedAuthors, post.author) || _.includes(bannedPosts, postURL);
};
export function getContentImages(content, parsed = false) {
const parsedBody = parsed ? content : getHtml(content, {}, 'text');
return extractImageTags(parsedBody).map(tag =>
_.unescape(tag.src.replace('https://steemitimages.com/0x0/', '')),
);
}
export function createPostMetadata(body, tags, oldMetadata = {}) {
let metaData = {
community: 'busy',
app: `busy/${appVersion}`,
format: 'markdown',
};
metaData = {
...oldMetadata,
...metaData,
};
const users = [];
const userRegex = /@([a-zA-Z.0-9-]+)/g;
let matches;
// eslint-disable-next-line no-cond-assign
while ((matches = userRegex.exec(body))) {
if (users.indexOf(matches[1]) === -1) {
users.push(matches[1]);
}
}
const parsedBody = getHtml(body, {}, 'text');
const images = getContentImages(parsedBody, true);
const links = extractLinks(parsedBody);
metaData.tags = tags;
metaData.users = users;
metaData.links = links.slice(0, 10);
metaData.image = images;
return metaData;
}