This repository has been archived on 2025-12-28. You can view files and clone it, but cannot push or open issues or pull requests.
inkletblot-forum-com-v1/scripts/forum.controller.js

649 lines
22 KiB
JavaScript

daddy.controller("forumCtrlr", function($scope, $http, $location) {
$scope.titles = {
main : {
heading : "Welcome!",
},
recent : {
heading : "Recent Posts",
list : "Catergory",
last : "Last Topic"
}
}
$scope.messages = {
welcome : "Hello one and all, please ignore the unfinished state of \
this forum, it is a work in progress. However, all offered \
features sould currently work as expected.",
context : "This forum is a personal project that is being used as a \
learning tool to explore all sorts of things, from node \
development to hosting containers, created using Docker, \
on a small Odroid-MC1 and N2 kubernetes cluster."
}
$scope.errors = {
topic : {
show : false,
text : "None yet!"
},
catagory : {
show : false,
text: "None yet!"
}
}
$scope.getCategories = () => {
console.log("getting categories form server")
$http({
url : server + "/forum/category/all",
method : "GET"
}).then((res) => {
if (res.status == 200) {
if (res.data.length == 0) {
$scope.errors.catagory.show = true
} else {
$scope.categories = res.data
for (let i = 0; i < res.data.length; i++) {
$http({
url : server + "/forum/category/topic/last?cat=" + res.data[i].catNo,
method : "GET"
}).then((res) => {
if (res.status == 200) {
console.log("topicNo: " + res.data.topicNo)
// if (res.data.topicNo == null) {
// $scope.errors.topic.show = true
// } else {
res.data.topicDate = niceDate(res.data.topicDate)
$scope.categories[i].lastTopic = res.data
// }
} else {
console.log("server has encountered an error: " + res.status)
}
})
}
console.log(res)
}
}
}).catch((res) => {
console.log("server has encountered an error: " + res.status)
})
}
$scope.setCat = (catNo) => {
$location.path("/categories/"+catNo)
}
$scope.setTopic = (topicNo) => {
$location.path("/topics/"+topicNo)
}
})
daddy.controller("createTopicCtrlr", function($scope, $location, $http, stateData) {
$scope.titles = {
form : "Create a topic!"
}
$scope.status = {
show : false,
text : ""
}
let error = {
loggedIn : {
show : false,
text1 : "You must be",
link1 : "Logged In",
text2 : "to create a topic!"
},
userLevel : {
show : false,
text1 : "No categories have been created, please contact an administrator about creating some.",
text2 : "Administrator contact: solomonlaing@inkletblot.com"
},
category : {
show : false
}
}
$scope.error = error
$scope.start = () => {
if (stateData.state.user.userName == null) {
console.log("not logged in.")
error.loggedIn.show = true
}
if (stateData.state.user.userLevel == 0) {
console.log("user not an admin.")
error.userLevel.show = true
}
$scope.getCategories()
}
$scope.getCategories = () => {
console.log("getting categories form server")
$http({
url : server + "/forum/category/all",
method : "GET"
}).then((res) => {
if (res.status == 200) {
if (res.data.length == 0) {
$scope.errors.catagory.show = true
} else {
$scope.categories = res.data
console.log(res)
}
}
}).catch((res) => {
console.log("server has encountered an error: " + res.status)
})
}
$scope.setLogin = () => {
$location.path("/login")
stateData.setPage("things")
}
$scope.createTopic = () => {
console.log("sending new topic to server!!")
let formData = new FormData()
formData.append('user', stateData.state.user.userName)
formData.append('sessionID', stateData.state.user.sessionID)
formData.append('topicSubject', $scope.topic.topicSubject)
formData.append('topicCat', $scope.topic.topicCat)
formData.append('userNo', stateData.state.user.userNo)
$http({
url : server + "/forum/topic/create",
method : "POST",
data : formData,
headers : { 'Content-Type' : undefined },
transformRequest : angular.identity
}).then((res) => {
if (res.status == 200) {
console.log("topic submitted")
$scope.createPost($scope.topic.postContent, res.data.topicNo)
}
}).catch((res) => {
if (res.status == 500) {
console.log("something went wrong: " + res.data)
$scope.status.text = "Something went wrong: " + res.status
} else if (res.status == 403) {
console.log("something went worng: " + res.data)
$scope.status.text = "Authentication failed. For saftey reasons, you have been logged out, please log in and try again."
stateData.logOut()
}
})
}
$scope.createPost = (postContent, topicNo) => {
console.log("sending new post to server")
let formData = new FormData()
formData.append('user', stateData.state.user.userName)
formData.append('sessionID', stateData.state.user.sessionID)
formData.append('postContent', postContent)
formData.append('topicNo', topicNo)
formData.append('userNo', stateData.state.user.userNo)
$http({
url : server + "/forum/topic/post/create",
method : "POST",
data : formData,
headers : { 'Content-Type' : undefined },
transformRequest : angular.identity
}).then((res) => {
if (res.status == 200) {
console.log("post added")
$location.path("/topics/" + topicNo)
}
}).catch((res) => {
if (res.status == 500) {
console.log("something went wrong: " + res.data)
$scope.status.text = "Something went wrong: " + res.status
} else if (res.status == 403) {
console.log("something went worng: " + res.data)
$scope.status.text = "Authentication failed. For saftey reasons, you have been logged out, please log in and try again."
stateData.logOut()
}
})
}
})
daddy.controller("createCategoryCtrlr", function($scope, $http, $location, stateData) {
let error = {
loggedIn : {
show : false,
text1 : "You must be",
link1 : "Logged In",
text2 : "to create a category!"
},
userLevel : {
show : false,
text1 : "You must be an administrator to create a category!"
}
}
$scope.error = error
$scope.titles = {
form : "Create a Category!"
}
$scope.status = {
show : false,
text : ""
}
$scope.start = () => {
if (stateData.state.user.userName == null) {
error.loggedIn.show = true
}
if (stateData.state.user.userLevel == 0) {
error.userLevel.show = true
}
}
$scope.setLogin = () => {
$location.path("/login")
stateData.setPage("things")
}
$scope.createCategory = () => {
$scope.status.show = true
$scope.status.text = "Adding category..."
let formData = new FormData()
formData.append('user', stateData.state.user.userName)
formData.append('sessionID', stateData.state.user.sessionID)
formData.append('catName', $scope.category.catName)
formData.append('catDescr', $scope.category.catDescr)
console.log("sending to server...")
$http({
url : server + "/forum/category/create",
method : "POST",
data : formData,
headers : { 'Content-Type' : undefined },
transformRequest : angular.identity
}).then((res) => {
if (res.status == 200) {
console.log("category submitted")
$location.path("/categories/" + res.data.catNo)
}
}).catch((res) => {
if (res.status == 500) {
console.log("something went wrong: " + res.data)
$scope.status.text = "Something went wrong: " + res.status
} else if (res.status == 403) {
console.log("something went worng: " + res.data)
$scope.status.text = "Authentication failed. For saftey reasons, you have been logged out, please log in and try again."
stateData.logOut()
}
})
}
})
daddy.controller("topicsCtrlr", function($scope, $routeParams, $timeout, $http, $location, stateData) {
$scope.error = false
$scope.message = {
start : null,
link : "Go Back!"
}
$scope.logged = {
text1 : "You must be",
link1 : "logged in",
text2 : "to reply. You can also",
link2 : "sign up",
text3 : "for an account."
}
$scope.status = {
show : false,
text : "",
}
$scope.first = {
show : false,
text : "Be the first to create a post, write below..."
}
$scope.getTopic = () => {
console.log("getting topic")
$http({
url : server + "/forum/topic?topic=" + $routeParams.topicNo,
method : "GET"
}).then((res) => {
console.log("got something")
if (res.status == 200) {
let topic = res.data
// if (res.data.posts.length == 0) {
// $scope.first.show = true
// }
console.log("got topics!")
for (let i = 0; i < topic.posts.length; i++) {
topic.posts[i].postDate = niceDate(topic.posts[i].postDate)
}
$scope.topic = topic
}
}).catch((res) => {
if (res.status == 404) {
console.log(res.data)
$scope.error = true
$scope.message.start = "No such topic,"
} else if (res.status == 500) {
console.log(res.data)
$scope.error = true
$scope.message.start = "An error has occured: " + res.status
} else {
console.log(res)
}
})
}
$scope.makeReply = (topicNo) => {
console.log("sending new post to server")
let formData = new FormData()
formData.append('user', stateData.state.user.userName)
formData.append('sessionID', stateData.state.user.sessionID)
formData.append('postContent', $scope.reply.postContent)
formData.append('topicNo', topicNo)
formData.append('userNo', stateData.state.user.userNo)
$http({
url : server + "/forum/topic/post/create",
method : "POST",
data : formData,
headers : { 'Content-Type' : undefined },
transformRequest : angular.identity
}).then((res) => {
if (res.status == 200) {
console.log("post added")
$scope.reply.postContent = ""
$timeout(function() { $scope.getTopic() }, 2000)
}
}).catch((res) => {
if (res.status == 500) {
console.log("something went wrong: ")
console.log(res.data)
$scope.status.text = "Something went wrong: " + res.status
} else if (res.status == 403) {
console.log("something went worng: " + res.data)
$scope.status.text = "Authentication failed. For saftey reasons, you have been logged out, please log in and try again."
stateData.logOut()
}
})
}
$scope.setLogin = () => {
$location.path("/login")
}
$scope.setSignup = () => {
$location.path("/signup")
}
$scope.deletePost = (postNo) => {
if (!window.confirm("Are you sure you want to delete this post?")) {
return
}
let formData = new FormData()
formData.append('user', stateData.state.user.userName)
formData.append('sessionID', stateData.state.user.sessionID)
formData.append("postNo", postNo)
$http({
url : server + "/forum/topic/post/delete",
method : "POST",
data : formData,
headers : { 'Content-Type' : undefined },
transformRequest : angular.identity
}).then((res) => {
if (res.status == 200) {
console.log("post deleted")
for (let i = 0; i < $scope.topic.posts.length; i++) {
if ($scope.topic.posts[i].postNo == postNo) {
$scope.topic.posts[i].postContent = "[deleted]"
}
}
}
}).catch((res) => {
if (res.status == 500) {
console.log("something went wrong: ")
console.log(res.data)
$scope.status.text = "Something went wrong: " + res.status
} else if (res.status == 403) {
console.log("something went worng: " + res.data)
$scope.status.text = "Authentication failed. For saftey reasons, you have been logged out, please log in and try again."
stateData.logOut()
}
})
}
$scope.editPost = (postNo, postContent) => {
/* This is incomplete, need to figure out hiding the edit box after edit. Would rather not use timeout. */
console.log("submitting edited post")
let formData = new FormData()
formData.append('user', stateData.state.user.userName)
formData.append('sessionID', stateData.state.user.sessionID)
formData.append("postNo", postNo)
formData.append("postContent", postContent)
$http({
url : server + "/forum/topic/post/edit",
method : "POST",
data : formData,
headers : { 'Content-Type' : undefined },
transformRequest : angular.identity
}).then((res) => {
if (res.status == 200) {
console.log("post edited")
for (let i = 0; i < $scope.topic.posts.length; i++) {
if ($scope.topic.posts[i].postNo == postNo) {
$scope.topic.posts[i].postContent = postContent + " [edited]"
}
}
}
}).catch((res) => {
if (res.status == 500) {
console.log("something went wrong: ")
console.log(res.data)
$scope.status.text = "Something went wrong: " + res.status
} else if (res.status == 403) {
console.log("something went worng: " + res.data)
$scope.status.text = "Authentication failed. For saftey reasons, you have been logged out, please log in and try again."
stateData.logOut()
}
})
}
})
daddy.controller("topicCtrlr", function($scope, $http, $location, stateData) {
$scope.titles = {
heading : "Topics",
subtitle : "Most recent topics by category.",
list : "Topic",
last : "Category"
}
$scope.errors = {
error : {
show : false,
message : null
},
topic : {
show : false,
text : "None yet!"
}
}
$scope.getTopics = () => {
console.log("getting topics")
$http({
url : server + "/forum/topic/all",
method : "GET"
}).then((res) => {
if (res.status == 200) {
let topics = res.data
if (topics.length == 0) {
$scope.erros.topic.show = true
} else if (topics.length > 10) {
topics = topics.slice(0, 9)
}
for (let i = 0; i < topics.length; i++) {
topics[i].topicDate = niceDate(topics[i].topicDate)
}
$scope.topics = topics
}
}).catch((res) => {
if (res.status == 500) {
console.log(res.data)
$scope.errors.error.show = true
$scope.errors.error.start = "An error has occured: " + res.status
} else {
console.log(res)
}
})
}
$scope.setCat = (catNo) => {
$location.path("/categories/"+catNo)
}
$scope.setTopic = (topicNo) => {
$location.path("/topics/"+topicNo)
}
})
daddy.controller("categoriesCtrlr", function($scope, $http, $routeParams, $location, stateData) {
$scope.errors = {
exists : {
show : false,
text1 : "No such category,",
link1 : "Go Back!"
},
empty : {
show : false,
text : "No topics have been created yet..."
}
}
$scope.titles = {
topic : "topic",
date : "date"
}
$scope.getCat = () => {
console.log("getting category")
$http({
url : server + "/forum/category?cat=" + $routeParams.catNo,
method : "GET"
}).then((res) => {
console.log(res)
if (res.status == 200) {
let category = res.data
if (category.topics.length == 0) {
$scope.errors.empty.show = true
} else {
for (let i = 0; i < category.topics.length; i++) {
category.topics[i].topicDate = niceDate(category.topics[i].topicDate)
}
}
$scope.category = category
}
}).catch((res) => {
if (res.status == 404) {
console.log(res.data)
$scope.errors.exists.show = true
} else if (res.status == 500) {
console.log(res.data)
} else {
console.log(res)
}
})
}
$scope.setTopic = (topicNo) => {
$location.path("/topics/"+topicNo)
}
$scope.setForum = () => {
$location.path("/")
}
})
daddy.controller("categoryCtrlr", function($scope, $http, $location, stateData) {
$scope.titles = {
heading : "Categories",
subtitle : "Most recent categories and topics.",
list : "Catergory",
last : "Last Topic"
}
$scope.errors = {
topic : {
show : false,
text : "None yet!"
},
catagory : {
show : false,
text: "None yet!"
}
}
$scope.getCategories = () => {
console.log("getting categories form server")
$http({
url : server + "/forum/category/all",
method : "GET"
}).then((res) => {
if (res.status == 200) {
if (res.data.length == 0) {
$scope.errors.catagory.show = true
} else {
$scope.categories = res.data
for (let i = 0; i < res.data.length; i++) {
$http({
url : server + "/forum/category/topic/last?cat=" + res.data[i].catNo,
method : "GET"
}).then((res) => {
if (res.status == 200) {
console.log("topicNo: " + res.data.topicNo)
// if (res.data.topicNo == null) {
// $scope.errors.topic.show = true
// } else {
res.data.topicDate = niceDate(res.data.topicDate)
$scope.categories[i].lastTopic = res.data
// }
} else {
console.log("server has encountered an error: " + res.status)
}
})
}
console.log(res)
}
}
}).catch((res) => {
console.log("server has encountered an error: " + res.status)
})
}
$scope.setCat = (catNo) => {
$location.path("/categories/"+catNo)
}
$scope.setTopic = (topicNo) => {
$location.path("/topics/"+topicNo)
}
})
niceDate = (string) => {
let date = new Date(string)
return "on " + date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate() + " at " + date.getHours() + ":" + date.getMinutes()
}
isNum = (str) => {
let re = /^[0-9]+$/
return re.test(String(str).toLowerCase())
}