-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from foo-comments/reactions
Reactions
- Loading branch information
Showing
15 changed files
with
198 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
MONGODB_URI=mongodb://mongo:27017/foo-comments | ||
API_URL=http://localhost:9547/api | ||
PORT=9547 | ||
|
||
ADMIN_EMAIL_ADDR= | ||
BOT_EMAIL_ADDR= | ||
BOT_EMAIL_PASS= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ jsconfig.json | |
yarn.lock | ||
.DS_Store | ||
*package-lock.json | ||
|
||
mongo-data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM node:18-alpine | ||
|
||
WORKDIR /app | ||
|
||
COPY ["package.json", "package-lock.json*", "./"] | ||
|
||
RUN npm install --production | ||
|
||
COPY . . | ||
|
||
CMD ["node", "index.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
build: | ||
docker build -t foo-comments-server . | ||
|
||
up: | ||
docker-compose up -d | ||
|
||
|
||
down: | ||
docker-compose down | ||
|
||
dev: | ||
docker-compose -f docker-compose.dev.yml up |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
version: '3.8' | ||
|
||
services: | ||
|
||
mongo: | ||
image: mongo | ||
restart: always | ||
volumes: | ||
- './mongo-data:/data/db' | ||
ports: | ||
- 27017:27017 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
version: '3.8' | ||
|
||
services: | ||
mongo: | ||
image: mongo | ||
restart: always | ||
volumes: | ||
- './mongo-data:/data/db' | ||
|
||
foo-comment-server: | ||
image: foo-comments-server | ||
restart: always | ||
volumes: | ||
- ./.env.docker:/app/.env:ro | ||
ports: | ||
- 9547:9547 | ||
depends_on: | ||
- mongo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { Router } from 'express'; | ||
import comments from './comments'; | ||
import reactions from './reactions'; | ||
|
||
const router = new Router(); | ||
|
||
router.use('/comments', comments); | ||
router.use('/reactions', reactions); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Reaction from './model'; | ||
import { getPageData } from './helper'; | ||
import { asyncRoute } from '../../services/express'; | ||
|
||
export const add = asyncRoute(async (req, res) => { | ||
const fingerprint = req.headers.fingerprint; | ||
const pageId = req.body.pageId || req.query.pageId; | ||
const reaction = req.body.reaction.slice(0, 20); // Just in case, limit characters by 20 | ||
|
||
if (!fingerprint) { | ||
return res.status(500).send('Fingerprint required for reacting.'); | ||
} | ||
|
||
const searchCondition = { pageId, fingerprint }; | ||
const recordInDB = await Reaction.findOne(searchCondition); | ||
|
||
if (!recordInDB) { | ||
await Reaction.create({ ...searchCondition, reaction }); | ||
} else { | ||
if (recordInDB.reaction === reaction) { | ||
await Reaction.deleteOne(recordInDB); | ||
} else { | ||
recordInDB.reaction = reaction; | ||
await recordInDB.save(); | ||
} | ||
} | ||
|
||
const reactions = await getPageData(searchCondition); | ||
|
||
return res.status(200).json(reactions); | ||
}); | ||
|
||
export const list = asyncRoute(async (req, res) => { | ||
const fingerprint = req.headers.fingerprint; | ||
const pageId = req.body.pageId || req.query.pageId; | ||
|
||
const reactions = await getPageData({ pageId, fingerprint }); | ||
|
||
return res.status(200).json(reactions); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import Reaction from './model'; | ||
|
||
export const getPageData = async ({ fingerprint, pageId }) => { | ||
const userReactionPromise = Reaction.findOne({ | ||
fingerprint, | ||
pageId | ||
}).select('reaction -_id'); | ||
|
||
const aggregationPromise = Reaction.aggregate() | ||
.match({ | ||
pageId | ||
}) | ||
.group({ | ||
_id: '$reaction', | ||
count: { $count: {} } | ||
}); | ||
|
||
const [aggregation, userReaction] = await Promise.all([ | ||
aggregationPromise, | ||
userReactionPromise | ||
]); | ||
|
||
return { aggregation, userReaction }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Router } from 'express'; | ||
import { list, add } from './controller'; | ||
|
||
const router = new Router(); | ||
|
||
router.post('/', add); | ||
router.get('/', list); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import mongoose, { Schema } from 'mongoose'; | ||
|
||
const schema = new Schema( | ||
{ | ||
owner: { | ||
ip: { | ||
type: String, | ||
required: false | ||
} | ||
}, | ||
fingerprint: { | ||
type: String, | ||
required: true | ||
}, | ||
pageId: { | ||
type: String, | ||
required: true | ||
}, | ||
reaction: { | ||
type: String, | ||
} | ||
}, | ||
{ | ||
timestamps: true | ||
} | ||
); | ||
|
||
const model = mongoose.model('Reaction', schema); | ||
|
||
export default model; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters