Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for filtering email events by post_id #15666

Merged
merged 1 commit into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ghost/core/core/server/models/email-recipient.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ const ghostBookshelf = require('./base');
const EmailRecipient = ghostBookshelf.Model.extend({
tableName: 'email_recipients',
hasTimestamps: false,

filterRelations: function filterRelations() {
return {
email: {
// Mongo-knex doesn't support belongsTo relations
tableName: 'emails',
tableNameAs: 'email',
type: 'manyToMany',
joinTable: 'email_recipients',
joinFrom: 'id',
joinTo: 'email_id'
}
};
},

email() {
return this.belongsTo('Email', 'email_id');
Expand Down
6 changes: 1 addition & 5 deletions ghost/core/core/server/models/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ const Email = ghostBookshelf.Model.extend({

model.emitChange('deleted', options);
}
}, {
post() {
return this.belongsTo('Post');
}
});
}, {});

const Emails = ghostBookshelf.Collection.extend({
model: Email
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ Object {
"data": Any<Object>,
"type": Any<String>,
},
Object {
"data": Any<Object>,
"type": Any<String>,
},
Object {
"data": Any<Object>,
"type": Any<String>,
},
],
"meta": Object {
"pagination": Object {
Expand All @@ -43,7 +51,7 @@ Object {
"page": null,
"pages": 1,
"prev": null,
"total": 8,
"total": 10,
},
},
}
Expand All @@ -53,7 +61,7 @@ exports[`Activity Feed API Can filter events by post id 2: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "15074",
"content-length": "17358",
"content-type": "application/json; charset=utf-8",
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
"vary": "Accept-Version, Origin, Accept-Encoding",
Expand All @@ -78,9 +86,9 @@ Object {
"limit": "2",
"next": null,
"page": null,
"pages": 4,
"pages": 5,
"prev": null,
"total": 8,
"total": 10,
},
},
}
Expand All @@ -90,7 +98,7 @@ exports[`Activity Feed API Can limit events 2: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "1239",
"content-length": "1240",
"content-type": "application/json; charset=utf-8",
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
"vary": "Accept-Version, Origin, Accept-Encoding",
Expand Down Expand Up @@ -348,6 +356,72 @@ Object {
}
`;

exports[`Activity Feed API Returns email delivered events in activity feed 1: [body] 1`] = `
Object {
"events": Array [
Object {
"data": Any<Object>,
"type": Any<String>,
},
],
"meta": Object {
"pagination": Object {
"limit": 10,
"next": null,
"page": null,
"pages": 1,
"prev": null,
"total": 1,
},
},
}
`;

exports[`Activity Feed API Returns email delivered events in activity feed 2: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "1246",
"content-type": "application/json; charset=utf-8",
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
"vary": "Accept-Version, Origin, Accept-Encoding",
"x-powered-by": "Express",
}
`;

exports[`Activity Feed API Returns email opened events in activity feed 1: [body] 1`] = `
Object {
"events": Array [
Object {
"data": Any<Object>,
"type": Any<String>,
},
],
"meta": Object {
"pagination": Object {
"limit": 10,
"next": null,
"page": null,
"pages": 1,
"prev": null,
"total": 1,
},
},
}
`;

exports[`Activity Feed API Returns email opened events in activity feed 2: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "1243",
"content-type": "application/json; charset=utf-8",
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
"vary": "Accept-Version, Origin, Accept-Encoding",
"x-powered-by": "Express",
}
`;

exports[`Activity Feed API Returns feedback events in activity feed 1: [body] 1`] = `
Object {
"events": Array [
Expand Down
56 changes: 50 additions & 6 deletions ghost/core/test/e2e-api/admin/activity-feed.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const {agentProvider, mockManager, fixtureManager, matchers} = require('../../utils/e2e-framework');
const {anyEtag, anyObjectId, anyUuid, anyISODate, anyString, anyObject, anyNumber} = matchers;
const models = require('../../../core/server/models');

const assert = require('assert');

let agent;
describe('Activity Feed API', function () {
before(async function () {
agent = await agentProvider.getAdminAPIAgent();
await fixtureManager.init('posts', 'newsletters', 'members:newsletters', 'comments', 'redirects', 'clicks', 'feedback');
await fixtureManager.init('posts', 'newsletters', 'members:newsletters', 'comments', 'redirects', 'clicks', 'feedback', 'members:emails');
await agent.loginAsOwner();
});

Expand Down Expand Up @@ -125,32 +126,75 @@ describe('Activity Feed API', function () {
});
});

it('Returns email delivered events in activity feed', async function () {
// Check activity feed
await agent
.get(`/members/events?filter=type:email_delivered_event`)
.expectStatus(200)
.matchHeaderSnapshot({
etag: anyEtag
})
.matchBodySnapshot({
events: new Array(1).fill({
type: anyString,
data: anyObject
})
})
.expect(({body}) => {
assert(body.events.find(e => e.type === 'email_delivered_event'), 'Expected an email delivered event');
assert(!body.events.find(e => e.type !== 'email_delivered_event'), 'Expected only email delivered events');
});
});

it('Returns email opened events in activity feed', async function () {
// Check activity feed
await agent
.get(`/members/events?filter=type:email_opened_event`)
.expectStatus(200)
.matchHeaderSnapshot({
etag: anyEtag
})
.matchBodySnapshot({
events: new Array(1).fill({
type: anyString,
data: anyObject
})
})
.expect(({body}) => {
assert(body.events.find(e => e.type === 'email_opened_event'), 'Expected an email opened event');
assert(!body.events.find(e => e.type !== 'email_opened_event'), 'Expected only email opened events');
});
});

it('Can filter events by post id', async function () {
const postId = fixtureManager.get('posts', 0).id;

await agent
.get(`/members/events?filter=data.post_id:${postId}`)
.expectStatus(200)
.matchHeaderSnapshot({
etag: anyEtag
})
.matchBodySnapshot({
events: new Array(8).fill({
events: new Array(10).fill({
type: anyString,
data: anyObject
})
})
.expect(({body}) => {
assert(!body.events.find(e => (e.data?.post?.id ?? e.data?.attribution?.id) !== postId), 'Should only return events for the post');
assert(!body.events.find(e => (e.data?.post?.id ?? e.data?.attribution?.id ?? e.data?.email?.post_id) !== postId), 'Should only return events for the post');

// Check all post_id event types are covered by this test
assert(body.events.find(e => e.type === 'click_event'), 'Expected a click event');
assert(body.events.find(e => e.type === 'comment_event'), 'Expected a comment event');
assert(body.events.find(e => e.type === 'feedback_event'), 'Expected a feedback event');
assert(body.events.find(e => e.type === 'signup_event'), 'Expected a signup event');
assert(body.events.find(e => e.type === 'subscription_event'), 'Expected a subscription event');
assert(body.events.find(e => e.type === 'email_delivered_event'), 'Expected an email delivered event');
assert(body.events.find(e => e.type === 'email_opened_event'), 'Expected an email opened event');

// Assert total is correct
assert.equal(body.meta.pagination.total, 8);
assert.equal(body.meta.pagination.total, 10);
});
});

Expand All @@ -169,10 +213,10 @@ describe('Activity Feed API', function () {
})
})
.expect(({body}) => {
assert(!body.events.find(e => (e.data?.post?.id ?? e.data?.attribution?.id) !== postId), 'Should only return events for the post');
assert(!body.events.find(e => (e.data?.post?.id ?? e.data?.attribution?.id ?? e.data?.email?.post_id) !== postId), 'Should only return events for the post');

// Assert total is correct
assert.equal(body.meta.pagination.total, 8);
assert.equal(body.meta.pagination.total, 10);
});
});
});
19 changes: 18 additions & 1 deletion ghost/core/test/utils/fixtures/data-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,19 @@ DataGenerator.Content = {
member_uuid: 'f6f91461-d7d8-4a3f-aa5d-8e582c40b343',
member_email: 'member1@test.com',
member_name: 'Mr Egg'
},
{
id: ObjectId().toHexString(),
email_id: null, // emails[0] relation added later
member_id: null, // members[4] relation added later
batch_id: null, // email_batches[0] relation added later
processed_at: moment().toDate(),
delivered_at: moment().toDate(),
opened_at: moment().toDate(),
failed_at: null,
member_uuid: 'f6f91461-d7d8-4a3f-aa5d-8e582c40b344',
member_email: 'member4@test.com',
member_name: 'Mr Egg'
}
],

Expand Down Expand Up @@ -850,6 +863,9 @@ DataGenerator.Content.email_recipients[2].member_id = DataGenerator.Content.memb
DataGenerator.Content.email_recipients[3].batch_id = DataGenerator.Content.email_batches[0].id;
DataGenerator.Content.email_recipients[3].email_id = DataGenerator.Content.email_batches[0].email_id;
DataGenerator.Content.email_recipients[3].member_id = DataGenerator.Content.members[3].id;
DataGenerator.Content.email_recipients[4].batch_id = DataGenerator.Content.email_batches[0].id;
DataGenerator.Content.email_recipients[4].email_id = DataGenerator.Content.email_batches[0].email_id;
DataGenerator.Content.email_recipients[4].member_id = DataGenerator.Content.members[4].id;
DataGenerator.Content.members_stripe_customers[0].member_id = DataGenerator.Content.members[2].id;
DataGenerator.Content.members_stripe_customers[1].member_id = DataGenerator.Content.members[3].id;
DataGenerator.Content.members_stripe_customers[2].member_id = DataGenerator.Content.members[4].id;
Expand Down Expand Up @@ -1533,7 +1549,8 @@ DataGenerator.forKnex = (function () {
createEmailRecipient(DataGenerator.Content.email_recipients[0]),
createEmailRecipient(DataGenerator.Content.email_recipients[1]),
createEmailRecipient(DataGenerator.Content.email_recipients[2]),
createEmailRecipient(DataGenerator.Content.email_recipients[3])
createEmailRecipient(DataGenerator.Content.email_recipients[3]),
createEmailRecipient(DataGenerator.Content.email_recipients[4])
];

const members = [
Expand Down
9 changes: 9 additions & 0 deletions ghost/members-api/lib/repositories/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ module.exports = class EventRepository {
if (filters['data.member_id']) {
options.filter.push(filters['data.member_id'].replace(/data.member_id:/g, 'member_id:'));
}
if (filters['data.post_id']) {
options.filter.push(filters['data.post_id'].replace(/data.post_id:/g, 'email.post_id:'));
}
options.filter = options.filter.join('+');
options.order = options.order.replace(/created_at/g, 'delivered_at');

Expand Down Expand Up @@ -480,6 +483,9 @@ module.exports = class EventRepository {
if (filters['data.member_id']) {
options.filter.push(filters['data.member_id'].replace(/data.member_id:/g, 'member_id:'));
}
if (filters['data.post_id']) {
options.filter.push(filters['data.post_id'].replace(/data.post_id:/g, 'email.post_id:'));
}
options.filter = options.filter.join('+');
options.order = options.order.replace(/created_at/g, 'opened_at');

Expand Down Expand Up @@ -517,6 +523,9 @@ module.exports = class EventRepository {
if (filters['data.member_id']) {
options.filter.push(filters['data.member_id'].replace(/data.member_id:/g, 'member_id:'));
}
if (filters['data.post_id']) {
options.filter.push(filters['data.post_id'].replace(/data.post_id:/g, 'email.post_id:'));
}
options.filter = options.filter.join('+');
options.order = options.order.replace(/created_at/g, 'failed_at');

Expand Down