-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontributions.js
271 lines (255 loc) · 9.16 KB
/
contributions.js
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
const { Pool } = require("pg");
const i18n = require('i18n');
// Assuming pool is already configured and exported from another module
const pool = new Pool({
user: process.env.PG_USER,
host: process.env.PG_HOST,
database: process.env.PG_DATABASE,
password: process.env.PG_PASSWORD,
port: process.env.PG_PORT,
ssl: { rejectUnauthorized: process.env.PG_SSL_REJECT_UNAUTHORIZED === "true" },
});
async function getContribution(req, res) {
const { lang, problemName } = req.params;
i18n.setLocale(res, lang);
try {
// Get current user's profile picture if logged in
let profilePictureCurrent = null;
if (req.session.userId) {
const currentUserResult = await pool.query(
"SELECT profile_picture FROM users WHERE id = $1",
[req.session.userId]
);
profilePictureCurrent = currentUserResult.rows[0]?.profile_picture;
}
const result = await pool.query(
`SELECT
c.*,
u.username,
u.full_name,
CASE
WHEN c.user_id IS NULL THEN c.ip_address
ELSE u.username
END as editor_identifier,
CASE
WHEN c.user_id IS NULL THEN c.ip_address
ELSE u.full_name
END as editor_name,
c.caption,
c.commit
FROM (
SELECT
id, user_id, edited_at, problem_name, language,
original_content, new_content, ip_address, content_changed,
NULL as caption, NULL as commit, coauthors
FROM contributions
UNION ALL
SELECT
id, user_id, edited_at, problem_name, language,
original_content, new_content, NULL as ip_address,
false as content_changed, caption, commit, coauthors
FROM github_contributions
) c
LEFT JOIN users u ON c.user_id = u.id
WHERE c.id = $1`,
[problemName]
);
if (result.rows.length === 0) {
return res.status(404).render("404", {
__: i18n.__,
pageUrl: req.originalUrl,
lang
});
}
const contribution = result.rows[0];
const originalLines = contribution.original_content.split('\n');
const newLines = contribution.new_content.split('\n');
const changes = [];
// Calculate changes
for (let i = 0; i < Math.max(originalLines.length, newLines.length); i++) {
if (originalLines[i] !== newLines[i]) {
if (originalLines[i] && !newLines[i]) {
changes.push({ type: 'removed', line: originalLines[i], lineNumber: i + 1 });
} else if (!originalLines[i] && newLines[i]) {
changes.push({ type: 'added', line: newLines[i], lineNumber: i + 1 });
} else {
changes.push({ type: 'modified', line: newLines[i], lineNumber: i + 1 });
}
}
}
// Render the EJS template with the diff HTML
res.render("contribution", {
__: i18n.__,
lang,
contribution: {
...result.rows[0],
isAnonymous: !result.rows[0].user_id
},
usernameCurrent: req.session.username,
userIdCurrent: req.session.userId,
profilePictureCurrent,
newContent: contribution.new_content,
originalContent: contribution.original_content,
changes, // Pass the changes array to the template
formatDate: (date) => {
return new Date(date).toLocaleString(lang === 'ru' ? 'ru-RU' : 'en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
});
}
});
} catch (error) {
console.error("Error fetching contribution:", error);
res.status(500).send("Error fetching contribution details");
}
}
async function getContributionsByUserId(userId, limit, offset) {
try {
const result = await pool.query(
`SELECT
c.*,
u.username,
u.full_name
FROM (
SELECT
id, user_id, edited_at, problem_name, language, original_content, new_content, NULL::text AS ip_address, false AS content_changed
FROM github_contributions
UNION ALL
SELECT
id, user_id, edited_at, problem_name, language, original_content, new_content, ip_address, content_changed
FROM contributions
) c
LEFT JOIN users u ON c.user_id = u.id
WHERE c.user_id = $1
ORDER BY c.edited_at DESC
LIMIT $2 OFFSET $3`,
[userId, limit, offset]
);
return result.rows;
} catch (error) {
console.error("Error fetching contributions by user ID:", error);
throw error;
}
}
async function getTotalContributions(userId) {
const result = await pool.query(
`
SELECT COUNT(*) AS total_contributions
FROM (
SELECT
id, user_id, edited_at, problem_name, language, original_content, new_content, NULL::text AS ip_address, false AS content_changed
FROM github_contributions
UNION ALL
SELECT
id, user_id, edited_at, problem_name, language, original_content, new_content, ip_address, content_changed
FROM contributions
) c
WHERE c.user_id = $1
`,
[userId]
);
return result.rows[0].total_contributions;
}
async function getUniqueContributions(userId) {
const result = await pool.query(
`
SELECT COUNT(*) AS total_contributions
FROM (
SELECT
id, user_id, edited_at, problem_name, language, original_content, new_content, NULL::text AS ip_address, false AS content_changed
FROM github_contributions
UNION ALL
SELECT
id, user_id, edited_at, problem_name, language, original_content, new_content, ip_address, content_changed
FROM contributions
) c
WHERE c.user_id = $1
`,
[userId]
);
return result.rows[0].total_contributions;
}
async function getUniqueSolutions(userId) {
const result = await pool.query(
`
SELECT COUNT(DISTINCT problem_name) AS unique_solutions
FROM (
SELECT
problem_name
FROM github_contributions
WHERE user_id = $1
UNION ALL
SELECT
problem_name
FROM contributions
WHERE user_id = $1
) c
`,
[userId]
);
return result.rows[0].unique_solutions;
}
async function getTranslations(userId) {
const result = await pool.query(
`
SELECT COUNT(*) AS unique_translations
FROM (
(
SELECT problem_name
FROM github_contributions
WHERE user_id = $1 AND language = 'ru'
UNION ALL
SELECT problem_name
FROM contributions
WHERE user_id = $1 AND language = 'ru'
)
INTERSECT
(
SELECT problem_name
FROM github_contributions
WHERE user_id = $1 AND language = 'en'
UNION ALL
SELECT problem_name
FROM contributions
WHERE user_id = $1 AND language = 'en'
)
) c
`,
[userId]
);
return result.rows[0].unique_translations;
}
async function getFrequentCollaborators(userId) {
const result = await pool.query(
`
SELECT
u.id AS collaborator_id,
u.username AS collaborator_username,
COUNT(*) AS collaboration_count
FROM (
SELECT
c1.problem_name,
c2.user_id AS collaborator_id
FROM contributions c1
JOIN contributions c2 ON c1.problem_name = c2.problem_name AND c1.user_id <> c2.user_id
WHERE c1.user_id = $1
UNION ALL
SELECT
gc1.problem_name,
gc2.user_id AS collaborator_id
FROM github_contributions gc1
JOIN github_contributions gc2 ON gc1.problem_name = gc2.problem_name AND gc1.user_id <> gc2.user_id
WHERE gc1.user_id = $1
) collaborations
JOIN users u ON collaborations.collaborator_id = u.id
GROUP BY u.id, u.username
ORDER BY collaboration_count DESC
`,
[userId]
);
return result.rows;
}
module.exports = { getContribution, getContributionsByUserId, getTotalContributions, getUniqueContributions, getUniqueSolutions, getTranslations, getFrequentCollaborators };