Added basic community library browser

This commit is contained in:
Stefan Zermatten
2023-06-12 22:16:20 +02:00
parent 9ae8d63fc4
commit c314c0ab05
17 changed files with 423 additions and 25 deletions

View File

@@ -29,6 +29,16 @@ let LibrarySchema = new SimpleSchema({
optional: true,
max: STORAGE_LIMITS.summary,
},
showInMarket: {
index: 1,
type: Boolean,
optional: true,
},
subscriberCount: {
index: 1,
type: Number,
optional: true,
},
});
LibrarySchema.extend(SharingSchema);
@@ -104,6 +114,29 @@ const updateLibraryDescription = new ValidatedMethod({
},
});
const updateLibraryShowInMarket = new ValidatedMethod({
name: 'libraries.updateShowInMarket',
validate: new SimpleSchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.id
},
value: {
type: Boolean,
},
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({ _id, value }) {
let library = Libraries.findOne(_id);
assertEditPermission(library, this.userId);
Libraries.update(_id, { $set: { showInMarket: value } });
},
});
const removeLibrary = new ValidatedMethod({
name: 'libraries.remove',
validate: new SimpleSchema({
@@ -130,4 +163,4 @@ export function removeLibaryWork(libraryId) {
LibraryNodes.remove({ 'ancestors.id': libraryId });
}
export { LibrarySchema, insertLibrary, updateLibraryName, updateLibraryDescription, removeLibrary };
export { LibrarySchema, insertLibrary, updateLibraryName, updateLibraryDescription, updateLibraryShowInMarket, removeLibrary };

View File

@@ -32,6 +32,16 @@ const LibraryCollectionSchema = new SimpleSchema({
type: String,
regEx: SimpleSchema.RegEx.Id,
},
showInMarket: {
index: 1,
type: Boolean,
optional: true,
},
subscriberCount: {
index: 1,
type: Number,
optional: true,
},
});
LibraryCollectionSchema.extend(SharingSchema);
@@ -48,12 +58,12 @@ const insertLibraryCollection = new ValidatedMethod({
run(libraryCollection) {
if (!this.userId) {
throw new Meteor.Error('LibraryCollections.methods.insert.denied',
'You need to be logged in to insert a library');
'You need to be logged in to insert a library');
}
let tier = getUserTier(this.userId);
if (!tier.paidBenefits){
if (!tier.paidBenefits) {
throw new Meteor.Error('LibraryCollections.methods.insert.denied',
`The ${tier.name} tier does not allow you to insert a library collection`);
`The ${tier.name} tier does not allow you to insert a library collection`);
}
libraryCollection.owner = this.userId;
return LibraryCollections.insert(libraryCollection);
@@ -72,7 +82,7 @@ const updateLibraryCollection = new ValidatedMethod({
},
update: {
type: LibraryCollectionSchema
.pick('name', 'description', 'libraries')
.pick('name', 'description', 'libraries', 'showInMarket')
.extend({ //make libraries optional
libraries: {
optional: true,
@@ -85,7 +95,7 @@ const updateLibraryCollection = new ValidatedMethod({
numRequests: 5,
timeInterval: 5000,
},
run({_id, update}){
run({ _id, update }) {
const libraryCollection = LibraryCollections.findOne(_id, {
fields: {
owner: 1,
@@ -93,7 +103,7 @@ const updateLibraryCollection = new ValidatedMethod({
}
});
assertEditPermission(libraryCollection, this.userId);
return LibraryCollections.update(_id, {$set: update});
return LibraryCollections.update(_id, { $set: update });
},
});
@@ -110,7 +120,7 @@ const removeLibraryCollection = new ValidatedMethod({
numRequests: 5,
timeInterval: 5000,
},
run({_id}){
run({ _id }) {
const libraryCollection = LibraryCollections.findOne(_id, {
fields: {
owner: 1,

View File

@@ -1,11 +1,12 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import Libraries from '/imports/api/library/Libraries.js';
import LibraryCollections from '/imports/api/library/LibraryCollections.js';
import '/imports/api/users/methods/deleteMyAccount.js';
import '/imports/api/users/methods/addEmail.js';
import '/imports/api/users/methods/removeEmail.js';
import '/imports/api/users/methods/updateFileStorageUsed.js';
import { some } from 'lodash';
const defaultLibraries = process.env.DEFAULT_LIBRARIES && process.env.DEFAULT_LIBRARIES.split(',') || [];
const defaultLibraryCollections = process.env.DEFAULT_LIBRARY_COLLECTIONS && process.env.DEFAULT_LIBRARY_COLLECTIONS.split(',') || [];
@@ -250,6 +251,29 @@ Meteor.users.setPreference = new ValidatedMethod({
},
});
if (Meteor.isServer) {
Accounts.onCreateUser(() => {
if (defaultLibraries?.length) {
Libraries.update({
_id: { $in: defaultLibraries }
}, {
$inc: { subscriberCount: 1 }
}, {
multi: true,
}, () => {/**/ });
}
if (defaultLibraryCollections?.length) {
LibraryCollections.update({
_id: { $in: defaultLibraryCollections }
}, {
$inc: { subscriberCount: 1 }
}, {
multi: true,
}, () => {/**/ });
}
});
}
Meteor.users.subscribeToLibrary = new ValidatedMethod({
name: 'users.subscribeToLibrary',
validate: new SimpleSchema({
@@ -264,15 +288,17 @@ Meteor.users.subscribeToLibrary = new ValidatedMethod({
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
timeInterval: 2000,
},
run({ libraryId, subscribe }) {
if (!this.userId) throw 'Can only subscribe if logged in';
if (subscribe) {
Libraries.update({ _id: libraryId }, { $inc: { subscriberCount: 1 } }, () => {/**/ });
return Meteor.users.update(this.userId, {
$addToSet: { subscribedLibraries: libraryId },
});
} else {
Libraries.update({ _id: libraryId }, { $inc: { subscriberCount: -1 } }, () => {/**/ });
return Meteor.users.update(this.userId, {
$pullAll: { subscribedLibraries: libraryId },
});
@@ -299,10 +325,12 @@ Meteor.users.subscribeToLibraryCollection = new ValidatedMethod({
run({ libraryCollectionId, subscribe }) {
if (!this.userId) throw 'Can only subscribe if logged in';
if (subscribe) {
LibraryCollections.update({ _id: libraryCollectionId }, { $inc: { subscriberCount: 1 } }, () => {/**/ });
return Meteor.users.update(this.userId, {
$addToSet: { subscribedLibraryCollections: libraryCollectionId },
});
} else {
LibraryCollections.update({ _id: libraryCollectionId }, { $inc: { subscriberCount: -1 } }, () => {/**/ });
return Meteor.users.update(this.userId, {
$pullAll: { subscribedLibraryCollections: libraryCollectionId },
});