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.
105 lines
2.4 KiB
105 lines
2.4 KiB
// hive.js
|
|
// Copyright (C) 2022 DTP Technologies, LLC
|
|
// License: Apache-2.0
|
|
|
|
'use strict';
|
|
|
|
const mongoose = require('mongoose');
|
|
|
|
const UserSubscription = mongoose.model('UserSubscription');
|
|
|
|
const slug = require('slug');
|
|
|
|
const { SiteService, SiteError } = require('../../lib/site-lib');
|
|
|
|
class HiveService extends SiteService {
|
|
|
|
constructor (dtp) {
|
|
super(dtp, module.exports);
|
|
}
|
|
|
|
async subscribe (user, client, emitterId) {
|
|
await UserSubscription.updateOne(
|
|
{ user: user._id },
|
|
{
|
|
$addToSet: {
|
|
subscriptions: {
|
|
client: client._id,
|
|
emitterId,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
upsert: true,
|
|
},
|
|
);
|
|
}
|
|
|
|
async unsubscribe (user, subscription) {
|
|
await UserSubscription.updateOne(
|
|
{ user: user._id },
|
|
{ $pull: { subscriptions: subscription } },
|
|
);
|
|
}
|
|
|
|
extractHashtags (content) {
|
|
const hashtags = content
|
|
.split(/ \r\n/g)
|
|
.filter((tag) => tag[0] === '#')
|
|
.map((tag) => slug(tag.slice(1)))
|
|
;
|
|
return hashtags;
|
|
}
|
|
|
|
extractLinks (content) {
|
|
let links = content
|
|
.split(/( |\r|\n)/g)
|
|
.filter((tag) => {
|
|
const test = tag.trim().toLowerCase();
|
|
return test.startsWith('http://') || test.startsWith('https://');
|
|
});
|
|
|
|
return links;
|
|
}
|
|
|
|
async resolveLink (author, url) {
|
|
const jobData = {
|
|
authorType: author.type,
|
|
author: author._id,
|
|
url,
|
|
};
|
|
this.log.info('creating job to resolve link', { jobData });
|
|
await this.resolver.add('resolve-link', jobData);
|
|
}
|
|
|
|
async processKaleidoscopeEvent (event) {
|
|
const {
|
|
userNotification: userNotificationService,
|
|
oauth2: oauth2Service,
|
|
} = this.dtp.services;
|
|
|
|
const client = await oauth2Service.getClientByDomainKey(event.source.site.domainKey);
|
|
if (!client) {
|
|
throw new SiteError(403, 'Unknown client domain key');
|
|
}
|
|
|
|
await UserSubscription
|
|
.find({
|
|
'subscriptions.client': client._id,
|
|
'subscriptions.emitterId': event.source.emitter._id,
|
|
})
|
|
.select('-subscriptions')
|
|
.cursor()
|
|
.eachAsync(async (subscription) => {
|
|
await userNotificationService.create(subscription.user, event);
|
|
}, 3);
|
|
|
|
this.emit('kaleidoscope:event', event, client);
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
slug: 'hive',
|
|
name: 'hive',
|
|
create: (dtp) => { return new HiveService(dtp); },
|
|
};
|