|
| 1 | +#!node_modules/.bin/babel-node |
| 2 | + |
| 3 | +import request from 'superagent' |
| 4 | +import bluebird from 'bluebird' |
| 5 | + |
| 6 | +global.Promise = bluebird; |
| 7 | +global.Promise.onPossiblyUnhandledRejection((e) => { throw e; }); |
| 8 | + |
| 9 | +import { load as configLoader } from '../config/config.js'; |
| 10 | + |
| 11 | +const config = configLoader() |
| 12 | + |
| 13 | +async function main() { |
| 14 | + const args = process.argv.slice(2); |
| 15 | + |
| 16 | + if (args.length !== 4) { |
| 17 | + process.stdout.write(`Usage: create_archive_user.js <freefeed_username> <friendfeed_username> <public or private> <password>\n`); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + const freefeed_username = args[0]; |
| 22 | + const friendfeed_username = args[1]; |
| 23 | + const password = args[3]; |
| 24 | + let is_private; |
| 25 | + |
| 26 | + if (args[2] === 'public') { |
| 27 | + is_private = 0; |
| 28 | + } else if (args[2] === 'private') { |
| 29 | + is_private = 1; |
| 30 | + } else { |
| 31 | + process.stdout.write(`Unexpected ${args[2]}, expected 'public' or 'private'\n`); |
| 32 | + process.exit(1); |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + const email = `freefeed.net+${freefeed_username}@gmail.com`; |
| 37 | + let auth_token = ``; |
| 38 | + let user_id = ``; |
| 39 | + |
| 40 | + |
| 41 | + process.stdout.write(`Creating a placeholder account @${freefeed_username} for @${friendfeed_username} comments from FriendFeed\n`); |
| 42 | + process.stdout.write(`Host: ${config.host}\n\n`); |
| 43 | + |
| 44 | + // Create user |
| 45 | + await request |
| 46 | + .post(`${config.host}/v1/users/sudo`) |
| 47 | + .send({ username: freefeed_username, password, email }) |
| 48 | + .then((res) => { |
| 49 | + auth_token = res.body.authToken; |
| 50 | + user_id = res.body.users.id; |
| 51 | + process.stdout.write(`User @${freefeed_username} created: ${user_id}\n\n`); |
| 52 | + }, (err) => { |
| 53 | + process.stdout.write(`Failed to create user: @${freefeed_username}\n`); |
| 54 | + process.stdout.write(`Status: ${err.response.status}\n`); |
| 55 | + process.stdout.write(`Text: ${err.response.text}\n`); |
| 56 | + process.exit(1); |
| 57 | + }); |
| 58 | + |
| 59 | + // Update description |
| 60 | + const description = `This is a placeholder account for @${friendfeed_username} comments from FriendFeed.com archives. Archives FAQ: https://dev.freefeed.net/w/archives-faq/`; |
| 61 | + await request |
| 62 | + .put(`${config.host}/v1/users/${user_id}`) |
| 63 | + .set(`X-Authentication-Token`, auth_token) |
| 64 | + .send({ user: { isPrivate: is_private, isProtected: is_private, description } }) |
| 65 | + .then(() => { |
| 66 | + process.stdout.write(`Description changed to:\n---\n${description}\n---\n\n`); |
| 67 | + }, (err) => { |
| 68 | + process.stdout.write(`Failed to update description for user: @${freefeed_username}\n`); |
| 69 | + process.stdout.write(`Status: ${err.response.status}\n`); |
| 70 | + process.stdout.write(`Text: ${err.response.text}\n`); |
| 71 | + process.exit(1); |
| 72 | + }); |
| 73 | + |
| 74 | + // Update privacy |
| 75 | + if (is_private) { |
| 76 | + await request |
| 77 | + .put(`${config.host}/v1/users/${user_id}`) |
| 78 | + .set(`X-Authentication-Token`, auth_token) |
| 79 | + .send({ user: { isPrivate: `1`, isProtected: `1` } }) |
| 80 | + .then(() => { |
| 81 | + process.stdout.write(`User feed is private\n`); |
| 82 | + }, (err) => { |
| 83 | + process.stdout.write(`Failed to update privacy settings for user: @${freefeed_username}\n`); |
| 84 | + process.stdout.write(`Status: ${err.response.status}\n`); |
| 85 | + process.stdout.write(`Text: ${err.response.text}\n`); |
| 86 | + process.exit(1); |
| 87 | + }); |
| 88 | + } else { |
| 89 | + process.stdout.write(`User feed is public\n`); |
| 90 | + } |
| 91 | + |
| 92 | + // Update profile pic |
| 93 | + await request |
| 94 | + .post(`${config.host}/v1/users/updateProfilePicture`) |
| 95 | + .set(`X-Authentication-Token`, auth_token) |
| 96 | + .attach(`image`, `bin/friendfeed.png`) |
| 97 | + .then(() => { |
| 98 | + process.stdout.write(`Profile picture updated\n`); |
| 99 | + }, (err) => { |
| 100 | + process.stdout.write(`Failed to update profile picture user: @${freefeed_username}\n`); |
| 101 | + process.stdout.write(`Status: ${err.response.status}\n`); |
| 102 | + process.stdout.write(`Text: ${err.response.text}\n`); |
| 103 | + process.exit(1); |
| 104 | + }); |
| 105 | + |
| 106 | + process.stdout.write(`\nArchive user @${freefeed_username} has been created successfully!\n`); |
| 107 | +} |
| 108 | + |
| 109 | +main() |
| 110 | + .then(() => { |
| 111 | + process.exit(0); |
| 112 | + }) |
| 113 | + .catch((e) => { |
| 114 | + process.stderr.write(e.message); |
| 115 | + process.exit(1); |
| 116 | + }); |
0 commit comments