messages + post formatting

This commit is contained in:
tdgmdev 2023-03-11 12:51:19 -05:00
parent 4c75756abc
commit 34d7d516b5
12 changed files with 359 additions and 38 deletions

View file

@ -40,6 +40,15 @@
<a href='/'>
<img src='/icon_sanifae.svg' alt='Sanifae Logo'>
</a>
{#if data.read > 0}
<a href='/messages'>
<img src='/unread.svg' alt='Messages'>
</a>
{:else}
<a href='/messages'>
<img src='/read.svg' alt='Messages'>
</a>
{/if}
{#if data.username && data.username != 'false'}
<a href='/users/{data.username}'>
{data.username}

View file

@ -11,15 +11,24 @@
p {
white-space: pre-wrap;
}
.bold, .bolditalic {
font-weight: bold;
}
.italic, .bolditalic {
font-style: italic;
}
</style>
<script>
import {formatPost} from '$lib/util.js';
export let content = '';
export let excludeImg = false;
let contentSplit;
$: contentSplit = formatPost(content || '');
$: contentSplit = formatPost(content || '', excludeImg);
console.log(contentSplit);
</script>
@ -28,27 +37,29 @@
{#each contentSplit as line}
<p>
{#each line as elem}
{#if elem && elem.type == 'img'}
{#if line.filter(x => x.type == 'img').length < 2}
<img src={elem.url} class='only-img' alt='Image preview'>
{:else}
<img src={elem.url} alt='Image preview'>
<span class={elem.format}>
{#if elem && elem.type == 'img'}
{#if line.filter(x => x.type == 'img').length < 2}
<img src={elem.url} class='only-img' alt='Image preview'>
{:else}
<img src={elem.url} alt='Image preview'>
{/if}
{:else if elem.type == 'video'}
{#if line.filter(x => x.type == 'video').length < 2}
<video class='only-img' alt='Video preview' controls>
<source src={elem.url}>
</video>
{:else}
<video alt='Video preview' controls>
<source src={elem.url}>
</video>
{/if}
{:else if elem.type == 'link'}
<a href={elem.url}>{elem.display}</a>
{:else if elem.type == 'text'}
{elem.content}
{/if}
{:else if elem.type == 'video'}
{#if line.filter(x => x.type == 'video').length < 2}
<video class='only-img' alt='Video preview' controls>
<source src={elem.url}>
</video>
{:else}
<video alt='Video preview' controls>
<source src={elem.url}>
</video>
{/if}
{:else if elem.type == 'link'}
<a href={elem.url}>{elem.display + ' '}</a>
{:else if !elem.type}
{elem + ' '}
{/if}
</span>
{/each}
</p>
{/each}

View file

@ -3,13 +3,15 @@
import Button from '$lib/components/Button.svelte';
import {setLocation} from '$lib/util.js';
export let data;
export let data, noRatings;
</script>
<p>
<Button clickFunc={() => { window.location.search = setLocation(window.location,'sort','hot')}}>Hot</Button>
<Button clickFunc={() => { window.location.search = setLocation(window.location,'sort','rating')}}>Top</Button>
<Button clickFunc={() => { window.location.search = setLocation(window.location,'sort','time')}}>Recent</Button>
{#if !noRatings}
<Button clickFunc={() => { window.location.search = setLocation(window.location,'sort','hot')}}>Hot</Button>
<Button clickFunc={() => { window.location.search = setLocation(window.location,'sort','rating')}}>Top</Button>
<Button clickFunc={() => { window.location.search = setLocation(window.location,'sort','time')}}>Recent</Button>
{/if}
</p>
{#if data && data.postJson && data.postJson.data}

View file

@ -83,6 +83,13 @@ async function initDb() {
username CHAR(64), \
following CHAR(64) \
)');
await db.run('CREATE TABLE IF NOT EXISTS messages (\
username CHAR(64), \
content CHAR(10240), \
time INTEGER, \
read INTEGER \
)');
}
let backendProxy = async ({route, backendParams}) => {
@ -99,7 +106,7 @@ let backendProxy = async ({route, backendParams}) => {
console.log(user);
if ((!user || user == '') && AUTH_ACTIONS.indexOf(route) != -1) return {'success': 'Not authorized.' };
3
let isAdmin = false;
if (user && user != '') isAdmin = ((await backend.userRoles({user}, {db})) || []).indexOf('Admin') != -1;

View file

@ -202,9 +202,23 @@ backend.postCreate = async ({content}, {user,db}) => {
backend.postDelete = async ({id}, {user, admin, db}) => {
if (admin) {
let postUser = await db.all('SELECT * from post where id = ?',[
id
]) || {};
postUser = postUser[0].username;
await db.run('DELETE FROM post WHERE id = ?', [
id
])
await db.run('INSERT INTO messages (username, content, time,read) VALUES (?, ?, ?, ?)', [
postUser,
`#${id} was deleted by an admin for rule violations.`,
Math.floor(new Date() * 1000),
0
]);
} else {
await db.run('DELETE FROM post WHERE username = ? AND id = ?', [
user,
@ -248,7 +262,7 @@ backend.postBulk = async ({page, id, user, cookies, sort, type}, {admin, db}) =>
sort = (LEGAL_SORTS[sort]) || 'rating';
if (sort + '' != sort) sort = 'rating';
if (sort + '' !== sort) sort = 'rating';
sort = sort.replaceAll('%d',Math.floor(new Date() * 1000));
@ -297,9 +311,9 @@ backend.vote = async ({id, vote}, {user, db}) => {
var isCreator = (await db.all('SELECT * from post WHERE id = ?', [
id
]))[0].username == user;
]))[0].username;
if (isCreator)
if (isCreator == user)
return {success: 'fail' };
await db.run('DELETE FROM vote WHERE username = ? AND id = ?', [
@ -327,6 +341,13 @@ backend.vote = async ({id, vote}, {user, db}) => {
id
]);
await db.run('INSERT INTO messages (username, content, time,read) VALUES (?, ?, ?, ?)', [
isCreator,
`@${user} ${vote == 'up' ? 'upvoted' : 'downvoted'} #${id}`,
Math.floor(new Date() * 1000),
0
]);
var user = await db.all('SELECT * from post WHERE id = ?', [
id
]) || [];
@ -353,12 +374,20 @@ backend.token = async ({cookies}, {db}) => {
}
backend.follow = async ({target}, {user, db}) => {
var userExists = ((await db.all('SELECT * FROM user WHERE username = ?',[
target
])) || []).length;
if (userExists < 1) return;
var isFollowing = await db.all('SELECT * FROM follow WHERE username = ? AND following = ?',[
user,
target
]);
if (isFollowing && isFollowing.length > 0) {
let unfollowed = (isFollowing && isFollowing.length > 0);
if (unfollowed) {
await db.run('DELETE FROM follow WHERE username = ? AND following = ?',[
user,
target
@ -378,9 +407,31 @@ backend.follow = async ({target}, {user, db}) => {
target
]);
await db.run('INSERT INTO messages (username, content, time,read) VALUES (?, ?, ?, ?)', [
target,
`@${user} ${unfollowed ? 'is now following' : 'unfollowed'} you`,
Math.floor(new Date() * 1000),
0
]);
return {'success': 'User followed/unfollowed.', 'data': {following, followers}};
};
backend.messages = async ({isRead}, {user, db}) => {
var msg = await db.all('SELECT * FROM messages WHERE username = ? ORDER BY time DESC', [
user
]) || [];
if (isRead) {
await db.run('UPDATE messages SET read = 1 WHERE username = ?', [
user
]);
}
let read = msg.filter(x => !x.read).length;
return {'data': {msg, read}};
};
export {

View file

@ -7,6 +7,12 @@ const EXTENSION_MAP = {
'mp4': 'video'
}
const formats = [
'italic',
'bold',
'bolditalic'
];
let checkLength = function(string, field, lowerBound, upperBound) {
if (string.length < lowerBound) {
if (string.length == 0) {
@ -66,11 +72,49 @@ let safeName = function (text) {
return text.replaceAll(/[^A-Za-z0-9\-\_]/g, '');
}
let formatPost = function(post) {
let formatPostText = function(post) {
post = post.map(x => (x === x + '') ? x.split(/(\*)/).filter(x => x != '') : x).flat();
let inc = false;
let formatType = -1;
let wasInc = false;
let dir = 1;
let newArr = [];
for (var i = 0; i < post.length; i++) {
inc = (post[i] == '*');
if (inc) {
formatType += dir;
} else {
if (wasInc) dir = !dir + 0;
if (dir) formatType = -1;
let formatTypeStr = formats[formatType] || 'default';
if (post[i] === post[i] + '') {
newArr.push({'type': 'text', 'format': formatTypeStr, 'content': post[i]})
} else {
let content = post[i];
content.format = formatTypeStr;
newArr.push(content);
}
}
wasInc = inc;
}
console.log(newArr);
return newArr;
}
let formatPost = function(post, ignoreImg) {
post = post.split('\n');
post = post.map(subPost => {
return subPost.split(' ');
return subPost.split(/( )/);
});
post = post.map(line => {
@ -78,7 +122,7 @@ let formatPost = function(post) {
var splitPost = subPost.split('||');
if (splitPost.length > 1) {
if (splitPost.length > 1 && !ignoreImg) {
var cap1 = splitPost[0];
if (cap1 == 'img') {
@ -94,7 +138,7 @@ let formatPost = function(post) {
} else if (subPost[0] == '@' || subPost[0] == '#') {
var subPostIn = safeName(subPost.substring(0));
var type = (subPost[0] == '@') ? 'user' : 'post';
var type = (subPost[0] == '@') ? 'users' : 'post';
splitPost = {'type': 'link', 'display': subPost, 'subtype': type, 'url': `/${type}/${subPostIn}`};
@ -103,7 +147,7 @@ let formatPost = function(post) {
var url;
var extension = subPost.split('.').pop().toLowerCase();
if (EXTENSION_MAP[extension]) {
if (EXTENSION_MAP[extension] && !ignoreImg) {
url = `/embed?url=${encodeURIComponent(subPost)}`;
splitPost = [
{'type': 'link', 'display': subPost, 'url': url},
@ -119,7 +163,7 @@ let formatPost = function(post) {
return subPost;
})
return line.flat();
return formatPostText(line.flat());
});
return post;