Skip to content

Commit

Permalink
chore:added more information to news (#265)
Browse files Browse the repository at this point in the history
* added more information to news

* fixed the news test

* fixed news Icon urls not being found

* fixed linting issues
  • Loading branch information
CommanderStorm authored Oct 16, 2023
1 parent 50e9844 commit 2d4c8c7
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 47 deletions.
38 changes: 31 additions & 7 deletions server/api/tumdev/campus_backend.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion server/api/tumdev/campus_backend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,12 @@ message News {
string link = 4;
// where a news thumbnail is stored. empty string means no image is available
string image_url = 5;
string source = 6;
// the id of the news source
string source_id = 6;
// where the icon can be found
string source_icon_url = 9;
// human readable title of the news source
string source_title = 10;
// when the news item was created in OUR database
google.protobuf.Timestamp created = 7;
// the date of the news item
Expand Down
13 changes: 11 additions & 2 deletions server/api/tumdev/campus_backend.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1623,8 +1623,17 @@
"type": "string",
"title": "where a news thumbnail is stored. empty string means no image is available"
},
"source": {
"type": "string"
"sourceId": {
"type": "string",
"title": "the id of the news source"
},
"sourceIconUrl": {
"type": "string",
"title": "where the icon can be found"
},
"sourceTitle": {
"type": "string",
"title": "human readable title of the news source"
},
"created": {
"type": "string",
Expand Down
19 changes: 10 additions & 9 deletions server/backend/cron/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,16 @@ func (c *CronService) parseNewsFeed(source model.NewsSource) error {
}

newsItem := model.News{
Date: *item.PublishedParsed,
Created: time.Now(),
Title: item.Title,
Description: bluemonday.StrictPolicy().Sanitize(item.Description),
Src: source.Source,
Link: item.Link,
Image: enclosureUrl,
FileID: fileID,
File: file,
Date: *item.PublishedParsed,
Created: time.Now(),
Title: item.Title,
Description: bluemonday.StrictPolicy().Sanitize(item.Description),
NewsSourceID: source.Source,
NewsSource: source,
Link: item.Link,
Image: enclosureUrl,
FileID: fileID,
File: file,
}
newNews = append(newNews, newsItem)
}
Expand Down
20 changes: 11 additions & 9 deletions server/backend/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (s *CampusServer) ListNews(ctx context.Context, req *pb.ListNewsRequest) (*
}

var newsEntries []model.News
tx := s.db.WithContext(ctx).Joins("File")
tx := s.db.WithContext(ctx).Joins("File").Joins("NewsSource").Joins("NewsSource.File")
if req.NewsSource != 0 {
tx = tx.Where("src = ?", req.NewsSource)
}
Expand All @@ -68,14 +68,16 @@ func (s *CampusServer) ListNews(ctx context.Context, req *pb.ListNewsRequest) (*
imgUrl = item.File.FullExternalUrl()
}
resp[i] = &pb.News{
Id: item.News,
Title: item.Title,
Text: item.Description,
Link: item.Link,
ImageUrl: imgUrl,
Source: fmt.Sprintf("%d", item.Src),
Created: timestamppb.New(item.Created),
Date: timestamppb.New(item.Date),
Id: item.News,
Title: item.Title,
Text: item.Description,
Link: item.Link,
ImageUrl: imgUrl,
SourceId: fmt.Sprintf("%d", item.NewsSource.Source),
SourceTitle: item.NewsSource.Title,
SourceIconUrl: item.NewsSource.File.FullExternalUrl(),
Created: timestamppb.New(item.Created),
Date: timestamppb.New(item.Date),
}
}
return &pb.ListNewsReply{News: resp}, nil
Expand Down
14 changes: 7 additions & 7 deletions server/backend/news_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ func (s *NewsSuite) Test_ListNewsSourcesNone() {
require.Equal(s.T(), expectedResp, response)
}

const ExpectedListNewsQuery = "SELECT `news`.`news`,`news`.`date`,`news`.`created`,`news`.`title`,`news`.`description`,`news`.`src`,`news`.`link`,`news`.`image`,`news`.`file`,`File`.`file` AS `File__file`,`File`.`name` AS `File__name`,`File`.`path` AS `File__path`,`File`.`downloads` AS `File__downloads`,`File`.`url` AS `File__url`,`File`.`downloaded` AS `File__downloaded` FROM `news` LEFT JOIN `files` `File` ON `news`.`file` = `File`.`file`"
const ExpectedListNewsQuery = "SELECT `news`.`news`,`news`.`date`,`news`.`created`,`news`.`title`,`news`.`description`,`news`.`src`,`news`.`link`,`news`.`image`,`news`.`file`,`File`.`file` AS `File__file`,`File`.`name` AS `File__name`,`File`.`path` AS `File__path`,`File`.`downloads` AS `File__downloads`,`File`.`url` AS `File__url`,`File`.`downloaded` AS `File__downloaded`,`NewsSource`.`source` AS `NewsSource__source`,`NewsSource`.`title` AS `NewsSource__title`,`NewsSource`.`url` AS `NewsSource__url`,`NewsSource`.`icon` AS `NewsSource__icon`,`NewsSource`.`hook` AS `NewsSource__hook`,`NewsSource__File`.`file` AS `NewsSource__File__file`,`NewsSource__File`.`name` AS `NewsSource__File__name`,`NewsSource__File`.`path` AS `NewsSource__File__path`,`NewsSource__File`.`downloads` AS `NewsSource__File__downloads`,`NewsSource__File`.`url` AS `NewsSource__File__url`,`NewsSource__File`.`downloaded` AS `NewsSource__File__downloaded` FROM `news` LEFT JOIN `files` `File` ON `news`.`file` = `File`.`file` LEFT JOIN `newsSource` `NewsSource` ON `news`.`src` = `NewsSource`.`source` LEFT JOIN `files` `NewsSource__File` ON `NewsSource`.`icon` = `NewsSource__File`.`file`"

func (s *NewsSuite) Test_ListNewsNone_withFilters() {
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedListNewsQuery+" WHERE src = ? AND news > ?")).
WithArgs(1, 2).
WillReturnRows(sqlmock.NewRows([]string{"news", "date", "created", "title", "description", "src", "link", "image", "file", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"}))
WillReturnRows(sqlmock.NewRows([]string{"news", "date", "created", "title", "description", "src", "link", "image", "file", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded", "source", "NewsSource__source", "NewsSource__title", "NewsSource__url", "NewsSource__icon", "NewsSource__hook", "NewsSource__File__file", "NewsSource__File__name", "NewsSource__File__path", "NewsSource__File__downloads", "NewsSource__File__url", "NewsSource__File__downloaded"}))

meta := metadata.NewIncomingContext(context.Background(), metadata.MD{})
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
Expand All @@ -159,7 +159,7 @@ func (s *NewsSuite) Test_ListNewsNone_withFilters() {
}
func (s *NewsSuite) Test_ListNewsNone() {
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedListNewsQuery)).
WillReturnRows(sqlmock.NewRows([]string{"news", "date", "created", "title", "description", "src", "link", "image", "file", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"}))
WillReturnRows(sqlmock.NewRows([]string{"news", "date", "created", "title", "description", "src", "link", "image", "file", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded", "source", "NewsSource__source", "NewsSource__title", "NewsSource__url", "NewsSource__icon", "NewsSource__hook", "NewsSource__File__file", "NewsSource__File__name", "NewsSource__File__path", "NewsSource__File__downloads", "NewsSource__File__url", "NewsSource__File__downloaded"}))

meta := metadata.NewIncomingContext(context.Background(), metadata.MD{})
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
Expand All @@ -175,17 +175,17 @@ func (s *NewsSuite) Test_ListNewsMultiple() {
n2 := news2()
s.mock.ExpectQuery(regexp.QuoteMeta(" ")).
WillReturnRows(sqlmock.NewRows([]string{"news", "date", "created", "title", "description", "src", "link", "image", "file", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"}).
AddRow(n1.News, n1.Date, n1.Created, n1.Title, n1.Description, n1.Src, n1.Link, n1.Image, n1.FileID, n1.File.File, n1.File.Name, n1.File.Path, n1.File.Downloads, n1.File.URL, n1.File.Downloaded).
AddRow(n2.News, n2.Date, n2.Created, n2.Title, n2.Description, n2.Src, n2.Link, n2.Image, nil, nil, nil, nil, nil, nil, nil))
AddRow(n1.News, n1.Date, n1.Created, n1.Title, n1.Description, n1.NewsSourceID, n1.Link, n1.Image, n1.FileID, n1.File.File, n1.File.Name, n1.File.Path, n1.File.Downloads, n1.File.URL, n1.File.Downloaded).
AddRow(n2.News, n2.Date, n2.Created, n2.Title, n2.Description, n2.NewsSourceID, n2.Link, n2.Image, nil, nil, nil, nil, nil, nil, nil))

meta := metadata.NewIncomingContext(context.Background(), metadata.MD{})
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
response, err := server.ListNews(meta, &pb.ListNewsRequest{})
require.NoError(s.T(), err)
expectedResp := &pb.ListNewsReply{
News: []*pb.News{
{Id: n1.News, Title: n1.Title, Text: n1.Description, Link: n1.Link, ImageUrl: "https://api.tum.app/files/news/sources/src_1.png", Source: fmt.Sprintf("%d", n1.Src), Created: timestamppb.New(n1.Created), Date: timestamppb.New(n1.Date)},
{Id: n2.News, Title: n2.Title, Text: n2.Description, Link: n2.Link, ImageUrl: "", Source: fmt.Sprintf("%d", n2.Src), Created: timestamppb.New(n2.Created), Date: timestamppb.New(n2.Date)},
{Id: n1.News, Title: n1.Title, Text: n1.Description, Link: n1.Link, ImageUrl: "https://api.tum.app/files/news/sources/src_1.png", SourceId: fmt.Sprintf("%d", n1.NewsSourceID), SourceIconUrl: n1.NewsSource.File.FullExternalUrl(), SourceTitle: n1.NewsSource.Title, Created: timestamppb.New(n1.Created), Date: timestamppb.New(n1.Date)},
{Id: n2.News, Title: n2.Title, Text: n2.Description, Link: n2.Link, ImageUrl: "", SourceId: fmt.Sprintf("%d", n2.NewsSourceID), SourceIconUrl: n2.NewsSource.File.FullExternalUrl(), SourceTitle: n2.NewsSource.Title, Created: timestamppb.New(n2.Created), Date: timestamppb.New(n2.Date)},
},
}
require.Equal(s.T(), expectedResp, response)
Expand Down
4 changes: 2 additions & 2 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ func main() {
func UnaryRequestLogger(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
start := time.Now()
resp, err := handler(ctx, req)
fields := log.Fields{"elapsed": time.Now().Sub(start)}
fields := log.Fields{"elapsed": time.Since(start)}
log.WithContext(ctx).WithFields(fields).WithError(err).Info(info.FullMethod)
return resp, err
}
func StreamRequestLogger(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
start := time.Now()
err := handler(srv, stream)
log.WithField("elapsed", time.Now().Sub(start)).WithError(err).Info(info.FullMethod)
log.WithField("elapsed", time.Since(start)).WithError(err).Info(info.FullMethod)
return err
}

Expand Down
21 changes: 11 additions & 10 deletions server/model/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ var (

// News struct is a row record of the news table in the tca database
type News struct {
News int64 `gorm:"primary_key;AUTO_INCREMENT;column:news;type:int;"`
Date time.Time `gorm:"column:date;type:datetime;"`
Created time.Time `gorm:"column:created;type:timestamp;default:CURRENT_TIMESTAMP;"`
Title string `gorm:"column:title;type:text;size:255;"`
Description string `gorm:"column:description;type:text;size:65535;"`
Src int64 `gorm:"column:src;type:int;"`
Link string `gorm:"column:link;type:varchar(190);"`
Image null.String `gorm:"column:image;type:text;size:65535;"`
FileID null.Int `gorm:"column:file;type:int;"`
File *File `gorm:"foreignKey:FileID;references:file;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
News int64 `gorm:"primary_key;AUTO_INCREMENT;column:news;type:int;"`
Date time.Time `gorm:"column:date;type:datetime;"`
Created time.Time `gorm:"column:created;type:timestamp;default:CURRENT_TIMESTAMP;"`
Title string `gorm:"column:title;type:text;size:255;"`
Description string `gorm:"column:description;type:text;size:65535;"`
NewsSourceID int64 `gorm:"column:src;type:int;"`
NewsSource NewsSource `gorm:"foreignKey:NewsSourceID;references:source;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Link string `gorm:"column:link;type:varchar(190);"`
Image null.String `gorm:"column:image;type:text;size:65535;"`
FileID null.Int `gorm:"column:file;type:int;"`
File *File `gorm:"foreignKey:FileID;references:file;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}

// TableName sets the insert table name for this struct type
Expand Down

0 comments on commit 2d4c8c7

Please sign in to comment.