Commit e534e958 authored by zauberstuhl's avatar zauberstuhl

Implement photo handling in the app server

* using fancybox as JS lib for displaying photos

related to ganggo/federation@5c90e6d6
fixes ganggo/ganggo#35
parent 5b5dc3ce
......@@ -30,3 +30,7 @@ js:node_modules/another-rest-client/rest-client.min.js.map
# Markdown
js:node_modules/marked/marked.min.js
# Gallery
js:node_modules/@fancyapps/fancybox/dist/jquery.fancybox.min.js
css:node_modules/@fancyapps/fancybox/dist/jquery.fancybox.min.css
package models
//
// GangGo Application Server
// Copyright (C) 2017 Lukas Matt <lukas@zauberstuhl.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
import (
"time"
"github.com/revel/revel"
federation "gopkg.in/ganggo/federation.v0"
)
type Photo struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
// size should be max 191 with mysql innodb
// cause asumming we use utf8mb 4*191 = 764 < 767
Guid string `gorm:"size:191"`
RemotePath string `gorm:"type:text"`
Public bool
PersonID uint
Text string `gorm:"type:text"`
PostID uint
Height int
Width int
Person Person
}
type Photos []Photo
func (p Photos) Create() error {
db, err := OpenDatabase()
if err != nil {
return err
}
defer db.Close()
for _, photo := range p {
err = db.Create(&photo).Error
if err != nil {
return err
}
}
return nil
}
func (p *Photos) Cast(entities federation.EntityPhotos) {
// XXX no error handling here that should change
for _, entity := range entities {
var person Person
err := person.FindByAuthor(entity.Author)
if err != nil {
revel.AppLog.Error(err.Error())
continue
}
var post Post
err = post.FindByGuid(entity.StatusMessageGuid)
if err != nil {
revel.AppLog.Error(err.Error())
continue
}
photo := Photo{
Guid: entity.Guid,
Public: entity.Public,
RemotePath: entity.RemotePhotoPath + entity.RemotePhotoName,
Text: entity.Text,
PersonID: person.ID,
PostID: post.ID,
Height: entity.Height,
Width: entity.Width,
}
*p = append(*p, photo)
}
}
......@@ -46,6 +46,7 @@ type Post struct {
Person Person `gorm:"ForeignKey:PersonID" json:",omitempty"`
Comments Comments `gorm:"ForeignKey:ShareableID" json:",omitempty"`
Photos Photos
}
type Posts []Post
......@@ -83,6 +84,11 @@ func (post *Post) AfterFind(db *gorm.DB) error {
return err
}
err = db.Model(post).Related(&post.Photos).Error
if err != nil {
return err
}
return db.Preload("Comments").First(post).Error
}
......@@ -130,6 +136,12 @@ func (p *Post) AfterDelete(db *gorm.DB) (err error) {
revel.AppLog.Error(err.Error())
return
}
// photos
err = db.Where("post_id = ?", p.ID).Delete(Photo{}).Error
if err != nil {
revel.AppLog.Error(err.Error())
return
}
// notifications
err = db.Where("shareable_guid = ? and shareable_type = ?",
p.Guid, ShareablePost).Delete(Notification{}).Error
......@@ -196,6 +208,18 @@ func (p *Post) Cast(entity interface{}) (err error) {
return
}
if statusMessage.Photos != nil {
go func() {
var photos Photos
photos.Cast(*statusMessage.Photos)
err = photos.Create()
if err != nil {
revel.AppLog.Error(err.Error())
return
}
}()
}
(*p).Public = statusMessage.Public
(*p).Guid = statusMessage.Guid
(*p).Type = StatusMessage
......
......@@ -88,6 +88,12 @@ func loadSchema(db *gorm.DB) {
db.Model(post).AddIndex("index_posts_on_id_and_type", "id", "type")
db.AutoMigrate(post)
photo := &Photo{}
db.Model(photo).AddUniqueIndex("index_photos_on_guid", "guid")
db.Model(photo).AddIndex("index_photos_on_post_id", "post_id")
db.Model(photo).AddIndex("index_photos_on_person_id", "person_id")
db.AutoMigrate(photo)
comment := &Comment{}
db.Model(comment).AddUniqueIndex("index_comments_on_guid", "guid")
db.Model(comment).AddIndex("index_comments_on_person_id", "person_id")
......
......@@ -11,6 +11,7 @@
</footer>
{{javascript_include_tag "tether.min"}}
{{javascript_include_tag "bootstrap.min"}}
{{javascript_include_tag "jquery.fancybox.min"}}
{{javascript_include_tag "main"}}
</body>
</html>
......@@ -20,6 +20,7 @@
{{stylesheet_link_tag "bootstrap-grid.min"}}
{{stylesheet_link_tag "bootstrap-reboot.min"}}
{{stylesheet_link_tag "font-awesome.min"}}
{{stylesheet_link_tag "jquery.fancybox.min"}}
{{stylesheet_link_tag "main"}}
<!--
......
......@@ -35,6 +35,13 @@
</div>
<div class="card-block">
<div class="card-body">
<p class="card-text">
{{range .post.Photos}}
<a data-fancybox="gallery{{$.post.ID}}" href="{{.RemotePath}}">
<img src="{{.RemotePath}}" alt="{{.Text}}" class="img-thumbnail w-25">
</a>
{{end}}
</p>
<p class="card-text" data-markdown>{{.post.Text}}</p>
<p class="card-text">
<p class="p-0 m-0 float-left">
......
......@@ -55,6 +55,13 @@
</div>
<div class="card-block">
<div class="card-body">
<p class="card-text">
{{range .post.Photos}}
<a data-fancybox="gallery{{$.post.ID}}" href="{{.RemotePath}}">
<img src="{{.RemotePath}}" alt="{{.Text}}" class="img-thumbnail w-25">
</a>
{{end}}
</p>
<p class="card-text" data-markdown>{{.post.Text}}</p>
<p class="card-text">
<p class="p-0 m-0 float-left">
......
......@@ -4,6 +4,11 @@
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@fancyapps/fancybox": {
"version": "3.2.5",
"resolved": "https://registry.npmjs.org/@fancyapps/fancybox/-/fancybox-3.2.5.tgz",
"integrity": "sha512-1LHcYj96LnmNML4cZndQA+4vuQrtI1E4wi9vpP7B8Y60ieALIUMDZE0J4VMMMJ/xW0XFMpof8RTng3Qy3u3XkQ=="
},
"abbrev": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
......
......@@ -7,6 +7,7 @@
"test": "tests"
},
"dependencies": {
"@fancyapps/fancybox": "^3.2.5",
"another-rest-client": "^0.4.2",
"bootstrap": "^4.0.0-alpha.6",
"coffee-script": "^1.12.7",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment