Skip to content

Commit 69a245d

Browse files
NEXT-173 - Shout Component
1 parent 35c75b0 commit 69a245d

File tree

12 files changed

+268
-7
lines changed

12 files changed

+268
-7
lines changed

server/models/contributions.model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = function (app) {
2727
contentExcerpt: { type: String, required: true },
2828
teaserImg: { type: String },
2929
language: { type: String, required: true },
30-
shouts: { type: Array },
30+
shoutCount: { type: Number, default: 0 },
3131
visibility: {
3232
type: String,
3333
enum: ['public', 'friends', 'private'],

server/models/shouts.model.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// users-shouts-model.js - A mongoose model
2+
//
3+
// See http://mongoosejs.com/docs/models.html
4+
// for more of what you can do here.
5+
module.exports = function (app) {
6+
const mongooseClient = app.get('mongooseClient');
7+
const shouts = new mongooseClient.Schema({
8+
userId: { type: String, required: true, index: true },
9+
foreignId: { type: String, required: true, index: true },
10+
foreignService: { type: String, required: true, index: true },
11+
createdAt: { type: Date, default: Date.now },
12+
wasSeeded: { type: Boolean }
13+
});
14+
15+
shouts.index(
16+
{ userId: 1, foreignId: 1 },
17+
{ unique: true }
18+
);
19+
20+
return mongooseClient.model('shouts', shouts);
21+
};

server/seeder/development/contributions.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const seedHelpers = require('../../helper/seed-helpers');
2-
const { size } = require('lodash');
32

43
module.exports = (seederstore) => {
54
return {
@@ -15,7 +14,6 @@ module.exports = (seederstore) => {
1514
content: '{{lorem.text}} {{lorem.text}}',
1615
teaserImg: seedHelpers.randomUnsplashUrl,
1716
language: () => seedHelpers.randomItem(['de', 'en']),
18-
shouts: () => seedHelpers.randomItems(seederstore.users, '_id', 0, Math.floor(size(seederstore.users) / 2)),
1917
visibility: 'public',
2018
createdAt: '{{date.recent}}',
2119
updatedAt: '{{date.recent}}',
@@ -28,7 +26,6 @@ module.exports = (seederstore) => {
2826
categoryIds: () => seedHelpers.randomCategories(seederstore),
2927
content: '{{lorem.text}} {{lorem.text}}',
3028
language: () => seedHelpers.randomItem(['de', 'en']),
31-
shouts: () => seedHelpers.randomItems(seederstore.users, '_id', 0, Math.floor(size(seederstore.users) / 2)),
3229
visibility: 'public',
3330
createdAt: '{{date.recent}}',
3431
updatedAt: '{{date.recent}}',
@@ -41,7 +38,6 @@ module.exports = (seederstore) => {
4138
categoryIds: () => seedHelpers.randomCategories(seederstore),
4239
content: '{{lorem.text}} {{lorem.text}}',
4340
language: () => seedHelpers.randomItem(['de', 'en']),
44-
shouts: () => seedHelpers.randomItems(seederstore.users, '_id', 0, Math.floor(size(seederstore.users) / 2)),
4541
visibility: 'public',
4642
isEnabled: false,
4743
createdAt: '{{date.recent}}',

server/seeder/development/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = function () {
1414
require('./follows'),
1515
require('./comments'),
1616
require('./emotions'),
17-
require('./invites')
17+
require('./invites'),
18+
require('./shouts')
1819
];
1920
};

server/seeder/development/shouts.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const seedHelpers = require('../../helper/seed-helpers');
2+
3+
module.exports = (seederstore) => {
4+
return {
5+
services: [{
6+
count: 240,
7+
path: 'shouts',
8+
templates: [
9+
{
10+
userId: () => seedHelpers.randomItem(seederstore.users)._id,
11+
foreignId: () => seedHelpers.randomItem(seederstore.contributions)._id,
12+
foreignService: 'contributions',
13+
wasSeeded: true
14+
}
15+
]
16+
}]
17+
};
18+
};

server/services/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const emotions = require('./emotions/emotions.service.js');
1313
const organizations = require('./organizations/organizations.service.js');
1414
const projects = require('./projects/projects.service.js');
1515
const follows = require('./follows/follows.service.js');
16+
const shouts = require('./shouts/shouts.service.js');
1617
const admin = require('./admin/admin.service.js');
1718
const invites = require('./invites/invites.service.js');
1819
const usersCandos = require('./users-candos/users-candos.service.js');
@@ -34,6 +35,7 @@ module.exports = function () {
3435
app.configure(organizations);
3536
app.configure(projects);
3637
app.configure(follows);
38+
app.configure(shouts);
3739
app.configure(admin);
3840
app.configure(usersCandos);
3941
app.configure(invites);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Set done date, when cando is done
2+
module.exports = () => async hook => {
3+
4+
// get count of all shouts on this thing
5+
const shoutCount = await hook.app.service('shouts').find({
6+
query: {
7+
foreignService: hook.result.foreignService,
8+
foreignId: hook.result.foreignId,
9+
$limit: 0
10+
}
11+
});
12+
13+
// update the shoud count on the foreign service
14+
await hook.app.service(hook.result.foreignService)
15+
.patch(hook.result.foreignId, {
16+
$set: {
17+
shoutCount: shoutCount.total
18+
}
19+
});
20+
21+
return hook;
22+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* eslint no-console: 1 */
2+
console.warn('You are using the default filter for the shouts service. For more information about event filters see https://docs.feathersjs.com/api/events.html#event-filtering'); // eslint-disable-line no-console
3+
4+
module.exports = function (data, connection, hook) { // eslint-disable-line no-unused-vars
5+
return data;
6+
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const { authenticate } = require('feathers-authentication').hooks;
2+
const { unless, isProvider } = require('feathers-hooks-common');
3+
const isModerator = require('../../hooks/is-moderator-boolean');
4+
const setShoutCount = require('./hooks/set-shout-count');
5+
const {
6+
associateCurrentUser,
7+
restrictToOwner
8+
} = require('feathers-authentication-hooks');
9+
const { isVerified } = require('feathers-authentication-management').hooks;
10+
11+
module.exports = {
12+
before: {
13+
all: [],
14+
find: [],
15+
get: [],
16+
create: [
17+
unless(isProvider('server'),
18+
authenticate('jwt'),
19+
isVerified(),
20+
associateCurrentUser()
21+
)
22+
],
23+
update: [
24+
authenticate('jwt'),
25+
unless(isModerator(),
26+
restrictToOwner()
27+
)
28+
],
29+
patch: [
30+
authenticate('jwt'),
31+
unless(isModerator(),
32+
restrictToOwner()
33+
)
34+
],
35+
remove: [
36+
authenticate('jwt'),
37+
unless(isModerator(),
38+
restrictToOwner()
39+
)
40+
]
41+
},
42+
43+
after: {
44+
all: [],
45+
find: [],
46+
get: [],
47+
create: [
48+
setShoutCount()
49+
],
50+
update: [
51+
setShoutCount()
52+
],
53+
patch: [
54+
setShoutCount()
55+
],
56+
remove: [
57+
setShoutCount()
58+
]
59+
},
60+
61+
error: {
62+
all: [],
63+
find: [],
64+
get: [],
65+
create: [],
66+
update: [],
67+
patch: [],
68+
remove: []
69+
}
70+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Initializes the `shouts` service on path `/shouts`
2+
const createService = require('feathers-mongoose');
3+
const createModel = require('../../models/shouts.model');
4+
const hooks = require('./shouts.hooks');
5+
const filters = require('./shouts.filters');
6+
7+
module.exports = function () {
8+
const app = this;
9+
const Model = createModel(app);
10+
const paginate = app.get('paginate');
11+
12+
const options = {
13+
name: 'shouts',
14+
Model,
15+
paginate
16+
};
17+
18+
// Initialize our service with any options it requires
19+
app.use('/shouts', createService(options));
20+
21+
// Get our initialized service so that we can register hooks and filters
22+
const service = app.service('shouts');
23+
24+
service.hooks(hooks);
25+
26+
if (service.filter) {
27+
service.filter(filters);
28+
}
29+
};

0 commit comments

Comments
 (0)