diff --git a/core/service.go b/core/service.go index bd6a351..e583c4f 100644 --- a/core/service.go +++ b/core/service.go @@ -1,7 +1,6 @@ package core import ( - "context" "crypto/md5" "encoding/hex" "encoding/json" @@ -455,6 +454,7 @@ func (sh *serviceDescription) unlink(w http.ResponseWriter, r *http.Request) { sType := queryvals.Get("stype") sId := queryvals.Get("sid") sk := queryvals.Get("sk") + targetType := queryvals.Get("ttype") authInfo, err := sh.sessionProvider.Query(sk) if err != nil { @@ -471,44 +471,51 @@ func (sh *serviceDescription) unlink(w http.ResponseWriter, r *http.Request) { // fmt.Println(authInfo.Uid) // fmt.Println("=================") + sType, sId, err = sh.getProviderInfo(sType, sId) + if err != nil { + logger.Println("getProviderInfo failed :", err) + w.WriteHeader(http.StatusBadRequest) + return + } + if authInfo.Uid != sId || authInfo.Platform != sType { logger.Println("unlink failed. session key is not correct :", authInfo, queryvals) w.WriteHeader(http.StatusBadRequest) return } - numRecord, err := sh.mongoClient.Collection(CollectionAccount).CountDocuments(context.Background(), bson.M{ + accDocs, err := sh.mongoClient.FindAll(CollectionAccount, bson.M{ "accid": authInfo.Account, - }, options.Count().SetLimit(2)) - + }, options.Find().SetProjection(bson.M{ + "_id": 1, + })) if err != nil { logger.Error("unlink failed, fail to count accounts :", err) w.WriteHeader(http.StatusBadRequest) } - if numRecord <= 1 { + if len(accDocs) <= 1 { logger.Println("unlink failed. At least one link must be maintained. :", r.URL.Query()) w.WriteHeader(http.StatusBadRequest) return } - sType, sId, err = sh.getProviderInfo(sType, sId) - if err != nil { - logger.Error("getProviderInfo failed :", err) - w.WriteHeader(http.StatusBadRequest) + var ids primitive.A + for _, accDoc := range accDocs { + ids = append(ids, accDoc["_id"].(primitive.ObjectID)) } - link, err := sh.mongoClient.FindOne(CollectionLink, bson.M{ - "platform": sType, - "uid": sId, - }, options.FindOne().SetProjection(bson.M{"_id": 1})) + link, err := sh.mongoClient.FindOneAndDelete(CollectionLink, bson.M{ + "platform": targetType, + "_id": bson.M{"$in": ids}, + }, options.FindOneAndDelete().SetProjection(bson.M{"_id": 1})) if err != nil { logger.Error("link failed. FindOneAndUpdate link err:", err) w.WriteHeader(http.StatusInternalServerError) return } - newid, err := sh.mongoClient.FindOneAndDelete(CollectionAccount, bson.M{ + preid, err := sh.mongoClient.FindOneAndDelete(CollectionAccount, bson.M{ "_id": link["_id"].(primitive.ObjectID), }, options.FindOneAndDelete().SetProjection(bson.M{"_id": 1})) if err != nil { @@ -517,9 +524,7 @@ func (sh *serviceDescription) unlink(w http.ResponseWriter, r *http.Request) { return } - // newid가 있어야 한다. 그래야 기존 서비스 계정이 없는 상태이다. - if newid == nil { - // 이미 계정이 있네? + if preid == nil { logger.Println("unlink failed. service account not found:", r.URL.Query()) w.WriteHeader(http.StatusBadRequest) return @@ -528,7 +533,7 @@ func (sh *serviceDescription) unlink(w http.ResponseWriter, r *http.Request) { logger.Println("unlink success :", r.URL.Query()) } -// == 연결된 계정 정보(숫자) 전달하는 API +// == 연결된 계정 정보(platform) 전달하는 API func (sh *serviceDescription) linkinfo(w http.ResponseWriter, r *http.Request) { defer func() { s := recover() @@ -562,6 +567,13 @@ func (sh *serviceDescription) linkinfo(w http.ResponseWriter, r *http.Request) { // fmt.Println(authInfo.Uid) // fmt.Println("=================") + sType, sId, err = sh.getProviderInfo(sType, sId) + if err != nil { + logger.Error("getProviderInfo failed :", err) + w.WriteHeader(http.StatusBadRequest) + return + } + //if oldAuth.Token != oldToken || oldAuth.Uid != oldId || oldAuth.Platform != oldType { if authInfo.Uid != sId || authInfo.Platform != sType { logger.Println("linkinfo failed. session key is not correct :", authInfo, queryvals) @@ -569,19 +581,46 @@ func (sh *serviceDescription) linkinfo(w http.ResponseWriter, r *http.Request) { return } - numRecord, err := sh.mongoClient.Collection(CollectionAccount).CountDocuments(context.Background(), bson.M{ - "accid": authInfo.Account, - }, options.Count().SetLimit(sh.MaximumNumLinkAccount)) - + platformName := "platform" + accDocs, err := sh.mongoClient.FindAll(CollectionAccount, bson.M{"accid": authInfo.Account}, options.Find().SetLimit(sh.MaximumNumLinkAccount).SetProjection(bson.M{ + "_id": 1, + })) if err != nil { logger.Error("linkinfo failed. CountDocuments err :", err) - w.WriteHeader(http.StatusBadRequest) + w.WriteHeader(http.StatusInternalServerError) return } - logger.Println("linkinfo :", numRecord) - w.Write([]byte(fmt.Sprintf(`{"num_linked_account":"%d"}`, numRecord))) + var ids primitive.A + for _, accDoc := range accDocs { + ids = append(ids, accDoc["_id"].(primitive.ObjectID)) + } + links, err := sh.mongoClient.FindAll(CollectionLink, bson.M{ + "_id": bson.M{"$in": ids}, + }, options.Find().SetLimit(sh.MaximumNumLinkAccount).SetProjection(bson.M{ + platformName: 1, + })) + if err != nil { + logger.Error("linkinfo failed. FindAll returns err :", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + var linkstrs []string + for _, link := range links { + linkstrs = append(linkstrs, link[platformName].(string)) + } + + linkbytes, err := json.Marshal(linkstrs) + if err != nil { + logger.Error("linkinfo failed. json marshal fail :", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + logger.Println("linkinfo :", linkstrs) + w.Write(linkbytes) } // == 계정 이메일 조회