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

Algolia Indexing - approved objects only #2

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 27 additions & 3 deletions packages/events/algolia.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<!--- The basic rule is: if publicly visible content is changed, archive first --->


<cffunction name="saved" access="public" output="false" hint="Invoked immediately before DB is updated">
<cfargument name="typename" type="string" required="true" hint="The type of the object" />
<cfargument name="oType" type="any" required="true" hint="A CFC instance of the object type" />
Expand All @@ -26,9 +25,14 @@
</cfif>

<!--- update index --->
<cfif application.fc.lib.algolia.isIndexable(stProps)>
<cfif application.fc.lib.algolia.isIndexable(stProps)>
<cfset structappend(stProps, application.fapi.getContentObject(typename=stProps.typename, objectid=stProps.objectid), false) />
<cfset application.fc.lib.algolia.importIntoIndex(stObject=stProps, operation="updated") />
<cfif StructKeyExists(stProps, 'status') AND stProps['status'] == 'approved'>
<cfif request.mode.debug><cflog file="algolia-event" text="saved(#arguments.typename#): update index stProperties=#serializeJSON(arguments.stProperties)#"></cfif>
<cfset application.fc.lib.algolia.importIntoIndex(stObject=stProps, operation="updated") />
<cfelse>
<cfif request.mode.debug><cflog file="algolia-event-draft" text="saved(#arguments.typename#): stProperties=#serializeJSON(arguments.stProperties)#"></cfif>
</cfif>
</cfif>
</cffunction>

Expand All @@ -40,8 +44,28 @@
<cfargument name="auditNote" type="string" required="true" />

<cfif application.fc.lib.algolia.isIndexable(arguments.stObject)>

<cfif request.mode.debug><cflog file="algolia-event" text="deleted(#arguments.typename#): stObject=#serializeJSON(arguments.stObject)#"></cfif>
<cfset application.fc.lib.algolia.importIntoIndex(stObject=arguments.stObject, operation="deleted") />
</cfif>
</cffunction>

<cffunction name="statusChanged" access="public" hint="I am invoked when a content object has been deleted">
<cfargument name="typename" type="string" required="true" hint="The type of the object" />
<cfargument name="oType" type="any" required="true" hint="A CFC instance of the object type" />
<cfargument name="stObject" type="struct" required="true" hint="The object" />
<cfargument name="newStatus" type="string" required="true" />
<cfargument name="previousStatus" type="string" required="true" />
<cfargument name="auditNote" type="string" required="true" />

<cfif application.fc.lib.algolia.isIndexable(arguments.stObject)>
<!--- if was approved, remove from index --->
<cfif arguments.previousStatus == 'approved' AND arguments.newStatus != 'approved'>
<cfif request.mode.debug><cflog file="algolia-event" text="statusChanged(#arguments.typename#) Removing from index [newStatus=#arguments.newStatus#|previousStatus=#arguments.previousStatus#]: stObject=#serializeJSON(arguments.stObject)#"></cfif>
<cfset application.fc.lib.algolia.importIntoIndex(stObject=arguments.stObject, operation="deleted") />
</cfif>

</cfif>
</cffunction>

</cfcomponent>
2 changes: 1 addition & 1 deletion packages/forms/configAlgolia.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ component extends="farcry.core.packages.forms.forms" key="algolia" displayName="
FROM #arguments.typename#
GROUP BY datetimeLastUpdated
HAVING count(*) > 1
", { }, { datasource=application.dsn_read }).recordcount gt 1;
", { }, { datasource=application.dsn_read }).recordcount gt 0;
}

public void function disambiguateTimestamps(required string typename) {
Expand Down
42 changes: 32 additions & 10 deletions packages/lib/algolia.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -663,10 +663,21 @@ component {
(not structKeyExists(oContent, "isIndexable") and isIndexable(indexName=indexname, stObject=stObject))
)
) {
strOut.append('{ "action": "addObject", "indexName": "#indexName#", "body": ');
processObject(indexName, strOut, arguments.stObject);
strOut.append(' }, ');
builtToDate = arguments.stObject.datetimeLastUpdated;

if (StructKeyExists(arguments.stObject, 'status') AND arguments.stObject['status'] != 'approved') {
// remove draft record
// Approved only - draft records do not get updated to approved; new records added to index each time object goes to draft
strOut.append('{ "action": "deleteObject", "indexName": "#indexName#", "body": ');
strOut.append('{ "objectID": "');
strOut.append(arguments.stObject.objectid);
strOut.append('" } }, ');
builtToDate = now();
} else {
strOut.append('{ "action": "addObject", "indexName": "#indexName#", "body": ');
processObject(indexName, strOut, arguments.stObject);
strOut.append(' }, ');
builtToDate = arguments.stObject.datetimeLastUpdated;
}
}
else if (arguments.operation eq "deleted") {
strOut.append('{ "action": "deleteObject", "indexName": "#indexName#", "body": ');
Expand Down Expand Up @@ -731,15 +742,25 @@ component {
for (row in qContent) {
for (indexName in indexableTypes[qContent.typename]) {
if (qContent.operation eq "updated") {

stContent = oContent.getData(objectid=qContent.objectid);

if (
(structKeyExists(oContent, "isIndexable") and oContent.isIndexable(indexName=indexname, stObject=stContent)) or
(not structKeyExists(oContent, "isIndexable") and isIndexable(indexName=indexname, stObject=stContent))
) {
strOut.append('{ "action": "addObject", "indexName":"#indexName#", "body": ');
processObject(indexName, strOut, stContent);
strOut.append(' }, ');
if (StructKeyExists(stContent, 'status') AND stContent['status'] != 'approved') {
// remove draft record
// Approved only - draft records do not get updated to approved; new records added to index each time object goes to draft
strOut.append('{ "action": "deleteObject", "indexName":"#indexName#", "body": ');
strOut.append('{ "objectID": "');
strOut.append(qContent.objectid);
strOut.append('" } }, ');
} else {
strOut.append('{ "action": "addObject", "indexName":"#indexName#", "body": ');
processObject(indexName, strOut, stContent);
strOut.append(' }, ');
}
}
}
else if (qContent.operation eq "deleted") {
Expand All @@ -748,7 +769,7 @@ component {
strOut.append(qContent.objectid);
strOut.append('" } }, ');
}
}
} //indexName

if (
strOut.length() * ((qContent.currentrow+1) / qContent.currentrow) gt arguments.requestSize or
Expand All @@ -758,10 +779,11 @@ component {
count = qContent.currentrow;
break;
}
}
} // row

processingTime += getTickCount() - start;

strOut.delete(strOut.length()-2, strOut.length());
if (right(strOut, 2) == ', ') strOut.delete(strOut.length()-2, strOut.length());
strOut.append(' ] }');

if (count) {
Expand Down