process: Replace closure

This commit is contained in:
Mike Dilger 2024-08-30 11:10:04 +12:00
parent 24bc41f1b5
commit 7c92bdd8f5

View File

@ -543,362 +543,362 @@ pub(crate) fn process_relationships_of_event<'a>(
) -> Result<Vec<Id>, Error> {
let mut invalidate: Vec<Id> = Vec::new();
let mut f = |txn: &mut RwTxn<'a>| -> Result<(), Error> {
// timestamps
if event.kind == EventKind::Timestamp {
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.db().write_relationship_by_id(
id,
event.id,
RelationshipById::Timestamps,
Some(txn),
)?;
}
}
let mut local_txn = None;
let txn = match rw_txn {
Some(x) => x,
None => {
local_txn = Some(GLOBALS.db().get_write_txn()?);
local_txn.as_mut().unwrap()
}
};
// deletes
if let Some((vec, reason)) = event.deletes() {
for er in vec.iter() {
match er {
EventReference::Id { id, .. } => {
// If we have the event,
// Actually delete at this point in some cases
if let Some(deleted_event) = GLOBALS.db().read_event(*id)? {
if !deleted_event.delete_author_allowed(event.pubkey) {
// No further processing if not a valid delete
continue;
}
invalidate.push(deleted_event.id);
if !deleted_event.kind.is_feed_displayable() {
// Otherwise actually delete (PITA to do otherwise)
GLOBALS.db().delete_event(deleted_event.id, Some(txn))?;
}
}
// Store the delete (we either don't have the target to verify,
// or we just verified above. In the former case, it is okay because
// we verify on usage)
GLOBALS.db().write_relationship_by_id(
*id,
event.id,
RelationshipById::Deletes {
by: event.pubkey,
reason: reason.clone(),
},
Some(txn),
)?;
}
EventReference::Addr(ea) => {
// If we have the event,
// Actually delete at this point in some cases
if let Some(deleted_event) = GLOBALS
.db()
.get_replaceable_event(ea.kind, ea.author, &ea.d)?
{
if !deleted_event.delete_author_allowed(event.pubkey) {
// No further processing if not a valid delete
continue;
}
invalidate.push(deleted_event.id);
if !deleted_event.kind.is_feed_displayable() {
// Otherwise actually delete (PITA to do otherwise)
GLOBALS.db().delete_event(deleted_event.id, Some(txn))?;
}
}
// Store the delete (we either don't have the target to verify,
// or we just verified above. In the former case, it is okay because
// we verify on usage)
GLOBALS.db().write_relationship_by_addr(
ea.clone(),
event.id,
RelationshipByAddr::Deletes {
by: event.pubkey,
reason: reason.clone(),
},
Some(txn),
)?;
}
}
}
}
// reacts to
if let Some((reacted_to_id, reaction, _maybe_url)) = event.reacts_to() {
// NOTE: reactions may precede the event they react to. So we cannot validate here.
GLOBALS.db().write_relationship_by_id(
reacted_to_id, // event reacted to
event.id, // the reaction event id
RelationshipById::ReactsTo {
by: event.pubkey,
reaction,
},
Some(txn),
)?;
invalidate.push(reacted_to_id);
}
// labels
if event.kind == EventKind::Label {
// Get the label from the "l" tag
let mut label = "";
let mut namespace = "";
for t in &event.tags {
if t.tagname() == "l" {
if t.value() != "" {
label = t.value();
if t.get_index(2) != "" {
namespace = t.get_index(2);
}
}
}
}
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.db().write_relationship_by_id(
id,
event.id,
RelationshipById::Labels {
label: label.to_owned(),
namespace: namespace.to_owned(),
},
Some(txn),
)?;
} else if let Ok((ea, _marker)) = tag.parse_address() {
GLOBALS.db().write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::Labels {
label: label.to_owned(),
namespace: namespace.to_owned(),
},
Some(txn),
)?;
}
}
}
// ListMutesThread
if event.kind == EventKind::MuteList {
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.db().write_relationship_by_id(
id,
event.id,
RelationshipById::Mutes,
Some(txn),
)?;
}
}
}
// ListPins
if event.kind == EventKind::PinList {
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.db().write_relationship_by_id(
id,
event.id,
RelationshipById::Pins,
Some(txn),
)?;
}
}
}
// Maybe update global's cache of bookmarks
if event.kind == EventKind::BookmarkList {
// Only if it is ours
if let Some(pk) = GLOBALS.identity.public_key() {
if pk == event.pubkey {
// Only if this event is the latest (it is already stored so we can do this check)
if let Some(newest_event) =
GLOBALS
.db()
.get_replaceable_event(EventKind::BookmarkList, pk, "")?
{
if newest_event == *event {
*GLOBALS.bookmarks.write() = BookmarkList::from_event(event)?;
GLOBALS.recompute_current_bookmarks.notify_one();
}
}
}
}
}
// NOTE: we do not store Bookmarks or Curates relationships anymore.
if event.kind == EventKind::LiveChatMessage {
for tag in &event.tags {
if let Ok((ea, _marker)) = tag.parse_address() {
GLOBALS.db().write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::ChatsWithin,
Some(txn),
)?;
}
}
}
if event.kind == EventKind::BadgeAward {
for tag in &event.tags {
if let Ok((ea, _marker)) = tag.parse_address() {
GLOBALS.db().write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::AwardsBadge,
Some(txn),
)?;
}
}
}
if event.kind == EventKind::HandlerRecommendation {
for tag in &event.tags {
if let Ok((ea, _marker)) = tag.parse_address() {
GLOBALS.db().write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::RecommendsHandler,
Some(txn),
)?;
}
}
}
if event.kind == EventKind::Reporting {
for tag in &event.tags {
if let Ok((id, Some(rurl), _)) = tag.parse_event() {
let report = &rurl.0;
GLOBALS.db().write_relationship_by_id(
id,
event.id,
RelationshipById::Reports(report.to_owned()),
Some(txn),
)?;
}
}
}
// zaps
if let Ok(Some(zapdata)) = event.zaps() {
GLOBALS.db().write_relationship_by_id(
zapdata.id,
event.id,
RelationshipById::Zaps {
by: event.pubkey,
amount: zapdata.amount,
},
Some(txn),
)?;
invalidate.push(zapdata.id);
}
// JobResult
if event.kind.is_job_result() {
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.db().write_relationship_by_id(
id,
event.id,
RelationshipById::SuppliesJobResult,
Some(txn),
)?;
}
}
}
// Reposts
if event.kind == EventKind::Repost {
if let Ok(inner_event) = serde_json::from_str::<Event>(&event.content) {
// timestamps
if event.kind == EventKind::Timestamp {
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.db().write_relationship_by_id(
inner_event.id,
id,
event.id,
RelationshipById::Reposts,
RelationshipById::Timestamps,
Some(txn),
)?;
}
}
}
// deletes
if let Some((vec, reason)) = event.deletes() {
for er in vec.iter() {
match er {
EventReference::Id { id, .. } => {
// If we have the event,
// Actually delete at this point in some cases
if let Some(deleted_event) = GLOBALS.db().read_event(*id)? {
if !deleted_event.delete_author_allowed(event.pubkey) {
// No further processing if not a valid delete
continue;
}
invalidate.push(deleted_event.id);
if !deleted_event.kind.is_feed_displayable() {
// Otherwise actually delete (PITA to do otherwise)
GLOBALS.db().delete_event(deleted_event.id, Some(txn))?;
}
}
// Store the delete (we either don't have the target to verify,
// or we just verified above. In the former case, it is okay because
// we verify on usage)
GLOBALS.db().write_relationship_by_id(
*id,
event.id,
RelationshipById::Deletes {
by: event.pubkey,
reason: reason.clone(),
},
Some(txn),
)?;
}
EventReference::Addr(ea) => {
// If we have the event,
// Actually delete at this point in some cases
if let Some(deleted_event) = GLOBALS
.db()
.get_replaceable_event(ea.kind, ea.author, &ea.d)?
{
if !deleted_event.delete_author_allowed(event.pubkey) {
// No further processing if not a valid delete
continue;
}
invalidate.push(deleted_event.id);
if !deleted_event.kind.is_feed_displayable() {
// Otherwise actually delete (PITA to do otherwise)
GLOBALS.db().delete_event(deleted_event.id, Some(txn))?;
}
}
// Store the delete (we either don't have the target to verify,
// or we just verified above. In the former case, it is okay because
// we verify on usage)
GLOBALS.db().write_relationship_by_addr(
ea.clone(),
event.id,
RelationshipByAddr::Deletes {
by: event.pubkey,
reason: reason.clone(),
},
Some(txn),
)?;
}
}
}
}
// reacts to
if let Some((reacted_to_id, reaction, _maybe_url)) = event.reacts_to() {
// NOTE: reactions may precede the event they react to. So we cannot validate here.
GLOBALS.db().write_relationship_by_id(
reacted_to_id, // event reacted to
event.id, // the reaction event id
RelationshipById::ReactsTo {
by: event.pubkey,
reaction,
},
Some(txn),
)?;
invalidate.push(reacted_to_id);
}
// labels
if event.kind == EventKind::Label {
// Get the label from the "l" tag
let mut label = "";
let mut namespace = "";
for t in &event.tags {
if t.tagname() == "l" {
if t.value() != "" {
label = t.value();
if t.get_index(2) != "" {
namespace = t.get_index(2);
}
}
}
}
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.db().write_relationship_by_id(
id,
event.id,
RelationshipById::Labels {
label: label.to_owned(),
namespace: namespace.to_owned(),
},
Some(txn),
)?;
} else if let Ok((ea, _marker)) = tag.parse_address() {
GLOBALS.db().write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::Labels {
label: label.to_owned(),
namespace: namespace.to_owned(),
},
Some(txn),
)?;
}
}
}
// ListMutesThread
if event.kind == EventKind::MuteList {
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.db().write_relationship_by_id(
id,
event.id,
RelationshipById::Mutes,
Some(txn),
)?;
}
}
}
// ListPins
if event.kind == EventKind::PinList {
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.db().write_relationship_by_id(
id,
event.id,
RelationshipById::Pins,
Some(txn),
)?;
}
}
}
// Maybe update global's cache of bookmarks
if event.kind == EventKind::BookmarkList {
// Only if it is ours
if let Some(pk) = GLOBALS.identity.public_key() {
if pk == event.pubkey {
// Only if this event is the latest (it is already stored so we can do this check)
if let Some(newest_event) =
GLOBALS
.db()
.get_replaceable_event(EventKind::BookmarkList, pk, "")?
{
if newest_event == *event {
*GLOBALS.bookmarks.write() = BookmarkList::from_event(event)?;
GLOBALS.recompute_current_bookmarks.notify_one();
}
}
}
}
}
// NOTE: we do not store Bookmarks or Curates relationships anymore.
if event.kind == EventKind::LiveChatMessage {
for tag in &event.tags {
if let Ok((ea, _marker)) = tag.parse_address() {
GLOBALS.db().write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::ChatsWithin,
Some(txn),
)?;
}
}
}
if event.kind == EventKind::BadgeAward {
for tag in &event.tags {
if let Ok((ea, _marker)) = tag.parse_address() {
GLOBALS.db().write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::AwardsBadge,
Some(txn),
)?;
}
}
}
if event.kind == EventKind::HandlerRecommendation {
for tag in &event.tags {
if let Ok((ea, _marker)) = tag.parse_address() {
GLOBALS.db().write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::RecommendsHandler,
Some(txn),
)?;
}
}
}
if event.kind == EventKind::Reporting {
for tag in &event.tags {
if let Ok((id, Some(rurl), _)) = tag.parse_event() {
let report = &rurl.0;
GLOBALS.db().write_relationship_by_id(
id,
event.id,
RelationshipById::Reports(report.to_owned()),
Some(txn),
)?;
}
}
}
// zaps
if let Ok(Some(zapdata)) = event.zaps() {
GLOBALS.db().write_relationship_by_id(
zapdata.id,
event.id,
RelationshipById::Zaps {
by: event.pubkey,
amount: zapdata.amount,
},
Some(txn),
)?;
invalidate.push(zapdata.id);
}
// JobResult
if event.kind.is_job_result() {
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.db().write_relationship_by_id(
id,
event.id,
RelationshipById::SuppliesJobResult,
Some(txn),
)?;
}
}
}
// Reposts
if event.kind == EventKind::Repost {
if let Ok(inner_event) = serde_json::from_str::<Event>(&event.content) {
GLOBALS.db().write_relationship_by_id(
inner_event.id,
event.id,
RelationshipById::Reposts,
Some(txn),
)?;
} else {
for eref in event.mentions().iter() {
if let EventReference::Id { id, .. } = eref {
GLOBALS.db().write_relationship_by_id(
*id,
event.id,
RelationshipById::Reposts,
Some(txn),
)?;
}
}
}
}
// Quotes
for eref in event.quotes().iter() {
if let EventReference::Id { id, .. } = eref {
GLOBALS.db().write_relationship_by_id(
*id,
event.id,
RelationshipById::Quotes,
Some(txn),
)?;
}
}
// RepliesTo (or Annotation)
match event.replies_to() {
Some(EventReference::Id { id, .. }) => {
if event.is_annotation() {
GLOBALS.db().write_relationship_by_id(
id,
event.id,
RelationshipById::Annotates,
Some(txn),
)?;
invalidate.push(id);
} else {
GLOBALS.db().write_relationship_by_id(
id,
event.id,
RelationshipById::RepliesTo,
Some(txn),
)?;
}
}
Some(EventReference::Addr(ea)) => {
if event.is_annotation() {
GLOBALS.db().write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::Annotates,
Some(txn),
)?;
} else {
for eref in event.mentions().iter() {
if let EventReference::Id { id, .. } = eref {
GLOBALS.db().write_relationship_by_id(
*id,
event.id,
RelationshipById::Reposts,
Some(txn),
)?;
}
}
}
}
// Quotes
for eref in event.quotes().iter() {
if let EventReference::Id { id, .. } = eref {
GLOBALS.db().write_relationship_by_id(
*id,
GLOBALS.db().write_relationship_by_addr(
ea,
event.id,
RelationshipById::Quotes,
RelationshipByAddr::RepliesTo,
Some(txn),
)?;
}
}
None => (),
}
// RepliesTo (or Annotation)
match event.replies_to() {
Some(EventReference::Id { id, .. }) => {
if event.is_annotation() {
GLOBALS.db().write_relationship_by_id(
id,
event.id,
RelationshipById::Annotates,
Some(txn),
)?;
invalidate.push(id);
} else {
GLOBALS.db().write_relationship_by_id(
id,
event.id,
RelationshipById::RepliesTo,
Some(txn),
)?;
}
}
Some(EventReference::Addr(ea)) => {
if event.is_annotation() {
GLOBALS.db().write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::Annotates,
Some(txn),
)?;
} else {
GLOBALS.db().write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::RepliesTo,
Some(txn),
)?;
}
}
None => (),
}
Ok(())
};
match rw_txn {
Some(txn) => f(txn)?,
None => {
let mut txn = GLOBALS.db().get_write_txn()?;
f(&mut txn)?;
txn.commit()?;
}
};
if let Some(txn) = local_txn {
txn.commit()?;
}
Ok(invalidate)
}