💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › 6d65f1ff12baf83e4b23757084… captured on 2023-03-20 at 18:03:17. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-01-29)
-=-=-=-=-=-=-
0 package gmi
1
2 import (
3 "gemigit/db"
4 "github.com/pitr/gig"
5 )
6
7 func isGroupOwner(c gig.Context) (int, error) {
8 user, exist := db.GetUser(c.CertHash())
9 if !exist {
10 return -1, c.NoContent(gig.StatusBadRequest,
11 "Invalid username")
12 }
13 groupID, err := db.GetGroupID(c.Param("group"))
14 if err != nil {
15 return -1, c.NoContent(gig.StatusBadRequest, err.Error())
16 }
17 owner, err := user.IsInGroupID(groupID)
18 if err != nil {
19 return -1, c.NoContent(gig.StatusBadRequest, err.Error())
20 }
21 if !owner {
22 return -1, c.NoContent(gig.StatusBadRequest,
23 "Permission denied")
24 }
25 return groupID, nil
26 }
27
28 func SetGroupDesc(c gig.Context) error {
29 query, err := c.QueryString()
30 if err != nil {
31 return c.NoContent(gig.StatusBadRequest, err.Error())
32 }
33 if query == "" {
34 return c.NoContent(gig.StatusInput, "Description")
35 }
36
37 id, err := isGroupOwner(c)
38 if err != nil {
39 return err
40 }
41
42 err = db.SetGroupDescription(id, query)
43 if err != nil {
44 return err
45 }
46 return c.NoContent(gig.StatusRedirectTemporary,
47 "/account/groups/" + c.Param("group"))
48 }
49
50 func DeleteGroup(c gig.Context) error {
51 name, err := c.QueryString()
52 if err != nil {
53 return c.NoContent(gig.StatusBadRequest,
54 "Invalid input received")
55 }
56 if name == "" {
57 return c.NoContent(gig.StatusInput,
58 "To confirm type the group name")
59 }
60 if name != c.Param("group") {
61 return c.NoContent(gig.StatusRedirectTemporary,
62 "/account/groups/" + c.Param("group"))
63 }
64 id, err := isGroupOwner(c)
65 if err != nil {
66 return c.NoContent(gig.StatusBadRequest, err.Error())
67 }
68 err = db.DeleteGroup(id)
69 if err != nil {
70 return c.NoContent(gig.StatusBadRequest, err.Error())
71 }
72 return c.NoContent(gig.StatusRedirectTemporary, "/account/groups")
73 }
74
75 func LeaveGroup(c gig.Context) (error) {
76 user, exist := db.GetUser(c.CertHash())
77 if !exist {
78 return c.NoContent(gig.StatusBadRequest,
79 "Invalid username")
80 }
81 groupID, err := db.GetGroupID(c.Param("group"))
82 if err != nil {
83 return c.NoContent(gig.StatusBadRequest, err.Error())
84 }
85 owner, err := user.IsInGroupID(groupID)
86 if err != nil {
87 return c.NoContent(gig.StatusBadRequest, err.Error())
88 }
89 if owner {
90 return c.NoContent(gig.StatusBadRequest,
91 "You cannot leave your own group")
92 }
93 err = db.DeleteMember(user.ID, groupID)
94 if err != nil {
95 return c.NoContent(gig.StatusBadRequest, err.Error())
96 }
97 return c.NoContent(gig.StatusRedirectTemporary, "/account/groups")
98 }
99
100 func RmFromGroup(c gig.Context) (error) {
101 groupID, err := isGroupOwner(c)
102 if err != nil {
103 return c.NoContent(gig.StatusBadRequest, err.Error())
104 }
105 userID, err := db.GetUserID(c.Param("user"))
106 if err != nil {
107 return c.NoContent(gig.StatusBadRequest, err.Error())
108 }
109 user, exist := db.GetUser(c.CertHash())
110 if !exist {
111 return c.NoContent(gig.StatusBadRequest,
112 "Invalid username")
113 }
114 if userID == user.ID {
115 return c.NoContent(gig.StatusBadRequest,
116 "You cannot remove yourself from your own group")
117 }
118 err = db.DeleteMember(userID, groupID)
119 if err != nil {
120 return c.NoContent(gig.StatusBadRequest, err.Error())
121 }
122 return c.NoContent(gig.StatusRedirectTemporary,
123 "/account/groups/" + c.Param("group"))
124 }
125
126 func AddToGroup(c gig.Context) (error) {
127 query, err := c.QueryString()
128 if err != nil {
129 return c.NoContent(gig.StatusBadRequest, err.Error())
130 }
131 if query == "" {
132 return c.NoContent(gig.StatusInput, "Username")
133 }
134
135 user, exist := db.GetUser(c.CertHash())
136 if !exist {
137 return c.NoContent(gig.StatusBadRequest,
138 "Invalid username")
139 }
140
141 group := c.Param("group")
142 owner, err := user.IsInGroup(group)
143 if err != nil {
144 return c.NoContent(gig.StatusBadRequest, err.Error())
145 }
146 if !owner {
147 return c.NoContent(gig.StatusBadRequest, "Permission denied")
148 }
149
150 if err = db.AddUserToGroup(group, query); err != nil {
151 return c.NoContent(gig.StatusBadRequest, err.Error())
152 }
153 return c.NoContent(gig.StatusRedirectTemporary,
154 "/account/groups/" + group)
155 }
156