/
recordprinterutils.go
136 lines (111 loc) · 4.3 KB
/
recordprinterutils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"fmt"
"strings"
"golang.org/x/net/context"
pbrc "github.com/brotherlogic/recordcollection/proto"
pbrm "github.com/brotherlogic/recordmover/proto"
)
func (s *Server) moveLoop(ctx context.Context, id int32) error {
moves, err := s.bridge.getMoves(ctx, id)
s.CtxLog(ctx, fmt.Sprintf("Got %v moves for %v", len(moves), id))
if err == nil {
for _, move := range moves {
err = s.move(ctx, move)
if err != nil {
break
}
}
}
return err
}
func (s *Server) buildMove(ctx context.Context, record *pbrc.Record, move *pbrm.RecordMove) []string {
lines := []string{}
surrounds := move.GetAfterContext()
// Don't show after context for sales
if move.GetAfterContext().GetLocation() == "Sell" || move.GetAfterContext().GetLocation() == "Sold" {
surrounds = nil
}
// If we're moving into the LP, use the before context
if move.GetAfterContext().GetLocation() == "Listening Pile" {
surrounds = move.GetBeforeContext()
s.CtxLog(ctx, fmt.Sprintf("MOVED: %v", move))
}
if surrounds != nil {
lines = append(lines, fmt.Sprintf("Slot %v", surrounds.GetSlot()))
if surrounds.GetBeforeInstance() != 0 {
bef, _ := s.bridge.getRecord(ctx, surrounds.GetBeforeInstance())
lines = append(lines, fmt.Sprintf(" %v", bef.GetRelease().GetTitle()))
}
lines = append(lines, fmt.Sprintf(" %v", record.GetRelease().Title))
if move.GetAfterContext().GetAfterInstance() != 0 {
aft, _ := s.bridge.getRecord(ctx, surrounds.GetAfterInstance())
lines = append(lines, fmt.Sprintf(" %v", aft.GetRelease().GetTitle()))
}
}
return lines
}
func (s *Server) move(ctx context.Context, move *pbrm.RecordMove) error {
s.currMove = move.InstanceId
if move.GetBeforeContext().GetLocation() != "" && move.GetAfterContext().GetLocation() != "" {
// Short circuit if this is a within folder move
if move.GetBeforeContext().GetLocation() == move.GetAfterContext().GetLocation() {
if move.GetToFolder() == move.GetFromFolder() {
return nil
}
return nil
}
// Short cicruit on sell
if move.GetAfterContext().GetLocation() == "Sell" {
return nil
}
record, err := s.bridge.getRecord(ctx, move.InstanceId)
if err != nil {
return fmt.Errorf("unable to get record: %w", err)
}
pmove := true
s.CtxLog(ctx, fmt.Sprintf("Moving %v -> %v", move.GetAfterContext().GetLocation(), record.GetMetadata().GetCategory()))
artistName := "Unknown Artist"
if len(record.GetRelease().GetArtists()) > 0 {
artistName = record.GetRelease().GetArtists()[0].GetName()
}
lines := []string{fmt.Sprintf("%v - %v: %v -> %v\n", artistName, record.GetRelease().Title, move.GetBeforeContext().GetLocation(), move.GetAfterContext().GetLocation())}
addlines := s.buildMove(ctx, record, move)
lines = append(lines, addlines...)
if (move.GetBeforeContext().GetLocation() == "Holding" && move.GetAfterContext().GetLocation() == "Listening Pile") ||
(move.GetBeforeContext().GetLocation() == "Listening Pile" && move.GetAfterContext().GetLocation() == "Listening Box") {
pmove = false
}
// Don't print bandcamp moves, unless they're into digital
// Don't print moves to stale sales
// Don't print moves into library records
if pmove {
if record.GetMetadata().GetGoalFolder() != 1782105 &&
record.GetMetadata().GetGoalFolder() != 2274270 &&
move.GetToFolder() != 268147 &&
move.GetToFolder() != 1708299 &&
!strings.Contains(move.GetAfterContext().GetLocation(), "Boxed") &&
!strings.Contains(move.GetAfterContext().GetLocation(), "Cleaning") &&
!strings.Contains(move.GetBeforeContext().GetLocation(), "Limbo") {
cleanToListen := strings.Contains(move.GetAfterContext().GetLocation(), "Listening") &&
strings.Contains(move.GetBeforeContext().GetLocation(), "Cleaning")
if !cleanToListen {
err = s.bridge.print(ctx, lines, move, true)
s.CtxLog(ctx, fmt.Sprintf("Printed %v -> %v", lines, err))
if err != nil {
return fmt.Errorf("unable to print: %w", err)
}
}
} else {
s.CtxLog(ctx, fmt.Sprintf("move for %v did not pass", record.GetRelease().GetInstanceId()))
}
}
err = s.bridge.clearMove(ctx, move)
if err != nil {
return fmt.Errorf("unable to clear move: %w", err)
}
} else {
s.RaiseIssue("Record Print Issue", fmt.Sprintf("Move for %v is not able to be printed -> %v", move.GetInstanceId(), move))
}
return nil
}