[bugfix] Read Bookwyrm Articles more thoroughly (#1410)

This commit is contained in:
tobi 2023-02-02 16:41:02 +01:00 committed by GitHub
commit 271da016b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 123 additions and 118 deletions

View file

@ -86,9 +86,10 @@ func (c *converter) ASRepresentationToAccount(ctx context.Context, accountable a
// display name aka name
// we default to the username, but take the more nuanced name property if it exists
acct.DisplayName = username
if displayName, err := ap.ExtractName(accountable); err == nil {
if displayName := ap.ExtractName(accountable); displayName != "" {
acct.DisplayName = displayName
} else {
acct.DisplayName = username
}
// account emojis (used in bio, display name, fields)
@ -101,10 +102,7 @@ func (c *converter) ASRepresentationToAccount(ctx context.Context, accountable a
// TODO: fields aka attachment array
// note aka summary
note, err := ap.ExtractSummary(accountable)
if err == nil && note != "" {
acct.Note = note
}
acct.Note = ap.ExtractSummary(accountable)
// check for bot and actor type
switch accountable.GetTypeName() {
@ -219,6 +217,38 @@ func (c *converter) ASRepresentationToAccount(ctx context.Context, accountable a
return acct, nil
}
func (c *converter) extractAttachments(i ap.WithAttachment) []*gtsmodel.MediaAttachment {
attachmentProp := i.GetActivityStreamsAttachment()
if attachmentProp == nil {
return nil
}
attachments := make([]*gtsmodel.MediaAttachment, 0, attachmentProp.Len())
for iter := attachmentProp.Begin(); iter != attachmentProp.End(); iter = iter.Next() {
t := iter.GetType()
if t == nil {
continue
}
attachmentable, ok := t.(ap.Attachmentable)
if !ok {
log.Error("ap attachment was not attachmentable")
continue
}
attachment, err := ap.ExtractAttachment(attachmentable)
if err != nil {
log.Errorf("error extracting attachment: %s", err)
continue
}
attachments = append(attachments, attachment)
}
return attachments
}
func (c *converter) ASStatusToStatus(ctx context.Context, statusable ap.Statusable) (*gtsmodel.Status, error) {
status := &gtsmodel.Status{}
@ -243,11 +273,7 @@ func (c *converter) ASStatusToStatus(ctx context.Context, statusable ap.Statusab
status.Content = ap.ExtractContent(statusable)
// attachments to dereference and fetch later on (we don't do that here)
if attachments, err := ap.ExtractAttachments(statusable); err != nil {
l.Infof("ASStatusToStatus: error extracting status attachments: %s", err)
} else {
status.Attachments = attachments
}
status.Attachments = c.extractAttachments(statusable)
// hashtags to dereference later on
if hashtags, err := ap.ExtractHashtags(statusable); err != nil {
@ -271,10 +297,11 @@ func (c *converter) ASStatusToStatus(ctx context.Context, statusable ap.Statusab
}
// cw string for this status
if cw, err := ap.ExtractSummary(statusable); err != nil {
l.Infof("ASStatusToStatus: error extracting status summary: %s", err)
// prefer Summary, fall back to Name
if summary := ap.ExtractSummary(statusable); summary != "" {
status.ContentWarning = summary
} else {
status.ContentWarning = cw
status.ContentWarning = ap.ExtractName(statusable)
}
// when was this status created?

View file

@ -215,6 +215,60 @@ func (suite *ASToInternalTestSuite) TestParseOwncastService() {
fmt.Printf("\n\n\n%s\n\n\n", string(b))
}
func (suite *ASToInternalTestSuite) TestParseBookwyrmStatus() {
authorAccount := suite.testAccounts["remote_account_1"]
raw := `{
"id": "` + authorAccount.URI + `/review/445260",
"type": "Article",
"published": "2022-11-09T16:34:28.488375+00:00",
"attributedTo": "` + authorAccount.URI + `",
"content": "<p>The original novel is a great read. Not just for the way it codified modern vampire lore. But for the way it's built entirely out of diary entries, letters, news fragments, telegrams and so on. For the way it shows modern science coming to grips with ancient superstition and figuring out how to deal with it. For showing an early example of a woman participating in her own rescue. And for some of the parts that <em>didn't</em> make it into general pop culture. (Count Dracula spends an awful lot of time in a shipping box.)</p>\n<p>In some senses it's the written-word equivalent of the \"found footage\" horror genre. Except the \"sources\" are wildly varying. John and Mina write their journals and letters to each other in shorthand. Business letters are of course written formally. Dr. Seward keeps an audio diary on a phonograph. Van Helsing's speech is rendered with every quirk of his Dutch accent and speech patterns. And then halfway through the book, when all the major characters finally come together...they collate all the documents and Mina transcribes them on a typewriter, and they pass around the first half of the book so they can all read up on what the rest of them have been doing! (Literally getting them all on the same page.)</p>\n<p>That's not to say it's flawless. It's unclear why some victims rise again as vampires while others don't. While the science/superstition contrast works well for the most part, eastern Europeans don't exactly come off very well. Especially when they'd talk about the \"gypsies\" carrying Dracula around Transylvania. I mean, it could have been a lot worse, but it's still jarring.</p>\n<p>Overall, though, it's an engaging read, whether approached as a book or, as Dracula Daily did, one day's letters at a time from May 3 through November 7. </p>\n<p>Dracula Daily: <a href=\"https://draculadaily.example.org/archive\">draculadaily.example.org/archive</a></p>\n<p>This review on my website: <a href=\"https://example.org/reviews/books/dracula/\">example.org/reviews/books/dracula/</a></p>",
"to": [
"https://www.w3.org/ns/activitystreams#Public"
],
"cc": [
"` + authorAccount.FollowersURI + `"
],
"replies": {
"id": "` + authorAccount.URI + `/review/445260/replies",
"type": "OrderedCollection",
"totalItems": 0,
"first": "` + authorAccount.URI + `/review/445260/replies?page=1",
"last": "` + authorAccount.URI + `/review/445260/replies?page=1",
"@context": "https://www.w3.org/ns/activitystreams"
},
"tag": [],
"attachment": [
{
"type": "Document",
"url": "` + authorAccount.URI + `/review/445260/images/previews/covers/451118-5f7bd96e-ca03-4865-ab14-baa7addaca8e.jpg",
"name": "Dracula (Paperback, 1992, Signet)",
"@context": "https://www.w3.org/ns/activitystreams"
}
],
"sensitive": false,
"inReplyToBook": "https://bookwyrm.social/book/451118",
"name": "Review of \"Dracula\" (5 stars): A great read, not just for codifying vampire lore, but the way it's built from letters and diaries.",
"rating": 5,
"@context": "https://www.w3.org/ns/activitystreams"
}`
t := suite.jsonToType(raw)
asArticle, ok := t.(ap.Statusable)
if !ok {
suite.FailNow("type not coercible")
}
status, err := suite.typeconverter.ASStatusToStatus(context.Background(), asArticle)
if err != nil {
suite.FailNow(err.Error())
}
suite.Equal("Review of \"Dracula\" (5 stars): A great read, not just for codifying vampire lore, but the way it's built from letters and diaries.", status.ContentWarning)
suite.Len(status.Attachments, 1)
}
func (suite *ASToInternalTestSuite) TestParseFlag1() {
reportedAccount := suite.testAccounts["local_account_1"]
reportingAccount := suite.testAccounts["remote_account_1"]