2019-09-23 17:52:22 +00:00
|
|
|
'use strict'
|
|
|
|
const connection = require('./connection')
|
|
|
|
module.exports = {
|
2019-09-24 03:18:35 +00:00
|
|
|
get,
|
2019-09-25 22:15:39 +00:00
|
|
|
remove,
|
2019-09-23 17:52:22 +00:00
|
|
|
save
|
|
|
|
}
|
|
|
|
|
2019-09-24 03:18:35 +00:00
|
|
|
function get (id) {
|
|
|
|
const db = connection.getDb()
|
|
|
|
return db.collection('streams')
|
|
|
|
.find({ id: id })
|
|
|
|
.limit(1)
|
|
|
|
.project({ _id: 0, _meta: 0 })
|
|
|
|
.next()
|
|
|
|
}
|
2019-09-23 17:52:22 +00:00
|
|
|
|
|
|
|
async function save (activity) {
|
|
|
|
const db = connection.getDb()
|
|
|
|
const q = { id: activity.id }
|
2019-09-24 03:18:35 +00:00
|
|
|
// activities may be duplicated for multiple local targets
|
2019-09-23 17:52:22 +00:00
|
|
|
if (activity._meta && activity._meta._target) {
|
|
|
|
q['_meta._target'] = activity._meta._target
|
|
|
|
}
|
|
|
|
const exists = await db.collection('streams')
|
|
|
|
.find(q)
|
|
|
|
.project({ _id: 1 })
|
|
|
|
.limit(1)
|
|
|
|
.hasNext()
|
|
|
|
if (exists) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.collection('streams')
|
|
|
|
// server object ID avoids mutating local copy of document
|
|
|
|
.insertOne(activity, { forceServerObjectId: true })
|
|
|
|
}
|
2019-09-25 22:15:39 +00:00
|
|
|
|
|
|
|
function remove (activity, actor) {
|
|
|
|
return connection.getDb().collection('streams')
|
|
|
|
.deleteMany({ id: activity.id, actor: actor })
|
|
|
|
}
|