Skip to content

Commit

Permalink
Merge branch 'master' into minor-fixes-and-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
MrIceman committed Oct 30, 2023
2 parents f377381 + 5eab256 commit 9e084c0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
28 changes: 28 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,34 @@ func main() {
userStartsChatting()
userBIsNotPartOfChat()
userBIsPartOfChat()
userJoinsChat()
}

func userJoinsChat() {
d := sequence.NewDiagram("new_chat")
userA := "userA (name could be quite longer)"
userB := "userB"
b := "backend"

d.SetTitle("User starts a chat with someone (doesn't matter if new or not)")

d.AddParticipants(userA, userB, b)

_ = d.AddDirectionalEdge(userA, b, `POST /inbox {"recipient": userID, "project": projectID, "projectType": challenge|solution}"`)
_ = d.AddDirectionalEdge(b, userA, "Response Status Code: 202, Body: Inbox{id, secret, recipient, ...}")
_ = d.AddDirectionalEdge(userA, b, "WS connect")
_ = d.AddDirectionalEdge(userA, b, `"{"action": "JOIN", "data": {"inboxId": str, "userId": "str", "inboxSecret": str}}"`)
_ = d.AddDirectionalEdge(b, b, "STORE_CONNECTION_ID(userA)")
_ = d.AddDirectionalEdge(b, b, "sets unread inbox userA = 0")
_ = d.AddDirectionalEdge(b, userA, `{"event": "new_message", "messages": [{"id": str, "inboxId": str, "content": str, "userId": str, "createdAt": str}]}`)
_ = d.AddDirectionalEdge(userA, b, `"{"action": "SEND_MESSAGE", "data": {"inboxId": str, "userId": "str", "inboxSecret": str, "content": "hi"}}"`)
_ = d.AddDirectionalEdge(b, b, "check connectionID userB")
_ = d.AddUndirectionalEdge(b, b, "if connected: ")
_ = d.AddDirectionalEdge(b, userB, `{"event": "new_message", "messages": [{"id": str, "inboxId": str, "content": str, "userId": str, "createdAt": str}]}`)
_ = d.AddUndirectionalEdge(b, b, "else: ")
_ = d.AddUndirectionalEdge(b, b, "increment unread inbox userB")

d.Render()
}

func userBIsPartOfChat() {
Expand Down Expand Up @@ -71,6 +98,7 @@ func userStartsChatting() {
d.AddParticipants(backend)
d.AddParticipants(db)

d.AddDirectionalEdge(client, backend, "GET /inbox/<TO_ID>")
d.AddDirectionalEdge(client, backend, "PUT /chat/user/<TO_ID>")
d.AddDirectionalEdge(backend, db, "checks or create inbox for user")
d.AddDirectionalEdge(backend, db, "set all unread messages to read if existing")
Expand Down
18 changes: 10 additions & 8 deletions sequence/diagram.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ func (d *Diagram) renderParticipants() {
}
}
spacePerBlock := float64(d.dc.Width() / len(d.participants))
startX := spacePerBlock*float64(len(d.renderedParticipants)+1) - spacePerBlock/2 - participantsPadding
// startX := float64(participantsPadding + (len(d.renderedParticipants) * (participantBoxWidth + 1000/(len(d.participants)))))
endX := startX + participantBoxWidth
startY := 1000 * 0.1 // 10% from the top
strWidth, strHeight := d.dc.MeasureString(p.Name)
startX := spacePerBlock*float64(len(d.renderedParticipants)+1) - spacePerBlock/2 - participantsPadding - strWidth/2

endX := startX + participantBoxWidth + strWidth
startY := height * 0.1 // 10% from the top
endY := startY + participantBoxHeight
// draw the border
d.dc.SetColor(color.Black)
Expand All @@ -104,11 +105,10 @@ func (d *Diagram) renderParticipants() {
d.dc.DrawRectangle(
startX,
startY,
participantBoxWidth,
participantBoxWidth+strWidth,
participantBoxHeight,
)
d.dc.SetColor(color.Black)
strWidth, strHeight := d.dc.MeasureString(p.Name)
centerStrWidth := startX + ((endX - startX) / 2) - strWidth/2
centerStrHeight := (endY-startY)/2 + startY + (strHeight / 2)

Expand Down Expand Up @@ -141,11 +141,13 @@ func (d *Diagram) renderEdges() {

for idx := range d.edges {
e := &d.edges[idx]
fromStrWidth, _ := d.dc.MeasureString(e.from.Name)
toStrWidth, _ := d.dc.MeasureString(e.to.Name)
fromCords := d.participantsCoordMap[e.from.Name]
toCords := d.participantsCoordMap[e.to.Name]
startX := fromCords.X + participantBoxWidth/2 - 2.5 // 2.5 = half of stroke width
startX := fromCords.X + participantBoxWidth/2 - 2.5 + fromStrWidth/2 // 2.5 = half of stroke width
startY := fromCords.Y + participantBoxHeight + 2.5 + float64((1+renderedEdges)*verticalSpaceBetweenEdges)
endX := toCords.X + participantBoxWidth/2 - 2.5
endX := toCords.X + participantBoxWidth/2 - 2.5 + toStrWidth/2
isReverseEdge := endX < startX

d.dc.SetDash(6)
Expand Down

0 comments on commit 9e084c0

Please sign in to comment.