예제 #1
0
파일: tasks.go 프로젝트: huluwa/2016_Gofr
func importSubscription(pfc *PFContext, ch chan<- *rss.Outline, userID storage.UserID, folderRef storage.FolderRef, outline *rss.Outline) {
	c := pfc.C
	subscriptionURL := outline.FeedURL

	if subscribed, err := storage.IsSubscriptionDuplicate(pfc.C, userID, subscriptionURL); err != nil {
		c.Errorf("Cannot determine if '%s' is duplicate: %s", subscriptionURL, err)
		goto done
	} else if subscribed {
		c.Infof("Already subscribed to %s", subscriptionURL)
		goto done // Already subscribed
	}

	if feed, err := storage.FeedByURL(pfc.C, subscriptionURL); err != nil {
		c.Errorf("Error locating feed %s: %s", subscriptionURL, err.Error())
		goto done
	} else if feed == nil {
		// Feed not available locally - fetch it
		client := createHttpClient(pfc.C)
		if response, err := client.Get(subscriptionURL); err != nil {
			c.Errorf("Error downloading feed %s: %s", subscriptionURL, err)
			goto done
		} else {
			defer response.Body.Close()
			if parsedFeed, err := rss.UnmarshalStream(subscriptionURL, response.Body); err != nil {
				c.Errorf("Error reading RSS content (%s): %s", subscriptionURL, err)
				goto done
			} else {
				favIconURL := ""
				if parsedFeed.WWWURL != "" {
					if url, err := locateFavIconURL(pfc.C, parsedFeed.WWWURL); err != nil {
						// Not critical
						pfc.C.Warningf("FavIcon retrieval error: %s", err)
					} else if url != "" {
						favIconURL = url
					}
				}

				if err := storage.UpdateFeed(pfc.C, parsedFeed, favIconURL, time.Now()); err != nil {
					c.Errorf("Error updating feed: %s", err)
					goto done
				}
			}
		}
	}

	if subscriptionRef, err := storage.Subscribe(pfc.C, folderRef, subscriptionURL, outline.Title); err != nil {
		c.Errorf("Error subscribing to feed %s: %s", subscriptionURL, err)
		goto done
	} else {
		if _, err := storage.UpdateSubscription(pfc.C, subscriptionURL, subscriptionRef); err != nil {
			c.Errorf("Error updating subscription %s: %s", subscriptionURL, err)
			goto done
		}
	}

done:
	ch <- outline
}
예제 #2
0
파일: tasks.go 프로젝트: huluwa/2016_Gofr
func subscribeTask(pfc *PFContext) (TaskMessage, error) {
	subscriptionURL := pfc.R.PostFormValue("url")
	folderID := pfc.R.PostFormValue("folderID")

	if subscriptionURL == "" {
		return TaskMessage{}, errors.New("Missing subscription URL")
	}

	subscriptionRef := storage.SubscriptionRef{
		FolderRef: storage.FolderRef{
			UserID:   pfc.UserID,
			FolderID: folderID,
		},
		SubscriptionID: subscriptionURL,
	}

	if exists, err := storage.SubscriptionExists(pfc.C, subscriptionRef); err != nil {
		return TaskMessage{}, err
	} else if !exists {
		pfc.C.Warningf("No longer subscribed to %s", subscriptionURL, err)
		return TaskMessage{}, nil
	}

	if feed, err := storage.FeedByURL(pfc.C, subscriptionURL); err != nil {
		return TaskMessage{}, err
	} else if feed == nil {
		// Feed not available locally - fetch it
		client := createHttpClient(pfc.C)
		if response, err := client.Get(subscriptionURL); err != nil {
			pfc.C.Errorf("Error downloading feed (%s): %s", subscriptionURL, err)
			return TaskMessage{}, NewReadableError(_l("An error occurred while downloading the feed"), &err)
		} else {
			defer response.Body.Close()
			if parsedFeed, err := rss.UnmarshalStream(subscriptionURL, response.Body); err != nil {
				pfc.C.Errorf("Error reading RSS content (%s): %s", subscriptionURL, err)
				return TaskMessage{}, NewReadableError(_l("Error reading RSS content"), &err)
			} else {
				favIconURL := ""
				if parsedFeed.WWWURL != "" {
					if url, err := locateFavIconURL(pfc.C, parsedFeed.WWWURL); err != nil {
						// Not critical
						pfc.C.Warningf("FavIcon retrieval error: %s", err)
					} else if url != "" {
						favIconURL = url
					}
				}

				if err := storage.UpdateFeed(pfc.C, parsedFeed, favIconURL, time.Now()); err != nil {
					return TaskMessage{}, err
				}
			}
		}
	}

	if _, err := storage.UpdateSubscription(pfc.C, subscriptionURL, subscriptionRef); err != nil {
		return TaskMessage{}, err
	}

	return TaskMessage{
		Refresh: true,
	}, nil
}