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

Deploy #3093

Merged
merged 4 commits into from
Jan 24, 2023
Merged

Deploy #3093

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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@babel/runtime": "7.0.0",
"airtable": "0.4.5",
"algoliasearch": "^4.10.5",
"amqplib": "^0.10.3",
"artsy-ezel-components": "artsy/artsy-ezel-components",
"artsy-morgan": "artsy/artsy-morgan",
"artsy-xapp": "1.0.4",
Expand Down
29 changes: 24 additions & 5 deletions src/api/apps/articles/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
onUnpublish,
} = require("./save.coffee")
const { removeFromSearch, getArticleUrl } = require("./distribute.coffee")
const Article = require("./../../../../api/models/article.coffee")

//
// Retrieval
Expand Down Expand Up @@ -157,17 +158,20 @@ export const save = (input, accessToken, options, callback) => {
}

// Handle publishing, unpublishing, published, draft
const publishing =
(modifiedArticle.published && !article.published) ||
(modifiedArticle.scheduled_publish_at && !article.published)
const publishing = modifiedArticle.published && !article.published
const scheduledPublishing =
modifiedArticle.scheduled_publish_at && !article.published
const unPublishing = article.published && !modifiedArticle.published
const hasSlugs =
modifiedArticle.slugs && modifiedArticle.slugs.length > 0

if (publishing) {
return onPublish(modifiedArticle, postPublishCallback(callback))
} else if (scheduledPublishing) {
return onPublish(modifiedArticle, sanitizeAndSave(callback))
} else if (unPublishing) {
return onUnpublish(modifiedArticle, sanitizeAndSave(callback))
} else if (!publishing && !hasSlugs) {
} else if (!(publishing || scheduledPublishing) && !hasSlugs) {
return generateSlugs(modifiedArticle, sanitizeAndSave(callback))
} else {
return sanitizeAndSave(callback)(null, modifiedArticle)
Expand Down Expand Up @@ -195,7 +199,7 @@ export const publishScheduledArticles = callback => {
published_at: moment(article.scheduled_publish_at).toDate(),
scheduled_publish_at: null,
})
return onPublish(article, sanitizeAndSave(cb))
return onPublish(article, postPublishCallback(cb))
},
(err, results) => {
if (err) {
Expand Down Expand Up @@ -359,3 +363,18 @@ export const backfill = callback => {
)
})
}

const postPublishCallback = callback => {
return (err, article) => {
const cb = sanitizeAndSave((err, article) => {
if (err) {
return callback(err)
}

const model = new Article(cloneDeep(article))
model.dispatchPublishEvent()
callback(null, article)
})
cb(err, article)
}
}
1 change: 1 addition & 0 deletions src/api/apps/articles/model/save.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,4 @@ sanitizeHtml = (html) ->
_.map article.sections, (section) ->
condensedHTML = condensedHTML.concat section.body if section.type is 'text'
condensedHTML

18 changes: 18 additions & 0 deletions src/api/apps/articles/test/model/index/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import moment from "moment"
import { ObjectId } from "mongojs"
import rewire from "rewire"
import { extend, times } from "underscore"
import sinon from "sinon"
const {
db,
fabricate,
Expand All @@ -12,9 +13,15 @@ const gravity = require("@artsy/antigravity").server
const app = require("express")()
const Article = rewire("../../../model/index.js")
const search = require("../../../../../lib/elasticsearch.coffee")
const { amqp } = require("../../../../../lib/amqp")

process.env.ENABLE_PUBLISH_RABBITMQ_EVENTS = "true"

describe("Article", () => {
let server
let publishStub
let sandbox

// @ts-ignore
before(done => {
app.use("/__gravity", gravity)
Expand All @@ -34,16 +41,23 @@ describe("Article", () => {
})

beforeEach(done => {
sandbox = sinon.createSandbox()
publishStub = sandbox.stub(amqp, "publish")
empty(() => fabricate("articles", times(10, () => ({})), () => done()))
})

afterEach(() => {
sandbox.restore()
})

describe("#publishScheduledArticles", () => {
it("calls #save on each article that needs to be published", done => {
fabricate(
"articles",
{
_id: ObjectId("54276766fd4f50996aeca2b8"),
author_id: ObjectId("5086df098523e60002000018"),
channel_id: "5086df098523e60002000018",
published: false,
scheduled_publish_at: moment("2016-01-01").toDate(),
author: {
Expand Down Expand Up @@ -80,6 +94,8 @@ describe("Article", () => {
results[0].sections[1].images[0].url.should.containEql(
"https://image.png"
)
publishStub.callCount.should.eql(1)

done()
})
)
Expand All @@ -92,6 +108,7 @@ describe("Article", () => {
"articles",
{
_id: ObjectId("54276766fd4f50996aeca2b8"),
channel_id: "5086df098523e60002000018",
weekly_email: true,
daily_email: true,
author: {
Expand All @@ -103,6 +120,7 @@ describe("Article", () => {
Article.unqueue((_err, results) => {
results[0].weekly_email.should.be.false()
results[0].daily_email.should.be.false()

done()
})
)))
Expand Down
127 changes: 127 additions & 0 deletions src/api/apps/articles/test/model/index/persistence.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ const Article = rewire("../../../model/index.js")
const gravity = require("@artsy/antigravity").server
const app = require("express")()
const search = require("../../../../../lib/elasticsearch.coffee")
const { amqp } = require("../../../../../lib/amqp")
import sinon from "sinon"
const Channel = require("../../../../channels/model.coffee")

process.env.ENABLE_PUBLISH_RABBITMQ_EVENTS = "true"

describe("Article Persistence", () => {
let server
let sandbox
let publishStub

// @ts-ignore
before(done => {
app.use("/__gravity", gravity)
Expand All @@ -35,9 +43,15 @@ describe("Article Persistence", () => {
})

beforeEach(done => {
sandbox = sinon.createSandbox()
publishStub = sandbox.stub(amqp, "publish")
empty(() => fabricate("articles", times(10, () => ({})), () => done()))
})

afterEach(() => {
sandbox.restore()
})

describe("#save", () => {
it("saves valid article input data", done =>
Article.save(
Expand Down Expand Up @@ -283,6 +297,119 @@ describe("Article Persistence", () => {
)
))

it("sends RabbitMQ event when publishing", done => {
Article.save(
{
title: "Top Ten Shows",
thumbnail_title: "Ten Shows",
author_id: "5086df098523e60002000018",
published: true,
id: "5086df098523e60002002222",
primary_featured_artist_ids: ["52868347b202a37bb000072a"],
channel_id: "5086df098523e60002000018",
},
"foo",
{},
(err, _) => {
if (err) {
done(err)
}

publishStub
.withArgs("editorial", "article.published", {
id: ObjectId("5086df098523e60002002222"),
title: "Top Ten Shows",
featured_artist_ids: [ObjectId("52868347b202a37bb000072a")],
href: "/article/undefined-ten-shows",
})
.callCount.should.eql(1)

done()
}
)
})

it("doesn't send RabbitMQ event when scheduling publishing", done => {
Article.save(
{
title: "Top Ten Shows",
thumbnail_title: "Ten Shows",
author_id: "5086df098523e60002000018",
scheduled_publish_at: moment("2040-01-01").toDate(),
id: "5086df098523e60002002222",
primary_featured_artist_ids: ["52868347b202a37bb000072a"],
channel_id: "5086df098523e60002000018",
},
"foo",
{},
(err, _) => {
if (err) {
done(err)
}

publishStub.callCount.should.eql(0)

done()
}
)
})

it("doesn't send RabbitMQ event when publishing not editorial article", done => {
Channel.save({ name: "Channel", type: "team" }, (err, channel) => {
if (err) {
done(err)
}

Article.save(
{
title: "Top Ten Shows",
thumbnail_title: "Ten Shows",
author_id: "5086df098523e60002000018",
published: true,
id: "5086df098523e60002002222",
primary_featured_artist_ids: ["52868347b202a37bb000072a"],
channel_id: channel.id,
},
"foo",
{},
(err, _) => {
if (err) {
done(err)
}

publishStub.callCount.should.eql(0)

done()
}
)
})
})

it("doesn't send RabbitMQ event when article doesn't have primary_featured_artist_ids", done => {
Article.save(
{
title: "Top Ten Shows",
thumbnail_title: "Ten Shows",
author_id: "5086df098523e60002000018",
published: true,
id: "5086df098523e60002002222",
primary_featured_artist_ids: [],
channel_id: "5086df098523e60002000018",
},
"foo",
{},
(err, _) => {
if (err) {
done(err)
}

publishStub.callCount.should.eql(0)

done()
}
)
})

it("saves published_at when the article is published", done =>
Article.save(
{
Expand Down
24 changes: 24 additions & 0 deletions src/api/lib/amqp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { RABBITMQ_URL } = process.env

const amqplib = require("amqplib/callback_api")

const amqp = {}
amqp.publish = (exchange, routingKey, message) => {
console.log("[amqp.publish]", exchange, routingKey, message)

amqplib.connect(
RABBITMQ_URL,
(err, conn) => {
if (err) throw err

conn.createChannel((err, ch) => {
if (err) throw err

ch.assertExchange(exchange, "topic")
ch.publish(exchange, routingKey, Buffer.from(JSON.stringify(message)))
})
}
)
}

module.exports = { amqp }
18 changes: 18 additions & 0 deletions src/api/models/article.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Backbone = require 'backbone'
moment = require 'moment'
cheerio = require 'cheerio'
Authors = require '../apps/authors/model'
{ amqp } = require '../lib/amqp'

module.exports = class Article extends Backbone.Model

Expand Down Expand Up @@ -65,3 +66,20 @@ module.exports = class Article extends Backbone.Model
cb [@get('author')?.name]
else
cb []

dispatchPublishEvent: ->
console.log("[dispatchPublishEvent] process.env.ENABLE_PUBLISH_RABBITMQ_EVENTS:", process.env.ENABLE_PUBLISH_RABBITMQ_EVENTS)
return unless process.env.ENABLE_PUBLISH_RABBITMQ_EVENTS == 'true'
return unless @shouldEmitPublishEvent()

payload =
id: @get('id')
featured_artist_ids: @get('primary_featured_artist_ids')
href: @href()
title: @get('title')

amqp.publish("editorial", "article.published", payload)

shouldEmitPublishEvent: ->
console.log("[shouldEmitPublishEvent] isEditorial:", @isEditorial(), ', primary_featured_artist_ids:', @get('primary_featured_artist_ids'))
return @isEditorial() && @get('primary_featured_artist_ids')?.length
Loading