Exemplo n.º 1
0
func (con UserSettings) Handler(c context.Context) http.Handler {
	cfg := readeef.GetConfig(c)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		user := readeef.GetUser(c, r)

		params := webfw.GetParams(c, r)
		attr := params["attribute"]

		var resp responseError
		if r.Method == "GET" {
			resp = getUserAttribute(user, attr)
		} else if r.Method == "POST" {
			buf := util.BufferPool.GetBuffer()
			defer util.BufferPool.Put(buf)

			buf.ReadFrom(r.Body)

			resp = setUserAttribute(user, []byte(cfg.Auth.Secret), attr, buf.Bytes())
		}

		var b []byte
		if resp.err == nil {
			b, resp.err = json.Marshal(resp.val)
		}

		if resp.err != nil {
			webfw.GetLogger(c).Print(resp.err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)
	})
}
Exemplo n.º 2
0
func (con Auth) Handler(c context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		action := webfw.GetMultiPatternIdentifier(c, r)
		sess := webfw.GetSession(c, r)

		var resp responseError

		switch action {
		case "auth-data":
			user := readeef.GetUser(c, r)
			resp = getAuthData(user, sess, con.capabilities)
		case "logout":
			resp = logout(sess)
		}

		var b []byte
		if resp.err == nil {
			b, resp.err = json.Marshal(resp.val)
		}
		if resp.err == nil {
			w.Write(b)
		} else {
			webfw.GetLogger(c).Print(resp.err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}
	})
}
Exemplo n.º 3
0
func (con User) Handler(c context.Context) http.Handler {
	cfg := readeef.GetConfig(c)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		user := readeef.GetUser(c, r)

		action := webfw.GetMultiPatternIdentifier(c, r)
		params := webfw.GetParams(c, r)

		var resp responseError
		switch action {
		case "list":
			resp = listUsers(user)
		case "add":
			buf := util.BufferPool.GetBuffer()
			defer util.BufferPool.Put(buf)

			buf.ReadFrom(r.Body)

			resp = addUser(user, data.Login(params["login"]), buf.String(), []byte(cfg.Auth.Secret))
		case "remove":
			resp = removeUser(user, data.Login(params["login"]))
		case "setAttr":
			resp = setAttributeForUser(user, []byte(cfg.Auth.Secret), data.Login(params["login"]), params["attr"], []byte(params["value"]))
		}

		switch resp.err {
		case errForbidden:
			w.WriteHeader(http.StatusForbidden)
			return
		case errUserExists:
			resp.val["Error"] = true
			resp.val["ErrorType"] = resp.errType
			resp.err = nil
		case errCurrentUser:
			resp.val["Error"] = true
			resp.val["ErrorType"] = resp.errType
			resp.err = nil
		}

		var b []byte
		if resp.err == nil {
			b, resp.err = json.Marshal(resp.val)
		}
		if resp.err != nil {
			webfw.GetLogger(c).Print(resp.err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)
	})
}
Exemplo n.º 4
0
func (con Auth) Handler(c context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		user := readeef.GetUser(c, r)

		resp := getAuthData(user)

		b, err := json.Marshal(resp.val)
		if err != nil {
			webfw.GetLogger(c).Print(err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)
	})
}
Exemplo n.º 5
0
func (con Article) Handler(c context.Context) http.Handler {
	logger := webfw.GetLogger(c)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		user := readeef.GetUser(c, r)

		params := webfw.GetParams(c, r)
		action := webfw.GetMultiPatternIdentifier(c, r)

		logger.Infof("Invoking Article controller with action '%s', article id '%s'\n", action, params["article-id"])

		var articleId int64
		var resp responseError

		articleId, resp.err = strconv.ParseInt(params["article-id"], 10, 64)

		if resp.err == nil {
			id := data.ArticleId(articleId)
			switch action {
			case "fetch":
				resp = fetchArticle(user, id)
			case "read":
				resp = articleReadState(user, id, params["value"] == "true")
			case "favorite":
				resp = articleFavoriteState(user, id, params["value"] == "true")
			case "format":
				resp = formatArticle(user, id, con.extractor, webfw.GetConfig(c), con.config)
			}
		}

		var b []byte
		if resp.err == nil {
			b, resp.err = json.Marshal(resp.val)
		}

		if resp.err == nil {
			w.Write(b)
		} else {
			webfw.GetLogger(c).Print(resp.err)

			w.WriteHeader(http.StatusInternalServerError)
		}
	})
}
Exemplo n.º 6
0
func (con Auth) Handler(c context.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		user := readeef.GetUser(c, r)

		type User struct {
			Login     string
			FirstName string
			LastName  string
			Email     string
			Admin     bool
		}
		type response struct {
			Auth        bool
			User        User
			ProfileData map[string]interface{}
		}

		resp := response{
			Auth: true,
			User: User{
				Login:     user.Login,
				FirstName: user.FirstName,
				LastName:  user.LastName,
				Email:     user.Email,
				Admin:     user.Admin,
			},
			ProfileData: user.ProfileData,
		}

		b, err := json.Marshal(resp)
		if err != nil {
			webfw.GetLogger(c).Print(err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)
	}
}
Exemplo n.º 7
0
func (con Search) Handler(c context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		user := readeef.GetUser(c, r)
		params := webfw.GetParams(c, r)
		query := params["query"]

		var resp responseError

		if resp.err = r.ParseForm(); resp.err == nil {
			highlight := ""
			feedId := ""

			if vs := r.Form["highlight"]; len(vs) > 0 {
				highlight = vs[0]
			}

			if vs := r.Form["id"]; len(vs) > 0 {
				if vs[0] != "all" && vs[0] != "favorite" {
					feedId = vs[0]
				}
			}

			resp = search(user, con.searchIndex, query, highlight, feedId)
		}

		var b []byte
		if resp.err == nil {
			b, resp.err = json.Marshal(resp.val)
		}
		if resp.err != nil {
			webfw.GetLogger(c).Print(resp.err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)
	})
}
Exemplo n.º 8
0
func (con Feed) Handler(c context.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var err error

		db := readeef.GetDB(c)
		user := readeef.GetUser(c, r)

		action := webfw.GetMultiPatternIdentifier(c, r)
		params := webfw.GetParams(c, r)
		resp := make(map[string]interface{})

	SWITCH:
		switch action {
		case "list":
			var feeds []readeef.Feed
			feeds, err = db.GetUserTagsFeeds(user)

			if err != nil {
				break
			}

			respFeeds := []feed{}

			for _, f := range feeds {
				respFeeds = append(respFeeds, feed{
					Id: f.Id, Title: f.Title, Description: f.Description,
					Link: f.Link, Image: f.Image, Tags: f.Tags,
					UpdateError: f.UpdateError, SubscribeError: f.SubscribeError,
				})
			}

			resp["Feeds"] = respFeeds
		case "discover":
			r.ParseForm()

			link := r.FormValue("url")
			var u *url.URL

			/* TODO: non-fatal error */
			if u, err = url.Parse(link); err != nil {
				break
				/* TODO: non-fatal error */
			} else if !u.IsAbs() {
				u.Scheme = "http"
				if u.Host == "" {
					parts := strings.SplitN(u.Path, "/", 2)
					u.Host = parts[0]
					if len(parts) > 1 {
						u.Path = "/" + parts[1]
					} else {
						u.Path = ""
					}
				}
				link = u.String()
			}

			var feeds []readeef.Feed
			feeds, err = con.fm.DiscoverFeeds(link)
			if err != nil {
				break
			}

			var userFeeds []readeef.Feed
			userFeeds, err = db.GetUserFeeds(user)
			if err != nil {
				break
			}

			userFeedIdMap := make(map[int64]bool)
			userFeedLinkMap := make(map[string]bool)
			for _, f := range userFeeds {
				userFeedIdMap[f.Id] = true
				userFeedLinkMap[f.Link] = true

				u, err := url.Parse(f.Link)
				if err == nil && strings.HasPrefix(u.Host, "www.") {
					u.Host = u.Host[4:]
					userFeedLinkMap[u.String()] = true
				}
			}

			respFeeds := []feed{}
			for _, f := range feeds {
				if !userFeedIdMap[f.Id] && !userFeedLinkMap[f.Link] {
					respFeeds = append(respFeeds, feed{
						Id: f.Id, Title: f.Title, Description: f.Description,
						Link: f.Link, Image: f.Image,
					})
				}
			}

			resp["Feeds"] = respFeeds
		case "opml":
			buf := util.BufferPool.GetBuffer()
			defer util.BufferPool.Put(buf)

			buf.ReadFrom(r.Body)

			var opml parser.Opml
			opml, err = parser.ParseOpml(buf.Bytes())
			if err != nil {
				break
			}

			var userFeeds []readeef.Feed
			userFeeds, err = db.GetUserFeeds(user)
			if err != nil {
				break
			}

			userFeedMap := make(map[int64]bool)
			for _, f := range userFeeds {
				userFeedMap[f.Id] = true
			}

			var feeds []readeef.Feed
			for _, opmlFeed := range opml.Feeds {
				var discovered []readeef.Feed

				discovered, err = con.fm.DiscoverFeeds(opmlFeed.Url)
				if err != nil {
					continue
				}

				for _, f := range discovered {
					if !userFeedMap[f.Id] {
						if len(opmlFeed.Tags) > 0 {
							f.Link += "#" + strings.Join(opmlFeed.Tags, ",")
						}

						feeds = append(feeds, f)
					}
				}
			}

			respFeeds := []feed{}
			for _, f := range feeds {
				respFeeds = append(respFeeds, feed{
					Id: f.Id, Title: f.Title, Description: f.Description,
					Link: f.Link, Image: f.Image,
				})
			}
			resp["Feeds"] = respFeeds
		case "add":
			r.ParseForm()
			links := r.Form["url"]
			success := false

			for _, link := range links {
				/* TODO: non-fatal error */
				var u *url.URL
				if u, err = url.Parse(link); err != nil {
					break SWITCH
					/* TODO: non-fatal error */
				} else if !u.IsAbs() {
					err = errors.New("Feed has no link")
					break SWITCH
				}

				var f readeef.Feed
				f, err = con.fm.AddFeedByLink(link)
				if err != nil {
					break SWITCH
				}

				f, err = db.CreateUserFeed(readeef.GetUser(c, r), f)
				if err != nil {
					break SWITCH
				}

				tags := strings.SplitN(u.Fragment, ",", -1)
				if u.Fragment != "" && len(tags) > 0 {
					err = db.CreateUserFeedTag(f, tags...)
				}

				success = true
			}

			resp["Success"] = success
		case "remove":
			var id int64
			id, err = strconv.ParseInt(params["feed-id"], 10, 64)

			/* TODO: non-fatal error */
			if err != nil {
				break
			}

			var feed readeef.Feed
			feed, err = db.GetUserFeed(id, user)
			/* TODO: non-fatal error */
			if err != nil {
				break
			}

			err = db.DeleteUserFeed(feed)
			/* TODO: non-fatal error */
			if err != nil {
				break
			}

			con.fm.RemoveFeed(feed)

			resp["Success"] = true
		case "tags":
			var id int64
			id, err = strconv.ParseInt(params["feed-id"], 10, 64)

			/* TODO: non-fatal error */
			if err != nil {
				break
			}

			var feed readeef.Feed
			feed, err = db.GetUserFeed(id, user)
			/* TODO: non-fatal error */
			if err != nil {
				break
			}

			if r.Method == "GET" {
				resp["Tags"] = feed.Tags
			} else if r.Method == "POST" {
				var tags []string

				tags, err = db.GetUserFeedTags(user, feed)
				if err != nil {
					break
				}

				err = db.DeleteUserFeedTag(feed, tags...)
				if err != nil {
					break
				}

				decoder := json.NewDecoder(r.Body)

				tags = []string{}
				err = decoder.Decode(&tags)
				if err != nil {
					break
				}

				err = db.CreateUserFeedTag(feed, tags...)
				if err != nil {
					break
				}

				resp["Success"] = true
				resp["Id"] = feed.Id
			}
		case "read":
			feedId := params["feed-id"]
			timestamp := params["timestamp"]

			var seconds int64
			seconds, err = strconv.ParseInt(timestamp, 10, 64)
			/* TODO: non-fatal error */
			if err != nil {
				break
			}

			t := time.Unix(seconds/1000, 0)

			switch {
			case feedId == "tag:__all__":
				err = db.MarkUserArticlesByDateAsRead(user, t, true)
			case feedId == "__favorite__":
				// Favorites are assumbed to have been read already
			case strings.HasPrefix(feedId, "tag:"):
				tag := feedId[4:]
				err = db.MarkUserTagArticlesByDateAsRead(user, tag, t, true)
			default:
				var id int64

				id, err = strconv.ParseInt(feedId, 10, 64)
				/* TODO: non-fatal error */
				if err != nil {
					break SWITCH
				}

				var feed readeef.Feed
				feed, err = db.GetUserFeed(id, user)
				/* TODO: non-fatal error */
				if err != nil {
					break SWITCH
				}

				err = db.MarkFeedArticlesByDateAsRead(feed, t, true)
			}

			if err == nil {
				resp["Success"] = true
			}
		case "articles":
			var articles []readeef.Article

			var limit, offset int

			feedId := params["feed-id"]

			limit, err = strconv.Atoi(params["limit"])
			/* TODO: non-fatal error */
			if err != nil {
				break
			}

			offset, err = strconv.Atoi(params["offset"])
			/* TODO: non-fatal error */
			if err != nil {
				break
			}

			newerFirst := params["newer-first"] == "true"
			unreadOnly := params["unread-only"] == "true"

			if limit > 50 {
				limit = 50
			}

			if feedId == "__favorite__" {
				if newerFirst {
					articles, err = db.GetUserFavoriteArticlesDesc(user, limit, offset)
				} else {
					articles, err = db.GetUserFavoriteArticles(user, limit, offset)
				}
				if err != nil {
					break
				}
			} else if feedId == "tag:__all__" {
				if newerFirst {
					if unreadOnly {
						articles, err = db.GetUnreadUserArticlesDesc(user, limit, offset)
					} else {
						articles, err = db.GetUserArticlesDesc(user, limit, offset)
					}
				} else {
					if unreadOnly {
						articles, err = db.GetUnreadUserArticles(user, limit, offset)
					} else {
						articles, err = db.GetUserArticles(user, limit, offset)
					}
				}
				if err != nil {
					break
				}
			} else if strings.HasPrefix(feedId, "tag:") {
				tag := feedId[4:]
				if newerFirst {
					if unreadOnly {
						articles, err = db.GetUnreadUserTagArticlesDesc(user, tag, limit, offset)
					} else {
						articles, err = db.GetUserTagArticlesDesc(user, tag, limit, offset)
					}
				} else {
					if unreadOnly {
						articles, err = db.GetUnreadUserTagArticles(user, tag, limit, offset)
					} else {
						articles, err = db.GetUserTagArticles(user, tag, limit, offset)
					}
				}
				if err != nil {
					break
				}
			} else {
				var f readeef.Feed

				var id int64
				id, err = strconv.ParseInt(feedId, 10, 64)

				if err != nil {
					err = errors.New("Unknown feed id " + feedId)
					break
				}

				f, err = db.GetFeed(id)
				/* TODO: non-fatal error */
				if err != nil {
					break
				}

				f.User = user

				if newerFirst {
					if unreadOnly {
						f, err = db.GetUnreadFeedArticlesDesc(f, limit, offset)
					} else {
						f, err = db.GetFeedArticlesDesc(f, limit, offset)
					}
				} else {
					if unreadOnly {
						f, err = db.GetUnreadFeedArticles(f, limit, offset)
					} else {
						f, err = db.GetFeedArticles(f, limit, offset)
					}
				}
				if err != nil {
					break
				}

				articles = f.Articles
			}

			resp["Articles"] = articles
		}

		var b []byte
		if err == nil {
			b, err = json.Marshal(resp)
		}

		if err == nil {
			w.Write(b)
		} else {
			webfw.GetLogger(c).Print(err)

			w.WriteHeader(http.StatusInternalServerError)
		}

	}
}
Exemplo n.º 9
0
func (con Article) Handler(c context.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var err error

		db := readeef.GetDB(c)
		user := readeef.GetUser(c, r)

		params := webfw.GetParams(c, r)
		action := params["action"]

		readeef.Debug.Printf("Invoking Article controller with action '%s', feed id '%s' and article id '%s'\n", action, params["feed-id"], params["article-id"])

		var feedId int64
		feedId, err = strconv.ParseInt(params["feed-id"], 10, 64)

		var article readeef.Article
		if err == nil {
			article, err = db.GetFeedArticle(feedId, params["article-id"], user)
		}

		if err == nil {
			switch action {
			case "read":
				read := params["value"] == "true"
				previouslyRead := article.Read

				if previouslyRead != read {
					err = db.MarkUserArticlesAsRead(user, []readeef.Article{article}, read)

					if err != nil {
						break
					}
				}

				type response struct {
					Success bool
					Read    bool
				}

				resp := response{Success: previouslyRead != read, Read: read}

				var b []byte
				b, err = json.Marshal(resp)
				if err != nil {
					break
				}

				w.Write(b)
			case "favorite":
				favorite := params["value"] == "true"
				previouslyFavorite := article.Favorite

				if previouslyFavorite != favorite {
					err = db.MarkUserArticlesAsFavorite(user, []readeef.Article{article}, favorite)

					if err != nil {
						break
					}
				}

				type response struct {
					Success  bool
					Favorite bool
				}

				resp := response{Success: previouslyFavorite != favorite, Favorite: favorite}

				var b []byte
				b, err = json.Marshal(resp)
				if err != nil {
					break
				}

				w.Write(b)
			}
		}

		if err != nil {
			webfw.GetLogger(c).Print(err)

			w.WriteHeader(http.StatusInternalServerError)
		}
	}
}
Exemplo n.º 10
0
func (con WebSocket) Handler(c context.Context) http.Handler {
	var mutex sync.RWMutex

	receivers := make(map[chan content.Feed]bool)
	logger := webfw.GetLogger(c)

	go func() {
		for {
			select {
			case feed := <-con.updateFeed:
				logger.Infoln("New articles notification for " + feed.String())
				mutex.RLock()

				for receiver, _ := range receivers {
					receiver <- feed
				}

				mutex.RUnlock()
			}
		}
	}()

	cfg := readeef.GetConfig(c)
	return websocket.Handler(func(ws *websocket.Conn) {
		user := readeef.GetUser(c, ws.Request())
		sess := webfw.GetSession(c, ws.Request())

		msg := make(chan apiRequest)
		resp := make(chan apiResponse)

		receiver := make(chan content.Feed)

		mutex.Lock()
		receivers[receiver] = true
		mutex.Unlock()
		defer func() {
			mutex.Lock()
			close(receiver)
			delete(receivers, receiver)
			mutex.Unlock()
		}()

		go func() {
			for {
				var r responseError

				select {
				case data := <-msg:
					var err error
					var processor Processor

					if processor, err = data.processor(c, sess, user, con.fm, con.sp, con.extractor,
						con.capabilities, []byte(cfg.Auth.Secret)); err == nil {
						if len(data.Arguments) > 0 {
							err = json.Unmarshal([]byte(data.Arguments), processor)
						}

						if err == nil {
							r = processor.Process()
						}
					}

					if err != nil {
						r.err = err
						switch err.(type) {
						case *json.UnmarshalTypeError:
							r.errType = errTypeInvalidArgValue
						default:
							if err == errInvalidMethodValue {
								r.errType = errTypeInvalidMethodValue
							} else if err == content.ErrNoContent {
								r.err = errResourceNotFound
								r.errType = errTypeResourceNotFound
							}
						}
					}

					go func() {
						var err string
						if r.err != nil {
							err = r.err.Error()
						}
						resp <- apiResponse{
							Success: r.err == nil, Error: err, ErrorType: r.errType,
							Method: data.Method, Tag: data.Tag, Arguments: r.val,
						}
					}()
				case f := <-receiver:
					if f == nil || user == nil {
						// Socket was closed
						return
					}
					logger.Infoln("Received notification for feed update of " + f.String())

					r := newResponse()

					uf := user.FeedById(f.Data().Id)

					if !uf.HasErr() {
						r.val["Feed"] = uf

						go func() {
							var err string
							if r.err != nil {
								err = r.err.Error()
							}
							resp <- apiResponse{
								Success: r.err == nil, Error: err, ErrorType: r.errType,
								Method: "feed-update-notifier", Tag: "", Arguments: r.val,
							}
						}()
					}
				case r := <-resp:
					websocket.JSON.Send(ws, r)
				}
			}
		}()

		for {
			var data apiRequest
			if err := websocket.JSON.Receive(ws, &data); err != nil {
				if err == io.EOF {
					// Websocket was closed
					break
				} else {
					websocket.JSON.Send(ws, apiResponse{
						Success: false, ErrorType: errTypeMessageParse,
						Error: err.Error(), Method: data.Method,
					})
				}
			}

			if forbidden(c, ws.Request()) {
				websocket.JSON.Send(ws, apiResponse{
					Success: false, ErrorType: errTypeUnauthorized,
					Error: errUnauthorized.Error(), Method: data.Method,
				})
				break
			}

			msg <- data
		}

		logger.Infoln("Closing web socket")
	})
}
Exemplo n.º 11
0
func (con Feed) Handler(c context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		action := webfw.GetMultiPatternIdentifier(c, r)
		user := readeef.GetUser(c, r)

		r.ParseForm()

		var resp responseError
		var feedId int64

		params := webfw.GetParams(c, r)

		switch action {
		case "list":
			resp = listFeeds(user)
		case "discover":
			link := r.FormValue("url")
			resp = discoverFeeds(user, con.fm, link)
		case "opml-export":
			resp = exportOpml(user)
		case "opml":
			buf := util.BufferPool.GetBuffer()
			defer util.BufferPool.Put(buf)

			buf.ReadFrom(r.Body)

			resp = parseOpml(user, con.fm, buf.Bytes())
		case "add":
			links := r.Form["url"]
			resp = addFeeds(user, con.fm, links)
		case "remove":
			if feedId, resp.err = strconv.ParseInt(params["feed-id"], 10, 64); resp.err == nil {
				resp = removeFeed(user, con.fm, data.FeedId(feedId))
			}
		case "tags":
			if feedId, resp.err = strconv.ParseInt(params["feed-id"], 10, 64); resp.err == nil {
				if r.Method == "GET" {
					resp = getFeedTags(user, data.FeedId(feedId))
				} else if r.Method == "POST" {
					if b, err := ioutil.ReadAll(r.Body); err == nil {
						tags := []data.TagValue{}
						if err = json.Unmarshal(b, &tags); err != nil {
							resp.err = fmt.Errorf("Error decoding request body: %s", err)
							break
						}

						resp = setFeedTags(user, data.FeedId(feedId), tags)
					} else {
						resp.err = fmt.Errorf("Error reading request body: %s", err)
						break
					}
				}
			}
		case "read":
			var timestamp, beforeId int64

			if bid, ok := params["before-id"]; ok {
				beforeId, resp.err = strconv.ParseInt(bid, 10, 64)
			} else {
				timestamp, resp.err = strconv.ParseInt(params["timestamp"], 10, 64)
			}

			if resp.err == nil {
				resp = readState(user, params["feed-id"], data.ArticleId(beforeId), timestamp)
			}
		case "articles":
			var limit, offset int

			if limit, resp.err = strconv.Atoi(params["limit"]); resp.err == nil {
				if offset, resp.err = strconv.Atoi(params["offset"]); resp.err == nil {
					minId, _ := strconv.ParseInt(params["min-id"], 10, 64)
					maxId, _ := strconv.ParseInt(params["max-id"], 10, 64)

					resp = getFeedArticles(user, con.sp, params["feed-id"],
						data.ArticleId(minId), data.ArticleId(maxId),
						limit, offset, params["older-first"] == "true", params["unread-only"] == "true")
				}
			}
		}

		switch resp.err {
		case readeef.ErrNoAbsolute:
			resp.val["Error"] = true
			resp.val["ErrorType"] = errTypeNoAbsolute
			resp.err = nil
		case readeef.ErrNoFeed:
			resp.val["Error"] = true
			resp.val["ErrorType"] = errTypeNoFeed
			resp.err = nil
		}

		var b []byte
		if resp.err == nil {
			b, resp.err = json.Marshal(resp.val)
		}

		if resp.err == nil {
			w.Write(b)
		} else {
			webfw.GetLogger(c).Print(resp.err)

			w.WriteHeader(http.StatusInternalServerError)
		}

	})
}
Exemplo n.º 12
0
func (con UserSettings) Handler(c context.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var err error

		db := readeef.GetDB(c)
		user := readeef.GetUser(c, r)

		params := webfw.GetParams(c, r)
		attr := params["attribute"]

		resp := map[string]interface{}{"Login": user.Login}
		if r.Method == "GET" {
			switch attr {
			case "FirstName":
				resp[attr] = user.FirstName
			case "LastName":
				resp[attr] = user.LastName
			case "Email":
				resp[attr] = user.Email
			case "ProfileData":
				resp[attr] = user.ProfileData
			default:
				err = errors.New("Error getting user attribute: unknown attribute " + attr)
			}
		} else if r.Method == "POST" {
			buf := util.BufferPool.GetBuffer()
			defer util.BufferPool.Put(buf)

			buf.ReadFrom(r.Body)

			switch attr {
			case "FirstName":
				user.FirstName = buf.String()
			case "LastName":
				user.LastName = buf.String()
			case "Email":
				user.Email = buf.String()
			case "ProfileData":
				err = json.Unmarshal(buf.Bytes(), &user.ProfileData)
			case "password":
				data := struct {
					Current string
					New     string
				}{}
				err = json.Unmarshal(buf.Bytes(), &data)
				if err == nil {
					/* TODO: non-fatal error */
					if user.Authenticate(data.Current) {
						err = user.SetPassword(data.New)
					} else {
						err = errors.New("Error change user password: current password is invalid")
					}
				}
			default:
				err = errors.New("Error getting user attribute: unknown attribute " + attr)
			}

			if err == nil {
				err = db.UpdateUser(user)
			}

			if err == nil {
				resp["Success"] = true
				resp["Attribute"] = attr
			}
		}

		var b []byte
		if err == nil {
			b, err = json.Marshal(resp)
		}
		if err != nil {
			webfw.GetLogger(c).Print(err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)
	}
}
Exemplo n.º 13
0
func (con User) Handler(c context.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var err error

		db := readeef.GetDB(c)
		user := readeef.GetUser(c, r)

		if !user.Admin {
			readeef.Debug.Println("User " + user.Login + " is not an admin")

			w.WriteHeader(http.StatusForbidden)
			return
		}

		action := webfw.GetMultiPatternIdentifier(c, r)
		params := webfw.GetParams(c, r)
		resp := make(map[string]interface{})

		switch action {
		case "list":
			users, err := db.GetUsers()
			if err != nil {
				break
			}

			type user struct {
				Login     string
				FirstName string
				LastName  string
				Email     string
				Active    bool
				Admin     bool
			}

			userList := []user{}
			for _, u := range users {
				userList = append(userList, user{
					Login:     u.Login,
					FirstName: u.FirstName,
					LastName:  u.LastName,
					Email:     u.Email,
					Active:    u.Active,
					Admin:     u.Admin,
				})
			}

			resp["Users"] = userList
		case "add":
			login := params["login"]

			_, err = db.GetUser(login)
			/* TODO: non-fatal error */
			if err == nil {
				err = errors.New("User with login " + login + " already exists")
				break
			} else if err != sql.ErrNoRows {
				break
			}

			buf := util.BufferPool.GetBuffer()
			defer util.BufferPool.Put(buf)

			buf.ReadFrom(r.Body)

			u := readeef.User{Login: login}

			err = u.SetPassword(buf.String())
			if err != nil {
				break
			}

			err = db.UpdateUser(u)
			if err != nil {
				break
			}

			resp["Success"] = true
			resp["Login"] = login
		case "remove":
			login := params["login"]

			if user.Login == login {
				err = errors.New("The current user cannot be removed")
				break
			}

			var u readeef.User

			u, err = db.GetUser(login)
			if err != nil {
				break
			}

			err = db.DeleteUser(u)
			if err != nil {
				break
			}

			resp["Success"] = true
			resp["Login"] = login
		case "active":
			login := params["login"]

			if user.Login == login {
				err = errors.New("The current user cannot be removed")
				break
			}

			active := params["state"] == "true"

			var u readeef.User

			u, err = db.GetUser(login)
			if err != nil {
				break
			}

			u.Active = active
			err = db.UpdateUser(u)
			if err != nil {
				break
			}

			resp["Success"] = true
			resp["Login"] = login
		}

		var b []byte
		if err == nil {
			b, err = json.Marshal(resp)
		}
		if err != nil {
			webfw.GetLogger(c).Print(err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)
	}
}
Exemplo n.º 14
0
func (con Feed) Handler(c context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		action := webfw.GetMultiPatternIdentifier(c, r)
		user := readeef.GetUser(c, r)

		r.ParseForm()

		var resp responseError
		var feedId int64

		params := webfw.GetParams(c, r)

		if resp.err == nil {
			switch action {
			case "list":
				resp = listFeeds(user)
			case "discover":
				link := r.FormValue("url")
				resp = discoverFeeds(user, con.fm, link)
			case "opml":
				buf := util.BufferPool.GetBuffer()
				defer util.BufferPool.Put(buf)

				buf.ReadFrom(r.Body)

				resp = parseOpml(user, con.fm, buf.Bytes())
			case "add":
				links := r.Form["url"]
				resp = addFeed(user, con.fm, links)
			case "remove":
				if feedId, resp.err = strconv.ParseInt(params["feed-id"], 10, 64); resp.err == nil {
					resp = removeFeed(user, con.fm, data.FeedId(feedId))
				}
			case "tags":
				if feedId, resp.err = strconv.ParseInt(params["feed-id"], 10, 64); resp.err == nil {
					if r.Method == "GET" {
						resp = getFeedTags(user, data.FeedId(feedId))
					} else if r.Method == "POST" {
						decoder := json.NewDecoder(r.Body)

						tags := []data.TagValue{}
						if resp.err = decoder.Decode(&tags); resp.err != nil && resp.err != io.EOF {
							break
						}

						resp.err = nil
						resp = setFeedTags(user, data.FeedId(feedId), tags)
					}
				}
			case "read":
				var timestamp int64

				if timestamp, resp.err = strconv.ParseInt(params["timestamp"], 10, 64); resp.err == nil {
					resp = markFeedAsRead(user, params["feed-id"], timestamp)
				}
			case "articles":
				var limit, offset int

				if limit, resp.err = strconv.Atoi(params["limit"]); resp.err == nil {
					if offset, resp.err = strconv.Atoi(params["offset"]); resp.err == nil {
						resp = getFeedArticles(user, params["feed-id"], limit, offset,
							params["newer-first"] == "true", params["unread-only"] == "true")
					}
				}
			}
		}

		switch resp.err {
		case readeef.ErrNoAbsolute:
			resp.val["Error"] = true
			resp.val["ErrorType"] = errTypeNoAbsolute
			resp.err = nil
		case readeef.ErrNoFeed:
			resp.val["Error"] = true
			resp.val["ErrorType"] = errTypeNoFeed
			resp.err = nil
		}

		var b []byte
		if resp.err == nil {
			b, resp.err = json.Marshal(resp.val)
		}

		if resp.err == nil {
			w.Write(b)
		} else {
			webfw.GetLogger(c).Print(resp.err)

			w.WriteHeader(http.StatusInternalServerError)
		}

	})
}