You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.7 KiB
80 lines
1.7 KiB
// minio.js
|
|
// Copyright (C) 2021 Digital Telepresence, LLC
|
|
// License: Apache-2.0
|
|
|
|
'use strict';
|
|
|
|
const mongoose = require('mongoose');
|
|
const Link = mongoose.model('Link');
|
|
|
|
const striptags = require('striptags');
|
|
|
|
const { SiteService } = require('../../lib/site-lib');
|
|
|
|
class LinkService extends SiteService {
|
|
|
|
constructor (dtp) {
|
|
super(dtp, module.exports);
|
|
|
|
this.populateLink = [
|
|
{
|
|
path: 'user',
|
|
select: '_id username username_lc displayName picture',
|
|
},
|
|
];
|
|
}
|
|
|
|
async create (user, linkDefinition) {
|
|
const NOW = new Date();
|
|
const link = new Link();
|
|
|
|
link.created = NOW;
|
|
link.user = user._id;
|
|
link.label = striptags(linkDefinition.label.trim());
|
|
link.href = striptags(linkDefinition.href.trim());
|
|
|
|
await link.save();
|
|
|
|
return link.toObject();
|
|
}
|
|
|
|
async update (link, linkDefinition) {
|
|
const updateOp = { $set: { } };
|
|
|
|
if (linkDefinition.label) {
|
|
updateOp.$set.label = striptags(linkDefinition.label.trim());
|
|
}
|
|
if (linkDefinition.href) {
|
|
updateOp.$set.href = striptags(linkDefinition.href.trim());
|
|
}
|
|
|
|
await Link.updateOne({ _id: link._id }, updateOp);
|
|
}
|
|
|
|
async getById (linkId) {
|
|
const link = await Link
|
|
.findById(linkId)
|
|
.populate(this.populateLink)
|
|
.lean();
|
|
return link;
|
|
}
|
|
|
|
async getForUser (user, pagination) {
|
|
const links = await Link
|
|
.find({ user: user._id })
|
|
.skip(pagination.skip)
|
|
.limit(pagination.cpp)
|
|
.lean();
|
|
return links;
|
|
}
|
|
|
|
async remove (link) {
|
|
await Link.deleteOne({ _id: link._id });
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
slug: 'link',
|
|
name: 'link',
|
|
create: (dtp) => { return new LinkService(dtp); },
|
|
};
|