forked from nerdzeu/nerdz-api
/
models.go
1526 lines (1374 loc) · 39.3 KB
/
models.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2016 Paolo Galeone <nessuno@nerdz.eu>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package nerdz
import (
"database/sql"
"github.com/galeone/igor"
"strings"
"time"
)
// Enrich models structure with unexported types
// boardType represents a board type
type boardType string
const (
// UserBoardID constant (of type boardType) makes possible to distinguish a User
// board from a Project board
UserBoardID boardType = "user"
// ProjectBoardID constant (of type boardType) makes possible to distinguish a PROJECT
// board from a User board
ProjectBoardID boardType = "project"
)
// Transferable represents a common interface for all the
// types defined by the backend that are able to generate
// a data structure that can be returned by the API
type Transferable interface {
// GetTO returns a proper data structure for the API
// users (max len 1) is the current user viewing the API (the one who grants
// the proprer access to the client application)
// Is optional because not all the TO are different (ore have different content)
// if the user is a specific user
GetTO(users ...*User) interface{}
}
// Models
// UserPostLock is the model for the relation posts_no_notify
type UserPostLock struct {
User uint64
Hpid uint64
Time time.Time `sql:"default:(now() at time zone 'utc')"`
Counter uint64 `igor:"primary_key"`
}
// GetTO returns its Transfer Object
func (u *UserPostLock) GetTO(users ...*User) *UserPostLockTO {
var userInfo *InfoTO
if user, e := NewUser(u.User); e == nil {
userInfo = user.Info().GetTO()
}
return &UserPostLockTO{
original: u,
User: userInfo,
Hpid: u.Hpid,
Time: u.Time,
Timestamp: u.Time.Unix(),
Counter: u.Counter,
}
}
// TableName returns the table name associated with the structure
func (UserPostLock) TableName() string {
return "posts_no_notify"
}
// UserPostUserLock is the model for the relation comments_no_notify
type UserPostUserLock struct {
From uint64
To uint64
Hpid uint64
Time time.Time `sql:"default:(now() at time zone 'utc')"`
Counter uint64 `igor:"primary_key"`
}
// TableName returns the table name associated with the structure
func (UserPostUserLock) TableName() string {
return "comments_no_notify"
}
// GetTO returns its Transfer Object
func (u *UserPostUserLock) GetTO(users ...*User) *UserPostUserLockTO {
var fromInfo, toInfo *InfoTO
if from, e := NewUser(u.From); e == nil {
fromInfo = from.Info().GetTO()
}
if to, e := NewUser(u.To); e == nil {
toInfo = to.Info().GetTO()
}
return &UserPostUserLockTO{
original: u,
FromInfo: fromInfo,
ToInfo: toInfo,
Hpid: u.Hpid,
Time: u.Time,
Timestamp: u.Time.Unix(),
Counter: u.Counter,
}
}
// UserPostCommentsNotify is the model for the relation comments_notify
type UserPostCommentsNotify struct {
From uint64
To uint64
Hpid uint64
Time time.Time `sql:"default:(now() at time zone 'utc')"`
Counter uint64 `igor:"primary_key"`
}
// GetTO returns its Transfer Object
func (u *UserPostCommentsNotify) GetTO(users ...*User) *UserPostCommentsNotifyTO {
var fromInfo, toInfo *InfoTO
if from, e := NewUser(u.From); e == nil {
fromInfo = from.Info().GetTO()
}
if to, e := NewUser(u.To); e == nil {
toInfo = to.Info().GetTO()
}
return &UserPostCommentsNotifyTO{
original: u,
FromInfo: fromInfo,
ToInfo: toInfo,
Hpid: u.Hpid,
Time: u.Time,
Timestamp: u.Time.Unix(),
Counter: u.Counter,
}
}
// TableName returns the table name associated with the structure
func (UserPostCommentsNotify) TableName() string {
return "comments_notify"
}
// Ban is the model for the relation ban
type Ban struct {
User uint64
Motivation string
Time time.Time `sql:"default:(now() at time zone 'utc')"`
Counter uint64 `igor:"primary_key"`
}
// GetTO returns its Transfer Object
func (b *Ban) GetTO(users ...*User) *BanTO {
var userInfo *InfoTO
if user, e := NewUser(b.User); e == nil {
userInfo = user.Info().GetTO()
}
return &BanTO{
original: b,
User: userInfo,
Motivation: b.Motivation,
Time: b.Time,
Timestamp: b.Time.Unix(),
Counter: b.Counter,
}
}
// TableName returns the table name associated with the structure
func (Ban) TableName() string {
return "ban"
}
// Blacklist is the model for the relation blacklist
type Blacklist struct {
From uint64
To uint64
Motivation string
Time time.Time `sql:"default:(now() at time zone 'utc')"`
Counter uint64 `igor:"primary_key"`
}
// GetTO returns its Transfer Object
func (b *Blacklist) GetTO(users ...*User) *BlacklistTO {
var fromInfo, toInfo *InfoTO
if from, e := NewUser(b.From); e == nil {
fromInfo = from.Info().GetTO()
}
if to, e := NewUser(b.To); e == nil {
toInfo = to.Info().GetTO()
}
return &BlacklistTO{
original: b,
FromInfo: fromInfo,
ToInfo: toInfo,
Motivation: b.Motivation,
Time: b.Time,
Timestamp: b.Time.Unix(),
Counter: b.Counter,
}
}
// TableName returns the table name associated with the structure
func (Blacklist) TableName() string {
return "blacklist"
}
// Whitelist is the model for the relation whitelist
type Whitelist struct {
From uint64
To uint64
Time time.Time `sql:"default:(now() at time zone 'utc')"`
Counter uint64 `igor:"primary_key"`
}
// GetTO returns its Transfer Object
func (w *Whitelist) GetTO(users ...*User) *WhitelistTO {
var fromInfo, toInfo *InfoTO
if from, e := NewUser(w.From); e == nil {
fromInfo = from.Info().GetTO()
}
if to, e := NewUser(w.To); e == nil {
toInfo = to.Info().GetTO()
}
return &WhitelistTO{
original: w,
FromInfo: fromInfo,
ToInfo: toInfo,
Time: w.Time,
Timestamp: w.Time.Unix(),
Counter: w.Counter,
}
}
// TableName returns the table name associated with the structure
func (Whitelist) TableName() string {
return "whitelist"
}
// UserFollower is the model for the relation followers
type UserFollower struct {
From uint64
To uint64
Time time.Time `sql:"default:(now() at time zone 'utc')"`
ToNotify bool
Counter uint64 `igor:"primary_key"`
}
// TableName returns the table name associated with the structure
func (UserFollower) TableName() string {
return "followers"
}
// GetTO returns its Transfer Object
func (u *UserFollower) GetTO(users ...*User) *UserFollowerTO {
var fromInfo, toInfo *InfoTO
if from, e := NewUser(u.From); e == nil {
fromInfo = from.Info().GetTO()
}
if to, e := NewUser(u.To); e == nil {
toInfo = to.Info().GetTO()
}
return &UserFollowerTO{
original: u,
FromInfo: fromInfo,
ToInfo: toInfo,
Time: u.Time,
Timestamp: u.Time.Unix(),
ToNotify: u.ToNotify,
Counter: u.Counter,
}
}
// ProjectNotify is the model for the relation groups_notify
type ProjectNotify struct {
From uint64
To uint64
Time time.Time `sql:"default:(now() at time zone 'utc')"`
Hpid uint64
Counter uint64 `igor:"primary_key"`
}
// TableName returns the table name associated with the structure
func (ProjectNotify) TableName() string {
return "groups_notify"
}
// GetTO returns its Transfer Object
func (p *ProjectNotify) GetTO(users ...*User) *ProjectNotifyTO {
var fromInfo, toInfo *InfoTO
if from, e := NewProject(p.From); e == nil {
fromInfo = from.Info().GetTO()
}
if to, e := NewUser(p.To); e == nil {
toInfo = to.Info().GetTO()
}
return &ProjectNotifyTO{
original: p,
FromInfo: fromInfo,
ToInfo: toInfo,
Time: p.Time,
Timestamp: p.Time.Unix(),
Hpid: p.Hpid,
Counter: p.Counter,
}
}
// ProjectPostLock is the model for the relation groups_posts_no_notify
type ProjectPostLock struct {
User uint64
Hpid uint64
Time time.Time `sql:"default:(now() at time zone 'utc')"`
Counter uint64 `igor:"primary_key"`
}
// GetTO returns its Transfer Object
func (p *ProjectPostLock) GetTO(users ...*User) *ProjectPostLockTO {
var userInfo *InfoTO
if user, e := NewUser(p.User); e == nil {
userInfo = user.Info().GetTO()
}
return &ProjectPostLockTO{
original: p,
User: userInfo,
Hpid: p.Hpid,
Time: p.Time,
Timestamp: p.Time.Unix(),
Counter: p.Counter,
}
}
// TableName returns the table name associated with the structure
func (ProjectPostLock) TableName() string {
return "groups_posts_no_notify"
}
// ProjectPostUserLock is the model for the relation groups_comments_no_notify
type ProjectPostUserLock struct {
From uint64
To uint64
Hpid uint64
Time time.Time `sql:"default:(now() at time zone 'utc')"`
Counter uint64 `igor:"primary_key"`
}
// GetTO returns its Transfer Object
func (p *ProjectPostUserLock) GetTO(users ...*User) *ProjectPostUserLockTO {
var fromInfo, toInfo *InfoTO
if from, e := NewUser(p.From); e == nil {
fromInfo = from.Info().GetTO()
}
if to, e := NewUser(p.To); e == nil {
toInfo = to.Info().GetTO()
}
return &ProjectPostUserLockTO{
original: p,
FromInfo: fromInfo,
ToInfo: toInfo,
Hpid: p.Hpid,
Time: p.Time,
Timestamp: p.Time.Unix(),
Counter: p.Counter,
}
}
// TableName returns the table name associated with the structure
func (ProjectPostUserLock) TableName() string {
return "groups_comments_no_notify"
}
// ProjectPostCommentsNotify is the model for the relation groups_comments_notify
type ProjectPostCommentsNotify struct {
From uint64
To uint64
Hpid uint64
Time time.Time `sql:"default:(now() at time zone 'utc')"`
Counter uint64 `igor:"primary_key"`
}
// GetTO returns its Transfer Object
func (p *ProjectPostCommentsNotify) GetTO(users ...*User) *ProjectPostCommentsNotifyTO {
var fromInfo, toInfo *InfoTO
if from, e := NewUser(p.From); e == nil {
fromInfo = from.Info().GetTO()
}
if to, e := NewUser(p.To); e == nil {
toInfo = to.Info().GetTO()
}
return &ProjectPostCommentsNotifyTO{
original: p,
FromInfo: fromInfo,
ToInfo: toInfo,
Hpid: p.Hpid,
Time: p.Time,
Timestamp: p.Time.Unix(),
Counter: p.Counter,
}
}
// TableName returns the table name associated with the structure
func (ProjectPostCommentsNotify) TableName() string {
return "groups_comments_notify"
}
// User is the model for the relation users
type User struct {
Counter uint64 `igor:"primary_key"`
Last time.Time `sql:"default:(now() at time zone 'utc')"`
NotifyStory igor.JSON `sql:"default:'{}'::jsonb"`
Private bool
Lang string
Username string
Password string
RemoteAddr string
HTTPUserAgent string `igor:"column:http_user_agent"`
Email string
Name string
Surname string
Gender bool
BirthDate time.Time `sql:"default:(now() at time zone 'utc')"`
BoardLang string
Timezone string
Viewonline bool
RegistrationTime time.Time `sql:"default:(now() at time zone 'utc')"`
// Relation. Manually fill the field when required
Profile Profile `sql:"-"`
}
// TableName returns the table name associated with the structure
func (User) TableName() string {
return "users"
}
// GetTO returns its Transfer Object: *User GetTO embeds *Profile GetTO
func (u *User) GetTO(users ...*User) *UserTO {
return &UserTO{
original: u,
Counter: u.Counter,
Last: u.Last,
NotifyStory: u.NotifyStory,
Private: u.Private,
Lang: u.Lang,
Username: u.Username,
Name: u.Name,
Surname: u.Surname,
Gender: u.Gender,
BirthDate: u.BirthDate,
BoardLang: u.BoardLang,
Timezone: u.Timezone,
Viewonline: u.Viewonline,
RegistrationTime: u.RegistrationTime,
Profile: ProfileTO{
Counter: u.Profile.Counter,
Website: u.Profile.Website,
Quotes: strings.Split(u.Profile.Quotes, "\n"),
Biography: u.Profile.Biography,
Interests: u.Interests(),
Github: u.Profile.Github,
Skype: u.Profile.Skype,
Jabber: u.Profile.Jabber,
Yahoo: u.Profile.Yahoo,
Userscript: u.Profile.Userscript,
Template: u.Profile.Template,
MobileTemplate: u.Profile.MobileTemplate,
Dateformat: u.Profile.Dateformat,
Facebook: u.Profile.Facebook,
Twitter: u.Profile.Twitter,
Steam: u.Profile.Steam,
Push: u.Profile.Push,
Pushregtime: u.Profile.Pushregtime,
Closed: u.Profile.Closed,
},
}
}
// Profile is the model for the relation profiles
type Profile struct {
Counter uint64 `igor:"primary_key"`
Website string
Quotes string
Biography string
Github string
Skype string
Jabber string
Yahoo string
Userscript string
Template uint8
MobileTemplate uint8
Dateformat string
Facebook string
Twitter string
Steam string
Push bool
Pushregtime time.Time `sql:"default:(now() at time zone 'utc')"`
Closed bool
}
// TableName returns the table name associated with the structure
func (Profile) TableName() string {
return "profiles"
}
// Interest is the model for the relation interests
type Interest struct {
ID uint64 `igor:"primary_key"`
From uint64
Value string
Time time.Time `sql:"default:(now() at time zone 'utc')"`
}
// TableName returns the table name associated with the structure
func (Interest) TableName() string {
return "interests"
}
// Post is the type of a generic post
type Post struct {
Hpid uint64 `igor:"primary_key"`
From uint64
To uint64
Pid uint64 `sql:"default:0"`
Message string
Time time.Time `sql:"default:(now() at time zone 'utc')"`
Lang string
News bool
Closed bool
}
// UserPost converts the Post to a UserPost
func (p *Post) UserPost() *UserPost {
return &UserPost{
Post: *p,
}
}
// ProjectPost converts the Post to ProjectPost
func (p *Post) ProjectPost() *ProjectPost {
return &ProjectPost{
Post: *p,
}
}
// GetTO returns its Transfer Object
func (p *Post) GetTO(users ...*User) *PostTO {
return &PostTO{
original: p,
Hpid: p.Hpid,
Pid: p.Pid,
Message: p.Message,
Time: p.Time,
Timestamp: p.Time.Unix(),
Lang: p.Lang,
News: p.News,
Closed: p.Closed,
}
}
// UserPost is the model for the relation posts
type UserPost struct {
Post
}
// GetTO returns its Transfer Object
func (p *UserPost) GetTO(users ...*User) *PostTO {
if len(users) != 1 {
panic("UserPost.GetTO requires a user parameter")
}
user := users[0]
postTO := p.Post.GetTO()
postTO.Type = UserBoardID
if from, e := NewUser(p.From); e == nil {
postTO.FromInfo = from.Info().GetTO()
}
if to, e := NewUser(p.To); e == nil {
postTO.ToInfo = to.Info().GetTO()
}
postTO.Rate = p.VotesCount()
postTO.RevisionsCount = p.RevisionsNumber()
postTO.CommentsCount = p.CommentsCount()
postTO.BookmarksCount = p.BookmarksCount()
postTO.LurkersCount = p.LurkersCount()
postTO.Timestamp = p.Time.Unix()
postTO.URL = p.URL().String()
postTO.CanBookmark = user.CanBookmark(p)
postTO.CanComment = user.CanComment(p)
postTO.CanDelete = user.CanDelete(p)
postTO.CanEdit = user.CanEdit(p)
postTO.CanLurk = user.CanLurk(p)
return postTO
}
// TableName returns the table name associated with the structure
func (UserPost) TableName() string {
return "posts"
}
// UserPostRevision is the model for the relation posts_revisions
type UserPostRevision struct {
Hpid uint64
Message string
Time time.Time `sql:"default:(now() at time zone 'utc')"`
RevNo uint16
Counter uint64 `igor:"primary_key"`
}
// GetTO returns its Transfer Object
func (p *UserPostRevision) GetTO(users ...*User) *UserPostRevisionTO {
return &UserPostRevisionTO{
original: p,
Hpid: p.Hpid,
Message: p.Message,
Time: p.Time,
Timestamp: p.Time.Unix(),
RevNo: p.RevNo,
Counter: p.Counter,
}
}
// TableName returns the table name associated with the structure
func (UserPostRevision) TableName() string {
return "posts_revisions"
}
// UserPostVote is the model for the relation votes
type UserPostVote struct {
Hpid uint64
From uint64
To uint64
Vote int8
Time time.Time `sql:"default:(now() at time zone 'utc')"`
Counter uint64 `igor:"primary_key"`
}
// TableName returns the table name associated with the structure
func (UserPostVote) TableName() string {
return "thumbs"
}
// GetTO returns its Transfer Object
func (t *UserPostVote) GetTO(users ...*User) *UserPostVoteTO {
var fromInfo, toInfo *InfoTO
if from, e := NewUser(t.From); e == nil {
fromInfo = from.Info().GetTO()
}
if to, e := NewUser(t.To); e == nil {
toInfo = to.Info().GetTO()
}
return &UserPostVoteTO{
original: t,
Hpid: t.Hpid,
FromInfo: fromInfo,
ToInfo: toInfo,
Vote: t.Vote,
Time: t.Time,
Timestamp: t.Time.Unix(),
Counter: t.Counter,
}
}
// UserPostLurk is the model for the relation lurkers
type UserPostLurk struct {
Hpid uint64
From uint64
To uint64
Time time.Time `sql:"default:(now() at time zone 'utc')"`
Counter uint64 `igor:"primary_key"`
}
// GetTO returns its Transfer Object
func (l *UserPostLurk) GetTO(users ...*User) *UserPostLurkTO {
var fromInfo, toInfo *InfoTO
if from, e := NewUser(l.From); e == nil {
fromInfo = from.Info().GetTO()
}
if to, e := NewUser(l.To); e == nil {
toInfo = to.Info().GetTO()
}
return &UserPostLurkTO{
original: l,
Hpid: l.Hpid,
FromInfo: fromInfo,
ToInfo: toInfo,
Time: l.Time,
Timestamp: l.Time.Unix(),
Counter: l.Counter,
}
}
// TableName returns the table name associated with the structure
func (UserPostLurk) TableName() string {
return "lurkers"
}
// UserPostComment is the model for the relation comments
type UserPostComment struct {
Hcid uint64 `igor:"primary_key"`
Hpid uint64
From uint64
To uint64
Message string
Lang string
Time time.Time `sql:"default:(now() at time zone 'utc')"`
Editable bool `sql:"default:true"`
}
// GetTO returns its Transfer Object
func (c *UserPostComment) GetTO(users ...*User) *UserPostCommentTO {
if len(users) != 1 {
panic("UserPostComment.GetTO requires a user parameter")
}
user := users[0]
var fromInfo, toInfo *InfoTO
if from, e := NewUser(c.From); e == nil {
fromInfo = from.Info().GetTO()
}
if to, e := NewUser(c.To); e == nil {
toInfo = to.Info().GetTO()
}
return &UserPostCommentTO{
original: c,
Hcid: c.Hcid,
Hpid: c.Hpid,
FromInfo: fromInfo,
ToInfo: toInfo,
Message: c.Message,
Lang: c.Lang,
Time: c.Time,
Timestamp: c.Time.Unix(),
CanEdit: user.CanEdit(c),
CanDelete: user.CanDelete(c),
}
}
// TableName returns the table name associated with the structure
func (UserPostComment) TableName() string {
return "comments"
}
// UserPostCommentRevision is the model for the relation comments_revisions
type UserPostCommentRevision struct {
Hcid uint64
Message string
Time time.Time `sql:"default:(now() at time zone 'utc')"`
RevNo int8
Counter uint64 `igor:"primary_key"`
}
// TableName returns the table name associated with the structure
func (UserPostCommentRevision) TableName() string {
return "comments_revisions"
}
// GetTO returns its Transfer Object
func (c *UserPostCommentRevision) GetTO(users ...*User) *UserPostCommentRevisionTO {
return &UserPostCommentRevisionTO{
original: c,
Hcid: c.Hcid,
Message: c.Message,
Time: c.Time,
Timestamp: c.Time.Unix(),
RevNo: c.RevNo,
Counter: c.Counter,
}
}
// UserPostBookmark is the model for the relation bookmarks
type UserPostBookmark struct {
Hpid uint64
From uint64
Time time.Time `sql:"default:(now() at time zone 'utc')"`
Counter uint64 `igor:"primary_key"`
}
// TableName returns the table name associated with the structure
func (UserPostBookmark) TableName() string {
return "bookmarks"
}
// GetTO returns its Transfer Object
func (b *UserPostBookmark) GetTO(users ...*User) *UserPostBookmarkTO {
var fromInfo *InfoTO
if from, e := NewUser(b.From); e == nil {
fromInfo = from.Info().GetTO()
}
return &UserPostBookmarkTO{
original: b,
Hpid: b.Hpid,
FromInfo: fromInfo,
Time: b.Time,
Timestamp: b.Time.Unix(),
Counter: b.Counter,
}
}
// Pm is the model for the relation pms
type Pm struct {
Pmid uint64 `igor:"primary_key"`
From uint64
To uint64
Message string
Lang string
ToRead bool
Time time.Time `sql:"default:(now() at time zone 'utc')"`
}
// GetTO returns its Transfer Object
func (p *Pm) GetTO(users ...*User) *PmTO {
if len(users) != 1 {
panic("Pm.GetTO requires a user parameter")
}
user := users[0]
var fromInfo, toInfo *InfoTO
if from, e := NewUser(p.From); e == nil {
fromInfo = from.Info().GetTO()
}
if to, e := NewUser(p.To); e == nil {
toInfo = to.Info().GetTO()
}
return &PmTO{
original: p,
Pmid: p.Pmid,
FromInfo: fromInfo,
ToInfo: toInfo,
Message: p.Message,
Lang: p.Lang,
ToRead: p.ToRead,
Time: p.Time,
Timestamp: p.Time.Unix(),
CanDelete: user.CanDelete(p),
CanEdit: user.CanEdit(p),
}
}
// TableName returns the table name associated with the structure
func (Pm) TableName() string {
return "pms"
}
// Project is the model for the relation groups
type Project struct {
Counter uint64 `igor:"primary_key"`
Description string
Name string
Private bool
Photo sql.NullString
Website sql.NullString
Goal string
Visible bool
Open bool
CreationTime time.Time `sql:"default:(now() at time zone 'utc')"`
}
// GetTO returns its Transfer Object
func (p *Project) GetTO(users ...*User) *ProjectTO {
return &ProjectTO{
original: p,
Counter: p.Counter,
Description: p.Description,
Name: p.Name,
Private: p.Private,
Photo: p.Photo,
Website: p.Website,
Goal: p.Goal,
Visible: p.Visible,
Open: p.Open,
CreationTime: p.CreationTime,
}
}
// TableName returns the table name associated with the structure
func (Project) TableName() string {
return "groups"
}
// ProjectMember is the model for the relation groups_members
type ProjectMember struct {
From uint64
To uint64
Time time.Time `sql:"default:(now() at time zone 'utc')"`
ToNotify bool
Counter uint64 `igor:"primary_key"`
}
// GetTO returns its Transfer Object
func (m *ProjectMember) GetTO(users ...*User) *ProjectMemberTO {
var fromInfo, toInfo *InfoTO
if from, e := NewUser(m.From); e == nil {
fromInfo = from.Info().GetTO()
}
if to, e := NewProject(m.To); e == nil {
toInfo = to.Info().GetTO()
}
return &ProjectMemberTO{
original: m,
FromInfo: fromInfo,
ToInfo: toInfo,
Time: m.Time,
Timestamp: m.Time.Unix(),
ToNotify: m.ToNotify,
Counter: m.Counter,
}
}
// TableName returns the table name associated with the structure
func (ProjectMember) TableName() string {
return "groups_members"
}
// ProjectOwner is the model for the relation groups_owners
type ProjectOwner struct {
From uint64
To uint64
Time time.Time `sql:"default:(now() at time zone 'utc')"`
ToNotify bool
Counter uint64 `igor:"primary_key"`
}
// GetTO returns its Transfer Object
func (o *ProjectOwner) GetTO(users ...*User) *ProjectOwnerTO {
var fromInfo, toInfo *InfoTO
if from, e := NewUser(o.From); e == nil {
fromInfo = from.Info().GetTO()
}
if to, e := NewProject(o.To); e == nil {
toInfo = to.Info().GetTO()
}
return &ProjectOwnerTO{
original: o,
FromInfo: fromInfo,
ToInfo: toInfo,
Time: o.Time,
Timestamp: o.Time.Unix(),
ToNotify: o.ToNotify,
Counter: o.Counter,
}
}
// TableName returns the table name associated with the structure
func (ProjectOwner) TableName() string {
return "groups_owners"
}
// ProjectPost is the model for the relation groups_posts
type ProjectPost struct {
Post
}
// TableName returns the table name associated with the structure
func (ProjectPost) TableName() string {
return "groups_posts"
}
// GetTO returns its Transfer Object
func (p *ProjectPost) GetTO(users ...*User) *PostTO {
if len(users) != 1 {
panic("ProjectPost.GetTO requires a user parameter")
}
user := users[0]
postTO := p.Post.GetTO()
postTO.Type = ProjectBoardID
if from, e := NewUser(p.From); e == nil {
postTO.FromInfo = from.Info().GetTO()
}
if to, e := NewProject(p.To); e == nil {
postTO.ToInfo = to.Info().GetTO()
}
postTO.Rate = p.VotesCount()
postTO.RevisionsCount = p.RevisionsNumber()
postTO.CommentsCount = p.CommentsCount()
postTO.BookmarksCount = p.BookmarksCount()
postTO.LurkersCount = p.LurkersCount()
postTO.Timestamp = p.Time.Unix()
postTO.URL = p.URL().String()
postTO.CanBookmark = user.CanBookmark(p)
postTO.CanComment = user.CanComment(p)
postTO.CanDelete = user.CanDelete(p)
postTO.CanEdit = user.CanEdit(p)
postTO.CanLurk = user.CanLurk(p)
return postTO
}
// ProjectPostRevision is the model for the relation groups_posts_revisions
type ProjectPostRevision struct {
Hpid uint64
Message string
Time time.Time `sql:"default:(now() at time zone 'utc')"`