Skip to content

Commit

Permalink
feat: shorter event format
Browse files Browse the repository at this point in the history
  • Loading branch information
akurilov committed Aug 6, 2024
1 parent 3724400 commit 52f4ef0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
22 changes: 13 additions & 9 deletions service/converter/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ const ceTypePrefixFollowersOnly = "com_awakari_mastodon_"

var ErrFail = errors.New("failed to convert")

var htmlStripTags = bluemonday.
StrictPolicy().
AddSpaceWhenStrippingTag(true)

func NewService(ceType, urlBase, evtReaderBase string, actorType vocab.ActivityVocabularyType) Service {
return service{
ceType: ceType,
Expand Down Expand Up @@ -629,7 +633,7 @@ func (svc service) ConvertEventToActivity(ctx context.Context, evt *pb.CloudEven
}

txt := eventSummaryText(evt)
txt = bluemonday.StrictPolicy().Sanitize(txt)
txt = htmlStripTags.Sanitize(txt)
txt = strings.ReplaceAll(txt, "\n", "<br/>")
txt = truncateStringUtf8(txt, fmtLenMaxBodyTxt)

Expand Down Expand Up @@ -676,12 +680,12 @@ func (svc service) ConvertEventToActivity(ctx context.Context, evt *pb.CloudEven
if addrOrigin == "" {
addrOrigin = evt.Source
}
obj.ID = vocab.ID(addrOrigin)
obj.ID = a.ID
obj.URL = vocab.IRI(addrOrigin)

txt += fmt.Sprintf(
"<br/><br/><a href=\"%s\">%s</a><br/><br/><a href=\"%s\">Attributes</a>",
addrOrigin, addrOrigin, a.ID,
"<br/>Original: <a href=\"%s\">%s</a><br/>Attributes: <a href=\"%s\">%s</a>",
addrOrigin, addrOrigin, a.URL, a.URL,
)
obj.Content = vocab.DefaultNaturalLanguageValue(txt)

Expand Down Expand Up @@ -727,11 +731,11 @@ func (svc service) ConvertEventToActivity(ctx context.Context, evt *pb.CloudEven
obj.To = a.To
obj.CC = a.CC
var addrReplies vocab.ID
switch strings.HasSuffix(obj.ID.String(), "/") {
switch strings.HasSuffix(obj.URL.GetLink().String(), "/") {
case true:
addrReplies = obj.ID + "replies"
addrReplies = obj.URL.GetLink() + "replies"
default:
addrReplies = obj.ID + "/replies"
addrReplies = obj.URL.GetLink() + "/replies"
}
replies := vocab.CollectionNew(addrReplies)
obj.Replies = replies
Expand Down Expand Up @@ -801,8 +805,8 @@ func (svc service) ConvertEventToActorUpdate(ctx context.Context, evt *pb.CloudE
}

func (svc service) initActivity(evt *pb.CloudEvent, interestId string, follower *vocab.Actor, t *time.Time, a *vocab.Activity) {
a.ID = vocab.ID(svc.urlReaderEvtBase + "/" + evt.Id)
a.URL = a.ID
a.ID = vocab.ID(svc.urlBase + "/" + evt.Id)
a.URL = vocab.IRI(svc.urlReaderEvtBase + "/" + evt.Id)
a.Context = vocab.IRI(model.NsAs)
a.Actor = vocab.ID(fmt.Sprintf("%s/actor/%s", svc.urlBase, interestId))
switch t {
Expand Down
8 changes: 4 additions & 4 deletions service/converter/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func TestService_ConvertEventToActivity(t *testing.T) {
Name: vocab.DefaultNaturalLanguageValue("John Doe"),
},
dst: vocab.Activity{
ID: "https://reader/evt/2jrVcFeXfGNcExKHLCcrrXBYyLJ",
ID: "https://base/2jrVcFeXfGNcExKHLCcrrXBYyLJ",
URL: vocab.IRI("https://reader/evt/2jrVcFeXfGNcExKHLCcrrXBYyLJ"),
Type: "Create",
Context: vocab.IRI("https://www.w3.org/ns/activitystreams"),
Expand All @@ -597,7 +597,7 @@ func TestService_ConvertEventToActivity(t *testing.T) {
},
Published: ts,
Object: &vocab.Object{
ID: "https://otakukart.com/the-10-must-watch-futuristic-anime-that-every-fan-should-see/",
ID: "https://base/2jrVcFeXfGNcExKHLCcrrXBYyLJ",
Type: "Note",
Name: vocab.NaturalLanguageValues{},
AttributedTo: vocab.IRI("https://otakukart.com/feed/"),
Expand All @@ -612,7 +612,7 @@ func TestService_ConvertEventToActivity(t *testing.T) {
Type: "Link",
},
Content: vocab.DefaultNaturalLanguageValue(
`The 10 Must-Watch Futuristic Anime That Every Fan Should See <br/>Anime is known for its wide ran...<br/><br/><a href="https://otakukart.com/the-10-must-watch-futuristic-anime-that-every-fan-should-see/">https://otakukart.com/the-10-must-watch-futuristic-anime-that-every-fan-should-see/</a><br/><br/><a href="https://reader/evt/2jrVcFeXfGNcExKHLCcrrXBYyLJ">Attributes</a>`),
`The 10 Must-Watch Futuristic Anime That Every Fan Should See <br/> Anime is known for its w...<br/>Original: <a href="https://otakukart.com/the-10-must-watch-futuristic-anime-that-every-fan-should-see/">https://otakukart.com/the-10-must-watch-futuristic-anime-that-every-fan-should-see/</a><br/>Attributes: <a href="https://reader/evt/2jrVcFeXfGNcExKHLCcrrXBYyLJ">https://reader/evt/2jrVcFeXfGNcExKHLCcrrXBYyLJ</a>`),
Published: ts,
Replies: &vocab.Collection{
ID: "https://otakukart.com/the-10-must-watch-futuristic-anime-that-every-fan-should-see/replies",
Expand Down Expand Up @@ -692,7 +692,7 @@ func TestService_ConvertEventToActorUpdate(t *testing.T) {
Name: vocab.DefaultNaturalLanguageValue("John Doe"),
},
dst: vocab.Activity{
ID: "https://reader/evt/2jrVcFeXfGNcExKHLCcrrXBYyLJ-update",
ID: "https://base/2jrVcFeXfGNcExKHLCcrrXBYyLJ-update",
URL: vocab.IRI("https://reader/evt/2jrVcFeXfGNcExKHLCcrrXBYyLJ"),
Type: "Update",
Context: vocab.IRI("https://www.w3.org/ns/activitystreams"),
Expand Down

0 comments on commit 52f4ef0

Please sign in to comment.