func (s *ApiServer) PostEntry(ctx context.Context, entry *pb.Entry) (*pb.Entry, error) { key, err := store.PutEntry(s.rdb, entry, false) // always use false if err != nil { return nil, err } s.spread(key) return entry, nil }
func (s *ApiServer) ArchiveFeed(stream pb.Api_ArchiveFeedServer) error { var entryCount int32 var dateStart string var dateEnd string var lastEntry *pb.Entry startTime := time.Now() // tooMuchExistsItem := 0 for { entry, err := stream.Recv() if err == io.EOF { endTime := time.Now() return stream.SendAndClose(&pb.FeedSummary{ EntryCount: entryCount, DateStart: dateStart, DateEnd: dateEnd, ElapsedTime: int32(endTime.Sub(startTime).Seconds()), }) } if err != nil { return err } entryCount++ key, err := store.PutEntry(s.rdb, entry, false) // always use false if err == nil { // no error or new key s.spread(key) } // Retuen if not force update and all entries are exists // TODO: client dead lock??? if serr, ok := err.(*store.Error); ok { if serr.Code == store.ExistItem { err = nil // tooMuchExistsItem++ // if tooMuchExistsItem > 200 { // return fmt.Errorf("Too much exists entries.") // } } } if err != nil { log.Println("db error:", err) } go s.mirrorMedia(s.fs, entry) if lastEntry == nil { dateEnd = entry.Date } lastEntry = entry dateStart = lastEntry.Date } }
func (s *ApiServer) FixComment() error { req := &pb.FeedRequest{ Id: "public", Start: int32(0), PageSize: 50, } feed, _ := s.cachedFeed(req) for _, e := range feed.Entries { for _, cmt := range e.Comments { // date, _ := time.Parse(time.RFC3339, cmt.Date) profile, _ := store.GetProfile(s.mdb, cmt.From.Id) fixedName := e.Id + profile.Uuid + cmt.Date uuid1 := uuid.NewV5(uuid.NamespaceURL, fixedName) cmt.Id = uuid1.String() } store.PutEntry(s.rdb, e, true) } return nil }
func (s *ApiServer) ForceArchiveFeed(stream pb.Api_ForceArchiveFeedServer) error { var entryCount int32 var dateStart string var dateEnd string var lastEntry *pb.Entry startTime := time.Now() // tooMuchExistsItem := 0 for { entry, err := stream.Recv() if err == io.EOF { endTime := time.Now() return stream.SendAndClose(&pb.FeedSummary{ EntryCount: entryCount, DateStart: dateStart, DateEnd: dateEnd, ElapsedTime: int32(endTime.Sub(startTime).Seconds()), }) } if err != nil { return err } entryCount++ // save db key, err := store.PutEntry(s.rdb, entry, true) if err != nil { log.Println("db error:", err) } else { s.cached["public"].Push(key.String()) } if lastEntry == nil { dateEnd = entry.Date } lastEntry = entry dateStart = lastEntry.Date } }
func TestPostProfile(t *testing.T) { setup() defer teardown() Convey("Given ApiServer, post profile then get profile", t, func() { ctx := context.Background() p1 := &pb.Profile{ Uuid: "c6f8dca854f011ddb489003048343a40", Id: "yinhm", Name: "yinhm", Type: "user", Private: false, SupId: "4566789", Description: "desc", RemoteKey: "xxx", } feedinfo := &pb.Feedinfo{ Uuid: "c6f8dca854f011ddb489003048343a40", Id: "yinhm", Name: "Heming Friend", Type: "user", Private: false, SupId: "123456-1234", Description: "Friendfeed land", Subscriptions: []*pb.Profile{p1}, } got, err := srv.PostFeedinfo(ctx, feedinfo) So(err, ShouldBeNil) So(got.Uuid, ShouldEqual, feedinfo.Uuid) So(got.RemoteKey, ShouldEqual, feedinfo.RemoteKey) profile, err := store.GetProfile(srv.mdb, feedinfo.Id) So(err, ShouldBeNil) So(profile.Uuid, ShouldEqual, feedinfo.Uuid) So(profile.RemoteKey, ShouldEqual, feedinfo.RemoteKey) newinfo, err := store.GetFeedinfo(srv.rdb, feedinfo.Uuid) So(err, ShouldBeNil) So(newinfo.Uuid, ShouldEqual, feedinfo.Uuid) So(newinfo.RemoteKey, ShouldEqual, feedinfo.RemoteKey) So(len(newinfo.Subscriptions), ShouldEqual, 1) Convey("post entry", func() { from := &pb.Feed{ Id: "yinhm", Name: "yinhm", Type: "user", } entry := &pb.Entry{ Body: "张无忌对张三丰说:“太师父,武当山的生活太寂寞了,只有清风和明月两个朋友能陪我玩。”张三丰叹了口气:“已经很不错啦,至少还有清风明月呢。想当年我在少林寺的时候,也是只有两个朋友,其中一个也叫清风……”“那另一个呢?”“叫心相印。”…", Id: "e/2b43a9066074d120ed2e45494eea1797", Date: "2012-09-07T07:40:22Z", Url: "http://friendfeed.com/yinhm/2b43a906/rt-trojansj", From: from, ProfileUuid: "c6f8dca854f011ddb489003048343a40", } _, err := store.PutEntry(srv.rdb, entry, false) So(err, ShouldBeNil) req := &pb.FeedRequest{ Id: "yinhm", Start: 0, PageSize: 50, } feed, err := srv.FetchFeed(context.Background(), req) So(err, ShouldBeNil) So(feed.Id, ShouldEqual, "yinhm") So(len(feed.Entries), ShouldEqual, 1) // prefix stripped So(feed.Entries[0].Id, ShouldEqual, "2b43a9066074d120ed2e45494eea1797") Convey("comment on the entry", func() { cmt := &pb.Comment{ Id: "2b43a9066074d120ed2e45494eea1797", Date: "2012-09-07T07:40:22Z", From: from, Body: "this is a comment", RawBody: "this is a comment", } cReq := &pb.CommentRequest{ Entry: entry.Id, Comment: cmt, } entry, err := srv.CommentEntry(context.Background(), cReq) So(err, ShouldBeNil) So(entry.Id, ShouldEqual, "2b43a9066074d120ed2e45494eea1797") // BUG: duplicated entry when comment feed, err := srv.FetchFeed(context.Background(), req) So(err, ShouldBeNil) So(feed.Id, ShouldEqual, "yinhm") So(len(feed.Entries), ShouldEqual, 1) }) }) }) }