|
| 1 | +let allUsers = [] |
| 2 | +let allContent = [] |
| 3 | +let converter = new showdown.Converter({ tables: true }) |
| 4 | + |
| 5 | +function getTrending(query, initial){ |
| 6 | + steem.api.getDiscussionsByTrending(query, (err, result) => { |
| 7 | + if (err === null) { |
| 8 | + displayContent(result,initial) |
| 9 | + getaccounts(result.map(post => post.author)) |
| 10 | + } else { |
| 11 | + console.log(err); |
| 12 | + } |
| 13 | + }); |
| 14 | +} |
1 | 15 |
|
2 | | -// EXAMPLE - FEED PAGE |
3 | | -if ($('main').hasClass('feed') ) { |
4 | | - let postInsert = '.feed-insert' |
5 | | - let feedType = $('main.feed').data('feed-type') |
6 | | - console.log(feedType) |
7 | | - steem.api.setOptions({ url: 'wss://rpc.buildteam.io' }); |
8 | | - steem.api.getState(`/${feedType}/`, (err, result) => { |
9 | | - |
10 | | - let resultsArray = []; |
11 | | - |
12 | | - for ( post in result.content ){ |
13 | | - |
14 | | - var converter = new showdown.Converter() |
15 | | - var html = converter.makeHtml(result.content[post].body) |
16 | | - var placeholder = document.createElement('div'); |
17 | | - placeholder.innerHTML = html; |
18 | | - var image = placeholder.querySelector('img'); |
19 | | - var plainText = placeholder.textContent || placeholder.innerText || ""; |
20 | | - |
21 | | - resultsArray.push({ |
22 | | - id: result.content[post].id, |
23 | | - title: result.content[post].title, |
24 | | - author: result.content[post].author, |
25 | | - body: plainText, |
26 | | - permlink: result.content[post].permlink, |
27 | | - image: image ? image.getAttribute('src') : '' |
28 | | - }) |
| 16 | +function getLatest(query, initial){ |
| 17 | + |
| 18 | + steem.api.getDiscussionsByCreated(query, (err, result) => { |
| 19 | + if (err === null) { |
| 20 | + displayContent(result, initial) |
| 21 | + getaccounts(result.map(post => post.author)) |
| 22 | + } else { |
| 23 | + console.log(err); |
| 24 | + } |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +function getMoreContent(filter, tag){ |
| 29 | + let lastItem = allContent[allContent.length - 1] |
| 30 | + let query = { |
| 31 | + 'tag': |
| 32 | + tag, |
| 33 | + 'limit': 24, |
| 34 | + start_author: lastItem.author, |
| 35 | + start_permlink: lastItem.permlink } |
| 36 | + |
| 37 | + if(filter === 'trending'){ |
| 38 | + getTrending(query, false) |
| 39 | + } else { |
| 40 | + getLatest(query, false) |
29 | 41 | } |
| 42 | +} |
30 | 43 |
|
31 | | - resultsArray = resultsArray.sort((a,b) => { |
32 | | - return b.id - a.id |
33 | | - }); |
34 | | - |
35 | | - resultsArray.forEach( (post, i, arr) => { |
36 | | - let template = ` |
37 | | - <div class="col-md-4" data-post-id="${post.id}"> |
38 | | - <div class="card mb-4 box-shadow"> |
39 | | - <img class="card-img-top" data-src="${post.image}" alt="Thumbnail [100%x225]" style="height: 225px; width: 100%; display: block;" src="${post.image}" data-holder-rendered="true"> |
40 | | - <div class="card-body"> |
41 | | - <h5 class="card-title">${post.title}</h5> |
42 | | - <p class="card-text">${ (post.body).substring(0, 150) }..</p> |
43 | | - <div class="d-flex justify-content-between align-items-center"> |
44 | | - <div class="btn-group"> |
45 | | - <form method="post"> |
46 | | - <input type="hidden" name="postId" value="${post.id}"> |
47 | | - <input type="hidden" name="author" value="${post.author}"> |
48 | | - <input type="hidden" name="permlink" value="${post.permlink}"> |
49 | | - <button type="button" class="btn btn-sm btn-outline-secondary vote">Vote</button> |
50 | | - </form> |
51 | | - <button type="button" class="btn btn-sm btn-outline-secondary">Edit</button> |
52 | | - </div> |
53 | | - </div> |
54 | | - </div> |
55 | | - </div> |
56 | | - </div> |
57 | | - ` |
58 | | - $(postInsert).append(template) |
59 | | - }) |
60 | | - }); |
61 | | - |
62 | | - $('main').on('click', '.vote',(e) => { |
63 | | - e.preventDefault() |
64 | | - $.post({ |
65 | | - url: '/post/vote', |
66 | | - dataType: 'json', |
67 | | - data: $(e.currentTarget).parent().serialize() |
68 | | - }, (response) => { |
69 | | - if (response.error) { |
70 | | - $(`*[data-post-id="${response.id}"]`).children().last().after() |
71 | | - .append(`<span>${response.error.error_description}</span>`) |
72 | | - } else { |
73 | | - $(`*[data-post-id="${response.id}"]`).children().last().after() |
74 | | - .append('<span> Voted!</span>') |
75 | | - } |
| 44 | +function displayContent(result, initial){ |
| 45 | + if (!initial) result.shift() |
| 46 | + for (let i = 0; i < result.length ; i++) { |
| 47 | + let post = result[i]; |
| 48 | + allContent.push(post) |
| 49 | + |
| 50 | + var urlRegex = /(https?:\/\/[^\s]+)/g; |
| 51 | + post.body = post.body.replace(urlRegex, (url) => { |
| 52 | + let last = url.slice(-3) |
| 53 | + if ( last === 'jpg' || last === 'png' || last === 'jpe' || last === 'gif' ) { |
| 54 | + return '<img src="' + url + '">'; |
| 55 | + } else { return url } |
76 | 56 | }) |
77 | 57 |
|
78 | | - }) |
| 58 | + if( typeof JSON.parse(post.json_metadata).image === 'undefined' ){ |
| 59 | + image = genImageInHTML(post.body) |
| 60 | + } else { |
| 61 | + image = JSON.parse(post.json_metadata).image[0] |
| 62 | + } |
| 63 | + |
| 64 | + let itemTemplate = ` |
| 65 | + <div class="item " data-url="${post.url}" data-permlink="${ post.permlink }"> |
| 66 | + <img class="item__image " src="https://steemitimages.com/480x768/${image}" onerror=""> |
| 67 | + <div class="item__author"> |
| 68 | + <h1>${post.title}</h1> |
| 69 | + <span>@${post.author}</span> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + ` |
| 73 | + $('.feed-insert').append(itemTemplate) |
| 74 | + } |
79 | 75 | } |
80 | 76 |
|
| 77 | +function getaccounts(usernames){ |
| 78 | + steem.api.getAccounts(usernames, (err, result) => { |
| 79 | + allUsers = allUsers.concat(result) |
| 80 | + }) |
| 81 | +} |
| 82 | + |
| 83 | +function genImageInHTML(markdown){ |
| 84 | + let placeholder = document.createElement('div'); |
| 85 | + placeholder.innerHTML = converter.makeHtml(markdown) |
| 86 | + let image = placeholder.querySelector('img') ; |
| 87 | + if (image) { |
| 88 | + return image.src |
| 89 | + } else { |
| 90 | + return false |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// EXAMPLE - FEED PAGE |
| 95 | +if ($('main').hasClass('feed') ) { |
| 96 | + |
| 97 | + let feedType = $('main.feed').data('feed-type') |
| 98 | + |
| 99 | + if(feedType === 'trending'){ |
| 100 | + getTrending({'limit': 20 }) |
| 101 | + } else { |
| 102 | + getLatest({'limit': 20 }) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | + |
81 | 108 | if ($('main').hasClass('profile') ) { |
82 | 109 | let username = $('.profile').data('username') |
83 | 110 |
|
84 | | - steem.api.setOptions({ url: 'wss://rpc.buildteam.io' }); |
85 | 111 |
|
86 | 112 | let totalVestingShares, totalVestingFundSteem; |
87 | 113 |
|
@@ -128,7 +154,7 @@ if ($('main').hasClass('profile') ) { |
128 | 154 | } |
129 | 155 |
|
130 | 156 |
|
131 | | - steem.api.getFollowCount(user.name, function(err, result){ |
| 157 | + steem.api.getFollowCount(user.name, function(err, result){ |
132 | 158 | data.followerCount = result.follower_count |
133 | 159 | data.followingCount = result.following_count |
134 | 160 | }) |
|
0 commit comments