Added following/followers

This commit is contained in:
tdgmdev 2023-03-06 02:26:57 -05:00
parent c98f799d67
commit 84082ac014
3 changed files with 81 additions and 6 deletions

View file

@ -27,6 +27,8 @@
{/if}
<p>
{#if data.id > 0}
<Button clickFunc={() => { window.location.search = setLocation(window.location,'page',((data.id)-1)) }}>Previous page</Button>
{/if}
<Button clickFunc={() => { window.location.search = setLocation(window.location,'page',((data.id)+1)) }}>Next page</Button>
</p>

View file

@ -4,7 +4,8 @@ const AUTH_ACTIONS = [
'postCreate',
'fileCreate',
'vote',
'postDelete'
'postDelete',
'follow'
];
const LEGAL_SORTS = [
@ -38,6 +39,7 @@ async function initDb() {
await db.run('CREATE TABLE IF NOT EXISTS vote (id CHAR(64), username CHAR(64), type INTEGER)');
await db.run('CREATE TABLE IF NOT EXISTS user (username CHAR(64), followers INTEGER, following INTEGER, upvotes INTEGER, downvotes INTEGER, reputation REAL)');
await db.run('CREATE TABLE IF NOT EXISTS bio (username CHAR(64), content CHAR(10240), roles INTEGER)');
await db.run('CREATE TABLE IF NOT EXISTS follow (username CHAR(64), following CHAR(64))');
}
let backendProxy = async ({route, backendParams}) => {
@ -107,7 +109,7 @@ backend.register = async ({user, pass, pass2}) => {
passHash
])
await updateUser({user: user[0].username});
await updateUser({user: user});
return { success: 'Successfully created account.', location: '/'};
}
@ -191,7 +193,19 @@ backend.userGet = async ({user}) => {
return {'success': 'User does not exist.'}
}
return {data: posts[0]};
var following = await db.all('SELECT * from follow WHERE username = ?', [
user
]);
var followers = await db.all('SELECT * from follow WHERE following = ?', [
user
]);
if (!following) following = [];
if (!followers) followers = [];
return {data: posts[0], following, followers};
}
backend.userBio = async ({user}) => {
@ -306,6 +320,35 @@ backend.token = async ({cookies}) => {
return {data: existingAccounts[0].username};
}
backend.follow = async ({target, user}) => {
var isFollowing = await db.all('SELECT * FROM follow WHERE username = ? AND following = ?',[
user,
target
]);
if (isFollowing && isFollowing.length > 0) {
await db.run('DELETE FROM follow WHERE username = ? AND following = ?',[
user,
target
]);
} else {
await db.run('INSERT INTO follow (username, following) VALUES (?, ?)',[
user,
target
]);
}
var following = await db.all('SELECT * from follow WHERE username = ?', [
target
]);
var followers = await db.all('SELECT * from follow WHERE following = ?', [
target
]);
return {'success': 'User followed/unfollowed.', 'data': {following, followers}};
};
backend.fileCreate = async({img, extension,id, last }) => {
if (ridArray[id] !== '' && !(ridArray[id])) {
ridArray[id] = img;

View file

@ -8,6 +8,24 @@
let userData = data.postJsonUser.data;
let userBio = data.postJsonUserBio.data;
let following = data.postJsonUser.following;
let followers = data.postJsonUser.followers;
function follow() {
let fData = (new FormData());
fData.append('target',userData.username);
fetch('/api/follow', {
method: 'POST',
body: fData
}).then(async x => {
let xJson = (await x.json()).data;
following = xJson.following;
followers = xJson.followers;
})
}
</script>
{#if userData}
@ -30,9 +48,21 @@
{#if userBio && userBio.roles == 69}
<p><b>This user is an Owner.</b></p>
{/if}
<h2>Following</h2>
{#each following as user}
<a href='/user/{user}'>{user.following} </a>
{/each}
<h2>Followers</h2>
{#each followers as user}
<a href='/user/{user}'>{user.username} </a>
{/each}
</span>
<span slot="footer">
<Button clickFunc={follow}>
Follow
</Button>
</span>
</Area>
{:else}