-
Notifications
You must be signed in to change notification settings - Fork 8
/
PublicationLib.sol
541 lines (491 loc) · 21.4 KB
/
PublicationLib.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import {ValidationLib} from 'contracts/libraries/ValidationLib.sol';
import {Types} from 'contracts/libraries/constants/Types.sol';
import {Events} from 'contracts/libraries/constants/Events.sol';
import {Errors} from 'contracts/libraries/constants/Errors.sol';
import {IReferenceModule} from 'contracts/interfaces/IReferenceModule.sol';
import {ILegacyReferenceModule} from 'contracts/interfaces/ILegacyReferenceModule.sol';
import {StorageLib} from 'contracts/libraries/StorageLib.sol';
import {IPublicationActionModule} from 'contracts/interfaces/IPublicationActionModule.sol';
library PublicationLib {
/**
* @notice Publishes a post to a given profile.
*
* @param postParams The PostParams struct.
*
* @return uint256 The created publication's pubId.
*/
function post(Types.PostParams calldata postParams, address transactionExecutor) external returns (uint256) {
uint256 pubIdAssigned = ++StorageLib.getProfile(postParams.profileId).pubCount;
Types.Publication storage _post = StorageLib.getPublication(postParams.profileId, pubIdAssigned);
_post.contentURI = postParams.contentURI;
_post.pubType = Types.PublicationType.Post;
bytes[] memory actionModulesReturnDatas = _initPubActionModules(
postParams.profileId,
transactionExecutor,
pubIdAssigned,
postParams.actionModules,
postParams.actionModulesInitDatas
);
bytes memory referenceModuleReturnData = _initPubReferenceModule(
postParams.profileId,
transactionExecutor,
pubIdAssigned,
postParams.referenceModule,
postParams.referenceModuleInitData
);
emit Events.PostCreated(
postParams,
pubIdAssigned,
actionModulesReturnDatas,
referenceModuleReturnData,
block.timestamp
);
return pubIdAssigned;
}
/**
* @notice Publishes a comment to a given profile via signature.
*
* @param commentParams the CommentParams struct.
*
* @return uint256 The created publication's pubId.
*/
function comment(Types.CommentParams calldata commentParams, address transactionExecutor)
external
returns (uint256)
{
(
uint256 pubIdAssigned,
bytes[] memory actionModulesInitReturnDatas,
bytes memory referenceModuleInitReturnData,
Types.PublicationType[] memory referrerPubTypes
) = _createReferencePublication(
_asReferencePubParams(commentParams),
transactionExecutor,
Types.PublicationType.Comment
);
bytes memory referenceModuleReturnData = _processCommentIfNeeded(
commentParams,
transactionExecutor,
referrerPubTypes
);
emit Events.CommentCreated(
commentParams,
pubIdAssigned,
referenceModuleReturnData,
actionModulesInitReturnDatas,
referenceModuleInitReturnData,
block.timestamp
);
return pubIdAssigned;
}
/**
* @notice Publishes a mirror to a given profile.
*
* @param mirrorParams the MirrorParams struct.
*
* @return uint256 The created publication's pubId.
*/
function mirror(Types.MirrorParams calldata mirrorParams, address transactionExecutor) external returns (uint256) {
ValidationLib.validatePointedPub(mirrorParams.pointedProfileId, mirrorParams.pointedPubId);
ValidationLib.validateNotBlocked({profile: mirrorParams.profileId, byProfile: mirrorParams.pointedProfileId});
Types.PublicationType[] memory referrerPubTypes = ValidationLib.validateReferrersAndGetReferrersPubTypes(
mirrorParams.referrerProfileIds,
mirrorParams.referrerPubIds,
mirrorParams.pointedProfileId,
mirrorParams.pointedPubId
);
uint256 pubIdAssigned = ++StorageLib.getProfile(mirrorParams.profileId).pubCount;
Types.Publication storage _publication = StorageLib.getPublication(mirrorParams.profileId, pubIdAssigned);
_publication.pointedProfileId = mirrorParams.pointedProfileId;
_publication.pointedPubId = mirrorParams.pointedPubId;
_publication.pubType = Types.PublicationType.Mirror;
_fillRootOfPublicationInStorage(_publication, mirrorParams.pointedProfileId, mirrorParams.pointedPubId);
bytes memory referenceModuleReturnData = _processMirrorIfNeeded(
mirrorParams,
transactionExecutor,
referrerPubTypes
);
emit Events.MirrorCreated(mirrorParams, pubIdAssigned, referenceModuleReturnData, block.timestamp);
return pubIdAssigned;
}
/**
* @notice Publishes a quote publication to a given profile via signature.
*
* @param quoteParams the QuoteParams struct.
*
* @return uint256 The created publication's pubId.
*/
function quote(Types.QuoteParams calldata quoteParams, address transactionExecutor) external returns (uint256) {
(
uint256 pubIdAssigned,
bytes[] memory actionModulesReturnDatas,
bytes memory referenceModuleInitReturnData,
Types.PublicationType[] memory referrerPubTypes
) = _createReferencePublication(
_asReferencePubParams(quoteParams),
transactionExecutor,
Types.PublicationType.Quote
);
bytes memory referenceModuleReturnData = _processQuoteIfNeeded(
quoteParams,
transactionExecutor,
referrerPubTypes
);
emit Events.QuoteCreated(
quoteParams,
pubIdAssigned,
referenceModuleReturnData,
actionModulesReturnDatas,
referenceModuleInitReturnData,
block.timestamp
);
return pubIdAssigned;
}
function getPublicationType(uint256 profileId, uint256 pubId) internal view returns (Types.PublicationType) {
Types.Publication storage _publication = StorageLib.getPublication(profileId, pubId);
Types.PublicationType pubType = _publication.pubType;
if (uint8(pubType) == 0) {
// Legacy V1: If the publication type is 0, we check using the legacy rules.
if (_publication.pointedProfileId != 0) {
// It is pointing to a publication, so it can be either a comment or a mirror, depending on if it has a
// collect module or not.
if (_publication.__DEPRECATED__collectModule == address(0)) {
return Types.PublicationType.Mirror;
} else {
return Types.PublicationType.Comment;
}
} else if (_publication.__DEPRECATED__collectModule != address(0)) {
return Types.PublicationType.Post;
}
}
return pubType;
}
function getContentURI(uint256 profileId, uint256 pubId) external view returns (string memory) {
Types.Publication storage _publication = StorageLib.getPublication(profileId, pubId);
Types.PublicationType pubType = _publication.pubType;
if (pubType == Types.PublicationType.Nonexistent) {
pubType = getPublicationType(profileId, pubId);
}
if (pubType == Types.PublicationType.Mirror) {
return StorageLib.getPublication(_publication.pointedProfileId, _publication.pointedPubId).contentURI;
} else {
return StorageLib.getPublication(profileId, pubId).contentURI;
}
}
function _asReferencePubParams(Types.QuoteParams calldata quoteParams)
private
pure
returns (Types.ReferencePubParams calldata referencePubParams)
{
// We use assembly to cast the types keeping the params in calldata, as they match the fields.
assembly {
referencePubParams := quoteParams
}
}
function _asReferencePubParams(Types.CommentParams calldata commentParams)
private
pure
returns (Types.ReferencePubParams calldata referencePubParams)
{
// We use assembly to cast the types keeping the params in calldata, as they match the fields.
assembly {
referencePubParams := commentParams
}
}
function _createReferencePublication(
Types.ReferencePubParams calldata referencePubParams,
address transactionExecutor,
Types.PublicationType referencePubType
)
private
returns (
uint256,
bytes[] memory,
bytes memory,
Types.PublicationType[] memory
)
{
ValidationLib.validatePointedPub(referencePubParams.pointedProfileId, referencePubParams.pointedPubId);
ValidationLib.validateNotBlocked({
profile: referencePubParams.profileId,
byProfile: referencePubParams.pointedProfileId
});
Types.PublicationType[] memory referrerPubTypes = ValidationLib.validateReferrersAndGetReferrersPubTypes(
referencePubParams.referrerProfileIds,
referencePubParams.referrerPubIds,
referencePubParams.pointedProfileId,
referencePubParams.pointedPubId
);
uint256 pubIdAssigned = _fillReferencePublicationStorage(referencePubParams, referencePubType);
bytes[] memory actionModulesReturnDatas = _initPubActionModules(
referencePubParams.profileId,
transactionExecutor,
pubIdAssigned,
referencePubParams.actionModules,
referencePubParams.actionModulesInitDatas
);
bytes memory referenceModuleReturnData = _initPubReferenceModule(
referencePubParams.profileId,
transactionExecutor,
pubIdAssigned,
referencePubParams.referenceModule,
referencePubParams.referenceModuleInitData
);
return (pubIdAssigned, actionModulesReturnDatas, referenceModuleReturnData, referrerPubTypes);
}
function _fillReferencePublicationStorage(
Types.ReferencePubParams calldata referencePubParams,
Types.PublicationType referencePubType
) private returns (uint256) {
uint256 pubIdAssigned = ++StorageLib.getProfile(referencePubParams.profileId).pubCount;
Types.Publication storage _referencePub;
_referencePub = StorageLib.getPublication(referencePubParams.profileId, pubIdAssigned);
_referencePub.pointedProfileId = referencePubParams.pointedProfileId;
_referencePub.pointedPubId = referencePubParams.pointedPubId;
_referencePub.contentURI = referencePubParams.contentURI;
_referencePub.pubType = referencePubType;
_fillRootOfPublicationInStorage(
_referencePub,
referencePubParams.pointedProfileId,
referencePubParams.pointedPubId
);
return pubIdAssigned;
}
function _fillRootOfPublicationInStorage(
Types.Publication storage _publication,
uint256 pointedProfileId,
uint256 pointedPubId
) internal {
Types.Publication storage _pubPointed = StorageLib.getPublication(pointedProfileId, pointedPubId);
Types.PublicationType pubPointedType = _pubPointed.pubType;
if (pubPointedType == Types.PublicationType.Post) {
// The publication pointed is a Lens V2 post.
_publication.rootProfileId = pointedProfileId;
_publication.rootPubId = pointedPubId;
} else if (pubPointedType == Types.PublicationType.Comment || pubPointedType == Types.PublicationType.Quote) {
// The publication pointed is either a Lens V2 comment or a Lens V2 quote.
// Note that even when the publication pointed is a V2 one, it will lack `rootProfileId` and `rootPubId` if
// there is a Lens V1 Legacy publication in the thread of interactions (including the root post itself).
_publication.rootProfileId = _pubPointed.rootProfileId;
_publication.rootPubId = _pubPointed.rootPubId;
}
// Otherwise the root is not filled, as the pointed publication is a Lens V1 Legacy publication, which does not
// support Lens V2 referral system.
}
function _processCommentIfNeeded(
Types.CommentParams calldata commentParams,
address transactionExecutor,
Types.PublicationType[] memory referrerPubTypes
) private returns (bytes memory) {
address refModule = StorageLib
.getPublication(commentParams.pointedProfileId, commentParams.pointedPubId)
.referenceModule;
if (refModule != address(0)) {
try
IReferenceModule(refModule).processComment(
Types.ProcessCommentParams({
profileId: commentParams.profileId,
transactionExecutor: transactionExecutor,
pointedProfileId: commentParams.pointedProfileId,
pointedPubId: commentParams.pointedPubId,
referrerProfileIds: commentParams.referrerProfileIds,
referrerPubIds: commentParams.referrerPubIds,
referrerPubTypes: referrerPubTypes,
data: commentParams.referenceModuleData
})
)
returns (bytes memory returnData) {
return (returnData);
} catch (bytes memory err) {
assembly {
/// Equivalent to reverting with the returned error selector if
/// the length is not zero.
let length := mload(err)
if iszero(iszero(length)) {
revert(add(err, 32), length)
}
}
if (commentParams.referrerProfileIds.length > 0) {
// Deprecated reference modules don't support referrers.
revert Errors.InvalidReferrer();
}
ILegacyReferenceModule(refModule).processComment(
commentParams.profileId,
commentParams.pointedProfileId,
commentParams.pointedPubId,
commentParams.referenceModuleData
);
}
} else {
if (commentParams.referrerProfileIds.length > 0) {
// We don't allow referrers if the reference module is not set.
revert Errors.InvalidReferrer();
}
}
return '';
}
function _processQuoteIfNeeded(
Types.QuoteParams calldata quoteParams,
address transactionExecutor,
Types.PublicationType[] memory referrerPubTypes
) private returns (bytes memory) {
address refModule = StorageLib
.getPublication(quoteParams.pointedProfileId, quoteParams.pointedPubId)
.referenceModule;
if (refModule != address(0)) {
try
IReferenceModule(refModule).processQuote(
Types.ProcessQuoteParams({
profileId: quoteParams.profileId,
transactionExecutor: transactionExecutor,
pointedProfileId: quoteParams.pointedProfileId,
pointedPubId: quoteParams.pointedPubId,
referrerProfileIds: quoteParams.referrerProfileIds,
referrerPubIds: quoteParams.referrerPubIds,
referrerPubTypes: referrerPubTypes,
data: quoteParams.referenceModuleData
})
)
returns (bytes memory returnData) {
return (returnData);
} catch (bytes memory err) {
assembly {
/// Equivalent to reverting with the returned error selector if
/// the length is not zero.
let length := mload(err)
if iszero(iszero(length)) {
revert(add(err, 32), length)
}
}
if (quoteParams.referrerProfileIds.length > 0) {
// Deprecated reference modules don't support referrers.
revert Errors.InvalidReferrer();
}
ILegacyReferenceModule(refModule).processComment(
quoteParams.profileId,
quoteParams.pointedProfileId,
quoteParams.pointedPubId,
quoteParams.referenceModuleData
);
}
} else {
if (quoteParams.referrerProfileIds.length > 0) {
// We don't allow referrers if the reference module is not set.
revert Errors.InvalidReferrer();
}
}
return '';
}
function _processMirrorIfNeeded(
Types.MirrorParams calldata mirrorParams,
address transactionExecutor,
Types.PublicationType[] memory referrerPubTypes
) private returns (bytes memory) {
address refModule = StorageLib
.getPublication(mirrorParams.pointedProfileId, mirrorParams.pointedPubId)
.referenceModule;
if (refModule != address(0)) {
try
IReferenceModule(refModule).processMirror(
Types.ProcessMirrorParams({
profileId: mirrorParams.profileId,
transactionExecutor: transactionExecutor,
pointedProfileId: mirrorParams.pointedProfileId,
pointedPubId: mirrorParams.pointedPubId,
referrerProfileIds: mirrorParams.referrerProfileIds,
referrerPubIds: mirrorParams.referrerPubIds,
referrerPubTypes: referrerPubTypes,
data: mirrorParams.referenceModuleData
})
)
returns (bytes memory returnData) {
return (returnData);
} catch (bytes memory err) {
assembly {
/// Equivalent to reverting with the returned error selector if
/// the length is not zero.
let length := mload(err)
if iszero(iszero(length)) {
revert(add(err, 32), length)
}
}
if (mirrorParams.referrerProfileIds.length > 0) {
// Deprecated reference modules don't support referrers.
revert Errors.InvalidReferrer();
}
ILegacyReferenceModule(refModule).processMirror(
mirrorParams.profileId,
mirrorParams.pointedProfileId,
mirrorParams.pointedPubId,
mirrorParams.referenceModuleData
);
}
} else {
if (mirrorParams.referrerProfileIds.length > 0) {
// We don't allow referrers if the reference module is not set.
revert Errors.InvalidReferrer();
}
}
return '';
}
function _initPubActionModules(
uint256 profileId,
address transactionExecutor,
uint256 pubId,
address[] calldata actionModules,
bytes[] calldata actionModulesInitDatas
) private returns (bytes[] memory) {
if (actionModules.length != actionModulesInitDatas.length) {
revert Errors.ArrayMismatch();
}
bytes[] memory actionModuleInitResults = new bytes[](actionModules.length);
uint256 enabledActionModulesBitmap;
uint256 i;
while (i < actionModules.length) {
Types.ActionModuleWhitelistData memory actionModuleWhitelistData = StorageLib.actionModuleWhitelistData()[
actionModules[i]
];
if (!actionModuleWhitelistData.isWhitelisted) {
revert Errors.NotWhitelisted();
}
uint256 actionModuleIdBitmapMask = 1 << (actionModuleWhitelistData.id - 1);
if (enabledActionModulesBitmap & actionModuleIdBitmapMask != 0) {
revert Errors.AlreadyEnabled();
}
enabledActionModulesBitmap |= actionModuleIdBitmapMask;
actionModuleInitResults[i] = IPublicationActionModule(actionModules[i]).initializePublicationAction(
profileId,
pubId,
transactionExecutor,
actionModulesInitDatas[i]
);
unchecked {
++i;
}
}
StorageLib.getPublication(profileId, pubId).enabledActionModulesBitmap = enabledActionModulesBitmap;
return actionModuleInitResults;
}
function _initPubReferenceModule(
uint256 profileId,
address transactionExecutor,
uint256 pubId,
address referenceModule,
bytes memory referenceModuleInitData
) private returns (bytes memory) {
if (referenceModule == address(0)) {
return new bytes(0);
}
ValidationLib.validateReferenceModuleWhitelisted(referenceModule);
StorageLib.getPublication(profileId, pubId).referenceModule = referenceModule;
return
IReferenceModule(referenceModule).initializeReferenceModule(
profileId,
pubId,
transactionExecutor,
referenceModuleInitData
);
}
}