From 1b07cadcbf6279cc6c4946c9e1d4f31614b4fdc4 Mon Sep 17 00:00:00 2001 From: Niko Storni Date: Thu, 3 Jan 2019 17:01:00 +0100 Subject: [PATCH] add support for transferred channels fix bugs update dependencies --- Gopkg.lock | 10 +++++----- sdk/api.go | 2 +- setup.go | 32 +++++++++++++++++++++----------- ytsync.go | 49 +++++++++++++++++++++++++++++-------------------- 4 files changed, 56 insertions(+), 37 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 99ba1fa..9310b6d 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -95,13 +95,13 @@ branch = "master" name = "github.com/lbryio/lbry.go" packages = ["errors","jsonrpc","lbrycrd","null","stop","util"] - revision = "7a6eb5728093ff8da4bc6d9691308acf10fb9a53" + revision = "f986bd3066b9044ae1a44534908bdf4b381bbd1b" [[projects]] branch = "master" - name = "github.com/lbryio/lbryschema.go" - packages = ["pb"] - revision = "185433f2fd0c732547654749b98b37e56223dd22" + name = "github.com/lbryio/types" + packages = ["go"] + revision = "594241d24e0025d769d2cb58168536b6963d51cf" [[projects]] branch = "master" @@ -131,7 +131,7 @@ branch = "master" name = "github.com/rylio/ytdl" packages = ["."] - revision = "0227c2bacb82a434f2332d7d8c64093615c08a40" + revision = "44fe64bba886b85c00fc7045e092d796484d6f90" [[projects]] branch = "master" diff --git a/sdk/api.go b/sdk/api.go index 73217ad..753cdbd 100644 --- a/sdk/api.go +++ b/sdk/api.go @@ -135,7 +135,7 @@ func (a *APIConfig) SetChannelClaimID(channelID string, channelClaimID string) e var response apiChannelStatusResponse err := json.Unmarshal(body, &response) if err != nil { - return err + return errors.Err(err) } if !response.Error.IsNull() { return errors.Err(response.Error.String) diff --git a/setup.go b/setup.go index 32e07b2..1918134 100644 --- a/setup.go +++ b/setup.go @@ -186,9 +186,25 @@ func (s *Sync) ensureChannelOwnership() error { } else if channels == nil { return errors.Err("no channel response") } - - isChannelMine := false - for _, channel := range *channels { + //special case for wallets we don't retain full control anymore + if len(*channels) > 1 { + // This wallet is probably not under our control anymore but we still want to publish to it + // here we shall check if within all the channels there is one that was created by ytsync + SendInfoToSlack("we are dealing with a wallet that has multiple channels. This indicates that the wallet was probably transferred but we still want to sync their content. YoutubeID: %s", s.YoutubeChannelID) + if s.lbryChannelID == "" { + return errors.Err("this channel does not have a recorded claimID in the database. To prevent failures, updates are not supported until an entry is manually added in the database") + } + for _, c := range *channels { + if c.ClaimID != s.lbryChannelID { + if c.Name != s.LbryChannelName { + return errors.Err("the channel in the wallet is different than the channel in the database") + } + return nil // we have the ytsync channel and both the claimID and the channelName from the database are correct + } + } + } + if len(*channels) == 1 { + channel := (*channels)[0] if channel.Name == s.LbryChannelName { //TODO: eventually get rid of this when the whole db is filled if s.lbryChannelID == "" { @@ -196,18 +212,12 @@ func (s *Sync) ensureChannelOwnership() error { } else if channel.ClaimID != s.lbryChannelID { return errors.Err("the channel in the wallet is different than the channel in the database") } - if channel.Name != s.LbryChannelName { - return errors.Err("the channel in the wallet is different than the channel in the database") - } s.lbryChannelID = channel.ClaimID - isChannelMine = true + return err } else { - return errors.Err("this wallet has multiple channels. maybe something went wrong during setup?") + return errors.Err("this channel does not belong to this wallet! Expected: %s, found: %s", s.LbryChannelName, channel.Name) } } - if isChannelMine { - return err - } channelBidAmount := channelClaimAmount diff --git a/ytsync.go b/ytsync.go index 168964d..b5bf817 100644 --- a/ytsync.go +++ b/ytsync.go @@ -102,23 +102,29 @@ func (s *Sync) AppendSyncedVideo(videoID string, published bool, failureReason s } // SendErrorToSlack Sends an error message to the default channel and to the process log. -func SendErrorToSlack(format string, a ...interface{}) error { +func SendErrorToSlack(format string, a ...interface{}) { message := format if len(a) > 0 { message = fmt.Sprintf(format, a...) } log.Errorln(message) - return util.SendToSlack(":sos: " + message) + err := util.SendToSlack(":sos: " + message) + if err != nil { + log.Errorln(err) + } } // SendInfoToSlack Sends an info message to the default channel and to the process log. -func SendInfoToSlack(format string, a ...interface{}) error { +func SendInfoToSlack(format string, a ...interface{}) { message := format if len(a) > 0 { message = fmt.Sprintf(format, a...) } log.Infoln(message) - return util.SendToSlack(":information_source: " + message) + err := util.SendToSlack(":information_source: " + message) + if err != nil { + log.Errorln(err) + } } // IsInterrupted can be queried to discover if the sync process was interrupted manually @@ -371,22 +377,28 @@ func logShutdownError(shutdownErr error) { SendErrorToSlack("WALLET HAS NOT BEEN MOVED TO THE WALLET BACKUP DIR") } +func isYtsyncClaim(c jsonrpc.Claim) bool { + if !util.InSlice(c.Category, []string{"claim", "update"}) || c.Value.Stream == nil { + return false + } + if c.Value.Stream.Metadata == nil || c.Value.Stream.Metadata.Thumbnail == nil { + //most likely a claim created outside of ytsync, ignore! + return false + } + + //we're dealing with something that wasn't published by us! + return !strings.Contains(*c.Value.Stream.Metadata.Thumbnail, "https://berk.ninja/thumbnails/") +} + // fixDupes abandons duplicate claims func (s *Sync) fixDupes(claims []jsonrpc.Claim) (bool, error) { abandonedClaims := false videoIDs := make(map[string]jsonrpc.Claim) for _, c := range claims { - if !util.InSlice(c.Category, []string{"claim", "update"}) || c.Value.Stream == nil { + if !isYtsyncClaim(c) { continue } - if c.Value.Stream.Metadata == nil || c.Value.Stream.Metadata.Thumbnail == nil { - return false, errors.Err("something is wrong with this claim: %s", c.ClaimID) - } tn := *c.Value.Stream.Metadata.Thumbnail - if !strings.Contains(tn, "https://berk.ninja/thumbnails/") { - //we're dealing with something that wasn't published by us! - continue - } videoID := tn[strings.LastIndex(tn, "/")+1:] log.Infof("claimid: %s, claimName: %s, videoID: %s", c.ClaimID, c.Name, videoID) @@ -402,11 +414,11 @@ func (s *Sync) fixDupes(claims []jsonrpc.Claim) (bool, error) { claimToAbandon = cl videoIDs[videoID] = c } + log.Debugf("abandoning %+v", claimToAbandon) _, err := s.daemon.ClaimAbandon(claimToAbandon.Txid, claimToAbandon.Nout) if err != nil { return true, err } - log.Debugf("abandoning %+v", claimToAbandon) abandonedClaims = true //return true, nil } @@ -417,12 +429,10 @@ func (s *Sync) fixDupes(claims []jsonrpc.Claim) (bool, error) { func (s *Sync) updateRemoteDB(claims []jsonrpc.Claim) (total int, fixed int, err error) { count := 0 for _, c := range claims { - if !util.InSlice(c.Category, []string{"claim", "update"}) || c.Value.Stream == nil { + if !isYtsyncClaim(c) { continue } - if c.Value.Stream.Metadata == nil || c.Value.Stream.Metadata.Thumbnail == nil { - return count, fixed, errors.Err("something is wrong with the this claim: %s", c.ClaimID) - } + count++ //check if claimID is in remote db tn := *c.Value.Stream.Metadata.Thumbnail videoID := tn[strings.LastIndex(tn, "/")+1:] @@ -431,12 +441,11 @@ func (s *Sync) updateRemoteDB(claims []jsonrpc.Claim) (total int, fixed int, err fixed++ err = s.Manager.apiConfig.MarkVideoStatus(s.YoutubeChannelID, videoID, VideoStatusPublished, c.ClaimID, c.Name, "", nil) if err != nil { - return total, fixed, err + return count, fixed, err } } - total++ } - return total, fixed, nil + return count, fixed, nil } func (s *Sync) doSync() error {