Example #1
0
 def from_dict(cls, d, signal_type_mapping: HMASignalTypeMapping):
     base = cls(
         **{f.name: d.get(f.name, None)
            for f in dataclasses.fields(cls)})
     base.content_type = signal_type_mapping.get_content_type_enforce(
         t.cast(str, base.content_type))
     return t.cast("MatchesForMediaRequest", base)
Example #2
0
 def from_dict(cls, d, signal_type_mapping: HMASignalTypeMapping):
     base = cls(
         **{f.name: d.get(f.name, None)
            for f in dataclasses.fields(cls)})
     base.content_type = signal_type_mapping.get_content_type_enforce(
         base.content_type  # type:ignore
     )
     return base
Example #3
0
 def from_dict(
         cls, d: t.Dict, signal_type_mapping: HMASignalTypeMapping
 ) -> "MatchesForHashRequest":
     base = cls(
         **{f.name: d.get(f.name, None)
            for f in dataclasses.fields(cls)})
     base.signal_type = signal_type_mapping.get_signal_type_enforce(
         t.cast(str, base.signal_type))
     return t.cast("MatchesForHashRequest", base)
Example #4
0
 def from_sqs_message(
         cls, d: dict, signal_type_mapping: HMASignalTypeMapping
 ) -> "URLSubmissionMessage":
     return cls(
         content_type=signal_type_mapping.get_content_type_enforce(
             d["ContentType"]),
         content_id=d["ContentId"],
         url=d["URL"],
         event_type=d["EventType"],
     )
Example #5
0
 def from_sqs_message(
         cls, d: dict,
         signal_type_mapping: HMASignalTypeMapping) -> "PipelineHashRecord":
     return cls(
         content_id=d["ContentId"],
         signal_type=signal_type_mapping.get_signal_type_enforce(
             d["SignalType"]),
         content_hash=d["ContentHash"],
         signal_specific_attributes=d["SignalSpecificAttributes"],
         updated_at=datetime.datetime.fromisoformat(d["UpdatedAt"]),
     )
Example #6
0
 def from_dynamodb_item(
         cls, item: t.Dict,
         signal_type_mapping: HMASignalTypeMapping) -> "BankMemberSignal":
     return cls(
         bank_id=item["BankId"],
         bank_member_id=item["BankMemberId"],
         signal_id=item["SignalId"],
         signal_type=signal_type_mapping.get_signal_type_enforce(
             item["SignalType"]),
         signal_value=item["SignalValue"],
         updated_at=datetime.fromisoformat(item["UpdatedAt"]),
     )
Example #7
0
 def _result_item_to_metadata(
     cls,
     item: t.Dict,
     signal_type_mapping: HMASignalTypeMapping,
 ) -> "ThreatExchangeSignalMetadata":
     return ThreatExchangeSignalMetadata(
         signal_id=cls.remove_signal_key_prefix(item["PK"],
                                                item["SignalSource"]),
         privacy_group_id=item["PrivacyGroup"],
         updated_at=datetime.datetime.fromisoformat(item["UpdatedAt"]),
         signal_type=signal_type_mapping.get_signal_type_enforce(
             item["SignalType"]),
         signal_hash=item["SignalHash"],
         tags=item["Tags"],
         pending_opinion_change=PendingThreatExchangeOpinionChange(
             item.get(
                 "PendingThreatExchangeOpinionChange",
                 PendingThreatExchangeOpinionChange.NONE.value,
             )),
     )
Example #8
0
 def _result_items_to_records(
     cls,
     items: t.List[t.Dict],
     signal_type_mapping: HMASignalTypeMapping,
 ) -> t.List["PipelineHashRecord"]:
     """
     Get a paginated list of recent hash records. Subsequent calls must use
     `return_value.last_evaluated_key`.
     """
     return [
         PipelineHashRecord(
             content_id=item["PK"][len(cls.CONTENT_KEY_PREFIX):],
             signal_type=signal_type_mapping.get_signal_type_enforce(
                 item["SignalType"]),
             content_hash=item["ContentHash"],
             updated_at=datetime.datetime.fromisoformat(item["UpdatedAt"]),
             signal_specific_attributes=cls.
             deserialize_signal_specific_attributes(item),
         ) for item in items
     ]
Example #9
0
 def from_dynamodb_item(
         cls, item: t.Dict,
         signal_type_mapping: HMASignalTypeMapping) -> "BankMember":
     content_type = signal_type_mapping.get_content_type_enforce(
         item["ContentType"])
     return cls(
         bank_id=item["BankId"],
         bank_member_id=item["BankMemberId"],
         content_type=content_type,
         storage_bucket=item["StorageBucket"],
         storage_key=item["StorageKey"],
         raw_content=item["RawContent"],
         notes=item["Notes"],
         created_at=datetime.fromisoformat(item["CreatedAt"]),
         updated_at=datetime.fromisoformat(item["UpdatedAt"]),
         is_removed=item["IsRemoved"],
         is_media_unavailable=item["IsMediaUnavailable"],
         # tags default to empty set
         bank_member_tags=cls.dynamodb_attribute_to_set(
             item.get("BankMemberTags", set())),
     )
Example #10
0
 def _result_items_to_records(
     cls,
     items: t.List[t.Dict],
     signal_type_mapping: HMASignalTypeMapping,
 ) -> t.List["MatchRecord"]:
     return [
         MatchRecord(
             content_id=cls.remove_content_key_prefix(item["PK"]),
             content_hash=item["ContentHash"],
             updated_at=datetime.datetime.fromisoformat(item["UpdatedAt"]),
             signal_type=signal_type_mapping.get_signal_type_enforce(
                 item["SignalType"]),
             signal_id=cls.remove_signal_key_prefix(item["SK"],
                                                    item["SignalSource"]),
             signal_source=item["SignalSource"],
             signal_hash=item["SignalHash"],
             signal_specific_attributes=cls.
             deserialize_signal_specific_attributes(item),
             match_distance=item.get("MatchDistance"),
         ) for item in items
     ]
Example #11
0
 def _result_item_to_object(
         cls, item: t.Dict,
         signal_type_mapping: HMASignalTypeMapping) -> "ContentObject":
     content_ref_type = ContentRefType(item["ContentRefType"])
     content_type = signal_type_mapping.get_content_type_enforce(
         item["ContentType"])
     # This value is added in the case that no additional fields
     # were provided and can be safely discarded.
     item["AdditionalFields"].discard(cls.ADDITIONAL_FIELDS_PLACE_HOLDER)
     return ContentObject(
         content_id=cls.remove_content_key_prefix(item["PK"], ),
         content_type=content_type,
         content_ref=item["ContentRef"],
         content_ref_type=content_ref_type,
         additional_fields=item["AdditionalFields"],
         # Notes careful not using this version to write back to the table...
         # Will dup previous submissions...
         submission_times=[
             datetime.datetime.fromisoformat(s)
             for s in item["SubmissionTimes"]
         ],
         created_at=datetime.datetime.fromisoformat(item["CreatedAt"]),
         updated_at=datetime.datetime.fromisoformat(item["UpdatedAt"]),
     )
Example #12
0
 def from_dict(cls, d, signal_type_mapping: HMASignalTypeMapping):
     base = super().from_dict(d, signal_type_mapping)
     base.signal_type = signal_type_mapping.get_signal_type_enforce(
         base.signal_type)
     return base
Example #13
0
def get_default_signal_type_mapping() -> HMASignalTypeMapping:
    return HMASignalTypeMapping(
        content_types=[PhotoContent, VideoContent],
        signal_types=[PdqSignal, VideoMD5Signal],
    )