Add new feature: community sentiment
This commit is contained in:
parent
aecf7d65c4
commit
dba356c413
109
src/lib/components/CommunitySentiment.svelte
Normal file
109
src/lib/components/CommunitySentiment.svelte
Normal file
File diff suppressed because one or more lines are too long
69
src/routes/api/community-sentiment/+server.ts
Normal file
69
src/routes/api/community-sentiment/+server.ts
Normal file
@ -0,0 +1,69 @@
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
function secondsUntilEndOfDay() {
|
||||
const now = new Date();
|
||||
const endOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
|
||||
const secondsUntilEndOfDay = (endOfDay - now) / 1000;
|
||||
return secondsUntilEndOfDay;
|
||||
}
|
||||
|
||||
|
||||
|
||||
export const POST = (async ( {request, cookies, locals} ) => {
|
||||
|
||||
let output = 'error';
|
||||
const data = await request.json()
|
||||
const sentiment = data?.sentiment;
|
||||
const ticker = data?.ticker
|
||||
const sentimentId = data?.sentimentId;
|
||||
const maxAge = secondsUntilEndOfDay();
|
||||
|
||||
let newData;
|
||||
|
||||
if (cookies?.get('community-sentiment-'+ticker)) {
|
||||
//console.log('already voted')
|
||||
return new Response(JSON.stringify(output))
|
||||
}
|
||||
else {
|
||||
try {
|
||||
if (sentimentId) {
|
||||
if (sentiment === 'upvote') {
|
||||
await locals?.pb?.collection('sentiment').update( sentimentId, {'upvote+': 1})
|
||||
}
|
||||
else if (sentiment === 'downvote') {
|
||||
await locals?.pb?.collection('sentiment').update( sentimentId, {'downvote+': 1})
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
|
||||
if (sentiment === 'upvote') {
|
||||
newData = await locals?.pb?.collection('sentiment').create({'ticker': ticker, 'upvote': 1})
|
||||
}
|
||||
else if (sentiment === 'downvote') {
|
||||
newData = await locals?.pb?.collection('sentiment').create({'ticker': ticker, 'downvote': 1})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
output = 'success';
|
||||
|
||||
cookies.set('community-sentiment-'+ticker, sentiment, {httpOnly: true,
|
||||
sameSite: 'lax',
|
||||
secure: true,
|
||||
path: '/',
|
||||
maxAge: maxAge // End of day expiry
|
||||
});
|
||||
|
||||
|
||||
} catch(e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return new Response(JSON.stringify(output))
|
||||
}) satisfies RequestHandler;
|
||||
|
||||
@ -85,7 +85,37 @@ async function fetchPortfolio(fastifyURL, userId)
|
||||
return output
|
||||
}
|
||||
|
||||
export const load = async ({ params, locals}) => {
|
||||
async function fetchCommunitySentiment(pb, ticker, cookies)
|
||||
{
|
||||
let alreadyVoted;
|
||||
const cookieVote = cookies?.get('community-sentiment-'+ticker);
|
||||
|
||||
const today = new Date();
|
||||
const tomorrow = new Date(today);
|
||||
tomorrow.setDate(today.getDate() + 1);
|
||||
|
||||
const startDate = today.toISOString().split('T')[0];
|
||||
const endDate = tomorrow.toISOString().split('T')[0];
|
||||
|
||||
const output = await pb.collection("sentiment").getFullList({
|
||||
filter: `ticker="${ticker}" && created >= "${startDate}" && created < "${endDate}"`
|
||||
});
|
||||
|
||||
if (cookieVote) {
|
||||
alreadyVoted = cookieVote;
|
||||
}
|
||||
|
||||
if(output?.length !== 0) {
|
||||
|
||||
return {'alreadyVoted': alreadyVoted, 'sentimentData': output?.at(0)}
|
||||
}
|
||||
else {
|
||||
return {'alreadyVoted': alreadyVoted, 'sentimentData': {} }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const load = async ({ params, locals, cookies}) => {
|
||||
|
||||
|
||||
const userRegion = locals?.region?.split("::")[0];
|
||||
@ -130,7 +160,8 @@ export const load = async ({ params, locals}) => {
|
||||
fetchData(apiURL,'/historical-price',params.tickerID),
|
||||
fetchData(apiURL,'/one-day-price',params.tickerID),
|
||||
fetchWatchlist(fastifyURL, locals?.user?.id),
|
||||
fetchPortfolio(fastifyURL, locals?.user?.id)
|
||||
fetchPortfolio(fastifyURL, locals?.user?.id),
|
||||
fetchCommunitySentiment(locals?.pb, params.tickerID, cookies)
|
||||
];
|
||||
|
||||
const [
|
||||
@ -160,6 +191,7 @@ export const load = async ({ params, locals}) => {
|
||||
getOneDayPrice,
|
||||
getUserWatchlist,
|
||||
getUserPortfolio,
|
||||
getCommunitySentiment,
|
||||
] = await Promise.all(promises);
|
||||
|
||||
/*
|
||||
@ -195,6 +227,7 @@ export const load = async ({ params, locals}) => {
|
||||
getOneDayPrice,
|
||||
getUserWatchlist,
|
||||
getUserPortfolio,
|
||||
getCommunitySentiment,
|
||||
companyName,
|
||||
};
|
||||
|
||||
|
||||
@ -136,6 +136,7 @@ export const actions = {
|
||||
|
||||
redirect(302,authProviderRedirect);
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
};
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
import AnalystEstimate from '$lib/components/AnalystEstimate.svelte';
|
||||
import StockKeyInformation from '$lib/components/StockKeyInformation.svelte';
|
||||
import BullBearSay from '$lib/components/BullBearSay.svelte';
|
||||
|
||||
import CommunitySentiment from '$lib/components/CommunitySentiment.svelte';
|
||||
|
||||
const usRegion = ['cle1','iad1','pdx1','sfo1'];
|
||||
|
||||
@ -49,6 +49,7 @@
|
||||
let trendList = [];
|
||||
let priceAnalysisDict = {};
|
||||
let fundamentalAnalysisDict = {};
|
||||
let communitySentiment = {};
|
||||
|
||||
//============================================//
|
||||
|
||||
@ -653,6 +654,7 @@ function changeChartType() {
|
||||
trendList = [];
|
||||
priceAnalysisDict = {};
|
||||
fundamentalAnalysisDict = {};
|
||||
communitySentiment = {}
|
||||
output = null;
|
||||
|
||||
|
||||
@ -673,7 +675,7 @@ function changeChartType() {
|
||||
enterpriseValues = data?.getEnterPriseValues;
|
||||
priceAnalysisDict = data?.getPriceAnalysis;
|
||||
fundamentalAnalysisDict = data?.getFundamentalAnalysis;
|
||||
|
||||
communitySentiment = data?.getCommunitySentiment;
|
||||
|
||||
similarstock = data?.getSimilarStock;
|
||||
topETFHolder = data?.getTopETFHolder;
|
||||
@ -1197,9 +1199,13 @@ function changeChartType() {
|
||||
<!--End Time Interval-->
|
||||
|
||||
|
||||
<div class="w-full mt-14 sm:mt-0 m-auto sm:pl-6 sm:pb-6 sm:pt-6">
|
||||
<CommunitySentiment communitySentiment={communitySentiment}/>
|
||||
</div>
|
||||
|
||||
|
||||
{#if $screenWidth <= 1022} <!--BUG: Dont remove since when changing ETF symbol display freezes-->
|
||||
<div class="w-full mt-8 m-auto sm:p-6 lg:hidden">
|
||||
<div class="w-full mt-10 m-auto sm:p-6 lg:hidden">
|
||||
<h3 class="cursor-pointer flex flex-row items-center text-white text-xl sm:text-3xl font-bold">
|
||||
Key Information
|
||||
</h3>
|
||||
@ -1214,7 +1220,9 @@ function changeChartType() {
|
||||
{/if}
|
||||
|
||||
|
||||
<div class="w-full mt-10 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {Object?.keys(marketMoods)?.length !== 0 ? '' : 'hidden'}">
|
||||
|
||||
|
||||
<div class="w-full mt-10 sm:mt-0 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {Object?.keys(marketMoods)?.length !== 0 ? '' : 'hidden'}">
|
||||
<BullBearSay marketMoods={marketMoods}/>
|
||||
</div>
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user