I think I found a simpler solution: add the Apex user to the contentdocument..
Here is the code:
//First, get all the CDLs for each object
ContentDocumentLink[] cdls = [SELECT
contentdocumentid,
linkedentity.name,
linkedentityid,
linkedentity.type
FROM ContentDocumentLink
WHERE linkedentityid in (SELECT id FROM User)];
cdls.addAll([SELECT
contentdocumentid,
linkedentity.name,
linkedentityid,
linkedentity.type
FROM ContentDocumentLink
WHERE linkedentityid in (SELECT id FROM Contact)]);
//...// repeat for each object
//Get unique document ids (since you can have multiple CDLs for a single Doc)
set<id> docIds = new set<id>();
for(ContentDocumentLink cdl : cdls){
docIds.add(cdl.contentdocumentid);
}
//get apex user id
id apexUser = UserInfo.getUserId();
//create new CDLs
ContentDocumentLink[] newCdls = new list<ContentDocumentLink>();
for(ContentDocumentLinkid cdldocId : cdlsdocIds){
ContentDocumentLink newCdl = new ContentDocumentLink(
ContentDocumentId='0690W000003iLIwQAM'ContentDocumentId=docId,
linkedentityid=apexUser,
Visibility='SharedUsers',
ShareType='C');
newCdls.add(newCdl);
}
insert newCdls;
//check new results
ContentDocument[] Cds = [SELECT id FROM ContentDocument];
//Profit