/
messages.go
executable file
·138 lines (117 loc) · 3.57 KB
/
messages.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
/*
Copyright (C) 2016-2020 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 (
"html"
"net/url"
"github.com/galeone/igor"
)
// Type definitions for [comment, post, pm]
// newMessage is the interface that wraps methods common to every new mesage
// Implementations: (UserPost, ProjectPost, UserPostComment, ProjectPostComment, Pm)
type newMessage interface {
SetSender(uint64)
SetReference(uint64)
SetText(string)
SetLanguage(string) error
ClearDefaults()
}
// Reference represents a reference.
// A comment refers to a user/project post
// A post, refers to a user/project board
type Reference interface {
ID() uint64
Language() string
}
type userReferenceRelation interface {
Sender() *User
NumericSender() uint64
Reference() Reference
NumericReference() uint64
}
// Bookmark is a generic interface to represent a bookmark
type Bookmark interface {
userReferenceRelation
}
// Vote is a generic interface to represent a vote
type Vote interface {
userReferenceRelation
Value() int8
}
// Lurk is a generic interface to represent the Lurk action
type Lurk interface {
userReferenceRelation
}
// Lock is a generic interface to represent the Lock action
type Lock interface {
userReferenceRelation
}
// The existingMessage interface represents a generic existing message
type existingMessage interface {
igor.DBModel
Reference
Sender() *User
NumericSender() uint64
Reference() Reference
NumericReference() uint64
Text() string
IsEditable() bool
NumericOwners() []uint64
Owners() []*User
Revisions() []string
RevisionsNumber() uint8
VotesCount() int
Votes() *[]Vote
}
// editingMessage interface represents a message while is edited
type editingMessage interface {
newMessage
existingMessage
}
// ExistingPost is the interface that wraps the methods common to every existing post
type ExistingPost interface {
existingMessage
Comments(CommentlistOptions) *[]ExistingComment
CommentsCount() uint8
NumericBookmarkers() []uint64
Bookmarkers() []*User
BookmarksCount() uint8
Bookmarks() *[]Bookmark
NumericLurkers() []uint64
Lurkers() []*User
LurkersCount() uint8
Lurks() *[]Lurk
URL() *url.URL
IsClosed() bool
NumericType() uint8
Type() string
}
// ExistingComment is the interface that wraps the methods common to every existing comment
type ExistingComment interface {
existingMessage
Post() (ExistingPost, error)
}
// Helper functions
// createMessage is an helper function. It's used to Init a new message structure
func createMessage(message newMessage, sender, reference uint64, text, language string) error {
message.ClearDefaults()
message.SetSender(sender)
message.SetReference(reference)
message.SetText(html.EscapeString(text))
return message.SetLanguage(language)
}
// updateMessage is an helper function. It's used to update a message (requires an editingMessage)
func updateMessage(message editingMessage) error {
return createMessage(message, message.NumericSender(), message.NumericReference(), message.Text(), message.Language())
}